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 59530ea7 2023-07-17 thomas
397 59530ea7 2023-07-17 thomas /* We only accept worktree-relative paths here. */
398 59530ea7 2023-07-17 thomas if (got_path_is_absolute(path)) {
399 59530ea7 2023-07-17 thomas return got_error_fmt(GOT_ERR_BAD_PATH,
400 59530ea7 2023-07-17 thomas "%s does not accept absolute paths: %s",
401 59530ea7 2023-07-17 thomas __func__, path);
402 59530ea7 2023-07-17 thomas }
403 4a1ddfc2 2019-01-12 stsp
404 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
406 4a1ddfc2 2019-01-12 stsp
407 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
408 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 ddcd8544 2019-03-11 stsp struct stat sb;
410 ddcd8544 2019-03-11 stsp err = NULL;
411 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
413 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
414 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
415 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
416 ddcd8544 2019-03-11 stsp }
417 ddcd8544 2019-03-11 stsp }
418 4a1ddfc2 2019-01-12 stsp free(abspath);
419 68c76935 2019-02-19 stsp return err;
420 68c76935 2019-02-19 stsp }
421 68c76935 2019-02-19 stsp
422 68c76935 2019-02-19 stsp static const struct got_error *
423 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
424 68c76935 2019-02-19 stsp {
425 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
426 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
427 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
428 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
429 68c76935 2019-02-19 stsp
430 68c76935 2019-02-19 stsp *same = 1;
431 68c76935 2019-02-19 stsp
432 230a42bd 2019-05-11 jcs for (;;) {
433 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
436 68c76935 2019-02-19 stsp break;
437 68c76935 2019-02-19 stsp }
438 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
440 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp if (flen1 == 0) {
444 68c76935 2019-02-19 stsp if (flen2 != 0)
445 68c76935 2019-02-19 stsp *same = 0;
446 68c76935 2019-02-19 stsp break;
447 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
448 68c76935 2019-02-19 stsp if (flen1 != 0)
449 68c76935 2019-02-19 stsp *same = 0;
450 68c76935 2019-02-19 stsp break;
451 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
452 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 68c76935 2019-02-19 stsp *same = 0;
454 68c76935 2019-02-19 stsp break;
455 68c76935 2019-02-19 stsp }
456 68c76935 2019-02-19 stsp } else {
457 68c76935 2019-02-19 stsp *same = 0;
458 68c76935 2019-02-19 stsp break;
459 68c76935 2019-02-19 stsp }
460 68c76935 2019-02-19 stsp }
461 68c76935 2019-02-19 stsp
462 68c76935 2019-02-19 stsp return err;
463 68c76935 2019-02-19 stsp }
464 68c76935 2019-02-19 stsp
465 68c76935 2019-02-19 stsp static const struct got_error *
466 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
467 68c76935 2019-02-19 stsp {
468 68c76935 2019-02-19 stsp struct stat sb;
469 68c76935 2019-02-19 stsp size_t size1, size2;
470 68c76935 2019-02-19 stsp
471 68c76935 2019-02-19 stsp *same = 1;
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
474 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
475 68c76935 2019-02-19 stsp size1 = sb.st_size;
476 68c76935 2019-02-19 stsp
477 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
478 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
479 68c76935 2019-02-19 stsp size2 = sb.st_size;
480 68c76935 2019-02-19 stsp
481 68c76935 2019-02-19 stsp if (size1 != size2) {
482 68c76935 2019-02-19 stsp *same = 0;
483 68c76935 2019-02-19 stsp return NULL;
484 68c76935 2019-02-19 stsp }
485 68c76935 2019-02-19 stsp
486 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
487 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
488 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
489 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
490 68c76935 2019-02-19 stsp
491 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
492 24f136e0 2021-11-20 thomas }
493 24f136e0 2021-11-20 thomas
494 24f136e0 2021-11-20 thomas static const struct got_error *
495 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
496 24f136e0 2021-11-20 thomas {
497 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
498 24f136e0 2021-11-20 thomas size_t flen;
499 24f136e0 2021-11-20 thomas ssize_t outlen;
500 24f136e0 2021-11-20 thomas
501 24f136e0 2021-11-20 thomas *outsize = 0;
502 24f136e0 2021-11-20 thomas
503 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
504 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
505 24f136e0 2021-11-20 thomas
506 24f136e0 2021-11-20 thomas for (;;) {
507 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
508 24f136e0 2021-11-20 thomas if (flen == 0) {
509 24f136e0 2021-11-20 thomas if (ferror(f))
510 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
511 24f136e0 2021-11-20 thomas if (feof(f))
512 24f136e0 2021-11-20 thomas break;
513 24f136e0 2021-11-20 thomas }
514 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
515 24f136e0 2021-11-20 thomas if (outlen == -1)
516 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
517 24f136e0 2021-11-20 thomas if (outlen != flen)
518 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
519 24f136e0 2021-11-20 thomas *outsize += outlen;
520 24f136e0 2021-11-20 thomas }
521 24f136e0 2021-11-20 thomas
522 24f136e0 2021-11-20 thomas return NULL;
523 24f136e0 2021-11-20 thomas }
524 24f136e0 2021-11-20 thomas
525 24f136e0 2021-11-20 thomas static const struct got_error *
526 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
527 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 24f136e0 2021-11-20 thomas const char *ondisk_path)
530 24f136e0 2021-11-20 thomas {
531 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
532 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
533 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
537 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
538 24f136e0 2021-11-20 thomas
539 24f136e0 2021-11-20 thomas *overlapcnt = 0;
540 24f136e0 2021-11-20 thomas
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
542 24f136e0 2021-11-20 thomas if (err)
543 24f136e0 2021-11-20 thomas return err;
544 24f136e0 2021-11-20 thomas
545 24f136e0 2021-11-20 thomas if (same_content)
546 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
547 24f136e0 2021-11-20 thomas
548 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
549 24f136e0 2021-11-20 thomas if (err)
550 24f136e0 2021-11-20 thomas return err;
551 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
552 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
553 24f136e0 2021-11-20 thomas if (err)
554 24f136e0 2021-11-20 thomas return err;
555 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
556 24f136e0 2021-11-20 thomas
557 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
558 24f136e0 2021-11-20 thomas *overlapcnt = 1;
559 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
561 24f136e0 2021-11-20 thomas goto done;
562 24f136e0 2021-11-20 thomas }
563 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
565 24f136e0 2021-11-20 thomas goto done;
566 24f136e0 2021-11-20 thomas }
567 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
569 24f136e0 2021-11-20 thomas goto done;
570 24f136e0 2021-11-20 thomas }
571 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 fc2a50f2 2022-11-01 thomas base_path_orig, "");
573 24f136e0 2021-11-20 thomas if (err)
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
577 24f136e0 2021-11-20 thomas if (err)
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
581 24f136e0 2021-11-20 thomas if (err)
582 24f136e0 2021-11-20 thomas goto done;
583 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
584 24f136e0 2021-11-20 thomas if (err)
585 24f136e0 2021-11-20 thomas goto done;
586 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
587 24f136e0 2021-11-20 thomas if (err)
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
590 24f136e0 2021-11-20 thomas if (err)
591 24f136e0 2021-11-20 thomas goto done;
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
593 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
596 24f136e0 2021-11-20 thomas }
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
600 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
601 24f136e0 2021-11-20 thomas path_deriv) < 0) {
602 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
603 24f136e0 2021-11-20 thomas goto done;
604 24f136e0 2021-11-20 thomas }
605 24f136e0 2021-11-20 thomas if (size_orig > 0) {
606 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
608 24f136e0 2021-11-20 thomas label_orig ? " " : "",
609 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
610 24f136e0 2021-11-20 thomas path_orig) < 0) {
611 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
612 24f136e0 2021-11-20 thomas goto done;
613 24f136e0 2021-11-20 thomas }
614 24f136e0 2021-11-20 thomas }
615 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
617 24f136e0 2021-11-20 thomas path_deriv2,
618 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
619 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
620 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
621 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
622 24f136e0 2021-11-20 thomas goto done;
623 24f136e0 2021-11-20 thomas }
624 24f136e0 2021-11-20 thomas } else if (changed_deriv)
625 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 24f136e0 2021-11-20 thomas else if (changed_deriv2)
627 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
628 24f136e0 2021-11-20 thomas done:
629 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 24f136e0 2021-11-20 thomas err == NULL)
631 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
632 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
634 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
636 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
638 24f136e0 2021-11-20 thomas free(path_orig);
639 24f136e0 2021-11-20 thomas free(path_deriv);
640 24f136e0 2021-11-20 thomas free(path_deriv2);
641 24f136e0 2021-11-20 thomas free(base_path_orig);
642 24f136e0 2021-11-20 thomas free(base_path_deriv);
643 24f136e0 2021-11-20 thomas free(base_path_deriv2);
644 24f136e0 2021-11-20 thomas return err;
645 6353ad76 2019-02-08 stsp }
646 6353ad76 2019-02-08 stsp
647 6353ad76 2019-02-08 stsp /*
648 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
649 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
650 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
651 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
652 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
653 6353ad76 2019-02-08 stsp */
654 6353ad76 2019-02-08 stsp static const struct got_error *
655 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
658 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
661 6353ad76 2019-02-08 stsp {
662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
663 6353ad76 2019-02-08 stsp int merged_fd = -1;
664 db590691 2021-06-02 stsp FILE *f_merged = NULL;
665 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
666 234035bc 2019-06-01 stsp int overlapcnt = 0;
667 3524bbf9 2020-10-20 stsp char *parent = NULL;
668 6353ad76 2019-02-08 stsp
669 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
670 234035bc 2019-06-01 stsp
671 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
672 3524bbf9 2020-10-20 stsp if (err)
673 3524bbf9 2020-10-20 stsp return err;
674 af54ae4a 2019-02-19 stsp
675 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
677 3524bbf9 2020-10-20 stsp goto done;
678 3524bbf9 2020-10-20 stsp }
679 af54ae4a 2019-02-19 stsp
680 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
681 6353ad76 2019-02-08 stsp if (err)
682 6353ad76 2019-02-08 stsp goto done;
683 3b9f0f87 2020-07-23 stsp
684 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 24f136e0 2021-11-20 thomas if (err) {
687 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
688 24f136e0 2021-11-20 thomas goto done;
689 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 24f136e0 2021-11-20 thomas ondisk_path);
692 24f136e0 2021-11-20 thomas if (err)
693 24f136e0 2021-11-20 thomas goto done;
694 24f136e0 2021-11-20 thomas }
695 6353ad76 2019-02-08 stsp
696 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
697 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
698 1ee397ad 2019-07-12 stsp if (err)
699 1ee397ad 2019-07-12 stsp goto done;
700 6353ad76 2019-02-08 stsp
701 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
703 816dc654 2019-02-16 stsp goto done;
704 68c76935 2019-02-19 stsp }
705 68c76935 2019-02-19 stsp
706 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
707 db590691 2021-06-02 stsp if (f_merged == NULL) {
708 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
709 db590691 2021-06-02 stsp goto done;
710 db590691 2021-06-02 stsp }
711 db590691 2021-06-02 stsp merged_fd = -1;
712 db590691 2021-06-02 stsp
713 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
714 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
715 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
716 db590691 2021-06-02 stsp f_merged);
717 68c76935 2019-02-19 stsp if (err)
718 68c76935 2019-02-19 stsp goto done;
719 816dc654 2019-02-16 stsp }
720 6353ad76 2019-02-08 stsp
721 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
723 70a0c8ec 2019-02-20 stsp goto done;
724 70a0c8ec 2019-02-20 stsp }
725 70a0c8ec 2019-02-20 stsp
726 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
727 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
728 230a42bd 2019-05-11 jcs ondisk_path);
729 6353ad76 2019-02-08 stsp goto done;
730 6353ad76 2019-02-08 stsp }
731 6353ad76 2019-02-08 stsp done:
732 fdcb7daf 2019-12-15 stsp if (err) {
733 fdcb7daf 2019-12-15 stsp if (merged_path)
734 fdcb7daf 2019-12-15 stsp unlink(merged_path);
735 3b9f0f87 2020-07-23 stsp }
736 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
738 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
740 6353ad76 2019-02-08 stsp free(merged_path);
741 af54ae4a 2019-02-19 stsp free(base_path);
742 3524bbf9 2020-10-20 stsp free(parent);
743 af57b12a 2020-07-23 stsp return err;
744 af57b12a 2020-07-23 stsp }
745 af57b12a 2020-07-23 stsp
746 af57b12a 2020-07-23 stsp static const struct got_error *
747 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
748 af57b12a 2020-07-23 stsp size_t target_len)
749 af57b12a 2020-07-23 stsp {
750 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
751 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
752 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
753 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
754 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
755 af57b12a 2020-07-23 stsp ondisk_path);
756 af57b12a 2020-07-23 stsp return NULL;
757 af57b12a 2020-07-23 stsp }
758 af57b12a 2020-07-23 stsp
759 af57b12a 2020-07-23 stsp /*
760 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
762 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
763 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
764 993e2a1b 2020-07-23 stsp *
765 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
766 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
767 993e2a1b 2020-07-23 stsp *
768 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
769 993e2a1b 2020-07-23 stsp * has deleted this symlink.
770 11cc08c1 2020-07-23 stsp */
771 11cc08c1 2020-07-23 stsp static const struct got_error *
772 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
773 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
775 11cc08c1 2020-07-23 stsp {
776 11cc08c1 2020-07-23 stsp const struct got_error *err;
777 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 11cc08c1 2020-07-23 stsp FILE *f = NULL;
779 11cc08c1 2020-07-23 stsp
780 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
781 11cc08c1 2020-07-23 stsp if (err)
782 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
783 11cc08c1 2020-07-23 stsp
784 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
785 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
787 11cc08c1 2020-07-23 stsp goto done;
788 11cc08c1 2020-07-23 stsp }
789 11cc08c1 2020-07-23 stsp
790 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
791 11cc08c1 2020-07-23 stsp if (err)
792 3818e3c4 2020-11-01 naddy goto done;
793 3818e3c4 2020-11-01 naddy
794 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
796 11cc08c1 2020-07-23 stsp goto done;
797 3818e3c4 2020-11-01 naddy }
798 11cc08c1 2020-07-23 stsp
799 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
802 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
803 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
804 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
805 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
806 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
807 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
809 11cc08c1 2020-07-23 stsp goto done;
810 11cc08c1 2020-07-23 stsp }
811 11cc08c1 2020-07-23 stsp
812 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
813 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
814 11cc08c1 2020-07-23 stsp goto done;
815 11cc08c1 2020-07-23 stsp }
816 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
817 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
818 11cc08c1 2020-07-23 stsp goto done;
819 11cc08c1 2020-07-23 stsp }
820 11cc08c1 2020-07-23 stsp done:
821 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
822 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
823 11cc08c1 2020-07-23 stsp free(path);
824 11cc08c1 2020-07-23 stsp free(id_str);
825 11cc08c1 2020-07-23 stsp free(label_deriv);
826 11cc08c1 2020-07-23 stsp return err;
827 11cc08c1 2020-07-23 stsp }
828 d219f183 2020-07-23 stsp
829 d219f183 2020-07-23 stsp /* forward declaration */
830 d219f183 2020-07-23 stsp static const struct got_error *
831 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
833 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
834 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
835 11cc08c1 2020-07-23 stsp
836 11cc08c1 2020-07-23 stsp /*
837 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
838 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
839 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
840 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
841 af57b12a 2020-07-23 stsp */
842 af57b12a 2020-07-23 stsp static const struct got_error *
843 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
844 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
845 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
846 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
848 af57b12a 2020-07-23 stsp {
849 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
850 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
851 af57b12a 2020-07-23 stsp struct stat sb;
852 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
853 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
854 11cc08c1 2020-07-23 stsp int have_local_change = 0;
855 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
856 af57b12a 2020-07-23 stsp
857 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
859 af57b12a 2020-07-23 stsp
860 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
861 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
862 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
863 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
864 af57b12a 2020-07-23 stsp ondisk_path);
865 af57b12a 2020-07-23 stsp goto done;
866 af57b12a 2020-07-23 stsp }
867 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
868 af57b12a 2020-07-23 stsp
869 526a746f 2020-07-23 stsp if (blob_orig) {
870 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
871 526a746f 2020-07-23 stsp if (err)
872 526a746f 2020-07-23 stsp goto done;
873 526a746f 2020-07-23 stsp }
874 af57b12a 2020-07-23 stsp
875 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
876 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 11cc08c1 2020-07-23 stsp have_local_change = 1;
879 11cc08c1 2020-07-23 stsp
880 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
881 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
882 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
883 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
885 11cc08c1 2020-07-23 stsp
886 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
887 11cc08c1 2020-07-23 stsp if (ancestor_target) {
888 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 11cc08c1 2020-07-23 stsp path);
891 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
892 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
894 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
895 11cc08c1 2020-07-23 stsp path);
896 11cc08c1 2020-07-23 stsp } else {
897 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
898 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
899 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
900 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
901 11cc08c1 2020-07-23 stsp if (err)
902 11cc08c1 2020-07-23 stsp goto done;
903 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
904 11cc08c1 2020-07-23 stsp path);
905 11cc08c1 2020-07-23 stsp }
906 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
907 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
908 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
909 af57b12a 2020-07-23 stsp strlen(deriv_target));
910 af57b12a 2020-07-23 stsp if (err)
911 af57b12a 2020-07-23 stsp goto done;
912 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
914 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
915 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
917 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
918 ea7786be 2020-07-23 stsp path);
919 ea7786be 2020-07-23 stsp } else {
920 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
921 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
922 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
923 ea7786be 2020-07-23 stsp if (err)
924 ea7786be 2020-07-23 stsp goto done;
925 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
926 ea7786be 2020-07-23 stsp path);
927 ea7786be 2020-07-23 stsp }
928 6353ad76 2019-02-08 stsp }
929 11cc08c1 2020-07-23 stsp
930 af57b12a 2020-07-23 stsp done:
931 af57b12a 2020-07-23 stsp free(ancestor_target);
932 eec2f5a9 2021-06-03 stsp return err;
933 eec2f5a9 2021-06-03 stsp }
934 eec2f5a9 2021-06-03 stsp
935 eec2f5a9 2021-06-03 stsp static const struct got_error *
936 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
937 eec2f5a9 2021-06-03 stsp {
938 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
939 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
940 eec2f5a9 2021-06-03 stsp ssize_t target_len;
941 eec2f5a9 2021-06-03 stsp size_t n;
942 eec2f5a9 2021-06-03 stsp FILE *f;
943 eec2f5a9 2021-06-03 stsp
944 eec2f5a9 2021-06-03 stsp *outfile = NULL;
945 eec2f5a9 2021-06-03 stsp
946 eec2f5a9 2021-06-03 stsp f = got_opentemp();
947 eec2f5a9 2021-06-03 stsp if (f == NULL)
948 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
949 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
952 eec2f5a9 2021-06-03 stsp goto done;
953 eec2f5a9 2021-06-03 stsp }
954 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
955 eec2f5a9 2021-06-03 stsp if (n != target_len) {
956 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
957 eec2f5a9 2021-06-03 stsp goto done;
958 eec2f5a9 2021-06-03 stsp }
959 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
960 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
961 eec2f5a9 2021-06-03 stsp goto done;
962 eec2f5a9 2021-06-03 stsp }
963 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
964 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
965 eec2f5a9 2021-06-03 stsp goto done;
966 eec2f5a9 2021-06-03 stsp }
967 eec2f5a9 2021-06-03 stsp done:
968 eec2f5a9 2021-06-03 stsp if (err)
969 eec2f5a9 2021-06-03 stsp fclose(f);
970 eec2f5a9 2021-06-03 stsp else
971 eec2f5a9 2021-06-03 stsp *outfile = f;
972 14c901f1 2019-08-08 stsp return err;
973 14c901f1 2019-08-08 stsp }
974 14c901f1 2019-08-08 stsp
975 14c901f1 2019-08-08 stsp /*
976 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
978 14c901f1 2019-08-08 stsp * acts as the second derived version.
979 14c901f1 2019-08-08 stsp */
980 14c901f1 2019-08-08 stsp static const struct got_error *
981 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
983 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
984 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
985 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
987 14c901f1 2019-08-08 stsp {
988 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
989 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
991 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
993 14c901f1 2019-08-08 stsp
994 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
995 14c901f1 2019-08-08 stsp
996 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
997 ed6b5030 2020-10-20 stsp if (err)
998 ed6b5030 2020-10-20 stsp return err;
999 14c901f1 2019-08-08 stsp
1000 67a66647 2021-05-31 stsp if (blob_orig) {
1001 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 67a66647 2021-05-31 stsp parent) == -1) {
1003 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1004 67a66647 2021-05-31 stsp base_path = NULL;
1005 67a66647 2021-05-31 stsp goto done;
1006 67a66647 2021-05-31 stsp }
1007 67a66647 2021-05-31 stsp
1008 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 fc2a50f2 2022-11-01 thomas base_path, "");
1010 67a66647 2021-05-31 stsp if (err)
1011 67a66647 2021-05-31 stsp goto done;
1012 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 67a66647 2021-05-31 stsp blob_orig);
1014 67a66647 2021-05-31 stsp if (err)
1015 67a66647 2021-05-31 stsp goto done;
1016 67a66647 2021-05-31 stsp free(base_path);
1017 db590691 2021-06-02 stsp } else {
1018 db590691 2021-06-02 stsp /*
1019 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1020 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1021 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1022 db590691 2021-06-02 stsp */
1023 db590691 2021-06-02 stsp f_orig = got_opentemp();
1024 db590691 2021-06-02 stsp if (f_orig == NULL) {
1025 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1026 db590691 2021-06-02 stsp goto done;
1027 db590691 2021-06-02 stsp }
1028 67a66647 2021-05-31 stsp }
1029 67a66647 2021-05-31 stsp
1030 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp base_path = NULL;
1033 14c901f1 2019-08-08 stsp goto done;
1034 14c901f1 2019-08-08 stsp }
1035 14c901f1 2019-08-08 stsp
1036 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1037 14c901f1 2019-08-08 stsp if (err)
1038 14c901f1 2019-08-08 stsp goto done;
1039 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 14c901f1 2019-08-08 stsp blob_deriv);
1041 14c901f1 2019-08-08 stsp if (err)
1042 14c901f1 2019-08-08 stsp goto done;
1043 14c901f1 2019-08-08 stsp
1044 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1045 14c901f1 2019-08-08 stsp if (err)
1046 14c901f1 2019-08-08 stsp goto done;
1047 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1050 14c901f1 2019-08-08 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp }
1052 eec2f5a9 2021-06-03 stsp
1053 b6b86fd1 2022-08-30 thomas /*
1054 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1055 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1056 eec2f5a9 2021-06-03 stsp */
1057 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1058 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1059 eec2f5a9 2021-06-03 stsp if (err)
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp } else {
1062 eec2f5a9 2021-06-03 stsp int fd;
1063 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1066 eec2f5a9 2021-06-03 stsp goto done;
1067 eec2f5a9 2021-06-03 stsp }
1068 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1071 eec2f5a9 2021-06-03 stsp close(fd);
1072 eec2f5a9 2021-06-03 stsp goto done;
1073 eec2f5a9 2021-06-03 stsp }
1074 14c901f1 2019-08-08 stsp }
1075 14c901f1 2019-08-08 stsp
1076 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1079 14c901f1 2019-08-08 stsp done:
1080 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1082 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1084 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1086 14c901f1 2019-08-08 stsp free(base_path);
1087 67a66647 2021-05-31 stsp if (blob_orig_path) {
1088 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1089 67a66647 2021-05-31 stsp free(blob_orig_path);
1090 67a66647 2021-05-31 stsp }
1091 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1092 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1093 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1094 14c901f1 2019-08-08 stsp }
1095 6353ad76 2019-02-08 stsp free(id_str);
1096 818c7501 2019-07-11 stsp free(label_deriv);
1097 ed6b5030 2020-10-20 stsp free(parent);
1098 4a1ddfc2 2019-01-12 stsp return err;
1099 4a1ddfc2 2019-01-12 stsp }
1100 4a1ddfc2 2019-01-12 stsp
1101 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1102 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1105 13d9040b 2019-03-27 stsp {
1106 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1107 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1108 13d9040b 2019-03-27 stsp
1109 65b05cec 2020-07-23 stsp *new_iep = NULL;
1110 65b05cec 2020-07-23 stsp
1111 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1112 054041d0 2020-06-24 stsp if (err)
1113 054041d0 2020-06-24 stsp return err;
1114 054041d0 2020-06-24 stsp
1115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1116 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1117 054041d0 2020-06-24 stsp if (err)
1118 054041d0 2020-06-24 stsp goto done;
1119 054041d0 2020-06-24 stsp
1120 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1121 054041d0 2020-06-24 stsp done:
1122 054041d0 2020-06-24 stsp if (err)
1123 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1124 65b05cec 2020-07-23 stsp else
1125 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1126 13d9040b 2019-03-27 stsp return err;
1127 1ebedb77 2019-10-19 stsp }
1128 1ebedb77 2019-10-19 stsp
1129 1ebedb77 2019-10-19 stsp static mode_t
1130 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1131 1ebedb77 2019-10-19 stsp {
1132 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1133 1ebedb77 2019-10-19 stsp
1134 1ebedb77 2019-10-19 stsp if (executable) {
1135 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1136 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1137 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1140 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1141 1ebedb77 2019-10-19 stsp }
1142 1ebedb77 2019-10-19 stsp
1143 a2c162eb 2022-10-30 thomas return st_mode;
1144 13d9040b 2019-03-27 stsp }
1145 13d9040b 2019-03-27 stsp
1146 8ba819a3 2020-07-23 stsp /* forward declaration */
1147 13d9040b 2019-03-27 stsp static const struct got_error *
1148 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1150 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1151 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1152 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1153 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 8ba819a3 2020-07-23 stsp
1155 5a1fbc73 2020-07-23 stsp /*
1156 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1157 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1158 5a1fbc73 2020-07-23 stsp */
1159 8ba819a3 2020-07-23 stsp static const struct got_error *
1160 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1162 5a1fbc73 2020-07-23 stsp {
1163 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1164 5a1fbc73 2020-07-23 stsp ssize_t elen;
1165 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1166 5a1fbc73 2020-07-23 stsp int fd;
1167 5a1fbc73 2020-07-23 stsp
1168 c6e8a826 2021-04-05 stsp *did_something = 0;
1169 c6e8a826 2021-04-05 stsp
1170 5a1fbc73 2020-07-23 stsp /*
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1177 5a1fbc73 2020-07-23 stsp */
1178 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1191 5a1fbc73 2020-07-23 stsp }
1192 5a1fbc73 2020-07-23 stsp
1193 c6e8a826 2021-04-05 stsp *did_something = 1;
1194 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1195 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1196 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1197 5a1fbc73 2020-07-23 stsp return err;
1198 3c1ec782 2020-07-23 stsp }
1199 3c1ec782 2020-07-23 stsp
1200 3c1ec782 2020-07-23 stsp static const struct got_error *
1201 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1203 3c1ec782 2020-07-23 stsp {
1204 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1205 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1206 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1207 3c1ec782 2020-07-23 stsp
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1211 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1212 3c1ec782 2020-07-23 stsp return NULL;
1213 3c1ec782 2020-07-23 stsp }
1214 3c1ec782 2020-07-23 stsp
1215 3c1ec782 2020-07-23 stsp /*
1216 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1217 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1218 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1219 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1220 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1221 3c1ec782 2020-07-23 stsp */
1222 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1223 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1224 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1225 ce031e9e 2020-10-20 stsp if (err)
1226 ce031e9e 2020-10-20 stsp return err;
1227 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1230 ce031e9e 2020-10-20 stsp }
1231 ce031e9e 2020-10-20 stsp free(parent);
1232 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1233 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp }
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp free(abspath);
1239 3c1ec782 2020-07-23 stsp if (err)
1240 3c1ec782 2020-07-23 stsp return err;
1241 3c1ec782 2020-07-23 stsp } else {
1242 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1243 3c1ec782 2020-07-23 stsp if (err)
1244 3c1ec782 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp }
1246 3c1ec782 2020-07-23 stsp
1247 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1248 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1249 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1250 3c1ec782 2020-07-23 stsp return NULL;
1251 3c1ec782 2020-07-23 stsp }
1252 3c1ec782 2020-07-23 stsp
1253 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1254 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1255 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1256 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1257 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1259 3c1ec782 2020-07-23 stsp
1260 3c1ec782 2020-07-23 stsp free(path_got);
1261 3c1ec782 2020-07-23 stsp return NULL;
1262 5a1fbc73 2020-07-23 stsp }
1263 5a1fbc73 2020-07-23 stsp
1264 5a1fbc73 2020-07-23 stsp static const struct got_error *
1265 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1268 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1269 ace90326 2021-09-27 thomas struct got_repository *repo,
1270 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1271 9d31a1d8 2018-03-11 stsp {
1272 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1273 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1274 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1275 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1277 8ba819a3 2020-07-23 stsp
1278 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1279 2e1fa222 2020-07-23 stsp
1280 9e39ca77 2022-07-21 thomas /*
1281 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1282 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1283 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1284 8ba819a3 2020-07-23 stsp * in the blob object.
1285 8ba819a3 2020-07-23 stsp */
1286 8ba819a3 2020-07-23 stsp do {
1287 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1288 3521b466 2022-07-21 thomas if (err)
1289 3521b466 2022-07-21 thomas return err;
1290 3521b466 2022-07-21 thomas
1291 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1292 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1293 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1294 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1295 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1296 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1298 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1299 3b9f0f87 2020-07-23 stsp progress_arg);
1300 8ba819a3 2020-07-23 stsp }
1301 8ba819a3 2020-07-23 stsp if (len > 0) {
1302 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1303 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1304 8ba819a3 2020-07-23 stsp len - hdrlen);
1305 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1306 8ba819a3 2020-07-23 stsp hdrlen = 0;
1307 8ba819a3 2020-07-23 stsp }
1308 8ba819a3 2020-07-23 stsp } while (len != 0);
1309 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1310 8ba819a3 2020-07-23 stsp
1311 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1313 3c1ec782 2020-07-23 stsp if (err)
1314 3c1ec782 2020-07-23 stsp return err;
1315 8ba819a3 2020-07-23 stsp
1316 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1317 906c123b 2020-07-23 stsp /* install as a regular file */
1318 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1319 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1320 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1322 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1323 9e39ca77 2022-07-21 thomas return err;
1324 906c123b 2020-07-23 stsp }
1325 906c123b 2020-07-23 stsp
1326 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1327 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1328 c6e8a826 2021-04-05 stsp int symlink_replaced;
1329 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1330 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1331 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1332 9e39ca77 2022-07-21 thomas return err;
1333 c90c8ce3 2020-07-23 stsp }
1334 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1335 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1336 5a1fbc73 2020-07-23 stsp if (err)
1337 9e39ca77 2022-07-21 thomas return err;
1338 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1339 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1344 c6e8a826 2021-04-05 stsp } else {
1345 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1347 c6e8a826 2021-04-05 stsp }
1348 f35fa46a 2020-07-23 stsp }
1349 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp
1352 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1353 f4994adc 2020-10-20 stsp char *parent;
1354 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1355 f4994adc 2020-10-20 stsp if (err)
1356 9e39ca77 2022-07-21 thomas return err;
1357 59530ea7 2023-07-17 thomas err = got_path_mkdir(parent);
1358 f4994adc 2020-10-20 stsp free(parent);
1359 f35fa46a 2020-07-23 stsp if (err)
1360 9e39ca77 2022-07-21 thomas return err;
1361 f35fa46a 2020-07-23 stsp /*
1362 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1363 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1364 f35fa46a 2020-07-23 stsp */
1365 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1366 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1367 59530ea7 2023-07-17 thomas if (progress_cb) {
1368 59530ea7 2023-07-17 thomas err = (*progress_cb)(progress_arg,
1369 59530ea7 2023-07-17 thomas reverting_versioned_file ?
1370 59530ea7 2023-07-17 thomas GOT_STATUS_REVERT : GOT_STATUS_ADD,
1371 59530ea7 2023-07-17 thomas path);
1372 59530ea7 2023-07-17 thomas }
1373 9e39ca77 2022-07-21 thomas return err;
1374 f35fa46a 2020-07-23 stsp }
1375 f35fa46a 2020-07-23 stsp }
1376 f35fa46a 2020-07-23 stsp
1377 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1378 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1379 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1380 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1381 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1382 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1383 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1385 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1386 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1387 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1388 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1389 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1390 8ba819a3 2020-07-23 stsp } else {
1391 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1392 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1393 8ba819a3 2020-07-23 stsp }
1394 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1395 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1396 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1397 8ba819a3 2020-07-23 stsp return err;
1398 8ba819a3 2020-07-23 stsp }
1399 8ba819a3 2020-07-23 stsp
1400 8ba819a3 2020-07-23 stsp static const struct got_error *
1401 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1402 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1403 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1404 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1405 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1406 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1407 8ba819a3 2020-07-23 stsp {
1408 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1409 507dc3bb 2018-12-29 stsp int fd = -1;
1410 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1411 507dc3bb 2018-12-29 stsp int update = 0;
1412 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1413 a2c162eb 2022-10-30 thomas mode_t mode;
1414 8ba819a3 2020-07-23 stsp
1415 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1416 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1417 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1418 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1419 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1420 f5375317 2020-10-20 stsp char *parent;
1421 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1422 f5375317 2020-10-20 stsp if (err)
1423 f5375317 2020-10-20 stsp return err;
1424 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1425 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1426 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1427 f5375317 2020-10-20 stsp free(parent);
1428 21908da4 2019-01-13 stsp if (err)
1429 21908da4 2019-01-13 stsp return err;
1430 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1431 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1432 a2c162eb 2022-10-30 thomas mode);
1433 21908da4 2019-01-13 stsp if (fd == -1)
1434 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1435 230a42bd 2019-05-11 jcs ondisk_path);
1436 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1437 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1438 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1439 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1440 3b9f0f87 2020-07-23 stsp goto done;
1441 3b9f0f87 2020-07-23 stsp }
1442 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1443 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1444 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1445 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1446 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1447 507dc3bb 2018-12-29 stsp goto done;
1448 d70b8e30 2018-12-27 stsp } else {
1449 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1450 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1451 507dc3bb 2018-12-29 stsp if (err)
1452 507dc3bb 2018-12-29 stsp goto done;
1453 507dc3bb 2018-12-29 stsp update = 1;
1454 a2c162eb 2022-10-30 thomas
1455 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1456 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1457 a2c162eb 2022-10-30 thomas tmppath);
1458 a2c162eb 2022-10-30 thomas goto done;
1459 a2c162eb 2022-10-30 thomas }
1460 9d31a1d8 2018-03-11 stsp }
1461 507dc3bb 2018-12-29 stsp } else
1462 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1463 3818e3c4 2020-11-01 naddy }
1464 3818e3c4 2020-11-01 naddy
1465 bd6aa359 2020-07-23 stsp if (progress_cb) {
1466 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1467 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 bd6aa359 2020-07-23 stsp path);
1469 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1470 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 bd6aa359 2020-07-23 stsp path);
1472 bd6aa359 2020-07-23 stsp else
1473 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1474 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 bd6aa359 2020-07-23 stsp if (err)
1476 bd6aa359 2020-07-23 stsp goto done;
1477 bd6aa359 2020-07-23 stsp }
1478 d7b62c98 2018-12-27 stsp
1479 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1480 9d31a1d8 2018-03-11 stsp do {
1481 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1483 9d31a1d8 2018-03-11 stsp if (err)
1484 9d31a1d8 2018-03-11 stsp break;
1485 9d31a1d8 2018-03-11 stsp if (len > 0) {
1486 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1487 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1490 b87c6f83 2018-12-24 stsp goto done;
1491 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1492 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1493 b87c6f83 2018-12-24 stsp goto done;
1494 9d31a1d8 2018-03-11 stsp }
1495 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1496 9d31a1d8 2018-03-11 stsp }
1497 9d31a1d8 2018-03-11 stsp } while (len != 0);
1498 9d31a1d8 2018-03-11 stsp
1499 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1501 816dc654 2019-02-16 stsp goto done;
1502 816dc654 2019-02-16 stsp }
1503 9d31a1d8 2018-03-11 stsp
1504 507dc3bb 2018-12-29 stsp if (update) {
1505 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1506 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1507 f6d8c0ac 2020-11-09 stsp goto done;
1508 f6d8c0ac 2020-11-09 stsp }
1509 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1510 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1511 230a42bd 2019-05-11 jcs ondisk_path);
1512 68ed9ba5 2019-02-10 stsp goto done;
1513 68ed9ba5 2019-02-10 stsp }
1514 63df146d 2020-11-09 stsp free(tmppath);
1515 63df146d 2020-11-09 stsp tmppath = NULL;
1516 507dc3bb 2018-12-29 stsp }
1517 507dc3bb 2018-12-29 stsp
1518 9d31a1d8 2018-03-11 stsp done:
1519 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1521 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1522 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1523 507dc3bb 2018-12-29 stsp free(tmppath);
1524 6353ad76 2019-02-08 stsp return err;
1525 6353ad76 2019-02-08 stsp }
1526 6353ad76 2019-02-08 stsp
1527 be94c032 2023-02-20 thomas /*
1528 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1529 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1530 be94c032 2023-02-20 thomas */
1531 6353ad76 2019-02-08 stsp static const struct got_error *
1532 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1533 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1534 be94c032 2023-02-20 thomas FILE *ondisk_file)
1535 7154f6ce 2019-03-27 stsp {
1536 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1537 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1538 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1540 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1541 7154f6ce 2019-03-27 stsp };
1542 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1543 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1544 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1545 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1546 9bdd68dd 2020-01-02 naddy char *line = NULL;
1547 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1548 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1549 7154f6ce 2019-03-27 stsp
1550 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1551 8de9d8ad 2023-02-20 thomas return NULL;
1552 be94c032 2023-02-20 thomas
1553 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1554 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1555 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1556 be94c032 2023-02-20 thomas
1557 8de9d8ad 2023-02-20 thomas if (blob) {
1558 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1559 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1560 be94c032 2023-02-20 thomas if (err)
1561 be94c032 2023-02-20 thomas goto done;
1562 8de9d8ad 2023-02-20 thomas }
1563 be94c032 2023-02-20 thomas
1564 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1565 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1566 8de9d8ad 2023-02-20 thomas if (err)
1567 8de9d8ad 2023-02-20 thomas goto done;
1568 8de9d8ad 2023-02-20 thomas
1569 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1570 8de9d8ad 2023-02-20 thomas
1571 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1572 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1573 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1574 450d9f6b 2023-02-20 thomas off_t pos;
1575 8de9d8ad 2023-02-20 thomas
1576 8de9d8ad 2023-02-20 thomas /*
1577 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1578 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1579 8de9d8ad 2023-02-20 thomas */
1580 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1581 8de9d8ad 2023-02-20 thomas
1582 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1583 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1584 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1585 7154f6ce 2019-03-27 stsp }
1586 7154f6ce 2019-03-27 stsp
1587 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1588 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1589 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 450d9f6b 2023-02-20 thomas goto done;
1591 7154f6ce 2019-03-27 stsp }
1592 8de9d8ad 2023-02-20 thomas
1593 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1594 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1595 450d9f6b 2023-02-20 thomas
1596 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1597 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1598 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1599 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1600 8de9d8ad 2023-02-20 thomas break;
1601 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 8de9d8ad 2023-02-20 thomas break;
1603 8de9d8ad 2023-02-20 thomas }
1604 8de9d8ad 2023-02-20 thomas
1605 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1606 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1607 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1608 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1609 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1610 8de9d8ad 2023-02-20 thomas goto done;
1611 8de9d8ad 2023-02-20 thomas } else
1612 8de9d8ad 2023-02-20 thomas i++;
1613 8de9d8ad 2023-02-20 thomas }
1614 8de9d8ad 2023-02-20 thomas ++ln;
1615 8de9d8ad 2023-02-20 thomas }
1616 7154f6ce 2019-03-27 stsp }
1617 be94c032 2023-02-20 thomas
1618 be94c032 2023-02-20 thomas done:
1619 9bdd68dd 2020-01-02 naddy free(line);
1620 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1621 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1622 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1623 8de9d8ad 2023-02-20 thomas if (err == NULL)
1624 8de9d8ad 2023-02-20 thomas err = free_err;
1625 7154f6ce 2019-03-27 stsp
1626 7154f6ce 2019-03-27 stsp return err;
1627 e2b1e152 2019-07-13 stsp }
1628 e2b1e152 2019-07-13 stsp
1629 e2b1e152 2019-07-13 stsp static int
1630 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1631 1ebedb77 2019-10-19 stsp {
1632 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1634 1ebedb77 2019-10-19 stsp }
1635 1ebedb77 2019-10-19 stsp
1636 1ebedb77 2019-10-19 stsp static int
1637 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1638 e2b1e152 2019-07-13 stsp {
1639 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1644 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1645 c363b2c1 2019-08-03 stsp }
1646 c363b2c1 2019-08-03 stsp
1647 c363b2c1 2019-08-03 stsp static unsigned char
1648 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1649 c363b2c1 2019-08-03 stsp {
1650 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1651 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1652 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1653 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1654 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1655 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1656 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1657 c363b2c1 2019-08-03 stsp default:
1658 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1659 a919d5c4 2020-07-23 stsp }
1660 a919d5c4 2020-07-23 stsp }
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp static const struct got_error *
1663 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1664 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1665 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1666 a919d5c4 2020-07-23 stsp {
1667 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1668 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1669 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1670 a919d5c4 2020-07-23 stsp ssize_t elen;
1671 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1672 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1674 a919d5c4 2020-07-23 stsp
1675 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1676 a919d5c4 2020-07-23 stsp
1677 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1678 a919d5c4 2020-07-23 stsp do {
1679 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1680 a919d5c4 2020-07-23 stsp if (err)
1681 a919d5c4 2020-07-23 stsp return err;
1682 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1683 a919d5c4 2020-07-23 stsp /*
1684 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1685 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1686 a919d5c4 2020-07-23 stsp */
1687 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1688 a919d5c4 2020-07-23 stsp }
1689 a919d5c4 2020-07-23 stsp if (len > 0) {
1690 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1691 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1692 a919d5c4 2020-07-23 stsp len - hdrlen);
1693 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1694 a919d5c4 2020-07-23 stsp hdrlen = 0;
1695 a919d5c4 2020-07-23 stsp }
1696 a919d5c4 2020-07-23 stsp } while (len != 0);
1697 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1700 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 a919d5c4 2020-07-23 stsp if (elen == -1)
1702 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1703 a919d5c4 2020-07-23 stsp } else {
1704 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1705 a919d5c4 2020-07-23 stsp if (elen == -1)
1706 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1707 c363b2c1 2019-08-03 stsp }
1708 a919d5c4 2020-07-23 stsp
1709 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1711 a919d5c4 2020-07-23 stsp
1712 a919d5c4 2020-07-23 stsp return NULL;
1713 7154f6ce 2019-03-27 stsp }
1714 7154f6ce 2019-03-27 stsp
1715 7154f6ce 2019-03-27 stsp static const struct got_error *
1716 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1717 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1718 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1719 6353ad76 2019-02-08 stsp {
1720 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1721 6353ad76 2019-02-08 stsp struct got_object_id id;
1722 6353ad76 2019-02-08 stsp size_t hdrlen;
1723 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1724 6353ad76 2019-02-08 stsp FILE *f = NULL;
1725 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1726 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1727 6353ad76 2019-02-08 stsp size_t flen, blen;
1728 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1729 6353ad76 2019-02-08 stsp
1730 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1731 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1732 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1733 6353ad76 2019-02-08 stsp
1734 7f91a133 2019-12-13 stsp /*
1735 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1736 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1737 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1738 7f91a133 2019-12-13 stsp */
1739 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1740 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1742 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1743 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1744 882ef1b9 2019-12-13 stsp else
1745 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1746 882ef1b9 2019-12-13 stsp goto done;
1747 882ef1b9 2019-12-13 stsp }
1748 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1749 3d35a492 2019-12-13 stsp goto done;
1750 3d35a492 2019-12-13 stsp }
1751 7f91a133 2019-12-13 stsp } else {
1752 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1753 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1754 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1755 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1756 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1757 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1758 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1759 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1761 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1762 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1763 3d35a492 2019-12-13 stsp else
1764 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1765 3d35a492 2019-12-13 stsp goto done;
1766 3d35a492 2019-12-13 stsp }
1767 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1768 1338848f 2019-12-13 stsp goto done;
1769 a378724f 2019-02-10 stsp }
1770 a378724f 2019-02-10 stsp }
1771 6353ad76 2019-02-08 stsp
1772 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1774 1338848f 2019-12-13 stsp goto done;
1775 3f148bc6 2019-07-27 stsp }
1776 efdd40df 2019-07-27 stsp
1777 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1779 1338848f 2019-12-13 stsp goto done;
1780 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1781 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1783 1338848f 2019-12-13 stsp goto done;
1784 d00136be 2019-03-26 stsp }
1785 b8f41171 2019-02-10 stsp
1786 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1787 f179e66d 2020-07-23 stsp goto done;
1788 f179e66d 2020-07-23 stsp
1789 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1790 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1792 1338848f 2019-12-13 stsp goto done;
1793 f179e66d 2020-07-23 stsp }
1794 6353ad76 2019-02-08 stsp
1795 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1796 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1797 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1798 c363b2c1 2019-08-03 stsp else
1799 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1800 c363b2c1 2019-08-03 stsp
1801 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1802 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1803 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1804 f4ae6ddb 2022-07-01 thomas goto done;
1805 f4ae6ddb 2022-07-01 thomas }
1806 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1807 6353ad76 2019-02-08 stsp if (err)
1808 1338848f 2019-12-13 stsp goto done;
1809 6353ad76 2019-02-08 stsp
1810 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1811 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1812 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1813 a919d5c4 2020-07-23 stsp goto done;
1814 a919d5c4 2020-07-23 stsp }
1815 a919d5c4 2020-07-23 stsp
1816 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1817 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1818 ab0d4361 2019-12-13 stsp if (fd == -1) {
1819 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1820 ab0d4361 2019-12-13 stsp goto done;
1821 ab0d4361 2019-12-13 stsp }
1822 3d35a492 2019-12-13 stsp }
1823 3d35a492 2019-12-13 stsp
1824 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1825 6353ad76 2019-02-08 stsp if (f == NULL) {
1826 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1827 6353ad76 2019-02-08 stsp goto done;
1828 6353ad76 2019-02-08 stsp }
1829 1338848f 2019-12-13 stsp fd = -1;
1830 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1831 656b1f76 2019-05-11 jcs for (;;) {
1832 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1833 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1834 6353ad76 2019-02-08 stsp if (err)
1835 7154f6ce 2019-03-27 stsp goto done;
1836 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1837 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1838 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1839 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1840 7154f6ce 2019-03-27 stsp goto done;
1841 80c5c120 2019-02-19 stsp }
1842 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1843 6353ad76 2019-02-08 stsp if (flen != 0)
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1847 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1848 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 6353ad76 2019-02-08 stsp break;
1850 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1851 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1852 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1853 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1854 6353ad76 2019-02-08 stsp break;
1855 6353ad76 2019-02-08 stsp }
1856 6353ad76 2019-02-08 stsp } else {
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1858 6353ad76 2019-02-08 stsp break;
1859 6353ad76 2019-02-08 stsp }
1860 6353ad76 2019-02-08 stsp hdrlen = 0;
1861 6353ad76 2019-02-08 stsp }
1862 7154f6ce 2019-03-27 stsp
1863 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1864 7154f6ce 2019-03-27 stsp rewind(f);
1865 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1866 be94c032 2023-02-20 thomas sb, f);
1867 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1868 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1869 6353ad76 2019-02-08 stsp done:
1870 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1871 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1872 6353ad76 2019-02-08 stsp if (blob)
1873 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1874 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1875 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1876 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1877 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1878 9d31a1d8 2018-03-11 stsp return err;
1879 e2b1e152 2019-07-13 stsp }
1880 e2b1e152 2019-07-13 stsp
1881 e2b1e152 2019-07-13 stsp /*
1882 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1883 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1884 e2b1e152 2019-07-13 stsp */
1885 e2b1e152 2019-07-13 stsp static const struct got_error *
1886 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1887 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1888 e2b1e152 2019-07-13 stsp {
1889 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1890 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1891 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1892 e2b1e152 2019-07-13 stsp
1893 e2b1e152 2019-07-13 stsp return NULL;
1894 9d31a1d8 2018-03-11 stsp }
1895 9d31a1d8 2018-03-11 stsp
1896 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1897 9f323212 2023-03-10 thomas
1898 9d31a1d8 2018-03-11 stsp static const struct got_error *
1899 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1900 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1901 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1902 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1903 0584f854 2019-04-06 stsp void *progress_arg)
1904 9d31a1d8 2018-03-11 stsp {
1905 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1906 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1907 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1908 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1909 b8f41171 2019-02-10 stsp struct stat sb;
1910 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1911 9d31a1d8 2018-03-11 stsp
1912 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1913 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1914 6353ad76 2019-02-08 stsp
1915 abb4604f 2019-07-27 stsp if (ie) {
1916 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1917 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1918 a76c42e6 2019-08-03 stsp goto done;
1919 a76c42e6 2019-08-03 stsp }
1920 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1921 7f91a133 2019-12-13 stsp repo);
1922 abb4604f 2019-07-27 stsp if (err)
1923 abb4604f 2019-07-27 stsp goto done;
1924 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1925 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1926 c90c8ce3 2020-07-23 stsp } else {
1927 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1928 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1929 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1930 e6f4ba31 2021-09-24 thomas ondisk_path);
1931 e6f4ba31 2021-09-24 thomas goto done;
1932 e6f4ba31 2021-09-24 thomas }
1933 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1935 e6f4ba31 2021-09-24 thomas } else {
1936 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1937 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1938 e6f4ba31 2021-09-24 thomas else
1939 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1940 e6f4ba31 2021-09-24 thomas }
1941 c90c8ce3 2020-07-23 stsp }
1942 6353ad76 2019-02-08 stsp
1943 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1944 2c41dce7 2021-06-27 stsp if (ie)
1945 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1946 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1947 b8f41171 2019-02-10 stsp goto done;
1948 b8f41171 2019-02-10 stsp }
1949 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1950 a769b60b 2021-06-27 stsp if (ie)
1951 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1952 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1953 5036ab18 2020-04-18 stsp path);
1954 5036ab18 2020-04-18 stsp goto done;
1955 9f323212 2023-03-10 thomas }
1956 9f323212 2023-03-10 thomas
1957 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1958 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1959 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1960 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1961 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1962 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1963 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1964 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1965 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1966 9f323212 2023-03-10 thomas } else if (ie) {
1967 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1968 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1969 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1970 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1971 9f323212 2023-03-10 thomas ie->path);
1972 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1973 9f323212 2023-03-10 thomas errno == ENOENT))
1974 9f323212 2023-03-10 thomas goto done;
1975 9f323212 2023-03-10 thomas }
1976 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1977 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1978 9f323212 2023-03-10 thomas ie->path);
1979 9f323212 2023-03-10 thomas }
1980 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1981 5036ab18 2020-04-18 stsp }
1982 b8f41171 2019-02-10 stsp
1983 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1984 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1985 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1986 c6e8a826 2021-04-05 stsp /*
1987 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1988 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1989 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1990 c6e8a826 2021-04-05 stsp * updating contents of this file.
1991 c6e8a826 2021-04-05 stsp */
1992 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1993 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1994 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1995 c6e8a826 2021-04-05 stsp /* Same commit. */
1996 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1997 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1998 e2b1e152 2019-07-13 stsp if (err)
1999 e2b1e152 2019-07-13 stsp goto done;
2000 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2001 b8f41171 2019-02-10 stsp path);
2002 6353ad76 2019-02-08 stsp goto done;
2003 a378724f 2019-02-10 stsp }
2004 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2005 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2006 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2007 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2008 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
2009 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
2010 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2011 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2012 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2013 b36a9774 2023-04-14 thomas }
2014 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2015 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2016 0f58026f 2021-04-05 stsp if (err)
2017 0f58026f 2021-04-05 stsp goto done;
2018 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2019 0f58026f 2021-04-05 stsp path);
2020 b8f41171 2019-02-10 stsp goto done;
2021 e2b1e152 2019-07-13 stsp }
2022 9d31a1d8 2018-03-11 stsp }
2023 9d31a1d8 2018-03-11 stsp
2024 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2025 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2026 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2027 f4ae6ddb 2022-07-01 thomas goto done;
2028 f4ae6ddb 2022-07-01 thomas }
2029 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2030 8da9e5f4 2019-01-12 stsp if (err)
2031 6353ad76 2019-02-08 stsp goto done;
2032 512f0d0e 2019-01-02 stsp
2033 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2034 234035bc 2019-06-01 stsp int update_timestamps;
2035 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2036 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2037 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2038 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2039 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2040 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2041 f4ae6ddb 2022-07-01 thomas goto done;
2042 f4ae6ddb 2022-07-01 thomas }
2043 234035bc 2019-06-01 stsp struct got_object_id id2;
2044 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2045 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2046 f4ae6ddb 2022-07-01 thomas fd2);
2047 234035bc 2019-06-01 stsp if (err)
2048 f69721c3 2019-10-21 stsp goto done;
2049 f69721c3 2019-10-21 stsp }
2050 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2051 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2052 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2053 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2054 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2055 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2056 f69721c3 2019-10-21 stsp goto done;
2057 f69721c3 2019-10-21 stsp }
2058 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2059 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2060 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2061 234035bc 2019-06-01 stsp goto done;
2062 f69721c3 2019-10-21 stsp }
2063 234035bc 2019-06-01 stsp }
2064 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2065 36bf999c 2020-07-23 stsp char *link_target;
2066 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2067 36bf999c 2020-07-23 stsp if (err)
2068 36bf999c 2020-07-23 stsp goto done;
2069 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2070 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2071 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2072 36bf999c 2020-07-23 stsp free(link_target);
2073 993e2a1b 2020-07-23 stsp } else {
2074 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2075 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2076 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2077 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2078 993e2a1b 2020-07-23 stsp }
2079 f69721c3 2019-10-21 stsp free(label_orig);
2080 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2081 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2082 f4ae6ddb 2022-07-01 thomas goto done;
2083 f4ae6ddb 2022-07-01 thomas }
2084 234035bc 2019-06-01 stsp if (blob2)
2085 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2086 909d120e 2019-09-22 stsp if (err)
2087 909d120e 2019-09-22 stsp goto done;
2088 234035bc 2019-06-01 stsp /*
2089 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2090 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2091 234035bc 2019-06-01 stsp * unmodified files again.
2092 234035bc 2019-06-01 stsp */
2093 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2095 234035bc 2019-06-01 stsp update_timestamps);
2096 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2098 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2099 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2100 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2101 1ee397ad 2019-07-12 stsp if (err)
2102 1ee397ad 2019-07-12 stsp goto done;
2103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2105 13d9040b 2019-03-27 stsp if (err)
2106 13d9040b 2019-03-27 stsp goto done;
2107 13d9040b 2019-03-27 stsp } else {
2108 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2109 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2110 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2111 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2112 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2113 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2114 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2115 2e1fa222 2020-07-23 stsp } else {
2116 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2117 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2118 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2119 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2120 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2121 2e1fa222 2020-07-23 stsp }
2122 13d9040b 2019-03-27 stsp if (err)
2123 13d9040b 2019-03-27 stsp goto done;
2124 65b05cec 2020-07-23 stsp
2125 054041d0 2020-06-24 stsp if (ie) {
2126 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2127 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2128 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2129 054041d0 2020-06-24 stsp } else {
2130 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2131 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2132 054041d0 2020-06-24 stsp &blob->id);
2133 054041d0 2020-06-24 stsp }
2134 13d9040b 2019-03-27 stsp if (err)
2135 13d9040b 2019-03-27 stsp goto done;
2136 65b05cec 2020-07-23 stsp
2137 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2138 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2139 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2140 2e1fa222 2020-07-23 stsp }
2141 13d9040b 2019-03-27 stsp }
2142 f4ae6ddb 2022-07-01 thomas
2143 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2144 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2145 f4ae6ddb 2022-07-01 thomas goto done;
2146 f4ae6ddb 2022-07-01 thomas }
2147 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2148 6353ad76 2019-02-08 stsp done:
2149 6353ad76 2019-02-08 stsp free(ondisk_path);
2150 8da9e5f4 2019-01-12 stsp return err;
2151 90285c3b 2019-01-08 stsp }
2152 90285c3b 2019-01-08 stsp
2153 90285c3b 2019-01-08 stsp static const struct got_error *
2154 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2155 90285c3b 2019-01-08 stsp {
2156 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2157 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2158 90285c3b 2019-01-08 stsp
2159 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2160 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2161 90285c3b 2019-01-08 stsp
2162 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2163 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2164 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2165 c97665ae 2019-01-13 stsp } else {
2166 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2167 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2168 1c4cdd89 2021-06-20 stsp if (err)
2169 1c4cdd89 2021-06-20 stsp goto done;
2170 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2171 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2172 fddefe3b 2020-10-20 stsp free(ondisk_path);
2173 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2174 1c4cdd89 2021-06-20 stsp parent = NULL;
2175 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2176 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2178 fddefe3b 2020-10-20 stsp ondisk_path);
2179 90285c3b 2019-01-08 stsp break;
2180 90285c3b 2019-01-08 stsp }
2181 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2182 1c4cdd89 2021-06-20 stsp if (err)
2183 1c4cdd89 2021-06-20 stsp break;
2184 1c4cdd89 2021-06-20 stsp }
2185 90285c3b 2019-01-08 stsp }
2186 1c4cdd89 2021-06-20 stsp done:
2187 90285c3b 2019-01-08 stsp free(ondisk_path);
2188 1c4cdd89 2021-06-20 stsp free(parent);
2189 90285c3b 2019-01-08 stsp return err;
2190 512f0d0e 2019-01-02 stsp }
2191 512f0d0e 2019-01-02 stsp
2192 708d8e67 2019-03-27 stsp static const struct got_error *
2193 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2195 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2196 708d8e67 2019-03-27 stsp {
2197 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2198 708d8e67 2019-03-27 stsp unsigned char status;
2199 708d8e67 2019-03-27 stsp struct stat sb;
2200 708d8e67 2019-03-27 stsp char *ondisk_path;
2201 708d8e67 2019-03-27 stsp
2202 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2203 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2204 a76c42e6 2019-08-03 stsp
2205 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2206 708d8e67 2019-03-27 stsp == -1)
2207 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2208 708d8e67 2019-03-27 stsp
2209 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2210 708d8e67 2019-03-27 stsp if (err)
2211 5a58a424 2020-06-23 stsp goto done;
2212 708d8e67 2019-03-27 stsp
2213 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2214 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2215 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2216 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2217 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2218 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2219 993e2a1b 2020-07-23 stsp goto done;
2220 993e2a1b 2020-07-23 stsp }
2221 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2222 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2223 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2224 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2225 993e2a1b 2020-07-23 stsp if (err)
2226 993e2a1b 2020-07-23 stsp goto done;
2227 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2228 993e2a1b 2020-07-23 stsp ie->path);
2229 993e2a1b 2020-07-23 stsp goto done;
2230 993e2a1b 2020-07-23 stsp }
2231 993e2a1b 2020-07-23 stsp
2232 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2233 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2234 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2235 1ee397ad 2019-07-12 stsp if (err)
2236 5a58a424 2020-06-23 stsp goto done;
2237 fc6346c4 2019-03-27 stsp /*
2238 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2239 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2240 fc6346c4 2019-03-27 stsp */
2241 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2242 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2243 fc6346c4 2019-03-27 stsp } else {
2244 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2245 1ee397ad 2019-07-12 stsp if (err)
2246 5a58a424 2020-06-23 stsp goto done;
2247 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2248 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2249 fc6346c4 2019-03-27 stsp if (err)
2250 5a58a424 2020-06-23 stsp goto done;
2251 fc6346c4 2019-03-27 stsp }
2252 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2253 708d8e67 2019-03-27 stsp }
2254 5a58a424 2020-06-23 stsp done:
2255 5a58a424 2020-06-23 stsp free(ondisk_path);
2256 fc6346c4 2019-03-27 stsp return err;
2257 708d8e67 2019-03-27 stsp }
2258 708d8e67 2019-03-27 stsp
2259 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2260 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2261 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2262 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2263 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2264 8da9e5f4 2019-01-12 stsp void *progress_arg;
2265 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2266 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2267 8da9e5f4 2019-01-12 stsp };
2268 8da9e5f4 2019-01-12 stsp
2269 512f0d0e 2019-01-02 stsp static const struct got_error *
2270 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2271 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2272 512f0d0e 2019-01-02 stsp {
2273 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2274 0584f854 2019-04-06 stsp
2275 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2276 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2277 512f0d0e 2019-01-02 stsp
2278 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2279 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2280 8da9e5f4 2019-01-12 stsp }
2281 512f0d0e 2019-01-02 stsp
2282 8da9e5f4 2019-01-12 stsp static const struct got_error *
2283 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2284 8da9e5f4 2019-01-12 stsp {
2285 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2286 7a9df742 2019-01-08 stsp
2287 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2289 0584f854 2019-04-06 stsp
2290 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2291 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2292 9d31a1d8 2018-03-11 stsp }
2293 9d31a1d8 2018-03-11 stsp
2294 9d31a1d8 2018-03-11 stsp static const struct got_error *
2295 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2296 9d31a1d8 2018-03-11 stsp {
2297 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2298 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2299 8da9e5f4 2019-01-12 stsp char *path;
2300 9d31a1d8 2018-03-11 stsp
2301 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2302 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2303 0584f854 2019-04-06 stsp
2304 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2305 59530ea7 2023-07-17 thomas return NULL;
2306 59530ea7 2023-07-17 thomas
2307 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2308 63c5ca5d 2019-08-24 stsp return NULL;
2309 63c5ca5d 2019-08-24 stsp
2310 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2311 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2312 8da9e5f4 2019-01-12 stsp == -1)
2313 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2314 9d31a1d8 2018-03-11 stsp
2315 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2316 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2317 9d31a1d8 2018-03-11 stsp
2318 8da9e5f4 2019-01-12 stsp free(path);
2319 0cd1c46a 2019-03-11 stsp return err;
2320 0cd1c46a 2019-03-11 stsp }
2321 0cd1c46a 2019-03-11 stsp
2322 b2118c49 2020-07-28 stsp const struct got_error *
2323 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2324 b2118c49 2020-07-28 stsp {
2325 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2326 b2118c49 2020-07-28 stsp
2327 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2328 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2329 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2330 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2331 b2118c49 2020-07-28 stsp }
2332 b2118c49 2020-07-28 stsp
2333 b2118c49 2020-07-28 stsp return NULL;
2334 b2118c49 2020-07-28 stsp }
2335 b2118c49 2020-07-28 stsp
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2338 0cd1c46a 2019-03-11 stsp {
2339 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2340 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2341 0cd1c46a 2019-03-11 stsp
2342 517bab73 2019-03-11 stsp *refname = NULL;
2343 517bab73 2019-03-11 stsp
2344 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2345 b2118c49 2020-07-28 stsp if (err)
2346 b2118c49 2020-07-28 stsp return err;
2347 0cd1c46a 2019-03-11 stsp
2348 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2350 0647c563 2019-03-11 stsp *refname = NULL;
2351 0cd1c46a 2019-03-11 stsp }
2352 517bab73 2019-03-11 stsp free(uuidstr);
2353 517bab73 2019-03-11 stsp return err;
2354 517bab73 2019-03-11 stsp }
2355 517bab73 2019-03-11 stsp
2356 818c7501 2019-07-11 stsp const struct got_error *
2357 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2358 f6cd0243 2023-01-28 thomas const char *prefix)
2359 f6cd0243 2023-01-28 thomas {
2360 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2361 f6cd0243 2023-01-28 thomas }
2362 f6cd0243 2023-01-28 thomas
2363 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2364 fcfaa6d5 2023-07-05 thomas get_base_ref_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_BASE_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_tmp_ref_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_TMP_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_newbase_symref_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, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2380 818c7501 2019-07-11 stsp }
2381 818c7501 2019-07-11 stsp
2382 818c7501 2019-07-11 stsp static const struct got_error *
2383 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2384 818c7501 2019-07-11 stsp {
2385 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2386 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2387 818c7501 2019-07-11 stsp }
2388 818c7501 2019-07-11 stsp
2389 818c7501 2019-07-11 stsp static const struct got_error *
2390 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2391 818c7501 2019-07-11 stsp {
2392 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2393 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2394 0ebf8283 2019-07-24 stsp }
2395 0ebf8283 2019-07-24 stsp
2396 0ebf8283 2019-07-24 stsp static const struct got_error *
2397 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2398 0ebf8283 2019-07-24 stsp {
2399 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2400 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2401 0ebf8283 2019-07-24 stsp }
2402 0ebf8283 2019-07-24 stsp
2403 0ebf8283 2019-07-24 stsp static const struct got_error *
2404 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2405 0ebf8283 2019-07-24 stsp {
2406 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2407 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2408 0ebf8283 2019-07-24 stsp }
2409 0ebf8283 2019-07-24 stsp
2410 0ebf8283 2019-07-24 stsp static const struct got_error *
2411 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2412 0ebf8283 2019-07-24 stsp {
2413 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2414 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2415 818c7501 2019-07-11 stsp }
2416 818c7501 2019-07-11 stsp
2417 0ebf8283 2019-07-24 stsp static const struct got_error *
2418 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2419 0ebf8283 2019-07-24 stsp {
2420 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2421 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2422 0ebf8283 2019-07-24 stsp }
2423 818c7501 2019-07-11 stsp
2424 0ebf8283 2019-07-24 stsp const struct got_error *
2425 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2426 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2427 0ebf8283 2019-07-24 stsp {
2428 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2429 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2430 0ebf8283 2019-07-24 stsp *path = NULL;
2431 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2432 0ebf8283 2019-07-24 stsp }
2433 0ebf8283 2019-07-24 stsp return NULL;
2434 10604dce 2021-09-24 thomas }
2435 10604dce 2021-09-24 thomas
2436 10604dce 2021-09-24 thomas static const struct got_error *
2437 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2438 10604dce 2021-09-24 thomas {
2439 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2440 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2441 0ebf8283 2019-07-24 stsp }
2442 0ebf8283 2019-07-24 stsp
2443 10604dce 2021-09-24 thomas static const struct got_error *
2444 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2445 10604dce 2021-09-24 thomas {
2446 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2447 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2448 10604dce 2021-09-24 thomas }
2449 10604dce 2021-09-24 thomas
2450 517bab73 2019-03-11 stsp /*
2451 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2452 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2453 517bab73 2019-03-11 stsp */
2454 517bab73 2019-03-11 stsp static const struct got_error *
2455 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2456 517bab73 2019-03-11 stsp {
2457 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2458 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2459 517bab73 2019-03-11 stsp char *refname;
2460 517bab73 2019-03-11 stsp
2461 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2462 517bab73 2019-03-11 stsp if (err)
2463 517bab73 2019-03-11 stsp return err;
2464 0cd1c46a 2019-03-11 stsp
2465 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2466 0cd1c46a 2019-03-11 stsp if (err)
2467 0cd1c46a 2019-03-11 stsp goto done;
2468 0cd1c46a 2019-03-11 stsp
2469 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2470 0cd1c46a 2019-03-11 stsp done:
2471 0cd1c46a 2019-03-11 stsp free(refname);
2472 0cd1c46a 2019-03-11 stsp if (ref)
2473 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2474 3e3a69f1 2019-07-25 stsp return err;
2475 3e3a69f1 2019-07-25 stsp }
2476 3e3a69f1 2019-07-25 stsp
2477 3e3a69f1 2019-07-25 stsp static const struct got_error *
2478 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2479 3e3a69f1 2019-07-25 stsp {
2480 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2481 3e3a69f1 2019-07-25 stsp
2482 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2483 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2484 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2485 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2486 3e3a69f1 2019-07-25 stsp }
2487 9d31a1d8 2018-03-11 stsp return err;
2488 9d31a1d8 2018-03-11 stsp }
2489 9d31a1d8 2018-03-11 stsp
2490 3e3a69f1 2019-07-25 stsp
2491 ebf99748 2019-05-09 stsp static const struct got_error *
2492 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2493 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2494 ebf99748 2019-05-09 stsp {
2495 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2496 ebf99748 2019-05-09 stsp FILE *index = NULL;
2497 0cd1c46a 2019-03-11 stsp
2498 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2499 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2500 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2501 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2502 ebf99748 2019-05-09 stsp
2503 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2504 3e3a69f1 2019-07-25 stsp if (err)
2505 ebf99748 2019-05-09 stsp goto done;
2506 ebf99748 2019-05-09 stsp
2507 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2508 ebf99748 2019-05-09 stsp if (index == NULL) {
2509 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2510 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2511 ebf99748 2019-05-09 stsp } else {
2512 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2513 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2514 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2515 ebf99748 2019-05-09 stsp }
2516 ebf99748 2019-05-09 stsp done:
2517 ebf99748 2019-05-09 stsp if (err) {
2518 ebf99748 2019-05-09 stsp free(*fileindex_path);
2519 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2520 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2521 ebf99748 2019-05-09 stsp *fileindex = NULL;
2522 ebf99748 2019-05-09 stsp }
2523 ebf99748 2019-05-09 stsp return err;
2524 ebf99748 2019-05-09 stsp }
2525 c932eeeb 2019-05-22 stsp
2526 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2527 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2528 c932eeeb 2019-05-22 stsp const char *path;
2529 c932eeeb 2019-05-22 stsp size_t path_len;
2530 c932eeeb 2019-05-22 stsp const char *entry_name;
2531 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2532 a484d721 2019-06-10 stsp void *progress_arg;
2533 c932eeeb 2019-05-22 stsp };
2534 ebf99748 2019-05-09 stsp
2535 c932eeeb 2019-05-22 stsp static const struct got_error *
2536 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2537 c932eeeb 2019-05-22 stsp {
2538 1ee397ad 2019-07-12 stsp const struct got_error *err;
2539 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2540 c932eeeb 2019-05-22 stsp
2541 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2542 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2543 c932eeeb 2019-05-22 stsp return NULL;
2544 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2545 102ff934 2019-06-11 stsp return NULL;
2546 102ff934 2019-06-11 stsp
2547 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2548 a769b60b 2021-06-27 stsp return NULL;
2549 a769b60b 2021-06-27 stsp
2550 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2551 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2552 c932eeeb 2019-05-22 stsp return NULL;
2553 c932eeeb 2019-05-22 stsp
2554 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2555 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2556 edd02c5e 2019-07-11 stsp ie->path);
2557 1ee397ad 2019-07-12 stsp if (err)
2558 1ee397ad 2019-07-12 stsp return err;
2559 1ee397ad 2019-07-12 stsp }
2560 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2561 c932eeeb 2019-05-22 stsp return NULL;
2562 a615e0e7 2020-12-16 stsp }
2563 a615e0e7 2020-12-16 stsp
2564 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2565 a615e0e7 2020-12-16 stsp static const struct got_error *
2566 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2567 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2568 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2569 a615e0e7 2020-12-16 stsp {
2570 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2571 a615e0e7 2020-12-16 stsp
2572 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2573 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2574 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2575 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2576 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2577 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2578 a615e0e7 2020-12-16 stsp
2579 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2580 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2581 9c6338c4 2019-06-02 stsp }
2582 9c6338c4 2019-06-02 stsp
2583 9c6338c4 2019-06-02 stsp static const struct got_error *
2584 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2585 9c6338c4 2019-06-02 stsp {
2586 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2587 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2588 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2589 867630bb 2020-01-17 stsp struct timespec timeout;
2590 9c6338c4 2019-06-02 stsp
2591 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2592 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2593 9c6338c4 2019-06-02 stsp if (err)
2594 9c6338c4 2019-06-02 stsp goto done;
2595 9c6338c4 2019-06-02 stsp
2596 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2597 9c6338c4 2019-06-02 stsp if (err)
2598 9c6338c4 2019-06-02 stsp goto done;
2599 9c6338c4 2019-06-02 stsp
2600 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2601 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2602 9c6338c4 2019-06-02 stsp fileindex_path);
2603 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2604 9c6338c4 2019-06-02 stsp }
2605 867630bb 2020-01-17 stsp
2606 867630bb 2020-01-17 stsp /*
2607 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2608 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2609 867630bb 2020-01-17 stsp * was recorded in the file index.
2610 867630bb 2020-01-17 stsp */
2611 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2612 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2613 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2614 9c6338c4 2019-06-02 stsp done:
2615 9c6338c4 2019-06-02 stsp if (new_index)
2616 9c6338c4 2019-06-02 stsp fclose(new_index);
2617 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2618 9c6338c4 2019-06-02 stsp return err;
2619 c932eeeb 2019-05-22 stsp }
2620 6ced4176 2019-07-12 stsp
2621 6ced4176 2019-07-12 stsp static const struct got_error *
2622 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2623 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2624 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2625 945f9229 2022-04-16 thomas struct got_repository *repo)
2626 6ced4176 2019-07-12 stsp {
2627 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2628 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2629 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2630 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2631 6ced4176 2019-07-12 stsp
2632 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2633 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2634 6ced4176 2019-07-12 stsp *tree_id = NULL;
2635 6ced4176 2019-07-12 stsp
2636 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2637 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2638 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2639 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2640 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2641 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2642 6ced4176 2019-07-12 stsp goto done;
2643 6ced4176 2019-07-12 stsp }
2644 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2645 945f9229 2022-04-16 thomas worktree->path_prefix);
2646 6ced4176 2019-07-12 stsp if (err)
2647 6ced4176 2019-07-12 stsp goto done;
2648 6ced4176 2019-07-12 stsp return NULL;
2649 6ced4176 2019-07-12 stsp }
2650 6ced4176 2019-07-12 stsp
2651 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2652 6ced4176 2019-07-12 stsp
2653 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2654 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2655 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2656 6ced4176 2019-07-12 stsp goto done;
2657 6ced4176 2019-07-12 stsp }
2658 6ced4176 2019-07-12 stsp
2659 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2660 6ced4176 2019-07-12 stsp if (err)
2661 6ced4176 2019-07-12 stsp goto done;
2662 6ced4176 2019-07-12 stsp
2663 6ced4176 2019-07-12 stsp free(in_repo_path);
2664 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2665 6ced4176 2019-07-12 stsp
2666 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2667 6ced4176 2019-07-12 stsp if (err)
2668 6ced4176 2019-07-12 stsp goto done;
2669 c932eeeb 2019-05-22 stsp
2670 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2671 6ced4176 2019-07-12 stsp /* Check out a single file. */
2672 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2673 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2674 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2675 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2676 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2677 6ced4176 2019-07-12 stsp goto done;
2678 6ced4176 2019-07-12 stsp }
2679 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2680 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2681 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2682 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp }
2684 6ced4176 2019-07-12 stsp } else {
2685 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2686 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2687 6ced4176 2019-07-12 stsp if (err)
2688 6ced4176 2019-07-12 stsp return err;
2689 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2690 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2691 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2692 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2693 6ced4176 2019-07-12 stsp goto done;
2694 6ced4176 2019-07-12 stsp }
2695 6ced4176 2019-07-12 stsp }
2696 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2697 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2698 6ced4176 2019-07-12 stsp } else {
2699 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2700 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2701 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2702 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2703 6ced4176 2019-07-12 stsp goto done;
2704 6ced4176 2019-07-12 stsp }
2705 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2706 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2707 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2708 6ced4176 2019-07-12 stsp goto done;
2709 6ced4176 2019-07-12 stsp }
2710 6ced4176 2019-07-12 stsp }
2711 6ced4176 2019-07-12 stsp done:
2712 6ced4176 2019-07-12 stsp free(id);
2713 6ced4176 2019-07-12 stsp free(in_repo_path);
2714 6ced4176 2019-07-12 stsp if (err) {
2715 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2716 6ced4176 2019-07-12 stsp free(*tree_relpath);
2717 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2718 6ced4176 2019-07-12 stsp free(*tree_id);
2719 6ced4176 2019-07-12 stsp *tree_id = NULL;
2720 6ced4176 2019-07-12 stsp }
2721 07ed472d 2019-07-12 stsp return err;
2722 07ed472d 2019-07-12 stsp }
2723 07ed472d 2019-07-12 stsp
2724 07ed472d 2019-07-12 stsp static const struct got_error *
2725 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2726 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2727 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2728 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2729 07ed472d 2019-07-12 stsp {
2730 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2731 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2732 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2733 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2734 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2735 07ed472d 2019-07-12 stsp
2736 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2737 7f47418f 2019-12-20 stsp if (err) {
2738 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2739 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2740 7f47418f 2019-12-20 stsp goto done;
2741 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2742 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2743 7f47418f 2019-12-20 stsp if (err)
2744 7f47418f 2019-12-20 stsp return err;
2745 7f47418f 2019-12-20 stsp }
2746 07ed472d 2019-07-12 stsp
2747 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2748 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2749 07ed472d 2019-07-12 stsp if (err)
2750 07ed472d 2019-07-12 stsp goto done;
2751 07ed472d 2019-07-12 stsp
2752 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2753 07ed472d 2019-07-12 stsp if (err)
2754 07ed472d 2019-07-12 stsp goto done;
2755 07ed472d 2019-07-12 stsp
2756 07ed472d 2019-07-12 stsp if (entry_name &&
2757 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2758 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2759 07ed472d 2019-07-12 stsp goto done;
2760 07ed472d 2019-07-12 stsp }
2761 07ed472d 2019-07-12 stsp
2762 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2763 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2764 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2765 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2766 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2767 07ed472d 2019-07-12 stsp arg.repo = repo;
2768 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2769 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2770 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2771 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2772 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2773 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2774 07ed472d 2019-07-12 stsp done:
2775 07ed472d 2019-07-12 stsp if (tree)
2776 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2777 07ed472d 2019-07-12 stsp if (commit)
2778 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2779 6ced4176 2019-07-12 stsp return err;
2780 6ced4176 2019-07-12 stsp }
2781 6ced4176 2019-07-12 stsp
2782 9d31a1d8 2018-03-11 stsp const struct got_error *
2783 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2784 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2785 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2786 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2787 9d31a1d8 2018-03-11 stsp {
2788 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2790 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2791 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2792 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2793 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2794 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2795 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2796 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2797 f2ea84fa 2019-07-27 stsp int entry_type;
2798 f2ea84fa 2019-07-27 stsp char *relpath;
2799 f2ea84fa 2019-07-27 stsp char *entry_name;
2800 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2801 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2802 9d31a1d8 2018-03-11 stsp
2803 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2804 f2ea84fa 2019-07-27 stsp
2805 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2806 9d31a1d8 2018-03-11 stsp if (err)
2807 9d31a1d8 2018-03-11 stsp return err;
2808 945f9229 2022-04-16 thomas
2809 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2810 945f9229 2022-04-16 thomas worktree->base_commit_id);
2811 945f9229 2022-04-16 thomas if (err)
2812 945f9229 2022-04-16 thomas goto done;
2813 93a30277 2018-12-24 stsp
2814 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2815 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2816 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2817 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2818 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2819 f2ea84fa 2019-07-27 stsp goto done;
2820 f2ea84fa 2019-07-27 stsp }
2821 6ced4176 2019-07-12 stsp
2822 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2823 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2824 945f9229 2022-04-16 thomas worktree, repo);
2825 f2ea84fa 2019-07-27 stsp if (err) {
2826 f2ea84fa 2019-07-27 stsp free(tpd);
2827 6ced4176 2019-07-12 stsp goto done;
2828 6ced4176 2019-07-12 stsp }
2829 f2ea84fa 2019-07-27 stsp
2830 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2831 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2832 f2ea84fa 2019-07-27 stsp if (err) {
2833 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2834 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2835 f2ea84fa 2019-07-27 stsp free(tpd);
2836 f2ea84fa 2019-07-27 stsp goto done;
2837 f2ea84fa 2019-07-27 stsp }
2838 f2ea84fa 2019-07-27 stsp } else
2839 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2840 f2ea84fa 2019-07-27 stsp
2841 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2842 6ced4176 2019-07-12 stsp }
2843 6ced4176 2019-07-12 stsp
2844 51514078 2018-12-25 stsp /*
2845 51514078 2018-12-25 stsp * Read the file index.
2846 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2847 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2848 51514078 2018-12-25 stsp */
2849 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2850 8da9e5f4 2019-01-12 stsp if (err)
2851 c4cdcb68 2019-04-03 stsp goto done;
2852 c4cdcb68 2019-04-03 stsp
2853 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2854 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2855 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2856 f2ea84fa 2019-07-27 stsp
2857 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2858 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2859 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2860 f2ea84fa 2019-07-27 stsp if (err)
2861 f2ea84fa 2019-07-27 stsp break;
2862 f2ea84fa 2019-07-27 stsp
2863 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2864 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2865 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2866 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2867 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2868 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2869 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2870 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2871 f2ea84fa 2019-07-27 stsp if (err)
2872 f2ea84fa 2019-07-27 stsp break;
2873 f2ea84fa 2019-07-27 stsp
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2875 711d9cd9 2019-07-12 stsp }
2876 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2877 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2878 af12c6b9 2019-06-04 stsp err = sync_err;
2879 9d31a1d8 2018-03-11 stsp done:
2880 9c6338c4 2019-06-02 stsp free(fileindex_path);
2881 edfa7d7f 2018-09-11 stsp if (tree)
2882 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2883 9d31a1d8 2018-03-11 stsp if (commit)
2884 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2885 5ade8233 2019-07-12 stsp if (fileindex)
2886 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2887 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2888 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2889 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2890 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2891 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2892 f2ea84fa 2019-07-27 stsp free(tpd);
2893 f2ea84fa 2019-07-27 stsp }
2894 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2895 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2896 9d31a1d8 2018-03-11 stsp err = unlockerr;
2897 f8d1f275 2019-02-04 stsp return err;
2898 f8d1f275 2019-02-04 stsp }
2899 f8d1f275 2019-02-04 stsp
2900 0529f8df 2023-03-10 thomas static const struct got_error *
2901 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2902 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2903 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2904 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2905 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2906 0529f8df 2023-03-10 thomas struct got_repository *repo,
2907 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2908 0529f8df 2023-03-10 thomas {
2909 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2910 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2911 0529f8df 2023-03-10 thomas
2912 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2913 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2914 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2915 0529f8df 2023-03-10 thomas restoring_missing_file,
2916 0529f8df 2023-03-10 thomas reverting_versioned_file,
2917 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2918 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2919 0529f8df 2023-03-10 thomas } else {
2920 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2921 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2922 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2923 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2924 0529f8df 2023-03-10 thomas }
2925 0529f8df 2023-03-10 thomas if (err)
2926 0529f8df 2023-03-10 thomas return err;
2927 0529f8df 2023-03-10 thomas if (ie == NULL) {
2928 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2929 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2930 0529f8df 2023-03-10 thomas if (err)
2931 0529f8df 2023-03-10 thomas return err;
2932 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2933 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2934 0529f8df 2023-03-10 thomas if (err) {
2935 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2936 0529f8df 2023-03-10 thomas return err;
2937 0529f8df 2023-03-10 thomas }
2938 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2939 0529f8df 2023-03-10 thomas if (err) {
2940 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2941 0529f8df 2023-03-10 thomas return err;
2942 0529f8df 2023-03-10 thomas }
2943 0529f8df 2023-03-10 thomas } else {
2944 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2945 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2946 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2947 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2948 0529f8df 2023-03-10 thomas if (err)
2949 0529f8df 2023-03-10 thomas return err;
2950 0529f8df 2023-03-10 thomas }
2951 0529f8df 2023-03-10 thomas
2952 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2953 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2954 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2955 0529f8df 2023-03-10 thomas }
2956 0529f8df 2023-03-10 thomas
2957 0529f8df 2023-03-10 thomas return NULL;
2958 0529f8df 2023-03-10 thomas }
2959 0529f8df 2023-03-10 thomas
2960 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2961 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2962 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2963 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2964 234035bc 2019-06-01 stsp void *progress_arg;
2965 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2966 234035bc 2019-06-01 stsp void *cancel_arg;
2967 f69721c3 2019-10-21 stsp const char *label_orig;
2968 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2969 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2970 234035bc 2019-06-01 stsp };
2971 234035bc 2019-06-01 stsp
2972 234035bc 2019-06-01 stsp static const struct got_error *
2973 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2974 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2975 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2976 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2977 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2978 234035bc 2019-06-01 stsp {
2979 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2980 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2981 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2982 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2983 234035bc 2019-06-01 stsp struct stat sb;
2984 234035bc 2019-06-01 stsp unsigned char status;
2985 234035bc 2019-06-01 stsp int local_changes_subsumed;
2986 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2987 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2988 234035bc 2019-06-01 stsp
2989 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2990 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2991 d6c87207 2019-08-02 stsp strlen(path2));
2992 1ee397ad 2019-07-12 stsp if (ie == NULL)
2993 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2994 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2995 234035bc 2019-06-01 stsp
2996 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2997 234035bc 2019-06-01 stsp path2) == -1)
2998 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2999 234035bc 2019-06-01 stsp
3000 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3001 7f91a133 2019-12-13 stsp repo);
3002 234035bc 2019-06-01 stsp if (err)
3003 234035bc 2019-06-01 stsp goto done;
3004 234035bc 2019-06-01 stsp
3005 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3006 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3007 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3008 234035bc 2019-06-01 stsp goto done;
3009 234035bc 2019-06-01 stsp }
3010 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3011 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3012 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3013 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3014 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3015 234035bc 2019-06-01 stsp goto done;
3016 234035bc 2019-06-01 stsp }
3017 234035bc 2019-06-01 stsp
3018 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3019 36bf999c 2020-07-23 stsp char *link_target2;
3020 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3021 36bf999c 2020-07-23 stsp if (err)
3022 36bf999c 2020-07-23 stsp goto done;
3023 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3024 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3025 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3026 36bf999c 2020-07-23 stsp free(link_target2);
3027 af57b12a 2020-07-23 stsp } else {
3028 1af628f4 2021-06-03 stsp int fd;
3029 1af628f4 2021-06-03 stsp
3030 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3031 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3032 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3033 1af628f4 2021-06-03 stsp goto done;
3034 1af628f4 2021-06-03 stsp }
3035 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 1af628f4 2021-06-03 stsp f_orig, blob1);
3037 1af628f4 2021-06-03 stsp if (err)
3038 1af628f4 2021-06-03 stsp goto done;
3039 1af628f4 2021-06-03 stsp
3040 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3041 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3042 1af628f4 2021-06-03 stsp goto done;
3043 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3044 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3045 1af628f4 2021-06-03 stsp if (err)
3046 1af628f4 2021-06-03 stsp goto done;
3047 1af628f4 2021-06-03 stsp
3048 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3049 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3050 1af628f4 2021-06-03 stsp if (fd == -1) {
3051 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3052 1af628f4 2021-06-03 stsp ondisk_path);
3053 1af628f4 2021-06-03 stsp goto done;
3054 1af628f4 2021-06-03 stsp }
3055 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3056 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3058 1af628f4 2021-06-03 stsp ondisk_path);
3059 1af628f4 2021-06-03 stsp close(fd);
3060 1af628f4 2021-06-03 stsp goto done;
3061 1af628f4 2021-06-03 stsp }
3062 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3063 1af628f4 2021-06-03 stsp if (err)
3064 1af628f4 2021-06-03 stsp goto done;
3065 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3066 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3067 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3068 1af628f4 2021-06-03 stsp goto done;
3069 1af628f4 2021-06-03 stsp }
3070 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3071 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3072 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3073 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3074 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3075 af57b12a 2020-07-23 stsp }
3076 234035bc 2019-06-01 stsp } else if (blob1) {
3077 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3078 d6c87207 2019-08-02 stsp strlen(path1));
3079 1ee397ad 2019-07-12 stsp if (ie == NULL)
3080 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3081 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3082 2b92fad7 2019-06-02 stsp
3083 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3084 2b92fad7 2019-06-02 stsp path1) == -1)
3085 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3086 2b92fad7 2019-06-02 stsp
3087 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3088 7f91a133 2019-12-13 stsp repo);
3089 2b92fad7 2019-06-02 stsp if (err)
3090 2b92fad7 2019-06-02 stsp goto done;
3091 2b92fad7 2019-06-02 stsp
3092 2b92fad7 2019-06-02 stsp switch (status) {
3093 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3094 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3095 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3096 1ee397ad 2019-07-12 stsp if (err)
3097 1ee397ad 2019-07-12 stsp goto done;
3098 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3099 2b92fad7 2019-06-02 stsp if (err)
3100 2b92fad7 2019-06-02 stsp goto done;
3101 2b92fad7 2019-06-02 stsp if (ie)
3102 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3103 2b92fad7 2019-06-02 stsp break;
3104 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3105 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3106 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3107 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3108 1ee397ad 2019-07-12 stsp if (err)
3109 1ee397ad 2019-07-12 stsp goto done;
3110 2b92fad7 2019-06-02 stsp if (ie)
3111 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3112 2b92fad7 2019-06-02 stsp break;
3113 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3114 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3115 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3116 e8f02263 2022-01-23 thomas off_t blob1_size;
3117 0a22ca1a 2020-09-23 stsp /*
3118 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3119 0a22ca1a 2020-09-23 stsp * exists in the repository.
3120 0a22ca1a 2020-09-23 stsp */
3121 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3122 e8f02263 2022-01-23 thomas &blob1_size, path1);
3123 0a22ca1a 2020-09-23 stsp if (err)
3124 0a22ca1a 2020-09-23 stsp goto done;
3125 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3126 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3127 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3128 0a22ca1a 2020-09-23 stsp if (err)
3129 0a22ca1a 2020-09-23 stsp goto done;
3130 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3131 0a22ca1a 2020-09-23 stsp path1);
3132 0a22ca1a 2020-09-23 stsp if (err)
3133 0a22ca1a 2020-09-23 stsp goto done;
3134 0a22ca1a 2020-09-23 stsp if (ie)
3135 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3136 0a22ca1a 2020-09-23 stsp ie);
3137 0a22ca1a 2020-09-23 stsp } else {
3138 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3139 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3140 0a22ca1a 2020-09-23 stsp }
3141 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3142 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3143 0a22ca1a 2020-09-23 stsp free(id);
3144 0a22ca1a 2020-09-23 stsp if (err)
3145 0a22ca1a 2020-09-23 stsp goto done;
3146 0a22ca1a 2020-09-23 stsp break;
3147 0a22ca1a 2020-09-23 stsp }
3148 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3149 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3150 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3151 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3152 1ee397ad 2019-07-12 stsp if (err)
3153 1ee397ad 2019-07-12 stsp goto done;
3154 2b92fad7 2019-06-02 stsp break;
3155 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3156 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3157 1ee397ad 2019-07-12 stsp if (err)
3158 1ee397ad 2019-07-12 stsp goto done;
3159 2b92fad7 2019-06-02 stsp break;
3160 2b92fad7 2019-06-02 stsp default:
3161 2b92fad7 2019-06-02 stsp break;
3162 2b92fad7 2019-06-02 stsp }
3163 234035bc 2019-06-01 stsp } else if (blob2) {
3164 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3165 234035bc 2019-06-01 stsp path2) == -1)
3166 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3167 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3168 d6c87207 2019-08-02 stsp strlen(path2));
3169 234035bc 2019-06-01 stsp if (ie) {
3170 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3171 7f91a133 2019-12-13 stsp -1, NULL, repo);
3172 234035bc 2019-06-01 stsp if (err)
3173 234035bc 2019-06-01 stsp goto done;
3174 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3175 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3176 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3177 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3178 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3179 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3180 1ee397ad 2019-07-12 stsp status, path2);
3181 234035bc 2019-06-01 stsp goto done;
3182 234035bc 2019-06-01 stsp }
3183 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3184 36bf999c 2020-07-23 stsp char *link_target2;
3185 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3186 36bf999c 2020-07-23 stsp blob2);
3187 36bf999c 2020-07-23 stsp if (err)
3188 36bf999c 2020-07-23 stsp goto done;
3189 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3190 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3191 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3192 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3193 36bf999c 2020-07-23 stsp free(link_target2);
3194 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3195 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3196 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3197 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3198 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3199 526a746f 2020-07-23 stsp a->progress_arg);
3200 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3201 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3202 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3203 526a746f 2020-07-23 stsp }
3204 dfe9fba0 2020-07-23 stsp if (err)
3205 dfe9fba0 2020-07-23 stsp goto done;
3206 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3207 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3208 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3209 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3210 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3211 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3212 234035bc 2019-06-01 stsp if (err)
3213 234035bc 2019-06-01 stsp goto done;
3214 234035bc 2019-06-01 stsp }
3215 234035bc 2019-06-01 stsp } else {
3216 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3217 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3218 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3219 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3220 234035bc 2019-06-01 stsp if (err)
3221 2b92fad7 2019-06-02 stsp goto done;
3222 234035bc 2019-06-01 stsp }
3223 234035bc 2019-06-01 stsp }
3224 234035bc 2019-06-01 stsp done:
3225 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3226 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3227 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3228 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3229 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3230 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3231 1af628f4 2021-06-03 stsp free(id_str);
3232 54d5be07 2021-06-03 stsp free(label_deriv2);
3233 234035bc 2019-06-01 stsp free(ondisk_path);
3234 234035bc 2019-06-01 stsp return err;
3235 234035bc 2019-06-01 stsp }
3236 234035bc 2019-06-01 stsp
3237 69de9dd4 2021-09-03 stsp static const struct got_error *
3238 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3239 69de9dd4 2021-09-03 stsp {
3240 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3241 69de9dd4 2021-09-03 stsp
3242 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3243 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3244 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3245 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3246 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3247 69de9dd4 2021-09-03 stsp
3248 69de9dd4 2021-09-03 stsp return NULL;
3249 69de9dd4 2021-09-03 stsp }
3250 69de9dd4 2021-09-03 stsp
3251 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3252 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3253 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3254 234035bc 2019-06-01 stsp struct got_repository *repo;
3255 234035bc 2019-06-01 stsp };
3256 234035bc 2019-06-01 stsp
3257 234035bc 2019-06-01 stsp static const struct got_error *
3258 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3259 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3260 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3261 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3262 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3263 234035bc 2019-06-01 stsp {
3264 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3265 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3266 234035bc 2019-06-01 stsp unsigned char status;
3267 234035bc 2019-06-01 stsp struct stat sb;
3268 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3269 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3270 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3271 234035bc 2019-06-01 stsp char *ondisk_path;
3272 234035bc 2019-06-01 stsp
3273 69de9dd4 2021-09-03 stsp if (id == NULL)
3274 69de9dd4 2021-09-03 stsp return NULL;
3275 234035bc 2019-06-01 stsp
3276 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3277 69de9dd4 2021-09-03 stsp if (ie == NULL)
3278 69de9dd4 2021-09-03 stsp return NULL;
3279 69de9dd4 2021-09-03 stsp
3280 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3281 234035bc 2019-06-01 stsp == -1)
3282 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3283 234035bc 2019-06-01 stsp
3284 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3285 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3286 5546d466 2021-09-02 stsp free(ondisk_path);
3287 234035bc 2019-06-01 stsp if (err)
3288 234035bc 2019-06-01 stsp return err;
3289 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3290 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3291 234035bc 2019-06-01 stsp
3292 234035bc 2019-06-01 stsp return NULL;
3293 234035bc 2019-06-01 stsp }
3294 234035bc 2019-06-01 stsp
3295 818c7501 2019-07-11 stsp static const struct got_error *
3296 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3297 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3298 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3299 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3300 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3301 234035bc 2019-06-01 stsp {
3302 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3303 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3304 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3306 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3307 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3308 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3309 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3310 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3311 234035bc 2019-06-01 stsp
3312 03415a1a 2019-06-02 stsp if (commit_id1) {
3313 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3314 945f9229 2022-04-16 thomas if (err)
3315 945f9229 2022-04-16 thomas goto done;
3316 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3317 03415a1a 2019-06-02 stsp worktree->path_prefix);
3318 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3319 03415a1a 2019-06-02 stsp goto done;
3320 69d57f3d 2020-07-31 stsp }
3321 69d57f3d 2020-07-31 stsp if (tree_id1) {
3322 69d57f3d 2020-07-31 stsp char *id_str;
3323 03415a1a 2019-06-02 stsp
3324 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3325 03415a1a 2019-06-02 stsp if (err)
3326 03415a1a 2019-06-02 stsp goto done;
3327 f69721c3 2019-10-21 stsp
3328 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3329 f69721c3 2019-10-21 stsp if (err)
3330 f69721c3 2019-10-21 stsp goto done;
3331 f69721c3 2019-10-21 stsp
3332 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3333 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3334 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3335 f69721c3 2019-10-21 stsp free(id_str);
3336 f69721c3 2019-10-21 stsp goto done;
3337 f69721c3 2019-10-21 stsp }
3338 f69721c3 2019-10-21 stsp free(id_str);
3339 a0f32f33 2022-06-13 thomas
3340 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3341 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3342 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3343 a0f32f33 2022-06-13 thomas goto done;
3344 a0f32f33 2022-06-13 thomas }
3345 03415a1a 2019-06-02 stsp }
3346 234035bc 2019-06-01 stsp
3347 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3348 945f9229 2022-04-16 thomas if (err)
3349 945f9229 2022-04-16 thomas goto done;
3350 945f9229 2022-04-16 thomas
3351 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3352 234035bc 2019-06-01 stsp worktree->path_prefix);
3353 234035bc 2019-06-01 stsp if (err)
3354 234035bc 2019-06-01 stsp goto done;
3355 234035bc 2019-06-01 stsp
3356 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3357 234035bc 2019-06-01 stsp if (err)
3358 234035bc 2019-06-01 stsp goto done;
3359 234035bc 2019-06-01 stsp
3360 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3361 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3362 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3363 19a6a6b5 2022-07-01 thomas goto done;
3364 19a6a6b5 2022-07-01 thomas }
3365 19a6a6b5 2022-07-01 thomas
3366 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3367 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3368 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3369 a0f32f33 2022-06-13 thomas goto done;
3370 a0f32f33 2022-06-13 thomas }
3371 a0f32f33 2022-06-13 thomas
3372 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3373 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3374 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3375 19a6a6b5 2022-07-01 thomas goto done;
3376 19a6a6b5 2022-07-01 thomas }
3377 19a6a6b5 2022-07-01 thomas
3378 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3379 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3380 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3381 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3383 69de9dd4 2021-09-03 stsp if (err)
3384 69de9dd4 2021-09-03 stsp goto done;
3385 69de9dd4 2021-09-03 stsp
3386 234035bc 2019-06-01 stsp arg.worktree = worktree;
3387 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3388 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3389 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3390 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3391 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3392 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3393 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3394 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3395 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3396 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3397 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3398 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3399 af12c6b9 2019-06-04 stsp err = sync_err;
3400 234035bc 2019-06-01 stsp done:
3401 945f9229 2022-04-16 thomas if (commit1)
3402 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3403 945f9229 2022-04-16 thomas if (commit2)
3404 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3405 234035bc 2019-06-01 stsp if (tree1)
3406 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3407 234035bc 2019-06-01 stsp if (tree2)
3408 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3409 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3410 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3411 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3412 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3413 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3414 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3415 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3416 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3417 f69721c3 2019-10-21 stsp free(label_orig);
3418 818c7501 2019-07-11 stsp return err;
3419 818c7501 2019-07-11 stsp }
3420 234035bc 2019-06-01 stsp
3421 818c7501 2019-07-11 stsp const struct got_error *
3422 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3423 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3424 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3425 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3426 818c7501 2019-07-11 stsp {
3427 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3428 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3429 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3430 818c7501 2019-07-11 stsp
3431 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3432 818c7501 2019-07-11 stsp if (err)
3433 818c7501 2019-07-11 stsp return err;
3434 818c7501 2019-07-11 stsp
3435 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3436 818c7501 2019-07-11 stsp if (err)
3437 818c7501 2019-07-11 stsp goto done;
3438 818c7501 2019-07-11 stsp
3439 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3440 69de9dd4 2021-09-03 stsp worktree);
3441 818c7501 2019-07-11 stsp if (err)
3442 818c7501 2019-07-11 stsp goto done;
3443 818c7501 2019-07-11 stsp
3444 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3445 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3446 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3447 818c7501 2019-07-11 stsp done:
3448 818c7501 2019-07-11 stsp if (fileindex)
3449 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3450 818c7501 2019-07-11 stsp free(fileindex_path);
3451 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3452 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3453 234035bc 2019-06-01 stsp err = unlockerr;
3454 234035bc 2019-06-01 stsp return err;
3455 234035bc 2019-06-01 stsp }
3456 234035bc 2019-06-01 stsp
3457 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3458 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3459 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3460 927df6b7 2019-02-10 stsp const char *status_path;
3461 927df6b7 2019-02-10 stsp size_t status_path_len;
3462 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3463 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3464 f8d1f275 2019-02-04 stsp void *status_arg;
3465 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3466 f8d1f275 2019-02-04 stsp void *cancel_arg;
3467 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3468 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3469 f2a9dc41 2019-12-13 tracey int report_unchanged;
3470 3143d852 2020-06-25 stsp int no_ignores;
3471 f8d1f275 2019-02-04 stsp };
3472 88d0e355 2019-08-03 stsp
3473 f8d1f275 2019-02-04 stsp static const struct got_error *
3474 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3475 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3476 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3477 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3478 927df6b7 2019-02-10 stsp {
3479 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3480 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3481 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3482 927df6b7 2019-02-10 stsp struct stat sb;
3483 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3484 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3485 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3486 927df6b7 2019-02-10 stsp
3487 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3488 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3489 98eaaa12 2019-08-03 stsp if (err)
3490 98eaaa12 2019-08-03 stsp return err;
3491 98eaaa12 2019-08-03 stsp
3492 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3493 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3494 98eaaa12 2019-08-03 stsp return NULL;
3495 98eaaa12 2019-08-03 stsp
3496 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3497 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3498 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3499 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3500 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3501 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3502 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3503 43010591 2023-02-17 thomas &staged_blob_id, ie);
3504 98eaaa12 2019-08-03 stsp }
3505 98eaaa12 2019-08-03 stsp
3506 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3507 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3508 927df6b7 2019-02-10 stsp }
3509 927df6b7 2019-02-10 stsp
3510 927df6b7 2019-02-10 stsp static const struct got_error *
3511 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3512 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3513 f8d1f275 2019-02-04 stsp {
3514 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3515 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3516 f8d1f275 2019-02-04 stsp char *abspath;
3517 f8d1f275 2019-02-04 stsp
3518 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3519 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3520 0584f854 2019-04-06 stsp
3521 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3522 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3523 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3524 927df6b7 2019-02-10 stsp return NULL;
3525 927df6b7 2019-02-10 stsp
3526 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3527 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3528 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3529 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3530 f8d1f275 2019-02-04 stsp } else {
3531 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3532 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3533 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3534 f8d1f275 2019-02-04 stsp }
3535 f8d1f275 2019-02-04 stsp
3536 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3537 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3538 f8d1f275 2019-02-04 stsp free(abspath);
3539 9d31a1d8 2018-03-11 stsp return err;
3540 9d31a1d8 2018-03-11 stsp }
3541 f8d1f275 2019-02-04 stsp
3542 f8d1f275 2019-02-04 stsp static const struct got_error *
3543 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3544 f8d1f275 2019-02-04 stsp {
3545 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3546 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3547 2ec1f75b 2019-03-26 stsp unsigned char status;
3548 927df6b7 2019-02-10 stsp
3549 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3550 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3551 0584f854 2019-04-06 stsp
3552 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3553 927df6b7 2019-02-10 stsp return NULL;
3554 927df6b7 2019-02-10 stsp
3555 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3556 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3557 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3558 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3559 2ec1f75b 2019-03-26 stsp else
3560 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3561 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3562 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3563 6841da00 2019-08-08 stsp }
3564 6841da00 2019-08-08 stsp
3565 ef20f542 2022-06-26 thomas static void
3566 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3567 6841da00 2019-08-08 stsp {
3568 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3569 6841da00 2019-08-08 stsp
3570 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3571 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3572 21c2d8be 2023-01-10 thomas
3573 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3574 6841da00 2019-08-08 stsp }
3575 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3576 6841da00 2019-08-08 stsp }
3577 6841da00 2019-08-08 stsp
3578 6841da00 2019-08-08 stsp static const struct got_error *
3579 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3580 6841da00 2019-08-08 stsp {
3581 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3582 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3583 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3584 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3585 6841da00 2019-08-08 stsp size_t linesize = 0;
3586 6841da00 2019-08-08 stsp ssize_t linelen;
3587 6841da00 2019-08-08 stsp
3588 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3589 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3590 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3591 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3592 6841da00 2019-08-08 stsp
3593 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3594 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3595 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3596 bd8de430 2019-10-04 stsp
3597 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3598 bd8de430 2019-10-04 stsp if (line[0] == '#')
3599 bd8de430 2019-10-04 stsp continue;
3600 bd8de430 2019-10-04 stsp
3601 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3602 bd8de430 2019-10-04 stsp if (line[0] == '!')
3603 bd8de430 2019-10-04 stsp continue;
3604 bd8de430 2019-10-04 stsp
3605 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3606 6841da00 2019-08-08 stsp line) == -1) {
3607 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3608 6841da00 2019-08-08 stsp goto done;
3609 6841da00 2019-08-08 stsp }
3610 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3611 6841da00 2019-08-08 stsp if (err)
3612 6841da00 2019-08-08 stsp goto done;
3613 6841da00 2019-08-08 stsp }
3614 6841da00 2019-08-08 stsp if (ferror(f)) {
3615 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3616 6841da00 2019-08-08 stsp goto done;
3617 6841da00 2019-08-08 stsp }
3618 6841da00 2019-08-08 stsp
3619 6841da00 2019-08-08 stsp dirpath = strdup(path);
3620 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3621 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3622 6841da00 2019-08-08 stsp goto done;
3623 6841da00 2019-08-08 stsp }
3624 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3625 6841da00 2019-08-08 stsp done:
3626 6841da00 2019-08-08 stsp free(line);
3627 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3628 6841da00 2019-08-08 stsp free(dirpath);
3629 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3630 6841da00 2019-08-08 stsp }
3631 6841da00 2019-08-08 stsp return err;
3632 1b5d300f 2023-02-20 thomas }
3633 1b5d300f 2023-02-20 thomas
3634 1b5d300f 2023-02-20 thomas static int
3635 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3636 1b5d300f 2023-02-20 thomas int flags)
3637 1b5d300f 2023-02-20 thomas {
3638 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3639 1b5d300f 2023-02-20 thomas
3640 1b5d300f 2023-02-20 thomas /*
3641 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3642 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3643 1b5d300f 2023-02-20 thomas */
3644 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3645 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3646 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3647 1b5d300f 2023-02-20 thomas
3648 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3649 1b5d300f 2023-02-20 thomas }
3650 1b5d300f 2023-02-20 thomas
3651 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3652 6841da00 2019-08-08 stsp }
3653 6841da00 2019-08-08 stsp
3654 ef20f542 2022-06-26 thomas static int
3655 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3656 6841da00 2019-08-08 stsp {
3657 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3658 bd8de430 2019-10-04 stsp
3659 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3660 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3661 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3662 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3663 bd8de430 2019-10-04 stsp
3664 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3665 1b5d300f 2023-02-20 thomas const char *p;
3666 6841da00 2019-08-08 stsp
3667 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3668 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3669 bd8de430 2019-10-04 stsp continue;
3670 bd8de430 2019-10-04 stsp p = path;
3671 bd8de430 2019-10-04 stsp while (*p) {
3672 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3673 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3674 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3675 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3676 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3677 bd8de430 2019-10-04 stsp p++;
3678 bd8de430 2019-10-04 stsp while (*p == '/')
3679 bd8de430 2019-10-04 stsp p++;
3680 bd8de430 2019-10-04 stsp continue;
3681 bd8de430 2019-10-04 stsp }
3682 bd8de430 2019-10-04 stsp return 1;
3683 bd8de430 2019-10-04 stsp }
3684 bd8de430 2019-10-04 stsp }
3685 bd8de430 2019-10-04 stsp }
3686 bd8de430 2019-10-04 stsp
3687 6841da00 2019-08-08 stsp /*
3688 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3689 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3690 6841da00 2019-08-08 stsp * ignores backwards.
3691 6841da00 2019-08-08 stsp */
3692 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3693 6841da00 2019-08-08 stsp while (pe) {
3694 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3695 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3696 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3697 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3698 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3699 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3700 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3701 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3702 1b5d300f 2023-02-20 thomas path, flags))
3703 6841da00 2019-08-08 stsp continue;
3704 6841da00 2019-08-08 stsp return 1;
3705 6841da00 2019-08-08 stsp }
3706 6841da00 2019-08-08 stsp }
3707 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3708 6841da00 2019-08-08 stsp }
3709 6841da00 2019-08-08 stsp
3710 6841da00 2019-08-08 stsp return 0;
3711 6841da00 2019-08-08 stsp }
3712 6841da00 2019-08-08 stsp
3713 6841da00 2019-08-08 stsp static const struct got_error *
3714 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3715 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3716 6841da00 2019-08-08 stsp {
3717 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3718 6841da00 2019-08-08 stsp char *ignorespath;
3719 886cec17 2019-12-15 stsp int fd = -1;
3720 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3721 6841da00 2019-08-08 stsp
3722 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3723 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3724 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3725 6841da00 2019-08-08 stsp
3726 886cec17 2019-12-15 stsp if (dirfd != -1) {
3727 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3728 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3729 886cec17 2019-12-15 stsp if (fd == -1) {
3730 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3731 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3732 886cec17 2019-12-15 stsp ignorespath);
3733 886cec17 2019-12-15 stsp } else {
3734 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3735 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3736 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3737 886cec17 2019-12-15 stsp ignorespath);
3738 886cec17 2019-12-15 stsp else {
3739 886cec17 2019-12-15 stsp fd = -1;
3740 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3741 886cec17 2019-12-15 stsp }
3742 886cec17 2019-12-15 stsp }
3743 886cec17 2019-12-15 stsp } else {
3744 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3745 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3746 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3747 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3748 886cec17 2019-12-15 stsp ignorespath);
3749 886cec17 2019-12-15 stsp } else
3750 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3751 886cec17 2019-12-15 stsp }
3752 6841da00 2019-08-08 stsp
3753 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3754 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3755 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3756 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3757 6841da00 2019-08-08 stsp free(ignorespath);
3758 6841da00 2019-08-08 stsp return err;
3759 f8d1f275 2019-02-04 stsp }
3760 f8d1f275 2019-02-04 stsp
3761 f8d1f275 2019-02-04 stsp static const struct got_error *
3762 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3763 6092c299 2021-10-04 thomas int dirfd)
3764 f8d1f275 2019-02-04 stsp {
3765 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3766 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3767 f8d1f275 2019-02-04 stsp char *path = NULL;
3768 6092c299 2021-10-04 thomas
3769 6092c299 2021-10-04 thomas if (ignore != NULL)
3770 6092c299 2021-10-04 thomas *ignore = 0;
3771 f8d1f275 2019-02-04 stsp
3772 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3773 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3774 0584f854 2019-04-06 stsp
3775 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3776 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3777 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3778 f8d1f275 2019-02-04 stsp } else {
3779 f8d1f275 2019-02-04 stsp path = de->d_name;
3780 f8d1f275 2019-02-04 stsp }
3781 f8d1f275 2019-02-04 stsp
3782 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3783 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3784 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3785 6092c299 2021-10-04 thomas *ignore = 1;
3786 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3787 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3788 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3789 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3790 f8d1f275 2019-02-04 stsp if (parent_path[0])
3791 f8d1f275 2019-02-04 stsp free(path);
3792 b72f483a 2019-02-05 stsp return err;
3793 f8d1f275 2019-02-04 stsp }
3794 f8d1f275 2019-02-04 stsp
3795 347d1d3e 2019-07-12 stsp static const struct got_error *
3796 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3797 3143d852 2020-06-25 stsp {
3798 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3799 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3800 3143d852 2020-06-25 stsp
3801 3143d852 2020-06-25 stsp if (a->no_ignores)
3802 3143d852 2020-06-25 stsp return NULL;
3803 3143d852 2020-06-25 stsp
3804 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3805 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3806 3143d852 2020-06-25 stsp if (err)
3807 3143d852 2020-06-25 stsp return err;
3808 3143d852 2020-06-25 stsp
3809 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3810 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3811 3143d852 2020-06-25 stsp
3812 3143d852 2020-06-25 stsp return err;
3813 3143d852 2020-06-25 stsp }
3814 3143d852 2020-06-25 stsp
3815 3143d852 2020-06-25 stsp static const struct got_error *
3816 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3817 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3818 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3819 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3820 abb4604f 2019-07-27 stsp {
3821 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3822 abb4604f 2019-07-27 stsp struct stat sb;
3823 ff56836b 2021-07-08 stsp
3824 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3825 abb4604f 2019-07-27 stsp if (ie)
3826 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3827 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3828 abb4604f 2019-07-27 stsp
3829 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3830 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3831 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3832 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3833 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3834 abb4604f 2019-07-27 stsp }
3835 abb4604f 2019-07-27 stsp
3836 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3837 fe1d8685 2022-02-12 thomas return NULL;
3838 fe1d8685 2022-02-12 thomas
3839 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3840 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3841 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3842 abb4604f 2019-07-27 stsp
3843 abb4604f 2019-07-27 stsp return NULL;
3844 3143d852 2020-06-25 stsp }
3845 3143d852 2020-06-25 stsp
3846 3143d852 2020-06-25 stsp static const struct got_error *
3847 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3848 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3849 3143d852 2020-06-25 stsp {
3850 3143d852 2020-06-25 stsp const struct got_error *err;
3851 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3852 3143d852 2020-06-25 stsp
3853 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3854 3143d852 2020-06-25 stsp ".cvsignore");
3855 3143d852 2020-06-25 stsp if (err)
3856 3143d852 2020-06-25 stsp return err;
3857 3143d852 2020-06-25 stsp
3858 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3859 3143d852 2020-06-25 stsp ".gitignore");
3860 3143d852 2020-06-25 stsp if (err)
3861 3143d852 2020-06-25 stsp return err;
3862 3143d852 2020-06-25 stsp
3863 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3864 3143d852 2020-06-25 stsp if (err) {
3865 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3866 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3867 3143d852 2020-06-25 stsp return err;
3868 3143d852 2020-06-25 stsp }
3869 3143d852 2020-06-25 stsp for (;;) {
3870 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3871 3143d852 2020-06-25 stsp ".cvsignore");
3872 3143d852 2020-06-25 stsp if (err)
3873 3143d852 2020-06-25 stsp break;
3874 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3875 3143d852 2020-06-25 stsp ".gitignore");
3876 3143d852 2020-06-25 stsp if (err)
3877 3143d852 2020-06-25 stsp break;
3878 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3879 3143d852 2020-06-25 stsp if (err) {
3880 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3881 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3882 3143d852 2020-06-25 stsp break;
3883 3143d852 2020-06-25 stsp }
3884 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3885 ff56836b 2021-07-08 stsp break;
3886 b737c85a 2020-06-26 stsp free(parent_path);
3887 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3888 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3889 3143d852 2020-06-25 stsp }
3890 3143d852 2020-06-25 stsp
3891 b737c85a 2020-06-26 stsp free(parent_path);
3892 b737c85a 2020-06-26 stsp free(next_parent_path);
3893 3143d852 2020-06-25 stsp return err;
3894 abb4604f 2019-07-27 stsp }
3895 f8836425 2023-04-14 thomas
3896 f8836425 2023-04-14 thomas struct find_missing_children_args {
3897 f8836425 2023-04-14 thomas const char *parent_path;
3898 f8836425 2023-04-14 thomas size_t parent_len;
3899 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3900 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3901 f8836425 2023-04-14 thomas void *cancel_arg;
3902 f8836425 2023-04-14 thomas };
3903 f8836425 2023-04-14 thomas
3904 abb4604f 2019-07-27 stsp static const struct got_error *
3905 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3906 f8836425 2023-04-14 thomas {
3907 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3908 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3909 f8836425 2023-04-14 thomas
3910 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3911 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3912 f8836425 2023-04-14 thomas if (err)
3913 f8836425 2023-04-14 thomas return err;
3914 f8836425 2023-04-14 thomas }
3915 f8836425 2023-04-14 thomas
3916 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3917 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3918 f8836425 2023-04-14 thomas
3919 f8836425 2023-04-14 thomas return err;
3920 f8836425 2023-04-14 thomas }
3921 f8836425 2023-04-14 thomas
3922 f8836425 2023-04-14 thomas static const struct got_error *
3923 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3924 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3925 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3926 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3927 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3928 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3929 f8836425 2023-04-14 thomas {
3930 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3931 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3932 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3933 f8836425 2023-04-14 thomas
3934 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3935 f8836425 2023-04-14 thomas if (cancel_cb) {
3936 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3937 f8836425 2023-04-14 thomas if (err)
3938 f8836425 2023-04-14 thomas break;
3939 f8836425 2023-04-14 thomas }
3940 f8836425 2023-04-14 thomas
3941 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3942 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3943 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3944 f8836425 2023-04-14 thomas ondisk_path = NULL;
3945 f8836425 2023-04-14 thomas break;
3946 f8836425 2023-04-14 thomas }
3947 f8836425 2023-04-14 thomas
3948 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3949 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3950 f8836425 2023-04-14 thomas ignores, no_ignores);
3951 f8836425 2023-04-14 thomas if (err)
3952 f8836425 2023-04-14 thomas break;
3953 f8836425 2023-04-14 thomas
3954 f8836425 2023-04-14 thomas free(ondisk_path);
3955 f8836425 2023-04-14 thomas ondisk_path = NULL;
3956 f8836425 2023-04-14 thomas }
3957 f8836425 2023-04-14 thomas
3958 f8836425 2023-04-14 thomas free(ondisk_path);
3959 f8836425 2023-04-14 thomas return err;
3960 f8836425 2023-04-14 thomas }
3961 f8836425 2023-04-14 thomas
3962 f8836425 2023-04-14 thomas static const struct got_error *
3963 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3964 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3965 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3966 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3967 f2a9dc41 2019-12-13 tracey int report_unchanged)
3968 f8d1f275 2019-02-04 stsp {
3969 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3970 6fc93f37 2019-12-13 stsp int fd = -1;
3971 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3972 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3973 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3974 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3975 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3976 3143d852 2020-06-25 stsp
3977 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3978 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3979 f8d1f275 2019-02-04 stsp
3980 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3981 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3982 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3983 8dc303cc 2019-07-27 stsp
3984 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3985 a78810f8 2022-03-13 thomas if (ie) {
3986 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3987 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3988 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3989 a78810f8 2022-03-13 thomas goto done;
3990 f8836425 2023-04-14 thomas } else {
3991 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3992 f8836425 2023-04-14 thomas fmca.parent_path = path;
3993 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3994 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3995 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3996 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3997 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3998 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3999 f8836425 2023-04-14 thomas if (err)
4000 f8836425 2023-04-14 thomas goto done;
4001 a78810f8 2022-03-13 thomas }
4002 a78810f8 2022-03-13 thomas
4003 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4004 6fc93f37 2019-12-13 stsp if (fd == -1) {
4005 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4006 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4007 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4008 ff56836b 2021-07-08 stsp else {
4009 ff56836b 2021-07-08 stsp if (!no_ignores) {
4010 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4011 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4012 ff56836b 2021-07-08 stsp if (err)
4013 ff56836b 2021-07-08 stsp goto done;
4014 ff56836b 2021-07-08 stsp }
4015 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4016 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4017 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4018 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4019 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4020 f8836425 2023-04-14 thomas if (err)
4021 f8836425 2023-04-14 thomas goto done;
4022 f8836425 2023-04-14 thomas } else {
4023 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4024 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4025 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4026 f8836425 2023-04-14 thomas &ignores, no_ignores,
4027 f8836425 2023-04-14 thomas status_cb, status_arg,
4028 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4029 6f97aa83 2023-04-14 thomas if (err)
4030 6f97aa83 2023-04-14 thomas goto done;
4031 f8836425 2023-04-14 thomas }
4032 ff56836b 2021-07-08 stsp }
4033 abb4604f 2019-07-27 stsp } else {
4034 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4035 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4036 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4037 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4038 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4039 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4040 abb4604f 2019-07-27 stsp arg.status_path = path;
4041 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4042 abb4604f 2019-07-27 stsp arg.repo = repo;
4043 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4044 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4045 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4046 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4047 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4048 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4049 022fae89 2019-12-06 tracey if (!no_ignores) {
4050 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4051 3143d852 2020-06-25 stsp worktree->root_path, path);
4052 3143d852 2020-06-25 stsp if (err)
4053 3143d852 2020-06-25 stsp goto done;
4054 022fae89 2019-12-06 tracey }
4055 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4056 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4057 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4058 927df6b7 2019-02-10 stsp }
4059 3143d852 2020-06-25 stsp done:
4060 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4061 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4062 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4063 927df6b7 2019-02-10 stsp free(ondisk_path);
4064 347d1d3e 2019-07-12 stsp return err;
4065 347d1d3e 2019-07-12 stsp }
4066 347d1d3e 2019-07-12 stsp
4067 347d1d3e 2019-07-12 stsp const struct got_error *
4068 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4069 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4070 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4071 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4072 347d1d3e 2019-07-12 stsp {
4073 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4074 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4075 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4076 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4077 347d1d3e 2019-07-12 stsp
4078 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4079 347d1d3e 2019-07-12 stsp if (err)
4080 347d1d3e 2019-07-12 stsp return err;
4081 347d1d3e 2019-07-12 stsp
4082 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4083 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4084 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4085 f6343036 2021-06-22 stsp no_ignores, 0);
4086 72ea6654 2019-07-27 stsp if (err)
4087 72ea6654 2019-07-27 stsp break;
4088 72ea6654 2019-07-27 stsp }
4089 f8d1f275 2019-02-04 stsp free(fileindex_path);
4090 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4091 f8d1f275 2019-02-04 stsp return err;
4092 f8d1f275 2019-02-04 stsp }
4093 6c7ab921 2019-03-18 stsp
4094 6c7ab921 2019-03-18 stsp const struct got_error *
4095 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4096 6c7ab921 2019-03-18 stsp const char *arg)
4097 6c7ab921 2019-03-18 stsp {
4098 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4099 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4100 6c7ab921 2019-03-18 stsp size_t len;
4101 00bb5ea0 2020-07-23 stsp struct stat sb;
4102 01740607 2020-11-04 stsp char *abspath = NULL;
4103 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4104 6c7ab921 2019-03-18 stsp
4105 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4106 6c7ab921 2019-03-18 stsp
4107 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4108 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4109 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4110 00bb5ea0 2020-07-23 stsp
4111 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4112 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4113 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4114 00bb5ea0 2020-07-23 stsp goto done;
4115 00bb5ea0 2020-07-23 stsp }
4116 727173c3 2020-11-06 stsp sb.st_mode = 0;
4117 00bb5ea0 2020-07-23 stsp }
4118 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4119 00bb5ea0 2020-07-23 stsp /*
4120 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4121 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4122 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4123 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4124 00bb5ea0 2020-07-23 stsp */
4125 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4126 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4127 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4128 00bb5ea0 2020-07-23 stsp goto done;
4129 00bb5ea0 2020-07-23 stsp }
4130 00bb5ea0 2020-07-23 stsp
4131 00bb5ea0 2020-07-23 stsp }
4132 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4133 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4134 00bb5ea0 2020-07-23 stsp if (err)
4135 d0710d08 2019-07-22 stsp goto done;
4136 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4137 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4138 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4139 00bb5ea0 2020-07-23 stsp goto done;
4140 00bb5ea0 2020-07-23 stsp }
4141 00bb5ea0 2020-07-23 stsp } else {
4142 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4143 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4144 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4145 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4146 00bb5ea0 2020-07-23 stsp goto done;
4147 00bb5ea0 2020-07-23 stsp }
4148 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4149 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4150 01740607 2020-11-04 stsp goto done;
4151 01740607 2020-11-04 stsp }
4152 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4153 01740607 2020-11-04 stsp sizeof(canonpath));
4154 01740607 2020-11-04 stsp if (err)
4155 00bb5ea0 2020-07-23 stsp goto done;
4156 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4157 01740607 2020-11-04 stsp if (resolved == NULL) {
4158 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4159 01740607 2020-11-04 stsp goto done;
4160 00bb5ea0 2020-07-23 stsp }
4161 d0710d08 2019-07-22 stsp }
4162 d0710d08 2019-07-22 stsp }
4163 6c7ab921 2019-03-18 stsp
4164 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4165 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4166 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4167 6c7ab921 2019-03-18 stsp goto done;
4168 6c7ab921 2019-03-18 stsp }
4169 6c7ab921 2019-03-18 stsp
4170 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4171 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4172 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4173 f6d88e1a 2019-05-29 stsp if (err)
4174 f6d88e1a 2019-05-29 stsp goto done;
4175 f6d88e1a 2019-05-29 stsp } else {
4176 f6d88e1a 2019-05-29 stsp path = strdup("");
4177 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4178 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4179 f6d88e1a 2019-05-29 stsp goto done;
4180 f6d88e1a 2019-05-29 stsp }
4181 6c7ab921 2019-03-18 stsp }
4182 6c7ab921 2019-03-18 stsp
4183 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4184 6c7ab921 2019-03-18 stsp len = strlen(path);
4185 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4186 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4187 6c7ab921 2019-03-18 stsp len--;
4188 6c7ab921 2019-03-18 stsp }
4189 6c7ab921 2019-03-18 stsp done:
4190 01740607 2020-11-04 stsp free(abspath);
4191 6c7ab921 2019-03-18 stsp free(resolved);
4192 d0710d08 2019-07-22 stsp free(cwd);
4193 6c7ab921 2019-03-18 stsp if (err == NULL)
4194 6c7ab921 2019-03-18 stsp *wt_path = path;
4195 6c7ab921 2019-03-18 stsp else
4196 6c7ab921 2019-03-18 stsp free(path);
4197 d00136be 2019-03-26 stsp return err;
4198 d00136be 2019-03-26 stsp }
4199 d00136be 2019-03-26 stsp
4200 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4201 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4202 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4203 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4204 4e68cba3 2019-11-23 stsp void *progress_arg;
4205 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4206 4e68cba3 2019-11-23 stsp };
4207 4e68cba3 2019-11-23 stsp
4208 b21ebdb0 2023-06-22 thomas static int
4209 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4210 b21ebdb0 2023-06-22 thomas {
4211 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4212 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4213 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4214 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4215 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4216 b21ebdb0 2023-06-22 thomas }
4217 b21ebdb0 2023-06-22 thomas
4218 4e68cba3 2019-11-23 stsp static const struct got_error *
4219 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4220 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4221 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4222 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4223 07f5b47a 2019-06-02 stsp {
4224 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4225 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4226 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4227 6d022e97 2019-08-04 stsp struct stat sb;
4228 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4229 07f5b47a 2019-06-02 stsp
4230 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4231 dbb83fbd 2019-12-12 stsp relpath) == -1)
4232 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4233 dbb83fbd 2019-12-12 stsp
4234 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4235 6d022e97 2019-08-04 stsp if (ie) {
4236 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4237 12463d8b 2019-12-13 stsp de_name, a->repo);
4238 6d022e97 2019-08-04 stsp if (err)
4239 dbb83fbd 2019-12-12 stsp goto done;
4240 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4241 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4242 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4243 dbb83fbd 2019-12-12 stsp goto done;
4244 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4245 dbb83fbd 2019-12-12 stsp if (err)
4246 dbb83fbd 2019-12-12 stsp goto done;
4247 6d022e97 2019-08-04 stsp }
4248 07f5b47a 2019-06-02 stsp
4249 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4250 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4251 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4252 84bf00a6 2022-02-12 thomas else
4253 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4254 dbb83fbd 2019-12-12 stsp goto done;
4255 4e68cba3 2019-11-23 stsp }
4256 07f5b47a 2019-06-02 stsp
4257 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4258 4e68cba3 2019-11-23 stsp if (err)
4259 4e68cba3 2019-11-23 stsp goto done;
4260 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4261 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4262 3969253a 2020-03-07 stsp if (err) {
4263 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4264 3969253a 2020-03-07 stsp goto done;
4265 3969253a 2020-03-07 stsp }
4266 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4267 07f5b47a 2019-06-02 stsp if (err) {
4268 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4269 4e68cba3 2019-11-23 stsp goto done;
4270 07f5b47a 2019-06-02 stsp }
4271 4e68cba3 2019-11-23 stsp done:
4272 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4273 dbb83fbd 2019-12-12 stsp if (err)
4274 dbb83fbd 2019-12-12 stsp return err;
4275 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4276 dbb83fbd 2019-12-12 stsp return NULL;
4277 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4278 07f5b47a 2019-06-02 stsp }
4279 07f5b47a 2019-06-02 stsp
4280 d00136be 2019-03-26 stsp const struct got_error *
4281 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4282 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4283 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4284 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4285 d00136be 2019-03-26 stsp {
4286 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4287 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4288 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4289 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4290 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4291 d00136be 2019-03-26 stsp
4292 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4293 d00136be 2019-03-26 stsp if (err)
4294 d00136be 2019-03-26 stsp return err;
4295 d00136be 2019-03-26 stsp
4296 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4297 d00136be 2019-03-26 stsp if (err)
4298 d00136be 2019-03-26 stsp goto done;
4299 d00136be 2019-03-26 stsp
4300 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4301 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4302 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4303 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4304 4e68cba3 2019-11-23 stsp saa.repo = repo;
4305 4e68cba3 2019-11-23 stsp
4306 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4307 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4308 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4309 1dd54920 2019-05-11 stsp if (err)
4310 af12c6b9 2019-06-04 stsp break;
4311 1dd54920 2019-05-11 stsp }
4312 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4313 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4314 af12c6b9 2019-06-04 stsp err = sync_err;
4315 d00136be 2019-03-26 stsp done:
4316 fb399478 2019-07-12 stsp free(fileindex_path);
4317 d00136be 2019-03-26 stsp if (fileindex)
4318 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4319 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4320 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4321 d00136be 2019-03-26 stsp err = unlockerr;
4322 6c7ab921 2019-03-18 stsp return err;
4323 6c7ab921 2019-03-18 stsp }
4324 17ed4618 2019-06-02 stsp
4325 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4326 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4327 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4328 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4329 f2a9dc41 2019-12-13 tracey void *progress_arg;
4330 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4331 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4332 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4333 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4334 8ec7352a 2023-06-01 thomas const char *status_path;
4335 8ec7352a 2023-06-01 thomas size_t status_path_len;
4336 766841c2 2020-08-13 stsp const char *status_codes;
4337 f2a9dc41 2019-12-13 tracey };
4338 f2a9dc41 2019-12-13 tracey
4339 f2a9dc41 2019-12-13 tracey static const struct got_error *
4340 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4341 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4342 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4343 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4344 17ed4618 2019-06-02 stsp {
4345 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4346 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4347 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4348 17ed4618 2019-06-02 stsp struct stat sb;
4349 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4350 d64cc78a 2022-01-25 thomas
4351 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4352 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4353 d64cc78a 2022-01-25 thomas return NULL;
4354 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4355 d64cc78a 2022-01-25 thomas }
4356 17ed4618 2019-06-02 stsp
4357 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4358 17ed4618 2019-06-02 stsp if (ie == NULL)
4359 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4360 2ec1f75b 2019-03-26 stsp
4361 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4362 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4363 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4364 9acbc4fa 2019-08-03 stsp return NULL;
4365 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4366 9acbc4fa 2019-08-03 stsp }
4367 9acbc4fa 2019-08-03 stsp
4368 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4369 f2a9dc41 2019-12-13 tracey relpath) == -1)
4370 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4371 f2a9dc41 2019-12-13 tracey
4372 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4373 12463d8b 2019-12-13 stsp a->repo);
4374 17ed4618 2019-06-02 stsp if (err)
4375 f2a9dc41 2019-12-13 tracey goto done;
4376 17ed4618 2019-06-02 stsp
4377 766841c2 2020-08-13 stsp if (a->status_codes) {
4378 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4379 766841c2 2020-08-13 stsp int i;
4380 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4381 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4382 766841c2 2020-08-13 stsp break;
4383 766841c2 2020-08-13 stsp }
4384 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4385 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4386 766841c2 2020-08-13 stsp free(ondisk_path);
4387 766841c2 2020-08-13 stsp return NULL;
4388 766841c2 2020-08-13 stsp }
4389 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4390 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4391 766841c2 2020-08-13 stsp static char msg[64];
4392 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4393 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4394 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4395 766841c2 2020-08-13 stsp goto done;
4396 766841c2 2020-08-13 stsp }
4397 766841c2 2020-08-13 stsp }
4398 766841c2 2020-08-13 stsp
4399 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4400 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4401 f2a9dc41 2019-12-13 tracey goto done;
4402 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4403 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4404 f2a9dc41 2019-12-13 tracey goto done;
4405 f2a9dc41 2019-12-13 tracey }
4406 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4407 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4408 d64cc78a 2022-01-25 thomas goto done;
4409 d64cc78a 2022-01-25 thomas }
4410 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4411 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4412 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4413 f2a9dc41 2019-12-13 tracey goto done;
4414 f2a9dc41 2019-12-13 tracey }
4415 17ed4618 2019-06-02 stsp }
4416 17ed4618 2019-06-02 stsp
4417 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4418 20a2ad1c 2020-10-20 stsp size_t root_len;
4419 20a2ad1c 2020-10-20 stsp
4420 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4421 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4422 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4423 12463d8b 2019-12-13 stsp ondisk_path);
4424 12463d8b 2019-12-13 stsp goto done;
4425 12463d8b 2019-12-13 stsp }
4426 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4427 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4428 12463d8b 2019-12-13 stsp goto done;
4429 15341bfd 2020-03-05 tracey }
4430 15341bfd 2020-03-05 tracey
4431 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4432 20a2ad1c 2020-10-20 stsp do {
4433 20a2ad1c 2020-10-20 stsp char *parent;
4434 8ec7352a 2023-06-01 thomas
4435 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4436 20a2ad1c 2020-10-20 stsp if (err)
4437 2513f20a 2020-10-20 stsp goto done;
4438 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4439 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4440 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4441 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4442 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4443 8ec7352a 2023-06-01 thomas a->status_path_len))
4444 8ec7352a 2023-06-01 thomas break;
4445 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4446 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4447 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4448 20a2ad1c 2020-10-20 stsp ondisk_path);
4449 15341bfd 2020-03-05 tracey break;
4450 15341bfd 2020-03-05 tracey }
4451 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4452 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4453 f2a9dc41 2019-12-13 tracey }
4454 17ed4618 2019-06-02 stsp
4455 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4456 f2a9dc41 2019-12-13 tracey done:
4457 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4458 f2a9dc41 2019-12-13 tracey if (err)
4459 f2a9dc41 2019-12-13 tracey return err;
4460 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4461 f2a9dc41 2019-12-13 tracey return NULL;
4462 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4463 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4464 17ed4618 2019-06-02 stsp }
4465 17ed4618 2019-06-02 stsp
4466 2ec1f75b 2019-03-26 stsp const struct got_error *
4467 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4468 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4469 766841c2 2020-08-13 stsp const char *status_codes,
4470 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4471 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4472 2ec1f75b 2019-03-26 stsp {
4473 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4474 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4475 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4476 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4477 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4478 2ec1f75b 2019-03-26 stsp
4479 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4480 2ec1f75b 2019-03-26 stsp if (err)
4481 2ec1f75b 2019-03-26 stsp return err;
4482 2ec1f75b 2019-03-26 stsp
4483 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4484 2ec1f75b 2019-03-26 stsp if (err)
4485 2ec1f75b 2019-03-26 stsp goto done;
4486 f2a9dc41 2019-12-13 tracey
4487 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4488 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4489 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4490 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4491 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4492 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4493 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4494 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4495 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4496 2ec1f75b 2019-03-26 stsp
4497 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4498 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4499 8ec7352a 2023-06-01 thomas
4500 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4501 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4502 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4503 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4504 8ec7352a 2023-06-01 thomas goto done;
4505 8ec7352a 2023-06-01 thomas }
4506 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4507 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4508 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4509 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4510 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4511 17ed4618 2019-06-02 stsp if (err)
4512 af12c6b9 2019-06-04 stsp break;
4513 2ec1f75b 2019-03-26 stsp }
4514 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4515 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4516 af12c6b9 2019-06-04 stsp err = sync_err;
4517 2ec1f75b 2019-03-26 stsp done:
4518 fb399478 2019-07-12 stsp free(fileindex_path);
4519 2ec1f75b 2019-03-26 stsp if (fileindex)
4520 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4521 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4522 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4523 2ec1f75b 2019-03-26 stsp err = unlockerr;
4524 33aa809d 2019-08-08 stsp return err;
4525 33aa809d 2019-08-08 stsp }
4526 33aa809d 2019-08-08 stsp
4527 33aa809d 2019-08-08 stsp static const struct got_error *
4528 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4529 33aa809d 2019-08-08 stsp {
4530 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4531 33aa809d 2019-08-08 stsp char *line = NULL;
4532 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4533 33aa809d 2019-08-08 stsp ssize_t linelen;
4534 33aa809d 2019-08-08 stsp
4535 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4536 33aa809d 2019-08-08 stsp if (linelen == -1) {
4537 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4538 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4539 33aa809d 2019-08-08 stsp goto done;
4540 33aa809d 2019-08-08 stsp }
4541 33aa809d 2019-08-08 stsp return NULL;
4542 33aa809d 2019-08-08 stsp }
4543 33aa809d 2019-08-08 stsp if (outfile) {
4544 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4545 33aa809d 2019-08-08 stsp if (n != linelen) {
4546 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4547 33aa809d 2019-08-08 stsp goto done;
4548 33aa809d 2019-08-08 stsp }
4549 33aa809d 2019-08-08 stsp }
4550 33aa809d 2019-08-08 stsp if (rejectfile) {
4551 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4552 33aa809d 2019-08-08 stsp if (n != linelen)
4553 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4554 33aa809d 2019-08-08 stsp }
4555 33aa809d 2019-08-08 stsp done:
4556 33aa809d 2019-08-08 stsp free(line);
4557 2ec1f75b 2019-03-26 stsp return err;
4558 2ec1f75b 2019-03-26 stsp }
4559 1f1abb7e 2019-08-08 stsp
4560 33aa809d 2019-08-08 stsp static const struct got_error *
4561 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4562 33aa809d 2019-08-08 stsp {
4563 33aa809d 2019-08-08 stsp char *line = NULL;
4564 33aa809d 2019-08-08 stsp size_t linesize = 0;
4565 33aa809d 2019-08-08 stsp ssize_t linelen;
4566 33aa809d 2019-08-08 stsp
4567 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4568 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4569 500467ff 2019-09-25 hiltjo if (ferror(f))
4570 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4571 500467ff 2019-09-25 hiltjo return NULL;
4572 500467ff 2019-09-25 hiltjo }
4573 33aa809d 2019-08-08 stsp free(line);
4574 33aa809d 2019-08-08 stsp return NULL;
4575 33aa809d 2019-08-08 stsp }
4576 33aa809d 2019-08-08 stsp
4577 33aa809d 2019-08-08 stsp static const struct got_error *
4578 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4579 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4580 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4581 abc59930 2021-09-05 naddy {
4582 33aa809d 2019-08-08 stsp const struct got_error *err;
4583 33aa809d 2019-08-08 stsp
4584 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4585 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4586 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4587 33aa809d 2019-08-08 stsp if (err)
4588 33aa809d 2019-08-08 stsp return err;
4589 33aa809d 2019-08-08 stsp (*line_cur1)++;
4590 33aa809d 2019-08-08 stsp }
4591 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4592 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4593 33aa809d 2019-08-08 stsp if (rejectfile)
4594 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4595 33aa809d 2019-08-08 stsp else
4596 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4597 33aa809d 2019-08-08 stsp if (err)
4598 33aa809d 2019-08-08 stsp return err;
4599 33aa809d 2019-08-08 stsp (*line_cur2)++;
4600 33aa809d 2019-08-08 stsp }
4601 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4602 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4603 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4604 33aa809d 2019-08-08 stsp if (err)
4605 33aa809d 2019-08-08 stsp return err;
4606 33aa809d 2019-08-08 stsp (*line_cur2)++;
4607 33aa809d 2019-08-08 stsp }
4608 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4609 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4610 33aa809d 2019-08-08 stsp if (rejectfile)
4611 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4612 33aa809d 2019-08-08 stsp else
4613 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4614 33aa809d 2019-08-08 stsp if (err)
4615 33aa809d 2019-08-08 stsp return err;
4616 33aa809d 2019-08-08 stsp (*line_cur1)++;
4617 33aa809d 2019-08-08 stsp }
4618 f1e81a05 2019-08-10 stsp
4619 f1e81a05 2019-08-10 stsp return NULL;
4620 f1e81a05 2019-08-10 stsp }
4621 f1e81a05 2019-08-10 stsp
4622 f1e81a05 2019-08-10 stsp static const struct got_error *
4623 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4624 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4625 f1e81a05 2019-08-10 stsp {
4626 f1e81a05 2019-08-10 stsp const struct got_error *err;
4627 f1e81a05 2019-08-10 stsp
4628 f1e81a05 2019-08-10 stsp if (outfile) {
4629 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4630 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4631 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4632 f1e81a05 2019-08-10 stsp if (err)
4633 f1e81a05 2019-08-10 stsp return err;
4634 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4635 f1e81a05 2019-08-10 stsp }
4636 f1e81a05 2019-08-10 stsp }
4637 f1e81a05 2019-08-10 stsp if (rejectfile) {
4638 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4639 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4640 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4641 f1e81a05 2019-08-10 stsp if (err)
4642 f1e81a05 2019-08-10 stsp return err;
4643 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4644 f1e81a05 2019-08-10 stsp }
4645 33aa809d 2019-08-08 stsp }
4646 33aa809d 2019-08-08 stsp
4647 33aa809d 2019-08-08 stsp return NULL;
4648 33aa809d 2019-08-08 stsp }
4649 33aa809d 2019-08-08 stsp
4650 33aa809d 2019-08-08 stsp static const struct got_error *
4651 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4652 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4653 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4654 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4655 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4656 33aa809d 2019-08-08 stsp {
4657 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4658 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4659 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4660 33aa809d 2019-08-08 stsp FILE *hunkfile;
4661 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4662 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4663 fe621944 2020-11-10 stsp int rc;
4664 33aa809d 2019-08-08 stsp
4665 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4666 33aa809d 2019-08-08 stsp
4667 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4668 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4669 fe621944 2020-11-10 stsp start_old = cc.left.start;
4670 fe621944 2020-11-10 stsp end_old = cc.left.end;
4671 fe621944 2020-11-10 stsp start_new = cc.right.start;
4672 fe621944 2020-11-10 stsp end_new = cc.right.end;
4673 33aa809d 2019-08-08 stsp
4674 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4675 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4676 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4677 33aa809d 2019-08-08 stsp
4678 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4679 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4680 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4681 33aa809d 2019-08-08 stsp
4682 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4683 fe621944 2020-11-10 stsp if (diff_state == NULL)
4684 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4685 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4686 fe621944 2020-11-10 stsp
4687 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4688 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4689 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4690 33aa809d 2019-08-08 stsp goto done;
4691 33aa809d 2019-08-08 stsp }
4692 fe621944 2020-11-10 stsp
4693 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4694 fe621944 2020-11-10 stsp diff_result, &cc);
4695 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4696 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4697 33aa809d 2019-08-08 stsp goto done;
4698 33aa809d 2019-08-08 stsp }
4699 fe621944 2020-11-10 stsp
4700 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4701 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4702 33aa809d 2019-08-08 stsp goto done;
4703 33aa809d 2019-08-08 stsp }
4704 33aa809d 2019-08-08 stsp
4705 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4706 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4707 33aa809d 2019-08-08 stsp if (err)
4708 33aa809d 2019-08-08 stsp goto done;
4709 33aa809d 2019-08-08 stsp
4710 33aa809d 2019-08-08 stsp switch (*choice) {
4711 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4712 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4713 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4714 33aa809d 2019-08-08 stsp break;
4715 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4716 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4717 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4718 33aa809d 2019-08-08 stsp break;
4719 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4720 33aa809d 2019-08-08 stsp break;
4721 33aa809d 2019-08-08 stsp default:
4722 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4723 33aa809d 2019-08-08 stsp break;
4724 33aa809d 2019-08-08 stsp }
4725 33aa809d 2019-08-08 stsp done:
4726 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4727 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4728 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4729 33aa809d 2019-08-08 stsp return err;
4730 33aa809d 2019-08-08 stsp }
4731 33aa809d 2019-08-08 stsp
4732 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4733 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4734 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4735 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4736 1f1abb7e 2019-08-08 stsp void *progress_arg;
4737 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4738 33aa809d 2019-08-08 stsp void *patch_arg;
4739 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4740 10604dce 2021-09-24 thomas int unlink_added_files;
4741 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4742 1f1abb7e 2019-08-08 stsp };
4743 a129376b 2019-03-28 stsp
4744 e20a8b6f 2019-06-04 stsp static const struct got_error *
4745 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4746 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4747 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4748 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4749 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4750 33aa809d 2019-08-08 stsp {
4751 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4752 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4753 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4754 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4755 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4756 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4757 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4758 fe621944 2020-11-10 stsp struct stat sb2;
4759 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4760 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4761 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4762 33aa809d 2019-08-08 stsp
4763 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4764 33aa809d 2019-08-08 stsp
4765 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4766 33aa809d 2019-08-08 stsp if (err)
4767 33aa809d 2019-08-08 stsp return err;
4768 33aa809d 2019-08-08 stsp
4769 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4770 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4771 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4772 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4773 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4774 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4775 fa3cef63 2020-07-23 stsp goto done;
4776 fa3cef63 2020-07-23 stsp }
4777 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4778 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4779 48b4f239 2021-12-31 thomas if (link_len == -1) {
4780 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4781 48b4f239 2021-12-31 thomas path2);
4782 48b4f239 2021-12-31 thomas }
4783 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4784 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4785 12463d8b 2019-12-13 stsp }
4786 12463d8b 2019-12-13 stsp } else {
4787 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4788 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4789 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4790 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4791 fa3cef63 2020-07-23 stsp goto done;
4792 fa3cef63 2020-07-23 stsp }
4793 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4794 fa3cef63 2020-07-23 stsp sizeof(link_target));
4795 fa3cef63 2020-07-23 stsp if (link_len == -1)
4796 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4797 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4798 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4799 12463d8b 2019-12-13 stsp }
4800 1ebedb77 2019-10-19 stsp }
4801 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4802 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4803 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4804 fa3cef63 2020-07-23 stsp goto done;
4805 fa3cef63 2020-07-23 stsp }
4806 1ebedb77 2019-10-19 stsp
4807 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4808 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4809 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4810 fa3cef63 2020-07-23 stsp goto done;
4811 fa3cef63 2020-07-23 stsp }
4812 fa3cef63 2020-07-23 stsp fd2 = -1;
4813 fa3cef63 2020-07-23 stsp } else {
4814 fa3cef63 2020-07-23 stsp size_t n;
4815 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4816 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4817 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4818 fa3cef63 2020-07-23 stsp goto done;
4819 fa3cef63 2020-07-23 stsp }
4820 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4821 fa3cef63 2020-07-23 stsp if (n != link_len) {
4822 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4823 fa3cef63 2020-07-23 stsp goto done;
4824 fa3cef63 2020-07-23 stsp }
4825 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4826 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4827 fa3cef63 2020-07-23 stsp goto done;
4828 fa3cef63 2020-07-23 stsp }
4829 fa3cef63 2020-07-23 stsp rewind(f2);
4830 33aa809d 2019-08-08 stsp }
4831 33aa809d 2019-08-08 stsp
4832 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4833 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4834 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4835 f4ae6ddb 2022-07-01 thomas goto done;
4836 f4ae6ddb 2022-07-01 thomas }
4837 f4ae6ddb 2022-07-01 thomas
4838 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
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(&path1, &f1, "got-patched-blob", "");
4843 33aa809d 2019-08-08 stsp if (err)
4844 33aa809d 2019-08-08 stsp goto done;
4845 33aa809d 2019-08-08 stsp
4846 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4847 33aa809d 2019-08-08 stsp if (err)
4848 33aa809d 2019-08-08 stsp goto done;
4849 33aa809d 2019-08-08 stsp
4850 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4851 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4852 33aa809d 2019-08-08 stsp if (err)
4853 33aa809d 2019-08-08 stsp goto done;
4854 33aa809d 2019-08-08 stsp
4855 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4856 fc2a50f2 2022-11-01 thomas "");
4857 33aa809d 2019-08-08 stsp if (err)
4858 33aa809d 2019-08-08 stsp goto done;
4859 33aa809d 2019-08-08 stsp
4860 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4861 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4862 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4863 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4864 fe621944 2020-11-10 stsp
4865 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4866 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4867 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4868 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4869 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4870 fe621944 2020-11-10 stsp nchanges++;
4871 fe621944 2020-11-10 stsp }
4872 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4873 33aa809d 2019-08-08 stsp int choice;
4874 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4875 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4876 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4877 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4878 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4879 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4880 33aa809d 2019-08-08 stsp if (err)
4881 33aa809d 2019-08-08 stsp goto done;
4882 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4883 33aa809d 2019-08-08 stsp have_content = 1;
4884 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4885 33aa809d 2019-08-08 stsp break;
4886 33aa809d 2019-08-08 stsp }
4887 1ebedb77 2019-10-19 stsp if (have_content) {
4888 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4889 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4890 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4891 1ebedb77 2019-10-19 stsp if (err)
4892 1ebedb77 2019-10-19 stsp goto done;
4893 1ebedb77 2019-10-19 stsp
4894 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4895 a2c162eb 2022-10-30 thomas mode_t mode;
4896 a2c162eb 2022-10-30 thomas
4897 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4898 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4899 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4900 fa3cef63 2020-07-23 stsp goto done;
4901 fa3cef63 2020-07-23 stsp }
4902 1ebedb77 2019-10-19 stsp }
4903 1ebedb77 2019-10-19 stsp }
4904 33aa809d 2019-08-08 stsp done:
4905 33aa809d 2019-08-08 stsp free(id_str);
4906 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4907 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4908 33aa809d 2019-08-08 stsp if (blob)
4909 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4910 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4911 fe621944 2020-11-10 stsp if (err == NULL)
4912 fe621944 2020-11-10 stsp err = free_err;
4913 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4914 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4915 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4916 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4917 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4918 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4919 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4920 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4921 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4922 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4923 33aa809d 2019-08-08 stsp if (err || !have_content) {
4924 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4925 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4926 33aa809d 2019-08-08 stsp free(*path_outfile);
4927 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4928 33aa809d 2019-08-08 stsp }
4929 33aa809d 2019-08-08 stsp free(path1);
4930 33aa809d 2019-08-08 stsp return err;
4931 33aa809d 2019-08-08 stsp }
4932 33aa809d 2019-08-08 stsp
4933 33aa809d 2019-08-08 stsp static const struct got_error *
4934 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4935 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4936 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4937 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4938 a129376b 2019-03-28 stsp {
4939 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4940 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4941 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4942 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4943 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4944 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4945 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4946 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4947 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4948 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4949 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4950 f4ae6ddb 2022-07-01 thomas int fd = -1;
4951 a129376b 2019-03-28 stsp
4952 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4953 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4954 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4955 d3bcc3d1 2019-08-08 stsp return NULL;
4956 3d69ad8d 2019-08-17 semarie
4957 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4958 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4959 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4960 d3bcc3d1 2019-08-08 stsp
4961 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4962 65084dad 2019-08-08 stsp if (ie == NULL)
4963 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4964 a129376b 2019-03-28 stsp
4965 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4966 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4967 a129376b 2019-03-28 stsp if (err) {
4968 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4969 a129376b 2019-03-28 stsp goto done;
4970 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4971 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4972 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4973 e20a8b6f 2019-06-04 stsp goto done;
4974 e20a8b6f 2019-06-04 stsp }
4975 a129376b 2019-03-28 stsp }
4976 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4977 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4978 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4979 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4980 a129376b 2019-03-28 stsp goto done;
4981 a129376b 2019-03-28 stsp }
4982 a129376b 2019-03-28 stsp } else {
4983 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4984 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4985 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4986 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4987 a129376b 2019-03-28 stsp goto done;
4988 a129376b 2019-03-28 stsp }
4989 a129376b 2019-03-28 stsp } else {
4990 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4991 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4992 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4993 a129376b 2019-03-28 stsp goto done;
4994 a129376b 2019-03-28 stsp }
4995 a129376b 2019-03-28 stsp }
4996 a129376b 2019-03-28 stsp }
4997 a129376b 2019-03-28 stsp
4998 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4999 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5000 945f9229 2022-04-16 thomas if (err)
5001 945f9229 2022-04-16 thomas goto done;
5002 945f9229 2022-04-16 thomas
5003 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5004 a9fa2909 2019-07-27 stsp if (err) {
5005 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5006 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5007 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5008 a9fa2909 2019-07-27 stsp goto done;
5009 a9fa2909 2019-07-27 stsp } else {
5010 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5011 a9fa2909 2019-07-27 stsp if (err)
5012 a9fa2909 2019-07-27 stsp goto done;
5013 a9fa2909 2019-07-27 stsp
5014 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5015 1233e6b6 2020-10-19 stsp if (err)
5016 a9fa2909 2019-07-27 stsp goto done;
5017 a9fa2909 2019-07-27 stsp
5018 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5019 1233e6b6 2020-10-19 stsp free(te_name);
5020 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5021 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5022 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5023 a9fa2909 2019-07-27 stsp goto done;
5024 a9fa2909 2019-07-27 stsp }
5025 a129376b 2019-03-28 stsp }
5026 a129376b 2019-03-28 stsp
5027 a129376b 2019-03-28 stsp switch (status) {
5028 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5029 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5030 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5031 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5032 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5033 33aa809d 2019-08-08 stsp if (err)
5034 33aa809d 2019-08-08 stsp goto done;
5035 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5036 33aa809d 2019-08-08 stsp break;
5037 33aa809d 2019-08-08 stsp }
5038 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5039 1f1abb7e 2019-08-08 stsp ie->path);
5040 1ee397ad 2019-07-12 stsp if (err)
5041 1ee397ad 2019-07-12 stsp goto done;
5042 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5043 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5044 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5045 8641a332 2023-04-14 thomas
5046 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5047 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5048 8641a332 2023-04-14 thomas
5049 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5050 8641a332 2023-04-14 thomas entry) {
5051 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5052 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5053 8641a332 2023-04-14 thomas continue;
5054 8641a332 2023-04-14 thomas do_unlink = 1;
5055 8641a332 2023-04-14 thomas break;
5056 8641a332 2023-04-14 thomas }
5057 10604dce 2021-09-24 thomas }
5058 8641a332 2023-04-14 thomas
5059 8641a332 2023-04-14 thomas if (do_unlink) {
5060 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5061 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5062 8641a332 2023-04-14 thomas relpath) == -1) {
5063 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5064 8641a332 2023-04-14 thomas goto done;
5065 8641a332 2023-04-14 thomas }
5066 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5067 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5068 8641a332 2023-04-14 thomas ondisk_path);
5069 8641a332 2023-04-14 thomas break;
5070 8641a332 2023-04-14 thomas }
5071 10604dce 2021-09-24 thomas }
5072 10604dce 2021-09-24 thomas }
5073 a129376b 2019-03-28 stsp break;
5074 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5075 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5076 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5077 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5078 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5079 33aa809d 2019-08-08 stsp if (err)
5080 33aa809d 2019-08-08 stsp goto done;
5081 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5082 33aa809d 2019-08-08 stsp break;
5083 33aa809d 2019-08-08 stsp }
5084 33aa809d 2019-08-08 stsp /* fall through */
5085 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5086 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5087 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5088 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5089 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5090 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5091 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5092 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5093 43010591 2023-02-17 thomas else
5094 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5095 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5096 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5097 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5098 f4ae6ddb 2022-07-01 thomas goto done;
5099 f4ae6ddb 2022-07-01 thomas }
5100 f4ae6ddb 2022-07-01 thomas
5101 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5102 a129376b 2019-03-28 stsp if (err)
5103 65084dad 2019-08-08 stsp goto done;
5104 65084dad 2019-08-08 stsp
5105 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5106 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5107 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5108 a129376b 2019-03-28 stsp goto done;
5109 65084dad 2019-08-08 stsp }
5110 65084dad 2019-08-08 stsp
5111 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5112 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5113 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5114 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5115 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5116 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5117 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5118 33aa809d 2019-08-08 stsp break;
5119 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5120 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5121 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5122 369fd7e5 2020-07-23 stsp path_content);
5123 369fd7e5 2020-07-23 stsp break;
5124 369fd7e5 2020-07-23 stsp }
5125 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5126 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5127 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5128 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5129 369fd7e5 2020-07-23 stsp } else {
5130 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5131 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5132 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5133 369fd7e5 2020-07-23 stsp goto done;
5134 369fd7e5 2020-07-23 stsp }
5135 33aa809d 2019-08-08 stsp }
5136 33aa809d 2019-08-08 stsp } else {
5137 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5138 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5139 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5140 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5141 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5142 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5143 2e1fa222 2020-07-23 stsp } else {
5144 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5145 2e1fa222 2020-07-23 stsp ie->path,
5146 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5147 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5148 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5149 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5150 2e1fa222 2020-07-23 stsp }
5151 a129376b 2019-03-28 stsp if (err)
5152 a129376b 2019-03-28 stsp goto done;
5153 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5154 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5155 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5156 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5157 437adc9d 2020-12-10 yzhong blob->id.sha1,
5158 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5159 2e1fa222 2020-07-23 stsp if (err)
5160 2e1fa222 2020-07-23 stsp goto done;
5161 2e1fa222 2020-07-23 stsp }
5162 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5163 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5164 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5165 33aa809d 2019-08-08 stsp }
5166 a129376b 2019-03-28 stsp }
5167 a129376b 2019-03-28 stsp break;
5168 e20a8b6f 2019-06-04 stsp }
5169 a129376b 2019-03-28 stsp default:
5170 1f1abb7e 2019-08-08 stsp break;
5171 a129376b 2019-03-28 stsp }
5172 a129376b 2019-03-28 stsp done:
5173 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5174 33aa809d 2019-08-08 stsp free(path_content);
5175 e20a8b6f 2019-06-04 stsp free(parent_path);
5176 a129376b 2019-03-28 stsp free(tree_path);
5177 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5178 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5179 a129376b 2019-03-28 stsp if (blob)
5180 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5181 a129376b 2019-03-28 stsp if (tree)
5182 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5183 a129376b 2019-03-28 stsp free(tree_id);
5184 945f9229 2022-04-16 thomas if (base_commit)
5185 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5186 e20a8b6f 2019-06-04 stsp return err;
5187 e20a8b6f 2019-06-04 stsp }
5188 e20a8b6f 2019-06-04 stsp
5189 e20a8b6f 2019-06-04 stsp const struct got_error *
5190 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5191 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5192 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5193 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5194 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5195 e20a8b6f 2019-06-04 stsp {
5196 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5197 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5198 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5199 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5200 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5201 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5202 e20a8b6f 2019-06-04 stsp
5203 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5204 e20a8b6f 2019-06-04 stsp if (err)
5205 e20a8b6f 2019-06-04 stsp return err;
5206 e20a8b6f 2019-06-04 stsp
5207 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5208 e20a8b6f 2019-06-04 stsp if (err)
5209 e20a8b6f 2019-06-04 stsp goto done;
5210 e20a8b6f 2019-06-04 stsp
5211 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5212 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5213 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5214 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5215 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5216 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5217 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5218 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5219 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5220 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5221 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5222 e20a8b6f 2019-06-04 stsp if (err)
5223 af12c6b9 2019-06-04 stsp break;
5224 e20a8b6f 2019-06-04 stsp }
5225 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5226 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5227 e20a8b6f 2019-06-04 stsp err = sync_err;
5228 af12c6b9 2019-06-04 stsp done:
5229 fb399478 2019-07-12 stsp free(fileindex_path);
5230 a129376b 2019-03-28 stsp if (fileindex)
5231 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5232 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5233 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5234 a129376b 2019-03-28 stsp err = unlockerr;
5235 c4296144 2019-05-09 stsp return err;
5236 c4296144 2019-05-09 stsp }
5237 c4296144 2019-05-09 stsp
5238 cf066bf8 2019-05-09 stsp static void
5239 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5240 cf066bf8 2019-05-09 stsp {
5241 24519714 2019-05-09 stsp free(ct->path);
5242 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5243 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5244 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5245 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5246 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5247 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5248 cf066bf8 2019-05-09 stsp free(ct);
5249 cf066bf8 2019-05-09 stsp }
5250 24519714 2019-05-09 stsp
5251 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5252 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5253 24519714 2019-05-09 stsp struct got_repository *repo;
5254 24519714 2019-05-09 stsp struct got_worktree *worktree;
5255 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5256 5f8a88c6 2019-08-03 stsp int have_staged_files;
5257 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5258 ef899790 2022-11-01 thomas int diff_header_shown;
5259 be94c032 2023-02-20 thomas int commit_conflicts;
5260 ef899790 2022-11-01 thomas FILE *diff_outfile;
5261 ef899790 2022-11-01 thomas FILE *f1;
5262 ef899790 2022-11-01 thomas FILE *f2;
5263 24519714 2019-05-09 stsp };
5264 ef899790 2022-11-01 thomas
5265 ef899790 2022-11-01 thomas /*
5266 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5267 ef899790 2022-11-01 thomas * it as content to the diff engine.
5268 ef899790 2022-11-01 thomas */
5269 ef899790 2022-11-01 thomas static const struct got_error *
5270 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5271 ef899790 2022-11-01 thomas const char *abspath)
5272 ef899790 2022-11-01 thomas {
5273 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5274 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5275 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5276 ef899790 2022-11-01 thomas
5277 ef899790 2022-11-01 thomas *fd = -1;
5278 ef899790 2022-11-01 thomas
5279 ef899790 2022-11-01 thomas if (dirfd != -1) {
5280 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5281 ef899790 2022-11-01 thomas if (target_len == -1)
5282 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5283 ef899790 2022-11-01 thomas } else {
5284 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5285 ef899790 2022-11-01 thomas if (target_len == -1)
5286 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5287 ef899790 2022-11-01 thomas }
5288 ef899790 2022-11-01 thomas
5289 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5290 ef899790 2022-11-01 thomas if (*fd == -1)
5291 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5292 ef899790 2022-11-01 thomas
5293 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5294 ef899790 2022-11-01 thomas if (outlen == -1) {
5295 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5296 ef899790 2022-11-01 thomas goto done;
5297 ef899790 2022-11-01 thomas }
5298 ef899790 2022-11-01 thomas
5299 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5300 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5301 ef899790 2022-11-01 thomas goto done;
5302 ef899790 2022-11-01 thomas }
5303 ef899790 2022-11-01 thomas done:
5304 ef899790 2022-11-01 thomas if (err) {
5305 ef899790 2022-11-01 thomas close(*fd);
5306 ef899790 2022-11-01 thomas *fd = -1;
5307 ef899790 2022-11-01 thomas }
5308 ef899790 2022-11-01 thomas return err;
5309 ef899790 2022-11-01 thomas }
5310 ef899790 2022-11-01 thomas
5311 ef899790 2022-11-01 thomas static const struct got_error *
5312 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5313 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5314 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5315 ef899790 2022-11-01 thomas {
5316 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5317 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5318 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5319 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5320 ef899790 2022-11-01 thomas char *label1 = NULL;
5321 ef899790 2022-11-01 thomas struct stat sb;
5322 ef899790 2022-11-01 thomas off_t size1 = 0;
5323 ef899790 2022-11-01 thomas int f2_exists = 0;
5324 ef899790 2022-11-01 thomas char *id_str = NULL;
5325 ef899790 2022-11-01 thomas
5326 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5327 d259e491 2022-11-01 thomas
5328 d259e491 2022-11-01 thomas if (diff_staged) {
5329 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5330 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5331 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5332 d259e491 2022-11-01 thomas return NULL;
5333 d259e491 2022-11-01 thomas } else {
5334 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5335 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5336 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5337 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5338 d259e491 2022-11-01 thomas return NULL;
5339 d259e491 2022-11-01 thomas }
5340 ef899790 2022-11-01 thomas
5341 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5342 ef899790 2022-11-01 thomas if (err)
5343 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5344 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5345 ef899790 2022-11-01 thomas if (err)
5346 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5347 ef899790 2022-11-01 thomas
5348 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5349 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5350 ef899790 2022-11-01 thomas if (err)
5351 ef899790 2022-11-01 thomas return err;
5352 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5353 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5354 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5355 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5356 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5357 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5358 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5359 ef899790 2022-11-01 thomas }
5360 ef899790 2022-11-01 thomas
5361 ef899790 2022-11-01 thomas if (diff_staged) {
5362 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5363 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5364 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5365 ef899790 2022-11-01 thomas label1 = ct->path;
5366 ef899790 2022-11-01 thomas label2 = ct->path;
5367 ef899790 2022-11-01 thomas break;
5368 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5369 ef899790 2022-11-01 thomas label2 = ct->path;
5370 ef899790 2022-11-01 thomas break;
5371 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5372 ef899790 2022-11-01 thomas label1 = ct->path;
5373 ef899790 2022-11-01 thomas break;
5374 ef899790 2022-11-01 thomas default:
5375 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5376 ef899790 2022-11-01 thomas }
5377 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5378 ef899790 2022-11-01 thomas if (fd1 == -1) {
5379 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5380 ef899790 2022-11-01 thomas goto done;
5381 ef899790 2022-11-01 thomas }
5382 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5383 ef899790 2022-11-01 thomas if (fd2 == -1) {
5384 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5385 ef899790 2022-11-01 thomas goto done;
5386 ef899790 2022-11-01 thomas }
5387 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5388 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5389 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5390 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
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 fd1 = got_opentempfd();
5395 ef899790 2022-11-01 thomas if (fd1 == -1) {
5396 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5397 ef899790 2022-11-01 thomas goto done;
5398 ef899790 2022-11-01 thomas }
5399 ef899790 2022-11-01 thomas
5400 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5401 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5402 ef899790 2022-11-01 thomas 8192, fd1);
5403 ef899790 2022-11-01 thomas if (err)
5404 ef899790 2022-11-01 thomas goto done;
5405 ef899790 2022-11-01 thomas }
5406 ef899790 2022-11-01 thomas
5407 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5408 ef899790 2022-11-01 thomas if (dirfd != -1) {
5409 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5410 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5411 ef899790 2022-11-01 thomas if (fd == -1) {
5412 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5413 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5414 ef899790 2022-11-01 thomas ct->ondisk_path);
5415 ef899790 2022-11-01 thomas goto done;
5416 ef899790 2022-11-01 thomas }
5417 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5418 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5419 ef899790 2022-11-01 thomas if (err)
5420 ef899790 2022-11-01 thomas goto done;
5421 ef899790 2022-11-01 thomas }
5422 ef899790 2022-11-01 thomas } else {
5423 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5424 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5425 ef899790 2022-11-01 thomas if (fd == -1) {
5426 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5427 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5428 ef899790 2022-11-01 thomas ct->ondisk_path);
5429 ef899790 2022-11-01 thomas goto done;
5430 ef899790 2022-11-01 thomas }
5431 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5432 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5433 ef899790 2022-11-01 thomas if (err)
5434 ef899790 2022-11-01 thomas goto done;
5435 ef899790 2022-11-01 thomas }
5436 ef899790 2022-11-01 thomas }
5437 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5438 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5439 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5440 ef899790 2022-11-01 thomas goto done;
5441 ef899790 2022-11-01 thomas }
5442 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5443 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5444 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5445 ef899790 2022-11-01 thomas goto done;
5446 ef899790 2022-11-01 thomas }
5447 ef899790 2022-11-01 thomas fd = -1;
5448 ef899790 2022-11-01 thomas f2_exists = 1;
5449 ef899790 2022-11-01 thomas }
5450 ef899790 2022-11-01 thomas
5451 ef899790 2022-11-01 thomas if (blob1) {
5452 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5453 ef899790 2022-11-01 thomas f1, blob1);
5454 ef899790 2022-11-01 thomas if (err)
5455 ef899790 2022-11-01 thomas goto done;
5456 ef899790 2022-11-01 thomas }
5457 ef899790 2022-11-01 thomas
5458 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5459 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5460 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5461 ef899790 2022-11-01 thomas done:
5462 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5463 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5464 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5465 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5466 ef899790 2022-11-01 thomas if (blob1)
5467 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5468 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5469 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5470 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5471 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5472 ef899790 2022-11-01 thomas return err;
5473 ef899790 2022-11-01 thomas }
5474 cf066bf8 2019-05-09 stsp
5475 c4296144 2019-05-09 stsp static const struct got_error *
5476 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5477 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5478 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5479 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5480 c4296144 2019-05-09 stsp {
5481 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5482 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5483 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5484 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5485 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5486 768aea60 2019-05-09 stsp struct stat sb;
5487 c4296144 2019-05-09 stsp
5488 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5489 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5490 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5491 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5492 5f8a88c6 2019-08-03 stsp return NULL;
5493 5f8a88c6 2019-08-03 stsp } else {
5494 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5495 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5496 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5497 be94c032 2023-02-20 thomas }
5498 c4296144 2019-05-09 stsp
5499 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5500 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5501 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5502 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5503 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5504 5f8a88c6 2019-08-03 stsp return NULL;
5505 5f8a88c6 2019-08-03 stsp }
5506 0b5cc0d6 2019-05-09 stsp
5507 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5509 036813ee 2019-05-09 stsp goto done;
5510 036813ee 2019-05-09 stsp }
5511 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5512 036813ee 2019-05-09 stsp parent_path = strdup("");
5513 69960a46 2019-05-09 stsp if (parent_path == NULL)
5514 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5515 69960a46 2019-05-09 stsp } else {
5516 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5517 69960a46 2019-05-09 stsp if (err)
5518 69960a46 2019-05-09 stsp return err;
5519 69960a46 2019-05-09 stsp }
5520 c4296144 2019-05-09 stsp
5521 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5522 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5523 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5524 c4296144 2019-05-09 stsp goto done;
5525 768aea60 2019-05-09 stsp }
5526 768aea60 2019-05-09 stsp
5527 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5528 768aea60 2019-05-09 stsp relpath) == -1) {
5529 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5530 768aea60 2019-05-09 stsp goto done;
5531 768aea60 2019-05-09 stsp }
5532 0aeb8099 2020-07-23 stsp
5533 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5534 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5535 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5536 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5537 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5538 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5539 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5540 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5541 0aeb8099 2020-07-23 stsp break;
5542 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5543 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5544 0aeb8099 2020-07-23 stsp break;
5545 0aeb8099 2020-07-23 stsp default:
5546 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5547 0aeb8099 2020-07-23 stsp goto done;
5548 0aeb8099 2020-07-23 stsp }
5549 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5550 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5551 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5552 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5553 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5554 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5555 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5556 12463d8b 2019-12-13 stsp ct->ondisk_path);
5557 12463d8b 2019-12-13 stsp goto done;
5558 12463d8b 2019-12-13 stsp }
5559 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5560 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5561 768aea60 2019-05-09 stsp goto done;
5562 768aea60 2019-05-09 stsp }
5563 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5564 c4296144 2019-05-09 stsp }
5565 c4296144 2019-05-09 stsp
5566 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5567 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5568 44d03001 2019-05-09 stsp relpath) == -1) {
5569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5570 44d03001 2019-05-09 stsp goto done;
5571 44d03001 2019-05-09 stsp }
5572 44d03001 2019-05-09 stsp
5573 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5574 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5575 35213c7c 2020-07-23 stsp int is_bad_symlink;
5576 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5577 35213c7c 2020-07-23 stsp ssize_t target_len;
5578 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5579 35213c7c 2020-07-23 stsp sizeof(target_path));
5580 35213c7c 2020-07-23 stsp if (target_len == -1) {
5581 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5582 35213c7c 2020-07-23 stsp ct->ondisk_path);
5583 35213c7c 2020-07-23 stsp goto done;
5584 35213c7c 2020-07-23 stsp }
5585 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5586 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5587 35213c7c 2020-07-23 stsp if (err)
5588 35213c7c 2020-07-23 stsp goto done;
5589 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5590 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5591 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5592 35213c7c 2020-07-23 stsp goto done;
5593 35213c7c 2020-07-23 stsp }
5594 35213c7c 2020-07-23 stsp }
5595 35213c7c 2020-07-23 stsp
5596 35213c7c 2020-07-23 stsp
5597 cf066bf8 2019-05-09 stsp ct->status = status;
5598 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5599 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5600 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5601 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5602 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5603 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5604 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5605 b416585c 2019-05-13 stsp goto done;
5606 b416585c 2019-05-13 stsp }
5607 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5608 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5609 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5610 f0b75401 2019-08-03 stsp goto done;
5611 f0b75401 2019-08-03 stsp }
5612 f0b75401 2019-08-03 stsp }
5613 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5614 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5615 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5616 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5617 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5618 036813ee 2019-05-09 stsp goto done;
5619 036813ee 2019-05-09 stsp }
5620 ca2503ea 2019-05-09 stsp }
5621 24519714 2019-05-09 stsp ct->path = strdup(path);
5622 24519714 2019-05-09 stsp if (ct->path == NULL) {
5623 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5624 24519714 2019-05-09 stsp goto done;
5625 24519714 2019-05-09 stsp }
5626 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5627 ef899790 2022-11-01 thomas if (err)
5628 ef899790 2022-11-01 thomas goto done;
5629 ef899790 2022-11-01 thomas
5630 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5631 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5632 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5633 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5634 ef899790 2022-11-01 thomas if (err)
5635 ef899790 2022-11-01 thomas goto done;
5636 ef899790 2022-11-01 thomas }
5637 c4296144 2019-05-09 stsp done:
5638 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5639 ed175427 2019-05-09 stsp free_commitable(ct);
5640 24519714 2019-05-09 stsp free(parent_path);
5641 036813ee 2019-05-09 stsp free(path);
5642 a129376b 2019-03-28 stsp return err;
5643 a129376b 2019-03-28 stsp }
5644 c4296144 2019-05-09 stsp
5645 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5646 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5647 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5648 036813ee 2019-05-09 stsp struct got_repository *);
5649 ed175427 2019-05-09 stsp
5650 ed175427 2019-05-09 stsp static const struct got_error *
5651 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5652 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5653 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5654 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5655 afa376bf 2019-05-09 stsp struct got_repository *repo)
5656 ed175427 2019-05-09 stsp {
5657 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5658 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5659 036813ee 2019-05-09 stsp char *subpath;
5660 ed175427 2019-05-09 stsp
5661 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5662 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5663 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5664 ed175427 2019-05-09 stsp
5665 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5666 ed175427 2019-05-09 stsp if (err)
5667 ed175427 2019-05-09 stsp return err;
5668 ed175427 2019-05-09 stsp
5669 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5670 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5671 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5672 036813ee 2019-05-09 stsp free(subpath);
5673 036813ee 2019-05-09 stsp return err;
5674 036813ee 2019-05-09 stsp }
5675 ed175427 2019-05-09 stsp
5676 036813ee 2019-05-09 stsp static const struct got_error *
5677 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5678 036813ee 2019-05-09 stsp {
5679 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5680 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5681 ed175427 2019-05-09 stsp
5682 036813ee 2019-05-09 stsp *match = 0;
5683 ed175427 2019-05-09 stsp
5684 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5685 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5686 0f63689d 2019-05-10 stsp return NULL;
5687 036813ee 2019-05-09 stsp }
5688 0b5cc0d6 2019-05-09 stsp
5689 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5690 0f63689d 2019-05-10 stsp if (err)
5691 0f63689d 2019-05-10 stsp return err;
5692 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5693 036813ee 2019-05-09 stsp free(ct_parent_path);
5694 036813ee 2019-05-09 stsp return err;
5695 036813ee 2019-05-09 stsp }
5696 0b5cc0d6 2019-05-09 stsp
5697 768aea60 2019-05-09 stsp static mode_t
5698 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5699 768aea60 2019-05-09 stsp {
5700 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5701 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5702 3d9a4ec4 2020-07-23 stsp
5703 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5704 768aea60 2019-05-09 stsp }
5705 768aea60 2019-05-09 stsp
5706 036813ee 2019-05-09 stsp static const struct got_error *
5707 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5708 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5709 036813ee 2019-05-09 stsp {
5710 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5711 ca2503ea 2019-05-09 stsp
5712 036813ee 2019-05-09 stsp *new_te = NULL;
5713 0b5cc0d6 2019-05-09 stsp
5714 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5715 036813ee 2019-05-09 stsp if (err)
5716 036813ee 2019-05-09 stsp goto done;
5717 0b5cc0d6 2019-05-09 stsp
5718 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5719 036813ee 2019-05-09 stsp
5720 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5721 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5722 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5723 5f8a88c6 2019-08-03 stsp else
5724 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5725 036813ee 2019-05-09 stsp done:
5726 036813ee 2019-05-09 stsp if (err && *new_te) {
5727 56e0773d 2019-11-28 stsp free(*new_te);
5728 036813ee 2019-05-09 stsp *new_te = NULL;
5729 036813ee 2019-05-09 stsp }
5730 036813ee 2019-05-09 stsp return err;
5731 036813ee 2019-05-09 stsp }
5732 036813ee 2019-05-09 stsp
5733 036813ee 2019-05-09 stsp static const struct got_error *
5734 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5735 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5736 036813ee 2019-05-09 stsp {
5737 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5738 102b254e 2020-10-19 stsp char *ct_name = NULL;
5739 036813ee 2019-05-09 stsp
5740 036813ee 2019-05-09 stsp *new_te = NULL;
5741 036813ee 2019-05-09 stsp
5742 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5743 036813ee 2019-05-09 stsp if (*new_te == NULL)
5744 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5745 036813ee 2019-05-09 stsp
5746 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5747 102b254e 2020-10-19 stsp if (err)
5748 036813ee 2019-05-09 stsp goto done;
5749 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5750 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5751 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5752 036813ee 2019-05-09 stsp goto done;
5753 036813ee 2019-05-09 stsp }
5754 036813ee 2019-05-09 stsp
5755 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5756 036813ee 2019-05-09 stsp
5757 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5758 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5759 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5760 5f8a88c6 2019-08-03 stsp else
5761 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5762 036813ee 2019-05-09 stsp done:
5763 102b254e 2020-10-19 stsp free(ct_name);
5764 036813ee 2019-05-09 stsp if (err && *new_te) {
5765 56e0773d 2019-11-28 stsp free(*new_te);
5766 036813ee 2019-05-09 stsp *new_te = NULL;
5767 036813ee 2019-05-09 stsp }
5768 036813ee 2019-05-09 stsp return err;
5769 036813ee 2019-05-09 stsp }
5770 036813ee 2019-05-09 stsp
5771 036813ee 2019-05-09 stsp static const struct got_error *
5772 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5773 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5774 036813ee 2019-05-09 stsp {
5775 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5776 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5777 036813ee 2019-05-09 stsp
5778 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5779 036813ee 2019-05-09 stsp if (err)
5780 036813ee 2019-05-09 stsp return err;
5781 036813ee 2019-05-09 stsp if (new_pe == NULL)
5782 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5783 036813ee 2019-05-09 stsp return NULL;
5784 afa376bf 2019-05-09 stsp }
5785 afa376bf 2019-05-09 stsp
5786 afa376bf 2019-05-09 stsp static const struct got_error *
5787 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5788 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5789 afa376bf 2019-05-09 stsp {
5790 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5791 5f8a88c6 2019-08-03 stsp unsigned char status;
5792 ae1e948a 2021-09-28 thomas
5793 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5794 ae1e948a 2021-09-28 thomas return NULL;
5795 5f8a88c6 2019-08-03 stsp
5796 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5797 afa376bf 2019-05-09 stsp ct_path++;
5798 5f8a88c6 2019-08-03 stsp
5799 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5800 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5801 5f8a88c6 2019-08-03 stsp else
5802 5f8a88c6 2019-08-03 stsp status = ct->status;
5803 5f8a88c6 2019-08-03 stsp
5804 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5805 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5806 036813ee 2019-05-09 stsp }
5807 036813ee 2019-05-09 stsp
5808 036813ee 2019-05-09 stsp static const struct got_error *
5809 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5810 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5811 44d03001 2019-05-09 stsp {
5812 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5813 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5814 44d03001 2019-05-09 stsp char *te_path;
5815 44d03001 2019-05-09 stsp
5816 44d03001 2019-05-09 stsp *modified = 0;
5817 44d03001 2019-05-09 stsp
5818 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5819 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5820 44d03001 2019-05-09 stsp te->name) == -1)
5821 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5822 44d03001 2019-05-09 stsp
5823 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5824 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5825 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5826 44d03001 2019-05-09 stsp strlen(te_path));
5827 62d463ca 2020-10-20 naddy if (*modified)
5828 44d03001 2019-05-09 stsp break;
5829 44d03001 2019-05-09 stsp }
5830 44d03001 2019-05-09 stsp
5831 44d03001 2019-05-09 stsp free(te_path);
5832 44d03001 2019-05-09 stsp return err;
5833 44d03001 2019-05-09 stsp }
5834 44d03001 2019-05-09 stsp
5835 44d03001 2019-05-09 stsp static const struct got_error *
5836 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5837 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5838 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5839 036813ee 2019-05-09 stsp {
5840 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5841 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5842 036813ee 2019-05-09 stsp
5843 036813ee 2019-05-09 stsp *ctp = NULL;
5844 036813ee 2019-05-09 stsp
5845 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5846 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5847 036813ee 2019-05-09 stsp char *ct_name = NULL;
5848 036813ee 2019-05-09 stsp int path_matches;
5849 036813ee 2019-05-09 stsp
5850 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5851 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5852 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5853 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5854 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5855 5f8a88c6 2019-08-03 stsp continue;
5856 5f8a88c6 2019-08-03 stsp } else {
5857 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5858 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5859 5f8a88c6 2019-08-03 stsp continue;
5860 5f8a88c6 2019-08-03 stsp }
5861 036813ee 2019-05-09 stsp
5862 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5863 036813ee 2019-05-09 stsp continue;
5864 036813ee 2019-05-09 stsp
5865 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5866 62d463ca 2020-10-20 naddy if (err)
5867 036813ee 2019-05-09 stsp return err;
5868 036813ee 2019-05-09 stsp if (!path_matches)
5869 036813ee 2019-05-09 stsp continue;
5870 036813ee 2019-05-09 stsp
5871 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5872 d34b633e 2020-10-19 stsp if (err)
5873 d34b633e 2020-10-19 stsp return err;
5874 d34b633e 2020-10-19 stsp
5875 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5876 d34b633e 2020-10-19 stsp free(ct_name);
5877 036813ee 2019-05-09 stsp continue;
5878 d34b633e 2020-10-19 stsp }
5879 d34b633e 2020-10-19 stsp free(ct_name);
5880 036813ee 2019-05-09 stsp
5881 036813ee 2019-05-09 stsp *ctp = ct;
5882 036813ee 2019-05-09 stsp break;
5883 036813ee 2019-05-09 stsp }
5884 2b496619 2019-07-10 stsp
5885 2b496619 2019-07-10 stsp return err;
5886 2b496619 2019-07-10 stsp }
5887 2b496619 2019-07-10 stsp
5888 2b496619 2019-07-10 stsp static const struct got_error *
5889 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5890 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5891 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5892 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5893 2b496619 2019-07-10 stsp struct got_repository *repo)
5894 2b496619 2019-07-10 stsp {
5895 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5896 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5897 2b496619 2019-07-10 stsp char *subtree_path;
5898 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5899 ba580f68 2020-03-22 stsp int nentries;
5900 2b496619 2019-07-10 stsp
5901 2b496619 2019-07-10 stsp *new_tep = NULL;
5902 2b496619 2019-07-10 stsp
5903 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5904 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5905 2b496619 2019-07-10 stsp child_path) == -1)
5906 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5907 036813ee 2019-05-09 stsp
5908 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5909 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5910 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5911 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5912 56e0773d 2019-11-28 stsp
5913 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5914 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5915 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5916 2b496619 2019-07-10 stsp goto done;
5917 2b496619 2019-07-10 stsp }
5918 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5919 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5920 2b496619 2019-07-10 stsp if (err) {
5921 56e0773d 2019-11-28 stsp free(new_te);
5922 2b496619 2019-07-10 stsp goto done;
5923 2b496619 2019-07-10 stsp }
5924 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5925 2b496619 2019-07-10 stsp done:
5926 56e0773d 2019-11-28 stsp free(id);
5927 2b496619 2019-07-10 stsp free(subtree_path);
5928 2b496619 2019-07-10 stsp if (err == NULL)
5929 2b496619 2019-07-10 stsp *new_tep = new_te;
5930 036813ee 2019-05-09 stsp return err;
5931 036813ee 2019-05-09 stsp }
5932 036813ee 2019-05-09 stsp
5933 036813ee 2019-05-09 stsp static const struct got_error *
5934 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5935 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5936 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5937 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5938 036813ee 2019-05-09 stsp struct got_repository *repo)
5939 036813ee 2019-05-09 stsp {
5940 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5941 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5942 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5943 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5944 036813ee 2019-05-09 stsp
5945 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5946 ba580f68 2020-03-22 stsp *nentries = 0;
5947 036813ee 2019-05-09 stsp
5948 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5949 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5950 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5951 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5952 036813ee 2019-05-09 stsp
5953 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5954 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5955 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5956 036813ee 2019-05-09 stsp continue;
5957 036813ee 2019-05-09 stsp
5958 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5959 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5960 036813ee 2019-05-09 stsp continue;
5961 036813ee 2019-05-09 stsp
5962 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5963 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5964 036813ee 2019-05-09 stsp if (err)
5965 036813ee 2019-05-09 stsp goto done;
5966 036813ee 2019-05-09 stsp
5967 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5968 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5969 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5970 036813ee 2019-05-09 stsp if (err)
5971 036813ee 2019-05-09 stsp goto done;
5972 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5973 afa376bf 2019-05-09 stsp if (err)
5974 afa376bf 2019-05-09 stsp goto done;
5975 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5976 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5977 2b496619 2019-07-10 stsp if (err)
5978 2f51b5b3 2019-05-09 stsp goto done;
5979 ba580f68 2020-03-22 stsp (*nentries)++;
5980 2b496619 2019-07-10 stsp } else {
5981 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5982 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5983 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5984 2b496619 2019-07-10 stsp == NULL) {
5985 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5986 2b496619 2019-07-10 stsp child_path, path_base_tree,
5987 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5988 2b496619 2019-07-10 stsp repo);
5989 2b496619 2019-07-10 stsp if (err)
5990 2b496619 2019-07-10 stsp goto done;
5991 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5992 2b496619 2019-07-10 stsp if (err)
5993 2b496619 2019-07-10 stsp goto done;
5994 ba580f68 2020-03-22 stsp (*nentries)++;
5995 9ba0479c 2019-05-10 stsp }
5996 ed175427 2019-05-09 stsp }
5997 2f51b5b3 2019-05-09 stsp }
5998 2f51b5b3 2019-05-09 stsp
5999 2f51b5b3 2019-05-09 stsp if (base_tree) {
6000 56e0773d 2019-11-28 stsp int i, nbase_entries;
6001 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6002 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6003 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6004 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6005 63c5ca5d 2019-08-24 stsp
6006 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6007 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6008 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6009 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6010 63c5ca5d 2019-08-24 stsp if (err)
6011 63c5ca5d 2019-08-24 stsp goto done;
6012 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6013 63c5ca5d 2019-08-24 stsp if (err)
6014 63c5ca5d 2019-08-24 stsp goto done;
6015 ba580f68 2020-03-22 stsp (*nentries)++;
6016 63c5ca5d 2019-08-24 stsp continue;
6017 63c5ca5d 2019-08-24 stsp }
6018 2f51b5b3 2019-05-09 stsp
6019 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6020 44d03001 2019-05-09 stsp int modified;
6021 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6022 036813ee 2019-05-09 stsp if (err)
6023 036813ee 2019-05-09 stsp goto done;
6024 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6025 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6026 2f51b5b3 2019-05-09 stsp if (err)
6027 2f51b5b3 2019-05-09 stsp goto done;
6028 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6029 44d03001 2019-05-09 stsp if (modified) {
6030 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6031 ba580f68 2020-03-22 stsp int nsubentries;
6032 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6033 ba580f68 2020-03-22 stsp &nsubentries, te,
6034 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6035 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6036 44d03001 2019-05-09 stsp if (err)
6037 44d03001 2019-05-09 stsp goto done;
6038 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6039 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6040 ba580f68 2020-03-22 stsp free(new_id);
6041 ba580f68 2020-03-22 stsp continue;
6042 ba580f68 2020-03-22 stsp }
6043 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6044 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6045 56e0773d 2019-11-28 stsp free(new_id);
6046 44d03001 2019-05-09 stsp }
6047 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6048 036813ee 2019-05-09 stsp if (err)
6049 036813ee 2019-05-09 stsp goto done;
6050 ba580f68 2020-03-22 stsp (*nentries)++;
6051 2f51b5b3 2019-05-09 stsp continue;
6052 036813ee 2019-05-09 stsp }
6053 2f51b5b3 2019-05-09 stsp
6054 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6055 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6056 f66c734c 2019-09-22 stsp if (err)
6057 f66c734c 2019-09-22 stsp goto done;
6058 2f51b5b3 2019-05-09 stsp if (ct) {
6059 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6060 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6061 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6062 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6063 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6064 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6065 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6066 2f51b5b3 2019-05-09 stsp if (err)
6067 2f51b5b3 2019-05-09 stsp goto done;
6068 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6069 2f51b5b3 2019-05-09 stsp if (err)
6070 2f51b5b3 2019-05-09 stsp goto done;
6071 ba580f68 2020-03-22 stsp (*nentries)++;
6072 2f51b5b3 2019-05-09 stsp }
6073 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6074 b416585c 2019-05-13 stsp status_arg);
6075 afa376bf 2019-05-09 stsp if (err)
6076 afa376bf 2019-05-09 stsp goto done;
6077 2f51b5b3 2019-05-09 stsp } else {
6078 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6079 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6080 2f51b5b3 2019-05-09 stsp if (err)
6081 2f51b5b3 2019-05-09 stsp goto done;
6082 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6083 2f51b5b3 2019-05-09 stsp if (err)
6084 2f51b5b3 2019-05-09 stsp goto done;
6085 ba580f68 2020-03-22 stsp (*nentries)++;
6086 2f51b5b3 2019-05-09 stsp }
6087 ca2503ea 2019-05-09 stsp }
6088 ed175427 2019-05-09 stsp }
6089 0b5cc0d6 2019-05-09 stsp
6090 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6091 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6092 ed175427 2019-05-09 stsp done:
6093 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6094 2e1fa222 2020-07-23 stsp return err;
6095 2e1fa222 2020-07-23 stsp }
6096 2e1fa222 2020-07-23 stsp
6097 2e1fa222 2020-07-23 stsp static const struct got_error *
6098 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6099 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6100 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6101 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6102 ebf99748 2019-05-09 stsp {
6103 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6104 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6105 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6106 ebf99748 2019-05-09 stsp
6107 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6109 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6110 ebf99748 2019-05-09 stsp
6111 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6112 437adc9d 2020-12-10 yzhong
6113 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6114 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6115 437adc9d 2020-12-10 yzhong if (err)
6116 437adc9d 2020-12-10 yzhong goto done;
6117 437adc9d 2020-12-10 yzhong
6118 ebf99748 2019-05-09 stsp if (ie) {
6119 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6120 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6121 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6122 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6123 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6124 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6125 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6126 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6127 437adc9d 2020-12-10 yzhong
6128 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6129 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6130 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6131 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6132 72fd46fa 2019-09-06 stsp !have_staged_files);
6133 ebf99748 2019-05-09 stsp } else
6134 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6135 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6136 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6137 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6138 72fd46fa 2019-09-06 stsp !have_staged_files);
6139 ebf99748 2019-05-09 stsp } else {
6140 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6141 ebf99748 2019-05-09 stsp if (err)
6142 437adc9d 2020-12-10 yzhong goto done;
6143 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6144 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6145 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6146 3969253a 2020-03-07 stsp if (err) {
6147 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6148 437adc9d 2020-12-10 yzhong goto done;
6149 3969253a 2020-03-07 stsp }
6150 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6151 3969253a 2020-03-07 stsp if (err) {
6152 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6153 437adc9d 2020-12-10 yzhong goto done;
6154 3969253a 2020-03-07 stsp }
6155 ebf99748 2019-05-09 stsp }
6156 437adc9d 2020-12-10 yzhong free(relpath);
6157 437adc9d 2020-12-10 yzhong relpath = NULL;
6158 ebf99748 2019-05-09 stsp }
6159 437adc9d 2020-12-10 yzhong done:
6160 437adc9d 2020-12-10 yzhong free(relpath);
6161 d56d26ce 2019-05-10 stsp return err;
6162 d56d26ce 2019-05-10 stsp }
6163 735ef5ac 2019-08-03 stsp
6164 d56d26ce 2019-05-10 stsp
6165 d56d26ce 2019-05-10 stsp static const struct got_error *
6166 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6167 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6168 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6169 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6170 735ef5ac 2019-08-03 stsp int ood_errcode)
6171 d56d26ce 2019-05-10 stsp {
6172 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6173 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6174 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6175 1a36436d 2019-06-10 stsp
6176 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6177 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6178 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6179 1a36436d 2019-06-10 stsp return NULL;
6180 9bead371 2019-07-28 stsp /*
6181 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6182 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6183 9bead371 2019-07-28 stsp */
6184 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6185 945f9229 2022-04-16 thomas if (err)
6186 945f9229 2022-04-16 thomas goto done;
6187 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6188 9bead371 2019-07-28 stsp if (err) {
6189 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6190 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6191 1a36436d 2019-06-10 stsp goto done;
6192 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6193 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6194 1a36436d 2019-06-10 stsp } else {
6195 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6196 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6197 945f9229 2022-04-16 thomas if (err)
6198 945f9229 2022-04-16 thomas goto done;
6199 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6200 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6201 1a36436d 2019-06-10 stsp goto done;
6202 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6203 1a36436d 2019-06-10 stsp }
6204 1a36436d 2019-06-10 stsp done:
6205 1a36436d 2019-06-10 stsp free(id);
6206 945f9229 2022-04-16 thomas if (commit)
6207 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6208 1a36436d 2019-06-10 stsp return err;
6209 ebf99748 2019-05-09 stsp }
6210 ebf99748 2019-05-09 stsp
6211 ef20f542 2022-06-26 thomas static const struct got_error *
6212 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6213 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6214 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6215 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6216 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6217 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6218 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6219 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6220 afa376bf 2019-05-09 stsp struct got_repository *repo)
6221 c4296144 2019-05-09 stsp {
6222 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6223 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6224 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6225 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6226 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6227 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6228 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6229 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6230 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6231 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6232 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6233 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6234 892eab0a 2022-07-22 thomas time_t timestamp;
6235 c4296144 2019-05-09 stsp
6236 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6237 c4296144 2019-05-09 stsp
6238 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6239 675c7539 2019-05-09 stsp
6240 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6241 588edf97 2019-05-10 stsp if (err)
6242 588edf97 2019-05-10 stsp goto done;
6243 de18fc63 2019-05-09 stsp
6244 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6245 588edf97 2019-05-10 stsp if (err)
6246 588edf97 2019-05-10 stsp goto done;
6247 588edf97 2019-05-10 stsp
6248 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6249 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6250 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6251 33ad4cbe 2019-05-12 jcs if (err)
6252 33ad4cbe 2019-05-12 jcs goto done;
6253 33ad4cbe 2019-05-12 jcs }
6254 c4296144 2019-05-09 stsp
6255 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6256 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6257 33ad4cbe 2019-05-12 jcs goto done;
6258 33ad4cbe 2019-05-12 jcs }
6259 33ad4cbe 2019-05-12 jcs
6260 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6261 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6263 cf066bf8 2019-05-09 stsp char *ondisk_path;
6264 cf066bf8 2019-05-09 stsp
6265 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6266 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6267 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6268 5f8a88c6 2019-08-03 stsp continue;
6269 5f8a88c6 2019-08-03 stsp
6270 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6271 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6272 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6273 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6274 cf066bf8 2019-05-09 stsp continue;
6275 cf066bf8 2019-05-09 stsp
6276 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6277 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6278 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6279 cf066bf8 2019-05-09 stsp goto done;
6280 cf066bf8 2019-05-09 stsp }
6281 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6282 2e1fa222 2020-07-23 stsp free(ondisk_path);
6283 2e1fa222 2020-07-23 stsp if (err)
6284 cf066bf8 2019-05-09 stsp goto done;
6285 cf066bf8 2019-05-09 stsp }
6286 036813ee 2019-05-09 stsp
6287 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6288 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6289 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6290 036813ee 2019-05-09 stsp if (err)
6291 036813ee 2019-05-09 stsp goto done;
6292 036813ee 2019-05-09 stsp
6293 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6294 de18fc63 2019-05-09 stsp if (err)
6295 de18fc63 2019-05-09 stsp goto done;
6296 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6297 10604dce 2021-09-24 thomas nparents++;
6298 10604dce 2021-09-24 thomas if (parent_id2) {
6299 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6300 10604dce 2021-09-24 thomas if (err)
6301 10604dce 2021-09-24 thomas goto done;
6302 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6303 10604dce 2021-09-24 thomas nparents++;
6304 10604dce 2021-09-24 thomas }
6305 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6306 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6307 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6308 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6309 33ad4cbe 2019-05-12 jcs free(logmsg);
6310 9d40349a 2019-05-09 stsp if (err)
6311 9d40349a 2019-05-09 stsp goto done;
6312 9d40349a 2019-05-09 stsp
6313 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6314 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6315 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6316 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6317 09f5bd90 2019-05-09 stsp goto done;
6318 09f5bd90 2019-05-09 stsp }
6319 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6320 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6321 09f5bd90 2019-05-09 stsp if (err)
6322 09f5bd90 2019-05-09 stsp goto done;
6323 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6324 ebf99748 2019-05-09 stsp if (err)
6325 ebf99748 2019-05-09 stsp goto done;
6326 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6327 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6328 09f5bd90 2019-05-09 stsp goto done;
6329 09f5bd90 2019-05-09 stsp }
6330 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6331 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6332 f2c16586 2019-05-09 stsp if (err)
6333 f2c16586 2019-05-09 stsp goto done;
6334 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6335 f2c16586 2019-05-09 stsp if (err)
6336 f2c16586 2019-05-09 stsp goto done;
6337 f2c16586 2019-05-09 stsp
6338 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6339 09f5bd90 2019-05-09 stsp if (err)
6340 09f5bd90 2019-05-09 stsp goto done;
6341 09f5bd90 2019-05-09 stsp
6342 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6343 ebf99748 2019-05-09 stsp if (err)
6344 ebf99748 2019-05-09 stsp goto done;
6345 c4296144 2019-05-09 stsp done:
6346 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6347 588edf97 2019-05-10 stsp if (head_tree)
6348 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6349 588edf97 2019-05-10 stsp if (head_commit)
6350 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6351 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6352 2f17228e 2019-05-12 stsp if (head_ref2) {
6353 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6354 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6355 2f17228e 2019-05-12 stsp err = unlockerr;
6356 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6357 39cd0ff6 2019-07-12 stsp }
6358 39cd0ff6 2019-07-12 stsp return err;
6359 39cd0ff6 2019-07-12 stsp }
6360 5c1e53bc 2019-07-28 stsp
6361 5c1e53bc 2019-07-28 stsp static const struct got_error *
6362 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6363 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6364 5c1e53bc 2019-07-28 stsp {
6365 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6366 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6367 39cd0ff6 2019-07-12 stsp
6368 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6369 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6370 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6371 5c1e53bc 2019-07-28 stsp
6372 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6373 5c1e53bc 2019-07-28 stsp ct_path++;
6374 5c1e53bc 2019-07-28 stsp
6375 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6376 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6377 5c1e53bc 2019-07-28 stsp break;
6378 5c1e53bc 2019-07-28 stsp }
6379 5c1e53bc 2019-07-28 stsp
6380 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6381 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6382 5c1e53bc 2019-07-28 stsp
6383 5c1e53bc 2019-07-28 stsp return NULL;
6384 5c1e53bc 2019-07-28 stsp }
6385 5c1e53bc 2019-07-28 stsp
6386 f0b75401 2019-08-03 stsp static const struct got_error *
6387 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6388 f0b75401 2019-08-03 stsp {
6389 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6390 f0b75401 2019-08-03 stsp
6391 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6392 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6393 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6394 f0b75401 2019-08-03 stsp }
6395 f0b75401 2019-08-03 stsp
6396 f0b75401 2019-08-03 stsp return NULL;
6397 f0b75401 2019-08-03 stsp }
6398 f0b75401 2019-08-03 stsp
6399 5f8a88c6 2019-08-03 stsp static const struct got_error *
6400 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6401 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6402 5f8a88c6 2019-08-03 stsp {
6403 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6404 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6405 5f8a88c6 2019-08-03 stsp
6406 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6407 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6408 5f8a88c6 2019-08-03 stsp continue;
6409 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6410 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6411 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6412 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6413 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6414 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6415 5f8a88c6 2019-08-03 stsp }
6416 5f8a88c6 2019-08-03 stsp
6417 5f8a88c6 2019-08-03 stsp return NULL;
6418 5f8a88c6 2019-08-03 stsp }
6419 5f8a88c6 2019-08-03 stsp
6420 39cd0ff6 2019-07-12 stsp const struct got_error *
6421 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6422 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6423 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6424 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6425 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6426 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6427 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6428 39cd0ff6 2019-07-12 stsp {
6429 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6430 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6431 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6432 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6433 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6434 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6435 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6436 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6437 ef899790 2022-11-01 thomas char *diff_path = NULL;
6438 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6439 39cd0ff6 2019-07-12 stsp
6440 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6441 39cd0ff6 2019-07-12 stsp
6442 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6443 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6444 39cd0ff6 2019-07-12 stsp
6445 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6446 39cd0ff6 2019-07-12 stsp if (err)
6447 39cd0ff6 2019-07-12 stsp goto done;
6448 39cd0ff6 2019-07-12 stsp
6449 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6450 39cd0ff6 2019-07-12 stsp if (err)
6451 39cd0ff6 2019-07-12 stsp goto done;
6452 39cd0ff6 2019-07-12 stsp
6453 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6454 39cd0ff6 2019-07-12 stsp if (err)
6455 39cd0ff6 2019-07-12 stsp goto done;
6456 39cd0ff6 2019-07-12 stsp
6457 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6458 39cd0ff6 2019-07-12 stsp if (err)
6459 39cd0ff6 2019-07-12 stsp goto done;
6460 39cd0ff6 2019-07-12 stsp
6461 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6462 5f8a88c6 2019-08-03 stsp &have_staged_files);
6463 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6464 5f8a88c6 2019-08-03 stsp goto done;
6465 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6466 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6467 5f8a88c6 2019-08-03 stsp if (err)
6468 5f8a88c6 2019-08-03 stsp goto done;
6469 5f8a88c6 2019-08-03 stsp }
6470 5f8a88c6 2019-08-03 stsp
6471 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6472 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6473 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6474 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6475 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6476 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6477 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6478 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6479 ef899790 2022-11-01 thomas if (show_diff) {
6480 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6481 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6482 ef899790 2022-11-01 thomas if (err)
6483 ef899790 2022-11-01 thomas goto done;
6484 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6485 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6486 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6487 ef899790 2022-11-01 thomas goto done;
6488 ef899790 2022-11-01 thomas }
6489 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6490 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6491 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6492 ef899790 2022-11-01 thomas goto done;
6493 ef899790 2022-11-01 thomas }
6494 ef899790 2022-11-01 thomas }
6495 ef899790 2022-11-01 thomas
6496 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6497 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6498 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6499 5c1e53bc 2019-07-28 stsp if (err)
6500 5c1e53bc 2019-07-28 stsp goto done;
6501 5c1e53bc 2019-07-28 stsp }
6502 39cd0ff6 2019-07-12 stsp
6503 ef899790 2022-11-01 thomas if (show_diff) {
6504 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6505 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6506 ef899790 2022-11-01 thomas goto done;
6507 ef899790 2022-11-01 thomas }
6508 ef899790 2022-11-01 thomas }
6509 ef899790 2022-11-01 thomas
6510 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6511 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6512 39cd0ff6 2019-07-12 stsp goto done;
6513 5c1e53bc 2019-07-28 stsp }
6514 5c1e53bc 2019-07-28 stsp
6515 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6516 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6517 5c1e53bc 2019-07-28 stsp if (err)
6518 5c1e53bc 2019-07-28 stsp goto done;
6519 39cd0ff6 2019-07-12 stsp }
6520 f0b75401 2019-08-03 stsp
6521 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6522 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6523 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6524 f0b75401 2019-08-03 stsp
6525 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6526 f0b75401 2019-08-03 stsp ct_path++;
6527 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6528 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6529 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6530 b50cabdf 2019-07-12 stsp if (err)
6531 b50cabdf 2019-07-12 stsp goto done;
6532 f0b75401 2019-08-03 stsp
6533 b50cabdf 2019-07-12 stsp }
6534 b50cabdf 2019-07-12 stsp
6535 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6536 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6537 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6538 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6539 39cd0ff6 2019-07-12 stsp if (err)
6540 39cd0ff6 2019-07-12 stsp goto done;
6541 39cd0ff6 2019-07-12 stsp
6542 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6543 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6544 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6545 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6546 39cd0ff6 2019-07-12 stsp err = sync_err;
6547 39cd0ff6 2019-07-12 stsp done:
6548 39cd0ff6 2019-07-12 stsp if (fileindex)
6549 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6550 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6551 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6552 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6553 39cd0ff6 2019-07-12 stsp err = unlockerr;
6554 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6555 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6556 21c2d8be 2023-01-10 thomas
6557 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6558 39cd0ff6 2019-07-12 stsp }
6559 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6560 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6561 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6562 ef899790 2022-11-01 thomas free(diff_path);
6563 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6564 ef899790 2022-11-01 thomas err == NULL)
6565 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6566 c4296144 2019-05-09 stsp return err;
6567 8656d6c4 2019-05-20 stsp }
6568 8656d6c4 2019-05-20 stsp
6569 8656d6c4 2019-05-20 stsp const char *
6570 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6571 8656d6c4 2019-05-20 stsp {
6572 8656d6c4 2019-05-20 stsp return ct->path;
6573 8656d6c4 2019-05-20 stsp }
6574 8656d6c4 2019-05-20 stsp
6575 8656d6c4 2019-05-20 stsp unsigned int
6576 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6577 8656d6c4 2019-05-20 stsp {
6578 8656d6c4 2019-05-20 stsp return ct->status;
6579 818c7501 2019-07-11 stsp }
6580 818c7501 2019-07-11 stsp
6581 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6582 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6583 818c7501 2019-07-11 stsp struct got_repository *repo;
6584 818c7501 2019-07-11 stsp };
6585 818c7501 2019-07-11 stsp
6586 818c7501 2019-07-11 stsp static const struct got_error *
6587 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6588 818c7501 2019-07-11 stsp {
6589 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6590 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6591 818c7501 2019-07-11 stsp unsigned char status;
6592 818c7501 2019-07-11 stsp struct stat sb;
6593 818c7501 2019-07-11 stsp char *ondisk_path;
6594 818c7501 2019-07-11 stsp
6595 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6596 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6597 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6598 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6599 818c7501 2019-07-11 stsp
6600 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6601 818c7501 2019-07-11 stsp == -1)
6602 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6603 818c7501 2019-07-11 stsp
6604 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6605 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6606 818c7501 2019-07-11 stsp free(ondisk_path);
6607 818c7501 2019-07-11 stsp if (err)
6608 818c7501 2019-07-11 stsp return err;
6609 818c7501 2019-07-11 stsp
6610 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6611 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6612 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6613 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6614 818c7501 2019-07-11 stsp
6615 818c7501 2019-07-11 stsp return NULL;
6616 818c7501 2019-07-11 stsp }
6617 818c7501 2019-07-11 stsp
6618 818c7501 2019-07-11 stsp const struct got_error *
6619 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6620 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6621 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6622 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6623 818c7501 2019-07-11 stsp {
6624 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6625 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6626 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6627 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6628 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6629 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6630 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6631 818c7501 2019-07-11 stsp
6632 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6633 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6634 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6635 818c7501 2019-07-11 stsp
6636 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6637 818c7501 2019-07-11 stsp if (err)
6638 818c7501 2019-07-11 stsp return err;
6639 818c7501 2019-07-11 stsp
6640 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6641 818c7501 2019-07-11 stsp if (err)
6642 818c7501 2019-07-11 stsp goto done;
6643 818c7501 2019-07-11 stsp
6644 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6645 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6646 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6647 818c7501 2019-07-11 stsp &ok_arg);
6648 818c7501 2019-07-11 stsp if (err)
6649 818c7501 2019-07-11 stsp goto done;
6650 818c7501 2019-07-11 stsp
6651 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6652 818c7501 2019-07-11 stsp if (err)
6653 818c7501 2019-07-11 stsp goto done;
6654 818c7501 2019-07-11 stsp
6655 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6656 818c7501 2019-07-11 stsp if (err)
6657 818c7501 2019-07-11 stsp goto done;
6658 818c7501 2019-07-11 stsp
6659 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6660 818c7501 2019-07-11 stsp if (err)
6661 818c7501 2019-07-11 stsp goto done;
6662 818c7501 2019-07-11 stsp
6663 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6664 818c7501 2019-07-11 stsp 0);
6665 e51d7b55 2020-01-04 stsp if (err)
6666 e51d7b55 2020-01-04 stsp goto done;
6667 e51d7b55 2020-01-04 stsp
6668 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6669 818c7501 2019-07-11 stsp if (err)
6670 818c7501 2019-07-11 stsp goto done;
6671 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6672 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6673 e51d7b55 2020-01-04 stsp goto done;
6674 e51d7b55 2020-01-04 stsp }
6675 818c7501 2019-07-11 stsp
6676 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6677 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6678 818c7501 2019-07-11 stsp if (err)
6679 818c7501 2019-07-11 stsp goto done;
6680 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6681 818c7501 2019-07-11 stsp if (err)
6682 818c7501 2019-07-11 stsp goto done;
6683 818c7501 2019-07-11 stsp
6684 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6685 818c7501 2019-07-11 stsp
6686 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6687 818c7501 2019-07-11 stsp if (err)
6688 818c7501 2019-07-11 stsp goto done;
6689 818c7501 2019-07-11 stsp
6690 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6691 818c7501 2019-07-11 stsp if (err)
6692 818c7501 2019-07-11 stsp goto done;
6693 818c7501 2019-07-11 stsp
6694 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6695 818c7501 2019-07-11 stsp worktree->base_commit_id);
6696 818c7501 2019-07-11 stsp if (err)
6697 818c7501 2019-07-11 stsp goto done;
6698 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6699 818c7501 2019-07-11 stsp if (err)
6700 818c7501 2019-07-11 stsp goto done;
6701 818c7501 2019-07-11 stsp
6702 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6703 818c7501 2019-07-11 stsp if (err)
6704 818c7501 2019-07-11 stsp goto done;
6705 818c7501 2019-07-11 stsp done:
6706 818c7501 2019-07-11 stsp free(fileindex_path);
6707 818c7501 2019-07-11 stsp free(tmp_branch_name);
6708 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6709 818c7501 2019-07-11 stsp free(branch_ref_name);
6710 818c7501 2019-07-11 stsp if (branch_ref)
6711 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6712 818c7501 2019-07-11 stsp if (wt_branch)
6713 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6714 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6715 818c7501 2019-07-11 stsp if (err) {
6716 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6717 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6718 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6719 818c7501 2019-07-11 stsp }
6720 818c7501 2019-07-11 stsp if (*tmp_branch) {
6721 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6722 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6723 818c7501 2019-07-11 stsp }
6724 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6725 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6726 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6727 3e3a69f1 2019-07-25 stsp }
6728 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6729 818c7501 2019-07-11 stsp }
6730 818c7501 2019-07-11 stsp return err;
6731 818c7501 2019-07-11 stsp }
6732 818c7501 2019-07-11 stsp
6733 818c7501 2019-07-11 stsp const struct got_error *
6734 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6735 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6736 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6737 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6738 818c7501 2019-07-11 stsp {
6739 818c7501 2019-07-11 stsp const struct got_error *err;
6740 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6741 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6742 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6743 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6744 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6745 818c7501 2019-07-11 stsp
6746 818c7501 2019-07-11 stsp *commit_id = NULL;
6747 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6748 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6749 3e3a69f1 2019-07-25 stsp *branch = NULL;
6750 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6751 3e3a69f1 2019-07-25 stsp
6752 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6753 3e3a69f1 2019-07-25 stsp if (err)
6754 3e3a69f1 2019-07-25 stsp return err;
6755 818c7501 2019-07-11 stsp
6756 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6757 3e3a69f1 2019-07-25 stsp if (err)
6758 f032f1f7 2019-08-04 stsp goto done;
6759 f032f1f7 2019-08-04 stsp
6760 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6761 f032f1f7 2019-08-04 stsp &have_staged_files);
6762 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6763 f032f1f7 2019-08-04 stsp goto done;
6764 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6765 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6766 3e3a69f1 2019-07-25 stsp goto done;
6767 f032f1f7 2019-08-04 stsp }
6768 3e3a69f1 2019-07-25 stsp
6769 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6770 818c7501 2019-07-11 stsp if (err)
6771 f032f1f7 2019-08-04 stsp goto done;
6772 818c7501 2019-07-11 stsp
6773 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6774 818c7501 2019-07-11 stsp if (err)
6775 818c7501 2019-07-11 stsp goto done;
6776 818c7501 2019-07-11 stsp
6777 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
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 = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
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_open(&branch_ref, repo, branch_ref_name, 0);
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(branch, repo,
6790 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 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(&commit_ref, repo, commit_ref_name, 0);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp goto done;
6797 818c7501 2019-07-11 stsp
6798 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6799 818c7501 2019-07-11 stsp if (err)
6800 818c7501 2019-07-11 stsp goto done;
6801 818c7501 2019-07-11 stsp
6802 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6803 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6804 818c7501 2019-07-11 stsp if (err)
6805 818c7501 2019-07-11 stsp goto done;
6806 818c7501 2019-07-11 stsp
6807 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6808 818c7501 2019-07-11 stsp if (err)
6809 818c7501 2019-07-11 stsp goto done;
6810 818c7501 2019-07-11 stsp done:
6811 818c7501 2019-07-11 stsp free(commit_ref_name);
6812 818c7501 2019-07-11 stsp free(branch_ref_name);
6813 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6814 818c7501 2019-07-11 stsp if (commit_ref)
6815 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6816 818c7501 2019-07-11 stsp if (branch_ref)
6817 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6818 818c7501 2019-07-11 stsp if (err) {
6819 818c7501 2019-07-11 stsp free(*commit_id);
6820 818c7501 2019-07-11 stsp *commit_id = NULL;
6821 818c7501 2019-07-11 stsp if (*tmp_branch) {
6822 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6823 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6824 818c7501 2019-07-11 stsp }
6825 818c7501 2019-07-11 stsp if (*new_base_branch) {
6826 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6827 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6828 818c7501 2019-07-11 stsp }
6829 818c7501 2019-07-11 stsp if (*branch) {
6830 818c7501 2019-07-11 stsp got_ref_close(*branch);
6831 818c7501 2019-07-11 stsp *branch = NULL;
6832 818c7501 2019-07-11 stsp }
6833 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6834 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6835 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6836 3e3a69f1 2019-07-25 stsp }
6837 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6838 818c7501 2019-07-11 stsp }
6839 818c7501 2019-07-11 stsp return err;
6840 c4296144 2019-05-09 stsp }
6841 818c7501 2019-07-11 stsp
6842 818c7501 2019-07-11 stsp const struct got_error *
6843 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6844 818c7501 2019-07-11 stsp {
6845 818c7501 2019-07-11 stsp const struct got_error *err;
6846 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6847 818c7501 2019-07-11 stsp
6848 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6849 818c7501 2019-07-11 stsp if (err)
6850 818c7501 2019-07-11 stsp return err;
6851 818c7501 2019-07-11 stsp
6852 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6853 818c7501 2019-07-11 stsp free(tmp_branch_name);
6854 818c7501 2019-07-11 stsp return NULL;
6855 818c7501 2019-07-11 stsp }
6856 818c7501 2019-07-11 stsp
6857 818c7501 2019-07-11 stsp static const struct got_error *
6858 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6859 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6860 818c7501 2019-07-11 stsp {
6861 0ebf8283 2019-07-24 stsp *logmsg = arg;
6862 818c7501 2019-07-11 stsp return NULL;
6863 818c7501 2019-07-11 stsp }
6864 818c7501 2019-07-11 stsp
6865 818c7501 2019-07-11 stsp static const struct got_error *
6866 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6867 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6868 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6869 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6870 818c7501 2019-07-11 stsp {
6871 818c7501 2019-07-11 stsp return NULL;
6872 01757395 2019-07-12 stsp }
6873 01757395 2019-07-12 stsp
6874 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6875 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6876 01757395 2019-07-12 stsp void *progress_arg;
6877 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6878 01757395 2019-07-12 stsp };
6879 01757395 2019-07-12 stsp
6880 01757395 2019-07-12 stsp static const struct got_error *
6881 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6882 01757395 2019-07-12 stsp {
6883 01757395 2019-07-12 stsp const struct got_error *err;
6884 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6885 01757395 2019-07-12 stsp char *p;
6886 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6887 01757395 2019-07-12 stsp
6888 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6889 01757395 2019-07-12 stsp if (err)
6890 01757395 2019-07-12 stsp return err;
6891 01757395 2019-07-12 stsp
6892 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6893 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6894 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6895 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6896 01757395 2019-07-12 stsp return NULL;
6897 01757395 2019-07-12 stsp
6898 01757395 2019-07-12 stsp p = strdup(path);
6899 01757395 2019-07-12 stsp if (p == NULL)
6900 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6901 01757395 2019-07-12 stsp
6902 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6903 01757395 2019-07-12 stsp if (err || new == NULL)
6904 01757395 2019-07-12 stsp free(p);
6905 01757395 2019-07-12 stsp return err;
6906 818c7501 2019-07-11 stsp }
6907 818c7501 2019-07-11 stsp
6908 0ebf8283 2019-07-24 stsp static const struct got_error *
6909 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6910 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6911 818c7501 2019-07-11 stsp {
6912 818c7501 2019-07-11 stsp const struct got_error *err;
6913 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6914 818c7501 2019-07-11 stsp
6915 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6916 818c7501 2019-07-11 stsp if (err) {
6917 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6918 818c7501 2019-07-11 stsp goto done;
6919 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6920 818c7501 2019-07-11 stsp if (err)
6921 818c7501 2019-07-11 stsp goto done;
6922 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6923 818c7501 2019-07-11 stsp if (err)
6924 818c7501 2019-07-11 stsp goto done;
6925 de05890f 2020-03-05 stsp } else if (is_rebase) {
6926 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6927 818c7501 2019-07-11 stsp int cmp;
6928 818c7501 2019-07-11 stsp
6929 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6930 818c7501 2019-07-11 stsp if (err)
6931 818c7501 2019-07-11 stsp goto done;
6932 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6933 818c7501 2019-07-11 stsp free(stored_id);
6934 818c7501 2019-07-11 stsp if (cmp != 0) {
6935 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6936 818c7501 2019-07-11 stsp goto done;
6937 818c7501 2019-07-11 stsp }
6938 818c7501 2019-07-11 stsp }
6939 0ebf8283 2019-07-24 stsp done:
6940 0ebf8283 2019-07-24 stsp if (commit_ref)
6941 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6942 0ebf8283 2019-07-24 stsp return err;
6943 0ebf8283 2019-07-24 stsp }
6944 0ebf8283 2019-07-24 stsp
6945 0ebf8283 2019-07-24 stsp static const struct got_error *
6946 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6947 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6948 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6949 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6950 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6951 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6952 0ebf8283 2019-07-24 stsp {
6953 0ebf8283 2019-07-24 stsp const struct got_error *err;
6954 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6955 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6956 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6957 818c7501 2019-07-11 stsp
6958 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6959 0ebf8283 2019-07-24 stsp
6960 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6961 0ebf8283 2019-07-24 stsp if (err)
6962 0ebf8283 2019-07-24 stsp return err;
6963 0ebf8283 2019-07-24 stsp
6964 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6965 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6966 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6967 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6968 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6969 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6970 818c7501 2019-07-11 stsp if (commit_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6972 818c7501 2019-07-11 stsp return err;
6973 818c7501 2019-07-11 stsp }
6974 818c7501 2019-07-11 stsp
6975 818c7501 2019-07-11 stsp const struct got_error *
6976 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6977 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6978 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6979 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6980 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6981 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6982 818c7501 2019-07-11 stsp {
6983 0ebf8283 2019-07-24 stsp const struct got_error *err;
6984 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6985 0ebf8283 2019-07-24 stsp
6986 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6987 0ebf8283 2019-07-24 stsp if (err)
6988 0ebf8283 2019-07-24 stsp return err;
6989 0ebf8283 2019-07-24 stsp
6990 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6991 0ebf8283 2019-07-24 stsp if (err)
6992 0ebf8283 2019-07-24 stsp goto done;
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6995 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6996 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6997 0ebf8283 2019-07-24 stsp done:
6998 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6999 0ebf8283 2019-07-24 stsp return err;
7000 0ebf8283 2019-07-24 stsp }
7001 0ebf8283 2019-07-24 stsp
7002 0ebf8283 2019-07-24 stsp const struct got_error *
7003 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7004 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7005 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7006 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7007 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7008 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7009 0ebf8283 2019-07-24 stsp {
7010 0ebf8283 2019-07-24 stsp const struct got_error *err;
7011 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7012 0ebf8283 2019-07-24 stsp
7013 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7014 0ebf8283 2019-07-24 stsp if (err)
7015 0ebf8283 2019-07-24 stsp return err;
7016 0ebf8283 2019-07-24 stsp
7017 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7018 0ebf8283 2019-07-24 stsp if (err)
7019 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7022 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7023 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7024 0ebf8283 2019-07-24 stsp done:
7025 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7026 0ebf8283 2019-07-24 stsp return err;
7027 0ebf8283 2019-07-24 stsp }
7028 0ebf8283 2019-07-24 stsp
7029 0ebf8283 2019-07-24 stsp static const struct got_error *
7030 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7031 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7032 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7033 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7034 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7035 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7036 0ebf8283 2019-07-24 stsp {
7037 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7038 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7039 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7040 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7041 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7042 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7043 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7044 818c7501 2019-07-11 stsp
7045 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7046 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7047 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7048 a0e95631 2019-07-12 stsp
7049 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7050 818c7501 2019-07-11 stsp
7051 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7052 a0e95631 2019-07-12 stsp if (err)
7053 3e3a69f1 2019-07-25 stsp return err;
7054 a0e95631 2019-07-12 stsp
7055 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7056 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7057 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7058 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7059 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7060 01757395 2019-07-12 stsp /*
7061 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7062 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7063 6c13b005 2021-09-02 stsp *
7064 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7065 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7066 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7067 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7068 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7069 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7070 01757395 2019-07-12 stsp */
7071 01757395 2019-07-12 stsp if (merged_paths) {
7072 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7073 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7074 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7075 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7076 f2a9dc41 2019-12-13 tracey 0);
7077 01757395 2019-07-12 stsp if (err)
7078 01757395 2019-07-12 stsp goto done;
7079 01757395 2019-07-12 stsp }
7080 01757395 2019-07-12 stsp } else {
7081 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7082 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7083 01757395 2019-07-12 stsp if (err)
7084 01757395 2019-07-12 stsp goto done;
7085 01757395 2019-07-12 stsp }
7086 a0e95631 2019-07-12 stsp
7087 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7088 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7089 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7090 a0e95631 2019-07-12 stsp if (err)
7091 a0e95631 2019-07-12 stsp goto done;
7092 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7093 a0e95631 2019-07-12 stsp goto done;
7094 ff0d2220 2019-07-11 stsp }
7095 818c7501 2019-07-11 stsp
7096 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7097 edd02c5e 2019-07-11 stsp if (err)
7098 edd02c5e 2019-07-11 stsp goto done;
7099 edd02c5e 2019-07-11 stsp
7100 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7101 818c7501 2019-07-11 stsp if (err)
7102 818c7501 2019-07-11 stsp goto done;
7103 818c7501 2019-07-11 stsp
7104 5943eee2 2019-08-13 stsp if (new_logmsg) {
7105 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7106 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7107 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7108 5943eee2 2019-08-13 stsp goto done;
7109 5943eee2 2019-08-13 stsp }
7110 5943eee2 2019-08-13 stsp } else {
7111 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7112 5943eee2 2019-08-13 stsp if (err)
7113 5943eee2 2019-08-13 stsp goto done;
7114 5943eee2 2019-08-13 stsp }
7115 0ebf8283 2019-07-24 stsp
7116 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7117 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7118 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7119 8db00f97 2022-07-22 thomas committer ? committer :
7120 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7121 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7122 a0e95631 2019-07-12 stsp if (err)
7123 a0e95631 2019-07-12 stsp goto done;
7124 a0e95631 2019-07-12 stsp
7125 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7126 a0e95631 2019-07-12 stsp if (err)
7127 a0e95631 2019-07-12 stsp goto done;
7128 a0e95631 2019-07-12 stsp
7129 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7130 a0e95631 2019-07-12 stsp if (err)
7131 a0e95631 2019-07-12 stsp goto done;
7132 a0e95631 2019-07-12 stsp
7133 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7134 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7135 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7136 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7137 a0e95631 2019-07-12 stsp err = sync_err;
7138 818c7501 2019-07-11 stsp done:
7139 a0e95631 2019-07-12 stsp free(fileindex_path);
7140 a0e95631 2019-07-12 stsp free(head_commit_id);
7141 a0e95631 2019-07-12 stsp if (head_ref)
7142 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7143 818c7501 2019-07-11 stsp if (err) {
7144 818c7501 2019-07-11 stsp free(*new_commit_id);
7145 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7146 818c7501 2019-07-11 stsp }
7147 818c7501 2019-07-11 stsp return err;
7148 818c7501 2019-07-11 stsp }
7149 818c7501 2019-07-11 stsp
7150 818c7501 2019-07-11 stsp const struct got_error *
7151 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7152 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7153 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7154 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7155 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7156 be94c032 2023-02-20 thomas struct got_repository *repo)
7157 0ebf8283 2019-07-24 stsp {
7158 0ebf8283 2019-07-24 stsp const struct got_error *err;
7159 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7160 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7161 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7162 0ebf8283 2019-07-24 stsp
7163 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7164 0ebf8283 2019-07-24 stsp if (err)
7165 0ebf8283 2019-07-24 stsp return err;
7166 0ebf8283 2019-07-24 stsp
7167 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7168 0ebf8283 2019-07-24 stsp if (err)
7169 0ebf8283 2019-07-24 stsp goto done;
7170 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7171 0ebf8283 2019-07-24 stsp if (err)
7172 0ebf8283 2019-07-24 stsp goto done;
7173 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7174 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7175 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp
7178 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7179 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7180 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7181 0ebf8283 2019-07-24 stsp done:
7182 0ebf8283 2019-07-24 stsp if (commit_ref)
7183 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7184 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7185 0ebf8283 2019-07-24 stsp free(commit_id);
7186 0ebf8283 2019-07-24 stsp return err;
7187 0ebf8283 2019-07-24 stsp }
7188 0ebf8283 2019-07-24 stsp
7189 0ebf8283 2019-07-24 stsp const struct got_error *
7190 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7191 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7192 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7193 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7194 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7195 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7196 0ebf8283 2019-07-24 stsp {
7197 0ebf8283 2019-07-24 stsp const struct got_error *err;
7198 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7199 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7200 0ebf8283 2019-07-24 stsp
7201 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7202 0ebf8283 2019-07-24 stsp if (err)
7203 0ebf8283 2019-07-24 stsp return err;
7204 0ebf8283 2019-07-24 stsp
7205 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7206 0ebf8283 2019-07-24 stsp if (err)
7207 0ebf8283 2019-07-24 stsp goto done;
7208 0ebf8283 2019-07-24 stsp
7209 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7210 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7211 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7212 0ebf8283 2019-07-24 stsp done:
7213 0ebf8283 2019-07-24 stsp if (commit_ref)
7214 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7215 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7216 0ebf8283 2019-07-24 stsp return err;
7217 0ebf8283 2019-07-24 stsp }
7218 0ebf8283 2019-07-24 stsp
7219 0ebf8283 2019-07-24 stsp const struct got_error *
7220 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7221 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7222 818c7501 2019-07-11 stsp {
7223 3e3a69f1 2019-07-25 stsp if (fileindex)
7224 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7225 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7226 69844fba 2019-07-11 stsp }
7227 69844fba 2019-07-11 stsp
7228 69844fba 2019-07-11 stsp static const struct got_error *
7229 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7230 69844fba 2019-07-11 stsp {
7231 69844fba 2019-07-11 stsp const struct got_error *err;
7232 69844fba 2019-07-11 stsp struct got_reference *ref;
7233 69844fba 2019-07-11 stsp
7234 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7235 69844fba 2019-07-11 stsp if (err) {
7236 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7237 69844fba 2019-07-11 stsp return NULL;
7238 69844fba 2019-07-11 stsp return err;
7239 69844fba 2019-07-11 stsp }
7240 69844fba 2019-07-11 stsp
7241 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7242 69844fba 2019-07-11 stsp got_ref_close(ref);
7243 69844fba 2019-07-11 stsp return err;
7244 818c7501 2019-07-11 stsp }
7245 818c7501 2019-07-11 stsp
7246 69844fba 2019-07-11 stsp static const struct got_error *
7247 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7248 69844fba 2019-07-11 stsp {
7249 69844fba 2019-07-11 stsp const struct got_error *err;
7250 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7251 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7252 69844fba 2019-07-11 stsp
7253 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7254 69844fba 2019-07-11 stsp if (err)
7255 69844fba 2019-07-11 stsp goto done;
7256 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7257 69844fba 2019-07-11 stsp if (err)
7258 69844fba 2019-07-11 stsp goto done;
7259 69844fba 2019-07-11 stsp
7260 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7261 69844fba 2019-07-11 stsp if (err)
7262 69844fba 2019-07-11 stsp goto done;
7263 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7264 69844fba 2019-07-11 stsp if (err)
7265 69844fba 2019-07-11 stsp goto done;
7266 69844fba 2019-07-11 stsp
7267 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7268 69844fba 2019-07-11 stsp if (err)
7269 69844fba 2019-07-11 stsp goto done;
7270 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7271 69844fba 2019-07-11 stsp if (err)
7272 69844fba 2019-07-11 stsp goto done;
7273 69844fba 2019-07-11 stsp
7274 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7275 69844fba 2019-07-11 stsp if (err)
7276 69844fba 2019-07-11 stsp goto done;
7277 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7278 69844fba 2019-07-11 stsp if (err)
7279 69844fba 2019-07-11 stsp goto done;
7280 69844fba 2019-07-11 stsp
7281 69844fba 2019-07-11 stsp done:
7282 69844fba 2019-07-11 stsp free(tmp_branch_name);
7283 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7284 69844fba 2019-07-11 stsp free(branch_ref_name);
7285 69844fba 2019-07-11 stsp free(commit_ref_name);
7286 e600f124 2021-03-21 stsp return err;
7287 e600f124 2021-03-21 stsp }
7288 e600f124 2021-03-21 stsp
7289 ef20f542 2022-06-26 thomas static const struct got_error *
7290 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7291 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7292 e600f124 2021-03-21 stsp {
7293 e600f124 2021-03-21 stsp const struct got_error *err;
7294 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7295 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7296 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7297 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7298 e600f124 2021-03-21 stsp char *refname = NULL;
7299 e600f124 2021-03-21 stsp
7300 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7301 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7302 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7303 e600f124 2021-03-21 stsp branch_name += 11;
7304 e600f124 2021-03-21 stsp
7305 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7306 e600f124 2021-03-21 stsp if (err)
7307 e600f124 2021-03-21 stsp return err;
7308 e600f124 2021-03-21 stsp
7309 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7310 e600f124 2021-03-21 stsp new_id_str) == -1) {
7311 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7312 e600f124 2021-03-21 stsp goto done;
7313 e600f124 2021-03-21 stsp }
7314 e600f124 2021-03-21 stsp
7315 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7316 e600f124 2021-03-21 stsp if (err)
7317 e600f124 2021-03-21 stsp goto done;
7318 e600f124 2021-03-21 stsp
7319 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7320 e600f124 2021-03-21 stsp if (err)
7321 e600f124 2021-03-21 stsp goto done;
7322 e600f124 2021-03-21 stsp
7323 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7324 e600f124 2021-03-21 stsp done:
7325 e600f124 2021-03-21 stsp free(new_id_str);
7326 e600f124 2021-03-21 stsp free(refname);
7327 e600f124 2021-03-21 stsp free(old_commit_id);
7328 e600f124 2021-03-21 stsp if (ref)
7329 e600f124 2021-03-21 stsp got_ref_close(ref);
7330 69844fba 2019-07-11 stsp return err;
7331 69844fba 2019-07-11 stsp }
7332 69844fba 2019-07-11 stsp
7333 818c7501 2019-07-11 stsp const struct got_error *
7334 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7335 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7336 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7337 f08f28be 2023-02-03 thomas int create_backup)
7338 818c7501 2019-07-11 stsp {
7339 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7340 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7341 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7342 818c7501 2019-07-11 stsp
7343 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7344 818c7501 2019-07-11 stsp if (err)
7345 818c7501 2019-07-11 stsp return err;
7346 e600f124 2021-03-21 stsp
7347 e600f124 2021-03-21 stsp if (create_backup) {
7348 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7349 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7350 e600f124 2021-03-21 stsp if (err)
7351 e600f124 2021-03-21 stsp goto done;
7352 e600f124 2021-03-21 stsp }
7353 818c7501 2019-07-11 stsp
7354 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7355 818c7501 2019-07-11 stsp if (err)
7356 818c7501 2019-07-11 stsp goto done;
7357 818c7501 2019-07-11 stsp
7358 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7359 818c7501 2019-07-11 stsp if (err)
7360 818c7501 2019-07-11 stsp goto done;
7361 818c7501 2019-07-11 stsp
7362 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7363 818c7501 2019-07-11 stsp if (err)
7364 818c7501 2019-07-11 stsp goto done;
7365 818c7501 2019-07-11 stsp
7366 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7367 a615e0e7 2020-12-16 stsp if (err)
7368 a615e0e7 2020-12-16 stsp goto done;
7369 a615e0e7 2020-12-16 stsp
7370 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7371 a615e0e7 2020-12-16 stsp if (err)
7372 a615e0e7 2020-12-16 stsp goto done;
7373 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7374 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7375 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7376 a615e0e7 2020-12-16 stsp err = sync_err;
7377 818c7501 2019-07-11 stsp done:
7378 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7379 a615e0e7 2020-12-16 stsp free(fileindex_path);
7380 818c7501 2019-07-11 stsp free(new_head_commit_id);
7381 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7382 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7383 818c7501 2019-07-11 stsp err = unlockerr;
7384 818c7501 2019-07-11 stsp return err;
7385 818c7501 2019-07-11 stsp }
7386 8641a332 2023-04-14 thomas
7387 8641a332 2023-04-14 thomas static const struct got_error *
7388 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7389 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7390 8641a332 2023-04-14 thomas struct got_repository *repo)
7391 8641a332 2023-04-14 thomas {
7392 8641a332 2023-04-14 thomas const struct got_error *err;
7393 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7394 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7395 8641a332 2023-04-14 thomas
7396 8641a332 2023-04-14 thomas if (id1) {
7397 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7398 8641a332 2023-04-14 thomas if (err)
7399 8641a332 2023-04-14 thomas goto done;
7400 8641a332 2023-04-14 thomas
7401 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7402 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7403 8641a332 2023-04-14 thomas if (err)
7404 8641a332 2023-04-14 thomas goto done;
7405 8641a332 2023-04-14 thomas }
7406 818c7501 2019-07-11 stsp
7407 8641a332 2023-04-14 thomas if (id2) {
7408 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7409 8641a332 2023-04-14 thomas if (err)
7410 8641a332 2023-04-14 thomas goto done;
7411 8641a332 2023-04-14 thomas
7412 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7413 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7414 8641a332 2023-04-14 thomas if (err)
7415 8641a332 2023-04-14 thomas goto done;
7416 8641a332 2023-04-14 thomas }
7417 8641a332 2023-04-14 thomas
7418 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7419 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7420 8641a332 2023-04-14 thomas if (err)
7421 8641a332 2023-04-14 thomas goto done;
7422 8641a332 2023-04-14 thomas done:
7423 8641a332 2023-04-14 thomas if (commit1)
7424 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7425 8641a332 2023-04-14 thomas if (commit2)
7426 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7427 8641a332 2023-04-14 thomas if (tree1)
7428 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7429 8641a332 2023-04-14 thomas if (tree2)
7430 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7431 8641a332 2023-04-14 thomas return err;
7432 8641a332 2023-04-14 thomas }
7433 8641a332 2023-04-14 thomas
7434 8641a332 2023-04-14 thomas static const struct got_error *
7435 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7436 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7437 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7438 8641a332 2023-04-14 thomas {
7439 8641a332 2023-04-14 thomas const struct got_error *err;
7440 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7441 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7442 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7443 8641a332 2023-04-14 thomas
7444 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7445 8641a332 2023-04-14 thomas
7446 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7447 8641a332 2023-04-14 thomas if (err)
7448 8641a332 2023-04-14 thomas goto done;
7449 8641a332 2023-04-14 thomas
7450 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7451 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7452 8641a332 2023-04-14 thomas
7453 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7454 8641a332 2023-04-14 thomas continue;
7455 8641a332 2023-04-14 thomas
7456 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7457 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7458 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7459 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7460 8641a332 2023-04-14 thomas goto done;
7461 8641a332 2023-04-14 thomas }
7462 8641a332 2023-04-14 thomas } else {
7463 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7464 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7465 8641a332 2023-04-14 thomas goto done;
7466 8641a332 2023-04-14 thomas }
7467 8641a332 2023-04-14 thomas
7468 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7469 8641a332 2023-04-14 thomas path_prefix, abspath);
7470 8641a332 2023-04-14 thomas if (err)
7471 8641a332 2023-04-14 thomas goto done;
7472 8641a332 2023-04-14 thomas free(abspath);
7473 8641a332 2023-04-14 thomas abspath = NULL;
7474 8641a332 2023-04-14 thomas }
7475 8641a332 2023-04-14 thomas
7476 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7477 8641a332 2023-04-14 thomas if (err)
7478 8641a332 2023-04-14 thomas goto done;
7479 8641a332 2023-04-14 thomas wt_path = NULL;
7480 8641a332 2023-04-14 thomas }
7481 8641a332 2023-04-14 thomas
7482 8641a332 2023-04-14 thomas done:
7483 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7484 8641a332 2023-04-14 thomas free(abspath);
7485 8641a332 2023-04-14 thomas free(wt_path);
7486 8641a332 2023-04-14 thomas return err;
7487 8641a332 2023-04-14 thomas }
7488 8641a332 2023-04-14 thomas
7489 8641a332 2023-04-14 thomas static const struct got_error *
7490 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7491 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7492 8641a332 2023-04-14 thomas struct got_repository *repo)
7493 8641a332 2023-04-14 thomas {
7494 8641a332 2023-04-14 thomas const struct got_error *err;
7495 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7496 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7497 8641a332 2023-04-14 thomas
7498 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7499 8641a332 2023-04-14 thomas if (err)
7500 8641a332 2023-04-14 thomas goto done;
7501 8641a332 2023-04-14 thomas
7502 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7503 8641a332 2023-04-14 thomas
7504 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7505 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7506 8641a332 2023-04-14 thomas if (err)
7507 8641a332 2023-04-14 thomas goto done;
7508 8641a332 2023-04-14 thomas done:
7509 8641a332 2023-04-14 thomas if (commit)
7510 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7511 8641a332 2023-04-14 thomas return err;
7512 8641a332 2023-04-14 thomas }
7513 8641a332 2023-04-14 thomas
7514 818c7501 2019-07-11 stsp const struct got_error *
7515 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7516 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7517 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7518 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7519 818c7501 2019-07-11 stsp {
7520 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7521 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7522 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7523 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7524 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7525 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7526 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7527 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7528 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7529 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7530 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7531 818c7501 2019-07-11 stsp
7532 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7533 8641a332 2023-04-14 thomas
7534 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7535 818c7501 2019-07-11 stsp if (err)
7536 818c7501 2019-07-11 stsp return err;
7537 8641a332 2023-04-14 thomas
7538 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7539 8641a332 2023-04-14 thomas if (err)
7540 8641a332 2023-04-14 thomas goto done;
7541 8641a332 2023-04-14 thomas
7542 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7543 8641a332 2023-04-14 thomas if (err)
7544 8641a332 2023-04-14 thomas goto done;
7545 8641a332 2023-04-14 thomas
7546 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7547 8641a332 2023-04-14 thomas if (err)
7548 8641a332 2023-04-14 thomas goto done;
7549 945f9229 2022-04-16 thomas
7550 8641a332 2023-04-14 thomas /*
7551 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7552 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7553 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7554 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7555 8641a332 2023-04-14 thomas */
7556 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7557 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7558 8641a332 2023-04-14 thomas if (err)
7559 8641a332 2023-04-14 thomas goto done;
7560 8641a332 2023-04-14 thomas
7561 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7562 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
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_head_ref(worktree, resolved);
7567 818c7501 2019-07-11 stsp if (err)
7568 818c7501 2019-07-11 stsp goto done;
7569 818c7501 2019-07-11 stsp
7570 818c7501 2019-07-11 stsp /*
7571 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7572 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7573 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7574 818c7501 2019-07-11 stsp */
7575 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7576 818c7501 2019-07-11 stsp if (err)
7577 818c7501 2019-07-11 stsp goto done;
7578 818c7501 2019-07-11 stsp
7579 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7580 25a102ed 2023-04-14 thomas if (err)
7581 25a102ed 2023-04-14 thomas goto done;
7582 25a102ed 2023-04-14 thomas
7583 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7584 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7585 818c7501 2019-07-11 stsp if (err)
7586 818c7501 2019-07-11 stsp goto done;
7587 818c7501 2019-07-11 stsp
7588 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7589 945f9229 2022-04-16 thomas worktree->path_prefix);
7590 ca355955 2019-07-12 stsp if (err)
7591 ca355955 2019-07-12 stsp goto done;
7592 ca355955 2019-07-12 stsp
7593 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7594 ca355955 2019-07-12 stsp if (err)
7595 ca355955 2019-07-12 stsp goto done;
7596 ca355955 2019-07-12 stsp
7597 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7598 818c7501 2019-07-11 stsp if (err)
7599 818c7501 2019-07-11 stsp goto done;
7600 818c7501 2019-07-11 stsp
7601 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7602 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7603 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7604 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7605 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7606 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7607 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7608 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7609 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7610 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7611 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7612 55bd499d 2019-07-12 stsp if (err)
7613 1f1abb7e 2019-08-08 stsp goto sync;
7614 55bd499d 2019-07-12 stsp
7615 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7616 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7617 ca355955 2019-07-12 stsp sync:
7618 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7619 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7620 ca355955 2019-07-12 stsp err = sync_err;
7621 818c7501 2019-07-11 stsp done:
7622 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7623 818c7501 2019-07-11 stsp got_ref_close(resolved);
7624 a3a2faf2 2019-07-12 stsp free(tree_id);
7625 818c7501 2019-07-11 stsp free(commit_id);
7626 8641a332 2023-04-14 thomas free(merged_commit_id);
7627 945f9229 2022-04-16 thomas if (commit)
7628 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7629 0ebf8283 2019-07-24 stsp if (fileindex)
7630 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7631 0ebf8283 2019-07-24 stsp free(fileindex_path);
7632 8641a332 2023-04-14 thomas free(commit_ref_name);
7633 8641a332 2023-04-14 thomas if (commit_ref)
7634 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7635 0ebf8283 2019-07-24 stsp
7636 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7637 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7638 0ebf8283 2019-07-24 stsp err = unlockerr;
7639 0ebf8283 2019-07-24 stsp return err;
7640 0ebf8283 2019-07-24 stsp }
7641 0ebf8283 2019-07-24 stsp
7642 0ebf8283 2019-07-24 stsp const struct got_error *
7643 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7644 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7645 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7646 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7647 0ebf8283 2019-07-24 stsp {
7648 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7649 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7650 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7651 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7652 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7653 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7654 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7655 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7656 0ebf8283 2019-07-24 stsp
7657 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7658 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7659 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7660 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7661 0ebf8283 2019-07-24 stsp
7662 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7663 0ebf8283 2019-07-24 stsp if (err)
7664 0ebf8283 2019-07-24 stsp return err;
7665 0ebf8283 2019-07-24 stsp
7666 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7667 0ebf8283 2019-07-24 stsp if (err)
7668 0ebf8283 2019-07-24 stsp goto done;
7669 0ebf8283 2019-07-24 stsp
7670 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7671 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7672 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7673 0ebf8283 2019-07-24 stsp &ok_arg);
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 = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7678 0ebf8283 2019-07-24 stsp if (err)
7679 0ebf8283 2019-07-24 stsp goto done;
7680 0ebf8283 2019-07-24 stsp
7681 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7682 0ebf8283 2019-07-24 stsp if (err)
7683 0ebf8283 2019-07-24 stsp goto done;
7684 0ebf8283 2019-07-24 stsp
7685 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7686 0ebf8283 2019-07-24 stsp worktree);
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_open(&wt_branch, repo, worktree->head_ref_name,
7691 0ebf8283 2019-07-24 stsp 0);
7692 0ebf8283 2019-07-24 stsp if (err)
7693 0ebf8283 2019-07-24 stsp goto done;
7694 0ebf8283 2019-07-24 stsp
7695 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7696 0ebf8283 2019-07-24 stsp if (err)
7697 0ebf8283 2019-07-24 stsp goto done;
7698 0ebf8283 2019-07-24 stsp
7699 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7700 0ebf8283 2019-07-24 stsp if (err)
7701 0ebf8283 2019-07-24 stsp goto done;
7702 0ebf8283 2019-07-24 stsp
7703 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_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(base_commit_ref, repo);
7708 0ebf8283 2019-07-24 stsp if (err)
7709 0ebf8283 2019-07-24 stsp goto done;
7710 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7711 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7712 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7713 0ebf8283 2019-07-24 stsp goto done;
7714 0ebf8283 2019-07-24 stsp }
7715 0ebf8283 2019-07-24 stsp
7716 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7717 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7718 0ebf8283 2019-07-24 stsp if (err)
7719 0ebf8283 2019-07-24 stsp goto done;
7720 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7721 0ebf8283 2019-07-24 stsp if (err)
7722 0ebf8283 2019-07-24 stsp goto done;
7723 0ebf8283 2019-07-24 stsp
7724 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7725 0ebf8283 2019-07-24 stsp if (err)
7726 0ebf8283 2019-07-24 stsp goto done;
7727 0ebf8283 2019-07-24 stsp done:
7728 0ebf8283 2019-07-24 stsp free(fileindex_path);
7729 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7730 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7731 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7732 0ebf8283 2019-07-24 stsp if (wt_branch)
7733 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7734 0ebf8283 2019-07-24 stsp if (err) {
7735 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7736 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7737 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7738 0ebf8283 2019-07-24 stsp }
7739 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7740 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7741 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7742 0ebf8283 2019-07-24 stsp }
7743 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7744 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7745 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7746 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7747 3e3a69f1 2019-07-25 stsp }
7748 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7749 0ebf8283 2019-07-24 stsp }
7750 0ebf8283 2019-07-24 stsp return err;
7751 0ebf8283 2019-07-24 stsp }
7752 0ebf8283 2019-07-24 stsp
7753 0ebf8283 2019-07-24 stsp const struct got_error *
7754 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7755 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7756 0ebf8283 2019-07-24 stsp {
7757 3e3a69f1 2019-07-25 stsp if (fileindex)
7758 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7759 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7760 0ebf8283 2019-07-24 stsp }
7761 0ebf8283 2019-07-24 stsp
7762 0ebf8283 2019-07-24 stsp const struct got_error *
7763 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7764 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7765 0ebf8283 2019-07-24 stsp {
7766 0ebf8283 2019-07-24 stsp const struct got_error *err;
7767 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7768 0ebf8283 2019-07-24 stsp
7769 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7770 0ebf8283 2019-07-24 stsp if (err)
7771 0ebf8283 2019-07-24 stsp return err;
7772 0ebf8283 2019-07-24 stsp
7773 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7774 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7775 0ebf8283 2019-07-24 stsp return NULL;
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp
7778 0ebf8283 2019-07-24 stsp const struct got_error *
7779 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7780 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7781 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7782 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7783 0ebf8283 2019-07-24 stsp {
7784 0ebf8283 2019-07-24 stsp const struct got_error *err;
7785 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7786 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7787 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7788 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7789 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7790 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7791 0ebf8283 2019-07-24 stsp
7792 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7793 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7794 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7795 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7796 0ebf8283 2019-07-24 stsp
7797 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7798 3e3a69f1 2019-07-25 stsp if (err)
7799 3e3a69f1 2019-07-25 stsp return err;
7800 3e3a69f1 2019-07-25 stsp
7801 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7802 3e3a69f1 2019-07-25 stsp if (err)
7803 f032f1f7 2019-08-04 stsp goto done;
7804 f032f1f7 2019-08-04 stsp
7805 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7806 f032f1f7 2019-08-04 stsp &have_staged_files);
7807 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7808 f032f1f7 2019-08-04 stsp goto done;
7809 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7810 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7811 3e3a69f1 2019-07-25 stsp goto done;
7812 f032f1f7 2019-08-04 stsp }
7813 3e3a69f1 2019-07-25 stsp
7814 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7815 0ebf8283 2019-07-24 stsp if (err)
7816 f032f1f7 2019-08-04 stsp goto done;
7817 0ebf8283 2019-07-24 stsp
7818 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
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 = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7823 0ebf8283 2019-07-24 stsp if (err)
7824 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp
7826 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7827 0ebf8283 2019-07-24 stsp worktree);
7828 0ebf8283 2019-07-24 stsp if (err)
7829 0ebf8283 2019-07-24 stsp goto done;
7830 0ebf8283 2019-07-24 stsp
7831 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7832 0ebf8283 2019-07-24 stsp if (err)
7833 0ebf8283 2019-07-24 stsp goto done;
7834 0ebf8283 2019-07-24 stsp
7835 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7836 0ebf8283 2019-07-24 stsp if (err)
7837 0ebf8283 2019-07-24 stsp goto done;
7838 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7839 0ebf8283 2019-07-24 stsp if (err)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 0ebf8283 2019-07-24 stsp
7842 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7843 0ebf8283 2019-07-24 stsp if (err)
7844 0ebf8283 2019-07-24 stsp goto done;
7845 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7846 0ebf8283 2019-07-24 stsp if (err)
7847 0ebf8283 2019-07-24 stsp goto done;
7848 0ebf8283 2019-07-24 stsp
7849 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7850 0ebf8283 2019-07-24 stsp if (err)
7851 0ebf8283 2019-07-24 stsp goto done;
7852 0ebf8283 2019-07-24 stsp done:
7853 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7854 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7855 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7856 0ebf8283 2019-07-24 stsp if (commit_ref)
7857 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7858 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7859 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7860 0ebf8283 2019-07-24 stsp if (err) {
7861 0ebf8283 2019-07-24 stsp free(*commit_id);
7862 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7863 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7864 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7865 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7866 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7867 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7868 0ebf8283 2019-07-24 stsp }
7869 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7870 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7871 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7872 3e3a69f1 2019-07-25 stsp }
7873 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7874 0ebf8283 2019-07-24 stsp }
7875 0ebf8283 2019-07-24 stsp return err;
7876 0ebf8283 2019-07-24 stsp }
7877 0ebf8283 2019-07-24 stsp
7878 0ebf8283 2019-07-24 stsp static const struct got_error *
7879 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7880 0ebf8283 2019-07-24 stsp {
7881 0ebf8283 2019-07-24 stsp const struct got_error *err;
7882 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7883 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7884 0ebf8283 2019-07-24 stsp
7885 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7886 0ebf8283 2019-07-24 stsp if (err)
7887 0ebf8283 2019-07-24 stsp goto done;
7888 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7889 0ebf8283 2019-07-24 stsp if (err)
7890 0ebf8283 2019-07-24 stsp goto done;
7891 0ebf8283 2019-07-24 stsp
7892 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7893 0ebf8283 2019-07-24 stsp worktree);
7894 0ebf8283 2019-07-24 stsp if (err)
7895 0ebf8283 2019-07-24 stsp goto done;
7896 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7897 0ebf8283 2019-07-24 stsp if (err)
7898 0ebf8283 2019-07-24 stsp goto done;
7899 0ebf8283 2019-07-24 stsp
7900 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7901 0ebf8283 2019-07-24 stsp if (err)
7902 0ebf8283 2019-07-24 stsp goto done;
7903 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7904 0ebf8283 2019-07-24 stsp if (err)
7905 0ebf8283 2019-07-24 stsp goto done;
7906 0ebf8283 2019-07-24 stsp
7907 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7908 0ebf8283 2019-07-24 stsp if (err)
7909 0ebf8283 2019-07-24 stsp goto done;
7910 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7911 0ebf8283 2019-07-24 stsp if (err)
7912 0ebf8283 2019-07-24 stsp goto done;
7913 0ebf8283 2019-07-24 stsp done:
7914 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7915 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7916 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7917 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7918 0ebf8283 2019-07-24 stsp return err;
7919 0ebf8283 2019-07-24 stsp }
7920 0ebf8283 2019-07-24 stsp
7921 0ebf8283 2019-07-24 stsp const struct got_error *
7922 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7923 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7924 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7925 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7926 0ebf8283 2019-07-24 stsp {
7927 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7928 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7929 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7930 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7931 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7932 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7933 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7934 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7935 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7936 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7937 0ebf8283 2019-07-24 stsp
7938 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7939 8641a332 2023-04-14 thomas
7940 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7941 0ebf8283 2019-07-24 stsp if (err)
7942 0ebf8283 2019-07-24 stsp return err;
7943 945f9229 2022-04-16 thomas
7944 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
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 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7949 8641a332 2023-04-14 thomas if (err) {
7950 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7951 8641a332 2023-04-14 thomas goto done;
7952 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7953 8641a332 2023-04-14 thomas commit_ref = NULL;
7954 8641a332 2023-04-14 thomas }
7955 8641a332 2023-04-14 thomas
7956 8641a332 2023-04-14 thomas if (commit_ref) {
7957 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7958 8641a332 2023-04-14 thomas if (err)
7959 8641a332 2023-04-14 thomas goto done;
7960 8641a332 2023-04-14 thomas
7961 8641a332 2023-04-14 thomas /*
7962 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7963 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7964 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7965 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7966 8641a332 2023-04-14 thomas */
7967 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7968 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7969 8641a332 2023-04-14 thomas if (err)
7970 8641a332 2023-04-14 thomas goto done;
7971 8641a332 2023-04-14 thomas }
7972 8641a332 2023-04-14 thomas
7973 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7974 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7975 0ebf8283 2019-07-24 stsp if (err)
7976 0ebf8283 2019-07-24 stsp goto done;
7977 0ebf8283 2019-07-24 stsp
7978 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7979 0ebf8283 2019-07-24 stsp if (err)
7980 0ebf8283 2019-07-24 stsp goto done;
7981 0ebf8283 2019-07-24 stsp
7982 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7983 0ebf8283 2019-07-24 stsp if (err)
7984 0ebf8283 2019-07-24 stsp goto done;
7985 0ebf8283 2019-07-24 stsp
7986 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7987 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7988 25a102ed 2023-04-14 thomas if (err)
7989 25a102ed 2023-04-14 thomas goto done;
7990 25a102ed 2023-04-14 thomas
7991 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7992 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7993 0ebf8283 2019-07-24 stsp if (err)
7994 0ebf8283 2019-07-24 stsp goto done;
7995 0ebf8283 2019-07-24 stsp
7996 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7997 0ebf8283 2019-07-24 stsp if (err)
7998 0ebf8283 2019-07-24 stsp goto done;
7999 0ebf8283 2019-07-24 stsp
8000 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8001 0ebf8283 2019-07-24 stsp if (err)
8002 0ebf8283 2019-07-24 stsp goto done;
8003 0ebf8283 2019-07-24 stsp
8004 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8005 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8006 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8007 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8008 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8009 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8010 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8011 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8012 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8013 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8014 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8015 0ebf8283 2019-07-24 stsp if (err)
8016 1f1abb7e 2019-08-08 stsp goto sync;
8017 0ebf8283 2019-07-24 stsp
8018 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8019 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8020 0ebf8283 2019-07-24 stsp sync:
8021 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8022 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8023 0ebf8283 2019-07-24 stsp err = sync_err;
8024 0ebf8283 2019-07-24 stsp done:
8025 8641a332 2023-04-14 thomas if (resolved)
8026 8641a332 2023-04-14 thomas got_ref_close(resolved);
8027 8641a332 2023-04-14 thomas if (commit_ref)
8028 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8029 8641a332 2023-04-14 thomas free(merged_commit_id);
8030 0ebf8283 2019-07-24 stsp free(tree_id);
8031 818c7501 2019-07-11 stsp free(fileindex_path);
8032 8641a332 2023-04-14 thomas free(commit_ref_name);
8033 818c7501 2019-07-11 stsp
8034 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8035 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8036 818c7501 2019-07-11 stsp err = unlockerr;
8037 818c7501 2019-07-11 stsp return err;
8038 818c7501 2019-07-11 stsp }
8039 0ebf8283 2019-07-24 stsp
8040 0ebf8283 2019-07-24 stsp const struct got_error *
8041 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8042 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8043 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8044 0ebf8283 2019-07-24 stsp {
8045 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8046 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8047 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8048 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8049 0ebf8283 2019-07-24 stsp
8050 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8051 0ebf8283 2019-07-24 stsp if (err)
8052 0ebf8283 2019-07-24 stsp return err;
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8055 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8056 e600f124 2021-03-21 stsp if (err)
8057 e600f124 2021-03-21 stsp goto done;
8058 e600f124 2021-03-21 stsp
8059 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8060 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8061 0ebf8283 2019-07-24 stsp if (err)
8062 0ebf8283 2019-07-24 stsp goto done;
8063 0ebf8283 2019-07-24 stsp
8064 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8065 0ebf8283 2019-07-24 stsp if (err)
8066 0ebf8283 2019-07-24 stsp goto done;
8067 0ebf8283 2019-07-24 stsp
8068 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8069 0ebf8283 2019-07-24 stsp if (err)
8070 0ebf8283 2019-07-24 stsp goto done;
8071 0ebf8283 2019-07-24 stsp
8072 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8073 0ebf8283 2019-07-24 stsp if (err)
8074 0ebf8283 2019-07-24 stsp goto done;
8075 0ebf8283 2019-07-24 stsp
8076 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8077 a615e0e7 2020-12-16 stsp if (err)
8078 a615e0e7 2020-12-16 stsp goto done;
8079 a615e0e7 2020-12-16 stsp
8080 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8081 a615e0e7 2020-12-16 stsp if (err)
8082 a615e0e7 2020-12-16 stsp goto done;
8083 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8084 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8085 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8086 a615e0e7 2020-12-16 stsp err = sync_err;
8087 0ebf8283 2019-07-24 stsp done:
8088 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8089 a615e0e7 2020-12-16 stsp free(fileindex_path);
8090 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8091 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8092 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8093 0ebf8283 2019-07-24 stsp err = unlockerr;
8094 0ebf8283 2019-07-24 stsp return err;
8095 0ebf8283 2019-07-24 stsp }
8096 0ebf8283 2019-07-24 stsp
8097 0ebf8283 2019-07-24 stsp const struct got_error *
8098 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8099 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8100 0ebf8283 2019-07-24 stsp {
8101 0ebf8283 2019-07-24 stsp const struct got_error *err;
8102 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8103 0ebf8283 2019-07-24 stsp
8104 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8105 0ebf8283 2019-07-24 stsp if (err)
8106 0ebf8283 2019-07-24 stsp return err;
8107 0ebf8283 2019-07-24 stsp
8108 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8109 0ebf8283 2019-07-24 stsp if (err)
8110 0ebf8283 2019-07-24 stsp goto done;
8111 0ebf8283 2019-07-24 stsp
8112 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8113 0ebf8283 2019-07-24 stsp done:
8114 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8115 2822a352 2019-10-15 stsp return err;
8116 2822a352 2019-10-15 stsp }
8117 2822a352 2019-10-15 stsp
8118 2822a352 2019-10-15 stsp const struct got_error *
8119 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8120 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8121 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8122 2822a352 2019-10-15 stsp struct got_repository *repo)
8123 2822a352 2019-10-15 stsp {
8124 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8125 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8126 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8127 2822a352 2019-10-15 stsp
8128 2822a352 2019-10-15 stsp *fileindex = NULL;
8129 2822a352 2019-10-15 stsp *branch_ref = NULL;
8130 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8131 2822a352 2019-10-15 stsp
8132 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8133 2822a352 2019-10-15 stsp if (err)
8134 2822a352 2019-10-15 stsp return err;
8135 2822a352 2019-10-15 stsp
8136 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8137 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8138 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8139 2822a352 2019-10-15 stsp "update -b or different branch name required");
8140 2822a352 2019-10-15 stsp goto done;
8141 2822a352 2019-10-15 stsp }
8142 2822a352 2019-10-15 stsp
8143 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8144 2822a352 2019-10-15 stsp if (err)
8145 2822a352 2019-10-15 stsp goto done;
8146 2822a352 2019-10-15 stsp
8147 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8148 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8149 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8150 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8151 2822a352 2019-10-15 stsp &ok_arg);
8152 2822a352 2019-10-15 stsp if (err)
8153 2822a352 2019-10-15 stsp goto done;
8154 2822a352 2019-10-15 stsp
8155 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8156 2822a352 2019-10-15 stsp if (err)
8157 2822a352 2019-10-15 stsp goto done;
8158 2822a352 2019-10-15 stsp
8159 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8160 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8161 2822a352 2019-10-15 stsp done:
8162 2822a352 2019-10-15 stsp if (err) {
8163 2822a352 2019-10-15 stsp if (*branch_ref) {
8164 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8165 2822a352 2019-10-15 stsp *branch_ref = NULL;
8166 2822a352 2019-10-15 stsp }
8167 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8168 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8169 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8170 2822a352 2019-10-15 stsp }
8171 2822a352 2019-10-15 stsp if (*fileindex) {
8172 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8173 2822a352 2019-10-15 stsp *fileindex = NULL;
8174 2822a352 2019-10-15 stsp }
8175 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8176 2822a352 2019-10-15 stsp }
8177 0cb83759 2019-08-03 stsp return err;
8178 0cb83759 2019-08-03 stsp }
8179 0cb83759 2019-08-03 stsp
8180 2822a352 2019-10-15 stsp const struct got_error *
8181 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8182 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8183 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8184 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8185 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8186 2822a352 2019-10-15 stsp {
8187 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8188 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8189 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8190 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8191 2822a352 2019-10-15 stsp
8192 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
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_ref_resolve(&commit_id, repo, branch_ref);
8197 2822a352 2019-10-15 stsp if (err)
8198 2822a352 2019-10-15 stsp goto done;
8199 2822a352 2019-10-15 stsp
8200 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8201 945f9229 2022-04-16 thomas if (err)
8202 945f9229 2022-04-16 thomas goto done;
8203 945f9229 2022-04-16 thomas
8204 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8205 2822a352 2019-10-15 stsp worktree->path_prefix);
8206 2822a352 2019-10-15 stsp if (err)
8207 2822a352 2019-10-15 stsp goto done;
8208 2822a352 2019-10-15 stsp
8209 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8210 2822a352 2019-10-15 stsp if (err)
8211 2822a352 2019-10-15 stsp goto done;
8212 2822a352 2019-10-15 stsp
8213 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8214 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8215 2822a352 2019-10-15 stsp if (err)
8216 2822a352 2019-10-15 stsp goto sync;
8217 2822a352 2019-10-15 stsp
8218 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8219 2822a352 2019-10-15 stsp if (err)
8220 2822a352 2019-10-15 stsp goto sync;
8221 2822a352 2019-10-15 stsp
8222 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8223 6e210706 2021-01-22 stsp if (err)
8224 6e210706 2021-01-22 stsp goto sync;
8225 6e210706 2021-01-22 stsp
8226 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8227 2822a352 2019-10-15 stsp sync:
8228 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8229 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8230 2822a352 2019-10-15 stsp err = sync_err;
8231 2822a352 2019-10-15 stsp
8232 2822a352 2019-10-15 stsp done:
8233 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8234 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8235 2822a352 2019-10-15 stsp err = unlockerr;
8236 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8237 2822a352 2019-10-15 stsp
8238 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8239 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8240 2822a352 2019-10-15 stsp err = unlockerr;
8241 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8242 2822a352 2019-10-15 stsp
8243 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8244 2822a352 2019-10-15 stsp free(fileindex_path);
8245 2822a352 2019-10-15 stsp free(tree_id);
8246 945f9229 2022-04-16 thomas if (commit)
8247 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8248 2822a352 2019-10-15 stsp
8249 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8250 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8251 2822a352 2019-10-15 stsp err = unlockerr;
8252 2822a352 2019-10-15 stsp return err;
8253 2822a352 2019-10-15 stsp }
8254 2822a352 2019-10-15 stsp
8255 2822a352 2019-10-15 stsp const struct got_error *
8256 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8257 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8258 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8259 2822a352 2019-10-15 stsp {
8260 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8261 8b692cd0 2019-10-21 stsp
8262 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8263 8b692cd0 2019-10-21 stsp
8264 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8265 8b692cd0 2019-10-21 stsp
8266 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8267 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8268 8b692cd0 2019-10-21 stsp err = unlockerr;
8269 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8270 8b692cd0 2019-10-21 stsp
8271 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8272 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8273 8b692cd0 2019-10-21 stsp err = unlockerr;
8274 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8275 10604dce 2021-09-24 thomas
8276 10604dce 2021-09-24 thomas return err;
8277 10604dce 2021-09-24 thomas }
8278 10604dce 2021-09-24 thomas
8279 10604dce 2021-09-24 thomas const struct got_error *
8280 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8281 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8282 10604dce 2021-09-24 thomas {
8283 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8284 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8285 10604dce 2021-09-24 thomas
8286 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8287 10604dce 2021-09-24 thomas if (err)
8288 10604dce 2021-09-24 thomas goto done;
8289 8b692cd0 2019-10-21 stsp
8290 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8291 10604dce 2021-09-24 thomas
8292 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8293 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8294 10604dce 2021-09-24 thomas err = sync_err;
8295 10604dce 2021-09-24 thomas done:
8296 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8297 10604dce 2021-09-24 thomas free(fileindex_path);
8298 8b692cd0 2019-10-21 stsp return err;
8299 2822a352 2019-10-15 stsp }
8300 2822a352 2019-10-15 stsp
8301 10604dce 2021-09-24 thomas static const struct got_error *
8302 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8303 10604dce 2021-09-24 thomas {
8304 10604dce 2021-09-24 thomas const struct got_error *err;
8305 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8306 10604dce 2021-09-24 thomas
8307 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8308 10604dce 2021-09-24 thomas if (err)
8309 10604dce 2021-09-24 thomas goto done;
8310 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8311 10604dce 2021-09-24 thomas if (err)
8312 10604dce 2021-09-24 thomas goto done;
8313 10604dce 2021-09-24 thomas
8314 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8315 10604dce 2021-09-24 thomas if (err)
8316 10604dce 2021-09-24 thomas goto done;
8317 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8318 10604dce 2021-09-24 thomas if (err)
8319 10604dce 2021-09-24 thomas goto done;
8320 10604dce 2021-09-24 thomas
8321 10604dce 2021-09-24 thomas done:
8322 10604dce 2021-09-24 thomas free(branch_refname);
8323 10604dce 2021-09-24 thomas free(commit_refname);
8324 10604dce 2021-09-24 thomas return err;
8325 10604dce 2021-09-24 thomas }
8326 10604dce 2021-09-24 thomas
8327 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8328 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8329 10604dce 2021-09-24 thomas const char *branch_name;
8330 10604dce 2021-09-24 thomas };
8331 10604dce 2021-09-24 thomas
8332 10604dce 2021-09-24 thomas static const struct got_error *
8333 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8334 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8335 10604dce 2021-09-24 thomas {
8336 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8337 10604dce 2021-09-24 thomas
8338 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8339 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8340 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8341 10604dce 2021-09-24 thomas
8342 10604dce 2021-09-24 thomas return NULL;
8343 10604dce 2021-09-24 thomas }
8344 10604dce 2021-09-24 thomas
8345 10604dce 2021-09-24 thomas
8346 10604dce 2021-09-24 thomas const struct got_error *
8347 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8348 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8349 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8350 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8351 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8352 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8353 10604dce 2021-09-24 thomas {
8354 10604dce 2021-09-24 thomas const struct got_error *err;
8355 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8356 10604dce 2021-09-24 thomas
8357 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8358 10604dce 2021-09-24 thomas if (err)
8359 10604dce 2021-09-24 thomas goto done;
8360 10604dce 2021-09-24 thomas
8361 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8362 10604dce 2021-09-24 thomas worktree);
8363 10604dce 2021-09-24 thomas if (err)
8364 10604dce 2021-09-24 thomas goto done;
8365 10604dce 2021-09-24 thomas
8366 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8367 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8368 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8369 10604dce 2021-09-24 thomas done:
8370 10604dce 2021-09-24 thomas free(fileindex_path);
8371 10604dce 2021-09-24 thomas return err;
8372 10604dce 2021-09-24 thomas }
8373 10604dce 2021-09-24 thomas
8374 10604dce 2021-09-24 thomas const struct got_error *
8375 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8376 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8377 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8378 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8379 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8380 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8381 ae1e948a 2021-09-28 thomas
8382 10604dce 2021-09-24 thomas {
8383 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8384 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8385 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8386 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8387 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8388 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8389 10604dce 2021-09-24 thomas int have_staged_files = 0;
8390 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8391 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8392 10604dce 2021-09-24 thomas
8393 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8394 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8395 10604dce 2021-09-24 thomas
8396 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8397 10604dce 2021-09-24 thomas
8398 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8399 10604dce 2021-09-24 thomas if (err)
8400 10604dce 2021-09-24 thomas goto done;
8401 10604dce 2021-09-24 thomas
8402 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8403 10604dce 2021-09-24 thomas if (err)
8404 10604dce 2021-09-24 thomas goto done;
8405 10604dce 2021-09-24 thomas
8406 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8407 10604dce 2021-09-24 thomas if (err)
8408 10604dce 2021-09-24 thomas goto done;
8409 10604dce 2021-09-24 thomas
8410 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8411 10604dce 2021-09-24 thomas &have_staged_files);
8412 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8413 10604dce 2021-09-24 thomas goto done;
8414 10604dce 2021-09-24 thomas if (have_staged_files) {
8415 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8416 10604dce 2021-09-24 thomas goto done;
8417 10604dce 2021-09-24 thomas }
8418 10604dce 2021-09-24 thomas
8419 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8420 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8421 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8422 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8423 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8424 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8425 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8426 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8427 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8428 10604dce 2021-09-24 thomas if (err)
8429 10604dce 2021-09-24 thomas goto done;
8430 10604dce 2021-09-24 thomas
8431 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8432 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8433 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8434 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8435 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8436 10604dce 2021-09-24 thomas if (err)
8437 10604dce 2021-09-24 thomas goto done;
8438 10604dce 2021-09-24 thomas
8439 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8440 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8441 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8442 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8443 10604dce 2021-09-24 thomas err = sync_err;
8444 10604dce 2021-09-24 thomas done:
8445 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8446 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8447 21c2d8be 2023-01-10 thomas
8448 10604dce 2021-09-24 thomas free_commitable(ct);
8449 10604dce 2021-09-24 thomas }
8450 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8451 10604dce 2021-09-24 thomas free(fileindex_path);
8452 10604dce 2021-09-24 thomas return err;
8453 10604dce 2021-09-24 thomas }
8454 10604dce 2021-09-24 thomas
8455 10604dce 2021-09-24 thomas const struct got_error *
8456 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8457 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8458 10604dce 2021-09-24 thomas {
8459 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8460 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8461 10604dce 2021-09-24 thomas
8462 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8463 10604dce 2021-09-24 thomas if (err)
8464 10604dce 2021-09-24 thomas goto done;
8465 10604dce 2021-09-24 thomas
8466 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8467 10604dce 2021-09-24 thomas if (err)
8468 10604dce 2021-09-24 thomas goto done;
8469 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8470 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8471 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8472 10604dce 2021-09-24 thomas err = sync_err;
8473 10604dce 2021-09-24 thomas done:
8474 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8475 10604dce 2021-09-24 thomas free(fileindex_path);
8476 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8477 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8478 10604dce 2021-09-24 thomas err = unlockerr;
8479 10604dce 2021-09-24 thomas return err;
8480 10604dce 2021-09-24 thomas }
8481 10604dce 2021-09-24 thomas
8482 10604dce 2021-09-24 thomas const struct got_error *
8483 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8484 10604dce 2021-09-24 thomas struct got_repository *repo)
8485 10604dce 2021-09-24 thomas {
8486 10604dce 2021-09-24 thomas const struct got_error *err;
8487 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8488 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8489 10604dce 2021-09-24 thomas
8490 10604dce 2021-09-24 thomas *in_progress = 0;
8491 10604dce 2021-09-24 thomas
8492 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8493 10604dce 2021-09-24 thomas if (err)
8494 10604dce 2021-09-24 thomas return err;
8495 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8496 dfe86d1f 2021-09-24 thomas free(branch_refname);
8497 10604dce 2021-09-24 thomas if (err) {
8498 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8499 10604dce 2021-09-24 thomas return err;
8500 10604dce 2021-09-24 thomas } else
8501 10604dce 2021-09-24 thomas *in_progress = 1;
8502 10604dce 2021-09-24 thomas
8503 10604dce 2021-09-24 thomas return NULL;
8504 10604dce 2021-09-24 thomas }
8505 10604dce 2021-09-24 thomas
8506 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8507 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8508 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8509 10604dce 2021-09-24 thomas {
8510 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8511 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8512 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8513 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8514 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8515 10604dce 2021-09-24 thomas
8516 10604dce 2021-09-24 thomas *fileindex = NULL;
8517 10604dce 2021-09-24 thomas
8518 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8519 10604dce 2021-09-24 thomas if (err)
8520 10604dce 2021-09-24 thomas return err;
8521 10604dce 2021-09-24 thomas
8522 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
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 /* Preconditions are the same as for rebase. */
8527 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8528 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8529 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8530 10604dce 2021-09-24 thomas &ok_arg);
8531 10604dce 2021-09-24 thomas if (err)
8532 10604dce 2021-09-24 thomas goto done;
8533 10604dce 2021-09-24 thomas
8534 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8535 10604dce 2021-09-24 thomas 0);
8536 10604dce 2021-09-24 thomas if (err)
8537 10604dce 2021-09-24 thomas goto done;
8538 10604dce 2021-09-24 thomas
8539 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8540 10604dce 2021-09-24 thomas if (err)
8541 10604dce 2021-09-24 thomas goto done;
8542 10604dce 2021-09-24 thomas
8543 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8544 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8545 10604dce 2021-09-24 thomas goto done;
8546 10604dce 2021-09-24 thomas }
8547 2b72f32d 2023-06-22 thomas
8548 2b72f32d 2023-06-22 thomas done:
8549 2b72f32d 2023-06-22 thomas free(fileindex_path);
8550 2b72f32d 2023-06-22 thomas if (wt_branch)
8551 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8552 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8553 2b72f32d 2023-06-22 thomas if (err) {
8554 2b72f32d 2023-06-22 thomas if (*fileindex) {
8555 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8556 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8557 2b72f32d 2023-06-22 thomas }
8558 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8559 2b72f32d 2023-06-22 thomas }
8560 2b72f32d 2023-06-22 thomas return err;
8561 2b72f32d 2023-06-22 thomas }
8562 10604dce 2021-09-24 thomas
8563 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8564 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8565 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8566 2b72f32d 2023-06-22 thomas {
8567 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8568 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8569 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8570 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8571 2b72f32d 2023-06-22 thomas
8572 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8573 2b72f32d 2023-06-22 thomas if (err)
8574 2b72f32d 2023-06-22 thomas return err;
8575 2b72f32d 2023-06-22 thomas
8576 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8577 2b72f32d 2023-06-22 thomas if (err)
8578 2b72f32d 2023-06-22 thomas return err;
8579 2b72f32d 2023-06-22 thomas
8580 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8581 10604dce 2021-09-24 thomas if (err)
8582 10604dce 2021-09-24 thomas goto done;
8583 10604dce 2021-09-24 thomas
8584 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8585 10604dce 2021-09-24 thomas if (err)
8586 10604dce 2021-09-24 thomas goto done;
8587 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8588 10604dce 2021-09-24 thomas if (err)
8589 10604dce 2021-09-24 thomas goto done;
8590 10604dce 2021-09-24 thomas
8591 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8592 10604dce 2021-09-24 thomas if (err)
8593 10604dce 2021-09-24 thomas goto done;
8594 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8595 10604dce 2021-09-24 thomas if (err)
8596 10604dce 2021-09-24 thomas goto done;
8597 10604dce 2021-09-24 thomas
8598 10604dce 2021-09-24 thomas done:
8599 10604dce 2021-09-24 thomas free(branch_refname);
8600 10604dce 2021-09-24 thomas free(commit_refname);
8601 10604dce 2021-09-24 thomas if (branch_ref)
8602 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8603 10604dce 2021-09-24 thomas if (commit_ref)
8604 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8605 2b72f32d 2023-06-22 thomas free(branch_tip);
8606 10604dce 2021-09-24 thomas return err;
8607 10604dce 2021-09-24 thomas }
8608 10604dce 2021-09-24 thomas
8609 10604dce 2021-09-24 thomas const struct got_error *
8610 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8611 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8612 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8613 10604dce 2021-09-24 thomas {
8614 10604dce 2021-09-24 thomas const struct got_error *err;
8615 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8616 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8617 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8618 10604dce 2021-09-24 thomas int have_staged_files = 0;
8619 10604dce 2021-09-24 thomas
8620 10604dce 2021-09-24 thomas *branch_name = NULL;
8621 10604dce 2021-09-24 thomas *branch_tip = NULL;
8622 10604dce 2021-09-24 thomas *fileindex = NULL;
8623 10604dce 2021-09-24 thomas
8624 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8625 10604dce 2021-09-24 thomas if (err)
8626 10604dce 2021-09-24 thomas return err;
8627 10604dce 2021-09-24 thomas
8628 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, 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 = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8633 10604dce 2021-09-24 thomas &have_staged_files);
8634 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8635 10604dce 2021-09-24 thomas goto done;
8636 10604dce 2021-09-24 thomas if (have_staged_files) {
8637 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8638 10604dce 2021-09-24 thomas goto done;
8639 10604dce 2021-09-24 thomas }
8640 10604dce 2021-09-24 thomas
8641 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8642 10604dce 2021-09-24 thomas if (err)
8643 10604dce 2021-09-24 thomas goto done;
8644 10604dce 2021-09-24 thomas
8645 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8646 10604dce 2021-09-24 thomas if (err)
8647 10604dce 2021-09-24 thomas goto done;
8648 10604dce 2021-09-24 thomas
8649 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8650 10604dce 2021-09-24 thomas if (err)
8651 10604dce 2021-09-24 thomas goto done;
8652 10604dce 2021-09-24 thomas
8653 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8654 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8655 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8656 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8657 10604dce 2021-09-24 thomas goto done;
8658 10604dce 2021-09-24 thomas }
8659 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8660 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8661 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8662 10604dce 2021-09-24 thomas goto done;
8663 10604dce 2021-09-24 thomas }
8664 10604dce 2021-09-24 thomas
8665 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8666 10604dce 2021-09-24 thomas if (err)
8667 10604dce 2021-09-24 thomas goto done;
8668 10604dce 2021-09-24 thomas
8669 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8670 10604dce 2021-09-24 thomas if (err)
8671 10604dce 2021-09-24 thomas goto done;
8672 10604dce 2021-09-24 thomas done:
8673 10604dce 2021-09-24 thomas free(commit_refname);
8674 10604dce 2021-09-24 thomas free(branch_refname);
8675 10604dce 2021-09-24 thomas free(fileindex_path);
8676 10604dce 2021-09-24 thomas if (commit_ref)
8677 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8678 10604dce 2021-09-24 thomas if (branch_ref)
8679 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8680 10604dce 2021-09-24 thomas if (err) {
8681 10604dce 2021-09-24 thomas if (*branch_name) {
8682 10604dce 2021-09-24 thomas free(*branch_name);
8683 10604dce 2021-09-24 thomas *branch_name = NULL;
8684 10604dce 2021-09-24 thomas }
8685 10604dce 2021-09-24 thomas free(*branch_tip);
8686 10604dce 2021-09-24 thomas *branch_tip = NULL;
8687 10604dce 2021-09-24 thomas if (*fileindex) {
8688 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8689 10604dce 2021-09-24 thomas *fileindex = NULL;
8690 10604dce 2021-09-24 thomas }
8691 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8692 10604dce 2021-09-24 thomas }
8693 10604dce 2021-09-24 thomas return err;
8694 10604dce 2021-09-24 thomas }
8695 10604dce 2021-09-24 thomas
8696 10604dce 2021-09-24 thomas const struct got_error *
8697 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8698 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8699 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8700 10604dce 2021-09-24 thomas {
8701 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8702 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8703 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8704 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8705 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8706 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8707 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8708 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8709 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8710 8641a332 2023-04-14 thomas
8711 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8712 8641a332 2023-04-14 thomas
8713 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8714 8641a332 2023-04-14 thomas if (err)
8715 8641a332 2023-04-14 thomas goto done;
8716 8641a332 2023-04-14 thomas
8717 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8718 8641a332 2023-04-14 thomas if (err)
8719 8641a332 2023-04-14 thomas goto done;
8720 8641a332 2023-04-14 thomas
8721 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8722 8641a332 2023-04-14 thomas if (err)
8723 8641a332 2023-04-14 thomas goto done;
8724 8641a332 2023-04-14 thomas
8725 8641a332 2023-04-14 thomas /*
8726 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8727 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8728 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8729 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8730 8641a332 2023-04-14 thomas */
8731 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8732 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8733 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8734 8641a332 2023-04-14 thomas if (err)
8735 8641a332 2023-04-14 thomas goto done;
8736 10604dce 2021-09-24 thomas
8737 8641a332 2023-04-14 thomas
8738 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8739 945f9229 2022-04-16 thomas worktree->base_commit_id);
8740 945f9229 2022-04-16 thomas if (err)
8741 945f9229 2022-04-16 thomas goto done;
8742 945f9229 2022-04-16 thomas
8743 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8744 945f9229 2022-04-16 thomas worktree->path_prefix);
8745 10604dce 2021-09-24 thomas if (err)
8746 10604dce 2021-09-24 thomas goto done;
8747 10604dce 2021-09-24 thomas
8748 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8749 10604dce 2021-09-24 thomas if (err)
8750 10604dce 2021-09-24 thomas goto done;
8751 10604dce 2021-09-24 thomas
8752 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8753 10604dce 2021-09-24 thomas if (err)
8754 10604dce 2021-09-24 thomas goto done;
8755 10604dce 2021-09-24 thomas
8756 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8757 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8758 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8759 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8760 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8761 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8762 10604dce 2021-09-24 thomas rfa.repo = repo;
8763 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8764 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8765 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8766 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8767 10604dce 2021-09-24 thomas if (err)
8768 10604dce 2021-09-24 thomas goto sync;
8769 10604dce 2021-09-24 thomas
8770 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8771 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8772 10604dce 2021-09-24 thomas sync:
8773 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8774 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8775 10604dce 2021-09-24 thomas err = sync_err;
8776 10604dce 2021-09-24 thomas done:
8777 10604dce 2021-09-24 thomas free(tree_id);
8778 8641a332 2023-04-14 thomas free(merged_commit_id);
8779 945f9229 2022-04-16 thomas if (commit)
8780 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8781 10604dce 2021-09-24 thomas if (fileindex)
8782 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8783 10604dce 2021-09-24 thomas free(fileindex_path);
8784 8641a332 2023-04-14 thomas if (commit_ref)
8785 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8786 8641a332 2023-04-14 thomas free(commit_ref_name);
8787 10604dce 2021-09-24 thomas
8788 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8789 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8790 10604dce 2021-09-24 thomas err = unlockerr;
8791 10604dce 2021-09-24 thomas return err;
8792 10604dce 2021-09-24 thomas }
8793 10604dce 2021-09-24 thomas
8794 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8795 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8796 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8797 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8798 2db2652d 2019-08-07 stsp struct got_repository *repo;
8799 2db2652d 2019-08-07 stsp int have_changes;
8800 2db2652d 2019-08-07 stsp };
8801 2db2652d 2019-08-07 stsp
8802 ef20f542 2022-06-26 thomas static const struct got_error *
8803 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8804 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8805 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8806 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8807 735ef5ac 2019-08-03 stsp {
8808 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8809 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8810 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8811 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8812 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8813 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8814 8b13ce36 2019-08-08 stsp
8815 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8816 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8817 8b13ce36 2019-08-08 stsp return NULL;
8818 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8819 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8820 735ef5ac 2019-08-03 stsp
8821 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8822 735ef5ac 2019-08-03 stsp if (ie == NULL)
8823 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8824 735ef5ac 2019-08-03 stsp
8825 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8826 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8827 735ef5ac 2019-08-03 stsp relpath) == -1)
8828 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8829 735ef5ac 2019-08-03 stsp
8830 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8831 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8832 43010591 2023-02-17 thomas &base_commit_id, ie);
8833 735ef5ac 2019-08-03 stsp }
8834 735ef5ac 2019-08-03 stsp
8835 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8836 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8837 3aa5969e 2019-08-06 stsp goto done;
8838 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8839 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8840 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8841 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8842 735ef5ac 2019-08-03 stsp goto done;
8843 3aa5969e 2019-08-06 stsp }
8844 735ef5ac 2019-08-03 stsp
8845 2db2652d 2019-08-07 stsp a->have_changes = 1;
8846 2db2652d 2019-08-07 stsp
8847 735ef5ac 2019-08-03 stsp p = in_repo_path;
8848 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8849 735ef5ac 2019-08-03 stsp p++;
8850 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8851 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8852 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8853 735ef5ac 2019-08-03 stsp done:
8854 735ef5ac 2019-08-03 stsp free(in_repo_path);
8855 dc424a06 2019-08-07 stsp return err;
8856 dc424a06 2019-08-07 stsp }
8857 dc424a06 2019-08-07 stsp
8858 2db2652d 2019-08-07 stsp struct stage_path_arg {
8859 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8860 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8861 2db2652d 2019-08-07 stsp struct got_repository *repo;
8862 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8863 2db2652d 2019-08-07 stsp void *status_arg;
8864 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8865 2db2652d 2019-08-07 stsp void *patch_arg;
8866 7b5dc508 2019-10-28 stsp int staged_something;
8867 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8868 2db2652d 2019-08-07 stsp };
8869 2db2652d 2019-08-07 stsp
8870 2db2652d 2019-08-07 stsp static const struct got_error *
8871 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8872 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8873 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8874 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8875 0cb83759 2019-08-03 stsp {
8876 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8877 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8878 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8879 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8880 0cb83759 2019-08-03 stsp uint32_t stage;
8881 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8882 0aeb8099 2020-07-23 stsp struct stat sb;
8883 8b13ce36 2019-08-08 stsp
8884 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8885 8b13ce36 2019-08-08 stsp return NULL;
8886 0cb83759 2019-08-03 stsp
8887 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8888 d3e7c587 2019-08-03 stsp if (ie == NULL)
8889 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8890 0cb83759 2019-08-03 stsp
8891 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8892 2db2652d 2019-08-07 stsp relpath)== -1)
8893 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8894 0cb83759 2019-08-03 stsp
8895 0cb83759 2019-08-03 stsp switch (status) {
8896 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8897 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8898 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8899 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8900 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8901 0aeb8099 2020-07-23 stsp break;
8902 0aeb8099 2020-07-23 stsp }
8903 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8904 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8905 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8906 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8907 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8908 dc424a06 2019-08-07 stsp if (err)
8909 dc424a06 2019-08-07 stsp break;
8910 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8911 dc424a06 2019-08-07 stsp break;
8912 dc424a06 2019-08-07 stsp } else {
8913 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8914 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8915 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8916 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8917 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8918 dc424a06 2019-08-07 stsp break;
8919 dc424a06 2019-08-07 stsp }
8920 dc424a06 2019-08-07 stsp }
8921 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8922 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8923 0cb83759 2019-08-03 stsp if (err)
8924 dc424a06 2019-08-07 stsp break;
8925 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8926 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8927 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8928 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8929 0cb83759 2019-08-03 stsp else
8930 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8931 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8932 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8933 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8934 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8935 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8936 35213c7c 2020-07-23 stsp ssize_t target_len;
8937 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8938 35213c7c 2020-07-23 stsp sizeof(target_path));
8939 35213c7c 2020-07-23 stsp if (target_len == -1) {
8940 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8941 35213c7c 2020-07-23 stsp ondisk_path);
8942 35213c7c 2020-07-23 stsp break;
8943 35213c7c 2020-07-23 stsp }
8944 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8945 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8946 35213c7c 2020-07-23 stsp a->worktree->root_path);
8947 35213c7c 2020-07-23 stsp if (err)
8948 35213c7c 2020-07-23 stsp break;
8949 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8950 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8951 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8952 35213c7c 2020-07-23 stsp break;
8953 35213c7c 2020-07-23 stsp }
8954 35213c7c 2020-07-23 stsp }
8955 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8956 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8957 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8958 35213c7c 2020-07-23 stsp else
8959 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8960 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8961 0aeb8099 2020-07-23 stsp } else {
8962 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8963 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8964 0aeb8099 2020-07-23 stsp }
8965 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8966 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8967 dc424a06 2019-08-07 stsp break;
8968 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8969 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8970 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8971 f289c82c 2022-06-13 thomas if (err)
8972 f289c82c 2022-06-13 thomas break;
8973 f289c82c 2022-06-13 thomas /*
8974 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8975 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8976 f289c82c 2022-06-13 thomas */
8977 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8978 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8979 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8980 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8981 f289c82c 2022-06-13 thomas }
8982 0cb83759 2019-08-03 stsp break;
8983 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8984 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8985 d3e7c587 2019-08-03 stsp break;
8986 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8987 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8988 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8989 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8990 dc424a06 2019-08-07 stsp if (err)
8991 dc424a06 2019-08-07 stsp break;
8992 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8993 88f33a19 2019-08-08 stsp break;
8994 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8995 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8996 dc424a06 2019-08-07 stsp break;
8997 88f33a19 2019-08-08 stsp }
8998 dc424a06 2019-08-07 stsp }
8999 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9000 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9001 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9002 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9003 dc424a06 2019-08-07 stsp break;
9004 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9005 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9006 12463d8b 2019-12-13 stsp de_name);
9007 0cb83759 2019-08-03 stsp break;
9008 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9009 d3e7c587 2019-08-03 stsp break;
9010 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9011 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9012 ebf48fd5 2019-08-03 stsp break;
9013 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9014 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9015 2a06fe5f 2019-08-24 stsp break;
9016 0cb83759 2019-08-03 stsp default:
9017 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9018 537ac44b 2019-08-03 stsp break;
9019 0cb83759 2019-08-03 stsp }
9020 dc424a06 2019-08-07 stsp
9021 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9022 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9023 dc424a06 2019-08-07 stsp free(path_content);
9024 2db2652d 2019-08-07 stsp free(ondisk_path);
9025 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9026 0ebf8283 2019-07-24 stsp return err;
9027 0ebf8283 2019-07-24 stsp }
9028 0cb83759 2019-08-03 stsp
9029 0cb83759 2019-08-03 stsp const struct got_error *
9030 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9031 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9032 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9033 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9034 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9035 0cb83759 2019-08-03 stsp {
9036 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9037 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9038 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9039 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9040 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9041 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9042 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9043 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9044 0cb83759 2019-08-03 stsp
9045 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9046 0cb83759 2019-08-03 stsp if (err)
9047 0cb83759 2019-08-03 stsp return err;
9048 0cb83759 2019-08-03 stsp
9049 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9050 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9051 735ef5ac 2019-08-03 stsp if (err)
9052 735ef5ac 2019-08-03 stsp goto done;
9053 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9054 735ef5ac 2019-08-03 stsp if (err)
9055 735ef5ac 2019-08-03 stsp goto done;
9056 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9057 0cb83759 2019-08-03 stsp if (err)
9058 0cb83759 2019-08-03 stsp goto done;
9059 0cb83759 2019-08-03 stsp
9060 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9061 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9062 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9063 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9064 2db2652d 2019-08-07 stsp oka.repo = repo;
9065 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9066 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9067 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9068 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9069 735ef5ac 2019-08-03 stsp if (err)
9070 735ef5ac 2019-08-03 stsp goto done;
9071 735ef5ac 2019-08-03 stsp }
9072 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9073 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9074 2db2652d 2019-08-07 stsp goto done;
9075 2db2652d 2019-08-07 stsp }
9076 735ef5ac 2019-08-03 stsp
9077 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9078 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9079 2db2652d 2019-08-07 stsp spa.repo = repo;
9080 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9081 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9082 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9083 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9084 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9085 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9086 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9087 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9088 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9089 42005733 2019-08-03 stsp if (err)
9090 2db2652d 2019-08-07 stsp goto done;
9091 7b5dc508 2019-10-28 stsp }
9092 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9093 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9094 7b5dc508 2019-10-28 stsp goto done;
9095 0cb83759 2019-08-03 stsp }
9096 0cb83759 2019-08-03 stsp
9097 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9098 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9099 0cb83759 2019-08-03 stsp err = sync_err;
9100 0cb83759 2019-08-03 stsp done:
9101 735ef5ac 2019-08-03 stsp if (head_ref)
9102 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9103 735ef5ac 2019-08-03 stsp free(head_commit_id);
9104 0cb83759 2019-08-03 stsp free(fileindex_path);
9105 0cb83759 2019-08-03 stsp if (fileindex)
9106 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9107 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9108 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9109 0cb83759 2019-08-03 stsp err = unlockerr;
9110 0cb83759 2019-08-03 stsp return err;
9111 0cb83759 2019-08-03 stsp }
9112 ad493afc 2019-08-03 stsp
9113 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9114 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9115 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9116 ad493afc 2019-08-03 stsp struct got_repository *repo;
9117 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9118 ad493afc 2019-08-03 stsp void *progress_arg;
9119 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9120 2e1f37b0 2019-08-08 stsp void *patch_arg;
9121 ad493afc 2019-08-03 stsp };
9122 2e1f37b0 2019-08-08 stsp
9123 2e1f37b0 2019-08-08 stsp static const struct got_error *
9124 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9125 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9126 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9127 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9128 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9129 2e1f37b0 2019-08-08 stsp {
9130 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9131 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9132 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9133 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9134 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9135 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9136 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9137 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9138 2e1f37b0 2019-08-08 stsp
9139 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9140 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9141 2e1f37b0 2019-08-08 stsp
9142 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9143 2e1f37b0 2019-08-08 stsp if (err)
9144 2e1f37b0 2019-08-08 stsp return err;
9145 f4ae6ddb 2022-07-01 thomas
9146 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9147 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9148 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9149 f4ae6ddb 2022-07-01 thomas goto done;
9150 f4ae6ddb 2022-07-01 thomas }
9151 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9152 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9153 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9154 f4ae6ddb 2022-07-01 thomas goto done;
9155 f4ae6ddb 2022-07-01 thomas }
9156 f4ae6ddb 2022-07-01 thomas
9157 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
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(&path1, &f1, "got-unstage-blob-base", "");
9162 2e1f37b0 2019-08-08 stsp if (err)
9163 2e1f37b0 2019-08-08 stsp goto done;
9164 2e1f37b0 2019-08-08 stsp
9165 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9166 2e1f37b0 2019-08-08 stsp if (err)
9167 2e1f37b0 2019-08-08 stsp goto done;
9168 2e1f37b0 2019-08-08 stsp
9169 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9170 f4ae6ddb 2022-07-01 thomas fd2);
9171 2e1f37b0 2019-08-08 stsp if (err)
9172 2e1f37b0 2019-08-08 stsp goto done;
9173 2e1f37b0 2019-08-08 stsp
9174 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9175 2e1f37b0 2019-08-08 stsp if (err)
9176 2e1f37b0 2019-08-08 stsp goto done;
9177 ad493afc 2019-08-03 stsp
9178 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9179 2e1f37b0 2019-08-08 stsp if (err)
9180 2e1f37b0 2019-08-08 stsp goto done;
9181 2e1f37b0 2019-08-08 stsp
9182 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9183 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9184 2e1f37b0 2019-08-08 stsp if (err)
9185 2e1f37b0 2019-08-08 stsp goto done;
9186 2e1f37b0 2019-08-08 stsp
9187 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9188 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9189 2e1f37b0 2019-08-08 stsp if (err)
9190 2e1f37b0 2019-08-08 stsp goto done;
9191 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9192 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9193 2e1f37b0 2019-08-08 stsp if (err)
9194 2e1f37b0 2019-08-08 stsp goto done;
9195 2e1f37b0 2019-08-08 stsp
9196 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9197 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9198 2e1f37b0 2019-08-08 stsp goto done;
9199 2e1f37b0 2019-08-08 stsp }
9200 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9201 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9202 2e1f37b0 2019-08-08 stsp goto done;
9203 2e1f37b0 2019-08-08 stsp }
9204 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9205 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9206 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9207 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9208 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9209 fe621944 2020-11-10 stsp nchanges++;
9210 fe621944 2020-11-10 stsp }
9211 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9212 2e1f37b0 2019-08-08 stsp int choice;
9213 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9214 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9215 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9216 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9217 2e1f37b0 2019-08-08 stsp if (err)
9218 2e1f37b0 2019-08-08 stsp goto done;
9219 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9220 2e1f37b0 2019-08-08 stsp have_content = 1;
9221 19e4b907 2019-08-08 stsp else
9222 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9223 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9224 2e1f37b0 2019-08-08 stsp break;
9225 2e1f37b0 2019-08-08 stsp }
9226 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9227 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9228 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9229 2e1f37b0 2019-08-08 stsp done:
9230 2e1f37b0 2019-08-08 stsp free(label1);
9231 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9232 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9233 2e1f37b0 2019-08-08 stsp if (blob)
9234 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9235 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9236 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9237 2e1f37b0 2019-08-08 stsp if (staged_blob)
9238 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9239 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9240 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9241 fe621944 2020-11-10 stsp err = free_err;
9242 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9243 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9244 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9245 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9246 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9247 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9248 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9249 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9250 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9251 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9252 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9253 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9254 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9255 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9256 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9257 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9258 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9259 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9260 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9261 2e1f37b0 2019-08-08 stsp }
9262 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9263 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9264 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9265 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9266 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9267 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9268 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9269 2e1f37b0 2019-08-08 stsp }
9270 2e1f37b0 2019-08-08 stsp free(path1);
9271 2e1f37b0 2019-08-08 stsp free(path2);
9272 fda8017d 2020-07-23 stsp return err;
9273 fda8017d 2020-07-23 stsp }
9274 fda8017d 2020-07-23 stsp
9275 fda8017d 2020-07-23 stsp static const struct got_error *
9276 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9277 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9278 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9279 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9280 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9281 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9282 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9283 fda8017d 2020-07-23 stsp {
9284 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9285 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9286 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9287 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9288 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9289 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9290 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9291 fda8017d 2020-07-23 stsp struct stat sb;
9292 fda8017d 2020-07-23 stsp
9293 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9294 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9295 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9296 fda8017d 2020-07-23 stsp if (err)
9297 fda8017d 2020-07-23 stsp return err;
9298 fda8017d 2020-07-23 stsp
9299 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9300 fda8017d 2020-07-23 stsp return NULL;
9301 fda8017d 2020-07-23 stsp
9302 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9303 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9304 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9305 fda8017d 2020-07-23 stsp if (err)
9306 fda8017d 2020-07-23 stsp goto done;
9307 fda8017d 2020-07-23 stsp }
9308 fda8017d 2020-07-23 stsp
9309 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9310 fda8017d 2020-07-23 stsp if (f == NULL) {
9311 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9312 fda8017d 2020-07-23 stsp path_unstaged_content);
9313 fda8017d 2020-07-23 stsp goto done;
9314 fda8017d 2020-07-23 stsp }
9315 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9316 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9317 fda8017d 2020-07-23 stsp goto done;
9318 fda8017d 2020-07-23 stsp }
9319 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9320 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9321 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9322 fda8017d 2020-07-23 stsp size_t r;
9323 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9324 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9325 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9326 fda8017d 2020-07-23 stsp goto done;
9327 fda8017d 2020-07-23 stsp }
9328 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9329 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9330 fda8017d 2020-07-23 stsp goto done;
9331 fda8017d 2020-07-23 stsp }
9332 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9333 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9334 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9335 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9336 fda8017d 2020-07-23 stsp progress_arg);
9337 fda8017d 2020-07-23 stsp } else {
9338 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9339 67a66647 2021-05-31 stsp
9340 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9341 67a66647 2021-05-31 stsp if (err)
9342 67a66647 2021-05-31 stsp return err;
9343 67a66647 2021-05-31 stsp
9344 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9345 67a66647 2021-05-31 stsp parent) == -1) {
9346 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9347 67a66647 2021-05-31 stsp base_path = NULL;
9348 67a66647 2021-05-31 stsp goto done;
9349 67a66647 2021-05-31 stsp }
9350 67a66647 2021-05-31 stsp
9351 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9352 fc2a50f2 2022-11-01 thomas base_path, "");
9353 67a66647 2021-05-31 stsp if (err)
9354 67a66647 2021-05-31 stsp goto done;
9355 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9356 67a66647 2021-05-31 stsp blob_base);
9357 67a66647 2021-05-31 stsp if (err)
9358 67a66647 2021-05-31 stsp goto done;
9359 67a66647 2021-05-31 stsp
9360 b6b86fd1 2022-08-30 thomas /*
9361 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9362 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9363 eec2f5a9 2021-06-03 stsp */
9364 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9365 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9366 eec2f5a9 2021-06-03 stsp ondisk_path);
9367 eec2f5a9 2021-06-03 stsp if (err)
9368 eec2f5a9 2021-06-03 stsp goto done;
9369 eec2f5a9 2021-06-03 stsp } else {
9370 eec2f5a9 2021-06-03 stsp int fd;
9371 06340621 2021-12-31 thomas fd = open(ondisk_path,
9372 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9373 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9374 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9375 eec2f5a9 2021-06-03 stsp goto done;
9376 eec2f5a9 2021-06-03 stsp }
9377 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9378 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9379 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9380 eec2f5a9 2021-06-03 stsp close(fd);
9381 eec2f5a9 2021-06-03 stsp goto done;
9382 eec2f5a9 2021-06-03 stsp }
9383 eec2f5a9 2021-06-03 stsp }
9384 eec2f5a9 2021-06-03 stsp
9385 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9386 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9387 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9388 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9389 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9390 fda8017d 2020-07-23 stsp }
9391 fda8017d 2020-07-23 stsp if (err)
9392 fda8017d 2020-07-23 stsp goto done;
9393 fda8017d 2020-07-23 stsp
9394 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9395 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9396 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9397 2ac8aa02 2020-11-09 stsp } else {
9398 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9399 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9400 2ac8aa02 2020-11-09 stsp }
9401 fda8017d 2020-07-23 stsp done:
9402 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9403 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9404 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9405 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9406 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9407 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9408 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9409 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9410 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9411 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9412 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9413 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9414 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9415 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9416 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9417 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9418 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9419 67a66647 2021-05-31 stsp free(blob_base_path);
9420 67a66647 2021-05-31 stsp free(parent);
9421 67a66647 2021-05-31 stsp free(base_path);
9422 2e1f37b0 2019-08-08 stsp return err;
9423 2e1f37b0 2019-08-08 stsp }
9424 2e1f37b0 2019-08-08 stsp
9425 ad493afc 2019-08-03 stsp static const struct got_error *
9426 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9427 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9428 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9429 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9430 ad493afc 2019-08-03 stsp {
9431 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9432 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9433 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9434 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9435 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9436 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9437 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9438 9bc94a15 2019-08-03 stsp struct stat sb;
9439 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9440 ad493afc 2019-08-03 stsp
9441 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9442 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9443 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9444 2e1f37b0 2019-08-08 stsp return NULL;
9445 2e1f37b0 2019-08-08 stsp
9446 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9447 ad493afc 2019-08-03 stsp if (ie == NULL)
9448 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9449 9bc94a15 2019-08-03 stsp
9450 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9451 9bc94a15 2019-08-03 stsp == -1)
9452 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9453 ad493afc 2019-08-03 stsp
9454 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9455 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9456 f69721c3 2019-10-21 stsp if (err)
9457 f69721c3 2019-10-21 stsp goto done;
9458 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9459 f69721c3 2019-10-21 stsp id_str) == -1) {
9460 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9461 f4ae6ddb 2022-07-01 thomas goto done;
9462 f4ae6ddb 2022-07-01 thomas }
9463 f4ae6ddb 2022-07-01 thomas
9464 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9465 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9466 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9467 f4ae6ddb 2022-07-01 thomas goto done;
9468 f4ae6ddb 2022-07-01 thomas }
9469 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9470 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9471 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9472 f69721c3 2019-10-21 stsp goto done;
9473 f69721c3 2019-10-21 stsp }
9474 f69721c3 2019-10-21 stsp
9475 ad493afc 2019-08-03 stsp switch (staged_status) {
9476 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9477 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9478 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9479 ad493afc 2019-08-03 stsp if (err)
9480 ad493afc 2019-08-03 stsp break;
9481 ad493afc 2019-08-03 stsp /* fall through */
9482 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9483 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9484 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9485 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9486 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9487 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9488 2e1f37b0 2019-08-08 stsp if (err)
9489 2e1f37b0 2019-08-08 stsp break;
9490 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9491 2e1f37b0 2019-08-08 stsp break;
9492 2e1f37b0 2019-08-08 stsp } else {
9493 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9494 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9495 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9496 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9497 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9498 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9499 2e1f37b0 2019-08-08 stsp }
9500 2e1f37b0 2019-08-08 stsp }
9501 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9502 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9503 ad493afc 2019-08-03 stsp if (err)
9504 ad493afc 2019-08-03 stsp break;
9505 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9506 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9507 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9508 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9509 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9510 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9511 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9512 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9513 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9514 ea7786be 2020-07-23 stsp break;
9515 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9516 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9517 36bf999c 2020-07-23 stsp char *staged_target;
9518 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9519 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9520 36bf999c 2020-07-23 stsp if (err)
9521 36bf999c 2020-07-23 stsp goto done;
9522 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9523 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9524 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9525 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9526 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9527 36bf999c 2020-07-23 stsp free(staged_target);
9528 dfe9fba0 2020-07-23 stsp } else {
9529 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9530 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9531 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9532 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9533 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9534 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9535 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9536 dfe9fba0 2020-07-23 stsp }
9537 ea7786be 2020-07-23 stsp break;
9538 ea7786be 2020-07-23 stsp default:
9539 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9540 ea7786be 2020-07-23 stsp break;
9541 ea7786be 2020-07-23 stsp }
9542 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9543 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9544 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9545 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9546 2ac8aa02 2020-11-09 stsp }
9547 ad493afc 2019-08-03 stsp break;
9548 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9549 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9550 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9551 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9552 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9553 2e1f37b0 2019-08-08 stsp if (err)
9554 2e1f37b0 2019-08-08 stsp break;
9555 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9556 2e1f37b0 2019-08-08 stsp break;
9557 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9558 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9559 2e1f37b0 2019-08-08 stsp break;
9560 2e1f37b0 2019-08-08 stsp }
9561 2e1f37b0 2019-08-08 stsp }
9562 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9563 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9564 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9565 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9566 9bc94a15 2019-08-03 stsp if (err)
9567 9bc94a15 2019-08-03 stsp break;
9568 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9569 ad493afc 2019-08-03 stsp break;
9570 ad493afc 2019-08-03 stsp }
9571 f69721c3 2019-10-21 stsp done:
9572 ad493afc 2019-08-03 stsp free(ondisk_path);
9573 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9574 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9575 ad493afc 2019-08-03 stsp if (blob_base)
9576 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9577 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9578 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9579 ad493afc 2019-08-03 stsp if (blob_staged)
9580 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9581 f69721c3 2019-10-21 stsp free(id_str);
9582 f69721c3 2019-10-21 stsp free(label_orig);
9583 ad493afc 2019-08-03 stsp return err;
9584 ad493afc 2019-08-03 stsp }
9585 ad493afc 2019-08-03 stsp
9586 ad493afc 2019-08-03 stsp const struct got_error *
9587 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9588 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9589 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9590 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9591 ad493afc 2019-08-03 stsp struct got_repository *repo)
9592 ad493afc 2019-08-03 stsp {
9593 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9594 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9595 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9596 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9597 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9598 ad493afc 2019-08-03 stsp
9599 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9600 ad493afc 2019-08-03 stsp if (err)
9601 ad493afc 2019-08-03 stsp return err;
9602 ad493afc 2019-08-03 stsp
9603 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
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 upa.worktree = worktree;
9608 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9609 ad493afc 2019-08-03 stsp upa.repo = repo;
9610 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9611 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9612 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9613 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9614 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9615 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9616 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9617 ad493afc 2019-08-03 stsp if (err)
9618 ad493afc 2019-08-03 stsp goto done;
9619 ad493afc 2019-08-03 stsp }
9620 ad493afc 2019-08-03 stsp
9621 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9622 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9623 ad493afc 2019-08-03 stsp err = sync_err;
9624 ad493afc 2019-08-03 stsp done:
9625 ad493afc 2019-08-03 stsp free(fileindex_path);
9626 ad493afc 2019-08-03 stsp if (fileindex)
9627 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9628 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9629 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9630 ad493afc 2019-08-03 stsp err = unlockerr;
9631 ad493afc 2019-08-03 stsp return err;
9632 ad493afc 2019-08-03 stsp }
9633 b2118c49 2020-07-28 stsp
9634 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9635 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9636 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9637 b2118c49 2020-07-28 stsp void *info_arg;
9638 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9639 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9640 b2118c49 2020-07-28 stsp void *cancel_arg;
9641 b2118c49 2020-07-28 stsp };
9642 b2118c49 2020-07-28 stsp
9643 b2118c49 2020-07-28 stsp static const struct got_error *
9644 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9645 b2118c49 2020-07-28 stsp {
9646 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9647 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9648 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9649 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9650 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9651 b2118c49 2020-07-28 stsp int stage;
9652 b2118c49 2020-07-28 stsp
9653 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9654 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9655 b2118c49 2020-07-28 stsp
9656 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9657 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9658 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9659 b2118c49 2020-07-28 stsp break;
9660 b2118c49 2020-07-28 stsp }
9661 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9662 b2118c49 2020-07-28 stsp return NULL;
9663 b2118c49 2020-07-28 stsp
9664 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9665 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9666 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9667 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9668 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9669 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9670 43010591 2023-02-17 thomas &staged_blob_id, ie);
9671 b2118c49 2020-07-28 stsp }
9672 b2118c49 2020-07-28 stsp
9673 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9674 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9675 b2118c49 2020-07-28 stsp
9676 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9677 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9678 b2118c49 2020-07-28 stsp }
9679 b2118c49 2020-07-28 stsp
9680 b2118c49 2020-07-28 stsp const struct got_error *
9681 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9682 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9683 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9684 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9685 b2118c49 2020-07-28 stsp
9686 b2118c49 2020-07-28 stsp {
9687 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9688 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9689 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9690 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9691 b2118c49 2020-07-28 stsp
9692 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9693 b2118c49 2020-07-28 stsp if (err)
9694 b2118c49 2020-07-28 stsp return err;
9695 b2118c49 2020-07-28 stsp
9696 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9697 b2118c49 2020-07-28 stsp if (err)
9698 b2118c49 2020-07-28 stsp goto done;
9699 b2118c49 2020-07-28 stsp
9700 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9701 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9702 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9703 b2118c49 2020-07-28 stsp arg.paths = paths;
9704 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9705 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9706 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9707 b2118c49 2020-07-28 stsp &arg);
9708 b2118c49 2020-07-28 stsp done:
9709 b2118c49 2020-07-28 stsp free(fileindex_path);
9710 b2118c49 2020-07-28 stsp if (fileindex)
9711 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9712 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9713 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9714 b2118c49 2020-07-28 stsp err = unlockerr;
9715 b2118c49 2020-07-28 stsp return err;
9716 b2118c49 2020-07-28 stsp }
9717 814624e7 2022-03-22 thomas
9718 814624e7 2022-03-22 thomas static const struct got_error *
9719 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9720 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9721 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9722 814624e7 2022-03-22 thomas {
9723 814624e7 2022-03-22 thomas const struct got_error *err;
9724 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9725 814624e7 2022-03-22 thomas struct stat sb;
9726 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9727 814624e7 2022-03-22 thomas
9728 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9729 814624e7 2022-03-22 thomas if (err)
9730 814624e7 2022-03-22 thomas return err;
9731 814624e7 2022-03-22 thomas
9732 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9733 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9734 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9735 814624e7 2022-03-22 thomas
9736 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9737 814624e7 2022-03-22 thomas if (ie) {
9738 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9739 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9740 12de5570 2022-05-01 thomas repo);
9741 814624e7 2022-03-22 thomas if (err)
9742 814624e7 2022-03-22 thomas goto done;
9743 814624e7 2022-03-22 thomas } else {
9744 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9745 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9746 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9747 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9748 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9749 814624e7 2022-03-22 thomas ondisk_path);
9750 814624e7 2022-03-22 thomas goto done;
9751 814624e7 2022-03-22 thomas }
9752 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9753 814624e7 2022-03-22 thomas }
9754 814624e7 2022-03-22 thomas }
9755 814624e7 2022-03-22 thomas
9756 814624e7 2022-03-22 thomas done:
9757 814624e7 2022-03-22 thomas free(ondisk_path);
9758 814624e7 2022-03-22 thomas return err;
9759 814624e7 2022-03-22 thomas }
9760 814624e7 2022-03-22 thomas
9761 814624e7 2022-03-22 thomas static const struct got_error *
9762 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9763 814624e7 2022-03-22 thomas unsigned char staged_status)
9764 814624e7 2022-03-22 thomas {
9765 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9766 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9767 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9768 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9769 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9770 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9771 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9772 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9773 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9774 814624e7 2022-03-22 thomas return NULL;
9775 814624e7 2022-03-22 thomas }
9776 814624e7 2022-03-22 thomas
9777 814624e7 2022-03-22 thomas static const struct got_error *
9778 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9779 814624e7 2022-03-22 thomas {
9780 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9781 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9782 814624e7 2022-03-22 thomas return NULL;
9783 814624e7 2022-03-22 thomas }
9784 814624e7 2022-03-22 thomas
9785 814624e7 2022-03-22 thomas static const struct got_error *
9786 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9787 814624e7 2022-03-22 thomas unsigned char staged_status)
9788 814624e7 2022-03-22 thomas {
9789 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9790 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9791 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9792 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9793 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9794 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9795 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9796 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9797 814624e7 2022-03-22 thomas return NULL;
9798 814624e7 2022-03-22 thomas }
9799 814624e7 2022-03-22 thomas
9800 814624e7 2022-03-22 thomas const struct got_error *
9801 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9802 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9803 814624e7 2022-03-22 thomas {
9804 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9805 814624e7 2022-03-22 thomas }
9806 814624e7 2022-03-22 thomas
9807 814624e7 2022-03-22 thomas const struct got_error *
9808 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9809 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9810 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9811 814624e7 2022-03-22 thomas {
9812 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9813 814624e7 2022-03-22 thomas int file_renamed = 0;
9814 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9815 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9816 814624e7 2022-03-22 thomas
9817 814624e7 2022-03-22 thomas *oldpath = NULL;
9818 814624e7 2022-03-22 thomas *newpath = NULL;
9819 814624e7 2022-03-22 thomas
9820 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9821 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9822 814624e7 2022-03-22 thomas if (err)
9823 814624e7 2022-03-22 thomas goto done;
9824 814624e7 2022-03-22 thomas
9825 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9826 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9827 814624e7 2022-03-22 thomas if (err)
9828 814624e7 2022-03-22 thomas goto done;
9829 814624e7 2022-03-22 thomas
9830 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9831 814624e7 2022-03-22 thomas file_renamed = 1;
9832 814624e7 2022-03-22 thomas
9833 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9834 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9835 814624e7 2022-03-22 thomas else if (file_renamed) {
9836 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9837 814624e7 2022-03-22 thomas if (err == NULL)
9838 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9839 814624e7 2022-03-22 thomas } else if (old == NULL)
9840 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9841 814624e7 2022-03-22 thomas else
9842 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9843 814624e7 2022-03-22 thomas
9844 814624e7 2022-03-22 thomas done:
9845 814624e7 2022-03-22 thomas if (err) {
9846 814624e7 2022-03-22 thomas free(*oldpath);
9847 814624e7 2022-03-22 thomas *oldpath = NULL;
9848 814624e7 2022-03-22 thomas free(*newpath);
9849 814624e7 2022-03-22 thomas *newpath = NULL;
9850 814624e7 2022-03-22 thomas }
9851 814624e7 2022-03-22 thomas return err;
9852 814624e7 2022-03-22 thomas }
9853 814624e7 2022-03-22 thomas
9854 814624e7 2022-03-22 thomas const struct got_error *
9855 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9856 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9857 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9858 814624e7 2022-03-22 thomas {
9859 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9860 5e22b737 2022-05-31 thomas
9861 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9862 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9863 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9864 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9865 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9866 5e22b737 2022-05-31 thomas saa.repo = repo;
9867 5e22b737 2022-05-31 thomas
9868 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9869 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9870 814624e7 2022-03-22 thomas }
9871 5e22b737 2022-05-31 thomas
9872 5e22b737 2022-05-31 thomas const struct got_error *
9873 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9874 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9875 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9876 5e22b737 2022-05-31 thomas {
9877 8ec7352a 2023-06-01 thomas const struct got_error *err;
9878 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9879 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9880 5e22b737 2022-05-31 thomas
9881 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9882 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9883 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9884 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9885 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9886 5e22b737 2022-05-31 thomas sda.repo = repo;
9887 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9888 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9889 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9890 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9891 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9892 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9893 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9894 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9895 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9896 8ec7352a 2023-06-01 thomas
9897 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9898 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9899 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9900 8ec7352a 2023-06-01 thomas return err;
9901 5e22b737 2022-05-31 thomas }
9902 5e22b737 2022-05-31 thomas
9903 5e22b737 2022-05-31 thomas const struct got_error *
9904 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9905 36832a8e 2022-05-31 thomas const char *fileindex_path)
9906 5e22b737 2022-05-31 thomas {
9907 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9908 5e22b737 2022-05-31 thomas
9909 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9910 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9911 5e22b737 2022-05-31 thomas
9912 5e22b737 2022-05-31 thomas return err;
9913 5e22b737 2022-05-31 thomas }