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 a807c50b 2023-07-05 thomas static mode_t
66 a807c50b 2023-07-05 thomas apply_umask(mode_t mode)
67 a807c50b 2023-07-05 thomas {
68 a807c50b 2023-07-05 thomas mode_t um;
69 a807c50b 2023-07-05 thomas
70 a807c50b 2023-07-05 thomas um = umask(000);
71 a807c50b 2023-07-05 thomas umask(um);
72 a807c50b 2023-07-05 thomas return mode & ~um;
73 a807c50b 2023-07-05 thomas }
74 86c3caaf 2018-03-09 stsp
75 99724ed4 2018-03-10 stsp static const struct got_error *
76 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
77 99724ed4 2018-03-10 stsp {
78 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
79 99724ed4 2018-03-10 stsp char *path;
80 99724ed4 2018-03-10 stsp
81 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
83 99724ed4 2018-03-10 stsp
84 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
85 99724ed4 2018-03-10 stsp free(path);
86 507dc3bb 2018-12-29 stsp return err;
87 507dc3bb 2018-12-29 stsp }
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp static const struct got_error *
90 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
91 507dc3bb 2018-12-29 stsp {
92 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
93 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
94 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
95 507dc3bb 2018-12-29 stsp char *path = NULL;
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
99 507dc3bb 2018-12-29 stsp path = NULL;
100 507dc3bb 2018-12-29 stsp goto done;
101 507dc3bb 2018-12-29 stsp }
102 507dc3bb 2018-12-29 stsp
103 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
104 507dc3bb 2018-12-29 stsp if (err)
105 507dc3bb 2018-12-29 stsp goto done;
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (content) {
108 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
109 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
111 507dc3bb 2018-12-29 stsp goto done;
112 507dc3bb 2018-12-29 stsp }
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp
115 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
116 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
117 2a57020b 2019-02-20 stsp unlink(tmppath);
118 507dc3bb 2018-12-29 stsp goto done;
119 507dc3bb 2018-12-29 stsp }
120 507dc3bb 2018-12-29 stsp
121 507dc3bb 2018-12-29 stsp done:
122 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
123 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
124 230a42bd 2019-05-11 jcs free(tmppath);
125 09fe317a 2018-03-11 stsp return err;
126 09fe317a 2018-03-11 stsp }
127 09fe317a 2018-03-11 stsp
128 024e9686 2019-05-14 stsp static const struct got_error *
129 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
130 024e9686 2019-05-14 stsp {
131 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
132 024e9686 2019-05-14 stsp char *refstr = NULL;
133 024e9686 2019-05-14 stsp
134 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
135 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
136 024e9686 2019-05-14 stsp if (refstr == NULL)
137 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
138 024e9686 2019-05-14 stsp } else {
139 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
140 024e9686 2019-05-14 stsp if (refstr == NULL)
141 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
142 024e9686 2019-05-14 stsp }
143 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 024e9686 2019-05-14 stsp free(refstr);
145 024e9686 2019-05-14 stsp return err;
146 024e9686 2019-05-14 stsp }
147 024e9686 2019-05-14 stsp
148 86c3caaf 2018-03-09 stsp const struct got_error *
149 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
150 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
151 86c3caaf 2018-03-09 stsp {
152 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
153 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
154 ec22038e 2019-03-10 stsp uuid_t uuid;
155 ec22038e 2019-03-10 stsp uint32_t uuid_status;
156 65596e15 2018-12-24 stsp int obj_type;
157 7ac97322 2018-03-11 stsp char *path_got = NULL;
158 1451e70d 2018-03-10 stsp char *formatstr = NULL;
159 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
160 65596e15 2018-12-24 stsp char *basestr = NULL;
161 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
162 65596e15 2018-12-24 stsp
163 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
165 0c48fee2 2019-03-11 stsp goto done;
166 0c48fee2 2019-03-11 stsp }
167 0c48fee2 2019-03-11 stsp
168 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
169 65596e15 2018-12-24 stsp if (err)
170 65596e15 2018-12-24 stsp return err;
171 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
172 65596e15 2018-12-24 stsp if (err)
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
176 86c3caaf 2018-03-09 stsp
177 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
178 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
180 0bb8a95e 2018-03-12 stsp }
181 577ec78f 2018-03-11 stsp
182 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
183 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 86c3caaf 2018-03-09 stsp
188 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
189 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
190 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
191 86c3caaf 2018-03-09 stsp goto done;
192 86c3caaf 2018-03-09 stsp }
193 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
195 86c3caaf 2018-03-09 stsp goto done;
196 86c3caaf 2018-03-09 stsp }
197 86c3caaf 2018-03-09 stsp
198 056e7441 2018-03-11 stsp /* Create an empty lock file. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 056e7441 2018-03-11 stsp if (err)
201 056e7441 2018-03-11 stsp goto done;
202 056e7441 2018-03-11 stsp
203 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
204 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 99724ed4 2018-03-10 stsp if (err)
206 86c3caaf 2018-03-09 stsp goto done;
207 86c3caaf 2018-03-09 stsp
208 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
209 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
210 65596e15 2018-12-24 stsp if (err)
211 65596e15 2018-12-24 stsp goto done;
212 65596e15 2018-12-24 stsp
213 65596e15 2018-12-24 stsp /* Record our base commit. */
214 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
215 65596e15 2018-12-24 stsp if (err)
216 65596e15 2018-12-24 stsp goto done;
217 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 99724ed4 2018-03-10 stsp if (err)
219 86c3caaf 2018-03-09 stsp goto done;
220 86c3caaf 2018-03-09 stsp
221 1451e70d 2018-03-10 stsp /* Store path to repository. */
222 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
224 99724ed4 2018-03-10 stsp if (err)
225 86c3caaf 2018-03-09 stsp goto done;
226 86c3caaf 2018-03-09 stsp
227 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
228 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
230 ec22038e 2019-03-10 stsp if (err)
231 ec22038e 2019-03-10 stsp goto done;
232 ec22038e 2019-03-10 stsp
233 ec22038e 2019-03-10 stsp /* Generate UUID. */
234 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
237 ec22038e 2019-03-10 stsp goto done;
238 ec22038e 2019-03-10 stsp }
239 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
241 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
242 ec22038e 2019-03-10 stsp goto done;
243 ec22038e 2019-03-10 stsp }
244 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 577ec78f 2018-03-11 stsp if (err)
246 577ec78f 2018-03-11 stsp goto done;
247 577ec78f 2018-03-11 stsp
248 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
249 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
251 1451e70d 2018-03-10 stsp goto done;
252 1451e70d 2018-03-10 stsp }
253 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
254 99724ed4 2018-03-10 stsp if (err)
255 1451e70d 2018-03-10 stsp goto done;
256 1451e70d 2018-03-10 stsp
257 86c3caaf 2018-03-09 stsp done:
258 65596e15 2018-12-24 stsp free(commit_id);
259 7ac97322 2018-03-11 stsp free(path_got);
260 1451e70d 2018-03-10 stsp free(formatstr);
261 0bb8a95e 2018-03-12 stsp free(absprefix);
262 65596e15 2018-12-24 stsp free(basestr);
263 ec22038e 2019-03-10 stsp free(uuidstr);
264 86c3caaf 2018-03-09 stsp return err;
265 86c3caaf 2018-03-09 stsp }
266 86c3caaf 2018-03-09 stsp
267 247140b2 2019-02-05 stsp const struct got_error *
268 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 e5dc7198 2018-12-29 stsp const char *path_prefix)
270 e5dc7198 2018-12-29 stsp {
271 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
272 e5dc7198 2018-12-29 stsp
273 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
274 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
276 e5dc7198 2018-12-29 stsp }
277 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
278 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
279 e5dc7198 2018-12-29 stsp free(absprefix);
280 e5dc7198 2018-12-29 stsp return NULL;
281 86c3caaf 2018-03-09 stsp }
282 86c3caaf 2018-03-09 stsp
283 bc70eb79 2019-05-09 stsp const char *
284 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
285 86c3caaf 2018-03-09 stsp {
286 36a38700 2019-05-10 stsp return worktree->head_ref_name;
287 024e9686 2019-05-14 stsp }
288 024e9686 2019-05-14 stsp
289 024e9686 2019-05-14 stsp const struct got_error *
290 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
291 024e9686 2019-05-14 stsp struct got_reference *head_ref)
292 024e9686 2019-05-14 stsp {
293 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
294 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
295 024e9686 2019-05-14 stsp
296 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
298 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
299 024e9686 2019-05-14 stsp path_got = NULL;
300 024e9686 2019-05-14 stsp goto done;
301 024e9686 2019-05-14 stsp }
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
304 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
305 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
306 024e9686 2019-05-14 stsp goto done;
307 024e9686 2019-05-14 stsp }
308 024e9686 2019-05-14 stsp
309 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
310 024e9686 2019-05-14 stsp if (err)
311 024e9686 2019-05-14 stsp goto done;
312 024e9686 2019-05-14 stsp
313 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
314 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
315 024e9686 2019-05-14 stsp done:
316 024e9686 2019-05-14 stsp free(path_got);
317 024e9686 2019-05-14 stsp if (err)
318 024e9686 2019-05-14 stsp free(head_ref_name);
319 024e9686 2019-05-14 stsp return err;
320 507dc3bb 2018-12-29 stsp }
321 507dc3bb 2018-12-29 stsp
322 b72f483a 2019-02-05 stsp struct got_object_id *
323 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
324 507dc3bb 2018-12-29 stsp {
325 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
326 86c3caaf 2018-03-09 stsp }
327 86c3caaf 2018-03-09 stsp
328 507dc3bb 2018-12-29 stsp const struct got_error *
329 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
331 507dc3bb 2018-12-29 stsp {
332 507dc3bb 2018-12-29 stsp const struct got_error *err;
333 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
334 507dc3bb 2018-12-29 stsp char *id_str = NULL;
335 507dc3bb 2018-12-29 stsp char *path_got = NULL;
336 507dc3bb 2018-12-29 stsp
337 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 507dc3bb 2018-12-29 stsp path_got = NULL;
341 507dc3bb 2018-12-29 stsp goto done;
342 507dc3bb 2018-12-29 stsp }
343 507dc3bb 2018-12-29 stsp
344 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
345 507dc3bb 2018-12-29 stsp if (err)
346 507dc3bb 2018-12-29 stsp return err;
347 507dc3bb 2018-12-29 stsp
348 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
350 507dc3bb 2018-12-29 stsp goto done;
351 507dc3bb 2018-12-29 stsp }
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp /* Record our base commit. */
354 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
355 507dc3bb 2018-12-29 stsp if (err)
356 507dc3bb 2018-12-29 stsp goto done;
357 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 507dc3bb 2018-12-29 stsp if (err)
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp
361 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
362 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
363 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
364 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
365 507dc3bb 2018-12-29 stsp goto done;
366 507dc3bb 2018-12-29 stsp }
367 507dc3bb 2018-12-29 stsp done:
368 507dc3bb 2018-12-29 stsp if (obj)
369 507dc3bb 2018-12-29 stsp got_object_close(obj);
370 507dc3bb 2018-12-29 stsp free(id_str);
371 507dc3bb 2018-12-29 stsp free(path_got);
372 507dc3bb 2018-12-29 stsp return err;
373 50b0790e 2020-09-11 stsp }
374 50b0790e 2020-09-11 stsp
375 50b0790e 2020-09-11 stsp const struct got_gotconfig *
376 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
377 50b0790e 2020-09-11 stsp {
378 50b0790e 2020-09-11 stsp return worktree->gotconfig;
379 507dc3bb 2018-12-29 stsp }
380 507dc3bb 2018-12-29 stsp
381 9d31a1d8 2018-03-11 stsp static const struct got_error *
382 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
383 86c3caaf 2018-03-09 stsp {
384 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
387 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
388 86c3caaf 2018-03-09 stsp return NULL;
389 21908da4 2019-01-13 stsp }
390 21908da4 2019-01-13 stsp
391 21908da4 2019-01-13 stsp static const struct got_error *
392 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
393 4a1ddfc2 2019-01-12 stsp {
394 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
395 4a1ddfc2 2019-01-12 stsp char *abspath;
396 4a1ddfc2 2019-01-12 stsp
397 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
398 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
399 4a1ddfc2 2019-01-12 stsp
400 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
401 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
402 ddcd8544 2019-03-11 stsp struct stat sb;
403 ddcd8544 2019-03-11 stsp err = NULL;
404 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
405 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
406 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
407 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
408 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
409 ddcd8544 2019-03-11 stsp }
410 ddcd8544 2019-03-11 stsp }
411 4a1ddfc2 2019-01-12 stsp free(abspath);
412 68c76935 2019-02-19 stsp return err;
413 68c76935 2019-02-19 stsp }
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp static const struct got_error *
416 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
417 68c76935 2019-02-19 stsp {
418 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
419 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
420 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
421 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
422 68c76935 2019-02-19 stsp
423 68c76935 2019-02-19 stsp *same = 1;
424 68c76935 2019-02-19 stsp
425 230a42bd 2019-05-11 jcs for (;;) {
426 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
427 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
429 68c76935 2019-02-19 stsp break;
430 68c76935 2019-02-19 stsp }
431 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
432 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
433 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
434 68c76935 2019-02-19 stsp break;
435 68c76935 2019-02-19 stsp }
436 68c76935 2019-02-19 stsp if (flen1 == 0) {
437 68c76935 2019-02-19 stsp if (flen2 != 0)
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
441 68c76935 2019-02-19 stsp if (flen1 != 0)
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
445 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
446 68c76935 2019-02-19 stsp *same = 0;
447 68c76935 2019-02-19 stsp break;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp } else {
450 68c76935 2019-02-19 stsp *same = 0;
451 68c76935 2019-02-19 stsp break;
452 68c76935 2019-02-19 stsp }
453 68c76935 2019-02-19 stsp }
454 68c76935 2019-02-19 stsp
455 68c76935 2019-02-19 stsp return err;
456 68c76935 2019-02-19 stsp }
457 68c76935 2019-02-19 stsp
458 68c76935 2019-02-19 stsp static const struct got_error *
459 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
460 68c76935 2019-02-19 stsp {
461 68c76935 2019-02-19 stsp struct stat sb;
462 68c76935 2019-02-19 stsp size_t size1, size2;
463 68c76935 2019-02-19 stsp
464 68c76935 2019-02-19 stsp *same = 1;
465 68c76935 2019-02-19 stsp
466 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
467 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
468 68c76935 2019-02-19 stsp size1 = sb.st_size;
469 68c76935 2019-02-19 stsp
470 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
471 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
472 68c76935 2019-02-19 stsp size2 = sb.st_size;
473 68c76935 2019-02-19 stsp
474 68c76935 2019-02-19 stsp if (size1 != size2) {
475 68c76935 2019-02-19 stsp *same = 0;
476 68c76935 2019-02-19 stsp return NULL;
477 68c76935 2019-02-19 stsp }
478 68c76935 2019-02-19 stsp
479 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
480 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
481 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
482 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
483 68c76935 2019-02-19 stsp
484 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
485 24f136e0 2021-11-20 thomas }
486 24f136e0 2021-11-20 thomas
487 24f136e0 2021-11-20 thomas static const struct got_error *
488 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
489 24f136e0 2021-11-20 thomas {
490 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
491 24f136e0 2021-11-20 thomas size_t flen;
492 24f136e0 2021-11-20 thomas ssize_t outlen;
493 24f136e0 2021-11-20 thomas
494 24f136e0 2021-11-20 thomas *outsize = 0;
495 24f136e0 2021-11-20 thomas
496 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
497 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
498 24f136e0 2021-11-20 thomas
499 24f136e0 2021-11-20 thomas for (;;) {
500 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
501 24f136e0 2021-11-20 thomas if (flen == 0) {
502 24f136e0 2021-11-20 thomas if (ferror(f))
503 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
504 24f136e0 2021-11-20 thomas if (feof(f))
505 24f136e0 2021-11-20 thomas break;
506 24f136e0 2021-11-20 thomas }
507 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
508 24f136e0 2021-11-20 thomas if (outlen == -1)
509 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
510 24f136e0 2021-11-20 thomas if (outlen != flen)
511 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
512 24f136e0 2021-11-20 thomas *outsize += outlen;
513 24f136e0 2021-11-20 thomas }
514 24f136e0 2021-11-20 thomas
515 24f136e0 2021-11-20 thomas return NULL;
516 24f136e0 2021-11-20 thomas }
517 24f136e0 2021-11-20 thomas
518 24f136e0 2021-11-20 thomas static const struct got_error *
519 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
520 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
521 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
522 24f136e0 2021-11-20 thomas const char *ondisk_path)
523 24f136e0 2021-11-20 thomas {
524 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
525 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
526 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
527 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
528 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
529 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
530 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
531 24f136e0 2021-11-20 thomas
532 24f136e0 2021-11-20 thomas *overlapcnt = 0;
533 24f136e0 2021-11-20 thomas
534 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
535 24f136e0 2021-11-20 thomas if (err)
536 24f136e0 2021-11-20 thomas return err;
537 24f136e0 2021-11-20 thomas
538 24f136e0 2021-11-20 thomas if (same_content)
539 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
540 24f136e0 2021-11-20 thomas
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
542 24f136e0 2021-11-20 thomas if (err)
543 24f136e0 2021-11-20 thomas return err;
544 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
545 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
546 24f136e0 2021-11-20 thomas if (err)
547 24f136e0 2021-11-20 thomas return err;
548 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
549 24f136e0 2021-11-20 thomas
550 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
551 24f136e0 2021-11-20 thomas *overlapcnt = 1;
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", 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 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
557 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
558 24f136e0 2021-11-20 thomas goto done;
559 24f136e0 2021-11-20 thomas }
560 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
561 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
562 24f136e0 2021-11-20 thomas goto done;
563 24f136e0 2021-11-20 thomas }
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
565 fc2a50f2 2022-11-01 thomas base_path_orig, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
569 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
570 24f136e0 2021-11-20 thomas if (err)
571 24f136e0 2021-11-20 thomas goto done;
572 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
573 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
574 24f136e0 2021-11-20 thomas if (err)
575 24f136e0 2021-11-20 thomas goto done;
576 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
577 24f136e0 2021-11-20 thomas if (err)
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
580 24f136e0 2021-11-20 thomas if (err)
581 24f136e0 2021-11-20 thomas goto done;
582 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
583 24f136e0 2021-11-20 thomas if (err)
584 24f136e0 2021-11-20 thomas goto done;
585 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
586 24f136e0 2021-11-20 thomas "merged automatically:\n") < 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 (dprintf(merged_fd, "%s%s%s\nfile %s\n",
591 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
592 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
593 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
594 24f136e0 2021-11-20 thomas path_deriv) < 0) {
595 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
596 24f136e0 2021-11-20 thomas goto done;
597 24f136e0 2021-11-20 thomas }
598 24f136e0 2021-11-20 thomas if (size_orig > 0) {
599 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
600 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
601 24f136e0 2021-11-20 thomas label_orig ? " " : "",
602 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
603 24f136e0 2021-11-20 thomas path_orig) < 0) {
604 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
605 24f136e0 2021-11-20 thomas goto done;
606 24f136e0 2021-11-20 thomas }
607 24f136e0 2021-11-20 thomas }
608 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
609 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
610 24f136e0 2021-11-20 thomas path_deriv2,
611 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
612 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
613 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
614 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
615 24f136e0 2021-11-20 thomas goto done;
616 24f136e0 2021-11-20 thomas }
617 24f136e0 2021-11-20 thomas } else if (changed_deriv)
618 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
619 24f136e0 2021-11-20 thomas else if (changed_deriv2)
620 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
621 24f136e0 2021-11-20 thomas done:
622 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
623 24f136e0 2021-11-20 thomas err == NULL)
624 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
625 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
626 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
627 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
628 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
629 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
630 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
631 24f136e0 2021-11-20 thomas free(path_orig);
632 24f136e0 2021-11-20 thomas free(path_deriv);
633 24f136e0 2021-11-20 thomas free(path_deriv2);
634 24f136e0 2021-11-20 thomas free(base_path_orig);
635 24f136e0 2021-11-20 thomas free(base_path_deriv);
636 24f136e0 2021-11-20 thomas free(base_path_deriv2);
637 24f136e0 2021-11-20 thomas return err;
638 6353ad76 2019-02-08 stsp }
639 6353ad76 2019-02-08 stsp
640 6353ad76 2019-02-08 stsp /*
641 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
642 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
643 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
644 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
645 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
646 6353ad76 2019-02-08 stsp */
647 6353ad76 2019-02-08 stsp static const struct got_error *
648 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
649 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
650 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
651 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
652 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
653 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
654 6353ad76 2019-02-08 stsp {
655 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
656 6353ad76 2019-02-08 stsp int merged_fd = -1;
657 db590691 2021-06-02 stsp FILE *f_merged = NULL;
658 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
659 234035bc 2019-06-01 stsp int overlapcnt = 0;
660 3524bbf9 2020-10-20 stsp char *parent = NULL;
661 6353ad76 2019-02-08 stsp
662 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
663 234035bc 2019-06-01 stsp
664 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
665 3524bbf9 2020-10-20 stsp if (err)
666 3524bbf9 2020-10-20 stsp return err;
667 af54ae4a 2019-02-19 stsp
668 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
669 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
670 3524bbf9 2020-10-20 stsp goto done;
671 3524bbf9 2020-10-20 stsp }
672 af54ae4a 2019-02-19 stsp
673 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
674 6353ad76 2019-02-08 stsp if (err)
675 6353ad76 2019-02-08 stsp goto done;
676 3b9f0f87 2020-07-23 stsp
677 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
678 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
679 24f136e0 2021-11-20 thomas if (err) {
680 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
681 24f136e0 2021-11-20 thomas goto done;
682 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
683 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
684 24f136e0 2021-11-20 thomas ondisk_path);
685 24f136e0 2021-11-20 thomas if (err)
686 24f136e0 2021-11-20 thomas goto done;
687 24f136e0 2021-11-20 thomas }
688 6353ad76 2019-02-08 stsp
689 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
690 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
691 1ee397ad 2019-07-12 stsp if (err)
692 1ee397ad 2019-07-12 stsp goto done;
693 6353ad76 2019-02-08 stsp
694 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
696 816dc654 2019-02-16 stsp goto done;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp
699 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
700 db590691 2021-06-02 stsp if (f_merged == NULL) {
701 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
702 db590691 2021-06-02 stsp goto done;
703 db590691 2021-06-02 stsp }
704 db590691 2021-06-02 stsp merged_fd = -1;
705 db590691 2021-06-02 stsp
706 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
707 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
708 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
709 db590691 2021-06-02 stsp f_merged);
710 68c76935 2019-02-19 stsp if (err)
711 68c76935 2019-02-19 stsp goto done;
712 816dc654 2019-02-16 stsp }
713 6353ad76 2019-02-08 stsp
714 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
715 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
716 70a0c8ec 2019-02-20 stsp goto done;
717 70a0c8ec 2019-02-20 stsp }
718 70a0c8ec 2019-02-20 stsp
719 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
720 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
721 230a42bd 2019-05-11 jcs ondisk_path);
722 6353ad76 2019-02-08 stsp goto done;
723 6353ad76 2019-02-08 stsp }
724 6353ad76 2019-02-08 stsp done:
725 fdcb7daf 2019-12-15 stsp if (err) {
726 fdcb7daf 2019-12-15 stsp if (merged_path)
727 fdcb7daf 2019-12-15 stsp unlink(merged_path);
728 3b9f0f87 2020-07-23 stsp }
729 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
730 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
731 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
732 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
733 6353ad76 2019-02-08 stsp free(merged_path);
734 af54ae4a 2019-02-19 stsp free(base_path);
735 3524bbf9 2020-10-20 stsp free(parent);
736 af57b12a 2020-07-23 stsp return err;
737 af57b12a 2020-07-23 stsp }
738 af57b12a 2020-07-23 stsp
739 af57b12a 2020-07-23 stsp static const struct got_error *
740 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
741 af57b12a 2020-07-23 stsp size_t target_len)
742 af57b12a 2020-07-23 stsp {
743 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
744 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
745 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
746 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
747 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
748 af57b12a 2020-07-23 stsp ondisk_path);
749 af57b12a 2020-07-23 stsp return NULL;
750 af57b12a 2020-07-23 stsp }
751 af57b12a 2020-07-23 stsp
752 af57b12a 2020-07-23 stsp /*
753 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
754 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
755 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
756 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
757 993e2a1b 2020-07-23 stsp *
758 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
759 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
760 993e2a1b 2020-07-23 stsp *
761 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
762 993e2a1b 2020-07-23 stsp * has deleted this symlink.
763 11cc08c1 2020-07-23 stsp */
764 11cc08c1 2020-07-23 stsp static const struct got_error *
765 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
766 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
767 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
768 11cc08c1 2020-07-23 stsp {
769 11cc08c1 2020-07-23 stsp const struct got_error *err;
770 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
771 11cc08c1 2020-07-23 stsp FILE *f = NULL;
772 11cc08c1 2020-07-23 stsp
773 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
774 11cc08c1 2020-07-23 stsp if (err)
775 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
776 11cc08c1 2020-07-23 stsp
777 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
778 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
779 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
780 11cc08c1 2020-07-23 stsp goto done;
781 11cc08c1 2020-07-23 stsp }
782 11cc08c1 2020-07-23 stsp
783 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
784 11cc08c1 2020-07-23 stsp if (err)
785 3818e3c4 2020-11-01 naddy goto done;
786 3818e3c4 2020-11-01 naddy
787 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
788 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
789 11cc08c1 2020-07-23 stsp goto done;
790 3818e3c4 2020-11-01 naddy }
791 11cc08c1 2020-07-23 stsp
792 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
793 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
794 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
795 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
796 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
797 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
798 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
799 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
800 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
801 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
802 11cc08c1 2020-07-23 stsp goto done;
803 11cc08c1 2020-07-23 stsp }
804 11cc08c1 2020-07-23 stsp
805 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
806 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
807 11cc08c1 2020-07-23 stsp goto done;
808 11cc08c1 2020-07-23 stsp }
809 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
810 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
811 11cc08c1 2020-07-23 stsp goto done;
812 11cc08c1 2020-07-23 stsp }
813 11cc08c1 2020-07-23 stsp done:
814 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
815 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
816 11cc08c1 2020-07-23 stsp free(path);
817 11cc08c1 2020-07-23 stsp free(id_str);
818 11cc08c1 2020-07-23 stsp free(label_deriv);
819 11cc08c1 2020-07-23 stsp return err;
820 11cc08c1 2020-07-23 stsp }
821 d219f183 2020-07-23 stsp
822 d219f183 2020-07-23 stsp /* forward declaration */
823 d219f183 2020-07-23 stsp static const struct got_error *
824 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
825 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
826 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
827 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
828 11cc08c1 2020-07-23 stsp
829 11cc08c1 2020-07-23 stsp /*
830 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
831 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
832 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
833 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
834 af57b12a 2020-07-23 stsp */
835 af57b12a 2020-07-23 stsp static const struct got_error *
836 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
837 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
838 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
839 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
840 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
841 af57b12a 2020-07-23 stsp {
842 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
843 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
844 af57b12a 2020-07-23 stsp struct stat sb;
845 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
846 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
847 11cc08c1 2020-07-23 stsp int have_local_change = 0;
848 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
849 af57b12a 2020-07-23 stsp
850 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
851 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
852 af57b12a 2020-07-23 stsp
853 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
854 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
855 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
856 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
857 af57b12a 2020-07-23 stsp ondisk_path);
858 af57b12a 2020-07-23 stsp goto done;
859 af57b12a 2020-07-23 stsp }
860 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
861 af57b12a 2020-07-23 stsp
862 526a746f 2020-07-23 stsp if (blob_orig) {
863 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
864 526a746f 2020-07-23 stsp if (err)
865 526a746f 2020-07-23 stsp goto done;
866 526a746f 2020-07-23 stsp }
867 af57b12a 2020-07-23 stsp
868 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
869 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
870 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
871 11cc08c1 2020-07-23 stsp have_local_change = 1;
872 11cc08c1 2020-07-23 stsp
873 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
874 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
875 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
876 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
877 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
878 11cc08c1 2020-07-23 stsp
879 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
880 11cc08c1 2020-07-23 stsp if (ancestor_target) {
881 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
882 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 11cc08c1 2020-07-23 stsp path);
884 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
885 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
886 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
887 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
888 11cc08c1 2020-07-23 stsp path);
889 11cc08c1 2020-07-23 stsp } else {
890 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
891 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
892 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
893 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
894 11cc08c1 2020-07-23 stsp if (err)
895 11cc08c1 2020-07-23 stsp goto done;
896 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
897 11cc08c1 2020-07-23 stsp path);
898 11cc08c1 2020-07-23 stsp }
899 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
900 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
901 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
902 af57b12a 2020-07-23 stsp strlen(deriv_target));
903 af57b12a 2020-07-23 stsp if (err)
904 af57b12a 2020-07-23 stsp goto done;
905 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
906 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
907 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
908 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
909 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp } else {
913 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
914 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
915 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
916 ea7786be 2020-07-23 stsp if (err)
917 ea7786be 2020-07-23 stsp goto done;
918 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
919 ea7786be 2020-07-23 stsp path);
920 ea7786be 2020-07-23 stsp }
921 6353ad76 2019-02-08 stsp }
922 11cc08c1 2020-07-23 stsp
923 af57b12a 2020-07-23 stsp done:
924 af57b12a 2020-07-23 stsp free(ancestor_target);
925 eec2f5a9 2021-06-03 stsp return err;
926 eec2f5a9 2021-06-03 stsp }
927 eec2f5a9 2021-06-03 stsp
928 eec2f5a9 2021-06-03 stsp static const struct got_error *
929 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
930 eec2f5a9 2021-06-03 stsp {
931 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
932 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
933 eec2f5a9 2021-06-03 stsp ssize_t target_len;
934 eec2f5a9 2021-06-03 stsp size_t n;
935 eec2f5a9 2021-06-03 stsp FILE *f;
936 eec2f5a9 2021-06-03 stsp
937 eec2f5a9 2021-06-03 stsp *outfile = NULL;
938 eec2f5a9 2021-06-03 stsp
939 eec2f5a9 2021-06-03 stsp f = got_opentemp();
940 eec2f5a9 2021-06-03 stsp if (f == NULL)
941 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
942 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
943 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
944 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
945 eec2f5a9 2021-06-03 stsp goto done;
946 eec2f5a9 2021-06-03 stsp }
947 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
948 eec2f5a9 2021-06-03 stsp if (n != target_len) {
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 if (fflush(f) == EOF) {
953 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
954 eec2f5a9 2021-06-03 stsp goto done;
955 eec2f5a9 2021-06-03 stsp }
956 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
957 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
958 eec2f5a9 2021-06-03 stsp goto done;
959 eec2f5a9 2021-06-03 stsp }
960 eec2f5a9 2021-06-03 stsp done:
961 eec2f5a9 2021-06-03 stsp if (err)
962 eec2f5a9 2021-06-03 stsp fclose(f);
963 eec2f5a9 2021-06-03 stsp else
964 eec2f5a9 2021-06-03 stsp *outfile = f;
965 14c901f1 2019-08-08 stsp return err;
966 14c901f1 2019-08-08 stsp }
967 14c901f1 2019-08-08 stsp
968 14c901f1 2019-08-08 stsp /*
969 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
970 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
971 14c901f1 2019-08-08 stsp * acts as the second derived version.
972 14c901f1 2019-08-08 stsp */
973 14c901f1 2019-08-08 stsp static const struct got_error *
974 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
975 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
976 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
977 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
978 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
980 14c901f1 2019-08-08 stsp {
981 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
982 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
983 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
984 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
985 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
986 14c901f1 2019-08-08 stsp
987 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
988 14c901f1 2019-08-08 stsp
989 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
990 ed6b5030 2020-10-20 stsp if (err)
991 ed6b5030 2020-10-20 stsp return err;
992 14c901f1 2019-08-08 stsp
993 67a66647 2021-05-31 stsp if (blob_orig) {
994 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
995 67a66647 2021-05-31 stsp parent) == -1) {
996 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
997 67a66647 2021-05-31 stsp base_path = NULL;
998 67a66647 2021-05-31 stsp goto done;
999 67a66647 2021-05-31 stsp }
1000 67a66647 2021-05-31 stsp
1001 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1002 fc2a50f2 2022-11-01 thomas base_path, "");
1003 67a66647 2021-05-31 stsp if (err)
1004 67a66647 2021-05-31 stsp goto done;
1005 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 67a66647 2021-05-31 stsp blob_orig);
1007 67a66647 2021-05-31 stsp if (err)
1008 67a66647 2021-05-31 stsp goto done;
1009 67a66647 2021-05-31 stsp free(base_path);
1010 db590691 2021-06-02 stsp } else {
1011 db590691 2021-06-02 stsp /*
1012 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1013 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1014 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1015 db590691 2021-06-02 stsp */
1016 db590691 2021-06-02 stsp f_orig = got_opentemp();
1017 db590691 2021-06-02 stsp if (f_orig == NULL) {
1018 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1019 db590691 2021-06-02 stsp goto done;
1020 db590691 2021-06-02 stsp }
1021 67a66647 2021-05-31 stsp }
1022 67a66647 2021-05-31 stsp
1023 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1024 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1025 14c901f1 2019-08-08 stsp base_path = NULL;
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp }
1028 14c901f1 2019-08-08 stsp
1029 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1033 14c901f1 2019-08-08 stsp blob_deriv);
1034 14c901f1 2019-08-08 stsp if (err)
1035 14c901f1 2019-08-08 stsp goto done;
1036 14c901f1 2019-08-08 stsp
1037 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1038 14c901f1 2019-08-08 stsp if (err)
1039 14c901f1 2019-08-08 stsp goto done;
1040 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1041 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1042 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1043 14c901f1 2019-08-08 stsp goto done;
1044 eec2f5a9 2021-06-03 stsp }
1045 eec2f5a9 2021-06-03 stsp
1046 b6b86fd1 2022-08-30 thomas /*
1047 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1048 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1049 eec2f5a9 2021-06-03 stsp */
1050 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1051 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1052 eec2f5a9 2021-06-03 stsp if (err)
1053 eec2f5a9 2021-06-03 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp } else {
1055 eec2f5a9 2021-06-03 stsp int fd;
1056 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1057 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1058 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1059 eec2f5a9 2021-06-03 stsp goto done;
1060 eec2f5a9 2021-06-03 stsp }
1061 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1062 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1063 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1064 eec2f5a9 2021-06-03 stsp close(fd);
1065 eec2f5a9 2021-06-03 stsp goto done;
1066 eec2f5a9 2021-06-03 stsp }
1067 14c901f1 2019-08-08 stsp }
1068 14c901f1 2019-08-08 stsp
1069 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1070 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1071 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1072 14c901f1 2019-08-08 stsp done:
1073 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1074 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1075 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1076 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1077 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1078 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1079 14c901f1 2019-08-08 stsp free(base_path);
1080 67a66647 2021-05-31 stsp if (blob_orig_path) {
1081 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1082 67a66647 2021-05-31 stsp free(blob_orig_path);
1083 67a66647 2021-05-31 stsp }
1084 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1085 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1086 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1087 14c901f1 2019-08-08 stsp }
1088 6353ad76 2019-02-08 stsp free(id_str);
1089 818c7501 2019-07-11 stsp free(label_deriv);
1090 ed6b5030 2020-10-20 stsp free(parent);
1091 4a1ddfc2 2019-01-12 stsp return err;
1092 4a1ddfc2 2019-01-12 stsp }
1093 4a1ddfc2 2019-01-12 stsp
1094 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1095 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1096 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1097 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1098 13d9040b 2019-03-27 stsp {
1099 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1100 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1101 13d9040b 2019-03-27 stsp
1102 65b05cec 2020-07-23 stsp *new_iep = NULL;
1103 65b05cec 2020-07-23 stsp
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1105 054041d0 2020-06-24 stsp if (err)
1106 054041d0 2020-06-24 stsp return err;
1107 054041d0 2020-06-24 stsp
1108 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1109 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1110 054041d0 2020-06-24 stsp if (err)
1111 054041d0 2020-06-24 stsp goto done;
1112 054041d0 2020-06-24 stsp
1113 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1114 054041d0 2020-06-24 stsp done:
1115 054041d0 2020-06-24 stsp if (err)
1116 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1117 65b05cec 2020-07-23 stsp else
1118 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1119 13d9040b 2019-03-27 stsp return err;
1120 1ebedb77 2019-10-19 stsp }
1121 1ebedb77 2019-10-19 stsp
1122 1ebedb77 2019-10-19 stsp static mode_t
1123 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1124 1ebedb77 2019-10-19 stsp {
1125 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1126 1ebedb77 2019-10-19 stsp
1127 1ebedb77 2019-10-19 stsp if (executable) {
1128 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1129 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1130 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1131 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1132 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1133 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1134 1ebedb77 2019-10-19 stsp }
1135 1ebedb77 2019-10-19 stsp
1136 a2c162eb 2022-10-30 thomas return st_mode;
1137 13d9040b 2019-03-27 stsp }
1138 13d9040b 2019-03-27 stsp
1139 8ba819a3 2020-07-23 stsp /* forward declaration */
1140 13d9040b 2019-03-27 stsp static const struct got_error *
1141 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1142 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1143 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1144 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1145 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1146 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1147 8ba819a3 2020-07-23 stsp
1148 5a1fbc73 2020-07-23 stsp /*
1149 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1150 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1151 5a1fbc73 2020-07-23 stsp */
1152 8ba819a3 2020-07-23 stsp static const struct got_error *
1153 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1154 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1155 5a1fbc73 2020-07-23 stsp {
1156 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1157 5a1fbc73 2020-07-23 stsp ssize_t elen;
1158 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1159 5a1fbc73 2020-07-23 stsp int fd;
1160 5a1fbc73 2020-07-23 stsp
1161 c6e8a826 2021-04-05 stsp *did_something = 0;
1162 c6e8a826 2021-04-05 stsp
1163 5a1fbc73 2020-07-23 stsp /*
1164 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1165 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1166 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1167 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1168 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1169 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1170 5a1fbc73 2020-07-23 stsp */
1171 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1172 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1173 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1174 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1175 5a1fbc73 2020-07-23 stsp
1176 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1177 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1178 5a1fbc73 2020-07-23 stsp if (elen == -1)
1179 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1180 5a1fbc73 2020-07-23 stsp
1181 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1182 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1183 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1184 5a1fbc73 2020-07-23 stsp }
1185 5a1fbc73 2020-07-23 stsp
1186 c6e8a826 2021-04-05 stsp *did_something = 1;
1187 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1188 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1189 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1190 5a1fbc73 2020-07-23 stsp return err;
1191 3c1ec782 2020-07-23 stsp }
1192 3c1ec782 2020-07-23 stsp
1193 3c1ec782 2020-07-23 stsp static const struct got_error *
1194 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1195 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1196 3c1ec782 2020-07-23 stsp {
1197 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1198 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1199 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1200 3c1ec782 2020-07-23 stsp
1201 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1204 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1205 3c1ec782 2020-07-23 stsp return NULL;
1206 3c1ec782 2020-07-23 stsp }
1207 3c1ec782 2020-07-23 stsp
1208 3c1ec782 2020-07-23 stsp /*
1209 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1210 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1211 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1212 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1213 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1214 3c1ec782 2020-07-23 stsp */
1215 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1216 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1217 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1218 ce031e9e 2020-10-20 stsp if (err)
1219 ce031e9e 2020-10-20 stsp return err;
1220 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1221 ce031e9e 2020-10-20 stsp free(parent);
1222 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1223 ce031e9e 2020-10-20 stsp }
1224 ce031e9e 2020-10-20 stsp free(parent);
1225 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1226 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1227 3c1ec782 2020-07-23 stsp free(abspath);
1228 3c1ec782 2020-07-23 stsp return err;
1229 3c1ec782 2020-07-23 stsp }
1230 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1231 3c1ec782 2020-07-23 stsp free(abspath);
1232 3c1ec782 2020-07-23 stsp if (err)
1233 3c1ec782 2020-07-23 stsp return err;
1234 3c1ec782 2020-07-23 stsp } else {
1235 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1236 3c1ec782 2020-07-23 stsp if (err)
1237 3c1ec782 2020-07-23 stsp return err;
1238 3c1ec782 2020-07-23 stsp }
1239 3c1ec782 2020-07-23 stsp
1240 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1241 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1242 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1243 3c1ec782 2020-07-23 stsp return NULL;
1244 3c1ec782 2020-07-23 stsp }
1245 3c1ec782 2020-07-23 stsp
1246 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1247 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1248 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1249 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1250 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1251 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1252 3c1ec782 2020-07-23 stsp
1253 3c1ec782 2020-07-23 stsp free(path_got);
1254 3c1ec782 2020-07-23 stsp return NULL;
1255 5a1fbc73 2020-07-23 stsp }
1256 5a1fbc73 2020-07-23 stsp
1257 5a1fbc73 2020-07-23 stsp static const struct got_error *
1258 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1259 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1260 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1261 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1262 ace90326 2021-09-27 thomas struct got_repository *repo,
1263 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1264 9d31a1d8 2018-03-11 stsp {
1265 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1266 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1267 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1268 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1270 8ba819a3 2020-07-23 stsp
1271 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1272 2e1fa222 2020-07-23 stsp
1273 9e39ca77 2022-07-21 thomas /*
1274 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1275 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1276 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1277 8ba819a3 2020-07-23 stsp * in the blob object.
1278 8ba819a3 2020-07-23 stsp */
1279 8ba819a3 2020-07-23 stsp do {
1280 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1281 3521b466 2022-07-21 thomas if (err)
1282 3521b466 2022-07-21 thomas return err;
1283 3521b466 2022-07-21 thomas
1284 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1285 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1286 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1287 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1288 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1289 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1290 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1291 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1292 3b9f0f87 2020-07-23 stsp progress_arg);
1293 8ba819a3 2020-07-23 stsp }
1294 8ba819a3 2020-07-23 stsp if (len > 0) {
1295 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1296 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1297 8ba819a3 2020-07-23 stsp len - hdrlen);
1298 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1299 8ba819a3 2020-07-23 stsp hdrlen = 0;
1300 8ba819a3 2020-07-23 stsp }
1301 8ba819a3 2020-07-23 stsp } while (len != 0);
1302 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1303 8ba819a3 2020-07-23 stsp
1304 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1305 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1306 3c1ec782 2020-07-23 stsp if (err)
1307 3c1ec782 2020-07-23 stsp return err;
1308 8ba819a3 2020-07-23 stsp
1309 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1310 906c123b 2020-07-23 stsp /* install as a regular file */
1311 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1312 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1313 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1314 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1315 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1316 9e39ca77 2022-07-21 thomas return err;
1317 906c123b 2020-07-23 stsp }
1318 906c123b 2020-07-23 stsp
1319 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1320 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1321 c6e8a826 2021-04-05 stsp int symlink_replaced;
1322 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1323 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1324 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1325 9e39ca77 2022-07-21 thomas return err;
1326 c90c8ce3 2020-07-23 stsp }
1327 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1328 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1329 5a1fbc73 2020-07-23 stsp if (err)
1330 9e39ca77 2022-07-21 thomas return err;
1331 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1332 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1333 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1334 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1335 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1336 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1337 c6e8a826 2021-04-05 stsp } else {
1338 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1340 c6e8a826 2021-04-05 stsp }
1341 f35fa46a 2020-07-23 stsp }
1342 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1343 f35fa46a 2020-07-23 stsp }
1344 f35fa46a 2020-07-23 stsp
1345 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1346 f4994adc 2020-10-20 stsp char *parent;
1347 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1348 f4994adc 2020-10-20 stsp if (err)
1349 9e39ca77 2022-07-21 thomas return err;
1350 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1351 f4994adc 2020-10-20 stsp free(parent);
1352 f35fa46a 2020-07-23 stsp if (err)
1353 9e39ca77 2022-07-21 thomas return err;
1354 f35fa46a 2020-07-23 stsp /*
1355 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1356 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1357 f35fa46a 2020-07-23 stsp */
1358 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1359 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1360 9e39ca77 2022-07-21 thomas return err;
1361 f35fa46a 2020-07-23 stsp }
1362 f35fa46a 2020-07-23 stsp }
1363 f35fa46a 2020-07-23 stsp
1364 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1365 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1366 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1367 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1368 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1369 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1370 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1371 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1372 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1373 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1374 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1375 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1376 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1377 8ba819a3 2020-07-23 stsp } else {
1378 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1379 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1380 8ba819a3 2020-07-23 stsp }
1381 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1382 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1383 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1384 8ba819a3 2020-07-23 stsp return err;
1385 8ba819a3 2020-07-23 stsp }
1386 8ba819a3 2020-07-23 stsp
1387 8ba819a3 2020-07-23 stsp static const struct got_error *
1388 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1389 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1390 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1391 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1392 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1393 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1394 8ba819a3 2020-07-23 stsp {
1395 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1396 507dc3bb 2018-12-29 stsp int fd = -1;
1397 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1398 507dc3bb 2018-12-29 stsp int update = 0;
1399 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1400 a2c162eb 2022-10-30 thomas mode_t mode;
1401 8ba819a3 2020-07-23 stsp
1402 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1403 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1404 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1405 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1406 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1407 f5375317 2020-10-20 stsp char *parent;
1408 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1409 f5375317 2020-10-20 stsp if (err)
1410 f5375317 2020-10-20 stsp return err;
1411 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1412 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1413 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1414 f5375317 2020-10-20 stsp free(parent);
1415 21908da4 2019-01-13 stsp if (err)
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 a2c162eb 2022-10-30 thomas mode);
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1428 3b9f0f87 2020-07-23 stsp }
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1435 d70b8e30 2018-12-27 stsp } else {
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1438 507dc3bb 2018-12-29 stsp if (err)
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1441 a2c162eb 2022-10-30 thomas
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1446 a2c162eb 2022-10-30 thomas }
1447 9d31a1d8 2018-03-11 stsp }
1448 507dc3bb 2018-12-29 stsp } else
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1450 3818e3c4 2020-11-01 naddy }
1451 3818e3c4 2020-11-01 naddy
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 bd6aa359 2020-07-23 stsp path);
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 bd6aa359 2020-07-23 stsp if (err)
1463 bd6aa359 2020-07-23 stsp goto done;
1464 bd6aa359 2020-07-23 stsp }
1465 d7b62c98 2018-12-27 stsp
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1467 9d31a1d8 2018-03-11 stsp do {
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1470 9d31a1d8 2018-03-11 stsp if (err)
1471 9d31a1d8 2018-03-11 stsp break;
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1481 9d31a1d8 2018-03-11 stsp }
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1485 9d31a1d8 2018-03-11 stsp
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1489 816dc654 2019-02-16 stsp }
1490 9d31a1d8 2018-03-11 stsp
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1495 f6d8c0ac 2020-11-09 stsp }
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1500 68ed9ba5 2019-02-10 stsp }
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1503 507dc3bb 2018-12-29 stsp }
1504 507dc3bb 2018-12-29 stsp
1505 9d31a1d8 2018-03-11 stsp done:
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1512 6353ad76 2019-02-08 stsp }
1513 6353ad76 2019-02-08 stsp
1514 be94c032 2023-02-20 thomas /*
1515 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1517 be94c032 2023-02-20 thomas */
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1520 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1521 be94c032 2023-02-20 thomas FILE *ondisk_file)
1522 7154f6ce 2019-03-27 stsp {
1523 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1528 7154f6ce 2019-03-27 stsp };
1529 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1530 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1531 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1532 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1533 9bdd68dd 2020-01-02 naddy char *line = NULL;
1534 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1535 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1536 7154f6ce 2019-03-27 stsp
1537 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1538 8de9d8ad 2023-02-20 thomas return NULL;
1539 be94c032 2023-02-20 thomas
1540 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1541 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1542 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1543 be94c032 2023-02-20 thomas
1544 8de9d8ad 2023-02-20 thomas if (blob) {
1545 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1546 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 be94c032 2023-02-20 thomas if (err)
1548 be94c032 2023-02-20 thomas goto done;
1549 8de9d8ad 2023-02-20 thomas }
1550 be94c032 2023-02-20 thomas
1551 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 8de9d8ad 2023-02-20 thomas if (err)
1554 8de9d8ad 2023-02-20 thomas goto done;
1555 8de9d8ad 2023-02-20 thomas
1556 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1560 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1561 450d9f6b 2023-02-20 thomas off_t pos;
1562 8de9d8ad 2023-02-20 thomas
1563 8de9d8ad 2023-02-20 thomas /*
1564 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1565 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1566 8de9d8ad 2023-02-20 thomas */
1567 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1568 8de9d8ad 2023-02-20 thomas
1569 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1571 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1572 7154f6ce 2019-03-27 stsp }
1573 7154f6ce 2019-03-27 stsp
1574 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1575 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 450d9f6b 2023-02-20 thomas goto done;
1578 7154f6ce 2019-03-27 stsp }
1579 8de9d8ad 2023-02-20 thomas
1580 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1582 450d9f6b 2023-02-20 thomas
1583 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1584 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1585 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1586 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1587 8de9d8ad 2023-02-20 thomas break;
1588 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas }
1591 8de9d8ad 2023-02-20 thomas
1592 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1593 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1594 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1595 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1597 8de9d8ad 2023-02-20 thomas goto done;
1598 8de9d8ad 2023-02-20 thomas } else
1599 8de9d8ad 2023-02-20 thomas i++;
1600 8de9d8ad 2023-02-20 thomas }
1601 8de9d8ad 2023-02-20 thomas ++ln;
1602 8de9d8ad 2023-02-20 thomas }
1603 7154f6ce 2019-03-27 stsp }
1604 be94c032 2023-02-20 thomas
1605 be94c032 2023-02-20 thomas done:
1606 9bdd68dd 2020-01-02 naddy free(line);
1607 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1609 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1610 8de9d8ad 2023-02-20 thomas if (err == NULL)
1611 8de9d8ad 2023-02-20 thomas err = free_err;
1612 7154f6ce 2019-03-27 stsp
1613 7154f6ce 2019-03-27 stsp return err;
1614 e2b1e152 2019-07-13 stsp }
1615 e2b1e152 2019-07-13 stsp
1616 e2b1e152 2019-07-13 stsp static int
1617 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1618 1ebedb77 2019-10-19 stsp {
1619 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1621 1ebedb77 2019-10-19 stsp }
1622 1ebedb77 2019-10-19 stsp
1623 1ebedb77 2019-10-19 stsp static int
1624 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1625 e2b1e152 2019-07-13 stsp {
1626 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1631 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1632 c363b2c1 2019-08-03 stsp }
1633 c363b2c1 2019-08-03 stsp
1634 c363b2c1 2019-08-03 stsp static unsigned char
1635 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1636 c363b2c1 2019-08-03 stsp {
1637 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1638 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1639 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1644 c363b2c1 2019-08-03 stsp default:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1646 a919d5c4 2020-07-23 stsp }
1647 a919d5c4 2020-07-23 stsp }
1648 a919d5c4 2020-07-23 stsp
1649 a919d5c4 2020-07-23 stsp static const struct got_error *
1650 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1651 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1652 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1653 a919d5c4 2020-07-23 stsp {
1654 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1655 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1656 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1657 a919d5c4 2020-07-23 stsp ssize_t elen;
1658 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1659 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1665 a919d5c4 2020-07-23 stsp do {
1666 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1667 a919d5c4 2020-07-23 stsp if (err)
1668 a919d5c4 2020-07-23 stsp return err;
1669 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1670 a919d5c4 2020-07-23 stsp /*
1671 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1672 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1673 a919d5c4 2020-07-23 stsp */
1674 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1675 a919d5c4 2020-07-23 stsp }
1676 a919d5c4 2020-07-23 stsp if (len > 0) {
1677 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1678 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1679 a919d5c4 2020-07-23 stsp len - hdrlen);
1680 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1681 a919d5c4 2020-07-23 stsp hdrlen = 0;
1682 a919d5c4 2020-07-23 stsp }
1683 a919d5c4 2020-07-23 stsp } while (len != 0);
1684 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1685 a919d5c4 2020-07-23 stsp
1686 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1687 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 a919d5c4 2020-07-23 stsp if (elen == -1)
1689 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1690 a919d5c4 2020-07-23 stsp } else {
1691 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1692 a919d5c4 2020-07-23 stsp if (elen == -1)
1693 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1694 c363b2c1 2019-08-03 stsp }
1695 a919d5c4 2020-07-23 stsp
1696 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp return NULL;
1700 7154f6ce 2019-03-27 stsp }
1701 7154f6ce 2019-03-27 stsp
1702 7154f6ce 2019-03-27 stsp static const struct got_error *
1703 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1704 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1705 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1706 6353ad76 2019-02-08 stsp {
1707 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1708 6353ad76 2019-02-08 stsp struct got_object_id id;
1709 6353ad76 2019-02-08 stsp size_t hdrlen;
1710 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1711 6353ad76 2019-02-08 stsp FILE *f = NULL;
1712 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1713 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1714 6353ad76 2019-02-08 stsp size_t flen, blen;
1715 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1716 6353ad76 2019-02-08 stsp
1717 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1718 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1719 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1720 6353ad76 2019-02-08 stsp
1721 7f91a133 2019-12-13 stsp /*
1722 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1723 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1724 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1725 7f91a133 2019-12-13 stsp */
1726 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1727 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1729 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1730 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1731 882ef1b9 2019-12-13 stsp else
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1733 882ef1b9 2019-12-13 stsp goto done;
1734 882ef1b9 2019-12-13 stsp }
1735 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1736 3d35a492 2019-12-13 stsp goto done;
1737 3d35a492 2019-12-13 stsp }
1738 7f91a133 2019-12-13 stsp } else {
1739 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1741 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1742 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1743 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1745 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1746 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1748 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1749 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1750 3d35a492 2019-12-13 stsp else
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1752 3d35a492 2019-12-13 stsp goto done;
1753 3d35a492 2019-12-13 stsp }
1754 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1755 1338848f 2019-12-13 stsp goto done;
1756 a378724f 2019-02-10 stsp }
1757 a378724f 2019-02-10 stsp }
1758 6353ad76 2019-02-08 stsp
1759 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1761 1338848f 2019-12-13 stsp goto done;
1762 3f148bc6 2019-07-27 stsp }
1763 efdd40df 2019-07-27 stsp
1764 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1766 1338848f 2019-12-13 stsp goto done;
1767 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1768 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1769 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1770 1338848f 2019-12-13 stsp goto done;
1771 d00136be 2019-03-26 stsp }
1772 b8f41171 2019-02-10 stsp
1773 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1774 f179e66d 2020-07-23 stsp goto done;
1775 f179e66d 2020-07-23 stsp
1776 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1777 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1779 1338848f 2019-12-13 stsp goto done;
1780 f179e66d 2020-07-23 stsp }
1781 6353ad76 2019-02-08 stsp
1782 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1783 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1784 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1785 c363b2c1 2019-08-03 stsp else
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp
1788 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1789 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1790 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1791 f4ae6ddb 2022-07-01 thomas goto done;
1792 f4ae6ddb 2022-07-01 thomas }
1793 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1794 6353ad76 2019-02-08 stsp if (err)
1795 1338848f 2019-12-13 stsp goto done;
1796 6353ad76 2019-02-08 stsp
1797 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1798 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1799 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1800 a919d5c4 2020-07-23 stsp goto done;
1801 a919d5c4 2020-07-23 stsp }
1802 a919d5c4 2020-07-23 stsp
1803 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1804 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 ab0d4361 2019-12-13 stsp if (fd == -1) {
1806 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1807 ab0d4361 2019-12-13 stsp goto done;
1808 ab0d4361 2019-12-13 stsp }
1809 3d35a492 2019-12-13 stsp }
1810 3d35a492 2019-12-13 stsp
1811 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1812 6353ad76 2019-02-08 stsp if (f == NULL) {
1813 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1814 6353ad76 2019-02-08 stsp goto done;
1815 6353ad76 2019-02-08 stsp }
1816 1338848f 2019-12-13 stsp fd = -1;
1817 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1818 656b1f76 2019-05-11 jcs for (;;) {
1819 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1821 6353ad76 2019-02-08 stsp if (err)
1822 7154f6ce 2019-03-27 stsp goto done;
1823 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1824 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1826 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1827 7154f6ce 2019-03-27 stsp goto done;
1828 80c5c120 2019-02-19 stsp }
1829 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1830 6353ad76 2019-02-08 stsp if (flen != 0)
1831 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1832 6353ad76 2019-02-08 stsp break;
1833 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1835 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1836 6353ad76 2019-02-08 stsp break;
1837 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1838 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1839 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1841 6353ad76 2019-02-08 stsp break;
1842 6353ad76 2019-02-08 stsp }
1843 6353ad76 2019-02-08 stsp } else {
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp }
1847 6353ad76 2019-02-08 stsp hdrlen = 0;
1848 6353ad76 2019-02-08 stsp }
1849 7154f6ce 2019-03-27 stsp
1850 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1851 7154f6ce 2019-03-27 stsp rewind(f);
1852 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1853 be94c032 2023-02-20 thomas sb, f);
1854 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1855 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1856 6353ad76 2019-02-08 stsp done:
1857 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1859 6353ad76 2019-02-08 stsp if (blob)
1860 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1861 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1862 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1863 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1864 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1865 9d31a1d8 2018-03-11 stsp return err;
1866 e2b1e152 2019-07-13 stsp }
1867 e2b1e152 2019-07-13 stsp
1868 e2b1e152 2019-07-13 stsp /*
1869 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1870 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1871 e2b1e152 2019-07-13 stsp */
1872 e2b1e152 2019-07-13 stsp static const struct got_error *
1873 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1875 e2b1e152 2019-07-13 stsp {
1876 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1878 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1879 e2b1e152 2019-07-13 stsp
1880 e2b1e152 2019-07-13 stsp return NULL;
1881 9d31a1d8 2018-03-11 stsp }
1882 9d31a1d8 2018-03-11 stsp
1883 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1884 9f323212 2023-03-10 thomas
1885 9d31a1d8 2018-03-11 stsp static const struct got_error *
1886 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1887 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1889 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 0584f854 2019-04-06 stsp void *progress_arg)
1891 9d31a1d8 2018-03-11 stsp {
1892 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1893 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1894 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1895 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1896 b8f41171 2019-02-10 stsp struct stat sb;
1897 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1898 9d31a1d8 2018-03-11 stsp
1899 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1901 6353ad76 2019-02-08 stsp
1902 abb4604f 2019-07-27 stsp if (ie) {
1903 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 a76c42e6 2019-08-03 stsp goto done;
1906 a76c42e6 2019-08-03 stsp }
1907 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1908 7f91a133 2019-12-13 stsp repo);
1909 abb4604f 2019-07-27 stsp if (err)
1910 abb4604f 2019-07-27 stsp goto done;
1911 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1913 c90c8ce3 2020-07-23 stsp } else {
1914 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1915 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1916 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1917 e6f4ba31 2021-09-24 thomas ondisk_path);
1918 e6f4ba31 2021-09-24 thomas goto done;
1919 e6f4ba31 2021-09-24 thomas }
1920 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1922 e6f4ba31 2021-09-24 thomas } else {
1923 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1925 e6f4ba31 2021-09-24 thomas else
1926 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1927 e6f4ba31 2021-09-24 thomas }
1928 c90c8ce3 2020-07-23 stsp }
1929 6353ad76 2019-02-08 stsp
1930 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1931 2c41dce7 2021-06-27 stsp if (ie)
1932 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1933 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1934 b8f41171 2019-02-10 stsp goto done;
1935 b8f41171 2019-02-10 stsp }
1936 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1937 a769b60b 2021-06-27 stsp if (ie)
1938 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1939 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1940 5036ab18 2020-04-18 stsp path);
1941 5036ab18 2020-04-18 stsp goto done;
1942 9f323212 2023-03-10 thomas }
1943 9f323212 2023-03-10 thomas
1944 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1945 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1946 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1947 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1948 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1949 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1950 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1951 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1952 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1953 9f323212 2023-03-10 thomas } else if (ie) {
1954 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1955 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1956 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1957 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1958 9f323212 2023-03-10 thomas ie->path);
1959 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1960 9f323212 2023-03-10 thomas errno == ENOENT))
1961 9f323212 2023-03-10 thomas goto done;
1962 9f323212 2023-03-10 thomas }
1963 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1964 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1965 9f323212 2023-03-10 thomas ie->path);
1966 9f323212 2023-03-10 thomas }
1967 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1968 5036ab18 2020-04-18 stsp }
1969 b8f41171 2019-02-10 stsp
1970 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1971 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1972 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1973 c6e8a826 2021-04-05 stsp /*
1974 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1975 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1976 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1977 c6e8a826 2021-04-05 stsp * updating contents of this file.
1978 c6e8a826 2021-04-05 stsp */
1979 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1980 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1981 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1982 c6e8a826 2021-04-05 stsp /* Same commit. */
1983 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1984 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1985 e2b1e152 2019-07-13 stsp if (err)
1986 e2b1e152 2019-07-13 stsp goto done;
1987 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1988 b8f41171 2019-02-10 stsp path);
1989 6353ad76 2019-02-08 stsp goto done;
1990 a378724f 2019-02-10 stsp }
1991 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1992 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1993 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1994 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1995 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
1996 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
1997 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
1998 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
1999 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2000 b36a9774 2023-04-14 thomas }
2001 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2002 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2003 0f58026f 2021-04-05 stsp if (err)
2004 0f58026f 2021-04-05 stsp goto done;
2005 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2006 0f58026f 2021-04-05 stsp path);
2007 b8f41171 2019-02-10 stsp goto done;
2008 e2b1e152 2019-07-13 stsp }
2009 9d31a1d8 2018-03-11 stsp }
2010 9d31a1d8 2018-03-11 stsp
2011 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2012 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2013 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2014 f4ae6ddb 2022-07-01 thomas goto done;
2015 f4ae6ddb 2022-07-01 thomas }
2016 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2017 8da9e5f4 2019-01-12 stsp if (err)
2018 6353ad76 2019-02-08 stsp goto done;
2019 512f0d0e 2019-01-02 stsp
2020 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2021 234035bc 2019-06-01 stsp int update_timestamps;
2022 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2023 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2024 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2025 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2026 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2027 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2028 f4ae6ddb 2022-07-01 thomas goto done;
2029 f4ae6ddb 2022-07-01 thomas }
2030 234035bc 2019-06-01 stsp struct got_object_id id2;
2031 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2032 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2033 f4ae6ddb 2022-07-01 thomas fd2);
2034 234035bc 2019-06-01 stsp if (err)
2035 f69721c3 2019-10-21 stsp goto done;
2036 f69721c3 2019-10-21 stsp }
2037 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2038 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2039 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2040 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2041 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2042 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2043 f69721c3 2019-10-21 stsp goto done;
2044 f69721c3 2019-10-21 stsp }
2045 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2046 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2047 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2048 234035bc 2019-06-01 stsp goto done;
2049 f69721c3 2019-10-21 stsp }
2050 234035bc 2019-06-01 stsp }
2051 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2052 36bf999c 2020-07-23 stsp char *link_target;
2053 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2054 36bf999c 2020-07-23 stsp if (err)
2055 36bf999c 2020-07-23 stsp goto done;
2056 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2057 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2058 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2059 36bf999c 2020-07-23 stsp free(link_target);
2060 993e2a1b 2020-07-23 stsp } else {
2061 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2062 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2063 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2064 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2065 993e2a1b 2020-07-23 stsp }
2066 f69721c3 2019-10-21 stsp free(label_orig);
2067 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2068 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2069 f4ae6ddb 2022-07-01 thomas goto done;
2070 f4ae6ddb 2022-07-01 thomas }
2071 234035bc 2019-06-01 stsp if (blob2)
2072 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2073 909d120e 2019-09-22 stsp if (err)
2074 909d120e 2019-09-22 stsp goto done;
2075 234035bc 2019-06-01 stsp /*
2076 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2077 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2078 234035bc 2019-06-01 stsp * unmodified files again.
2079 234035bc 2019-06-01 stsp */
2080 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2081 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2082 234035bc 2019-06-01 stsp update_timestamps);
2083 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2084 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2085 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2086 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2087 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2088 1ee397ad 2019-07-12 stsp if (err)
2089 1ee397ad 2019-07-12 stsp goto done;
2090 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2091 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2092 13d9040b 2019-03-27 stsp if (err)
2093 13d9040b 2019-03-27 stsp goto done;
2094 13d9040b 2019-03-27 stsp } else {
2095 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2096 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2097 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2098 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2099 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2100 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2101 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2102 2e1fa222 2020-07-23 stsp } else {
2103 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2104 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2105 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2106 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2107 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2108 2e1fa222 2020-07-23 stsp }
2109 13d9040b 2019-03-27 stsp if (err)
2110 13d9040b 2019-03-27 stsp goto done;
2111 65b05cec 2020-07-23 stsp
2112 054041d0 2020-06-24 stsp if (ie) {
2113 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2114 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2115 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2116 054041d0 2020-06-24 stsp } else {
2117 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2118 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2119 054041d0 2020-06-24 stsp &blob->id);
2120 054041d0 2020-06-24 stsp }
2121 13d9040b 2019-03-27 stsp if (err)
2122 13d9040b 2019-03-27 stsp goto done;
2123 65b05cec 2020-07-23 stsp
2124 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2125 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2126 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2127 2e1fa222 2020-07-23 stsp }
2128 13d9040b 2019-03-27 stsp }
2129 f4ae6ddb 2022-07-01 thomas
2130 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2131 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2132 f4ae6ddb 2022-07-01 thomas goto done;
2133 f4ae6ddb 2022-07-01 thomas }
2134 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2135 6353ad76 2019-02-08 stsp done:
2136 6353ad76 2019-02-08 stsp free(ondisk_path);
2137 8da9e5f4 2019-01-12 stsp return err;
2138 90285c3b 2019-01-08 stsp }
2139 90285c3b 2019-01-08 stsp
2140 90285c3b 2019-01-08 stsp static const struct got_error *
2141 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2142 90285c3b 2019-01-08 stsp {
2143 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2144 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2145 90285c3b 2019-01-08 stsp
2146 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2147 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2148 90285c3b 2019-01-08 stsp
2149 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2150 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2151 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2152 c97665ae 2019-01-13 stsp } else {
2153 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2154 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2155 1c4cdd89 2021-06-20 stsp if (err)
2156 1c4cdd89 2021-06-20 stsp goto done;
2157 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2158 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2159 fddefe3b 2020-10-20 stsp free(ondisk_path);
2160 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2161 1c4cdd89 2021-06-20 stsp parent = NULL;
2162 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2163 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2164 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2165 fddefe3b 2020-10-20 stsp ondisk_path);
2166 90285c3b 2019-01-08 stsp break;
2167 90285c3b 2019-01-08 stsp }
2168 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2169 1c4cdd89 2021-06-20 stsp if (err)
2170 1c4cdd89 2021-06-20 stsp break;
2171 1c4cdd89 2021-06-20 stsp }
2172 90285c3b 2019-01-08 stsp }
2173 1c4cdd89 2021-06-20 stsp done:
2174 90285c3b 2019-01-08 stsp free(ondisk_path);
2175 1c4cdd89 2021-06-20 stsp free(parent);
2176 90285c3b 2019-01-08 stsp return err;
2177 512f0d0e 2019-01-02 stsp }
2178 512f0d0e 2019-01-02 stsp
2179 708d8e67 2019-03-27 stsp static const struct got_error *
2180 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2181 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2182 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2183 708d8e67 2019-03-27 stsp {
2184 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2185 708d8e67 2019-03-27 stsp unsigned char status;
2186 708d8e67 2019-03-27 stsp struct stat sb;
2187 708d8e67 2019-03-27 stsp char *ondisk_path;
2188 708d8e67 2019-03-27 stsp
2189 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2190 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2191 a76c42e6 2019-08-03 stsp
2192 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2193 708d8e67 2019-03-27 stsp == -1)
2194 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2195 708d8e67 2019-03-27 stsp
2196 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2197 708d8e67 2019-03-27 stsp if (err)
2198 5a58a424 2020-06-23 stsp goto done;
2199 708d8e67 2019-03-27 stsp
2200 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2201 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2202 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2203 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2204 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2205 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2206 993e2a1b 2020-07-23 stsp goto done;
2207 993e2a1b 2020-07-23 stsp }
2208 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2209 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2210 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2211 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2212 993e2a1b 2020-07-23 stsp if (err)
2213 993e2a1b 2020-07-23 stsp goto done;
2214 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2215 993e2a1b 2020-07-23 stsp ie->path);
2216 993e2a1b 2020-07-23 stsp goto done;
2217 993e2a1b 2020-07-23 stsp }
2218 993e2a1b 2020-07-23 stsp
2219 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2220 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2221 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2222 1ee397ad 2019-07-12 stsp if (err)
2223 5a58a424 2020-06-23 stsp goto done;
2224 fc6346c4 2019-03-27 stsp /*
2225 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2226 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2227 fc6346c4 2019-03-27 stsp */
2228 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2229 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2230 fc6346c4 2019-03-27 stsp } else {
2231 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2232 1ee397ad 2019-07-12 stsp if (err)
2233 5a58a424 2020-06-23 stsp goto done;
2234 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2235 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2236 fc6346c4 2019-03-27 stsp if (err)
2237 5a58a424 2020-06-23 stsp goto done;
2238 fc6346c4 2019-03-27 stsp }
2239 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2240 708d8e67 2019-03-27 stsp }
2241 5a58a424 2020-06-23 stsp done:
2242 5a58a424 2020-06-23 stsp free(ondisk_path);
2243 fc6346c4 2019-03-27 stsp return err;
2244 708d8e67 2019-03-27 stsp }
2245 708d8e67 2019-03-27 stsp
2246 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2247 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2248 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2249 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2250 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2251 8da9e5f4 2019-01-12 stsp void *progress_arg;
2252 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2253 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2254 8da9e5f4 2019-01-12 stsp };
2255 8da9e5f4 2019-01-12 stsp
2256 512f0d0e 2019-01-02 stsp static const struct got_error *
2257 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2258 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2259 512f0d0e 2019-01-02 stsp {
2260 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2261 0584f854 2019-04-06 stsp
2262 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2263 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2264 512f0d0e 2019-01-02 stsp
2265 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2266 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2267 8da9e5f4 2019-01-12 stsp }
2268 512f0d0e 2019-01-02 stsp
2269 8da9e5f4 2019-01-12 stsp static const struct got_error *
2270 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2271 8da9e5f4 2019-01-12 stsp {
2272 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2273 7a9df742 2019-01-08 stsp
2274 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2275 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2276 0584f854 2019-04-06 stsp
2277 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2278 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2279 9d31a1d8 2018-03-11 stsp }
2280 9d31a1d8 2018-03-11 stsp
2281 9d31a1d8 2018-03-11 stsp static const struct got_error *
2282 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2283 9d31a1d8 2018-03-11 stsp {
2284 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2285 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2286 8da9e5f4 2019-01-12 stsp char *path;
2287 9d31a1d8 2018-03-11 stsp
2288 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2289 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2290 0584f854 2019-04-06 stsp
2291 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2292 63c5ca5d 2019-08-24 stsp return NULL;
2293 63c5ca5d 2019-08-24 stsp
2294 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2295 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2296 8da9e5f4 2019-01-12 stsp == -1)
2297 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2298 9d31a1d8 2018-03-11 stsp
2299 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2300 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2301 8da9e5f4 2019-01-12 stsp else
2302 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2303 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2304 9d31a1d8 2018-03-11 stsp
2305 8da9e5f4 2019-01-12 stsp free(path);
2306 0cd1c46a 2019-03-11 stsp return err;
2307 0cd1c46a 2019-03-11 stsp }
2308 0cd1c46a 2019-03-11 stsp
2309 b2118c49 2020-07-28 stsp const struct got_error *
2310 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2311 b2118c49 2020-07-28 stsp {
2312 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2313 b2118c49 2020-07-28 stsp
2314 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2315 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2316 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2317 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2318 b2118c49 2020-07-28 stsp }
2319 b2118c49 2020-07-28 stsp
2320 b2118c49 2020-07-28 stsp return NULL;
2321 b2118c49 2020-07-28 stsp }
2322 b2118c49 2020-07-28 stsp
2323 818c7501 2019-07-11 stsp static const struct got_error *
2324 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2325 0cd1c46a 2019-03-11 stsp {
2326 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2327 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2328 0cd1c46a 2019-03-11 stsp
2329 517bab73 2019-03-11 stsp *refname = NULL;
2330 517bab73 2019-03-11 stsp
2331 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2332 b2118c49 2020-07-28 stsp if (err)
2333 b2118c49 2020-07-28 stsp return err;
2334 0cd1c46a 2019-03-11 stsp
2335 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2336 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2337 0647c563 2019-03-11 stsp *refname = NULL;
2338 0cd1c46a 2019-03-11 stsp }
2339 517bab73 2019-03-11 stsp free(uuidstr);
2340 517bab73 2019-03-11 stsp return err;
2341 517bab73 2019-03-11 stsp }
2342 517bab73 2019-03-11 stsp
2343 818c7501 2019-07-11 stsp const struct got_error *
2344 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2345 f6cd0243 2023-01-28 thomas const char *prefix)
2346 f6cd0243 2023-01-28 thomas {
2347 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2348 f6cd0243 2023-01-28 thomas }
2349 f6cd0243 2023-01-28 thomas
2350 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2351 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2352 818c7501 2019-07-11 stsp {
2353 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2354 818c7501 2019-07-11 stsp }
2355 818c7501 2019-07-11 stsp
2356 818c7501 2019-07-11 stsp static const struct got_error *
2357 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2358 818c7501 2019-07-11 stsp {
2359 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2360 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2361 818c7501 2019-07-11 stsp }
2362 818c7501 2019-07-11 stsp
2363 818c7501 2019-07-11 stsp static const struct got_error *
2364 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2365 818c7501 2019-07-11 stsp {
2366 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2367 818c7501 2019-07-11 stsp }
2368 818c7501 2019-07-11 stsp
2369 818c7501 2019-07-11 stsp static const struct got_error *
2370 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2371 818c7501 2019-07-11 stsp {
2372 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2373 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2374 818c7501 2019-07-11 stsp }
2375 818c7501 2019-07-11 stsp
2376 818c7501 2019-07-11 stsp static const struct got_error *
2377 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 818c7501 2019-07-11 stsp {
2379 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2380 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2381 0ebf8283 2019-07-24 stsp }
2382 0ebf8283 2019-07-24 stsp
2383 0ebf8283 2019-07-24 stsp static const struct got_error *
2384 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2385 0ebf8283 2019-07-24 stsp {
2386 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2387 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2388 0ebf8283 2019-07-24 stsp }
2389 0ebf8283 2019-07-24 stsp
2390 0ebf8283 2019-07-24 stsp static const struct got_error *
2391 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2392 0ebf8283 2019-07-24 stsp {
2393 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2394 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2395 0ebf8283 2019-07-24 stsp }
2396 0ebf8283 2019-07-24 stsp
2397 0ebf8283 2019-07-24 stsp static const struct got_error *
2398 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 0ebf8283 2019-07-24 stsp {
2400 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2401 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2402 818c7501 2019-07-11 stsp }
2403 818c7501 2019-07-11 stsp
2404 0ebf8283 2019-07-24 stsp static const struct got_error *
2405 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 0ebf8283 2019-07-24 stsp {
2407 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2408 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2409 0ebf8283 2019-07-24 stsp }
2410 818c7501 2019-07-11 stsp
2411 0ebf8283 2019-07-24 stsp const struct got_error *
2412 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2413 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2414 0ebf8283 2019-07-24 stsp {
2415 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2416 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2417 0ebf8283 2019-07-24 stsp *path = NULL;
2418 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2419 0ebf8283 2019-07-24 stsp }
2420 0ebf8283 2019-07-24 stsp return NULL;
2421 10604dce 2021-09-24 thomas }
2422 10604dce 2021-09-24 thomas
2423 10604dce 2021-09-24 thomas static const struct got_error *
2424 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2425 10604dce 2021-09-24 thomas {
2426 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2427 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2428 0ebf8283 2019-07-24 stsp }
2429 0ebf8283 2019-07-24 stsp
2430 10604dce 2021-09-24 thomas static const struct got_error *
2431 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2432 10604dce 2021-09-24 thomas {
2433 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2434 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2435 10604dce 2021-09-24 thomas }
2436 10604dce 2021-09-24 thomas
2437 517bab73 2019-03-11 stsp /*
2438 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2439 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2440 517bab73 2019-03-11 stsp */
2441 517bab73 2019-03-11 stsp static const struct got_error *
2442 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2443 517bab73 2019-03-11 stsp {
2444 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2445 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2446 517bab73 2019-03-11 stsp char *refname;
2447 517bab73 2019-03-11 stsp
2448 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2449 517bab73 2019-03-11 stsp if (err)
2450 517bab73 2019-03-11 stsp return err;
2451 0cd1c46a 2019-03-11 stsp
2452 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2453 0cd1c46a 2019-03-11 stsp if (err)
2454 0cd1c46a 2019-03-11 stsp goto done;
2455 0cd1c46a 2019-03-11 stsp
2456 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2457 0cd1c46a 2019-03-11 stsp done:
2458 0cd1c46a 2019-03-11 stsp free(refname);
2459 0cd1c46a 2019-03-11 stsp if (ref)
2460 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2461 3e3a69f1 2019-07-25 stsp return err;
2462 3e3a69f1 2019-07-25 stsp }
2463 3e3a69f1 2019-07-25 stsp
2464 3e3a69f1 2019-07-25 stsp static const struct got_error *
2465 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2466 3e3a69f1 2019-07-25 stsp {
2467 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2468 3e3a69f1 2019-07-25 stsp
2469 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2470 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2471 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2472 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2473 3e3a69f1 2019-07-25 stsp }
2474 9d31a1d8 2018-03-11 stsp return err;
2475 9d31a1d8 2018-03-11 stsp }
2476 9d31a1d8 2018-03-11 stsp
2477 3e3a69f1 2019-07-25 stsp
2478 ebf99748 2019-05-09 stsp static const struct got_error *
2479 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2480 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2481 ebf99748 2019-05-09 stsp {
2482 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2483 ebf99748 2019-05-09 stsp FILE *index = NULL;
2484 0cd1c46a 2019-03-11 stsp
2485 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2486 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2487 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2488 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2489 ebf99748 2019-05-09 stsp
2490 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2491 3e3a69f1 2019-07-25 stsp if (err)
2492 ebf99748 2019-05-09 stsp goto done;
2493 ebf99748 2019-05-09 stsp
2494 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2495 ebf99748 2019-05-09 stsp if (index == NULL) {
2496 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2497 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2498 ebf99748 2019-05-09 stsp } else {
2499 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2500 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2501 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2502 ebf99748 2019-05-09 stsp }
2503 ebf99748 2019-05-09 stsp done:
2504 ebf99748 2019-05-09 stsp if (err) {
2505 ebf99748 2019-05-09 stsp free(*fileindex_path);
2506 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2507 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2508 ebf99748 2019-05-09 stsp *fileindex = NULL;
2509 ebf99748 2019-05-09 stsp }
2510 ebf99748 2019-05-09 stsp return err;
2511 ebf99748 2019-05-09 stsp }
2512 c932eeeb 2019-05-22 stsp
2513 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2514 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2515 c932eeeb 2019-05-22 stsp const char *path;
2516 c932eeeb 2019-05-22 stsp size_t path_len;
2517 c932eeeb 2019-05-22 stsp const char *entry_name;
2518 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2519 a484d721 2019-06-10 stsp void *progress_arg;
2520 c932eeeb 2019-05-22 stsp };
2521 ebf99748 2019-05-09 stsp
2522 c932eeeb 2019-05-22 stsp static const struct got_error *
2523 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2524 c932eeeb 2019-05-22 stsp {
2525 1ee397ad 2019-07-12 stsp const struct got_error *err;
2526 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2527 c932eeeb 2019-05-22 stsp
2528 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2529 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2530 c932eeeb 2019-05-22 stsp return NULL;
2531 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2532 102ff934 2019-06-11 stsp return NULL;
2533 102ff934 2019-06-11 stsp
2534 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2535 a769b60b 2021-06-27 stsp return NULL;
2536 a769b60b 2021-06-27 stsp
2537 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2538 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2539 c932eeeb 2019-05-22 stsp return NULL;
2540 c932eeeb 2019-05-22 stsp
2541 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2542 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2543 edd02c5e 2019-07-11 stsp ie->path);
2544 1ee397ad 2019-07-12 stsp if (err)
2545 1ee397ad 2019-07-12 stsp return err;
2546 1ee397ad 2019-07-12 stsp }
2547 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2548 c932eeeb 2019-05-22 stsp return NULL;
2549 a615e0e7 2020-12-16 stsp }
2550 a615e0e7 2020-12-16 stsp
2551 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2552 a615e0e7 2020-12-16 stsp static const struct got_error *
2553 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2554 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2555 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2556 a615e0e7 2020-12-16 stsp {
2557 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2558 a615e0e7 2020-12-16 stsp
2559 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2560 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2561 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2562 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2563 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2564 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2565 a615e0e7 2020-12-16 stsp
2566 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2567 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2568 9c6338c4 2019-06-02 stsp }
2569 9c6338c4 2019-06-02 stsp
2570 9c6338c4 2019-06-02 stsp static const struct got_error *
2571 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2572 9c6338c4 2019-06-02 stsp {
2573 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2574 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2575 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2576 867630bb 2020-01-17 stsp struct timespec timeout;
2577 9c6338c4 2019-06-02 stsp
2578 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2579 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2580 9c6338c4 2019-06-02 stsp if (err)
2581 9c6338c4 2019-06-02 stsp goto done;
2582 9c6338c4 2019-06-02 stsp
2583 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2584 9c6338c4 2019-06-02 stsp if (err)
2585 9c6338c4 2019-06-02 stsp goto done;
2586 9c6338c4 2019-06-02 stsp
2587 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2588 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2589 9c6338c4 2019-06-02 stsp fileindex_path);
2590 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2591 9c6338c4 2019-06-02 stsp }
2592 867630bb 2020-01-17 stsp
2593 867630bb 2020-01-17 stsp /*
2594 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2595 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2596 867630bb 2020-01-17 stsp * was recorded in the file index.
2597 867630bb 2020-01-17 stsp */
2598 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2599 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2600 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2601 9c6338c4 2019-06-02 stsp done:
2602 9c6338c4 2019-06-02 stsp if (new_index)
2603 9c6338c4 2019-06-02 stsp fclose(new_index);
2604 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2605 9c6338c4 2019-06-02 stsp return err;
2606 c932eeeb 2019-05-22 stsp }
2607 6ced4176 2019-07-12 stsp
2608 6ced4176 2019-07-12 stsp static const struct got_error *
2609 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2610 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2611 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2612 945f9229 2022-04-16 thomas struct got_repository *repo)
2613 6ced4176 2019-07-12 stsp {
2614 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2615 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2616 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2617 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2618 6ced4176 2019-07-12 stsp
2619 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2620 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2621 6ced4176 2019-07-12 stsp *tree_id = NULL;
2622 6ced4176 2019-07-12 stsp
2623 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2624 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2625 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2626 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2627 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2628 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2629 6ced4176 2019-07-12 stsp goto done;
2630 6ced4176 2019-07-12 stsp }
2631 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2632 945f9229 2022-04-16 thomas worktree->path_prefix);
2633 6ced4176 2019-07-12 stsp if (err)
2634 6ced4176 2019-07-12 stsp goto done;
2635 6ced4176 2019-07-12 stsp return NULL;
2636 6ced4176 2019-07-12 stsp }
2637 6ced4176 2019-07-12 stsp
2638 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2639 6ced4176 2019-07-12 stsp
2640 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2641 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2642 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2643 6ced4176 2019-07-12 stsp goto done;
2644 6ced4176 2019-07-12 stsp }
2645 6ced4176 2019-07-12 stsp
2646 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2647 6ced4176 2019-07-12 stsp if (err)
2648 6ced4176 2019-07-12 stsp goto done;
2649 6ced4176 2019-07-12 stsp
2650 6ced4176 2019-07-12 stsp free(in_repo_path);
2651 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2652 6ced4176 2019-07-12 stsp
2653 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2654 6ced4176 2019-07-12 stsp if (err)
2655 6ced4176 2019-07-12 stsp goto done;
2656 c932eeeb 2019-05-22 stsp
2657 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2658 6ced4176 2019-07-12 stsp /* Check out a single file. */
2659 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2660 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2661 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2662 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2663 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2664 6ced4176 2019-07-12 stsp goto done;
2665 6ced4176 2019-07-12 stsp }
2666 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2667 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2668 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2669 6ced4176 2019-07-12 stsp goto done;
2670 6ced4176 2019-07-12 stsp }
2671 6ced4176 2019-07-12 stsp } else {
2672 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2673 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2674 6ced4176 2019-07-12 stsp if (err)
2675 6ced4176 2019-07-12 stsp return err;
2676 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2677 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2678 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2679 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2680 6ced4176 2019-07-12 stsp goto done;
2681 6ced4176 2019-07-12 stsp }
2682 6ced4176 2019-07-12 stsp }
2683 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2684 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2685 6ced4176 2019-07-12 stsp } else {
2686 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2687 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2688 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2689 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2690 6ced4176 2019-07-12 stsp goto done;
2691 6ced4176 2019-07-12 stsp }
2692 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2693 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2694 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2695 6ced4176 2019-07-12 stsp goto done;
2696 6ced4176 2019-07-12 stsp }
2697 6ced4176 2019-07-12 stsp }
2698 6ced4176 2019-07-12 stsp done:
2699 6ced4176 2019-07-12 stsp free(id);
2700 6ced4176 2019-07-12 stsp free(in_repo_path);
2701 6ced4176 2019-07-12 stsp if (err) {
2702 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2703 6ced4176 2019-07-12 stsp free(*tree_relpath);
2704 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2705 6ced4176 2019-07-12 stsp free(*tree_id);
2706 6ced4176 2019-07-12 stsp *tree_id = NULL;
2707 6ced4176 2019-07-12 stsp }
2708 07ed472d 2019-07-12 stsp return err;
2709 07ed472d 2019-07-12 stsp }
2710 07ed472d 2019-07-12 stsp
2711 07ed472d 2019-07-12 stsp static const struct got_error *
2712 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2713 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2714 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2715 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2716 07ed472d 2019-07-12 stsp {
2717 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2718 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2719 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2720 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2721 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2722 07ed472d 2019-07-12 stsp
2723 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2724 7f47418f 2019-12-20 stsp if (err) {
2725 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2726 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2727 7f47418f 2019-12-20 stsp goto done;
2728 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2729 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2730 7f47418f 2019-12-20 stsp if (err)
2731 7f47418f 2019-12-20 stsp return err;
2732 7f47418f 2019-12-20 stsp }
2733 07ed472d 2019-07-12 stsp
2734 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2735 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2736 07ed472d 2019-07-12 stsp if (err)
2737 07ed472d 2019-07-12 stsp goto done;
2738 07ed472d 2019-07-12 stsp
2739 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2740 07ed472d 2019-07-12 stsp if (err)
2741 07ed472d 2019-07-12 stsp goto done;
2742 07ed472d 2019-07-12 stsp
2743 07ed472d 2019-07-12 stsp if (entry_name &&
2744 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2745 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2746 07ed472d 2019-07-12 stsp goto done;
2747 07ed472d 2019-07-12 stsp }
2748 07ed472d 2019-07-12 stsp
2749 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2750 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2751 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2752 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2753 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2754 07ed472d 2019-07-12 stsp arg.repo = repo;
2755 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2756 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2757 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2758 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2759 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2760 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2761 07ed472d 2019-07-12 stsp done:
2762 07ed472d 2019-07-12 stsp if (tree)
2763 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2764 07ed472d 2019-07-12 stsp if (commit)
2765 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2766 6ced4176 2019-07-12 stsp return err;
2767 6ced4176 2019-07-12 stsp }
2768 6ced4176 2019-07-12 stsp
2769 9d31a1d8 2018-03-11 stsp const struct got_error *
2770 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2771 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2772 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2773 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2774 9d31a1d8 2018-03-11 stsp {
2775 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2776 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2777 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2778 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2779 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2780 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2781 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2782 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2783 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2784 f2ea84fa 2019-07-27 stsp int entry_type;
2785 f2ea84fa 2019-07-27 stsp char *relpath;
2786 f2ea84fa 2019-07-27 stsp char *entry_name;
2787 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2788 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2789 9d31a1d8 2018-03-11 stsp
2790 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2791 f2ea84fa 2019-07-27 stsp
2792 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2793 9d31a1d8 2018-03-11 stsp if (err)
2794 9d31a1d8 2018-03-11 stsp return err;
2795 945f9229 2022-04-16 thomas
2796 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2797 945f9229 2022-04-16 thomas worktree->base_commit_id);
2798 945f9229 2022-04-16 thomas if (err)
2799 945f9229 2022-04-16 thomas goto done;
2800 93a30277 2018-12-24 stsp
2801 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2802 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2803 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2804 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2805 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2806 f2ea84fa 2019-07-27 stsp goto done;
2807 f2ea84fa 2019-07-27 stsp }
2808 6ced4176 2019-07-12 stsp
2809 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2810 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2811 945f9229 2022-04-16 thomas worktree, repo);
2812 f2ea84fa 2019-07-27 stsp if (err) {
2813 f2ea84fa 2019-07-27 stsp free(tpd);
2814 6ced4176 2019-07-12 stsp goto done;
2815 6ced4176 2019-07-12 stsp }
2816 f2ea84fa 2019-07-27 stsp
2817 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2818 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2819 f2ea84fa 2019-07-27 stsp if (err) {
2820 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2821 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2822 f2ea84fa 2019-07-27 stsp free(tpd);
2823 f2ea84fa 2019-07-27 stsp goto done;
2824 f2ea84fa 2019-07-27 stsp }
2825 f2ea84fa 2019-07-27 stsp } else
2826 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2827 f2ea84fa 2019-07-27 stsp
2828 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2829 6ced4176 2019-07-12 stsp }
2830 6ced4176 2019-07-12 stsp
2831 51514078 2018-12-25 stsp /*
2832 51514078 2018-12-25 stsp * Read the file index.
2833 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2834 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2835 51514078 2018-12-25 stsp */
2836 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2837 8da9e5f4 2019-01-12 stsp if (err)
2838 c4cdcb68 2019-04-03 stsp goto done;
2839 c4cdcb68 2019-04-03 stsp
2840 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2841 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2842 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2843 f2ea84fa 2019-07-27 stsp
2844 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2845 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2846 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2847 f2ea84fa 2019-07-27 stsp if (err)
2848 f2ea84fa 2019-07-27 stsp break;
2849 f2ea84fa 2019-07-27 stsp
2850 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2851 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2852 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2853 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2854 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2855 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2856 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2857 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2858 f2ea84fa 2019-07-27 stsp if (err)
2859 f2ea84fa 2019-07-27 stsp break;
2860 f2ea84fa 2019-07-27 stsp
2861 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2862 711d9cd9 2019-07-12 stsp }
2863 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2864 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2865 af12c6b9 2019-06-04 stsp err = sync_err;
2866 9d31a1d8 2018-03-11 stsp done:
2867 9c6338c4 2019-06-02 stsp free(fileindex_path);
2868 edfa7d7f 2018-09-11 stsp if (tree)
2869 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2870 9d31a1d8 2018-03-11 stsp if (commit)
2871 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2872 5ade8233 2019-07-12 stsp if (fileindex)
2873 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2874 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2875 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2876 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2877 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2878 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2879 f2ea84fa 2019-07-27 stsp free(tpd);
2880 f2ea84fa 2019-07-27 stsp }
2881 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2882 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2883 9d31a1d8 2018-03-11 stsp err = unlockerr;
2884 f8d1f275 2019-02-04 stsp return err;
2885 f8d1f275 2019-02-04 stsp }
2886 f8d1f275 2019-02-04 stsp
2887 0529f8df 2023-03-10 thomas static const struct got_error *
2888 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2889 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2890 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2891 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2892 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2893 0529f8df 2023-03-10 thomas struct got_repository *repo,
2894 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2895 0529f8df 2023-03-10 thomas {
2896 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2897 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2898 0529f8df 2023-03-10 thomas
2899 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2900 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2901 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2902 0529f8df 2023-03-10 thomas restoring_missing_file,
2903 0529f8df 2023-03-10 thomas reverting_versioned_file,
2904 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2905 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2906 0529f8df 2023-03-10 thomas } else {
2907 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2908 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2909 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2910 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2911 0529f8df 2023-03-10 thomas }
2912 0529f8df 2023-03-10 thomas if (err)
2913 0529f8df 2023-03-10 thomas return err;
2914 0529f8df 2023-03-10 thomas if (ie == NULL) {
2915 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2916 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2917 0529f8df 2023-03-10 thomas if (err)
2918 0529f8df 2023-03-10 thomas return err;
2919 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2920 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2921 0529f8df 2023-03-10 thomas if (err) {
2922 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2923 0529f8df 2023-03-10 thomas return err;
2924 0529f8df 2023-03-10 thomas }
2925 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2926 0529f8df 2023-03-10 thomas if (err) {
2927 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2928 0529f8df 2023-03-10 thomas return err;
2929 0529f8df 2023-03-10 thomas }
2930 0529f8df 2023-03-10 thomas } else {
2931 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2932 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2933 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2934 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2935 0529f8df 2023-03-10 thomas if (err)
2936 0529f8df 2023-03-10 thomas return err;
2937 0529f8df 2023-03-10 thomas }
2938 0529f8df 2023-03-10 thomas
2939 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2940 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2941 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2942 0529f8df 2023-03-10 thomas }
2943 0529f8df 2023-03-10 thomas
2944 0529f8df 2023-03-10 thomas return NULL;
2945 0529f8df 2023-03-10 thomas }
2946 0529f8df 2023-03-10 thomas
2947 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2948 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2949 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2950 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2951 234035bc 2019-06-01 stsp void *progress_arg;
2952 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2953 234035bc 2019-06-01 stsp void *cancel_arg;
2954 f69721c3 2019-10-21 stsp const char *label_orig;
2955 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2956 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2957 234035bc 2019-06-01 stsp };
2958 234035bc 2019-06-01 stsp
2959 234035bc 2019-06-01 stsp static const struct got_error *
2960 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2961 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2962 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2963 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2964 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2965 234035bc 2019-06-01 stsp {
2966 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2967 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2968 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2969 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2970 234035bc 2019-06-01 stsp struct stat sb;
2971 234035bc 2019-06-01 stsp unsigned char status;
2972 234035bc 2019-06-01 stsp int local_changes_subsumed;
2973 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2974 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2975 234035bc 2019-06-01 stsp
2976 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2977 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2978 d6c87207 2019-08-02 stsp strlen(path2));
2979 1ee397ad 2019-07-12 stsp if (ie == NULL)
2980 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2981 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2982 234035bc 2019-06-01 stsp
2983 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 234035bc 2019-06-01 stsp path2) == -1)
2985 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2986 234035bc 2019-06-01 stsp
2987 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2988 7f91a133 2019-12-13 stsp repo);
2989 234035bc 2019-06-01 stsp if (err)
2990 234035bc 2019-06-01 stsp goto done;
2991 234035bc 2019-06-01 stsp
2992 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2993 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2994 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2995 234035bc 2019-06-01 stsp goto done;
2996 234035bc 2019-06-01 stsp }
2997 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2998 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2999 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3000 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3001 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3002 234035bc 2019-06-01 stsp goto done;
3003 234035bc 2019-06-01 stsp }
3004 234035bc 2019-06-01 stsp
3005 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3006 36bf999c 2020-07-23 stsp char *link_target2;
3007 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3008 36bf999c 2020-07-23 stsp if (err)
3009 36bf999c 2020-07-23 stsp goto done;
3010 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3011 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3012 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3013 36bf999c 2020-07-23 stsp free(link_target2);
3014 af57b12a 2020-07-23 stsp } else {
3015 1af628f4 2021-06-03 stsp int fd;
3016 1af628f4 2021-06-03 stsp
3017 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3018 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3019 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3020 1af628f4 2021-06-03 stsp goto done;
3021 1af628f4 2021-06-03 stsp }
3022 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3023 1af628f4 2021-06-03 stsp f_orig, blob1);
3024 1af628f4 2021-06-03 stsp if (err)
3025 1af628f4 2021-06-03 stsp goto done;
3026 1af628f4 2021-06-03 stsp
3027 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3028 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3029 1af628f4 2021-06-03 stsp goto done;
3030 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3031 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3032 1af628f4 2021-06-03 stsp if (err)
3033 1af628f4 2021-06-03 stsp goto done;
3034 1af628f4 2021-06-03 stsp
3035 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3036 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3037 1af628f4 2021-06-03 stsp if (fd == -1) {
3038 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3039 1af628f4 2021-06-03 stsp ondisk_path);
3040 1af628f4 2021-06-03 stsp goto done;
3041 1af628f4 2021-06-03 stsp }
3042 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3043 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3044 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3045 1af628f4 2021-06-03 stsp ondisk_path);
3046 1af628f4 2021-06-03 stsp close(fd);
3047 1af628f4 2021-06-03 stsp goto done;
3048 1af628f4 2021-06-03 stsp }
3049 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3050 1af628f4 2021-06-03 stsp if (err)
3051 1af628f4 2021-06-03 stsp goto done;
3052 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3053 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3054 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3055 1af628f4 2021-06-03 stsp goto done;
3056 1af628f4 2021-06-03 stsp }
3057 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3058 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3059 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3060 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3061 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3062 af57b12a 2020-07-23 stsp }
3063 234035bc 2019-06-01 stsp } else if (blob1) {
3064 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3065 d6c87207 2019-08-02 stsp strlen(path1));
3066 1ee397ad 2019-07-12 stsp if (ie == NULL)
3067 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3068 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3069 2b92fad7 2019-06-02 stsp
3070 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3071 2b92fad7 2019-06-02 stsp path1) == -1)
3072 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3073 2b92fad7 2019-06-02 stsp
3074 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3075 7f91a133 2019-12-13 stsp repo);
3076 2b92fad7 2019-06-02 stsp if (err)
3077 2b92fad7 2019-06-02 stsp goto done;
3078 2b92fad7 2019-06-02 stsp
3079 2b92fad7 2019-06-02 stsp switch (status) {
3080 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3081 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3082 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3083 1ee397ad 2019-07-12 stsp if (err)
3084 1ee397ad 2019-07-12 stsp goto done;
3085 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3086 2b92fad7 2019-06-02 stsp if (err)
3087 2b92fad7 2019-06-02 stsp goto done;
3088 2b92fad7 2019-06-02 stsp if (ie)
3089 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3090 2b92fad7 2019-06-02 stsp break;
3091 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3092 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3093 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3094 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3095 1ee397ad 2019-07-12 stsp if (err)
3096 1ee397ad 2019-07-12 stsp goto done;
3097 2b92fad7 2019-06-02 stsp if (ie)
3098 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3099 2b92fad7 2019-06-02 stsp break;
3100 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3101 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3102 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3103 e8f02263 2022-01-23 thomas off_t blob1_size;
3104 0a22ca1a 2020-09-23 stsp /*
3105 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3106 0a22ca1a 2020-09-23 stsp * exists in the repository.
3107 0a22ca1a 2020-09-23 stsp */
3108 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3109 e8f02263 2022-01-23 thomas &blob1_size, path1);
3110 0a22ca1a 2020-09-23 stsp if (err)
3111 0a22ca1a 2020-09-23 stsp goto done;
3112 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3113 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3114 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3115 0a22ca1a 2020-09-23 stsp if (err)
3116 0a22ca1a 2020-09-23 stsp goto done;
3117 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3118 0a22ca1a 2020-09-23 stsp path1);
3119 0a22ca1a 2020-09-23 stsp if (err)
3120 0a22ca1a 2020-09-23 stsp goto done;
3121 0a22ca1a 2020-09-23 stsp if (ie)
3122 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3123 0a22ca1a 2020-09-23 stsp ie);
3124 0a22ca1a 2020-09-23 stsp } else {
3125 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3126 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3127 0a22ca1a 2020-09-23 stsp }
3128 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3129 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3130 0a22ca1a 2020-09-23 stsp free(id);
3131 0a22ca1a 2020-09-23 stsp if (err)
3132 0a22ca1a 2020-09-23 stsp goto done;
3133 0a22ca1a 2020-09-23 stsp break;
3134 0a22ca1a 2020-09-23 stsp }
3135 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3136 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3137 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3138 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3139 1ee397ad 2019-07-12 stsp if (err)
3140 1ee397ad 2019-07-12 stsp goto done;
3141 2b92fad7 2019-06-02 stsp break;
3142 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3143 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3144 1ee397ad 2019-07-12 stsp if (err)
3145 1ee397ad 2019-07-12 stsp goto done;
3146 2b92fad7 2019-06-02 stsp break;
3147 2b92fad7 2019-06-02 stsp default:
3148 2b92fad7 2019-06-02 stsp break;
3149 2b92fad7 2019-06-02 stsp }
3150 234035bc 2019-06-01 stsp } else if (blob2) {
3151 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3152 234035bc 2019-06-01 stsp path2) == -1)
3153 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3154 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3155 d6c87207 2019-08-02 stsp strlen(path2));
3156 234035bc 2019-06-01 stsp if (ie) {
3157 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3158 7f91a133 2019-12-13 stsp -1, NULL, repo);
3159 234035bc 2019-06-01 stsp if (err)
3160 234035bc 2019-06-01 stsp goto done;
3161 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3162 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3163 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3164 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3165 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3166 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3167 1ee397ad 2019-07-12 stsp status, path2);
3168 234035bc 2019-06-01 stsp goto done;
3169 234035bc 2019-06-01 stsp }
3170 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3171 36bf999c 2020-07-23 stsp char *link_target2;
3172 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3173 36bf999c 2020-07-23 stsp blob2);
3174 36bf999c 2020-07-23 stsp if (err)
3175 36bf999c 2020-07-23 stsp goto done;
3176 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3177 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3178 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3179 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3180 36bf999c 2020-07-23 stsp free(link_target2);
3181 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3182 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3183 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3184 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3185 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3186 526a746f 2020-07-23 stsp a->progress_arg);
3187 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3188 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3189 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3190 526a746f 2020-07-23 stsp }
3191 dfe9fba0 2020-07-23 stsp if (err)
3192 dfe9fba0 2020-07-23 stsp goto done;
3193 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3194 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3195 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3196 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3197 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3198 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3199 234035bc 2019-06-01 stsp if (err)
3200 234035bc 2019-06-01 stsp goto done;
3201 234035bc 2019-06-01 stsp }
3202 234035bc 2019-06-01 stsp } else {
3203 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3204 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3205 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3206 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3207 234035bc 2019-06-01 stsp if (err)
3208 2b92fad7 2019-06-02 stsp goto done;
3209 234035bc 2019-06-01 stsp }
3210 234035bc 2019-06-01 stsp }
3211 234035bc 2019-06-01 stsp done:
3212 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3213 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3214 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3215 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3216 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3217 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3218 1af628f4 2021-06-03 stsp free(id_str);
3219 54d5be07 2021-06-03 stsp free(label_deriv2);
3220 234035bc 2019-06-01 stsp free(ondisk_path);
3221 234035bc 2019-06-01 stsp return err;
3222 234035bc 2019-06-01 stsp }
3223 234035bc 2019-06-01 stsp
3224 69de9dd4 2021-09-03 stsp static const struct got_error *
3225 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3226 69de9dd4 2021-09-03 stsp {
3227 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3228 69de9dd4 2021-09-03 stsp
3229 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3230 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3231 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3232 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3233 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3234 69de9dd4 2021-09-03 stsp
3235 69de9dd4 2021-09-03 stsp return NULL;
3236 69de9dd4 2021-09-03 stsp }
3237 69de9dd4 2021-09-03 stsp
3238 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3239 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3240 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3241 234035bc 2019-06-01 stsp struct got_repository *repo;
3242 234035bc 2019-06-01 stsp };
3243 234035bc 2019-06-01 stsp
3244 234035bc 2019-06-01 stsp static const struct got_error *
3245 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3246 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3247 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3248 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3249 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3250 234035bc 2019-06-01 stsp {
3251 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3252 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3253 234035bc 2019-06-01 stsp unsigned char status;
3254 234035bc 2019-06-01 stsp struct stat sb;
3255 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3256 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3257 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3258 234035bc 2019-06-01 stsp char *ondisk_path;
3259 234035bc 2019-06-01 stsp
3260 69de9dd4 2021-09-03 stsp if (id == NULL)
3261 69de9dd4 2021-09-03 stsp return NULL;
3262 234035bc 2019-06-01 stsp
3263 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3264 69de9dd4 2021-09-03 stsp if (ie == NULL)
3265 69de9dd4 2021-09-03 stsp return NULL;
3266 69de9dd4 2021-09-03 stsp
3267 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3268 234035bc 2019-06-01 stsp == -1)
3269 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3270 234035bc 2019-06-01 stsp
3271 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3272 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3273 5546d466 2021-09-02 stsp free(ondisk_path);
3274 234035bc 2019-06-01 stsp if (err)
3275 234035bc 2019-06-01 stsp return err;
3276 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3277 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3278 234035bc 2019-06-01 stsp
3279 234035bc 2019-06-01 stsp return NULL;
3280 234035bc 2019-06-01 stsp }
3281 234035bc 2019-06-01 stsp
3282 818c7501 2019-07-11 stsp static const struct got_error *
3283 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3284 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3285 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3286 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3287 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3288 234035bc 2019-06-01 stsp {
3289 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3290 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3291 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3292 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3293 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3294 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3295 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3296 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3297 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3298 234035bc 2019-06-01 stsp
3299 03415a1a 2019-06-02 stsp if (commit_id1) {
3300 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3301 945f9229 2022-04-16 thomas if (err)
3302 945f9229 2022-04-16 thomas goto done;
3303 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3304 03415a1a 2019-06-02 stsp worktree->path_prefix);
3305 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3306 03415a1a 2019-06-02 stsp goto done;
3307 69d57f3d 2020-07-31 stsp }
3308 69d57f3d 2020-07-31 stsp if (tree_id1) {
3309 69d57f3d 2020-07-31 stsp char *id_str;
3310 03415a1a 2019-06-02 stsp
3311 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3312 03415a1a 2019-06-02 stsp if (err)
3313 03415a1a 2019-06-02 stsp goto done;
3314 f69721c3 2019-10-21 stsp
3315 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3316 f69721c3 2019-10-21 stsp if (err)
3317 f69721c3 2019-10-21 stsp goto done;
3318 f69721c3 2019-10-21 stsp
3319 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3320 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3321 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3322 f69721c3 2019-10-21 stsp free(id_str);
3323 f69721c3 2019-10-21 stsp goto done;
3324 f69721c3 2019-10-21 stsp }
3325 f69721c3 2019-10-21 stsp free(id_str);
3326 a0f32f33 2022-06-13 thomas
3327 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3328 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3329 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3330 a0f32f33 2022-06-13 thomas goto done;
3331 a0f32f33 2022-06-13 thomas }
3332 03415a1a 2019-06-02 stsp }
3333 234035bc 2019-06-01 stsp
3334 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3335 945f9229 2022-04-16 thomas if (err)
3336 945f9229 2022-04-16 thomas goto done;
3337 945f9229 2022-04-16 thomas
3338 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3339 234035bc 2019-06-01 stsp worktree->path_prefix);
3340 234035bc 2019-06-01 stsp if (err)
3341 234035bc 2019-06-01 stsp goto done;
3342 234035bc 2019-06-01 stsp
3343 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3344 234035bc 2019-06-01 stsp if (err)
3345 234035bc 2019-06-01 stsp goto done;
3346 234035bc 2019-06-01 stsp
3347 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3348 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3349 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3350 19a6a6b5 2022-07-01 thomas goto done;
3351 19a6a6b5 2022-07-01 thomas }
3352 19a6a6b5 2022-07-01 thomas
3353 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3354 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3355 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3356 a0f32f33 2022-06-13 thomas goto done;
3357 a0f32f33 2022-06-13 thomas }
3358 a0f32f33 2022-06-13 thomas
3359 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3360 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3361 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3362 19a6a6b5 2022-07-01 thomas goto done;
3363 19a6a6b5 2022-07-01 thomas }
3364 19a6a6b5 2022-07-01 thomas
3365 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3366 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3367 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3368 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3369 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3370 69de9dd4 2021-09-03 stsp if (err)
3371 69de9dd4 2021-09-03 stsp goto done;
3372 69de9dd4 2021-09-03 stsp
3373 234035bc 2019-06-01 stsp arg.worktree = worktree;
3374 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3375 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3376 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3377 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3378 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3379 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3380 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3381 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3382 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3383 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3384 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3385 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3386 af12c6b9 2019-06-04 stsp err = sync_err;
3387 234035bc 2019-06-01 stsp done:
3388 945f9229 2022-04-16 thomas if (commit1)
3389 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3390 945f9229 2022-04-16 thomas if (commit2)
3391 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3392 234035bc 2019-06-01 stsp if (tree1)
3393 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3394 234035bc 2019-06-01 stsp if (tree2)
3395 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3396 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3397 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3398 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3399 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3400 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3401 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3402 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3403 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3404 f69721c3 2019-10-21 stsp free(label_orig);
3405 818c7501 2019-07-11 stsp return err;
3406 818c7501 2019-07-11 stsp }
3407 234035bc 2019-06-01 stsp
3408 818c7501 2019-07-11 stsp const struct got_error *
3409 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3410 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3411 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3412 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3413 818c7501 2019-07-11 stsp {
3414 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3415 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3416 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3417 818c7501 2019-07-11 stsp
3418 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3419 818c7501 2019-07-11 stsp if (err)
3420 818c7501 2019-07-11 stsp return err;
3421 818c7501 2019-07-11 stsp
3422 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3423 818c7501 2019-07-11 stsp if (err)
3424 818c7501 2019-07-11 stsp goto done;
3425 818c7501 2019-07-11 stsp
3426 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3427 69de9dd4 2021-09-03 stsp worktree);
3428 818c7501 2019-07-11 stsp if (err)
3429 818c7501 2019-07-11 stsp goto done;
3430 818c7501 2019-07-11 stsp
3431 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3432 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3433 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3434 818c7501 2019-07-11 stsp done:
3435 818c7501 2019-07-11 stsp if (fileindex)
3436 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3437 818c7501 2019-07-11 stsp free(fileindex_path);
3438 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3439 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3440 234035bc 2019-06-01 stsp err = unlockerr;
3441 234035bc 2019-06-01 stsp return err;
3442 234035bc 2019-06-01 stsp }
3443 234035bc 2019-06-01 stsp
3444 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3445 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3446 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3447 927df6b7 2019-02-10 stsp const char *status_path;
3448 927df6b7 2019-02-10 stsp size_t status_path_len;
3449 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3450 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3451 f8d1f275 2019-02-04 stsp void *status_arg;
3452 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3453 f8d1f275 2019-02-04 stsp void *cancel_arg;
3454 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3455 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3456 f2a9dc41 2019-12-13 tracey int report_unchanged;
3457 3143d852 2020-06-25 stsp int no_ignores;
3458 f8d1f275 2019-02-04 stsp };
3459 88d0e355 2019-08-03 stsp
3460 f8d1f275 2019-02-04 stsp static const struct got_error *
3461 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3462 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3463 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3464 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3465 927df6b7 2019-02-10 stsp {
3466 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3467 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3468 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3469 927df6b7 2019-02-10 stsp struct stat sb;
3470 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3471 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3472 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3473 927df6b7 2019-02-10 stsp
3474 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3475 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3476 98eaaa12 2019-08-03 stsp if (err)
3477 98eaaa12 2019-08-03 stsp return err;
3478 98eaaa12 2019-08-03 stsp
3479 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3480 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3481 98eaaa12 2019-08-03 stsp return NULL;
3482 98eaaa12 2019-08-03 stsp
3483 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3484 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3485 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3486 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3487 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3488 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3489 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3490 43010591 2023-02-17 thomas &staged_blob_id, ie);
3491 98eaaa12 2019-08-03 stsp }
3492 98eaaa12 2019-08-03 stsp
3493 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3494 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3495 927df6b7 2019-02-10 stsp }
3496 927df6b7 2019-02-10 stsp
3497 927df6b7 2019-02-10 stsp static const struct got_error *
3498 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3499 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3500 f8d1f275 2019-02-04 stsp {
3501 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3502 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3503 f8d1f275 2019-02-04 stsp char *abspath;
3504 f8d1f275 2019-02-04 stsp
3505 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3507 0584f854 2019-04-06 stsp
3508 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3509 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3510 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3511 927df6b7 2019-02-10 stsp return NULL;
3512 927df6b7 2019-02-10 stsp
3513 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3514 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3515 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3517 f8d1f275 2019-02-04 stsp } else {
3518 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3519 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3520 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3521 f8d1f275 2019-02-04 stsp }
3522 f8d1f275 2019-02-04 stsp
3523 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3524 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3525 f8d1f275 2019-02-04 stsp free(abspath);
3526 9d31a1d8 2018-03-11 stsp return err;
3527 9d31a1d8 2018-03-11 stsp }
3528 f8d1f275 2019-02-04 stsp
3529 f8d1f275 2019-02-04 stsp static const struct got_error *
3530 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3531 f8d1f275 2019-02-04 stsp {
3532 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3533 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3534 2ec1f75b 2019-03-26 stsp unsigned char status;
3535 927df6b7 2019-02-10 stsp
3536 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3537 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3538 0584f854 2019-04-06 stsp
3539 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3540 927df6b7 2019-02-10 stsp return NULL;
3541 927df6b7 2019-02-10 stsp
3542 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3543 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3544 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3545 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3546 2ec1f75b 2019-03-26 stsp else
3547 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3548 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3549 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3550 6841da00 2019-08-08 stsp }
3551 6841da00 2019-08-08 stsp
3552 ef20f542 2022-06-26 thomas static void
3553 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3554 6841da00 2019-08-08 stsp {
3555 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3556 6841da00 2019-08-08 stsp
3557 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3558 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3559 21c2d8be 2023-01-10 thomas
3560 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3561 6841da00 2019-08-08 stsp }
3562 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3563 6841da00 2019-08-08 stsp }
3564 6841da00 2019-08-08 stsp
3565 6841da00 2019-08-08 stsp static const struct got_error *
3566 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3567 6841da00 2019-08-08 stsp {
3568 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3569 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3570 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3571 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3572 6841da00 2019-08-08 stsp size_t linesize = 0;
3573 6841da00 2019-08-08 stsp ssize_t linelen;
3574 6841da00 2019-08-08 stsp
3575 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3576 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3577 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3578 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3579 6841da00 2019-08-08 stsp
3580 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3581 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3582 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3583 bd8de430 2019-10-04 stsp
3584 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3585 bd8de430 2019-10-04 stsp if (line[0] == '#')
3586 bd8de430 2019-10-04 stsp continue;
3587 bd8de430 2019-10-04 stsp
3588 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3589 bd8de430 2019-10-04 stsp if (line[0] == '!')
3590 bd8de430 2019-10-04 stsp continue;
3591 bd8de430 2019-10-04 stsp
3592 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3593 6841da00 2019-08-08 stsp line) == -1) {
3594 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3595 6841da00 2019-08-08 stsp goto done;
3596 6841da00 2019-08-08 stsp }
3597 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3598 6841da00 2019-08-08 stsp if (err)
3599 6841da00 2019-08-08 stsp goto done;
3600 6841da00 2019-08-08 stsp }
3601 6841da00 2019-08-08 stsp if (ferror(f)) {
3602 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3603 6841da00 2019-08-08 stsp goto done;
3604 6841da00 2019-08-08 stsp }
3605 6841da00 2019-08-08 stsp
3606 6841da00 2019-08-08 stsp dirpath = strdup(path);
3607 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3608 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3609 6841da00 2019-08-08 stsp goto done;
3610 6841da00 2019-08-08 stsp }
3611 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3612 6841da00 2019-08-08 stsp done:
3613 6841da00 2019-08-08 stsp free(line);
3614 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3615 6841da00 2019-08-08 stsp free(dirpath);
3616 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3617 6841da00 2019-08-08 stsp }
3618 6841da00 2019-08-08 stsp return err;
3619 1b5d300f 2023-02-20 thomas }
3620 1b5d300f 2023-02-20 thomas
3621 1b5d300f 2023-02-20 thomas static int
3622 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3623 1b5d300f 2023-02-20 thomas int flags)
3624 1b5d300f 2023-02-20 thomas {
3625 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3626 1b5d300f 2023-02-20 thomas
3627 1b5d300f 2023-02-20 thomas /*
3628 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3629 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3630 1b5d300f 2023-02-20 thomas */
3631 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3632 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3633 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3634 1b5d300f 2023-02-20 thomas
3635 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3636 1b5d300f 2023-02-20 thomas }
3637 1b5d300f 2023-02-20 thomas
3638 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3639 6841da00 2019-08-08 stsp }
3640 6841da00 2019-08-08 stsp
3641 ef20f542 2022-06-26 thomas static int
3642 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3643 6841da00 2019-08-08 stsp {
3644 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3645 bd8de430 2019-10-04 stsp
3646 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3647 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3648 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3649 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3650 bd8de430 2019-10-04 stsp
3651 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3652 1b5d300f 2023-02-20 thomas const char *p;
3653 6841da00 2019-08-08 stsp
3654 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3655 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3656 bd8de430 2019-10-04 stsp continue;
3657 bd8de430 2019-10-04 stsp p = path;
3658 bd8de430 2019-10-04 stsp while (*p) {
3659 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3660 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3661 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3662 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3663 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3664 bd8de430 2019-10-04 stsp p++;
3665 bd8de430 2019-10-04 stsp while (*p == '/')
3666 bd8de430 2019-10-04 stsp p++;
3667 bd8de430 2019-10-04 stsp continue;
3668 bd8de430 2019-10-04 stsp }
3669 bd8de430 2019-10-04 stsp return 1;
3670 bd8de430 2019-10-04 stsp }
3671 bd8de430 2019-10-04 stsp }
3672 bd8de430 2019-10-04 stsp }
3673 bd8de430 2019-10-04 stsp
3674 6841da00 2019-08-08 stsp /*
3675 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3676 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3677 6841da00 2019-08-08 stsp * ignores backwards.
3678 6841da00 2019-08-08 stsp */
3679 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3680 6841da00 2019-08-08 stsp while (pe) {
3681 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3682 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3683 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3684 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3685 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3686 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3687 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3688 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3689 1b5d300f 2023-02-20 thomas path, flags))
3690 6841da00 2019-08-08 stsp continue;
3691 6841da00 2019-08-08 stsp return 1;
3692 6841da00 2019-08-08 stsp }
3693 6841da00 2019-08-08 stsp }
3694 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3695 6841da00 2019-08-08 stsp }
3696 6841da00 2019-08-08 stsp
3697 6841da00 2019-08-08 stsp return 0;
3698 6841da00 2019-08-08 stsp }
3699 6841da00 2019-08-08 stsp
3700 6841da00 2019-08-08 stsp static const struct got_error *
3701 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3702 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3703 6841da00 2019-08-08 stsp {
3704 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3705 6841da00 2019-08-08 stsp char *ignorespath;
3706 886cec17 2019-12-15 stsp int fd = -1;
3707 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3708 6841da00 2019-08-08 stsp
3709 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3710 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3711 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3712 6841da00 2019-08-08 stsp
3713 886cec17 2019-12-15 stsp if (dirfd != -1) {
3714 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3715 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3716 886cec17 2019-12-15 stsp if (fd == -1) {
3717 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3718 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3719 886cec17 2019-12-15 stsp ignorespath);
3720 886cec17 2019-12-15 stsp } else {
3721 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3722 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3723 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3724 886cec17 2019-12-15 stsp ignorespath);
3725 886cec17 2019-12-15 stsp else {
3726 886cec17 2019-12-15 stsp fd = -1;
3727 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3728 886cec17 2019-12-15 stsp }
3729 886cec17 2019-12-15 stsp }
3730 886cec17 2019-12-15 stsp } else {
3731 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3732 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3733 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3734 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3735 886cec17 2019-12-15 stsp ignorespath);
3736 886cec17 2019-12-15 stsp } else
3737 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3738 886cec17 2019-12-15 stsp }
3739 6841da00 2019-08-08 stsp
3740 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3741 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3742 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3743 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3744 6841da00 2019-08-08 stsp free(ignorespath);
3745 6841da00 2019-08-08 stsp return err;
3746 f8d1f275 2019-02-04 stsp }
3747 f8d1f275 2019-02-04 stsp
3748 f8d1f275 2019-02-04 stsp static const struct got_error *
3749 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3750 6092c299 2021-10-04 thomas int dirfd)
3751 f8d1f275 2019-02-04 stsp {
3752 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3753 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3754 f8d1f275 2019-02-04 stsp char *path = NULL;
3755 6092c299 2021-10-04 thomas
3756 6092c299 2021-10-04 thomas if (ignore != NULL)
3757 6092c299 2021-10-04 thomas *ignore = 0;
3758 f8d1f275 2019-02-04 stsp
3759 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3760 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3761 0584f854 2019-04-06 stsp
3762 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3763 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3764 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3765 f8d1f275 2019-02-04 stsp } else {
3766 f8d1f275 2019-02-04 stsp path = de->d_name;
3767 f8d1f275 2019-02-04 stsp }
3768 f8d1f275 2019-02-04 stsp
3769 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3770 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3771 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3772 6092c299 2021-10-04 thomas *ignore = 1;
3773 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3774 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3775 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3776 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3777 f8d1f275 2019-02-04 stsp if (parent_path[0])
3778 f8d1f275 2019-02-04 stsp free(path);
3779 b72f483a 2019-02-05 stsp return err;
3780 f8d1f275 2019-02-04 stsp }
3781 f8d1f275 2019-02-04 stsp
3782 347d1d3e 2019-07-12 stsp static const struct got_error *
3783 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3784 3143d852 2020-06-25 stsp {
3785 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3786 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3787 3143d852 2020-06-25 stsp
3788 3143d852 2020-06-25 stsp if (a->no_ignores)
3789 3143d852 2020-06-25 stsp return NULL;
3790 3143d852 2020-06-25 stsp
3791 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3792 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3793 3143d852 2020-06-25 stsp if (err)
3794 3143d852 2020-06-25 stsp return err;
3795 3143d852 2020-06-25 stsp
3796 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3797 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3798 3143d852 2020-06-25 stsp
3799 3143d852 2020-06-25 stsp return err;
3800 3143d852 2020-06-25 stsp }
3801 3143d852 2020-06-25 stsp
3802 3143d852 2020-06-25 stsp static const struct got_error *
3803 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3804 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3805 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3806 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3807 abb4604f 2019-07-27 stsp {
3808 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3809 abb4604f 2019-07-27 stsp struct stat sb;
3810 ff56836b 2021-07-08 stsp
3811 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3812 abb4604f 2019-07-27 stsp if (ie)
3813 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3814 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3815 abb4604f 2019-07-27 stsp
3816 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3817 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3818 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3819 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3820 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3821 abb4604f 2019-07-27 stsp }
3822 abb4604f 2019-07-27 stsp
3823 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3824 fe1d8685 2022-02-12 thomas return NULL;
3825 fe1d8685 2022-02-12 thomas
3826 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3827 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3828 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3829 abb4604f 2019-07-27 stsp
3830 abb4604f 2019-07-27 stsp return NULL;
3831 3143d852 2020-06-25 stsp }
3832 3143d852 2020-06-25 stsp
3833 3143d852 2020-06-25 stsp static const struct got_error *
3834 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3835 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3836 3143d852 2020-06-25 stsp {
3837 3143d852 2020-06-25 stsp const struct got_error *err;
3838 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3839 3143d852 2020-06-25 stsp
3840 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3841 3143d852 2020-06-25 stsp ".cvsignore");
3842 3143d852 2020-06-25 stsp if (err)
3843 3143d852 2020-06-25 stsp return err;
3844 3143d852 2020-06-25 stsp
3845 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3846 3143d852 2020-06-25 stsp ".gitignore");
3847 3143d852 2020-06-25 stsp if (err)
3848 3143d852 2020-06-25 stsp return err;
3849 3143d852 2020-06-25 stsp
3850 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3851 3143d852 2020-06-25 stsp if (err) {
3852 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3853 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3854 3143d852 2020-06-25 stsp return err;
3855 3143d852 2020-06-25 stsp }
3856 3143d852 2020-06-25 stsp for (;;) {
3857 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3858 3143d852 2020-06-25 stsp ".cvsignore");
3859 3143d852 2020-06-25 stsp if (err)
3860 3143d852 2020-06-25 stsp break;
3861 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3862 3143d852 2020-06-25 stsp ".gitignore");
3863 3143d852 2020-06-25 stsp if (err)
3864 3143d852 2020-06-25 stsp break;
3865 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3866 3143d852 2020-06-25 stsp if (err) {
3867 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3868 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3869 3143d852 2020-06-25 stsp break;
3870 3143d852 2020-06-25 stsp }
3871 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3872 ff56836b 2021-07-08 stsp break;
3873 b737c85a 2020-06-26 stsp free(parent_path);
3874 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3875 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3876 3143d852 2020-06-25 stsp }
3877 3143d852 2020-06-25 stsp
3878 b737c85a 2020-06-26 stsp free(parent_path);
3879 b737c85a 2020-06-26 stsp free(next_parent_path);
3880 3143d852 2020-06-25 stsp return err;
3881 abb4604f 2019-07-27 stsp }
3882 f8836425 2023-04-14 thomas
3883 f8836425 2023-04-14 thomas struct find_missing_children_args {
3884 f8836425 2023-04-14 thomas const char *parent_path;
3885 f8836425 2023-04-14 thomas size_t parent_len;
3886 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3887 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3888 f8836425 2023-04-14 thomas void *cancel_arg;
3889 f8836425 2023-04-14 thomas };
3890 f8836425 2023-04-14 thomas
3891 abb4604f 2019-07-27 stsp static const struct got_error *
3892 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3893 f8836425 2023-04-14 thomas {
3894 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3895 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3896 f8836425 2023-04-14 thomas
3897 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3898 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3899 f8836425 2023-04-14 thomas if (err)
3900 f8836425 2023-04-14 thomas return err;
3901 f8836425 2023-04-14 thomas }
3902 f8836425 2023-04-14 thomas
3903 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3904 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3905 f8836425 2023-04-14 thomas
3906 f8836425 2023-04-14 thomas return err;
3907 f8836425 2023-04-14 thomas }
3908 f8836425 2023-04-14 thomas
3909 f8836425 2023-04-14 thomas static const struct got_error *
3910 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3911 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3912 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3913 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3914 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3915 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3916 f8836425 2023-04-14 thomas {
3917 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3918 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3919 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3920 f8836425 2023-04-14 thomas
3921 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3922 f8836425 2023-04-14 thomas if (cancel_cb) {
3923 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3924 f8836425 2023-04-14 thomas if (err)
3925 f8836425 2023-04-14 thomas break;
3926 f8836425 2023-04-14 thomas }
3927 f8836425 2023-04-14 thomas
3928 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3929 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3930 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3931 f8836425 2023-04-14 thomas ondisk_path = NULL;
3932 f8836425 2023-04-14 thomas break;
3933 f8836425 2023-04-14 thomas }
3934 f8836425 2023-04-14 thomas
3935 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3936 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3937 f8836425 2023-04-14 thomas ignores, no_ignores);
3938 f8836425 2023-04-14 thomas if (err)
3939 f8836425 2023-04-14 thomas break;
3940 f8836425 2023-04-14 thomas
3941 f8836425 2023-04-14 thomas free(ondisk_path);
3942 f8836425 2023-04-14 thomas ondisk_path = NULL;
3943 f8836425 2023-04-14 thomas }
3944 f8836425 2023-04-14 thomas
3945 f8836425 2023-04-14 thomas free(ondisk_path);
3946 f8836425 2023-04-14 thomas return err;
3947 f8836425 2023-04-14 thomas }
3948 f8836425 2023-04-14 thomas
3949 f8836425 2023-04-14 thomas static const struct got_error *
3950 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3951 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3952 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3953 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3954 f2a9dc41 2019-12-13 tracey int report_unchanged)
3955 f8d1f275 2019-02-04 stsp {
3956 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3957 6fc93f37 2019-12-13 stsp int fd = -1;
3958 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3959 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3960 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3961 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3962 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3963 3143d852 2020-06-25 stsp
3964 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3965 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3966 f8d1f275 2019-02-04 stsp
3967 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3968 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3969 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3970 8dc303cc 2019-07-27 stsp
3971 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3972 a78810f8 2022-03-13 thomas if (ie) {
3973 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3974 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3975 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3976 a78810f8 2022-03-13 thomas goto done;
3977 f8836425 2023-04-14 thomas } else {
3978 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3979 f8836425 2023-04-14 thomas fmca.parent_path = path;
3980 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3981 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3982 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3983 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3984 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3985 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3986 f8836425 2023-04-14 thomas if (err)
3987 f8836425 2023-04-14 thomas goto done;
3988 a78810f8 2022-03-13 thomas }
3989 a78810f8 2022-03-13 thomas
3990 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3991 6fc93f37 2019-12-13 stsp if (fd == -1) {
3992 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3993 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3994 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3995 ff56836b 2021-07-08 stsp else {
3996 ff56836b 2021-07-08 stsp if (!no_ignores) {
3997 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3998 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3999 ff56836b 2021-07-08 stsp if (err)
4000 ff56836b 2021-07-08 stsp goto done;
4001 ff56836b 2021-07-08 stsp }
4002 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4003 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4004 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4005 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4006 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4007 f8836425 2023-04-14 thomas if (err)
4008 f8836425 2023-04-14 thomas goto done;
4009 f8836425 2023-04-14 thomas } else {
4010 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4011 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4012 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4013 f8836425 2023-04-14 thomas &ignores, no_ignores,
4014 f8836425 2023-04-14 thomas status_cb, status_arg,
4015 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4016 6f97aa83 2023-04-14 thomas if (err)
4017 6f97aa83 2023-04-14 thomas goto done;
4018 f8836425 2023-04-14 thomas }
4019 ff56836b 2021-07-08 stsp }
4020 abb4604f 2019-07-27 stsp } else {
4021 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4022 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4023 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4024 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4025 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4026 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4027 abb4604f 2019-07-27 stsp arg.status_path = path;
4028 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4029 abb4604f 2019-07-27 stsp arg.repo = repo;
4030 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4031 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4032 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4033 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4034 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4035 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4036 022fae89 2019-12-06 tracey if (!no_ignores) {
4037 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4038 3143d852 2020-06-25 stsp worktree->root_path, path);
4039 3143d852 2020-06-25 stsp if (err)
4040 3143d852 2020-06-25 stsp goto done;
4041 022fae89 2019-12-06 tracey }
4042 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4043 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4044 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4045 927df6b7 2019-02-10 stsp }
4046 3143d852 2020-06-25 stsp done:
4047 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4048 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4049 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4050 927df6b7 2019-02-10 stsp free(ondisk_path);
4051 347d1d3e 2019-07-12 stsp return err;
4052 347d1d3e 2019-07-12 stsp }
4053 347d1d3e 2019-07-12 stsp
4054 347d1d3e 2019-07-12 stsp const struct got_error *
4055 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4056 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4057 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4058 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4059 347d1d3e 2019-07-12 stsp {
4060 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4061 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4062 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4063 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4064 347d1d3e 2019-07-12 stsp
4065 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4066 347d1d3e 2019-07-12 stsp if (err)
4067 347d1d3e 2019-07-12 stsp return err;
4068 347d1d3e 2019-07-12 stsp
4069 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4070 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4071 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4072 f6343036 2021-06-22 stsp no_ignores, 0);
4073 72ea6654 2019-07-27 stsp if (err)
4074 72ea6654 2019-07-27 stsp break;
4075 72ea6654 2019-07-27 stsp }
4076 f8d1f275 2019-02-04 stsp free(fileindex_path);
4077 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4078 f8d1f275 2019-02-04 stsp return err;
4079 f8d1f275 2019-02-04 stsp }
4080 6c7ab921 2019-03-18 stsp
4081 6c7ab921 2019-03-18 stsp const struct got_error *
4082 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4083 6c7ab921 2019-03-18 stsp const char *arg)
4084 6c7ab921 2019-03-18 stsp {
4085 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4086 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4087 6c7ab921 2019-03-18 stsp size_t len;
4088 00bb5ea0 2020-07-23 stsp struct stat sb;
4089 01740607 2020-11-04 stsp char *abspath = NULL;
4090 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4091 6c7ab921 2019-03-18 stsp
4092 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4093 6c7ab921 2019-03-18 stsp
4094 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4095 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4096 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4097 00bb5ea0 2020-07-23 stsp
4098 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4099 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4100 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4101 00bb5ea0 2020-07-23 stsp goto done;
4102 00bb5ea0 2020-07-23 stsp }
4103 727173c3 2020-11-06 stsp sb.st_mode = 0;
4104 00bb5ea0 2020-07-23 stsp }
4105 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4106 00bb5ea0 2020-07-23 stsp /*
4107 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4108 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4109 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4110 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4111 00bb5ea0 2020-07-23 stsp */
4112 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4113 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4114 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4115 00bb5ea0 2020-07-23 stsp goto done;
4116 00bb5ea0 2020-07-23 stsp }
4117 00bb5ea0 2020-07-23 stsp
4118 00bb5ea0 2020-07-23 stsp }
4119 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4120 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4121 00bb5ea0 2020-07-23 stsp if (err)
4122 d0710d08 2019-07-22 stsp goto done;
4123 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4124 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4125 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4126 00bb5ea0 2020-07-23 stsp goto done;
4127 00bb5ea0 2020-07-23 stsp }
4128 00bb5ea0 2020-07-23 stsp } else {
4129 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4130 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4131 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4132 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4133 00bb5ea0 2020-07-23 stsp goto done;
4134 00bb5ea0 2020-07-23 stsp }
4135 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4136 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4137 01740607 2020-11-04 stsp goto done;
4138 01740607 2020-11-04 stsp }
4139 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4140 01740607 2020-11-04 stsp sizeof(canonpath));
4141 01740607 2020-11-04 stsp if (err)
4142 00bb5ea0 2020-07-23 stsp goto done;
4143 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4144 01740607 2020-11-04 stsp if (resolved == NULL) {
4145 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4146 01740607 2020-11-04 stsp goto done;
4147 00bb5ea0 2020-07-23 stsp }
4148 d0710d08 2019-07-22 stsp }
4149 d0710d08 2019-07-22 stsp }
4150 6c7ab921 2019-03-18 stsp
4151 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4152 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4153 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4154 6c7ab921 2019-03-18 stsp goto done;
4155 6c7ab921 2019-03-18 stsp }
4156 6c7ab921 2019-03-18 stsp
4157 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4158 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4159 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4160 f6d88e1a 2019-05-29 stsp if (err)
4161 f6d88e1a 2019-05-29 stsp goto done;
4162 f6d88e1a 2019-05-29 stsp } else {
4163 f6d88e1a 2019-05-29 stsp path = strdup("");
4164 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4165 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4166 f6d88e1a 2019-05-29 stsp goto done;
4167 f6d88e1a 2019-05-29 stsp }
4168 6c7ab921 2019-03-18 stsp }
4169 6c7ab921 2019-03-18 stsp
4170 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4171 6c7ab921 2019-03-18 stsp len = strlen(path);
4172 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4173 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4174 6c7ab921 2019-03-18 stsp len--;
4175 6c7ab921 2019-03-18 stsp }
4176 6c7ab921 2019-03-18 stsp done:
4177 01740607 2020-11-04 stsp free(abspath);
4178 6c7ab921 2019-03-18 stsp free(resolved);
4179 d0710d08 2019-07-22 stsp free(cwd);
4180 6c7ab921 2019-03-18 stsp if (err == NULL)
4181 6c7ab921 2019-03-18 stsp *wt_path = path;
4182 6c7ab921 2019-03-18 stsp else
4183 6c7ab921 2019-03-18 stsp free(path);
4184 d00136be 2019-03-26 stsp return err;
4185 d00136be 2019-03-26 stsp }
4186 d00136be 2019-03-26 stsp
4187 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4188 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4189 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4190 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4191 4e68cba3 2019-11-23 stsp void *progress_arg;
4192 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4193 4e68cba3 2019-11-23 stsp };
4194 4e68cba3 2019-11-23 stsp
4195 b21ebdb0 2023-06-22 thomas static int
4196 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4197 b21ebdb0 2023-06-22 thomas {
4198 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4199 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4200 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4201 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4202 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4203 b21ebdb0 2023-06-22 thomas }
4204 b21ebdb0 2023-06-22 thomas
4205 4e68cba3 2019-11-23 stsp static const struct got_error *
4206 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4207 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4208 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4209 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4210 07f5b47a 2019-06-02 stsp {
4211 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4212 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4213 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4214 6d022e97 2019-08-04 stsp struct stat sb;
4215 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4216 07f5b47a 2019-06-02 stsp
4217 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4218 dbb83fbd 2019-12-12 stsp relpath) == -1)
4219 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4220 dbb83fbd 2019-12-12 stsp
4221 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4222 6d022e97 2019-08-04 stsp if (ie) {
4223 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4224 12463d8b 2019-12-13 stsp de_name, a->repo);
4225 6d022e97 2019-08-04 stsp if (err)
4226 dbb83fbd 2019-12-12 stsp goto done;
4227 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4228 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4229 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4230 dbb83fbd 2019-12-12 stsp goto done;
4231 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4232 dbb83fbd 2019-12-12 stsp if (err)
4233 dbb83fbd 2019-12-12 stsp goto done;
4234 6d022e97 2019-08-04 stsp }
4235 07f5b47a 2019-06-02 stsp
4236 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4237 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4238 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4239 84bf00a6 2022-02-12 thomas else
4240 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4241 dbb83fbd 2019-12-12 stsp goto done;
4242 4e68cba3 2019-11-23 stsp }
4243 07f5b47a 2019-06-02 stsp
4244 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4245 4e68cba3 2019-11-23 stsp if (err)
4246 4e68cba3 2019-11-23 stsp goto done;
4247 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4248 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4249 3969253a 2020-03-07 stsp if (err) {
4250 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4251 3969253a 2020-03-07 stsp goto done;
4252 3969253a 2020-03-07 stsp }
4253 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4254 07f5b47a 2019-06-02 stsp if (err) {
4255 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4256 4e68cba3 2019-11-23 stsp goto done;
4257 07f5b47a 2019-06-02 stsp }
4258 4e68cba3 2019-11-23 stsp done:
4259 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4260 dbb83fbd 2019-12-12 stsp if (err)
4261 dbb83fbd 2019-12-12 stsp return err;
4262 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4263 dbb83fbd 2019-12-12 stsp return NULL;
4264 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4265 07f5b47a 2019-06-02 stsp }
4266 07f5b47a 2019-06-02 stsp
4267 d00136be 2019-03-26 stsp const struct got_error *
4268 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4269 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4270 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4271 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4272 d00136be 2019-03-26 stsp {
4273 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4274 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4275 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4276 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4277 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4278 d00136be 2019-03-26 stsp
4279 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4280 d00136be 2019-03-26 stsp if (err)
4281 d00136be 2019-03-26 stsp return err;
4282 d00136be 2019-03-26 stsp
4283 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4284 d00136be 2019-03-26 stsp if (err)
4285 d00136be 2019-03-26 stsp goto done;
4286 d00136be 2019-03-26 stsp
4287 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4288 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4289 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4290 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4291 4e68cba3 2019-11-23 stsp saa.repo = repo;
4292 4e68cba3 2019-11-23 stsp
4293 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4294 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4295 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4296 1dd54920 2019-05-11 stsp if (err)
4297 af12c6b9 2019-06-04 stsp break;
4298 1dd54920 2019-05-11 stsp }
4299 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4300 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4301 af12c6b9 2019-06-04 stsp err = sync_err;
4302 d00136be 2019-03-26 stsp done:
4303 fb399478 2019-07-12 stsp free(fileindex_path);
4304 d00136be 2019-03-26 stsp if (fileindex)
4305 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4306 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4307 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4308 d00136be 2019-03-26 stsp err = unlockerr;
4309 6c7ab921 2019-03-18 stsp return err;
4310 6c7ab921 2019-03-18 stsp }
4311 17ed4618 2019-06-02 stsp
4312 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4313 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4314 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4315 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4316 f2a9dc41 2019-12-13 tracey void *progress_arg;
4317 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4318 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4319 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4320 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4321 8ec7352a 2023-06-01 thomas const char *status_path;
4322 8ec7352a 2023-06-01 thomas size_t status_path_len;
4323 766841c2 2020-08-13 stsp const char *status_codes;
4324 f2a9dc41 2019-12-13 tracey };
4325 f2a9dc41 2019-12-13 tracey
4326 f2a9dc41 2019-12-13 tracey static const struct got_error *
4327 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4328 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4329 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4330 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4331 17ed4618 2019-06-02 stsp {
4332 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4333 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4334 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4335 17ed4618 2019-06-02 stsp struct stat sb;
4336 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4337 d64cc78a 2022-01-25 thomas
4338 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4339 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4340 d64cc78a 2022-01-25 thomas return NULL;
4341 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4342 d64cc78a 2022-01-25 thomas }
4343 17ed4618 2019-06-02 stsp
4344 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4345 17ed4618 2019-06-02 stsp if (ie == NULL)
4346 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4347 2ec1f75b 2019-03-26 stsp
4348 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4349 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4350 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4351 9acbc4fa 2019-08-03 stsp return NULL;
4352 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4353 9acbc4fa 2019-08-03 stsp }
4354 9acbc4fa 2019-08-03 stsp
4355 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4356 f2a9dc41 2019-12-13 tracey relpath) == -1)
4357 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4358 f2a9dc41 2019-12-13 tracey
4359 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4360 12463d8b 2019-12-13 stsp a->repo);
4361 17ed4618 2019-06-02 stsp if (err)
4362 f2a9dc41 2019-12-13 tracey goto done;
4363 17ed4618 2019-06-02 stsp
4364 766841c2 2020-08-13 stsp if (a->status_codes) {
4365 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4366 766841c2 2020-08-13 stsp int i;
4367 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4368 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4369 766841c2 2020-08-13 stsp break;
4370 766841c2 2020-08-13 stsp }
4371 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4372 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4373 766841c2 2020-08-13 stsp free(ondisk_path);
4374 766841c2 2020-08-13 stsp return NULL;
4375 766841c2 2020-08-13 stsp }
4376 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4377 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4378 766841c2 2020-08-13 stsp static char msg[64];
4379 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4380 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4381 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4382 766841c2 2020-08-13 stsp goto done;
4383 766841c2 2020-08-13 stsp }
4384 766841c2 2020-08-13 stsp }
4385 766841c2 2020-08-13 stsp
4386 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4387 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4388 f2a9dc41 2019-12-13 tracey goto done;
4389 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4390 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4391 f2a9dc41 2019-12-13 tracey goto done;
4392 f2a9dc41 2019-12-13 tracey }
4393 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4394 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4395 d64cc78a 2022-01-25 thomas goto done;
4396 d64cc78a 2022-01-25 thomas }
4397 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4398 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4399 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4400 f2a9dc41 2019-12-13 tracey goto done;
4401 f2a9dc41 2019-12-13 tracey }
4402 17ed4618 2019-06-02 stsp }
4403 17ed4618 2019-06-02 stsp
4404 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4405 20a2ad1c 2020-10-20 stsp size_t root_len;
4406 20a2ad1c 2020-10-20 stsp
4407 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4408 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4409 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4410 12463d8b 2019-12-13 stsp ondisk_path);
4411 12463d8b 2019-12-13 stsp goto done;
4412 12463d8b 2019-12-13 stsp }
4413 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4414 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4415 12463d8b 2019-12-13 stsp goto done;
4416 15341bfd 2020-03-05 tracey }
4417 15341bfd 2020-03-05 tracey
4418 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4419 20a2ad1c 2020-10-20 stsp do {
4420 20a2ad1c 2020-10-20 stsp char *parent;
4421 8ec7352a 2023-06-01 thomas
4422 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4423 20a2ad1c 2020-10-20 stsp if (err)
4424 2513f20a 2020-10-20 stsp goto done;
4425 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4426 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4427 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4428 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4429 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4430 8ec7352a 2023-06-01 thomas a->status_path_len))
4431 8ec7352a 2023-06-01 thomas break;
4432 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4433 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4434 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4435 20a2ad1c 2020-10-20 stsp ondisk_path);
4436 15341bfd 2020-03-05 tracey break;
4437 15341bfd 2020-03-05 tracey }
4438 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4439 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4440 f2a9dc41 2019-12-13 tracey }
4441 17ed4618 2019-06-02 stsp
4442 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4443 f2a9dc41 2019-12-13 tracey done:
4444 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4445 f2a9dc41 2019-12-13 tracey if (err)
4446 f2a9dc41 2019-12-13 tracey return err;
4447 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4448 f2a9dc41 2019-12-13 tracey return NULL;
4449 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4450 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4451 17ed4618 2019-06-02 stsp }
4452 17ed4618 2019-06-02 stsp
4453 2ec1f75b 2019-03-26 stsp const struct got_error *
4454 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4455 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4456 766841c2 2020-08-13 stsp const char *status_codes,
4457 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4458 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4459 2ec1f75b 2019-03-26 stsp {
4460 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4461 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4462 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4463 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4464 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4465 2ec1f75b 2019-03-26 stsp
4466 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4467 2ec1f75b 2019-03-26 stsp if (err)
4468 2ec1f75b 2019-03-26 stsp return err;
4469 2ec1f75b 2019-03-26 stsp
4470 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4471 2ec1f75b 2019-03-26 stsp if (err)
4472 2ec1f75b 2019-03-26 stsp goto done;
4473 f2a9dc41 2019-12-13 tracey
4474 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4475 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4476 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4477 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4478 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4479 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4480 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4481 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4482 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4483 2ec1f75b 2019-03-26 stsp
4484 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4485 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4486 8ec7352a 2023-06-01 thomas
4487 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4488 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4489 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4490 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4491 8ec7352a 2023-06-01 thomas goto done;
4492 8ec7352a 2023-06-01 thomas }
4493 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4494 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4495 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4496 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4497 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4498 17ed4618 2019-06-02 stsp if (err)
4499 af12c6b9 2019-06-04 stsp break;
4500 2ec1f75b 2019-03-26 stsp }
4501 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4502 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4503 af12c6b9 2019-06-04 stsp err = sync_err;
4504 2ec1f75b 2019-03-26 stsp done:
4505 fb399478 2019-07-12 stsp free(fileindex_path);
4506 2ec1f75b 2019-03-26 stsp if (fileindex)
4507 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4508 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4509 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4510 2ec1f75b 2019-03-26 stsp err = unlockerr;
4511 33aa809d 2019-08-08 stsp return err;
4512 33aa809d 2019-08-08 stsp }
4513 33aa809d 2019-08-08 stsp
4514 33aa809d 2019-08-08 stsp static const struct got_error *
4515 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4516 33aa809d 2019-08-08 stsp {
4517 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4518 33aa809d 2019-08-08 stsp char *line = NULL;
4519 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4520 33aa809d 2019-08-08 stsp ssize_t linelen;
4521 33aa809d 2019-08-08 stsp
4522 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4523 33aa809d 2019-08-08 stsp if (linelen == -1) {
4524 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4525 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4526 33aa809d 2019-08-08 stsp goto done;
4527 33aa809d 2019-08-08 stsp }
4528 33aa809d 2019-08-08 stsp return NULL;
4529 33aa809d 2019-08-08 stsp }
4530 33aa809d 2019-08-08 stsp if (outfile) {
4531 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4532 33aa809d 2019-08-08 stsp if (n != linelen) {
4533 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4534 33aa809d 2019-08-08 stsp goto done;
4535 33aa809d 2019-08-08 stsp }
4536 33aa809d 2019-08-08 stsp }
4537 33aa809d 2019-08-08 stsp if (rejectfile) {
4538 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4539 33aa809d 2019-08-08 stsp if (n != linelen)
4540 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4541 33aa809d 2019-08-08 stsp }
4542 33aa809d 2019-08-08 stsp done:
4543 33aa809d 2019-08-08 stsp free(line);
4544 2ec1f75b 2019-03-26 stsp return err;
4545 2ec1f75b 2019-03-26 stsp }
4546 1f1abb7e 2019-08-08 stsp
4547 33aa809d 2019-08-08 stsp static const struct got_error *
4548 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4549 33aa809d 2019-08-08 stsp {
4550 33aa809d 2019-08-08 stsp char *line = NULL;
4551 33aa809d 2019-08-08 stsp size_t linesize = 0;
4552 33aa809d 2019-08-08 stsp ssize_t linelen;
4553 33aa809d 2019-08-08 stsp
4554 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4555 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4556 500467ff 2019-09-25 hiltjo if (ferror(f))
4557 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4558 500467ff 2019-09-25 hiltjo return NULL;
4559 500467ff 2019-09-25 hiltjo }
4560 33aa809d 2019-08-08 stsp free(line);
4561 33aa809d 2019-08-08 stsp return NULL;
4562 33aa809d 2019-08-08 stsp }
4563 33aa809d 2019-08-08 stsp
4564 33aa809d 2019-08-08 stsp static const struct got_error *
4565 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4566 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4567 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4568 abc59930 2021-09-05 naddy {
4569 33aa809d 2019-08-08 stsp const struct got_error *err;
4570 33aa809d 2019-08-08 stsp
4571 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4572 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4573 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4574 33aa809d 2019-08-08 stsp if (err)
4575 33aa809d 2019-08-08 stsp return err;
4576 33aa809d 2019-08-08 stsp (*line_cur1)++;
4577 33aa809d 2019-08-08 stsp }
4578 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4579 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4580 33aa809d 2019-08-08 stsp if (rejectfile)
4581 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4582 33aa809d 2019-08-08 stsp else
4583 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4584 33aa809d 2019-08-08 stsp if (err)
4585 33aa809d 2019-08-08 stsp return err;
4586 33aa809d 2019-08-08 stsp (*line_cur2)++;
4587 33aa809d 2019-08-08 stsp }
4588 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4589 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4590 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4591 33aa809d 2019-08-08 stsp if (err)
4592 33aa809d 2019-08-08 stsp return err;
4593 33aa809d 2019-08-08 stsp (*line_cur2)++;
4594 33aa809d 2019-08-08 stsp }
4595 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4596 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4597 33aa809d 2019-08-08 stsp if (rejectfile)
4598 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4599 33aa809d 2019-08-08 stsp else
4600 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4601 33aa809d 2019-08-08 stsp if (err)
4602 33aa809d 2019-08-08 stsp return err;
4603 33aa809d 2019-08-08 stsp (*line_cur1)++;
4604 33aa809d 2019-08-08 stsp }
4605 f1e81a05 2019-08-10 stsp
4606 f1e81a05 2019-08-10 stsp return NULL;
4607 f1e81a05 2019-08-10 stsp }
4608 f1e81a05 2019-08-10 stsp
4609 f1e81a05 2019-08-10 stsp static const struct got_error *
4610 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4611 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4612 f1e81a05 2019-08-10 stsp {
4613 f1e81a05 2019-08-10 stsp const struct got_error *err;
4614 f1e81a05 2019-08-10 stsp
4615 f1e81a05 2019-08-10 stsp if (outfile) {
4616 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4617 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4618 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4619 f1e81a05 2019-08-10 stsp if (err)
4620 f1e81a05 2019-08-10 stsp return err;
4621 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4622 f1e81a05 2019-08-10 stsp }
4623 f1e81a05 2019-08-10 stsp }
4624 f1e81a05 2019-08-10 stsp if (rejectfile) {
4625 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4626 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4627 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4628 f1e81a05 2019-08-10 stsp if (err)
4629 f1e81a05 2019-08-10 stsp return err;
4630 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4631 f1e81a05 2019-08-10 stsp }
4632 33aa809d 2019-08-08 stsp }
4633 33aa809d 2019-08-08 stsp
4634 33aa809d 2019-08-08 stsp return NULL;
4635 33aa809d 2019-08-08 stsp }
4636 33aa809d 2019-08-08 stsp
4637 33aa809d 2019-08-08 stsp static const struct got_error *
4638 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4639 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4640 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4641 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4642 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4643 33aa809d 2019-08-08 stsp {
4644 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4645 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4646 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4647 33aa809d 2019-08-08 stsp FILE *hunkfile;
4648 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4649 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4650 fe621944 2020-11-10 stsp int rc;
4651 33aa809d 2019-08-08 stsp
4652 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4653 33aa809d 2019-08-08 stsp
4654 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4655 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4656 fe621944 2020-11-10 stsp start_old = cc.left.start;
4657 fe621944 2020-11-10 stsp end_old = cc.left.end;
4658 fe621944 2020-11-10 stsp start_new = cc.right.start;
4659 fe621944 2020-11-10 stsp end_new = cc.right.end;
4660 33aa809d 2019-08-08 stsp
4661 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4662 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4663 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4664 33aa809d 2019-08-08 stsp
4665 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4666 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4667 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4668 33aa809d 2019-08-08 stsp
4669 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4670 fe621944 2020-11-10 stsp if (diff_state == NULL)
4671 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4672 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4673 fe621944 2020-11-10 stsp
4674 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4675 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4676 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4677 33aa809d 2019-08-08 stsp goto done;
4678 33aa809d 2019-08-08 stsp }
4679 fe621944 2020-11-10 stsp
4680 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4681 fe621944 2020-11-10 stsp diff_result, &cc);
4682 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4683 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4684 33aa809d 2019-08-08 stsp goto done;
4685 33aa809d 2019-08-08 stsp }
4686 fe621944 2020-11-10 stsp
4687 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4688 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4689 33aa809d 2019-08-08 stsp goto done;
4690 33aa809d 2019-08-08 stsp }
4691 33aa809d 2019-08-08 stsp
4692 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4693 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4694 33aa809d 2019-08-08 stsp if (err)
4695 33aa809d 2019-08-08 stsp goto done;
4696 33aa809d 2019-08-08 stsp
4697 33aa809d 2019-08-08 stsp switch (*choice) {
4698 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4699 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4700 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4701 33aa809d 2019-08-08 stsp break;
4702 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4703 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4704 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4705 33aa809d 2019-08-08 stsp break;
4706 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4707 33aa809d 2019-08-08 stsp break;
4708 33aa809d 2019-08-08 stsp default:
4709 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4710 33aa809d 2019-08-08 stsp break;
4711 33aa809d 2019-08-08 stsp }
4712 33aa809d 2019-08-08 stsp done:
4713 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4714 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4715 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4716 33aa809d 2019-08-08 stsp return err;
4717 33aa809d 2019-08-08 stsp }
4718 33aa809d 2019-08-08 stsp
4719 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4720 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4721 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4722 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4723 1f1abb7e 2019-08-08 stsp void *progress_arg;
4724 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4725 33aa809d 2019-08-08 stsp void *patch_arg;
4726 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4727 10604dce 2021-09-24 thomas int unlink_added_files;
4728 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4729 1f1abb7e 2019-08-08 stsp };
4730 a129376b 2019-03-28 stsp
4731 e20a8b6f 2019-06-04 stsp static const struct got_error *
4732 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4733 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4734 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4735 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4736 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4737 33aa809d 2019-08-08 stsp {
4738 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4739 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4740 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4741 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4742 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4743 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4744 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4745 fe621944 2020-11-10 stsp struct stat sb2;
4746 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4747 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4748 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4749 33aa809d 2019-08-08 stsp
4750 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4751 33aa809d 2019-08-08 stsp
4752 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4753 33aa809d 2019-08-08 stsp if (err)
4754 33aa809d 2019-08-08 stsp return err;
4755 33aa809d 2019-08-08 stsp
4756 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4757 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4758 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4759 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4760 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4761 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4762 fa3cef63 2020-07-23 stsp goto done;
4763 fa3cef63 2020-07-23 stsp }
4764 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4765 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4766 48b4f239 2021-12-31 thomas if (link_len == -1) {
4767 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4768 48b4f239 2021-12-31 thomas path2);
4769 48b4f239 2021-12-31 thomas }
4770 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4771 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4772 12463d8b 2019-12-13 stsp }
4773 12463d8b 2019-12-13 stsp } else {
4774 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4775 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4776 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4777 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4778 fa3cef63 2020-07-23 stsp goto done;
4779 fa3cef63 2020-07-23 stsp }
4780 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4781 fa3cef63 2020-07-23 stsp sizeof(link_target));
4782 fa3cef63 2020-07-23 stsp if (link_len == -1)
4783 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4784 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4785 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4786 12463d8b 2019-12-13 stsp }
4787 1ebedb77 2019-10-19 stsp }
4788 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4789 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4790 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4791 fa3cef63 2020-07-23 stsp goto done;
4792 fa3cef63 2020-07-23 stsp }
4793 1ebedb77 2019-10-19 stsp
4794 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4795 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4796 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4797 fa3cef63 2020-07-23 stsp goto done;
4798 fa3cef63 2020-07-23 stsp }
4799 fa3cef63 2020-07-23 stsp fd2 = -1;
4800 fa3cef63 2020-07-23 stsp } else {
4801 fa3cef63 2020-07-23 stsp size_t n;
4802 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4803 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4804 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4805 fa3cef63 2020-07-23 stsp goto done;
4806 fa3cef63 2020-07-23 stsp }
4807 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4808 fa3cef63 2020-07-23 stsp if (n != link_len) {
4809 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4810 fa3cef63 2020-07-23 stsp goto done;
4811 fa3cef63 2020-07-23 stsp }
4812 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4813 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4814 fa3cef63 2020-07-23 stsp goto done;
4815 fa3cef63 2020-07-23 stsp }
4816 fa3cef63 2020-07-23 stsp rewind(f2);
4817 33aa809d 2019-08-08 stsp }
4818 33aa809d 2019-08-08 stsp
4819 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4820 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4821 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4822 f4ae6ddb 2022-07-01 thomas goto done;
4823 f4ae6ddb 2022-07-01 thomas }
4824 f4ae6ddb 2022-07-01 thomas
4825 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4826 33aa809d 2019-08-08 stsp if (err)
4827 33aa809d 2019-08-08 stsp goto done;
4828 33aa809d 2019-08-08 stsp
4829 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4830 33aa809d 2019-08-08 stsp if (err)
4831 33aa809d 2019-08-08 stsp goto done;
4832 33aa809d 2019-08-08 stsp
4833 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4834 33aa809d 2019-08-08 stsp if (err)
4835 33aa809d 2019-08-08 stsp goto done;
4836 33aa809d 2019-08-08 stsp
4837 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4838 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4839 33aa809d 2019-08-08 stsp if (err)
4840 33aa809d 2019-08-08 stsp goto done;
4841 33aa809d 2019-08-08 stsp
4842 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4843 fc2a50f2 2022-11-01 thomas "");
4844 33aa809d 2019-08-08 stsp if (err)
4845 33aa809d 2019-08-08 stsp goto done;
4846 33aa809d 2019-08-08 stsp
4847 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4848 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4849 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4850 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4851 fe621944 2020-11-10 stsp
4852 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4853 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4854 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4855 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4856 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4857 fe621944 2020-11-10 stsp nchanges++;
4858 fe621944 2020-11-10 stsp }
4859 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4860 33aa809d 2019-08-08 stsp int choice;
4861 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4862 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4863 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4864 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4865 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4866 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4867 33aa809d 2019-08-08 stsp if (err)
4868 33aa809d 2019-08-08 stsp goto done;
4869 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4870 33aa809d 2019-08-08 stsp have_content = 1;
4871 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4872 33aa809d 2019-08-08 stsp break;
4873 33aa809d 2019-08-08 stsp }
4874 1ebedb77 2019-10-19 stsp if (have_content) {
4875 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4876 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4877 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4878 1ebedb77 2019-10-19 stsp if (err)
4879 1ebedb77 2019-10-19 stsp goto done;
4880 1ebedb77 2019-10-19 stsp
4881 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4882 a2c162eb 2022-10-30 thomas mode_t mode;
4883 a2c162eb 2022-10-30 thomas
4884 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4885 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4886 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4887 fa3cef63 2020-07-23 stsp goto done;
4888 fa3cef63 2020-07-23 stsp }
4889 1ebedb77 2019-10-19 stsp }
4890 1ebedb77 2019-10-19 stsp }
4891 33aa809d 2019-08-08 stsp done:
4892 33aa809d 2019-08-08 stsp free(id_str);
4893 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4894 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4895 33aa809d 2019-08-08 stsp if (blob)
4896 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4897 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4898 fe621944 2020-11-10 stsp if (err == NULL)
4899 fe621944 2020-11-10 stsp err = free_err;
4900 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4901 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4902 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4903 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4904 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4905 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4906 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4907 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4908 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4909 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4910 33aa809d 2019-08-08 stsp if (err || !have_content) {
4911 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4912 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4913 33aa809d 2019-08-08 stsp free(*path_outfile);
4914 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4915 33aa809d 2019-08-08 stsp }
4916 33aa809d 2019-08-08 stsp free(path1);
4917 33aa809d 2019-08-08 stsp return err;
4918 33aa809d 2019-08-08 stsp }
4919 33aa809d 2019-08-08 stsp
4920 33aa809d 2019-08-08 stsp static const struct got_error *
4921 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4922 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4923 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4924 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4925 a129376b 2019-03-28 stsp {
4926 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4927 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4928 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4929 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4930 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4931 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4932 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4933 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4934 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4935 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4936 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4937 f4ae6ddb 2022-07-01 thomas int fd = -1;
4938 a129376b 2019-03-28 stsp
4939 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4940 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4941 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4942 d3bcc3d1 2019-08-08 stsp return NULL;
4943 3d69ad8d 2019-08-17 semarie
4944 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4945 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4946 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4947 d3bcc3d1 2019-08-08 stsp
4948 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4949 65084dad 2019-08-08 stsp if (ie == NULL)
4950 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4951 a129376b 2019-03-28 stsp
4952 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4953 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4954 a129376b 2019-03-28 stsp if (err) {
4955 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4956 a129376b 2019-03-28 stsp goto done;
4957 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4958 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4959 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4960 e20a8b6f 2019-06-04 stsp goto done;
4961 e20a8b6f 2019-06-04 stsp }
4962 a129376b 2019-03-28 stsp }
4963 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4964 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4965 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4966 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4967 a129376b 2019-03-28 stsp goto done;
4968 a129376b 2019-03-28 stsp }
4969 a129376b 2019-03-28 stsp } else {
4970 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4971 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4972 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4973 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4974 a129376b 2019-03-28 stsp goto done;
4975 a129376b 2019-03-28 stsp }
4976 a129376b 2019-03-28 stsp } else {
4977 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4978 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4979 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4980 a129376b 2019-03-28 stsp goto done;
4981 a129376b 2019-03-28 stsp }
4982 a129376b 2019-03-28 stsp }
4983 a129376b 2019-03-28 stsp }
4984 a129376b 2019-03-28 stsp
4985 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4986 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4987 945f9229 2022-04-16 thomas if (err)
4988 945f9229 2022-04-16 thomas goto done;
4989 945f9229 2022-04-16 thomas
4990 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4991 a9fa2909 2019-07-27 stsp if (err) {
4992 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4993 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4994 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4995 a9fa2909 2019-07-27 stsp goto done;
4996 a9fa2909 2019-07-27 stsp } else {
4997 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4998 a9fa2909 2019-07-27 stsp if (err)
4999 a9fa2909 2019-07-27 stsp goto done;
5000 a9fa2909 2019-07-27 stsp
5001 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5002 1233e6b6 2020-10-19 stsp if (err)
5003 a9fa2909 2019-07-27 stsp goto done;
5004 a9fa2909 2019-07-27 stsp
5005 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5006 1233e6b6 2020-10-19 stsp free(te_name);
5007 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5008 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5009 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5010 a9fa2909 2019-07-27 stsp goto done;
5011 a9fa2909 2019-07-27 stsp }
5012 a129376b 2019-03-28 stsp }
5013 a129376b 2019-03-28 stsp
5014 a129376b 2019-03-28 stsp switch (status) {
5015 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5016 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5017 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5018 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5019 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5020 33aa809d 2019-08-08 stsp if (err)
5021 33aa809d 2019-08-08 stsp goto done;
5022 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5023 33aa809d 2019-08-08 stsp break;
5024 33aa809d 2019-08-08 stsp }
5025 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5026 1f1abb7e 2019-08-08 stsp ie->path);
5027 1ee397ad 2019-07-12 stsp if (err)
5028 1ee397ad 2019-07-12 stsp goto done;
5029 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5030 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5031 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5032 8641a332 2023-04-14 thomas
5033 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5034 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5035 8641a332 2023-04-14 thomas
5036 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5037 8641a332 2023-04-14 thomas entry) {
5038 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5039 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5040 8641a332 2023-04-14 thomas continue;
5041 8641a332 2023-04-14 thomas do_unlink = 1;
5042 8641a332 2023-04-14 thomas break;
5043 8641a332 2023-04-14 thomas }
5044 10604dce 2021-09-24 thomas }
5045 8641a332 2023-04-14 thomas
5046 8641a332 2023-04-14 thomas if (do_unlink) {
5047 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5048 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5049 8641a332 2023-04-14 thomas relpath) == -1) {
5050 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5051 8641a332 2023-04-14 thomas goto done;
5052 8641a332 2023-04-14 thomas }
5053 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5054 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5055 8641a332 2023-04-14 thomas ondisk_path);
5056 8641a332 2023-04-14 thomas break;
5057 8641a332 2023-04-14 thomas }
5058 10604dce 2021-09-24 thomas }
5059 10604dce 2021-09-24 thomas }
5060 a129376b 2019-03-28 stsp break;
5061 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5062 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5063 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5064 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5065 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5066 33aa809d 2019-08-08 stsp if (err)
5067 33aa809d 2019-08-08 stsp goto done;
5068 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5069 33aa809d 2019-08-08 stsp break;
5070 33aa809d 2019-08-08 stsp }
5071 33aa809d 2019-08-08 stsp /* fall through */
5072 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5073 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5074 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5075 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5076 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5077 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5078 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5079 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5080 43010591 2023-02-17 thomas else
5081 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5082 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5083 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5084 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5085 f4ae6ddb 2022-07-01 thomas goto done;
5086 f4ae6ddb 2022-07-01 thomas }
5087 f4ae6ddb 2022-07-01 thomas
5088 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5089 a129376b 2019-03-28 stsp if (err)
5090 65084dad 2019-08-08 stsp goto done;
5091 65084dad 2019-08-08 stsp
5092 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5093 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5094 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5095 a129376b 2019-03-28 stsp goto done;
5096 65084dad 2019-08-08 stsp }
5097 65084dad 2019-08-08 stsp
5098 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5099 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5100 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5101 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5102 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5103 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5104 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5105 33aa809d 2019-08-08 stsp break;
5106 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5107 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5108 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5109 369fd7e5 2020-07-23 stsp path_content);
5110 369fd7e5 2020-07-23 stsp break;
5111 369fd7e5 2020-07-23 stsp }
5112 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5113 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5114 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5115 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5116 369fd7e5 2020-07-23 stsp } else {
5117 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5118 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5119 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5120 369fd7e5 2020-07-23 stsp goto done;
5121 369fd7e5 2020-07-23 stsp }
5122 33aa809d 2019-08-08 stsp }
5123 33aa809d 2019-08-08 stsp } else {
5124 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5125 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5126 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5127 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5128 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5129 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5130 2e1fa222 2020-07-23 stsp } else {
5131 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5132 2e1fa222 2020-07-23 stsp ie->path,
5133 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5134 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5135 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5136 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5137 2e1fa222 2020-07-23 stsp }
5138 a129376b 2019-03-28 stsp if (err)
5139 a129376b 2019-03-28 stsp goto done;
5140 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5141 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5142 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5143 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5144 437adc9d 2020-12-10 yzhong blob->id.sha1,
5145 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5146 2e1fa222 2020-07-23 stsp if (err)
5147 2e1fa222 2020-07-23 stsp goto done;
5148 2e1fa222 2020-07-23 stsp }
5149 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5150 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5151 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5152 33aa809d 2019-08-08 stsp }
5153 a129376b 2019-03-28 stsp }
5154 a129376b 2019-03-28 stsp break;
5155 e20a8b6f 2019-06-04 stsp }
5156 a129376b 2019-03-28 stsp default:
5157 1f1abb7e 2019-08-08 stsp break;
5158 a129376b 2019-03-28 stsp }
5159 a129376b 2019-03-28 stsp done:
5160 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5161 33aa809d 2019-08-08 stsp free(path_content);
5162 e20a8b6f 2019-06-04 stsp free(parent_path);
5163 a129376b 2019-03-28 stsp free(tree_path);
5164 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5165 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5166 a129376b 2019-03-28 stsp if (blob)
5167 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5168 a129376b 2019-03-28 stsp if (tree)
5169 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5170 a129376b 2019-03-28 stsp free(tree_id);
5171 945f9229 2022-04-16 thomas if (base_commit)
5172 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5173 e20a8b6f 2019-06-04 stsp return err;
5174 e20a8b6f 2019-06-04 stsp }
5175 e20a8b6f 2019-06-04 stsp
5176 e20a8b6f 2019-06-04 stsp const struct got_error *
5177 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5178 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5179 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5180 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5181 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5182 e20a8b6f 2019-06-04 stsp {
5183 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5184 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5185 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5186 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5187 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5188 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5189 e20a8b6f 2019-06-04 stsp
5190 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5191 e20a8b6f 2019-06-04 stsp if (err)
5192 e20a8b6f 2019-06-04 stsp return err;
5193 e20a8b6f 2019-06-04 stsp
5194 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5195 e20a8b6f 2019-06-04 stsp if (err)
5196 e20a8b6f 2019-06-04 stsp goto done;
5197 e20a8b6f 2019-06-04 stsp
5198 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5199 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5200 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5201 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5202 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5203 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5204 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5205 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5206 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5207 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5208 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5209 e20a8b6f 2019-06-04 stsp if (err)
5210 af12c6b9 2019-06-04 stsp break;
5211 e20a8b6f 2019-06-04 stsp }
5212 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5213 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5214 e20a8b6f 2019-06-04 stsp err = sync_err;
5215 af12c6b9 2019-06-04 stsp done:
5216 fb399478 2019-07-12 stsp free(fileindex_path);
5217 a129376b 2019-03-28 stsp if (fileindex)
5218 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5219 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5220 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5221 a129376b 2019-03-28 stsp err = unlockerr;
5222 c4296144 2019-05-09 stsp return err;
5223 c4296144 2019-05-09 stsp }
5224 c4296144 2019-05-09 stsp
5225 cf066bf8 2019-05-09 stsp static void
5226 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5227 cf066bf8 2019-05-09 stsp {
5228 24519714 2019-05-09 stsp free(ct->path);
5229 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5230 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5231 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5232 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5233 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5234 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5235 cf066bf8 2019-05-09 stsp free(ct);
5236 cf066bf8 2019-05-09 stsp }
5237 24519714 2019-05-09 stsp
5238 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5239 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5240 24519714 2019-05-09 stsp struct got_repository *repo;
5241 24519714 2019-05-09 stsp struct got_worktree *worktree;
5242 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5243 5f8a88c6 2019-08-03 stsp int have_staged_files;
5244 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5245 ef899790 2022-11-01 thomas int diff_header_shown;
5246 be94c032 2023-02-20 thomas int commit_conflicts;
5247 ef899790 2022-11-01 thomas FILE *diff_outfile;
5248 ef899790 2022-11-01 thomas FILE *f1;
5249 ef899790 2022-11-01 thomas FILE *f2;
5250 24519714 2019-05-09 stsp };
5251 ef899790 2022-11-01 thomas
5252 ef899790 2022-11-01 thomas /*
5253 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5254 ef899790 2022-11-01 thomas * it as content to the diff engine.
5255 ef899790 2022-11-01 thomas */
5256 ef899790 2022-11-01 thomas static const struct got_error *
5257 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5258 ef899790 2022-11-01 thomas const char *abspath)
5259 ef899790 2022-11-01 thomas {
5260 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5261 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5262 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5263 ef899790 2022-11-01 thomas
5264 ef899790 2022-11-01 thomas *fd = -1;
5265 ef899790 2022-11-01 thomas
5266 ef899790 2022-11-01 thomas if (dirfd != -1) {
5267 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5268 ef899790 2022-11-01 thomas if (target_len == -1)
5269 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5270 ef899790 2022-11-01 thomas } else {
5271 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5272 ef899790 2022-11-01 thomas if (target_len == -1)
5273 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5274 ef899790 2022-11-01 thomas }
5275 ef899790 2022-11-01 thomas
5276 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5277 ef899790 2022-11-01 thomas if (*fd == -1)
5278 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5279 ef899790 2022-11-01 thomas
5280 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5281 ef899790 2022-11-01 thomas if (outlen == -1) {
5282 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5283 ef899790 2022-11-01 thomas goto done;
5284 ef899790 2022-11-01 thomas }
5285 ef899790 2022-11-01 thomas
5286 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5287 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5288 ef899790 2022-11-01 thomas goto done;
5289 ef899790 2022-11-01 thomas }
5290 ef899790 2022-11-01 thomas done:
5291 ef899790 2022-11-01 thomas if (err) {
5292 ef899790 2022-11-01 thomas close(*fd);
5293 ef899790 2022-11-01 thomas *fd = -1;
5294 ef899790 2022-11-01 thomas }
5295 ef899790 2022-11-01 thomas return err;
5296 ef899790 2022-11-01 thomas }
5297 ef899790 2022-11-01 thomas
5298 ef899790 2022-11-01 thomas static const struct got_error *
5299 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5300 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5301 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5302 ef899790 2022-11-01 thomas {
5303 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5304 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5305 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5306 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5307 ef899790 2022-11-01 thomas char *label1 = NULL;
5308 ef899790 2022-11-01 thomas struct stat sb;
5309 ef899790 2022-11-01 thomas off_t size1 = 0;
5310 ef899790 2022-11-01 thomas int f2_exists = 0;
5311 ef899790 2022-11-01 thomas char *id_str = NULL;
5312 ef899790 2022-11-01 thomas
5313 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5314 d259e491 2022-11-01 thomas
5315 d259e491 2022-11-01 thomas if (diff_staged) {
5316 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5317 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5318 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5319 d259e491 2022-11-01 thomas return NULL;
5320 d259e491 2022-11-01 thomas } else {
5321 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5322 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5323 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5324 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5325 d259e491 2022-11-01 thomas return NULL;
5326 d259e491 2022-11-01 thomas }
5327 ef899790 2022-11-01 thomas
5328 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5329 ef899790 2022-11-01 thomas if (err)
5330 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5331 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5332 ef899790 2022-11-01 thomas if (err)
5333 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5334 ef899790 2022-11-01 thomas
5335 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5336 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5337 ef899790 2022-11-01 thomas if (err)
5338 ef899790 2022-11-01 thomas return err;
5339 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5340 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5341 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5342 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5343 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5344 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5345 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5346 ef899790 2022-11-01 thomas }
5347 ef899790 2022-11-01 thomas
5348 ef899790 2022-11-01 thomas if (diff_staged) {
5349 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5350 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5351 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5352 ef899790 2022-11-01 thomas label1 = ct->path;
5353 ef899790 2022-11-01 thomas label2 = ct->path;
5354 ef899790 2022-11-01 thomas break;
5355 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5356 ef899790 2022-11-01 thomas label2 = ct->path;
5357 ef899790 2022-11-01 thomas break;
5358 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5359 ef899790 2022-11-01 thomas label1 = ct->path;
5360 ef899790 2022-11-01 thomas break;
5361 ef899790 2022-11-01 thomas default:
5362 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5363 ef899790 2022-11-01 thomas }
5364 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5365 ef899790 2022-11-01 thomas if (fd1 == -1) {
5366 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5367 ef899790 2022-11-01 thomas goto done;
5368 ef899790 2022-11-01 thomas }
5369 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5370 ef899790 2022-11-01 thomas if (fd2 == -1) {
5371 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5372 ef899790 2022-11-01 thomas goto done;
5373 ef899790 2022-11-01 thomas }
5374 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5375 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5376 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5377 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5378 ef899790 2022-11-01 thomas goto done;
5379 ef899790 2022-11-01 thomas }
5380 ef899790 2022-11-01 thomas
5381 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5382 ef899790 2022-11-01 thomas if (fd1 == -1) {
5383 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5384 ef899790 2022-11-01 thomas goto done;
5385 ef899790 2022-11-01 thomas }
5386 ef899790 2022-11-01 thomas
5387 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5388 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5389 ef899790 2022-11-01 thomas 8192, fd1);
5390 ef899790 2022-11-01 thomas if (err)
5391 ef899790 2022-11-01 thomas goto done;
5392 ef899790 2022-11-01 thomas }
5393 ef899790 2022-11-01 thomas
5394 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5395 ef899790 2022-11-01 thomas if (dirfd != -1) {
5396 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5397 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5398 ef899790 2022-11-01 thomas if (fd == -1) {
5399 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5400 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5401 ef899790 2022-11-01 thomas ct->ondisk_path);
5402 ef899790 2022-11-01 thomas goto done;
5403 ef899790 2022-11-01 thomas }
5404 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5405 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5406 ef899790 2022-11-01 thomas if (err)
5407 ef899790 2022-11-01 thomas goto done;
5408 ef899790 2022-11-01 thomas }
5409 ef899790 2022-11-01 thomas } else {
5410 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5411 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5412 ef899790 2022-11-01 thomas if (fd == -1) {
5413 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5414 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5415 ef899790 2022-11-01 thomas ct->ondisk_path);
5416 ef899790 2022-11-01 thomas goto done;
5417 ef899790 2022-11-01 thomas }
5418 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5419 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5420 ef899790 2022-11-01 thomas if (err)
5421 ef899790 2022-11-01 thomas goto done;
5422 ef899790 2022-11-01 thomas }
5423 ef899790 2022-11-01 thomas }
5424 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5425 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5426 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5427 ef899790 2022-11-01 thomas goto done;
5428 ef899790 2022-11-01 thomas }
5429 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5430 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5431 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5432 ef899790 2022-11-01 thomas goto done;
5433 ef899790 2022-11-01 thomas }
5434 ef899790 2022-11-01 thomas fd = -1;
5435 ef899790 2022-11-01 thomas f2_exists = 1;
5436 ef899790 2022-11-01 thomas }
5437 ef899790 2022-11-01 thomas
5438 ef899790 2022-11-01 thomas if (blob1) {
5439 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5440 ef899790 2022-11-01 thomas f1, blob1);
5441 ef899790 2022-11-01 thomas if (err)
5442 ef899790 2022-11-01 thomas goto done;
5443 ef899790 2022-11-01 thomas }
5444 ef899790 2022-11-01 thomas
5445 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5446 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5447 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5448 ef899790 2022-11-01 thomas done:
5449 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5450 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5451 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5452 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5453 ef899790 2022-11-01 thomas if (blob1)
5454 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5455 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5456 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5457 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5458 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5459 ef899790 2022-11-01 thomas return err;
5460 ef899790 2022-11-01 thomas }
5461 cf066bf8 2019-05-09 stsp
5462 c4296144 2019-05-09 stsp static const struct got_error *
5463 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5464 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5465 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5466 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5467 c4296144 2019-05-09 stsp {
5468 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5469 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5470 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5471 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5472 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5473 768aea60 2019-05-09 stsp struct stat sb;
5474 c4296144 2019-05-09 stsp
5475 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5476 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5477 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5478 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5479 5f8a88c6 2019-08-03 stsp return NULL;
5480 5f8a88c6 2019-08-03 stsp } else {
5481 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5482 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5483 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5484 be94c032 2023-02-20 thomas }
5485 c4296144 2019-05-09 stsp
5486 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5487 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5488 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5489 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5490 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5491 5f8a88c6 2019-08-03 stsp return NULL;
5492 5f8a88c6 2019-08-03 stsp }
5493 0b5cc0d6 2019-05-09 stsp
5494 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5495 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5496 036813ee 2019-05-09 stsp goto done;
5497 036813ee 2019-05-09 stsp }
5498 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5499 036813ee 2019-05-09 stsp parent_path = strdup("");
5500 69960a46 2019-05-09 stsp if (parent_path == NULL)
5501 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5502 69960a46 2019-05-09 stsp } else {
5503 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5504 69960a46 2019-05-09 stsp if (err)
5505 69960a46 2019-05-09 stsp return err;
5506 69960a46 2019-05-09 stsp }
5507 c4296144 2019-05-09 stsp
5508 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5509 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5510 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5511 c4296144 2019-05-09 stsp goto done;
5512 768aea60 2019-05-09 stsp }
5513 768aea60 2019-05-09 stsp
5514 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5515 768aea60 2019-05-09 stsp relpath) == -1) {
5516 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5517 768aea60 2019-05-09 stsp goto done;
5518 768aea60 2019-05-09 stsp }
5519 0aeb8099 2020-07-23 stsp
5520 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5521 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5522 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5523 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5524 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5525 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5526 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5527 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5528 0aeb8099 2020-07-23 stsp break;
5529 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5530 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5531 0aeb8099 2020-07-23 stsp break;
5532 0aeb8099 2020-07-23 stsp default:
5533 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5534 0aeb8099 2020-07-23 stsp goto done;
5535 0aeb8099 2020-07-23 stsp }
5536 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5537 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5538 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5539 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5540 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5541 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5542 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5543 12463d8b 2019-12-13 stsp ct->ondisk_path);
5544 12463d8b 2019-12-13 stsp goto done;
5545 12463d8b 2019-12-13 stsp }
5546 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5547 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5548 768aea60 2019-05-09 stsp goto done;
5549 768aea60 2019-05-09 stsp }
5550 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5551 c4296144 2019-05-09 stsp }
5552 c4296144 2019-05-09 stsp
5553 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5554 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5555 44d03001 2019-05-09 stsp relpath) == -1) {
5556 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5557 44d03001 2019-05-09 stsp goto done;
5558 44d03001 2019-05-09 stsp }
5559 44d03001 2019-05-09 stsp
5560 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5561 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5562 35213c7c 2020-07-23 stsp int is_bad_symlink;
5563 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5564 35213c7c 2020-07-23 stsp ssize_t target_len;
5565 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5566 35213c7c 2020-07-23 stsp sizeof(target_path));
5567 35213c7c 2020-07-23 stsp if (target_len == -1) {
5568 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5569 35213c7c 2020-07-23 stsp ct->ondisk_path);
5570 35213c7c 2020-07-23 stsp goto done;
5571 35213c7c 2020-07-23 stsp }
5572 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5573 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5574 35213c7c 2020-07-23 stsp if (err)
5575 35213c7c 2020-07-23 stsp goto done;
5576 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5577 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5578 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5579 35213c7c 2020-07-23 stsp goto done;
5580 35213c7c 2020-07-23 stsp }
5581 35213c7c 2020-07-23 stsp }
5582 35213c7c 2020-07-23 stsp
5583 35213c7c 2020-07-23 stsp
5584 cf066bf8 2019-05-09 stsp ct->status = status;
5585 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5586 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5587 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5588 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5589 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5590 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5591 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5592 b416585c 2019-05-13 stsp goto done;
5593 b416585c 2019-05-13 stsp }
5594 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5595 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5596 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5597 f0b75401 2019-08-03 stsp goto done;
5598 f0b75401 2019-08-03 stsp }
5599 f0b75401 2019-08-03 stsp }
5600 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5601 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5602 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5603 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5605 036813ee 2019-05-09 stsp goto done;
5606 036813ee 2019-05-09 stsp }
5607 ca2503ea 2019-05-09 stsp }
5608 24519714 2019-05-09 stsp ct->path = strdup(path);
5609 24519714 2019-05-09 stsp if (ct->path == NULL) {
5610 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5611 24519714 2019-05-09 stsp goto done;
5612 24519714 2019-05-09 stsp }
5613 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5614 ef899790 2022-11-01 thomas if (err)
5615 ef899790 2022-11-01 thomas goto done;
5616 ef899790 2022-11-01 thomas
5617 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5618 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5619 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5620 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5621 ef899790 2022-11-01 thomas if (err)
5622 ef899790 2022-11-01 thomas goto done;
5623 ef899790 2022-11-01 thomas }
5624 c4296144 2019-05-09 stsp done:
5625 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5626 ed175427 2019-05-09 stsp free_commitable(ct);
5627 24519714 2019-05-09 stsp free(parent_path);
5628 036813ee 2019-05-09 stsp free(path);
5629 a129376b 2019-03-28 stsp return err;
5630 a129376b 2019-03-28 stsp }
5631 c4296144 2019-05-09 stsp
5632 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5633 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5634 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5635 036813ee 2019-05-09 stsp struct got_repository *);
5636 ed175427 2019-05-09 stsp
5637 ed175427 2019-05-09 stsp static const struct got_error *
5638 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5639 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5640 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5641 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5642 afa376bf 2019-05-09 stsp struct got_repository *repo)
5643 ed175427 2019-05-09 stsp {
5644 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5645 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5646 036813ee 2019-05-09 stsp char *subpath;
5647 ed175427 2019-05-09 stsp
5648 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5649 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5650 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5651 ed175427 2019-05-09 stsp
5652 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5653 ed175427 2019-05-09 stsp if (err)
5654 ed175427 2019-05-09 stsp return err;
5655 ed175427 2019-05-09 stsp
5656 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5657 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5658 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5659 036813ee 2019-05-09 stsp free(subpath);
5660 036813ee 2019-05-09 stsp return err;
5661 036813ee 2019-05-09 stsp }
5662 ed175427 2019-05-09 stsp
5663 036813ee 2019-05-09 stsp static const struct got_error *
5664 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5665 036813ee 2019-05-09 stsp {
5666 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5667 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5668 ed175427 2019-05-09 stsp
5669 036813ee 2019-05-09 stsp *match = 0;
5670 ed175427 2019-05-09 stsp
5671 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5672 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5673 0f63689d 2019-05-10 stsp return NULL;
5674 036813ee 2019-05-09 stsp }
5675 0b5cc0d6 2019-05-09 stsp
5676 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5677 0f63689d 2019-05-10 stsp if (err)
5678 0f63689d 2019-05-10 stsp return err;
5679 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5680 036813ee 2019-05-09 stsp free(ct_parent_path);
5681 036813ee 2019-05-09 stsp return err;
5682 036813ee 2019-05-09 stsp }
5683 0b5cc0d6 2019-05-09 stsp
5684 768aea60 2019-05-09 stsp static mode_t
5685 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5686 768aea60 2019-05-09 stsp {
5687 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5688 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5689 3d9a4ec4 2020-07-23 stsp
5690 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5691 768aea60 2019-05-09 stsp }
5692 768aea60 2019-05-09 stsp
5693 036813ee 2019-05-09 stsp static const struct got_error *
5694 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5695 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5696 036813ee 2019-05-09 stsp {
5697 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5698 ca2503ea 2019-05-09 stsp
5699 036813ee 2019-05-09 stsp *new_te = NULL;
5700 0b5cc0d6 2019-05-09 stsp
5701 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5702 036813ee 2019-05-09 stsp if (err)
5703 036813ee 2019-05-09 stsp goto done;
5704 0b5cc0d6 2019-05-09 stsp
5705 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5706 036813ee 2019-05-09 stsp
5707 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5708 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5709 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5710 5f8a88c6 2019-08-03 stsp else
5711 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5712 036813ee 2019-05-09 stsp done:
5713 036813ee 2019-05-09 stsp if (err && *new_te) {
5714 56e0773d 2019-11-28 stsp free(*new_te);
5715 036813ee 2019-05-09 stsp *new_te = NULL;
5716 036813ee 2019-05-09 stsp }
5717 036813ee 2019-05-09 stsp return err;
5718 036813ee 2019-05-09 stsp }
5719 036813ee 2019-05-09 stsp
5720 036813ee 2019-05-09 stsp static const struct got_error *
5721 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5722 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5723 036813ee 2019-05-09 stsp {
5724 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5725 102b254e 2020-10-19 stsp char *ct_name = NULL;
5726 036813ee 2019-05-09 stsp
5727 036813ee 2019-05-09 stsp *new_te = NULL;
5728 036813ee 2019-05-09 stsp
5729 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5730 036813ee 2019-05-09 stsp if (*new_te == NULL)
5731 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5732 036813ee 2019-05-09 stsp
5733 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5734 102b254e 2020-10-19 stsp if (err)
5735 036813ee 2019-05-09 stsp goto done;
5736 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5737 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5738 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5739 036813ee 2019-05-09 stsp goto done;
5740 036813ee 2019-05-09 stsp }
5741 036813ee 2019-05-09 stsp
5742 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5743 036813ee 2019-05-09 stsp
5744 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5745 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5746 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5747 5f8a88c6 2019-08-03 stsp else
5748 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5749 036813ee 2019-05-09 stsp done:
5750 102b254e 2020-10-19 stsp free(ct_name);
5751 036813ee 2019-05-09 stsp if (err && *new_te) {
5752 56e0773d 2019-11-28 stsp free(*new_te);
5753 036813ee 2019-05-09 stsp *new_te = NULL;
5754 036813ee 2019-05-09 stsp }
5755 036813ee 2019-05-09 stsp return err;
5756 036813ee 2019-05-09 stsp }
5757 036813ee 2019-05-09 stsp
5758 036813ee 2019-05-09 stsp static const struct got_error *
5759 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5760 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5761 036813ee 2019-05-09 stsp {
5762 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5763 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5764 036813ee 2019-05-09 stsp
5765 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5766 036813ee 2019-05-09 stsp if (err)
5767 036813ee 2019-05-09 stsp return err;
5768 036813ee 2019-05-09 stsp if (new_pe == NULL)
5769 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5770 036813ee 2019-05-09 stsp return NULL;
5771 afa376bf 2019-05-09 stsp }
5772 afa376bf 2019-05-09 stsp
5773 afa376bf 2019-05-09 stsp static const struct got_error *
5774 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5775 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5776 afa376bf 2019-05-09 stsp {
5777 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5778 5f8a88c6 2019-08-03 stsp unsigned char status;
5779 ae1e948a 2021-09-28 thomas
5780 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5781 ae1e948a 2021-09-28 thomas return NULL;
5782 5f8a88c6 2019-08-03 stsp
5783 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5784 afa376bf 2019-05-09 stsp ct_path++;
5785 5f8a88c6 2019-08-03 stsp
5786 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5787 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5788 5f8a88c6 2019-08-03 stsp else
5789 5f8a88c6 2019-08-03 stsp status = ct->status;
5790 5f8a88c6 2019-08-03 stsp
5791 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5792 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5793 036813ee 2019-05-09 stsp }
5794 036813ee 2019-05-09 stsp
5795 036813ee 2019-05-09 stsp static const struct got_error *
5796 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5797 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5798 44d03001 2019-05-09 stsp {
5799 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5800 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5801 44d03001 2019-05-09 stsp char *te_path;
5802 44d03001 2019-05-09 stsp
5803 44d03001 2019-05-09 stsp *modified = 0;
5804 44d03001 2019-05-09 stsp
5805 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5806 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5807 44d03001 2019-05-09 stsp te->name) == -1)
5808 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5809 44d03001 2019-05-09 stsp
5810 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5811 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5812 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5813 44d03001 2019-05-09 stsp strlen(te_path));
5814 62d463ca 2020-10-20 naddy if (*modified)
5815 44d03001 2019-05-09 stsp break;
5816 44d03001 2019-05-09 stsp }
5817 44d03001 2019-05-09 stsp
5818 44d03001 2019-05-09 stsp free(te_path);
5819 44d03001 2019-05-09 stsp return err;
5820 44d03001 2019-05-09 stsp }
5821 44d03001 2019-05-09 stsp
5822 44d03001 2019-05-09 stsp static const struct got_error *
5823 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5824 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5825 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5826 036813ee 2019-05-09 stsp {
5827 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5828 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5829 036813ee 2019-05-09 stsp
5830 036813ee 2019-05-09 stsp *ctp = NULL;
5831 036813ee 2019-05-09 stsp
5832 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5833 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5834 036813ee 2019-05-09 stsp char *ct_name = NULL;
5835 036813ee 2019-05-09 stsp int path_matches;
5836 036813ee 2019-05-09 stsp
5837 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5838 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5839 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5840 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5841 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5842 5f8a88c6 2019-08-03 stsp continue;
5843 5f8a88c6 2019-08-03 stsp } else {
5844 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5845 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5846 5f8a88c6 2019-08-03 stsp continue;
5847 5f8a88c6 2019-08-03 stsp }
5848 036813ee 2019-05-09 stsp
5849 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5850 036813ee 2019-05-09 stsp continue;
5851 036813ee 2019-05-09 stsp
5852 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5853 62d463ca 2020-10-20 naddy if (err)
5854 036813ee 2019-05-09 stsp return err;
5855 036813ee 2019-05-09 stsp if (!path_matches)
5856 036813ee 2019-05-09 stsp continue;
5857 036813ee 2019-05-09 stsp
5858 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5859 d34b633e 2020-10-19 stsp if (err)
5860 d34b633e 2020-10-19 stsp return err;
5861 d34b633e 2020-10-19 stsp
5862 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5863 d34b633e 2020-10-19 stsp free(ct_name);
5864 036813ee 2019-05-09 stsp continue;
5865 d34b633e 2020-10-19 stsp }
5866 d34b633e 2020-10-19 stsp free(ct_name);
5867 036813ee 2019-05-09 stsp
5868 036813ee 2019-05-09 stsp *ctp = ct;
5869 036813ee 2019-05-09 stsp break;
5870 036813ee 2019-05-09 stsp }
5871 2b496619 2019-07-10 stsp
5872 2b496619 2019-07-10 stsp return err;
5873 2b496619 2019-07-10 stsp }
5874 2b496619 2019-07-10 stsp
5875 2b496619 2019-07-10 stsp static const struct got_error *
5876 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5877 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5878 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5879 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5880 2b496619 2019-07-10 stsp struct got_repository *repo)
5881 2b496619 2019-07-10 stsp {
5882 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5883 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5884 2b496619 2019-07-10 stsp char *subtree_path;
5885 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5886 ba580f68 2020-03-22 stsp int nentries;
5887 2b496619 2019-07-10 stsp
5888 2b496619 2019-07-10 stsp *new_tep = NULL;
5889 2b496619 2019-07-10 stsp
5890 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5891 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5892 2b496619 2019-07-10 stsp child_path) == -1)
5893 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5894 036813ee 2019-05-09 stsp
5895 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5896 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5897 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5898 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5899 56e0773d 2019-11-28 stsp
5900 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5901 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5902 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5903 2b496619 2019-07-10 stsp goto done;
5904 2b496619 2019-07-10 stsp }
5905 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5906 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5907 2b496619 2019-07-10 stsp if (err) {
5908 56e0773d 2019-11-28 stsp free(new_te);
5909 2b496619 2019-07-10 stsp goto done;
5910 2b496619 2019-07-10 stsp }
5911 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5912 2b496619 2019-07-10 stsp done:
5913 56e0773d 2019-11-28 stsp free(id);
5914 2b496619 2019-07-10 stsp free(subtree_path);
5915 2b496619 2019-07-10 stsp if (err == NULL)
5916 2b496619 2019-07-10 stsp *new_tep = new_te;
5917 036813ee 2019-05-09 stsp return err;
5918 036813ee 2019-05-09 stsp }
5919 036813ee 2019-05-09 stsp
5920 036813ee 2019-05-09 stsp static const struct got_error *
5921 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5922 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5923 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5924 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5925 036813ee 2019-05-09 stsp struct got_repository *repo)
5926 036813ee 2019-05-09 stsp {
5927 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5928 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5929 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5930 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5931 036813ee 2019-05-09 stsp
5932 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5933 ba580f68 2020-03-22 stsp *nentries = 0;
5934 036813ee 2019-05-09 stsp
5935 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5936 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5937 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5938 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5939 036813ee 2019-05-09 stsp
5940 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5941 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5942 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5943 036813ee 2019-05-09 stsp continue;
5944 036813ee 2019-05-09 stsp
5945 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5946 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5947 036813ee 2019-05-09 stsp continue;
5948 036813ee 2019-05-09 stsp
5949 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5950 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5951 036813ee 2019-05-09 stsp if (err)
5952 036813ee 2019-05-09 stsp goto done;
5953 036813ee 2019-05-09 stsp
5954 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5955 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5956 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5957 036813ee 2019-05-09 stsp if (err)
5958 036813ee 2019-05-09 stsp goto done;
5959 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5960 afa376bf 2019-05-09 stsp if (err)
5961 afa376bf 2019-05-09 stsp goto done;
5962 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5963 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5964 2b496619 2019-07-10 stsp if (err)
5965 2f51b5b3 2019-05-09 stsp goto done;
5966 ba580f68 2020-03-22 stsp (*nentries)++;
5967 2b496619 2019-07-10 stsp } else {
5968 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5969 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5970 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5971 2b496619 2019-07-10 stsp == NULL) {
5972 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5973 2b496619 2019-07-10 stsp child_path, path_base_tree,
5974 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5975 2b496619 2019-07-10 stsp repo);
5976 2b496619 2019-07-10 stsp if (err)
5977 2b496619 2019-07-10 stsp goto done;
5978 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5979 2b496619 2019-07-10 stsp if (err)
5980 2b496619 2019-07-10 stsp goto done;
5981 ba580f68 2020-03-22 stsp (*nentries)++;
5982 9ba0479c 2019-05-10 stsp }
5983 ed175427 2019-05-09 stsp }
5984 2f51b5b3 2019-05-09 stsp }
5985 2f51b5b3 2019-05-09 stsp
5986 2f51b5b3 2019-05-09 stsp if (base_tree) {
5987 56e0773d 2019-11-28 stsp int i, nbase_entries;
5988 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5989 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5990 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5991 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5992 63c5ca5d 2019-08-24 stsp
5993 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5994 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5995 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5996 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5997 63c5ca5d 2019-08-24 stsp if (err)
5998 63c5ca5d 2019-08-24 stsp goto done;
5999 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6000 63c5ca5d 2019-08-24 stsp if (err)
6001 63c5ca5d 2019-08-24 stsp goto done;
6002 ba580f68 2020-03-22 stsp (*nentries)++;
6003 63c5ca5d 2019-08-24 stsp continue;
6004 63c5ca5d 2019-08-24 stsp }
6005 2f51b5b3 2019-05-09 stsp
6006 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6007 44d03001 2019-05-09 stsp int modified;
6008 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6009 036813ee 2019-05-09 stsp if (err)
6010 036813ee 2019-05-09 stsp goto done;
6011 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6012 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6013 2f51b5b3 2019-05-09 stsp if (err)
6014 2f51b5b3 2019-05-09 stsp goto done;
6015 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6016 44d03001 2019-05-09 stsp if (modified) {
6017 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6018 ba580f68 2020-03-22 stsp int nsubentries;
6019 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6020 ba580f68 2020-03-22 stsp &nsubentries, te,
6021 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6022 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6023 44d03001 2019-05-09 stsp if (err)
6024 44d03001 2019-05-09 stsp goto done;
6025 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6026 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6027 ba580f68 2020-03-22 stsp free(new_id);
6028 ba580f68 2020-03-22 stsp continue;
6029 ba580f68 2020-03-22 stsp }
6030 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6031 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6032 56e0773d 2019-11-28 stsp free(new_id);
6033 44d03001 2019-05-09 stsp }
6034 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6035 036813ee 2019-05-09 stsp if (err)
6036 036813ee 2019-05-09 stsp goto done;
6037 ba580f68 2020-03-22 stsp (*nentries)++;
6038 2f51b5b3 2019-05-09 stsp continue;
6039 036813ee 2019-05-09 stsp }
6040 2f51b5b3 2019-05-09 stsp
6041 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6042 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6043 f66c734c 2019-09-22 stsp if (err)
6044 f66c734c 2019-09-22 stsp goto done;
6045 2f51b5b3 2019-05-09 stsp if (ct) {
6046 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6047 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6048 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6049 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6050 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6051 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6052 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6053 2f51b5b3 2019-05-09 stsp if (err)
6054 2f51b5b3 2019-05-09 stsp goto done;
6055 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6056 2f51b5b3 2019-05-09 stsp if (err)
6057 2f51b5b3 2019-05-09 stsp goto done;
6058 ba580f68 2020-03-22 stsp (*nentries)++;
6059 2f51b5b3 2019-05-09 stsp }
6060 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6061 b416585c 2019-05-13 stsp status_arg);
6062 afa376bf 2019-05-09 stsp if (err)
6063 afa376bf 2019-05-09 stsp goto done;
6064 2f51b5b3 2019-05-09 stsp } else {
6065 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6066 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6067 2f51b5b3 2019-05-09 stsp if (err)
6068 2f51b5b3 2019-05-09 stsp goto done;
6069 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6070 2f51b5b3 2019-05-09 stsp if (err)
6071 2f51b5b3 2019-05-09 stsp goto done;
6072 ba580f68 2020-03-22 stsp (*nentries)++;
6073 2f51b5b3 2019-05-09 stsp }
6074 ca2503ea 2019-05-09 stsp }
6075 ed175427 2019-05-09 stsp }
6076 0b5cc0d6 2019-05-09 stsp
6077 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6078 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6079 ed175427 2019-05-09 stsp done:
6080 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6081 2e1fa222 2020-07-23 stsp return err;
6082 2e1fa222 2020-07-23 stsp }
6083 2e1fa222 2020-07-23 stsp
6084 2e1fa222 2020-07-23 stsp static const struct got_error *
6085 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6086 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6087 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6088 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6089 ebf99748 2019-05-09 stsp {
6090 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6091 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6092 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6093 ebf99748 2019-05-09 stsp
6094 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6095 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6096 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6097 ebf99748 2019-05-09 stsp
6098 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6099 437adc9d 2020-12-10 yzhong
6100 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6101 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6102 437adc9d 2020-12-10 yzhong if (err)
6103 437adc9d 2020-12-10 yzhong goto done;
6104 437adc9d 2020-12-10 yzhong
6105 ebf99748 2019-05-09 stsp if (ie) {
6106 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6107 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6108 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6109 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6110 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6111 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6112 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6113 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6114 437adc9d 2020-12-10 yzhong
6115 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6116 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6117 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6118 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6119 72fd46fa 2019-09-06 stsp !have_staged_files);
6120 ebf99748 2019-05-09 stsp } else
6121 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6122 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6123 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6124 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6125 72fd46fa 2019-09-06 stsp !have_staged_files);
6126 ebf99748 2019-05-09 stsp } else {
6127 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6128 ebf99748 2019-05-09 stsp if (err)
6129 437adc9d 2020-12-10 yzhong goto done;
6130 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6131 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6132 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6133 3969253a 2020-03-07 stsp if (err) {
6134 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6135 437adc9d 2020-12-10 yzhong goto done;
6136 3969253a 2020-03-07 stsp }
6137 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6138 3969253a 2020-03-07 stsp if (err) {
6139 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6140 437adc9d 2020-12-10 yzhong goto done;
6141 3969253a 2020-03-07 stsp }
6142 ebf99748 2019-05-09 stsp }
6143 437adc9d 2020-12-10 yzhong free(relpath);
6144 437adc9d 2020-12-10 yzhong relpath = NULL;
6145 ebf99748 2019-05-09 stsp }
6146 437adc9d 2020-12-10 yzhong done:
6147 437adc9d 2020-12-10 yzhong free(relpath);
6148 d56d26ce 2019-05-10 stsp return err;
6149 d56d26ce 2019-05-10 stsp }
6150 735ef5ac 2019-08-03 stsp
6151 d56d26ce 2019-05-10 stsp
6152 d56d26ce 2019-05-10 stsp static const struct got_error *
6153 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6154 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6155 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6156 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6157 735ef5ac 2019-08-03 stsp int ood_errcode)
6158 d56d26ce 2019-05-10 stsp {
6159 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6160 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6161 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6162 1a36436d 2019-06-10 stsp
6163 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6164 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6165 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6166 1a36436d 2019-06-10 stsp return NULL;
6167 9bead371 2019-07-28 stsp /*
6168 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6169 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6170 9bead371 2019-07-28 stsp */
6171 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6172 945f9229 2022-04-16 thomas if (err)
6173 945f9229 2022-04-16 thomas goto done;
6174 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6175 9bead371 2019-07-28 stsp if (err) {
6176 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6177 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6178 1a36436d 2019-06-10 stsp goto done;
6179 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6180 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6181 1a36436d 2019-06-10 stsp } else {
6182 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6183 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6184 945f9229 2022-04-16 thomas if (err)
6185 945f9229 2022-04-16 thomas goto done;
6186 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6187 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6188 1a36436d 2019-06-10 stsp goto done;
6189 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6190 1a36436d 2019-06-10 stsp }
6191 1a36436d 2019-06-10 stsp done:
6192 1a36436d 2019-06-10 stsp free(id);
6193 945f9229 2022-04-16 thomas if (commit)
6194 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6195 1a36436d 2019-06-10 stsp return err;
6196 ebf99748 2019-05-09 stsp }
6197 ebf99748 2019-05-09 stsp
6198 ef20f542 2022-06-26 thomas static const struct got_error *
6199 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6200 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6201 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6202 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6203 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6204 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6205 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6206 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6207 afa376bf 2019-05-09 stsp struct got_repository *repo)
6208 c4296144 2019-05-09 stsp {
6209 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6210 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6211 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6212 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6213 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6214 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6215 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6216 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6217 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6218 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6219 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6220 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6221 892eab0a 2022-07-22 thomas time_t timestamp;
6222 c4296144 2019-05-09 stsp
6223 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6224 c4296144 2019-05-09 stsp
6225 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6226 675c7539 2019-05-09 stsp
6227 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6228 588edf97 2019-05-10 stsp if (err)
6229 588edf97 2019-05-10 stsp goto done;
6230 de18fc63 2019-05-09 stsp
6231 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6232 588edf97 2019-05-10 stsp if (err)
6233 588edf97 2019-05-10 stsp goto done;
6234 588edf97 2019-05-10 stsp
6235 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6236 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6237 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6238 33ad4cbe 2019-05-12 jcs if (err)
6239 33ad4cbe 2019-05-12 jcs goto done;
6240 33ad4cbe 2019-05-12 jcs }
6241 c4296144 2019-05-09 stsp
6242 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6243 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6244 33ad4cbe 2019-05-12 jcs goto done;
6245 33ad4cbe 2019-05-12 jcs }
6246 33ad4cbe 2019-05-12 jcs
6247 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6248 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6249 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6250 cf066bf8 2019-05-09 stsp char *ondisk_path;
6251 cf066bf8 2019-05-09 stsp
6252 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6253 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6254 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6255 5f8a88c6 2019-08-03 stsp continue;
6256 5f8a88c6 2019-08-03 stsp
6257 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6258 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6259 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6260 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6261 cf066bf8 2019-05-09 stsp continue;
6262 cf066bf8 2019-05-09 stsp
6263 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6264 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6265 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6266 cf066bf8 2019-05-09 stsp goto done;
6267 cf066bf8 2019-05-09 stsp }
6268 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6269 2e1fa222 2020-07-23 stsp free(ondisk_path);
6270 2e1fa222 2020-07-23 stsp if (err)
6271 cf066bf8 2019-05-09 stsp goto done;
6272 cf066bf8 2019-05-09 stsp }
6273 036813ee 2019-05-09 stsp
6274 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6275 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6276 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6277 036813ee 2019-05-09 stsp if (err)
6278 036813ee 2019-05-09 stsp goto done;
6279 036813ee 2019-05-09 stsp
6280 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6281 de18fc63 2019-05-09 stsp if (err)
6282 de18fc63 2019-05-09 stsp goto done;
6283 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6284 10604dce 2021-09-24 thomas nparents++;
6285 10604dce 2021-09-24 thomas if (parent_id2) {
6286 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6287 10604dce 2021-09-24 thomas if (err)
6288 10604dce 2021-09-24 thomas goto done;
6289 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6290 10604dce 2021-09-24 thomas nparents++;
6291 10604dce 2021-09-24 thomas }
6292 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6293 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6294 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6295 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6296 33ad4cbe 2019-05-12 jcs free(logmsg);
6297 9d40349a 2019-05-09 stsp if (err)
6298 9d40349a 2019-05-09 stsp goto done;
6299 9d40349a 2019-05-09 stsp
6300 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6301 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6302 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6303 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6304 09f5bd90 2019-05-09 stsp goto done;
6305 09f5bd90 2019-05-09 stsp }
6306 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6307 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6308 09f5bd90 2019-05-09 stsp if (err)
6309 09f5bd90 2019-05-09 stsp goto done;
6310 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6311 ebf99748 2019-05-09 stsp if (err)
6312 ebf99748 2019-05-09 stsp goto done;
6313 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6314 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6315 09f5bd90 2019-05-09 stsp goto done;
6316 09f5bd90 2019-05-09 stsp }
6317 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6318 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6319 f2c16586 2019-05-09 stsp if (err)
6320 f2c16586 2019-05-09 stsp goto done;
6321 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6322 f2c16586 2019-05-09 stsp if (err)
6323 f2c16586 2019-05-09 stsp goto done;
6324 f2c16586 2019-05-09 stsp
6325 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6326 09f5bd90 2019-05-09 stsp if (err)
6327 09f5bd90 2019-05-09 stsp goto done;
6328 09f5bd90 2019-05-09 stsp
6329 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6330 ebf99748 2019-05-09 stsp if (err)
6331 ebf99748 2019-05-09 stsp goto done;
6332 c4296144 2019-05-09 stsp done:
6333 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6334 588edf97 2019-05-10 stsp if (head_tree)
6335 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6336 588edf97 2019-05-10 stsp if (head_commit)
6337 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6338 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6339 2f17228e 2019-05-12 stsp if (head_ref2) {
6340 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6341 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6342 2f17228e 2019-05-12 stsp err = unlockerr;
6343 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6344 39cd0ff6 2019-07-12 stsp }
6345 39cd0ff6 2019-07-12 stsp return err;
6346 39cd0ff6 2019-07-12 stsp }
6347 5c1e53bc 2019-07-28 stsp
6348 5c1e53bc 2019-07-28 stsp static const struct got_error *
6349 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6350 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6351 5c1e53bc 2019-07-28 stsp {
6352 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6353 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6354 39cd0ff6 2019-07-12 stsp
6355 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6356 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6357 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6358 5c1e53bc 2019-07-28 stsp
6359 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6360 5c1e53bc 2019-07-28 stsp ct_path++;
6361 5c1e53bc 2019-07-28 stsp
6362 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6363 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6364 5c1e53bc 2019-07-28 stsp break;
6365 5c1e53bc 2019-07-28 stsp }
6366 5c1e53bc 2019-07-28 stsp
6367 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6368 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6369 5c1e53bc 2019-07-28 stsp
6370 5c1e53bc 2019-07-28 stsp return NULL;
6371 5c1e53bc 2019-07-28 stsp }
6372 5c1e53bc 2019-07-28 stsp
6373 f0b75401 2019-08-03 stsp static const struct got_error *
6374 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6375 f0b75401 2019-08-03 stsp {
6376 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6377 f0b75401 2019-08-03 stsp
6378 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6379 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6380 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6381 f0b75401 2019-08-03 stsp }
6382 f0b75401 2019-08-03 stsp
6383 f0b75401 2019-08-03 stsp return NULL;
6384 f0b75401 2019-08-03 stsp }
6385 f0b75401 2019-08-03 stsp
6386 5f8a88c6 2019-08-03 stsp static const struct got_error *
6387 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6388 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6389 5f8a88c6 2019-08-03 stsp {
6390 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6391 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6392 5f8a88c6 2019-08-03 stsp
6393 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6394 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6395 5f8a88c6 2019-08-03 stsp continue;
6396 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6397 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6398 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6399 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6400 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6401 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6402 5f8a88c6 2019-08-03 stsp }
6403 5f8a88c6 2019-08-03 stsp
6404 5f8a88c6 2019-08-03 stsp return NULL;
6405 5f8a88c6 2019-08-03 stsp }
6406 5f8a88c6 2019-08-03 stsp
6407 39cd0ff6 2019-07-12 stsp const struct got_error *
6408 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6409 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6410 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6411 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6412 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6413 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6414 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6415 39cd0ff6 2019-07-12 stsp {
6416 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6417 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6418 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6419 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6420 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6421 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6422 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6423 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6424 ef899790 2022-11-01 thomas char *diff_path = NULL;
6425 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6426 39cd0ff6 2019-07-12 stsp
6427 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6428 39cd0ff6 2019-07-12 stsp
6429 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6430 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6431 39cd0ff6 2019-07-12 stsp
6432 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6433 39cd0ff6 2019-07-12 stsp if (err)
6434 39cd0ff6 2019-07-12 stsp goto done;
6435 39cd0ff6 2019-07-12 stsp
6436 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6437 39cd0ff6 2019-07-12 stsp if (err)
6438 39cd0ff6 2019-07-12 stsp goto done;
6439 39cd0ff6 2019-07-12 stsp
6440 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6441 39cd0ff6 2019-07-12 stsp if (err)
6442 39cd0ff6 2019-07-12 stsp goto done;
6443 39cd0ff6 2019-07-12 stsp
6444 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6445 39cd0ff6 2019-07-12 stsp if (err)
6446 39cd0ff6 2019-07-12 stsp goto done;
6447 39cd0ff6 2019-07-12 stsp
6448 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6449 5f8a88c6 2019-08-03 stsp &have_staged_files);
6450 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6451 5f8a88c6 2019-08-03 stsp goto done;
6452 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6453 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6454 5f8a88c6 2019-08-03 stsp if (err)
6455 5f8a88c6 2019-08-03 stsp goto done;
6456 5f8a88c6 2019-08-03 stsp }
6457 5f8a88c6 2019-08-03 stsp
6458 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6459 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6460 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6461 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6462 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6463 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6464 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6465 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6466 ef899790 2022-11-01 thomas if (show_diff) {
6467 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6468 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6469 ef899790 2022-11-01 thomas if (err)
6470 ef899790 2022-11-01 thomas goto done;
6471 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6472 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6473 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6474 ef899790 2022-11-01 thomas goto done;
6475 ef899790 2022-11-01 thomas }
6476 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6477 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6478 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6479 ef899790 2022-11-01 thomas goto done;
6480 ef899790 2022-11-01 thomas }
6481 ef899790 2022-11-01 thomas }
6482 ef899790 2022-11-01 thomas
6483 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6484 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6485 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6486 5c1e53bc 2019-07-28 stsp if (err)
6487 5c1e53bc 2019-07-28 stsp goto done;
6488 5c1e53bc 2019-07-28 stsp }
6489 39cd0ff6 2019-07-12 stsp
6490 ef899790 2022-11-01 thomas if (show_diff) {
6491 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6492 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6493 ef899790 2022-11-01 thomas goto done;
6494 ef899790 2022-11-01 thomas }
6495 ef899790 2022-11-01 thomas }
6496 ef899790 2022-11-01 thomas
6497 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6498 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6499 39cd0ff6 2019-07-12 stsp goto done;
6500 5c1e53bc 2019-07-28 stsp }
6501 5c1e53bc 2019-07-28 stsp
6502 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6503 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6504 5c1e53bc 2019-07-28 stsp if (err)
6505 5c1e53bc 2019-07-28 stsp goto done;
6506 39cd0ff6 2019-07-12 stsp }
6507 f0b75401 2019-08-03 stsp
6508 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6509 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6510 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6511 f0b75401 2019-08-03 stsp
6512 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6513 f0b75401 2019-08-03 stsp ct_path++;
6514 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6515 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6516 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6517 b50cabdf 2019-07-12 stsp if (err)
6518 b50cabdf 2019-07-12 stsp goto done;
6519 f0b75401 2019-08-03 stsp
6520 b50cabdf 2019-07-12 stsp }
6521 b50cabdf 2019-07-12 stsp
6522 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6523 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6524 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6525 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6526 39cd0ff6 2019-07-12 stsp if (err)
6527 39cd0ff6 2019-07-12 stsp goto done;
6528 39cd0ff6 2019-07-12 stsp
6529 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6530 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6531 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6532 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6533 39cd0ff6 2019-07-12 stsp err = sync_err;
6534 39cd0ff6 2019-07-12 stsp done:
6535 39cd0ff6 2019-07-12 stsp if (fileindex)
6536 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6537 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6538 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6539 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6540 39cd0ff6 2019-07-12 stsp err = unlockerr;
6541 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6542 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6543 21c2d8be 2023-01-10 thomas
6544 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6545 39cd0ff6 2019-07-12 stsp }
6546 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6547 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6548 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6549 ef899790 2022-11-01 thomas free(diff_path);
6550 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6551 ef899790 2022-11-01 thomas err == NULL)
6552 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6553 c4296144 2019-05-09 stsp return err;
6554 8656d6c4 2019-05-20 stsp }
6555 8656d6c4 2019-05-20 stsp
6556 8656d6c4 2019-05-20 stsp const char *
6557 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6558 8656d6c4 2019-05-20 stsp {
6559 8656d6c4 2019-05-20 stsp return ct->path;
6560 8656d6c4 2019-05-20 stsp }
6561 8656d6c4 2019-05-20 stsp
6562 8656d6c4 2019-05-20 stsp unsigned int
6563 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6564 8656d6c4 2019-05-20 stsp {
6565 8656d6c4 2019-05-20 stsp return ct->status;
6566 818c7501 2019-07-11 stsp }
6567 818c7501 2019-07-11 stsp
6568 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6569 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6570 818c7501 2019-07-11 stsp struct got_repository *repo;
6571 818c7501 2019-07-11 stsp };
6572 818c7501 2019-07-11 stsp
6573 818c7501 2019-07-11 stsp static const struct got_error *
6574 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6575 818c7501 2019-07-11 stsp {
6576 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6577 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6578 818c7501 2019-07-11 stsp unsigned char status;
6579 818c7501 2019-07-11 stsp struct stat sb;
6580 818c7501 2019-07-11 stsp char *ondisk_path;
6581 818c7501 2019-07-11 stsp
6582 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6583 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6584 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6585 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6586 818c7501 2019-07-11 stsp
6587 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6588 818c7501 2019-07-11 stsp == -1)
6589 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6590 818c7501 2019-07-11 stsp
6591 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6592 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6593 818c7501 2019-07-11 stsp free(ondisk_path);
6594 818c7501 2019-07-11 stsp if (err)
6595 818c7501 2019-07-11 stsp return err;
6596 818c7501 2019-07-11 stsp
6597 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6598 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6599 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6600 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6601 818c7501 2019-07-11 stsp
6602 818c7501 2019-07-11 stsp return NULL;
6603 818c7501 2019-07-11 stsp }
6604 818c7501 2019-07-11 stsp
6605 818c7501 2019-07-11 stsp const struct got_error *
6606 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6607 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6608 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6609 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6610 818c7501 2019-07-11 stsp {
6611 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6612 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6613 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6614 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6615 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6616 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6617 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6618 818c7501 2019-07-11 stsp
6619 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6620 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6621 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6622 818c7501 2019-07-11 stsp
6623 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6624 818c7501 2019-07-11 stsp if (err)
6625 818c7501 2019-07-11 stsp return err;
6626 818c7501 2019-07-11 stsp
6627 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6628 818c7501 2019-07-11 stsp if (err)
6629 818c7501 2019-07-11 stsp goto done;
6630 818c7501 2019-07-11 stsp
6631 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6632 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6633 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6634 818c7501 2019-07-11 stsp &ok_arg);
6635 818c7501 2019-07-11 stsp if (err)
6636 818c7501 2019-07-11 stsp goto done;
6637 818c7501 2019-07-11 stsp
6638 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6639 818c7501 2019-07-11 stsp if (err)
6640 818c7501 2019-07-11 stsp goto done;
6641 818c7501 2019-07-11 stsp
6642 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6643 818c7501 2019-07-11 stsp if (err)
6644 818c7501 2019-07-11 stsp goto done;
6645 818c7501 2019-07-11 stsp
6646 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6647 818c7501 2019-07-11 stsp if (err)
6648 818c7501 2019-07-11 stsp goto done;
6649 818c7501 2019-07-11 stsp
6650 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6651 818c7501 2019-07-11 stsp 0);
6652 e51d7b55 2020-01-04 stsp if (err)
6653 e51d7b55 2020-01-04 stsp goto done;
6654 e51d7b55 2020-01-04 stsp
6655 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6656 818c7501 2019-07-11 stsp if (err)
6657 818c7501 2019-07-11 stsp goto done;
6658 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6659 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6660 e51d7b55 2020-01-04 stsp goto done;
6661 e51d7b55 2020-01-04 stsp }
6662 818c7501 2019-07-11 stsp
6663 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6664 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6665 818c7501 2019-07-11 stsp if (err)
6666 818c7501 2019-07-11 stsp goto done;
6667 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6668 818c7501 2019-07-11 stsp if (err)
6669 818c7501 2019-07-11 stsp goto done;
6670 818c7501 2019-07-11 stsp
6671 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6672 818c7501 2019-07-11 stsp
6673 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6674 818c7501 2019-07-11 stsp if (err)
6675 818c7501 2019-07-11 stsp goto done;
6676 818c7501 2019-07-11 stsp
6677 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6678 818c7501 2019-07-11 stsp if (err)
6679 818c7501 2019-07-11 stsp goto done;
6680 818c7501 2019-07-11 stsp
6681 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6682 818c7501 2019-07-11 stsp worktree->base_commit_id);
6683 818c7501 2019-07-11 stsp if (err)
6684 818c7501 2019-07-11 stsp goto done;
6685 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6686 818c7501 2019-07-11 stsp if (err)
6687 818c7501 2019-07-11 stsp goto done;
6688 818c7501 2019-07-11 stsp
6689 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6690 818c7501 2019-07-11 stsp if (err)
6691 818c7501 2019-07-11 stsp goto done;
6692 818c7501 2019-07-11 stsp done:
6693 818c7501 2019-07-11 stsp free(fileindex_path);
6694 818c7501 2019-07-11 stsp free(tmp_branch_name);
6695 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6696 818c7501 2019-07-11 stsp free(branch_ref_name);
6697 818c7501 2019-07-11 stsp if (branch_ref)
6698 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6699 818c7501 2019-07-11 stsp if (wt_branch)
6700 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6701 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6702 818c7501 2019-07-11 stsp if (err) {
6703 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6704 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6705 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6706 818c7501 2019-07-11 stsp }
6707 818c7501 2019-07-11 stsp if (*tmp_branch) {
6708 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6709 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6710 818c7501 2019-07-11 stsp }
6711 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6712 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6713 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6714 3e3a69f1 2019-07-25 stsp }
6715 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6716 818c7501 2019-07-11 stsp }
6717 818c7501 2019-07-11 stsp return err;
6718 818c7501 2019-07-11 stsp }
6719 818c7501 2019-07-11 stsp
6720 818c7501 2019-07-11 stsp const struct got_error *
6721 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6722 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6723 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6724 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6725 818c7501 2019-07-11 stsp {
6726 818c7501 2019-07-11 stsp const struct got_error *err;
6727 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6728 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6730 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6731 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6732 818c7501 2019-07-11 stsp
6733 818c7501 2019-07-11 stsp *commit_id = NULL;
6734 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6735 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6736 3e3a69f1 2019-07-25 stsp *branch = NULL;
6737 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6738 3e3a69f1 2019-07-25 stsp
6739 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6740 3e3a69f1 2019-07-25 stsp if (err)
6741 3e3a69f1 2019-07-25 stsp return err;
6742 818c7501 2019-07-11 stsp
6743 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6744 3e3a69f1 2019-07-25 stsp if (err)
6745 f032f1f7 2019-08-04 stsp goto done;
6746 f032f1f7 2019-08-04 stsp
6747 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 f032f1f7 2019-08-04 stsp &have_staged_files);
6749 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6750 f032f1f7 2019-08-04 stsp goto done;
6751 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6752 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6753 3e3a69f1 2019-07-25 stsp goto done;
6754 f032f1f7 2019-08-04 stsp }
6755 3e3a69f1 2019-07-25 stsp
6756 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6757 818c7501 2019-07-11 stsp if (err)
6758 f032f1f7 2019-08-04 stsp goto done;
6759 818c7501 2019-07-11 stsp
6760 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6761 818c7501 2019-07-11 stsp if (err)
6762 818c7501 2019-07-11 stsp goto done;
6763 818c7501 2019-07-11 stsp
6764 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6765 818c7501 2019-07-11 stsp if (err)
6766 818c7501 2019-07-11 stsp goto done;
6767 818c7501 2019-07-11 stsp
6768 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6769 818c7501 2019-07-11 stsp if (err)
6770 818c7501 2019-07-11 stsp goto done;
6771 818c7501 2019-07-11 stsp
6772 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6773 818c7501 2019-07-11 stsp if (err)
6774 818c7501 2019-07-11 stsp goto done;
6775 818c7501 2019-07-11 stsp
6776 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6777 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6778 818c7501 2019-07-11 stsp if (err)
6779 818c7501 2019-07-11 stsp goto done;
6780 818c7501 2019-07-11 stsp
6781 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6782 818c7501 2019-07-11 stsp if (err)
6783 818c7501 2019-07-11 stsp goto done;
6784 818c7501 2019-07-11 stsp
6785 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6786 818c7501 2019-07-11 stsp if (err)
6787 818c7501 2019-07-11 stsp goto done;
6788 818c7501 2019-07-11 stsp
6789 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6790 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6791 818c7501 2019-07-11 stsp if (err)
6792 818c7501 2019-07-11 stsp goto done;
6793 818c7501 2019-07-11 stsp
6794 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp goto done;
6797 818c7501 2019-07-11 stsp done:
6798 818c7501 2019-07-11 stsp free(commit_ref_name);
6799 818c7501 2019-07-11 stsp free(branch_ref_name);
6800 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6801 818c7501 2019-07-11 stsp if (commit_ref)
6802 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6803 818c7501 2019-07-11 stsp if (branch_ref)
6804 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6805 818c7501 2019-07-11 stsp if (err) {
6806 818c7501 2019-07-11 stsp free(*commit_id);
6807 818c7501 2019-07-11 stsp *commit_id = NULL;
6808 818c7501 2019-07-11 stsp if (*tmp_branch) {
6809 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6810 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6811 818c7501 2019-07-11 stsp }
6812 818c7501 2019-07-11 stsp if (*new_base_branch) {
6813 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6814 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6815 818c7501 2019-07-11 stsp }
6816 818c7501 2019-07-11 stsp if (*branch) {
6817 818c7501 2019-07-11 stsp got_ref_close(*branch);
6818 818c7501 2019-07-11 stsp *branch = NULL;
6819 818c7501 2019-07-11 stsp }
6820 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6821 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6822 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6823 3e3a69f1 2019-07-25 stsp }
6824 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6825 818c7501 2019-07-11 stsp }
6826 818c7501 2019-07-11 stsp return err;
6827 c4296144 2019-05-09 stsp }
6828 818c7501 2019-07-11 stsp
6829 818c7501 2019-07-11 stsp const struct got_error *
6830 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6831 818c7501 2019-07-11 stsp {
6832 818c7501 2019-07-11 stsp const struct got_error *err;
6833 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6834 818c7501 2019-07-11 stsp
6835 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6836 818c7501 2019-07-11 stsp if (err)
6837 818c7501 2019-07-11 stsp return err;
6838 818c7501 2019-07-11 stsp
6839 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6840 818c7501 2019-07-11 stsp free(tmp_branch_name);
6841 818c7501 2019-07-11 stsp return NULL;
6842 818c7501 2019-07-11 stsp }
6843 818c7501 2019-07-11 stsp
6844 818c7501 2019-07-11 stsp static const struct got_error *
6845 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6846 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6847 818c7501 2019-07-11 stsp {
6848 0ebf8283 2019-07-24 stsp *logmsg = arg;
6849 818c7501 2019-07-11 stsp return NULL;
6850 818c7501 2019-07-11 stsp }
6851 818c7501 2019-07-11 stsp
6852 818c7501 2019-07-11 stsp static const struct got_error *
6853 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6854 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6855 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6856 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6857 818c7501 2019-07-11 stsp {
6858 818c7501 2019-07-11 stsp return NULL;
6859 01757395 2019-07-12 stsp }
6860 01757395 2019-07-12 stsp
6861 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6862 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6863 01757395 2019-07-12 stsp void *progress_arg;
6864 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6865 01757395 2019-07-12 stsp };
6866 01757395 2019-07-12 stsp
6867 01757395 2019-07-12 stsp static const struct got_error *
6868 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6869 01757395 2019-07-12 stsp {
6870 01757395 2019-07-12 stsp const struct got_error *err;
6871 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6872 01757395 2019-07-12 stsp char *p;
6873 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6874 01757395 2019-07-12 stsp
6875 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6876 01757395 2019-07-12 stsp if (err)
6877 01757395 2019-07-12 stsp return err;
6878 01757395 2019-07-12 stsp
6879 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6880 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6881 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6882 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6883 01757395 2019-07-12 stsp return NULL;
6884 01757395 2019-07-12 stsp
6885 01757395 2019-07-12 stsp p = strdup(path);
6886 01757395 2019-07-12 stsp if (p == NULL)
6887 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6888 01757395 2019-07-12 stsp
6889 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6890 01757395 2019-07-12 stsp if (err || new == NULL)
6891 01757395 2019-07-12 stsp free(p);
6892 01757395 2019-07-12 stsp return err;
6893 818c7501 2019-07-11 stsp }
6894 818c7501 2019-07-11 stsp
6895 0ebf8283 2019-07-24 stsp static const struct got_error *
6896 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6897 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6898 818c7501 2019-07-11 stsp {
6899 818c7501 2019-07-11 stsp const struct got_error *err;
6900 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6901 818c7501 2019-07-11 stsp
6902 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6903 818c7501 2019-07-11 stsp if (err) {
6904 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6905 818c7501 2019-07-11 stsp goto done;
6906 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6907 818c7501 2019-07-11 stsp if (err)
6908 818c7501 2019-07-11 stsp goto done;
6909 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6910 818c7501 2019-07-11 stsp if (err)
6911 818c7501 2019-07-11 stsp goto done;
6912 de05890f 2020-03-05 stsp } else if (is_rebase) {
6913 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6914 818c7501 2019-07-11 stsp int cmp;
6915 818c7501 2019-07-11 stsp
6916 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6917 818c7501 2019-07-11 stsp if (err)
6918 818c7501 2019-07-11 stsp goto done;
6919 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6920 818c7501 2019-07-11 stsp free(stored_id);
6921 818c7501 2019-07-11 stsp if (cmp != 0) {
6922 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6923 818c7501 2019-07-11 stsp goto done;
6924 818c7501 2019-07-11 stsp }
6925 818c7501 2019-07-11 stsp }
6926 0ebf8283 2019-07-24 stsp done:
6927 0ebf8283 2019-07-24 stsp if (commit_ref)
6928 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6929 0ebf8283 2019-07-24 stsp return err;
6930 0ebf8283 2019-07-24 stsp }
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp static const struct got_error *
6933 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6934 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6935 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6936 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6937 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6939 0ebf8283 2019-07-24 stsp {
6940 0ebf8283 2019-07-24 stsp const struct got_error *err;
6941 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6942 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6943 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6944 818c7501 2019-07-11 stsp
6945 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6946 0ebf8283 2019-07-24 stsp
6947 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6948 0ebf8283 2019-07-24 stsp if (err)
6949 0ebf8283 2019-07-24 stsp return err;
6950 0ebf8283 2019-07-24 stsp
6951 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6952 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6953 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6954 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6955 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6956 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6957 818c7501 2019-07-11 stsp if (commit_ref)
6958 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6959 818c7501 2019-07-11 stsp return err;
6960 818c7501 2019-07-11 stsp }
6961 818c7501 2019-07-11 stsp
6962 818c7501 2019-07-11 stsp const struct got_error *
6963 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6964 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6965 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6966 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6967 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6968 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6969 818c7501 2019-07-11 stsp {
6970 0ebf8283 2019-07-24 stsp const struct got_error *err;
6971 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6972 0ebf8283 2019-07-24 stsp
6973 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6974 0ebf8283 2019-07-24 stsp if (err)
6975 0ebf8283 2019-07-24 stsp return err;
6976 0ebf8283 2019-07-24 stsp
6977 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6978 0ebf8283 2019-07-24 stsp if (err)
6979 0ebf8283 2019-07-24 stsp goto done;
6980 0ebf8283 2019-07-24 stsp
6981 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6982 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6983 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6984 0ebf8283 2019-07-24 stsp done:
6985 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6986 0ebf8283 2019-07-24 stsp return err;
6987 0ebf8283 2019-07-24 stsp }
6988 0ebf8283 2019-07-24 stsp
6989 0ebf8283 2019-07-24 stsp const struct got_error *
6990 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6991 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6992 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6993 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6994 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6995 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6996 0ebf8283 2019-07-24 stsp {
6997 0ebf8283 2019-07-24 stsp const struct got_error *err;
6998 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6999 0ebf8283 2019-07-24 stsp
7000 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7001 0ebf8283 2019-07-24 stsp if (err)
7002 0ebf8283 2019-07-24 stsp return err;
7003 0ebf8283 2019-07-24 stsp
7004 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7005 0ebf8283 2019-07-24 stsp if (err)
7006 0ebf8283 2019-07-24 stsp goto done;
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7009 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7010 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7011 0ebf8283 2019-07-24 stsp done:
7012 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7013 0ebf8283 2019-07-24 stsp return err;
7014 0ebf8283 2019-07-24 stsp }
7015 0ebf8283 2019-07-24 stsp
7016 0ebf8283 2019-07-24 stsp static const struct got_error *
7017 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7018 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7019 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7020 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7021 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7022 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7023 0ebf8283 2019-07-24 stsp {
7024 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7025 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7026 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7027 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7028 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7029 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7030 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7031 818c7501 2019-07-11 stsp
7032 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7033 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7034 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7035 a0e95631 2019-07-12 stsp
7036 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7037 818c7501 2019-07-11 stsp
7038 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7039 a0e95631 2019-07-12 stsp if (err)
7040 3e3a69f1 2019-07-25 stsp return err;
7041 a0e95631 2019-07-12 stsp
7042 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7043 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7044 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7045 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7046 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7047 01757395 2019-07-12 stsp /*
7048 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7049 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7050 6c13b005 2021-09-02 stsp *
7051 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7052 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7053 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7054 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7055 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7056 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7057 01757395 2019-07-12 stsp */
7058 01757395 2019-07-12 stsp if (merged_paths) {
7059 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7060 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7061 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7062 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7063 f2a9dc41 2019-12-13 tracey 0);
7064 01757395 2019-07-12 stsp if (err)
7065 01757395 2019-07-12 stsp goto done;
7066 01757395 2019-07-12 stsp }
7067 01757395 2019-07-12 stsp } else {
7068 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7069 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7070 01757395 2019-07-12 stsp if (err)
7071 01757395 2019-07-12 stsp goto done;
7072 01757395 2019-07-12 stsp }
7073 a0e95631 2019-07-12 stsp
7074 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7075 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7076 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7077 a0e95631 2019-07-12 stsp if (err)
7078 a0e95631 2019-07-12 stsp goto done;
7079 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7080 a0e95631 2019-07-12 stsp goto done;
7081 ff0d2220 2019-07-11 stsp }
7082 818c7501 2019-07-11 stsp
7083 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7084 edd02c5e 2019-07-11 stsp if (err)
7085 edd02c5e 2019-07-11 stsp goto done;
7086 edd02c5e 2019-07-11 stsp
7087 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7088 818c7501 2019-07-11 stsp if (err)
7089 818c7501 2019-07-11 stsp goto done;
7090 818c7501 2019-07-11 stsp
7091 5943eee2 2019-08-13 stsp if (new_logmsg) {
7092 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7093 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7094 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7095 5943eee2 2019-08-13 stsp goto done;
7096 5943eee2 2019-08-13 stsp }
7097 5943eee2 2019-08-13 stsp } else {
7098 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7099 5943eee2 2019-08-13 stsp if (err)
7100 5943eee2 2019-08-13 stsp goto done;
7101 5943eee2 2019-08-13 stsp }
7102 0ebf8283 2019-07-24 stsp
7103 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7104 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7105 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7106 8db00f97 2022-07-22 thomas committer ? committer :
7107 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7108 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7109 a0e95631 2019-07-12 stsp if (err)
7110 a0e95631 2019-07-12 stsp goto done;
7111 a0e95631 2019-07-12 stsp
7112 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7113 a0e95631 2019-07-12 stsp if (err)
7114 a0e95631 2019-07-12 stsp goto done;
7115 a0e95631 2019-07-12 stsp
7116 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7117 a0e95631 2019-07-12 stsp if (err)
7118 a0e95631 2019-07-12 stsp goto done;
7119 a0e95631 2019-07-12 stsp
7120 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7121 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7122 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7123 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7124 a0e95631 2019-07-12 stsp err = sync_err;
7125 818c7501 2019-07-11 stsp done:
7126 a0e95631 2019-07-12 stsp free(fileindex_path);
7127 a0e95631 2019-07-12 stsp free(head_commit_id);
7128 a0e95631 2019-07-12 stsp if (head_ref)
7129 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7130 818c7501 2019-07-11 stsp if (err) {
7131 818c7501 2019-07-11 stsp free(*new_commit_id);
7132 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7133 818c7501 2019-07-11 stsp }
7134 818c7501 2019-07-11 stsp return err;
7135 818c7501 2019-07-11 stsp }
7136 818c7501 2019-07-11 stsp
7137 818c7501 2019-07-11 stsp const struct got_error *
7138 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7139 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7140 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7141 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7142 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7143 be94c032 2023-02-20 thomas struct got_repository *repo)
7144 0ebf8283 2019-07-24 stsp {
7145 0ebf8283 2019-07-24 stsp const struct got_error *err;
7146 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7147 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7148 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7149 0ebf8283 2019-07-24 stsp
7150 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7151 0ebf8283 2019-07-24 stsp if (err)
7152 0ebf8283 2019-07-24 stsp return err;
7153 0ebf8283 2019-07-24 stsp
7154 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7155 0ebf8283 2019-07-24 stsp if (err)
7156 0ebf8283 2019-07-24 stsp goto done;
7157 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7158 0ebf8283 2019-07-24 stsp if (err)
7159 0ebf8283 2019-07-24 stsp goto done;
7160 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7161 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp }
7164 0ebf8283 2019-07-24 stsp
7165 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7166 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7167 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7168 0ebf8283 2019-07-24 stsp done:
7169 0ebf8283 2019-07-24 stsp if (commit_ref)
7170 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7171 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7172 0ebf8283 2019-07-24 stsp free(commit_id);
7173 0ebf8283 2019-07-24 stsp return err;
7174 0ebf8283 2019-07-24 stsp }
7175 0ebf8283 2019-07-24 stsp
7176 0ebf8283 2019-07-24 stsp const struct got_error *
7177 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7178 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7179 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7180 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7181 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7182 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7183 0ebf8283 2019-07-24 stsp {
7184 0ebf8283 2019-07-24 stsp const struct got_error *err;
7185 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7186 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7187 0ebf8283 2019-07-24 stsp
7188 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7189 0ebf8283 2019-07-24 stsp if (err)
7190 0ebf8283 2019-07-24 stsp return err;
7191 0ebf8283 2019-07-24 stsp
7192 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7193 0ebf8283 2019-07-24 stsp if (err)
7194 0ebf8283 2019-07-24 stsp goto done;
7195 0ebf8283 2019-07-24 stsp
7196 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7197 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7198 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7199 0ebf8283 2019-07-24 stsp done:
7200 0ebf8283 2019-07-24 stsp if (commit_ref)
7201 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7202 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7203 0ebf8283 2019-07-24 stsp return err;
7204 0ebf8283 2019-07-24 stsp }
7205 0ebf8283 2019-07-24 stsp
7206 0ebf8283 2019-07-24 stsp const struct got_error *
7207 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7208 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7209 818c7501 2019-07-11 stsp {
7210 3e3a69f1 2019-07-25 stsp if (fileindex)
7211 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7212 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7213 69844fba 2019-07-11 stsp }
7214 69844fba 2019-07-11 stsp
7215 69844fba 2019-07-11 stsp static const struct got_error *
7216 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7217 69844fba 2019-07-11 stsp {
7218 69844fba 2019-07-11 stsp const struct got_error *err;
7219 69844fba 2019-07-11 stsp struct got_reference *ref;
7220 69844fba 2019-07-11 stsp
7221 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7222 69844fba 2019-07-11 stsp if (err) {
7223 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7224 69844fba 2019-07-11 stsp return NULL;
7225 69844fba 2019-07-11 stsp return err;
7226 69844fba 2019-07-11 stsp }
7227 69844fba 2019-07-11 stsp
7228 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7229 69844fba 2019-07-11 stsp got_ref_close(ref);
7230 69844fba 2019-07-11 stsp return err;
7231 818c7501 2019-07-11 stsp }
7232 818c7501 2019-07-11 stsp
7233 69844fba 2019-07-11 stsp static const struct got_error *
7234 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7235 69844fba 2019-07-11 stsp {
7236 69844fba 2019-07-11 stsp const struct got_error *err;
7237 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7238 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7239 69844fba 2019-07-11 stsp
7240 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7241 69844fba 2019-07-11 stsp if (err)
7242 69844fba 2019-07-11 stsp goto done;
7243 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7244 69844fba 2019-07-11 stsp if (err)
7245 69844fba 2019-07-11 stsp goto done;
7246 69844fba 2019-07-11 stsp
7247 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7248 69844fba 2019-07-11 stsp if (err)
7249 69844fba 2019-07-11 stsp goto done;
7250 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7251 69844fba 2019-07-11 stsp if (err)
7252 69844fba 2019-07-11 stsp goto done;
7253 69844fba 2019-07-11 stsp
7254 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7255 69844fba 2019-07-11 stsp if (err)
7256 69844fba 2019-07-11 stsp goto done;
7257 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7258 69844fba 2019-07-11 stsp if (err)
7259 69844fba 2019-07-11 stsp goto done;
7260 69844fba 2019-07-11 stsp
7261 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7262 69844fba 2019-07-11 stsp if (err)
7263 69844fba 2019-07-11 stsp goto done;
7264 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7265 69844fba 2019-07-11 stsp if (err)
7266 69844fba 2019-07-11 stsp goto done;
7267 69844fba 2019-07-11 stsp
7268 69844fba 2019-07-11 stsp done:
7269 69844fba 2019-07-11 stsp free(tmp_branch_name);
7270 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7271 69844fba 2019-07-11 stsp free(branch_ref_name);
7272 69844fba 2019-07-11 stsp free(commit_ref_name);
7273 e600f124 2021-03-21 stsp return err;
7274 e600f124 2021-03-21 stsp }
7275 e600f124 2021-03-21 stsp
7276 ef20f542 2022-06-26 thomas static const struct got_error *
7277 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7278 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7279 e600f124 2021-03-21 stsp {
7280 e600f124 2021-03-21 stsp const struct got_error *err;
7281 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7282 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7283 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7284 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7285 e600f124 2021-03-21 stsp char *refname = NULL;
7286 e600f124 2021-03-21 stsp
7287 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7288 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7289 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7290 e600f124 2021-03-21 stsp branch_name += 11;
7291 e600f124 2021-03-21 stsp
7292 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7293 e600f124 2021-03-21 stsp if (err)
7294 e600f124 2021-03-21 stsp return err;
7295 e600f124 2021-03-21 stsp
7296 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7297 e600f124 2021-03-21 stsp new_id_str) == -1) {
7298 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7299 e600f124 2021-03-21 stsp goto done;
7300 e600f124 2021-03-21 stsp }
7301 e600f124 2021-03-21 stsp
7302 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7303 e600f124 2021-03-21 stsp if (err)
7304 e600f124 2021-03-21 stsp goto done;
7305 e600f124 2021-03-21 stsp
7306 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7307 e600f124 2021-03-21 stsp if (err)
7308 e600f124 2021-03-21 stsp goto done;
7309 e600f124 2021-03-21 stsp
7310 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7311 e600f124 2021-03-21 stsp done:
7312 e600f124 2021-03-21 stsp free(new_id_str);
7313 e600f124 2021-03-21 stsp free(refname);
7314 e600f124 2021-03-21 stsp free(old_commit_id);
7315 e600f124 2021-03-21 stsp if (ref)
7316 e600f124 2021-03-21 stsp got_ref_close(ref);
7317 69844fba 2019-07-11 stsp return err;
7318 69844fba 2019-07-11 stsp }
7319 69844fba 2019-07-11 stsp
7320 818c7501 2019-07-11 stsp const struct got_error *
7321 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7322 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7323 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7324 f08f28be 2023-02-03 thomas int create_backup)
7325 818c7501 2019-07-11 stsp {
7326 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7327 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7328 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7329 818c7501 2019-07-11 stsp
7330 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7331 818c7501 2019-07-11 stsp if (err)
7332 818c7501 2019-07-11 stsp return err;
7333 e600f124 2021-03-21 stsp
7334 e600f124 2021-03-21 stsp if (create_backup) {
7335 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7336 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7337 e600f124 2021-03-21 stsp if (err)
7338 e600f124 2021-03-21 stsp goto done;
7339 e600f124 2021-03-21 stsp }
7340 818c7501 2019-07-11 stsp
7341 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7342 818c7501 2019-07-11 stsp if (err)
7343 818c7501 2019-07-11 stsp goto done;
7344 818c7501 2019-07-11 stsp
7345 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7346 818c7501 2019-07-11 stsp if (err)
7347 818c7501 2019-07-11 stsp goto done;
7348 818c7501 2019-07-11 stsp
7349 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7350 818c7501 2019-07-11 stsp if (err)
7351 818c7501 2019-07-11 stsp goto done;
7352 818c7501 2019-07-11 stsp
7353 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7354 a615e0e7 2020-12-16 stsp if (err)
7355 a615e0e7 2020-12-16 stsp goto done;
7356 a615e0e7 2020-12-16 stsp
7357 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7358 a615e0e7 2020-12-16 stsp if (err)
7359 a615e0e7 2020-12-16 stsp goto done;
7360 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7361 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7362 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7363 a615e0e7 2020-12-16 stsp err = sync_err;
7364 818c7501 2019-07-11 stsp done:
7365 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7366 a615e0e7 2020-12-16 stsp free(fileindex_path);
7367 818c7501 2019-07-11 stsp free(new_head_commit_id);
7368 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7369 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7370 818c7501 2019-07-11 stsp err = unlockerr;
7371 818c7501 2019-07-11 stsp return err;
7372 818c7501 2019-07-11 stsp }
7373 8641a332 2023-04-14 thomas
7374 8641a332 2023-04-14 thomas static const struct got_error *
7375 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7376 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7377 8641a332 2023-04-14 thomas struct got_repository *repo)
7378 8641a332 2023-04-14 thomas {
7379 8641a332 2023-04-14 thomas const struct got_error *err;
7380 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7381 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7382 8641a332 2023-04-14 thomas
7383 8641a332 2023-04-14 thomas if (id1) {
7384 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7385 8641a332 2023-04-14 thomas if (err)
7386 8641a332 2023-04-14 thomas goto done;
7387 8641a332 2023-04-14 thomas
7388 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7389 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7390 8641a332 2023-04-14 thomas if (err)
7391 8641a332 2023-04-14 thomas goto done;
7392 8641a332 2023-04-14 thomas }
7393 818c7501 2019-07-11 stsp
7394 8641a332 2023-04-14 thomas if (id2) {
7395 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7396 8641a332 2023-04-14 thomas if (err)
7397 8641a332 2023-04-14 thomas goto done;
7398 8641a332 2023-04-14 thomas
7399 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7400 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7401 8641a332 2023-04-14 thomas if (err)
7402 8641a332 2023-04-14 thomas goto done;
7403 8641a332 2023-04-14 thomas }
7404 8641a332 2023-04-14 thomas
7405 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7406 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7407 8641a332 2023-04-14 thomas if (err)
7408 8641a332 2023-04-14 thomas goto done;
7409 8641a332 2023-04-14 thomas done:
7410 8641a332 2023-04-14 thomas if (commit1)
7411 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7412 8641a332 2023-04-14 thomas if (commit2)
7413 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7414 8641a332 2023-04-14 thomas if (tree1)
7415 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7416 8641a332 2023-04-14 thomas if (tree2)
7417 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7418 8641a332 2023-04-14 thomas return err;
7419 8641a332 2023-04-14 thomas }
7420 8641a332 2023-04-14 thomas
7421 8641a332 2023-04-14 thomas static const struct got_error *
7422 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7423 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7424 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7425 8641a332 2023-04-14 thomas {
7426 8641a332 2023-04-14 thomas const struct got_error *err;
7427 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7428 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7429 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7430 8641a332 2023-04-14 thomas
7431 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7432 8641a332 2023-04-14 thomas
7433 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7434 8641a332 2023-04-14 thomas if (err)
7435 8641a332 2023-04-14 thomas goto done;
7436 8641a332 2023-04-14 thomas
7437 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7438 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7439 8641a332 2023-04-14 thomas
7440 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7441 8641a332 2023-04-14 thomas continue;
7442 8641a332 2023-04-14 thomas
7443 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7444 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7445 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7446 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7447 8641a332 2023-04-14 thomas goto done;
7448 8641a332 2023-04-14 thomas }
7449 8641a332 2023-04-14 thomas } else {
7450 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7451 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7452 8641a332 2023-04-14 thomas goto done;
7453 8641a332 2023-04-14 thomas }
7454 8641a332 2023-04-14 thomas
7455 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7456 8641a332 2023-04-14 thomas path_prefix, abspath);
7457 8641a332 2023-04-14 thomas if (err)
7458 8641a332 2023-04-14 thomas goto done;
7459 8641a332 2023-04-14 thomas free(abspath);
7460 8641a332 2023-04-14 thomas abspath = NULL;
7461 8641a332 2023-04-14 thomas }
7462 8641a332 2023-04-14 thomas
7463 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7464 8641a332 2023-04-14 thomas if (err)
7465 8641a332 2023-04-14 thomas goto done;
7466 8641a332 2023-04-14 thomas wt_path = NULL;
7467 8641a332 2023-04-14 thomas }
7468 8641a332 2023-04-14 thomas
7469 8641a332 2023-04-14 thomas done:
7470 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7471 8641a332 2023-04-14 thomas free(abspath);
7472 8641a332 2023-04-14 thomas free(wt_path);
7473 8641a332 2023-04-14 thomas return err;
7474 8641a332 2023-04-14 thomas }
7475 8641a332 2023-04-14 thomas
7476 8641a332 2023-04-14 thomas static const struct got_error *
7477 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7478 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7479 8641a332 2023-04-14 thomas struct got_repository *repo)
7480 8641a332 2023-04-14 thomas {
7481 8641a332 2023-04-14 thomas const struct got_error *err;
7482 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7483 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7484 8641a332 2023-04-14 thomas
7485 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7486 8641a332 2023-04-14 thomas if (err)
7487 8641a332 2023-04-14 thomas goto done;
7488 8641a332 2023-04-14 thomas
7489 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7490 8641a332 2023-04-14 thomas
7491 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7492 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7493 8641a332 2023-04-14 thomas if (err)
7494 8641a332 2023-04-14 thomas goto done;
7495 8641a332 2023-04-14 thomas done:
7496 8641a332 2023-04-14 thomas if (commit)
7497 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7498 8641a332 2023-04-14 thomas return err;
7499 8641a332 2023-04-14 thomas }
7500 8641a332 2023-04-14 thomas
7501 818c7501 2019-07-11 stsp const struct got_error *
7502 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7503 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7504 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7505 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7506 818c7501 2019-07-11 stsp {
7507 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7508 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7509 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7510 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7511 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7512 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7513 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7514 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7515 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7516 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7517 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7518 818c7501 2019-07-11 stsp
7519 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7520 8641a332 2023-04-14 thomas
7521 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7522 818c7501 2019-07-11 stsp if (err)
7523 818c7501 2019-07-11 stsp return err;
7524 8641a332 2023-04-14 thomas
7525 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7526 8641a332 2023-04-14 thomas if (err)
7527 8641a332 2023-04-14 thomas goto done;
7528 8641a332 2023-04-14 thomas
7529 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7530 8641a332 2023-04-14 thomas if (err)
7531 8641a332 2023-04-14 thomas goto done;
7532 8641a332 2023-04-14 thomas
7533 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7534 8641a332 2023-04-14 thomas if (err)
7535 8641a332 2023-04-14 thomas goto done;
7536 945f9229 2022-04-16 thomas
7537 8641a332 2023-04-14 thomas /*
7538 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7539 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7540 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7541 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7542 8641a332 2023-04-14 thomas */
7543 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7544 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7545 8641a332 2023-04-14 thomas if (err)
7546 8641a332 2023-04-14 thomas goto done;
7547 8641a332 2023-04-14 thomas
7548 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7549 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7550 818c7501 2019-07-11 stsp if (err)
7551 818c7501 2019-07-11 stsp goto done;
7552 818c7501 2019-07-11 stsp
7553 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7554 818c7501 2019-07-11 stsp if (err)
7555 818c7501 2019-07-11 stsp goto done;
7556 818c7501 2019-07-11 stsp
7557 818c7501 2019-07-11 stsp /*
7558 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7559 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7560 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7561 818c7501 2019-07-11 stsp */
7562 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7563 818c7501 2019-07-11 stsp if (err)
7564 818c7501 2019-07-11 stsp goto done;
7565 818c7501 2019-07-11 stsp
7566 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7567 25a102ed 2023-04-14 thomas if (err)
7568 25a102ed 2023-04-14 thomas goto done;
7569 25a102ed 2023-04-14 thomas
7570 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7571 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7572 818c7501 2019-07-11 stsp if (err)
7573 818c7501 2019-07-11 stsp goto done;
7574 818c7501 2019-07-11 stsp
7575 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7576 945f9229 2022-04-16 thomas worktree->path_prefix);
7577 ca355955 2019-07-12 stsp if (err)
7578 ca355955 2019-07-12 stsp goto done;
7579 ca355955 2019-07-12 stsp
7580 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7581 ca355955 2019-07-12 stsp if (err)
7582 ca355955 2019-07-12 stsp goto done;
7583 ca355955 2019-07-12 stsp
7584 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7585 818c7501 2019-07-11 stsp if (err)
7586 818c7501 2019-07-11 stsp goto done;
7587 818c7501 2019-07-11 stsp
7588 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7589 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7590 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7591 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7592 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7593 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7594 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7595 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7596 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7597 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7598 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7599 55bd499d 2019-07-12 stsp if (err)
7600 1f1abb7e 2019-08-08 stsp goto sync;
7601 55bd499d 2019-07-12 stsp
7602 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7603 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7604 ca355955 2019-07-12 stsp sync:
7605 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7606 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7607 ca355955 2019-07-12 stsp err = sync_err;
7608 818c7501 2019-07-11 stsp done:
7609 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7610 818c7501 2019-07-11 stsp got_ref_close(resolved);
7611 a3a2faf2 2019-07-12 stsp free(tree_id);
7612 818c7501 2019-07-11 stsp free(commit_id);
7613 8641a332 2023-04-14 thomas free(merged_commit_id);
7614 945f9229 2022-04-16 thomas if (commit)
7615 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7616 0ebf8283 2019-07-24 stsp if (fileindex)
7617 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7618 0ebf8283 2019-07-24 stsp free(fileindex_path);
7619 8641a332 2023-04-14 thomas free(commit_ref_name);
7620 8641a332 2023-04-14 thomas if (commit_ref)
7621 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7622 0ebf8283 2019-07-24 stsp
7623 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7624 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7625 0ebf8283 2019-07-24 stsp err = unlockerr;
7626 0ebf8283 2019-07-24 stsp return err;
7627 0ebf8283 2019-07-24 stsp }
7628 0ebf8283 2019-07-24 stsp
7629 0ebf8283 2019-07-24 stsp const struct got_error *
7630 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7631 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7632 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7633 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7634 0ebf8283 2019-07-24 stsp {
7635 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7636 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7637 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7638 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7639 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7640 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7641 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7642 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7643 0ebf8283 2019-07-24 stsp
7644 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7645 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7646 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7647 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7648 0ebf8283 2019-07-24 stsp
7649 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7650 0ebf8283 2019-07-24 stsp if (err)
7651 0ebf8283 2019-07-24 stsp return err;
7652 0ebf8283 2019-07-24 stsp
7653 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7654 0ebf8283 2019-07-24 stsp if (err)
7655 0ebf8283 2019-07-24 stsp goto done;
7656 0ebf8283 2019-07-24 stsp
7657 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7658 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7659 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7660 0ebf8283 2019-07-24 stsp &ok_arg);
7661 0ebf8283 2019-07-24 stsp if (err)
7662 0ebf8283 2019-07-24 stsp goto done;
7663 0ebf8283 2019-07-24 stsp
7664 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7665 0ebf8283 2019-07-24 stsp if (err)
7666 0ebf8283 2019-07-24 stsp goto done;
7667 0ebf8283 2019-07-24 stsp
7668 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7669 0ebf8283 2019-07-24 stsp if (err)
7670 0ebf8283 2019-07-24 stsp goto done;
7671 0ebf8283 2019-07-24 stsp
7672 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7673 0ebf8283 2019-07-24 stsp worktree);
7674 0ebf8283 2019-07-24 stsp if (err)
7675 0ebf8283 2019-07-24 stsp goto done;
7676 0ebf8283 2019-07-24 stsp
7677 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7678 0ebf8283 2019-07-24 stsp 0);
7679 0ebf8283 2019-07-24 stsp if (err)
7680 0ebf8283 2019-07-24 stsp goto done;
7681 0ebf8283 2019-07-24 stsp
7682 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7683 0ebf8283 2019-07-24 stsp if (err)
7684 0ebf8283 2019-07-24 stsp goto done;
7685 0ebf8283 2019-07-24 stsp
7686 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7687 0ebf8283 2019-07-24 stsp if (err)
7688 0ebf8283 2019-07-24 stsp goto done;
7689 0ebf8283 2019-07-24 stsp
7690 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7691 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7692 0ebf8283 2019-07-24 stsp if (err)
7693 0ebf8283 2019-07-24 stsp goto done;
7694 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7695 0ebf8283 2019-07-24 stsp if (err)
7696 0ebf8283 2019-07-24 stsp goto done;
7697 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7698 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7699 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7700 0ebf8283 2019-07-24 stsp goto done;
7701 0ebf8283 2019-07-24 stsp }
7702 0ebf8283 2019-07-24 stsp
7703 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7704 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7705 0ebf8283 2019-07-24 stsp if (err)
7706 0ebf8283 2019-07-24 stsp goto done;
7707 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7708 0ebf8283 2019-07-24 stsp if (err)
7709 0ebf8283 2019-07-24 stsp goto done;
7710 0ebf8283 2019-07-24 stsp
7711 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7712 0ebf8283 2019-07-24 stsp if (err)
7713 0ebf8283 2019-07-24 stsp goto done;
7714 0ebf8283 2019-07-24 stsp done:
7715 0ebf8283 2019-07-24 stsp free(fileindex_path);
7716 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7717 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7718 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7719 0ebf8283 2019-07-24 stsp if (wt_branch)
7720 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7721 0ebf8283 2019-07-24 stsp if (err) {
7722 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7723 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7724 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7725 0ebf8283 2019-07-24 stsp }
7726 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7727 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7728 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7729 0ebf8283 2019-07-24 stsp }
7730 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7731 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7732 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7733 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7734 3e3a69f1 2019-07-25 stsp }
7735 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7736 0ebf8283 2019-07-24 stsp }
7737 0ebf8283 2019-07-24 stsp return err;
7738 0ebf8283 2019-07-24 stsp }
7739 0ebf8283 2019-07-24 stsp
7740 0ebf8283 2019-07-24 stsp const struct got_error *
7741 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7742 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7743 0ebf8283 2019-07-24 stsp {
7744 3e3a69f1 2019-07-25 stsp if (fileindex)
7745 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7746 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7747 0ebf8283 2019-07-24 stsp }
7748 0ebf8283 2019-07-24 stsp
7749 0ebf8283 2019-07-24 stsp const struct got_error *
7750 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7751 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7752 0ebf8283 2019-07-24 stsp {
7753 0ebf8283 2019-07-24 stsp const struct got_error *err;
7754 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7755 0ebf8283 2019-07-24 stsp
7756 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7757 0ebf8283 2019-07-24 stsp if (err)
7758 0ebf8283 2019-07-24 stsp return err;
7759 0ebf8283 2019-07-24 stsp
7760 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7761 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7762 0ebf8283 2019-07-24 stsp return NULL;
7763 0ebf8283 2019-07-24 stsp }
7764 0ebf8283 2019-07-24 stsp
7765 0ebf8283 2019-07-24 stsp const struct got_error *
7766 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7767 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7768 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7769 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7770 0ebf8283 2019-07-24 stsp {
7771 0ebf8283 2019-07-24 stsp const struct got_error *err;
7772 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7773 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7774 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7775 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7776 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7777 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7778 0ebf8283 2019-07-24 stsp
7779 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7780 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7781 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7782 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7783 0ebf8283 2019-07-24 stsp
7784 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7785 3e3a69f1 2019-07-25 stsp if (err)
7786 3e3a69f1 2019-07-25 stsp return err;
7787 3e3a69f1 2019-07-25 stsp
7788 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7789 3e3a69f1 2019-07-25 stsp if (err)
7790 f032f1f7 2019-08-04 stsp goto done;
7791 f032f1f7 2019-08-04 stsp
7792 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7793 f032f1f7 2019-08-04 stsp &have_staged_files);
7794 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7795 f032f1f7 2019-08-04 stsp goto done;
7796 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7797 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7798 3e3a69f1 2019-07-25 stsp goto done;
7799 f032f1f7 2019-08-04 stsp }
7800 3e3a69f1 2019-07-25 stsp
7801 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7802 0ebf8283 2019-07-24 stsp if (err)
7803 f032f1f7 2019-08-04 stsp goto done;
7804 0ebf8283 2019-07-24 stsp
7805 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7806 0ebf8283 2019-07-24 stsp if (err)
7807 0ebf8283 2019-07-24 stsp goto done;
7808 0ebf8283 2019-07-24 stsp
7809 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7810 0ebf8283 2019-07-24 stsp if (err)
7811 0ebf8283 2019-07-24 stsp goto done;
7812 0ebf8283 2019-07-24 stsp
7813 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7814 0ebf8283 2019-07-24 stsp worktree);
7815 0ebf8283 2019-07-24 stsp if (err)
7816 0ebf8283 2019-07-24 stsp goto done;
7817 0ebf8283 2019-07-24 stsp
7818 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7819 0ebf8283 2019-07-24 stsp if (err)
7820 0ebf8283 2019-07-24 stsp goto done;
7821 0ebf8283 2019-07-24 stsp
7822 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7823 0ebf8283 2019-07-24 stsp if (err)
7824 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7826 0ebf8283 2019-07-24 stsp if (err)
7827 0ebf8283 2019-07-24 stsp goto done;
7828 0ebf8283 2019-07-24 stsp
7829 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7830 0ebf8283 2019-07-24 stsp if (err)
7831 0ebf8283 2019-07-24 stsp goto done;
7832 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7833 0ebf8283 2019-07-24 stsp if (err)
7834 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp
7836 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7837 0ebf8283 2019-07-24 stsp if (err)
7838 0ebf8283 2019-07-24 stsp goto done;
7839 0ebf8283 2019-07-24 stsp done:
7840 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7841 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7842 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7843 0ebf8283 2019-07-24 stsp if (commit_ref)
7844 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7845 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7846 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7847 0ebf8283 2019-07-24 stsp if (err) {
7848 0ebf8283 2019-07-24 stsp free(*commit_id);
7849 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7850 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7851 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7852 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7853 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7854 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7855 0ebf8283 2019-07-24 stsp }
7856 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7857 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7858 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7859 3e3a69f1 2019-07-25 stsp }
7860 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7861 0ebf8283 2019-07-24 stsp }
7862 0ebf8283 2019-07-24 stsp return err;
7863 0ebf8283 2019-07-24 stsp }
7864 0ebf8283 2019-07-24 stsp
7865 0ebf8283 2019-07-24 stsp static const struct got_error *
7866 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7867 0ebf8283 2019-07-24 stsp {
7868 0ebf8283 2019-07-24 stsp const struct got_error *err;
7869 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7870 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7871 0ebf8283 2019-07-24 stsp
7872 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7873 0ebf8283 2019-07-24 stsp if (err)
7874 0ebf8283 2019-07-24 stsp goto done;
7875 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7876 0ebf8283 2019-07-24 stsp if (err)
7877 0ebf8283 2019-07-24 stsp goto done;
7878 0ebf8283 2019-07-24 stsp
7879 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7880 0ebf8283 2019-07-24 stsp worktree);
7881 0ebf8283 2019-07-24 stsp if (err)
7882 0ebf8283 2019-07-24 stsp goto done;
7883 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7884 0ebf8283 2019-07-24 stsp if (err)
7885 0ebf8283 2019-07-24 stsp goto done;
7886 0ebf8283 2019-07-24 stsp
7887 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7888 0ebf8283 2019-07-24 stsp if (err)
7889 0ebf8283 2019-07-24 stsp goto done;
7890 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7891 0ebf8283 2019-07-24 stsp if (err)
7892 0ebf8283 2019-07-24 stsp goto done;
7893 0ebf8283 2019-07-24 stsp
7894 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7895 0ebf8283 2019-07-24 stsp if (err)
7896 0ebf8283 2019-07-24 stsp goto done;
7897 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7898 0ebf8283 2019-07-24 stsp if (err)
7899 0ebf8283 2019-07-24 stsp goto done;
7900 0ebf8283 2019-07-24 stsp done:
7901 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7902 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7903 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7904 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7905 0ebf8283 2019-07-24 stsp return err;
7906 0ebf8283 2019-07-24 stsp }
7907 0ebf8283 2019-07-24 stsp
7908 0ebf8283 2019-07-24 stsp const struct got_error *
7909 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7910 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7911 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7912 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7913 0ebf8283 2019-07-24 stsp {
7914 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7915 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7916 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7917 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7918 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7919 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7920 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7921 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7922 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7923 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7924 0ebf8283 2019-07-24 stsp
7925 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7926 8641a332 2023-04-14 thomas
7927 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7928 0ebf8283 2019-07-24 stsp if (err)
7929 0ebf8283 2019-07-24 stsp return err;
7930 945f9229 2022-04-16 thomas
7931 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7932 8641a332 2023-04-14 thomas if (err)
7933 8641a332 2023-04-14 thomas goto done;
7934 8641a332 2023-04-14 thomas
7935 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7936 8641a332 2023-04-14 thomas if (err) {
7937 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7938 8641a332 2023-04-14 thomas goto done;
7939 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7940 8641a332 2023-04-14 thomas commit_ref = NULL;
7941 8641a332 2023-04-14 thomas }
7942 8641a332 2023-04-14 thomas
7943 8641a332 2023-04-14 thomas if (commit_ref) {
7944 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7945 8641a332 2023-04-14 thomas if (err)
7946 8641a332 2023-04-14 thomas goto done;
7947 8641a332 2023-04-14 thomas
7948 8641a332 2023-04-14 thomas /*
7949 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7950 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7951 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7952 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7953 8641a332 2023-04-14 thomas */
7954 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7955 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7956 8641a332 2023-04-14 thomas if (err)
7957 8641a332 2023-04-14 thomas goto done;
7958 8641a332 2023-04-14 thomas }
7959 8641a332 2023-04-14 thomas
7960 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7961 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7962 0ebf8283 2019-07-24 stsp if (err)
7963 0ebf8283 2019-07-24 stsp goto done;
7964 0ebf8283 2019-07-24 stsp
7965 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7966 0ebf8283 2019-07-24 stsp if (err)
7967 0ebf8283 2019-07-24 stsp goto done;
7968 0ebf8283 2019-07-24 stsp
7969 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7970 0ebf8283 2019-07-24 stsp if (err)
7971 0ebf8283 2019-07-24 stsp goto done;
7972 0ebf8283 2019-07-24 stsp
7973 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7974 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7975 25a102ed 2023-04-14 thomas if (err)
7976 25a102ed 2023-04-14 thomas goto done;
7977 25a102ed 2023-04-14 thomas
7978 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7979 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7980 0ebf8283 2019-07-24 stsp if (err)
7981 0ebf8283 2019-07-24 stsp goto done;
7982 0ebf8283 2019-07-24 stsp
7983 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7984 0ebf8283 2019-07-24 stsp if (err)
7985 0ebf8283 2019-07-24 stsp goto done;
7986 0ebf8283 2019-07-24 stsp
7987 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7988 0ebf8283 2019-07-24 stsp if (err)
7989 0ebf8283 2019-07-24 stsp goto done;
7990 0ebf8283 2019-07-24 stsp
7991 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7992 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7993 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7994 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7995 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7996 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7997 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7998 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7999 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8000 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8001 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8002 0ebf8283 2019-07-24 stsp if (err)
8003 1f1abb7e 2019-08-08 stsp goto sync;
8004 0ebf8283 2019-07-24 stsp
8005 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8006 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8007 0ebf8283 2019-07-24 stsp sync:
8008 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8009 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8010 0ebf8283 2019-07-24 stsp err = sync_err;
8011 0ebf8283 2019-07-24 stsp done:
8012 8641a332 2023-04-14 thomas if (resolved)
8013 8641a332 2023-04-14 thomas got_ref_close(resolved);
8014 8641a332 2023-04-14 thomas if (commit_ref)
8015 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8016 8641a332 2023-04-14 thomas free(merged_commit_id);
8017 0ebf8283 2019-07-24 stsp free(tree_id);
8018 818c7501 2019-07-11 stsp free(fileindex_path);
8019 8641a332 2023-04-14 thomas free(commit_ref_name);
8020 818c7501 2019-07-11 stsp
8021 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8022 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8023 818c7501 2019-07-11 stsp err = unlockerr;
8024 818c7501 2019-07-11 stsp return err;
8025 818c7501 2019-07-11 stsp }
8026 0ebf8283 2019-07-24 stsp
8027 0ebf8283 2019-07-24 stsp const struct got_error *
8028 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8029 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8030 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8031 0ebf8283 2019-07-24 stsp {
8032 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8033 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8034 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8035 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8036 0ebf8283 2019-07-24 stsp
8037 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8038 0ebf8283 2019-07-24 stsp if (err)
8039 0ebf8283 2019-07-24 stsp return err;
8040 0ebf8283 2019-07-24 stsp
8041 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8042 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8043 e600f124 2021-03-21 stsp if (err)
8044 e600f124 2021-03-21 stsp goto done;
8045 e600f124 2021-03-21 stsp
8046 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8047 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8048 0ebf8283 2019-07-24 stsp if (err)
8049 0ebf8283 2019-07-24 stsp goto done;
8050 0ebf8283 2019-07-24 stsp
8051 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8052 0ebf8283 2019-07-24 stsp if (err)
8053 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp
8055 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8056 0ebf8283 2019-07-24 stsp if (err)
8057 0ebf8283 2019-07-24 stsp goto done;
8058 0ebf8283 2019-07-24 stsp
8059 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8060 0ebf8283 2019-07-24 stsp if (err)
8061 0ebf8283 2019-07-24 stsp goto done;
8062 0ebf8283 2019-07-24 stsp
8063 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8064 a615e0e7 2020-12-16 stsp if (err)
8065 a615e0e7 2020-12-16 stsp goto done;
8066 a615e0e7 2020-12-16 stsp
8067 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8068 a615e0e7 2020-12-16 stsp if (err)
8069 a615e0e7 2020-12-16 stsp goto done;
8070 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8071 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8072 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8073 a615e0e7 2020-12-16 stsp err = sync_err;
8074 0ebf8283 2019-07-24 stsp done:
8075 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8076 a615e0e7 2020-12-16 stsp free(fileindex_path);
8077 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8078 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8079 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8080 0ebf8283 2019-07-24 stsp err = unlockerr;
8081 0ebf8283 2019-07-24 stsp return err;
8082 0ebf8283 2019-07-24 stsp }
8083 0ebf8283 2019-07-24 stsp
8084 0ebf8283 2019-07-24 stsp const struct got_error *
8085 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8086 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8087 0ebf8283 2019-07-24 stsp {
8088 0ebf8283 2019-07-24 stsp const struct got_error *err;
8089 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8090 0ebf8283 2019-07-24 stsp
8091 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8092 0ebf8283 2019-07-24 stsp if (err)
8093 0ebf8283 2019-07-24 stsp return err;
8094 0ebf8283 2019-07-24 stsp
8095 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8096 0ebf8283 2019-07-24 stsp if (err)
8097 0ebf8283 2019-07-24 stsp goto done;
8098 0ebf8283 2019-07-24 stsp
8099 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8100 0ebf8283 2019-07-24 stsp done:
8101 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8102 2822a352 2019-10-15 stsp return err;
8103 2822a352 2019-10-15 stsp }
8104 2822a352 2019-10-15 stsp
8105 2822a352 2019-10-15 stsp const struct got_error *
8106 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8107 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8108 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8109 2822a352 2019-10-15 stsp struct got_repository *repo)
8110 2822a352 2019-10-15 stsp {
8111 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8112 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8113 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8114 2822a352 2019-10-15 stsp
8115 2822a352 2019-10-15 stsp *fileindex = NULL;
8116 2822a352 2019-10-15 stsp *branch_ref = NULL;
8117 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8118 2822a352 2019-10-15 stsp
8119 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8120 2822a352 2019-10-15 stsp if (err)
8121 2822a352 2019-10-15 stsp return err;
8122 2822a352 2019-10-15 stsp
8123 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8124 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8125 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8126 2822a352 2019-10-15 stsp "update -b or different branch name required");
8127 2822a352 2019-10-15 stsp goto done;
8128 2822a352 2019-10-15 stsp }
8129 2822a352 2019-10-15 stsp
8130 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8131 2822a352 2019-10-15 stsp if (err)
8132 2822a352 2019-10-15 stsp goto done;
8133 2822a352 2019-10-15 stsp
8134 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8135 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8136 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8137 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8138 2822a352 2019-10-15 stsp &ok_arg);
8139 2822a352 2019-10-15 stsp if (err)
8140 2822a352 2019-10-15 stsp goto done;
8141 2822a352 2019-10-15 stsp
8142 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8143 2822a352 2019-10-15 stsp if (err)
8144 2822a352 2019-10-15 stsp goto done;
8145 2822a352 2019-10-15 stsp
8146 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8147 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8148 2822a352 2019-10-15 stsp done:
8149 2822a352 2019-10-15 stsp if (err) {
8150 2822a352 2019-10-15 stsp if (*branch_ref) {
8151 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8152 2822a352 2019-10-15 stsp *branch_ref = NULL;
8153 2822a352 2019-10-15 stsp }
8154 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8155 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8156 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8157 2822a352 2019-10-15 stsp }
8158 2822a352 2019-10-15 stsp if (*fileindex) {
8159 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8160 2822a352 2019-10-15 stsp *fileindex = NULL;
8161 2822a352 2019-10-15 stsp }
8162 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8163 2822a352 2019-10-15 stsp }
8164 0cb83759 2019-08-03 stsp return err;
8165 0cb83759 2019-08-03 stsp }
8166 0cb83759 2019-08-03 stsp
8167 2822a352 2019-10-15 stsp const struct got_error *
8168 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8169 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8170 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8171 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8172 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8173 2822a352 2019-10-15 stsp {
8174 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8175 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8176 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8177 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8178 2822a352 2019-10-15 stsp
8179 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8180 2822a352 2019-10-15 stsp if (err)
8181 2822a352 2019-10-15 stsp goto done;
8182 2822a352 2019-10-15 stsp
8183 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8184 2822a352 2019-10-15 stsp if (err)
8185 2822a352 2019-10-15 stsp goto done;
8186 2822a352 2019-10-15 stsp
8187 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8188 945f9229 2022-04-16 thomas if (err)
8189 945f9229 2022-04-16 thomas goto done;
8190 945f9229 2022-04-16 thomas
8191 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8192 2822a352 2019-10-15 stsp worktree->path_prefix);
8193 2822a352 2019-10-15 stsp if (err)
8194 2822a352 2019-10-15 stsp goto done;
8195 2822a352 2019-10-15 stsp
8196 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8197 2822a352 2019-10-15 stsp if (err)
8198 2822a352 2019-10-15 stsp goto done;
8199 2822a352 2019-10-15 stsp
8200 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8201 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8202 2822a352 2019-10-15 stsp if (err)
8203 2822a352 2019-10-15 stsp goto sync;
8204 2822a352 2019-10-15 stsp
8205 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8206 2822a352 2019-10-15 stsp if (err)
8207 2822a352 2019-10-15 stsp goto sync;
8208 2822a352 2019-10-15 stsp
8209 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8210 6e210706 2021-01-22 stsp if (err)
8211 6e210706 2021-01-22 stsp goto sync;
8212 6e210706 2021-01-22 stsp
8213 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8214 2822a352 2019-10-15 stsp sync:
8215 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8216 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8217 2822a352 2019-10-15 stsp err = sync_err;
8218 2822a352 2019-10-15 stsp
8219 2822a352 2019-10-15 stsp done:
8220 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8221 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8222 2822a352 2019-10-15 stsp err = unlockerr;
8223 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8224 2822a352 2019-10-15 stsp
8225 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8226 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8227 2822a352 2019-10-15 stsp err = unlockerr;
8228 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8229 2822a352 2019-10-15 stsp
8230 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8231 2822a352 2019-10-15 stsp free(fileindex_path);
8232 2822a352 2019-10-15 stsp free(tree_id);
8233 945f9229 2022-04-16 thomas if (commit)
8234 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8235 2822a352 2019-10-15 stsp
8236 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8237 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8238 2822a352 2019-10-15 stsp err = unlockerr;
8239 2822a352 2019-10-15 stsp return err;
8240 2822a352 2019-10-15 stsp }
8241 2822a352 2019-10-15 stsp
8242 2822a352 2019-10-15 stsp const struct got_error *
8243 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8244 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8245 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8246 2822a352 2019-10-15 stsp {
8247 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8248 8b692cd0 2019-10-21 stsp
8249 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8250 8b692cd0 2019-10-21 stsp
8251 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8252 8b692cd0 2019-10-21 stsp
8253 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8254 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8255 8b692cd0 2019-10-21 stsp err = unlockerr;
8256 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8257 8b692cd0 2019-10-21 stsp
8258 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8259 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8260 8b692cd0 2019-10-21 stsp err = unlockerr;
8261 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8262 10604dce 2021-09-24 thomas
8263 10604dce 2021-09-24 thomas return err;
8264 10604dce 2021-09-24 thomas }
8265 10604dce 2021-09-24 thomas
8266 10604dce 2021-09-24 thomas const struct got_error *
8267 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8268 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8269 10604dce 2021-09-24 thomas {
8270 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8271 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8272 10604dce 2021-09-24 thomas
8273 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8274 10604dce 2021-09-24 thomas if (err)
8275 10604dce 2021-09-24 thomas goto done;
8276 8b692cd0 2019-10-21 stsp
8277 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8278 10604dce 2021-09-24 thomas
8279 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8280 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8281 10604dce 2021-09-24 thomas err = sync_err;
8282 10604dce 2021-09-24 thomas done:
8283 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8284 10604dce 2021-09-24 thomas free(fileindex_path);
8285 8b692cd0 2019-10-21 stsp return err;
8286 2822a352 2019-10-15 stsp }
8287 2822a352 2019-10-15 stsp
8288 10604dce 2021-09-24 thomas static const struct got_error *
8289 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8290 10604dce 2021-09-24 thomas {
8291 10604dce 2021-09-24 thomas const struct got_error *err;
8292 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8293 10604dce 2021-09-24 thomas
8294 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8295 10604dce 2021-09-24 thomas if (err)
8296 10604dce 2021-09-24 thomas goto done;
8297 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8298 10604dce 2021-09-24 thomas if (err)
8299 10604dce 2021-09-24 thomas goto done;
8300 10604dce 2021-09-24 thomas
8301 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8302 10604dce 2021-09-24 thomas if (err)
8303 10604dce 2021-09-24 thomas goto done;
8304 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8305 10604dce 2021-09-24 thomas if (err)
8306 10604dce 2021-09-24 thomas goto done;
8307 10604dce 2021-09-24 thomas
8308 10604dce 2021-09-24 thomas done:
8309 10604dce 2021-09-24 thomas free(branch_refname);
8310 10604dce 2021-09-24 thomas free(commit_refname);
8311 10604dce 2021-09-24 thomas return err;
8312 10604dce 2021-09-24 thomas }
8313 10604dce 2021-09-24 thomas
8314 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8315 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8316 10604dce 2021-09-24 thomas const char *branch_name;
8317 10604dce 2021-09-24 thomas };
8318 10604dce 2021-09-24 thomas
8319 10604dce 2021-09-24 thomas static const struct got_error *
8320 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8321 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8322 10604dce 2021-09-24 thomas {
8323 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8324 10604dce 2021-09-24 thomas
8325 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8326 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8327 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8328 10604dce 2021-09-24 thomas
8329 10604dce 2021-09-24 thomas return NULL;
8330 10604dce 2021-09-24 thomas }
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_branch(struct got_worktree *worktree,
8335 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8336 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8337 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8338 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8339 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8340 10604dce 2021-09-24 thomas {
8341 10604dce 2021-09-24 thomas const struct got_error *err;
8342 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8343 10604dce 2021-09-24 thomas
8344 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8345 10604dce 2021-09-24 thomas if (err)
8346 10604dce 2021-09-24 thomas goto done;
8347 10604dce 2021-09-24 thomas
8348 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8349 10604dce 2021-09-24 thomas worktree);
8350 10604dce 2021-09-24 thomas if (err)
8351 10604dce 2021-09-24 thomas goto done;
8352 10604dce 2021-09-24 thomas
8353 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8354 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8355 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8356 10604dce 2021-09-24 thomas done:
8357 10604dce 2021-09-24 thomas free(fileindex_path);
8358 10604dce 2021-09-24 thomas return err;
8359 10604dce 2021-09-24 thomas }
8360 10604dce 2021-09-24 thomas
8361 10604dce 2021-09-24 thomas const struct got_error *
8362 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8363 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8364 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8365 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8366 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8367 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8368 ae1e948a 2021-09-28 thomas
8369 10604dce 2021-09-24 thomas {
8370 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8371 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8372 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8373 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8374 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8375 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8376 10604dce 2021-09-24 thomas int have_staged_files = 0;
8377 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8378 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8379 10604dce 2021-09-24 thomas
8380 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8381 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8382 10604dce 2021-09-24 thomas
8383 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8384 10604dce 2021-09-24 thomas
8385 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8386 10604dce 2021-09-24 thomas if (err)
8387 10604dce 2021-09-24 thomas goto done;
8388 10604dce 2021-09-24 thomas
8389 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8390 10604dce 2021-09-24 thomas if (err)
8391 10604dce 2021-09-24 thomas goto done;
8392 10604dce 2021-09-24 thomas
8393 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8394 10604dce 2021-09-24 thomas if (err)
8395 10604dce 2021-09-24 thomas goto done;
8396 10604dce 2021-09-24 thomas
8397 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8398 10604dce 2021-09-24 thomas &have_staged_files);
8399 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8400 10604dce 2021-09-24 thomas goto done;
8401 10604dce 2021-09-24 thomas if (have_staged_files) {
8402 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8403 10604dce 2021-09-24 thomas goto done;
8404 10604dce 2021-09-24 thomas }
8405 10604dce 2021-09-24 thomas
8406 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8407 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8408 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8409 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8410 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8411 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8412 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8413 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8414 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8415 10604dce 2021-09-24 thomas if (err)
8416 10604dce 2021-09-24 thomas goto done;
8417 10604dce 2021-09-24 thomas
8418 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8419 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8420 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8421 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8422 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8423 10604dce 2021-09-24 thomas if (err)
8424 10604dce 2021-09-24 thomas goto done;
8425 10604dce 2021-09-24 thomas
8426 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8427 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8428 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8429 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8430 10604dce 2021-09-24 thomas err = sync_err;
8431 10604dce 2021-09-24 thomas done:
8432 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8433 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8434 21c2d8be 2023-01-10 thomas
8435 10604dce 2021-09-24 thomas free_commitable(ct);
8436 10604dce 2021-09-24 thomas }
8437 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8438 10604dce 2021-09-24 thomas free(fileindex_path);
8439 10604dce 2021-09-24 thomas return err;
8440 10604dce 2021-09-24 thomas }
8441 10604dce 2021-09-24 thomas
8442 10604dce 2021-09-24 thomas const struct got_error *
8443 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8444 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8445 10604dce 2021-09-24 thomas {
8446 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8447 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8448 10604dce 2021-09-24 thomas
8449 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8450 10604dce 2021-09-24 thomas if (err)
8451 10604dce 2021-09-24 thomas goto done;
8452 10604dce 2021-09-24 thomas
8453 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8454 10604dce 2021-09-24 thomas if (err)
8455 10604dce 2021-09-24 thomas goto done;
8456 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8457 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8458 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8459 10604dce 2021-09-24 thomas err = sync_err;
8460 10604dce 2021-09-24 thomas done:
8461 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8462 10604dce 2021-09-24 thomas free(fileindex_path);
8463 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8464 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8465 10604dce 2021-09-24 thomas err = unlockerr;
8466 10604dce 2021-09-24 thomas return err;
8467 10604dce 2021-09-24 thomas }
8468 10604dce 2021-09-24 thomas
8469 10604dce 2021-09-24 thomas const struct got_error *
8470 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8471 10604dce 2021-09-24 thomas struct got_repository *repo)
8472 10604dce 2021-09-24 thomas {
8473 10604dce 2021-09-24 thomas const struct got_error *err;
8474 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8475 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8476 10604dce 2021-09-24 thomas
8477 10604dce 2021-09-24 thomas *in_progress = 0;
8478 10604dce 2021-09-24 thomas
8479 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8480 10604dce 2021-09-24 thomas if (err)
8481 10604dce 2021-09-24 thomas return err;
8482 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8483 dfe86d1f 2021-09-24 thomas free(branch_refname);
8484 10604dce 2021-09-24 thomas if (err) {
8485 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8486 10604dce 2021-09-24 thomas return err;
8487 10604dce 2021-09-24 thomas } else
8488 10604dce 2021-09-24 thomas *in_progress = 1;
8489 10604dce 2021-09-24 thomas
8490 10604dce 2021-09-24 thomas return NULL;
8491 10604dce 2021-09-24 thomas }
8492 10604dce 2021-09-24 thomas
8493 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8494 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8495 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8496 10604dce 2021-09-24 thomas {
8497 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8498 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8499 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8500 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8501 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8502 10604dce 2021-09-24 thomas
8503 10604dce 2021-09-24 thomas *fileindex = NULL;
8504 10604dce 2021-09-24 thomas
8505 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8506 10604dce 2021-09-24 thomas if (err)
8507 10604dce 2021-09-24 thomas return err;
8508 10604dce 2021-09-24 thomas
8509 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8510 10604dce 2021-09-24 thomas if (err)
8511 10604dce 2021-09-24 thomas goto done;
8512 10604dce 2021-09-24 thomas
8513 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8514 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8515 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8516 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8517 10604dce 2021-09-24 thomas &ok_arg);
8518 10604dce 2021-09-24 thomas if (err)
8519 10604dce 2021-09-24 thomas goto done;
8520 10604dce 2021-09-24 thomas
8521 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8522 10604dce 2021-09-24 thomas 0);
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 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8527 10604dce 2021-09-24 thomas if (err)
8528 10604dce 2021-09-24 thomas goto done;
8529 10604dce 2021-09-24 thomas
8530 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8531 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8532 10604dce 2021-09-24 thomas goto done;
8533 10604dce 2021-09-24 thomas }
8534 2b72f32d 2023-06-22 thomas
8535 2b72f32d 2023-06-22 thomas done:
8536 2b72f32d 2023-06-22 thomas free(fileindex_path);
8537 2b72f32d 2023-06-22 thomas if (wt_branch)
8538 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8539 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8540 2b72f32d 2023-06-22 thomas if (err) {
8541 2b72f32d 2023-06-22 thomas if (*fileindex) {
8542 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8543 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8544 2b72f32d 2023-06-22 thomas }
8545 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8546 2b72f32d 2023-06-22 thomas }
8547 2b72f32d 2023-06-22 thomas return err;
8548 2b72f32d 2023-06-22 thomas }
8549 10604dce 2021-09-24 thomas
8550 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8551 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8552 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8553 2b72f32d 2023-06-22 thomas {
8554 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8555 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8556 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8557 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8558 2b72f32d 2023-06-22 thomas
8559 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8560 2b72f32d 2023-06-22 thomas if (err)
8561 2b72f32d 2023-06-22 thomas return err;
8562 2b72f32d 2023-06-22 thomas
8563 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8564 2b72f32d 2023-06-22 thomas if (err)
8565 2b72f32d 2023-06-22 thomas return err;
8566 2b72f32d 2023-06-22 thomas
8567 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8568 10604dce 2021-09-24 thomas if (err)
8569 10604dce 2021-09-24 thomas goto done;
8570 10604dce 2021-09-24 thomas
8571 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8572 10604dce 2021-09-24 thomas if (err)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8575 10604dce 2021-09-24 thomas if (err)
8576 10604dce 2021-09-24 thomas goto done;
8577 10604dce 2021-09-24 thomas
8578 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8579 10604dce 2021-09-24 thomas if (err)
8580 10604dce 2021-09-24 thomas goto done;
8581 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8582 10604dce 2021-09-24 thomas if (err)
8583 10604dce 2021-09-24 thomas goto done;
8584 10604dce 2021-09-24 thomas
8585 10604dce 2021-09-24 thomas done:
8586 10604dce 2021-09-24 thomas free(branch_refname);
8587 10604dce 2021-09-24 thomas free(commit_refname);
8588 10604dce 2021-09-24 thomas if (branch_ref)
8589 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8590 10604dce 2021-09-24 thomas if (commit_ref)
8591 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8592 2b72f32d 2023-06-22 thomas free(branch_tip);
8593 10604dce 2021-09-24 thomas return err;
8594 10604dce 2021-09-24 thomas }
8595 10604dce 2021-09-24 thomas
8596 10604dce 2021-09-24 thomas const struct got_error *
8597 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8598 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8599 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8600 10604dce 2021-09-24 thomas {
8601 10604dce 2021-09-24 thomas const struct got_error *err;
8602 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8603 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8604 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8605 10604dce 2021-09-24 thomas int have_staged_files = 0;
8606 10604dce 2021-09-24 thomas
8607 10604dce 2021-09-24 thomas *branch_name = NULL;
8608 10604dce 2021-09-24 thomas *branch_tip = NULL;
8609 10604dce 2021-09-24 thomas *fileindex = NULL;
8610 10604dce 2021-09-24 thomas
8611 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8612 10604dce 2021-09-24 thomas if (err)
8613 10604dce 2021-09-24 thomas return err;
8614 10604dce 2021-09-24 thomas
8615 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8616 10604dce 2021-09-24 thomas if (err)
8617 10604dce 2021-09-24 thomas goto done;
8618 10604dce 2021-09-24 thomas
8619 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8620 10604dce 2021-09-24 thomas &have_staged_files);
8621 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8622 10604dce 2021-09-24 thomas goto done;
8623 10604dce 2021-09-24 thomas if (have_staged_files) {
8624 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8625 10604dce 2021-09-24 thomas goto done;
8626 10604dce 2021-09-24 thomas }
8627 10604dce 2021-09-24 thomas
8628 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8629 10604dce 2021-09-24 thomas if (err)
8630 10604dce 2021-09-24 thomas goto done;
8631 10604dce 2021-09-24 thomas
8632 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8633 10604dce 2021-09-24 thomas if (err)
8634 10604dce 2021-09-24 thomas goto done;
8635 10604dce 2021-09-24 thomas
8636 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8637 10604dce 2021-09-24 thomas if (err)
8638 10604dce 2021-09-24 thomas goto done;
8639 10604dce 2021-09-24 thomas
8640 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8641 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8642 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8643 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8644 10604dce 2021-09-24 thomas goto done;
8645 10604dce 2021-09-24 thomas }
8646 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8647 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8648 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8649 10604dce 2021-09-24 thomas goto done;
8650 10604dce 2021-09-24 thomas }
8651 10604dce 2021-09-24 thomas
8652 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8653 10604dce 2021-09-24 thomas if (err)
8654 10604dce 2021-09-24 thomas goto done;
8655 10604dce 2021-09-24 thomas
8656 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8657 10604dce 2021-09-24 thomas if (err)
8658 10604dce 2021-09-24 thomas goto done;
8659 10604dce 2021-09-24 thomas done:
8660 10604dce 2021-09-24 thomas free(commit_refname);
8661 10604dce 2021-09-24 thomas free(branch_refname);
8662 10604dce 2021-09-24 thomas free(fileindex_path);
8663 10604dce 2021-09-24 thomas if (commit_ref)
8664 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8665 10604dce 2021-09-24 thomas if (branch_ref)
8666 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8667 10604dce 2021-09-24 thomas if (err) {
8668 10604dce 2021-09-24 thomas if (*branch_name) {
8669 10604dce 2021-09-24 thomas free(*branch_name);
8670 10604dce 2021-09-24 thomas *branch_name = NULL;
8671 10604dce 2021-09-24 thomas }
8672 10604dce 2021-09-24 thomas free(*branch_tip);
8673 10604dce 2021-09-24 thomas *branch_tip = NULL;
8674 10604dce 2021-09-24 thomas if (*fileindex) {
8675 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8676 10604dce 2021-09-24 thomas *fileindex = NULL;
8677 10604dce 2021-09-24 thomas }
8678 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8679 10604dce 2021-09-24 thomas }
8680 10604dce 2021-09-24 thomas return err;
8681 10604dce 2021-09-24 thomas }
8682 10604dce 2021-09-24 thomas
8683 10604dce 2021-09-24 thomas const struct got_error *
8684 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8685 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8686 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8687 10604dce 2021-09-24 thomas {
8688 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8689 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8690 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8691 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8692 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8693 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8694 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8695 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8696 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8697 8641a332 2023-04-14 thomas
8698 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8699 8641a332 2023-04-14 thomas
8700 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8701 8641a332 2023-04-14 thomas if (err)
8702 8641a332 2023-04-14 thomas goto done;
8703 8641a332 2023-04-14 thomas
8704 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8705 8641a332 2023-04-14 thomas if (err)
8706 8641a332 2023-04-14 thomas goto done;
8707 8641a332 2023-04-14 thomas
8708 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8709 8641a332 2023-04-14 thomas if (err)
8710 8641a332 2023-04-14 thomas goto done;
8711 8641a332 2023-04-14 thomas
8712 8641a332 2023-04-14 thomas /*
8713 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8714 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8715 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8716 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8717 8641a332 2023-04-14 thomas */
8718 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8719 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8720 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8721 8641a332 2023-04-14 thomas if (err)
8722 8641a332 2023-04-14 thomas goto done;
8723 10604dce 2021-09-24 thomas
8724 8641a332 2023-04-14 thomas
8725 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8726 945f9229 2022-04-16 thomas worktree->base_commit_id);
8727 945f9229 2022-04-16 thomas if (err)
8728 945f9229 2022-04-16 thomas goto done;
8729 945f9229 2022-04-16 thomas
8730 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8731 945f9229 2022-04-16 thomas worktree->path_prefix);
8732 10604dce 2021-09-24 thomas if (err)
8733 10604dce 2021-09-24 thomas goto done;
8734 10604dce 2021-09-24 thomas
8735 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8736 10604dce 2021-09-24 thomas if (err)
8737 10604dce 2021-09-24 thomas goto done;
8738 10604dce 2021-09-24 thomas
8739 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8740 10604dce 2021-09-24 thomas if (err)
8741 10604dce 2021-09-24 thomas goto done;
8742 10604dce 2021-09-24 thomas
8743 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8744 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8745 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8746 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8747 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8748 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8749 10604dce 2021-09-24 thomas rfa.repo = repo;
8750 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8751 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8752 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8753 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8754 10604dce 2021-09-24 thomas if (err)
8755 10604dce 2021-09-24 thomas goto sync;
8756 10604dce 2021-09-24 thomas
8757 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8758 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8759 10604dce 2021-09-24 thomas sync:
8760 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8761 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8762 10604dce 2021-09-24 thomas err = sync_err;
8763 10604dce 2021-09-24 thomas done:
8764 10604dce 2021-09-24 thomas free(tree_id);
8765 8641a332 2023-04-14 thomas free(merged_commit_id);
8766 945f9229 2022-04-16 thomas if (commit)
8767 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8768 10604dce 2021-09-24 thomas if (fileindex)
8769 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8770 10604dce 2021-09-24 thomas free(fileindex_path);
8771 8641a332 2023-04-14 thomas if (commit_ref)
8772 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8773 8641a332 2023-04-14 thomas free(commit_ref_name);
8774 10604dce 2021-09-24 thomas
8775 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8776 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8777 10604dce 2021-09-24 thomas err = unlockerr;
8778 10604dce 2021-09-24 thomas return err;
8779 10604dce 2021-09-24 thomas }
8780 10604dce 2021-09-24 thomas
8781 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8782 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8783 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8784 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8785 2db2652d 2019-08-07 stsp struct got_repository *repo;
8786 2db2652d 2019-08-07 stsp int have_changes;
8787 2db2652d 2019-08-07 stsp };
8788 2db2652d 2019-08-07 stsp
8789 ef20f542 2022-06-26 thomas static const struct got_error *
8790 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8791 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8792 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8793 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8794 735ef5ac 2019-08-03 stsp {
8795 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8796 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8797 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8798 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8799 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8800 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8801 8b13ce36 2019-08-08 stsp
8802 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8803 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8804 8b13ce36 2019-08-08 stsp return NULL;
8805 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8806 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8807 735ef5ac 2019-08-03 stsp
8808 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8809 735ef5ac 2019-08-03 stsp if (ie == NULL)
8810 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8811 735ef5ac 2019-08-03 stsp
8812 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8813 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8814 735ef5ac 2019-08-03 stsp relpath) == -1)
8815 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8816 735ef5ac 2019-08-03 stsp
8817 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8818 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8819 43010591 2023-02-17 thomas &base_commit_id, ie);
8820 735ef5ac 2019-08-03 stsp }
8821 735ef5ac 2019-08-03 stsp
8822 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8823 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8824 3aa5969e 2019-08-06 stsp goto done;
8825 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8826 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8827 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8828 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8829 735ef5ac 2019-08-03 stsp goto done;
8830 3aa5969e 2019-08-06 stsp }
8831 735ef5ac 2019-08-03 stsp
8832 2db2652d 2019-08-07 stsp a->have_changes = 1;
8833 2db2652d 2019-08-07 stsp
8834 735ef5ac 2019-08-03 stsp p = in_repo_path;
8835 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8836 735ef5ac 2019-08-03 stsp p++;
8837 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8838 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8839 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8840 735ef5ac 2019-08-03 stsp done:
8841 735ef5ac 2019-08-03 stsp free(in_repo_path);
8842 dc424a06 2019-08-07 stsp return err;
8843 dc424a06 2019-08-07 stsp }
8844 dc424a06 2019-08-07 stsp
8845 2db2652d 2019-08-07 stsp struct stage_path_arg {
8846 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8847 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8848 2db2652d 2019-08-07 stsp struct got_repository *repo;
8849 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8850 2db2652d 2019-08-07 stsp void *status_arg;
8851 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8852 2db2652d 2019-08-07 stsp void *patch_arg;
8853 7b5dc508 2019-10-28 stsp int staged_something;
8854 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8855 2db2652d 2019-08-07 stsp };
8856 2db2652d 2019-08-07 stsp
8857 2db2652d 2019-08-07 stsp static const struct got_error *
8858 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8859 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8860 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8861 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8862 0cb83759 2019-08-03 stsp {
8863 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8864 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8865 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8866 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8867 0cb83759 2019-08-03 stsp uint32_t stage;
8868 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8869 0aeb8099 2020-07-23 stsp struct stat sb;
8870 8b13ce36 2019-08-08 stsp
8871 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8872 8b13ce36 2019-08-08 stsp return NULL;
8873 0cb83759 2019-08-03 stsp
8874 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8875 d3e7c587 2019-08-03 stsp if (ie == NULL)
8876 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8877 0cb83759 2019-08-03 stsp
8878 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8879 2db2652d 2019-08-07 stsp relpath)== -1)
8880 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8881 0cb83759 2019-08-03 stsp
8882 0cb83759 2019-08-03 stsp switch (status) {
8883 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8884 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8885 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8886 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8887 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8888 0aeb8099 2020-07-23 stsp break;
8889 0aeb8099 2020-07-23 stsp }
8890 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8891 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8892 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8893 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8894 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8895 dc424a06 2019-08-07 stsp if (err)
8896 dc424a06 2019-08-07 stsp break;
8897 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8898 dc424a06 2019-08-07 stsp break;
8899 dc424a06 2019-08-07 stsp } else {
8900 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8901 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8902 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8903 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8904 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8905 dc424a06 2019-08-07 stsp break;
8906 dc424a06 2019-08-07 stsp }
8907 dc424a06 2019-08-07 stsp }
8908 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8909 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8910 0cb83759 2019-08-03 stsp if (err)
8911 dc424a06 2019-08-07 stsp break;
8912 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8913 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8914 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8915 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8916 0cb83759 2019-08-03 stsp else
8917 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8918 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8919 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8920 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8921 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8922 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8923 35213c7c 2020-07-23 stsp ssize_t target_len;
8924 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8925 35213c7c 2020-07-23 stsp sizeof(target_path));
8926 35213c7c 2020-07-23 stsp if (target_len == -1) {
8927 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8928 35213c7c 2020-07-23 stsp ondisk_path);
8929 35213c7c 2020-07-23 stsp break;
8930 35213c7c 2020-07-23 stsp }
8931 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8932 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8933 35213c7c 2020-07-23 stsp a->worktree->root_path);
8934 35213c7c 2020-07-23 stsp if (err)
8935 35213c7c 2020-07-23 stsp break;
8936 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8937 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8938 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8939 35213c7c 2020-07-23 stsp break;
8940 35213c7c 2020-07-23 stsp }
8941 35213c7c 2020-07-23 stsp }
8942 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8943 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8944 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8945 35213c7c 2020-07-23 stsp else
8946 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8947 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8948 0aeb8099 2020-07-23 stsp } else {
8949 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8950 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8951 0aeb8099 2020-07-23 stsp }
8952 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8953 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8954 dc424a06 2019-08-07 stsp break;
8955 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8956 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8957 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8958 f289c82c 2022-06-13 thomas if (err)
8959 f289c82c 2022-06-13 thomas break;
8960 f289c82c 2022-06-13 thomas /*
8961 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8962 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8963 f289c82c 2022-06-13 thomas */
8964 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8965 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8966 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8967 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8968 f289c82c 2022-06-13 thomas }
8969 0cb83759 2019-08-03 stsp break;
8970 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8971 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8972 d3e7c587 2019-08-03 stsp break;
8973 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8974 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8975 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8976 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8977 dc424a06 2019-08-07 stsp if (err)
8978 dc424a06 2019-08-07 stsp break;
8979 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8980 88f33a19 2019-08-08 stsp break;
8981 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8982 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8983 dc424a06 2019-08-07 stsp break;
8984 88f33a19 2019-08-08 stsp }
8985 dc424a06 2019-08-07 stsp }
8986 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8987 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8988 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8989 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8990 dc424a06 2019-08-07 stsp break;
8991 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8992 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8993 12463d8b 2019-12-13 stsp de_name);
8994 0cb83759 2019-08-03 stsp break;
8995 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8996 d3e7c587 2019-08-03 stsp break;
8997 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8998 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8999 ebf48fd5 2019-08-03 stsp break;
9000 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9001 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9002 2a06fe5f 2019-08-24 stsp break;
9003 0cb83759 2019-08-03 stsp default:
9004 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9005 537ac44b 2019-08-03 stsp break;
9006 0cb83759 2019-08-03 stsp }
9007 dc424a06 2019-08-07 stsp
9008 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9009 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9010 dc424a06 2019-08-07 stsp free(path_content);
9011 2db2652d 2019-08-07 stsp free(ondisk_path);
9012 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9013 0ebf8283 2019-07-24 stsp return err;
9014 0ebf8283 2019-07-24 stsp }
9015 0cb83759 2019-08-03 stsp
9016 0cb83759 2019-08-03 stsp const struct got_error *
9017 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9018 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9019 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9020 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9021 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9022 0cb83759 2019-08-03 stsp {
9023 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9024 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9025 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9026 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9027 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9028 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9029 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9030 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9031 0cb83759 2019-08-03 stsp
9032 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9033 0cb83759 2019-08-03 stsp if (err)
9034 0cb83759 2019-08-03 stsp return err;
9035 0cb83759 2019-08-03 stsp
9036 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9037 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9038 735ef5ac 2019-08-03 stsp if (err)
9039 735ef5ac 2019-08-03 stsp goto done;
9040 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9041 735ef5ac 2019-08-03 stsp if (err)
9042 735ef5ac 2019-08-03 stsp goto done;
9043 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9044 0cb83759 2019-08-03 stsp if (err)
9045 0cb83759 2019-08-03 stsp goto done;
9046 0cb83759 2019-08-03 stsp
9047 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9048 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9049 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9050 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9051 2db2652d 2019-08-07 stsp oka.repo = repo;
9052 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9053 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9054 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9055 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9056 735ef5ac 2019-08-03 stsp if (err)
9057 735ef5ac 2019-08-03 stsp goto done;
9058 735ef5ac 2019-08-03 stsp }
9059 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9060 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9061 2db2652d 2019-08-07 stsp goto done;
9062 2db2652d 2019-08-07 stsp }
9063 735ef5ac 2019-08-03 stsp
9064 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9065 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9066 2db2652d 2019-08-07 stsp spa.repo = repo;
9067 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9068 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9069 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9070 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9071 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9072 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9073 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9074 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9075 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9076 42005733 2019-08-03 stsp if (err)
9077 2db2652d 2019-08-07 stsp goto done;
9078 7b5dc508 2019-10-28 stsp }
9079 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9080 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9081 7b5dc508 2019-10-28 stsp goto done;
9082 0cb83759 2019-08-03 stsp }
9083 0cb83759 2019-08-03 stsp
9084 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9085 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9086 0cb83759 2019-08-03 stsp err = sync_err;
9087 0cb83759 2019-08-03 stsp done:
9088 735ef5ac 2019-08-03 stsp if (head_ref)
9089 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9090 735ef5ac 2019-08-03 stsp free(head_commit_id);
9091 0cb83759 2019-08-03 stsp free(fileindex_path);
9092 0cb83759 2019-08-03 stsp if (fileindex)
9093 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9094 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9095 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9096 0cb83759 2019-08-03 stsp err = unlockerr;
9097 0cb83759 2019-08-03 stsp return err;
9098 0cb83759 2019-08-03 stsp }
9099 ad493afc 2019-08-03 stsp
9100 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9101 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9102 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9103 ad493afc 2019-08-03 stsp struct got_repository *repo;
9104 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9105 ad493afc 2019-08-03 stsp void *progress_arg;
9106 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9107 2e1f37b0 2019-08-08 stsp void *patch_arg;
9108 ad493afc 2019-08-03 stsp };
9109 2e1f37b0 2019-08-08 stsp
9110 2e1f37b0 2019-08-08 stsp static const struct got_error *
9111 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9112 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9113 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9114 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9115 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9116 2e1f37b0 2019-08-08 stsp {
9117 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9118 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9119 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9120 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9121 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9122 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9123 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9124 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9125 2e1f37b0 2019-08-08 stsp
9126 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9127 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9128 2e1f37b0 2019-08-08 stsp
9129 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9130 2e1f37b0 2019-08-08 stsp if (err)
9131 2e1f37b0 2019-08-08 stsp return err;
9132 f4ae6ddb 2022-07-01 thomas
9133 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9134 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9135 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9136 f4ae6ddb 2022-07-01 thomas goto done;
9137 f4ae6ddb 2022-07-01 thomas }
9138 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9139 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9140 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9141 f4ae6ddb 2022-07-01 thomas goto done;
9142 f4ae6ddb 2022-07-01 thomas }
9143 f4ae6ddb 2022-07-01 thomas
9144 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9145 2e1f37b0 2019-08-08 stsp if (err)
9146 2e1f37b0 2019-08-08 stsp goto done;
9147 2e1f37b0 2019-08-08 stsp
9148 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9149 2e1f37b0 2019-08-08 stsp if (err)
9150 2e1f37b0 2019-08-08 stsp goto done;
9151 2e1f37b0 2019-08-08 stsp
9152 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9153 2e1f37b0 2019-08-08 stsp if (err)
9154 2e1f37b0 2019-08-08 stsp goto done;
9155 2e1f37b0 2019-08-08 stsp
9156 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9157 f4ae6ddb 2022-07-01 thomas fd2);
9158 2e1f37b0 2019-08-08 stsp if (err)
9159 2e1f37b0 2019-08-08 stsp goto done;
9160 2e1f37b0 2019-08-08 stsp
9161 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9162 2e1f37b0 2019-08-08 stsp if (err)
9163 2e1f37b0 2019-08-08 stsp goto done;
9164 ad493afc 2019-08-03 stsp
9165 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9166 2e1f37b0 2019-08-08 stsp if (err)
9167 2e1f37b0 2019-08-08 stsp goto done;
9168 2e1f37b0 2019-08-08 stsp
9169 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9170 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9171 2e1f37b0 2019-08-08 stsp if (err)
9172 2e1f37b0 2019-08-08 stsp goto done;
9173 2e1f37b0 2019-08-08 stsp
9174 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9175 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9176 2e1f37b0 2019-08-08 stsp if (err)
9177 2e1f37b0 2019-08-08 stsp goto done;
9178 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9179 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9180 2e1f37b0 2019-08-08 stsp if (err)
9181 2e1f37b0 2019-08-08 stsp goto done;
9182 2e1f37b0 2019-08-08 stsp
9183 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9184 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9185 2e1f37b0 2019-08-08 stsp goto done;
9186 2e1f37b0 2019-08-08 stsp }
9187 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9188 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9189 2e1f37b0 2019-08-08 stsp goto done;
9190 2e1f37b0 2019-08-08 stsp }
9191 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9192 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9193 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9194 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9195 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9196 fe621944 2020-11-10 stsp nchanges++;
9197 fe621944 2020-11-10 stsp }
9198 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9199 2e1f37b0 2019-08-08 stsp int choice;
9200 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9201 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9202 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9203 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9204 2e1f37b0 2019-08-08 stsp if (err)
9205 2e1f37b0 2019-08-08 stsp goto done;
9206 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9207 2e1f37b0 2019-08-08 stsp have_content = 1;
9208 19e4b907 2019-08-08 stsp else
9209 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9210 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9211 2e1f37b0 2019-08-08 stsp break;
9212 2e1f37b0 2019-08-08 stsp }
9213 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9214 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9215 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9216 2e1f37b0 2019-08-08 stsp done:
9217 2e1f37b0 2019-08-08 stsp free(label1);
9218 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9219 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9220 2e1f37b0 2019-08-08 stsp if (blob)
9221 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9222 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9223 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9224 2e1f37b0 2019-08-08 stsp if (staged_blob)
9225 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9226 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9227 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9228 fe621944 2020-11-10 stsp err = free_err;
9229 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9230 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9231 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9232 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9233 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9234 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9235 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9236 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9237 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9238 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9239 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9240 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9241 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9242 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9243 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9244 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9245 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9246 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9247 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9248 2e1f37b0 2019-08-08 stsp }
9249 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9250 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9251 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9252 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9253 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9254 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9255 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9256 2e1f37b0 2019-08-08 stsp }
9257 2e1f37b0 2019-08-08 stsp free(path1);
9258 2e1f37b0 2019-08-08 stsp free(path2);
9259 fda8017d 2020-07-23 stsp return err;
9260 fda8017d 2020-07-23 stsp }
9261 fda8017d 2020-07-23 stsp
9262 fda8017d 2020-07-23 stsp static const struct got_error *
9263 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9264 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9265 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9266 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9267 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9268 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9269 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9270 fda8017d 2020-07-23 stsp {
9271 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9272 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9273 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9274 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9275 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9276 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9277 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9278 fda8017d 2020-07-23 stsp struct stat sb;
9279 fda8017d 2020-07-23 stsp
9280 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9281 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9282 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9283 fda8017d 2020-07-23 stsp if (err)
9284 fda8017d 2020-07-23 stsp return err;
9285 fda8017d 2020-07-23 stsp
9286 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9287 fda8017d 2020-07-23 stsp return NULL;
9288 fda8017d 2020-07-23 stsp
9289 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9290 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9291 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9292 fda8017d 2020-07-23 stsp if (err)
9293 fda8017d 2020-07-23 stsp goto done;
9294 fda8017d 2020-07-23 stsp }
9295 fda8017d 2020-07-23 stsp
9296 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9297 fda8017d 2020-07-23 stsp if (f == NULL) {
9298 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9299 fda8017d 2020-07-23 stsp path_unstaged_content);
9300 fda8017d 2020-07-23 stsp goto done;
9301 fda8017d 2020-07-23 stsp }
9302 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9303 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9304 fda8017d 2020-07-23 stsp goto done;
9305 fda8017d 2020-07-23 stsp }
9306 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9307 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9308 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9309 fda8017d 2020-07-23 stsp size_t r;
9310 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9311 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9312 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9313 fda8017d 2020-07-23 stsp goto done;
9314 fda8017d 2020-07-23 stsp }
9315 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9316 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9317 fda8017d 2020-07-23 stsp goto done;
9318 fda8017d 2020-07-23 stsp }
9319 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9320 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9321 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9322 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9323 fda8017d 2020-07-23 stsp progress_arg);
9324 fda8017d 2020-07-23 stsp } else {
9325 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9326 67a66647 2021-05-31 stsp
9327 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9328 67a66647 2021-05-31 stsp if (err)
9329 67a66647 2021-05-31 stsp return err;
9330 67a66647 2021-05-31 stsp
9331 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9332 67a66647 2021-05-31 stsp parent) == -1) {
9333 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9334 67a66647 2021-05-31 stsp base_path = NULL;
9335 67a66647 2021-05-31 stsp goto done;
9336 67a66647 2021-05-31 stsp }
9337 67a66647 2021-05-31 stsp
9338 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9339 fc2a50f2 2022-11-01 thomas base_path, "");
9340 67a66647 2021-05-31 stsp if (err)
9341 67a66647 2021-05-31 stsp goto done;
9342 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9343 67a66647 2021-05-31 stsp blob_base);
9344 67a66647 2021-05-31 stsp if (err)
9345 67a66647 2021-05-31 stsp goto done;
9346 67a66647 2021-05-31 stsp
9347 b6b86fd1 2022-08-30 thomas /*
9348 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9349 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9350 eec2f5a9 2021-06-03 stsp */
9351 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9352 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9353 eec2f5a9 2021-06-03 stsp ondisk_path);
9354 eec2f5a9 2021-06-03 stsp if (err)
9355 eec2f5a9 2021-06-03 stsp goto done;
9356 eec2f5a9 2021-06-03 stsp } else {
9357 eec2f5a9 2021-06-03 stsp int fd;
9358 06340621 2021-12-31 thomas fd = open(ondisk_path,
9359 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9360 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9361 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9362 eec2f5a9 2021-06-03 stsp goto done;
9363 eec2f5a9 2021-06-03 stsp }
9364 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9365 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9366 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9367 eec2f5a9 2021-06-03 stsp close(fd);
9368 eec2f5a9 2021-06-03 stsp goto done;
9369 eec2f5a9 2021-06-03 stsp }
9370 eec2f5a9 2021-06-03 stsp }
9371 eec2f5a9 2021-06-03 stsp
9372 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9373 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9374 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9375 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9376 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9377 fda8017d 2020-07-23 stsp }
9378 fda8017d 2020-07-23 stsp if (err)
9379 fda8017d 2020-07-23 stsp goto done;
9380 fda8017d 2020-07-23 stsp
9381 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9382 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9383 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9384 2ac8aa02 2020-11-09 stsp } else {
9385 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9386 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9387 2ac8aa02 2020-11-09 stsp }
9388 fda8017d 2020-07-23 stsp done:
9389 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9390 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9391 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9392 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9393 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9394 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9395 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9396 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9397 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9398 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9399 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9400 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9401 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9402 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9403 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9404 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9405 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9406 67a66647 2021-05-31 stsp free(blob_base_path);
9407 67a66647 2021-05-31 stsp free(parent);
9408 67a66647 2021-05-31 stsp free(base_path);
9409 2e1f37b0 2019-08-08 stsp return err;
9410 2e1f37b0 2019-08-08 stsp }
9411 2e1f37b0 2019-08-08 stsp
9412 ad493afc 2019-08-03 stsp static const struct got_error *
9413 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9414 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9415 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9416 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9417 ad493afc 2019-08-03 stsp {
9418 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9419 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9420 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9421 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9422 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9423 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9424 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9425 9bc94a15 2019-08-03 stsp struct stat sb;
9426 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9427 ad493afc 2019-08-03 stsp
9428 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9429 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9430 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9431 2e1f37b0 2019-08-08 stsp return NULL;
9432 2e1f37b0 2019-08-08 stsp
9433 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9434 ad493afc 2019-08-03 stsp if (ie == NULL)
9435 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9436 9bc94a15 2019-08-03 stsp
9437 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9438 9bc94a15 2019-08-03 stsp == -1)
9439 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9440 ad493afc 2019-08-03 stsp
9441 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9442 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9443 f69721c3 2019-10-21 stsp if (err)
9444 f69721c3 2019-10-21 stsp goto done;
9445 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9446 f69721c3 2019-10-21 stsp id_str) == -1) {
9447 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9448 f4ae6ddb 2022-07-01 thomas goto done;
9449 f4ae6ddb 2022-07-01 thomas }
9450 f4ae6ddb 2022-07-01 thomas
9451 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9452 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9453 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9454 f4ae6ddb 2022-07-01 thomas goto done;
9455 f4ae6ddb 2022-07-01 thomas }
9456 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9457 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9458 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9459 f69721c3 2019-10-21 stsp goto done;
9460 f69721c3 2019-10-21 stsp }
9461 f69721c3 2019-10-21 stsp
9462 ad493afc 2019-08-03 stsp switch (staged_status) {
9463 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9464 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9465 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9466 ad493afc 2019-08-03 stsp if (err)
9467 ad493afc 2019-08-03 stsp break;
9468 ad493afc 2019-08-03 stsp /* fall through */
9469 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9470 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9471 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9472 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9473 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9474 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9475 2e1f37b0 2019-08-08 stsp if (err)
9476 2e1f37b0 2019-08-08 stsp break;
9477 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9478 2e1f37b0 2019-08-08 stsp break;
9479 2e1f37b0 2019-08-08 stsp } else {
9480 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9481 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9482 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9483 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9484 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9485 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9486 2e1f37b0 2019-08-08 stsp }
9487 2e1f37b0 2019-08-08 stsp }
9488 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9489 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9490 ad493afc 2019-08-03 stsp if (err)
9491 ad493afc 2019-08-03 stsp break;
9492 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9493 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9494 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9495 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9496 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9497 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9498 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9499 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9500 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9501 ea7786be 2020-07-23 stsp break;
9502 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9503 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9504 36bf999c 2020-07-23 stsp char *staged_target;
9505 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9506 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9507 36bf999c 2020-07-23 stsp if (err)
9508 36bf999c 2020-07-23 stsp goto done;
9509 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9510 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9511 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9512 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9513 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9514 36bf999c 2020-07-23 stsp free(staged_target);
9515 dfe9fba0 2020-07-23 stsp } else {
9516 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9517 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9518 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9519 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9520 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9521 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9522 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9523 dfe9fba0 2020-07-23 stsp }
9524 ea7786be 2020-07-23 stsp break;
9525 ea7786be 2020-07-23 stsp default:
9526 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9527 ea7786be 2020-07-23 stsp break;
9528 ea7786be 2020-07-23 stsp }
9529 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9530 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9531 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9532 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9533 2ac8aa02 2020-11-09 stsp }
9534 ad493afc 2019-08-03 stsp break;
9535 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9536 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9537 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9538 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9539 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9540 2e1f37b0 2019-08-08 stsp if (err)
9541 2e1f37b0 2019-08-08 stsp break;
9542 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9543 2e1f37b0 2019-08-08 stsp break;
9544 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9545 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9546 2e1f37b0 2019-08-08 stsp break;
9547 2e1f37b0 2019-08-08 stsp }
9548 2e1f37b0 2019-08-08 stsp }
9549 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9550 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9551 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9552 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9553 9bc94a15 2019-08-03 stsp if (err)
9554 9bc94a15 2019-08-03 stsp break;
9555 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9556 ad493afc 2019-08-03 stsp break;
9557 ad493afc 2019-08-03 stsp }
9558 f69721c3 2019-10-21 stsp done:
9559 ad493afc 2019-08-03 stsp free(ondisk_path);
9560 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9561 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9562 ad493afc 2019-08-03 stsp if (blob_base)
9563 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9564 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9565 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9566 ad493afc 2019-08-03 stsp if (blob_staged)
9567 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9568 f69721c3 2019-10-21 stsp free(id_str);
9569 f69721c3 2019-10-21 stsp free(label_orig);
9570 ad493afc 2019-08-03 stsp return err;
9571 ad493afc 2019-08-03 stsp }
9572 ad493afc 2019-08-03 stsp
9573 ad493afc 2019-08-03 stsp const struct got_error *
9574 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9575 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9576 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9577 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9578 ad493afc 2019-08-03 stsp struct got_repository *repo)
9579 ad493afc 2019-08-03 stsp {
9580 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9581 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9582 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9583 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9584 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9585 ad493afc 2019-08-03 stsp
9586 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9587 ad493afc 2019-08-03 stsp if (err)
9588 ad493afc 2019-08-03 stsp return err;
9589 ad493afc 2019-08-03 stsp
9590 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9591 ad493afc 2019-08-03 stsp if (err)
9592 ad493afc 2019-08-03 stsp goto done;
9593 ad493afc 2019-08-03 stsp
9594 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9595 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9596 ad493afc 2019-08-03 stsp upa.repo = repo;
9597 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9598 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9599 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9600 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9601 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9602 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9603 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9604 ad493afc 2019-08-03 stsp if (err)
9605 ad493afc 2019-08-03 stsp goto done;
9606 ad493afc 2019-08-03 stsp }
9607 ad493afc 2019-08-03 stsp
9608 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9609 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9610 ad493afc 2019-08-03 stsp err = sync_err;
9611 ad493afc 2019-08-03 stsp done:
9612 ad493afc 2019-08-03 stsp free(fileindex_path);
9613 ad493afc 2019-08-03 stsp if (fileindex)
9614 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9615 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9616 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9617 ad493afc 2019-08-03 stsp err = unlockerr;
9618 ad493afc 2019-08-03 stsp return err;
9619 ad493afc 2019-08-03 stsp }
9620 b2118c49 2020-07-28 stsp
9621 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9622 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9623 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9624 b2118c49 2020-07-28 stsp void *info_arg;
9625 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9626 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9627 b2118c49 2020-07-28 stsp void *cancel_arg;
9628 b2118c49 2020-07-28 stsp };
9629 b2118c49 2020-07-28 stsp
9630 b2118c49 2020-07-28 stsp static const struct got_error *
9631 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9632 b2118c49 2020-07-28 stsp {
9633 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9634 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9635 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9636 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9637 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9638 b2118c49 2020-07-28 stsp int stage;
9639 b2118c49 2020-07-28 stsp
9640 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9641 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9642 b2118c49 2020-07-28 stsp
9643 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9644 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9645 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9646 b2118c49 2020-07-28 stsp break;
9647 b2118c49 2020-07-28 stsp }
9648 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9649 b2118c49 2020-07-28 stsp return NULL;
9650 b2118c49 2020-07-28 stsp
9651 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9652 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9653 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9654 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9655 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9656 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9657 43010591 2023-02-17 thomas &staged_blob_id, ie);
9658 b2118c49 2020-07-28 stsp }
9659 b2118c49 2020-07-28 stsp
9660 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9661 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9662 b2118c49 2020-07-28 stsp
9663 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9664 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9665 b2118c49 2020-07-28 stsp }
9666 b2118c49 2020-07-28 stsp
9667 b2118c49 2020-07-28 stsp const struct got_error *
9668 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9669 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9670 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9671 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9672 b2118c49 2020-07-28 stsp
9673 b2118c49 2020-07-28 stsp {
9674 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9675 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9676 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9677 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9678 b2118c49 2020-07-28 stsp
9679 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9680 b2118c49 2020-07-28 stsp if (err)
9681 b2118c49 2020-07-28 stsp return err;
9682 b2118c49 2020-07-28 stsp
9683 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9684 b2118c49 2020-07-28 stsp if (err)
9685 b2118c49 2020-07-28 stsp goto done;
9686 b2118c49 2020-07-28 stsp
9687 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9688 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9689 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9690 b2118c49 2020-07-28 stsp arg.paths = paths;
9691 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9692 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9693 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9694 b2118c49 2020-07-28 stsp &arg);
9695 b2118c49 2020-07-28 stsp done:
9696 b2118c49 2020-07-28 stsp free(fileindex_path);
9697 b2118c49 2020-07-28 stsp if (fileindex)
9698 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9699 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9700 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9701 b2118c49 2020-07-28 stsp err = unlockerr;
9702 b2118c49 2020-07-28 stsp return err;
9703 b2118c49 2020-07-28 stsp }
9704 814624e7 2022-03-22 thomas
9705 814624e7 2022-03-22 thomas static const struct got_error *
9706 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9707 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9708 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9709 814624e7 2022-03-22 thomas {
9710 814624e7 2022-03-22 thomas const struct got_error *err;
9711 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9712 814624e7 2022-03-22 thomas struct stat sb;
9713 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9714 814624e7 2022-03-22 thomas
9715 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9716 814624e7 2022-03-22 thomas if (err)
9717 814624e7 2022-03-22 thomas return err;
9718 814624e7 2022-03-22 thomas
9719 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9720 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9721 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9722 814624e7 2022-03-22 thomas
9723 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9724 814624e7 2022-03-22 thomas if (ie) {
9725 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9726 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9727 12de5570 2022-05-01 thomas repo);
9728 814624e7 2022-03-22 thomas if (err)
9729 814624e7 2022-03-22 thomas goto done;
9730 814624e7 2022-03-22 thomas } else {
9731 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9732 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9733 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9734 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9735 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9736 814624e7 2022-03-22 thomas ondisk_path);
9737 814624e7 2022-03-22 thomas goto done;
9738 814624e7 2022-03-22 thomas }
9739 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9740 814624e7 2022-03-22 thomas }
9741 814624e7 2022-03-22 thomas }
9742 814624e7 2022-03-22 thomas
9743 814624e7 2022-03-22 thomas done:
9744 814624e7 2022-03-22 thomas free(ondisk_path);
9745 814624e7 2022-03-22 thomas return err;
9746 814624e7 2022-03-22 thomas }
9747 814624e7 2022-03-22 thomas
9748 814624e7 2022-03-22 thomas static const struct got_error *
9749 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9750 814624e7 2022-03-22 thomas unsigned char staged_status)
9751 814624e7 2022-03-22 thomas {
9752 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9753 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9754 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9755 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9756 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9757 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9758 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9759 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9760 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 814624e7 2022-03-22 thomas return NULL;
9762 814624e7 2022-03-22 thomas }
9763 814624e7 2022-03-22 thomas
9764 814624e7 2022-03-22 thomas static const struct got_error *
9765 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9766 814624e7 2022-03-22 thomas {
9767 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9768 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9769 814624e7 2022-03-22 thomas return NULL;
9770 814624e7 2022-03-22 thomas }
9771 814624e7 2022-03-22 thomas
9772 814624e7 2022-03-22 thomas static const struct got_error *
9773 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9774 814624e7 2022-03-22 thomas unsigned char staged_status)
9775 814624e7 2022-03-22 thomas {
9776 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9777 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9778 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9779 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9780 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9781 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9782 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9783 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9784 814624e7 2022-03-22 thomas return NULL;
9785 814624e7 2022-03-22 thomas }
9786 814624e7 2022-03-22 thomas
9787 814624e7 2022-03-22 thomas const struct got_error *
9788 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9789 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9790 814624e7 2022-03-22 thomas {
9791 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9792 814624e7 2022-03-22 thomas }
9793 814624e7 2022-03-22 thomas
9794 814624e7 2022-03-22 thomas const struct got_error *
9795 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9796 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9797 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9798 814624e7 2022-03-22 thomas {
9799 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9800 814624e7 2022-03-22 thomas int file_renamed = 0;
9801 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9802 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9803 814624e7 2022-03-22 thomas
9804 814624e7 2022-03-22 thomas *oldpath = NULL;
9805 814624e7 2022-03-22 thomas *newpath = NULL;
9806 814624e7 2022-03-22 thomas
9807 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9808 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9809 814624e7 2022-03-22 thomas if (err)
9810 814624e7 2022-03-22 thomas goto done;
9811 814624e7 2022-03-22 thomas
9812 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9813 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9814 814624e7 2022-03-22 thomas if (err)
9815 814624e7 2022-03-22 thomas goto done;
9816 814624e7 2022-03-22 thomas
9817 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9818 814624e7 2022-03-22 thomas file_renamed = 1;
9819 814624e7 2022-03-22 thomas
9820 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9821 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9822 814624e7 2022-03-22 thomas else if (file_renamed) {
9823 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9824 814624e7 2022-03-22 thomas if (err == NULL)
9825 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9826 814624e7 2022-03-22 thomas } else if (old == NULL)
9827 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9828 814624e7 2022-03-22 thomas else
9829 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9830 814624e7 2022-03-22 thomas
9831 814624e7 2022-03-22 thomas done:
9832 814624e7 2022-03-22 thomas if (err) {
9833 814624e7 2022-03-22 thomas free(*oldpath);
9834 814624e7 2022-03-22 thomas *oldpath = NULL;
9835 814624e7 2022-03-22 thomas free(*newpath);
9836 814624e7 2022-03-22 thomas *newpath = NULL;
9837 814624e7 2022-03-22 thomas }
9838 814624e7 2022-03-22 thomas return err;
9839 814624e7 2022-03-22 thomas }
9840 814624e7 2022-03-22 thomas
9841 814624e7 2022-03-22 thomas const struct got_error *
9842 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9843 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9844 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9845 814624e7 2022-03-22 thomas {
9846 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9847 5e22b737 2022-05-31 thomas
9848 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9849 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9850 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9851 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9852 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9853 5e22b737 2022-05-31 thomas saa.repo = repo;
9854 5e22b737 2022-05-31 thomas
9855 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9856 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9857 814624e7 2022-03-22 thomas }
9858 5e22b737 2022-05-31 thomas
9859 5e22b737 2022-05-31 thomas const struct got_error *
9860 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9861 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9862 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9863 5e22b737 2022-05-31 thomas {
9864 8ec7352a 2023-06-01 thomas const struct got_error *err;
9865 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9866 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9867 5e22b737 2022-05-31 thomas
9868 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9869 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9870 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9871 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9872 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9873 5e22b737 2022-05-31 thomas sda.repo = repo;
9874 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9875 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9876 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9877 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9878 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9879 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9880 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9881 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9882 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9883 8ec7352a 2023-06-01 thomas
9884 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9885 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9886 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9887 8ec7352a 2023-06-01 thomas return err;
9888 5e22b737 2022-05-31 thomas }
9889 5e22b737 2022-05-31 thomas
9890 5e22b737 2022-05-31 thomas const struct got_error *
9891 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9892 36832a8e 2022-05-31 thomas const char *fileindex_path)
9893 5e22b737 2022-05-31 thomas {
9894 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9895 5e22b737 2022-05-31 thomas
9896 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9897 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9898 5e22b737 2022-05-31 thomas
9899 5e22b737 2022-05-31 thomas return err;
9900 5e22b737 2022-05-31 thomas }