Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
999 67a66647 2021-05-31 stsp if (err)
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1002 db590691 2021-06-02 stsp } else {
1003 db590691 2021-06-02 stsp /*
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp */
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1012 db590691 2021-06-02 stsp }
1013 67a66647 2021-05-31 stsp }
1014 67a66647 2021-05-31 stsp
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1019 14c901f1 2019-08-08 stsp }
1020 14c901f1 2019-08-08 stsp
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp }
1037 eec2f5a9 2021-06-03 stsp
1038 b6b86fd1 2022-08-30 thomas /*
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp */
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp if (err)
1045 eec2f5a9 2021-06-03 stsp goto done;
1046 eec2f5a9 2021-06-03 stsp } else {
1047 eec2f5a9 2021-06-03 stsp int fd;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp }
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1058 eec2f5a9 2021-06-03 stsp }
1059 14c901f1 2019-08-08 stsp }
1060 14c901f1 2019-08-08 stsp
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 14c901f1 2019-08-08 stsp done:
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 67a66647 2021-05-31 stsp }
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp }
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1084 4a1ddfc2 2019-01-12 stsp }
1085 4a1ddfc2 2019-01-12 stsp
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp {
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 13d9040b 2019-03-27 stsp
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 65b05cec 2020-07-23 stsp
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp if (err)
1098 054041d0 2020-06-24 stsp return err;
1099 054041d0 2020-06-24 stsp
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1106 054041d0 2020-06-24 stsp done:
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp else
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1112 1ebedb77 2019-10-19 stsp }
1113 1ebedb77 2019-10-19 stsp
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp {
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1126 1ebedb77 2019-10-19 stsp }
1127 1ebedb77 2019-10-19 stsp
1128 a2c162eb 2022-10-30 thomas return st_mode;
1129 a2c162eb 2022-10-30 thomas }
1130 a2c162eb 2022-10-30 thomas
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1133 a2c162eb 2022-10-30 thomas {
1134 a2c162eb 2022-10-30 thomas mode_t um;
1135 a2c162eb 2022-10-30 thomas
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1139 13d9040b 2019-03-27 stsp }
1140 13d9040b 2019-03-27 stsp
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 8ba819a3 2020-07-23 stsp
1150 5a1fbc73 2020-07-23 stsp /*
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1153 5a1fbc73 2020-07-23 stsp */
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1157 5a1fbc73 2020-07-23 stsp {
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 5a1fbc73 2020-07-23 stsp int fd;
1162 5a1fbc73 2020-07-23 stsp
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 c6e8a826 2021-04-05 stsp
1165 5a1fbc73 2020-07-23 stsp /*
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1172 5a1fbc73 2020-07-23 stsp */
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1177 5a1fbc73 2020-07-23 stsp
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 5a1fbc73 2020-07-23 stsp }
1187 5a1fbc73 2020-07-23 stsp
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp }
1194 3c1ec782 2020-07-23 stsp
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 3c1ec782 2020-07-23 stsp {
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1208 3c1ec782 2020-07-23 stsp }
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp /*
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1216 3c1ec782 2020-07-23 stsp */
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1220 ce031e9e 2020-10-20 stsp if (err)
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1225 ce031e9e 2020-10-20 stsp }
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp }
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1234 3c1ec782 2020-07-23 stsp if (err)
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp } else {
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp if (err)
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp }
1258 5a1fbc73 2020-07-23 stsp
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 9d31a1d8 2018-03-11 stsp {
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 8ba819a3 2020-07-23 stsp
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 2e1fa222 2020-07-23 stsp
1275 9e39ca77 2022-07-21 thomas /*
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp */
1281 8ba819a3 2020-07-23 stsp do {
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1285 3521b466 2022-07-21 thomas
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1295 8ba819a3 2020-07-23 stsp }
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1305 8ba819a3 2020-07-23 stsp
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1308 3c1ec782 2020-07-23 stsp if (err)
1309 3c1ec782 2020-07-23 stsp return err;
1310 8ba819a3 2020-07-23 stsp
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1319 906c123b 2020-07-23 stsp }
1320 906c123b 2020-07-23 stsp
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1328 c90c8ce3 2020-07-23 stsp }
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1331 5a1fbc73 2020-07-23 stsp if (err)
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1339 c6e8a826 2021-04-05 stsp } else {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 c6e8a826 2021-04-05 stsp }
1343 f35fa46a 2020-07-23 stsp }
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp }
1346 f35fa46a 2020-07-23 stsp
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1350 f4994adc 2020-10-20 stsp if (err)
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1354 f35fa46a 2020-07-23 stsp if (err)
1355 9e39ca77 2022-07-21 thomas return err;
1356 f35fa46a 2020-07-23 stsp /*
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1359 f35fa46a 2020-07-23 stsp */
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1379 8ba819a3 2020-07-23 stsp } else {
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1382 8ba819a3 2020-07-23 stsp }
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp }
1388 8ba819a3 2020-07-23 stsp
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 8ba819a3 2020-07-23 stsp {
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1403 8ba819a3 2020-07-23 stsp
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1411 f5375317 2020-10-20 stsp if (err)
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1416 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp if (err)
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 a2c162eb 2022-10-30 thomas mode);
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1430 3b9f0f87 2020-07-23 stsp }
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1437 d70b8e30 2018-12-27 stsp } else {
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp if (err)
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1443 a2c162eb 2022-10-30 thomas
1444 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1445 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1446 a2c162eb 2022-10-30 thomas tmppath);
1447 a2c162eb 2022-10-30 thomas goto done;
1448 a2c162eb 2022-10-30 thomas }
1449 9d31a1d8 2018-03-11 stsp }
1450 507dc3bb 2018-12-29 stsp } else
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 3818e3c4 2020-11-01 naddy }
1453 3818e3c4 2020-11-01 naddy
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp if (err)
1465 bd6aa359 2020-07-23 stsp goto done;
1466 bd6aa359 2020-07-23 stsp }
1467 d7b62c98 2018-12-27 stsp
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp do {
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (err)
1473 9d31a1d8 2018-03-11 stsp break;
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1483 9d31a1d8 2018-03-11 stsp }
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp }
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 9d31a1d8 2018-03-11 stsp
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1491 816dc654 2019-02-16 stsp }
1492 9d31a1d8 2018-03-11 stsp
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1497 f6d8c0ac 2020-11-09 stsp }
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1502 68ed9ba5 2019-02-10 stsp }
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1505 507dc3bb 2018-12-29 stsp }
1506 507dc3bb 2018-12-29 stsp
1507 9d31a1d8 2018-03-11 stsp done:
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1514 6353ad76 2019-02-08 stsp }
1515 6353ad76 2019-02-08 stsp
1516 be94c032 2023-02-20 thomas /*
1517 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1519 be94c032 2023-02-20 thomas */
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1522 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1523 be94c032 2023-02-20 thomas FILE *ondisk_file)
1524 7154f6ce 2019-03-27 stsp {
1525 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1526 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1530 7154f6ce 2019-03-27 stsp };
1531 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1532 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1533 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1534 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1535 9bdd68dd 2020-01-02 naddy char *line = NULL;
1536 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1537 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1538 7154f6ce 2019-03-27 stsp
1539 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1540 8de9d8ad 2023-02-20 thomas return NULL;
1541 be94c032 2023-02-20 thomas
1542 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1543 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1544 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1545 be94c032 2023-02-20 thomas
1546 8de9d8ad 2023-02-20 thomas if (blob) {
1547 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1548 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 be94c032 2023-02-20 thomas if (err)
1550 be94c032 2023-02-20 thomas goto done;
1551 8de9d8ad 2023-02-20 thomas }
1552 be94c032 2023-02-20 thomas
1553 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 8de9d8ad 2023-02-20 thomas if (err)
1556 8de9d8ad 2023-02-20 thomas goto done;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1559 8de9d8ad 2023-02-20 thomas
1560 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1562 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1563 450d9f6b 2023-02-20 thomas off_t pos;
1564 8de9d8ad 2023-02-20 thomas
1565 8de9d8ad 2023-02-20 thomas /*
1566 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1567 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1568 8de9d8ad 2023-02-20 thomas */
1569 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1570 8de9d8ad 2023-02-20 thomas
1571 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1573 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1574 7154f6ce 2019-03-27 stsp }
1575 7154f6ce 2019-03-27 stsp
1576 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1577 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 450d9f6b 2023-02-20 thomas goto done;
1580 7154f6ce 2019-03-27 stsp }
1581 8de9d8ad 2023-02-20 thomas
1582 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1584 450d9f6b 2023-02-20 thomas
1585 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1586 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1587 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1588 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 8de9d8ad 2023-02-20 thomas break;
1592 8de9d8ad 2023-02-20 thomas }
1593 8de9d8ad 2023-02-20 thomas
1594 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1595 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1596 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1597 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1599 8de9d8ad 2023-02-20 thomas goto done;
1600 8de9d8ad 2023-02-20 thomas } else
1601 8de9d8ad 2023-02-20 thomas i++;
1602 8de9d8ad 2023-02-20 thomas }
1603 8de9d8ad 2023-02-20 thomas ++ln;
1604 8de9d8ad 2023-02-20 thomas }
1605 7154f6ce 2019-03-27 stsp }
1606 be94c032 2023-02-20 thomas
1607 be94c032 2023-02-20 thomas done:
1608 9bdd68dd 2020-01-02 naddy free(line);
1609 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1611 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1612 8de9d8ad 2023-02-20 thomas if (err == NULL)
1613 8de9d8ad 2023-02-20 thomas err = free_err;
1614 7154f6ce 2019-03-27 stsp
1615 7154f6ce 2019-03-27 stsp return err;
1616 e2b1e152 2019-07-13 stsp }
1617 e2b1e152 2019-07-13 stsp
1618 e2b1e152 2019-07-13 stsp static int
1619 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 1ebedb77 2019-10-19 stsp {
1621 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 1ebedb77 2019-10-19 stsp }
1624 1ebedb77 2019-10-19 stsp
1625 1ebedb77 2019-10-19 stsp static int
1626 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 e2b1e152 2019-07-13 stsp {
1628 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1633 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1634 c363b2c1 2019-08-03 stsp }
1635 c363b2c1 2019-08-03 stsp
1636 c363b2c1 2019-08-03 stsp static unsigned char
1637 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1638 c363b2c1 2019-08-03 stsp {
1639 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1644 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1646 c363b2c1 2019-08-03 stsp default:
1647 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp }
1650 a919d5c4 2020-07-23 stsp
1651 a919d5c4 2020-07-23 stsp static const struct got_error *
1652 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1653 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1654 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1655 a919d5c4 2020-07-23 stsp {
1656 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1657 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1658 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1659 a919d5c4 2020-07-23 stsp ssize_t elen;
1660 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1661 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1665 a919d5c4 2020-07-23 stsp
1666 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1667 a919d5c4 2020-07-23 stsp do {
1668 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1669 a919d5c4 2020-07-23 stsp if (err)
1670 a919d5c4 2020-07-23 stsp return err;
1671 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1672 a919d5c4 2020-07-23 stsp /*
1673 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1674 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1675 a919d5c4 2020-07-23 stsp */
1676 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1677 a919d5c4 2020-07-23 stsp }
1678 a919d5c4 2020-07-23 stsp if (len > 0) {
1679 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1680 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1681 a919d5c4 2020-07-23 stsp len - hdrlen);
1682 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1683 a919d5c4 2020-07-23 stsp hdrlen = 0;
1684 a919d5c4 2020-07-23 stsp }
1685 a919d5c4 2020-07-23 stsp } while (len != 0);
1686 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1687 a919d5c4 2020-07-23 stsp
1688 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1689 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 a919d5c4 2020-07-23 stsp if (elen == -1)
1691 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1692 a919d5c4 2020-07-23 stsp } else {
1693 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1694 a919d5c4 2020-07-23 stsp if (elen == -1)
1695 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1696 c363b2c1 2019-08-03 stsp }
1697 a919d5c4 2020-07-23 stsp
1698 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 a919d5c4 2020-07-23 stsp
1701 a919d5c4 2020-07-23 stsp return NULL;
1702 7154f6ce 2019-03-27 stsp }
1703 7154f6ce 2019-03-27 stsp
1704 7154f6ce 2019-03-27 stsp static const struct got_error *
1705 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1706 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1707 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1708 6353ad76 2019-02-08 stsp {
1709 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1710 6353ad76 2019-02-08 stsp struct got_object_id id;
1711 6353ad76 2019-02-08 stsp size_t hdrlen;
1712 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1713 6353ad76 2019-02-08 stsp FILE *f = NULL;
1714 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1715 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1716 6353ad76 2019-02-08 stsp size_t flen, blen;
1717 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1718 6353ad76 2019-02-08 stsp
1719 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1720 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1721 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1722 6353ad76 2019-02-08 stsp
1723 7f91a133 2019-12-13 stsp /*
1724 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1725 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1726 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1727 7f91a133 2019-12-13 stsp */
1728 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1729 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1731 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 882ef1b9 2019-12-13 stsp else
1734 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1735 882ef1b9 2019-12-13 stsp goto done;
1736 882ef1b9 2019-12-13 stsp }
1737 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1738 3d35a492 2019-12-13 stsp goto done;
1739 3d35a492 2019-12-13 stsp }
1740 7f91a133 2019-12-13 stsp } else {
1741 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1743 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1744 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1745 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1747 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1748 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1750 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1752 3d35a492 2019-12-13 stsp else
1753 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1754 3d35a492 2019-12-13 stsp goto done;
1755 3d35a492 2019-12-13 stsp }
1756 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1757 1338848f 2019-12-13 stsp goto done;
1758 a378724f 2019-02-10 stsp }
1759 a378724f 2019-02-10 stsp }
1760 6353ad76 2019-02-08 stsp
1761 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1763 1338848f 2019-12-13 stsp goto done;
1764 3f148bc6 2019-07-27 stsp }
1765 efdd40df 2019-07-27 stsp
1766 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1768 1338848f 2019-12-13 stsp goto done;
1769 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1770 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1771 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1772 1338848f 2019-12-13 stsp goto done;
1773 d00136be 2019-03-26 stsp }
1774 b8f41171 2019-02-10 stsp
1775 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1776 f179e66d 2020-07-23 stsp goto done;
1777 f179e66d 2020-07-23 stsp
1778 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1779 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1781 1338848f 2019-12-13 stsp goto done;
1782 f179e66d 2020-07-23 stsp }
1783 6353ad76 2019-02-08 stsp
1784 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1785 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp else
1788 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1789 c363b2c1 2019-08-03 stsp
1790 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1791 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1792 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1793 f4ae6ddb 2022-07-01 thomas goto done;
1794 f4ae6ddb 2022-07-01 thomas }
1795 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 6353ad76 2019-02-08 stsp if (err)
1797 1338848f 2019-12-13 stsp goto done;
1798 6353ad76 2019-02-08 stsp
1799 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1800 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1801 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1802 a919d5c4 2020-07-23 stsp goto done;
1803 a919d5c4 2020-07-23 stsp }
1804 a919d5c4 2020-07-23 stsp
1805 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1806 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 ab0d4361 2019-12-13 stsp if (fd == -1) {
1808 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1809 ab0d4361 2019-12-13 stsp goto done;
1810 ab0d4361 2019-12-13 stsp }
1811 3d35a492 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp
1813 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1814 6353ad76 2019-02-08 stsp if (f == NULL) {
1815 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1816 6353ad76 2019-02-08 stsp goto done;
1817 6353ad76 2019-02-08 stsp }
1818 1338848f 2019-12-13 stsp fd = -1;
1819 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1820 656b1f76 2019-05-11 jcs for (;;) {
1821 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1823 6353ad76 2019-02-08 stsp if (err)
1824 7154f6ce 2019-03-27 stsp goto done;
1825 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1826 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1828 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1829 7154f6ce 2019-03-27 stsp goto done;
1830 80c5c120 2019-02-19 stsp }
1831 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1832 6353ad76 2019-02-08 stsp if (flen != 0)
1833 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1834 6353ad76 2019-02-08 stsp break;
1835 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1836 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1837 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp break;
1839 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1840 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1841 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1843 6353ad76 2019-02-08 stsp break;
1844 6353ad76 2019-02-08 stsp }
1845 6353ad76 2019-02-08 stsp } else {
1846 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1847 6353ad76 2019-02-08 stsp break;
1848 6353ad76 2019-02-08 stsp }
1849 6353ad76 2019-02-08 stsp hdrlen = 0;
1850 6353ad76 2019-02-08 stsp }
1851 7154f6ce 2019-03-27 stsp
1852 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1853 7154f6ce 2019-03-27 stsp rewind(f);
1854 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1855 be94c032 2023-02-20 thomas sb, f);
1856 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1857 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1858 6353ad76 2019-02-08 stsp done:
1859 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1861 6353ad76 2019-02-08 stsp if (blob)
1862 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1863 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1864 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1865 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1866 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1867 9d31a1d8 2018-03-11 stsp return err;
1868 e2b1e152 2019-07-13 stsp }
1869 e2b1e152 2019-07-13 stsp
1870 e2b1e152 2019-07-13 stsp /*
1871 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1872 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1873 e2b1e152 2019-07-13 stsp */
1874 e2b1e152 2019-07-13 stsp static const struct got_error *
1875 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1877 e2b1e152 2019-07-13 stsp {
1878 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1880 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1881 e2b1e152 2019-07-13 stsp
1882 e2b1e152 2019-07-13 stsp return NULL;
1883 9d31a1d8 2018-03-11 stsp }
1884 9d31a1d8 2018-03-11 stsp
1885 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1886 9f323212 2023-03-10 thomas
1887 9d31a1d8 2018-03-11 stsp static const struct got_error *
1888 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1889 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1891 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 0584f854 2019-04-06 stsp void *progress_arg)
1893 9d31a1d8 2018-03-11 stsp {
1894 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1895 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1896 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1897 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1898 b8f41171 2019-02-10 stsp struct stat sb;
1899 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1900 9d31a1d8 2018-03-11 stsp
1901 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1903 6353ad76 2019-02-08 stsp
1904 abb4604f 2019-07-27 stsp if (ie) {
1905 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 a76c42e6 2019-08-03 stsp goto done;
1908 a76c42e6 2019-08-03 stsp }
1909 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 7f91a133 2019-12-13 stsp repo);
1911 abb4604f 2019-07-27 stsp if (err)
1912 abb4604f 2019-07-27 stsp goto done;
1913 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1915 c90c8ce3 2020-07-23 stsp } else {
1916 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1917 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1918 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1919 e6f4ba31 2021-09-24 thomas ondisk_path);
1920 e6f4ba31 2021-09-24 thomas goto done;
1921 e6f4ba31 2021-09-24 thomas }
1922 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1924 e6f4ba31 2021-09-24 thomas } else {
1925 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1927 e6f4ba31 2021-09-24 thomas else
1928 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1929 e6f4ba31 2021-09-24 thomas }
1930 c90c8ce3 2020-07-23 stsp }
1931 6353ad76 2019-02-08 stsp
1932 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1933 2c41dce7 2021-06-27 stsp if (ie)
1934 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1935 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1936 b8f41171 2019-02-10 stsp goto done;
1937 b8f41171 2019-02-10 stsp }
1938 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1939 a769b60b 2021-06-27 stsp if (ie)
1940 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1941 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 5036ab18 2020-04-18 stsp path);
1943 5036ab18 2020-04-18 stsp goto done;
1944 9f323212 2023-03-10 thomas }
1945 9f323212 2023-03-10 thomas
1946 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1948 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1949 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1950 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1951 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1952 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1953 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1954 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1955 9f323212 2023-03-10 thomas } else if (ie) {
1956 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1957 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1958 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1959 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1960 9f323212 2023-03-10 thomas ie->path);
1961 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1962 9f323212 2023-03-10 thomas errno == ENOENT))
1963 9f323212 2023-03-10 thomas goto done;
1964 9f323212 2023-03-10 thomas }
1965 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1966 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 9f323212 2023-03-10 thomas ie->path);
1968 9f323212 2023-03-10 thomas }
1969 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1970 5036ab18 2020-04-18 stsp }
1971 b8f41171 2019-02-10 stsp
1972 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1974 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1975 c6e8a826 2021-04-05 stsp /*
1976 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1977 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1978 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1979 c6e8a826 2021-04-05 stsp * updating contents of this file.
1980 c6e8a826 2021-04-05 stsp */
1981 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1982 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1984 c6e8a826 2021-04-05 stsp /* Same commit. */
1985 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1986 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1987 e2b1e152 2019-07-13 stsp if (err)
1988 e2b1e152 2019-07-13 stsp goto done;
1989 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 b8f41171 2019-02-10 stsp path);
1991 6353ad76 2019-02-08 stsp goto done;
1992 a378724f 2019-02-10 stsp }
1993 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1994 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1995 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1996 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1997 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
1998 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
1999 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2000 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2001 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2002 b36a9774 2023-04-14 thomas }
2003 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2004 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2005 0f58026f 2021-04-05 stsp if (err)
2006 0f58026f 2021-04-05 stsp goto done;
2007 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2008 0f58026f 2021-04-05 stsp path);
2009 b8f41171 2019-02-10 stsp goto done;
2010 e2b1e152 2019-07-13 stsp }
2011 9d31a1d8 2018-03-11 stsp }
2012 9d31a1d8 2018-03-11 stsp
2013 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2014 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2015 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2016 f4ae6ddb 2022-07-01 thomas goto done;
2017 f4ae6ddb 2022-07-01 thomas }
2018 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2019 8da9e5f4 2019-01-12 stsp if (err)
2020 6353ad76 2019-02-08 stsp goto done;
2021 512f0d0e 2019-01-02 stsp
2022 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2023 234035bc 2019-06-01 stsp int update_timestamps;
2024 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2025 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2026 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2027 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2028 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2029 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2030 f4ae6ddb 2022-07-01 thomas goto done;
2031 f4ae6ddb 2022-07-01 thomas }
2032 234035bc 2019-06-01 stsp struct got_object_id id2;
2033 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2034 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2035 f4ae6ddb 2022-07-01 thomas fd2);
2036 234035bc 2019-06-01 stsp if (err)
2037 f69721c3 2019-10-21 stsp goto done;
2038 f69721c3 2019-10-21 stsp }
2039 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2040 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2041 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2042 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2043 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2044 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2045 f69721c3 2019-10-21 stsp goto done;
2046 f69721c3 2019-10-21 stsp }
2047 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2049 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2050 234035bc 2019-06-01 stsp goto done;
2051 f69721c3 2019-10-21 stsp }
2052 234035bc 2019-06-01 stsp }
2053 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2054 36bf999c 2020-07-23 stsp char *link_target;
2055 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2056 36bf999c 2020-07-23 stsp if (err)
2057 36bf999c 2020-07-23 stsp goto done;
2058 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2059 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2060 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2061 36bf999c 2020-07-23 stsp free(link_target);
2062 993e2a1b 2020-07-23 stsp } else {
2063 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2064 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2065 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2066 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2067 993e2a1b 2020-07-23 stsp }
2068 f69721c3 2019-10-21 stsp free(label_orig);
2069 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2070 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2071 f4ae6ddb 2022-07-01 thomas goto done;
2072 f4ae6ddb 2022-07-01 thomas }
2073 234035bc 2019-06-01 stsp if (blob2)
2074 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2075 909d120e 2019-09-22 stsp if (err)
2076 909d120e 2019-09-22 stsp goto done;
2077 234035bc 2019-06-01 stsp /*
2078 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2079 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2080 234035bc 2019-06-01 stsp * unmodified files again.
2081 234035bc 2019-06-01 stsp */
2082 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2083 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2084 234035bc 2019-06-01 stsp update_timestamps);
2085 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2089 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2090 1ee397ad 2019-07-12 stsp if (err)
2091 1ee397ad 2019-07-12 stsp goto done;
2092 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2093 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2094 13d9040b 2019-03-27 stsp if (err)
2095 13d9040b 2019-03-27 stsp goto done;
2096 13d9040b 2019-03-27 stsp } else {
2097 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2098 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2099 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2100 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2101 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2102 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2103 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2104 2e1fa222 2020-07-23 stsp } else {
2105 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2106 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2107 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2108 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2109 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2110 2e1fa222 2020-07-23 stsp }
2111 13d9040b 2019-03-27 stsp if (err)
2112 13d9040b 2019-03-27 stsp goto done;
2113 65b05cec 2020-07-23 stsp
2114 054041d0 2020-06-24 stsp if (ie) {
2115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2116 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2117 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2118 054041d0 2020-06-24 stsp } else {
2119 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2120 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2121 054041d0 2020-06-24 stsp &blob->id);
2122 054041d0 2020-06-24 stsp }
2123 13d9040b 2019-03-27 stsp if (err)
2124 13d9040b 2019-03-27 stsp goto done;
2125 65b05cec 2020-07-23 stsp
2126 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2127 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2128 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2129 2e1fa222 2020-07-23 stsp }
2130 13d9040b 2019-03-27 stsp }
2131 f4ae6ddb 2022-07-01 thomas
2132 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2133 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2134 f4ae6ddb 2022-07-01 thomas goto done;
2135 f4ae6ddb 2022-07-01 thomas }
2136 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2137 6353ad76 2019-02-08 stsp done:
2138 6353ad76 2019-02-08 stsp free(ondisk_path);
2139 8da9e5f4 2019-01-12 stsp return err;
2140 90285c3b 2019-01-08 stsp }
2141 90285c3b 2019-01-08 stsp
2142 90285c3b 2019-01-08 stsp static const struct got_error *
2143 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2144 90285c3b 2019-01-08 stsp {
2145 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2146 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2147 90285c3b 2019-01-08 stsp
2148 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2149 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2150 90285c3b 2019-01-08 stsp
2151 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2152 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2153 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2154 c97665ae 2019-01-13 stsp } else {
2155 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2156 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2157 1c4cdd89 2021-06-20 stsp if (err)
2158 1c4cdd89 2021-06-20 stsp goto done;
2159 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2160 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2161 fddefe3b 2020-10-20 stsp free(ondisk_path);
2162 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2163 1c4cdd89 2021-06-20 stsp parent = NULL;
2164 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2165 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2166 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2167 fddefe3b 2020-10-20 stsp ondisk_path);
2168 90285c3b 2019-01-08 stsp break;
2169 90285c3b 2019-01-08 stsp }
2170 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2171 1c4cdd89 2021-06-20 stsp if (err)
2172 1c4cdd89 2021-06-20 stsp break;
2173 1c4cdd89 2021-06-20 stsp }
2174 90285c3b 2019-01-08 stsp }
2175 1c4cdd89 2021-06-20 stsp done:
2176 90285c3b 2019-01-08 stsp free(ondisk_path);
2177 1c4cdd89 2021-06-20 stsp free(parent);
2178 90285c3b 2019-01-08 stsp return err;
2179 512f0d0e 2019-01-02 stsp }
2180 512f0d0e 2019-01-02 stsp
2181 708d8e67 2019-03-27 stsp static const struct got_error *
2182 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2183 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2184 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2185 708d8e67 2019-03-27 stsp {
2186 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2187 708d8e67 2019-03-27 stsp unsigned char status;
2188 708d8e67 2019-03-27 stsp struct stat sb;
2189 708d8e67 2019-03-27 stsp char *ondisk_path;
2190 708d8e67 2019-03-27 stsp
2191 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2192 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2193 a76c42e6 2019-08-03 stsp
2194 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2195 708d8e67 2019-03-27 stsp == -1)
2196 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2197 708d8e67 2019-03-27 stsp
2198 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2199 708d8e67 2019-03-27 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 708d8e67 2019-03-27 stsp
2202 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2203 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2204 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2205 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2206 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2207 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2208 993e2a1b 2020-07-23 stsp goto done;
2209 993e2a1b 2020-07-23 stsp }
2210 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2211 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2212 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2213 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2214 993e2a1b 2020-07-23 stsp if (err)
2215 993e2a1b 2020-07-23 stsp goto done;
2216 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2217 993e2a1b 2020-07-23 stsp ie->path);
2218 993e2a1b 2020-07-23 stsp goto done;
2219 993e2a1b 2020-07-23 stsp }
2220 993e2a1b 2020-07-23 stsp
2221 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2222 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2223 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2224 1ee397ad 2019-07-12 stsp if (err)
2225 5a58a424 2020-06-23 stsp goto done;
2226 fc6346c4 2019-03-27 stsp /*
2227 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2228 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2229 fc6346c4 2019-03-27 stsp */
2230 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2231 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2232 fc6346c4 2019-03-27 stsp } else {
2233 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2234 1ee397ad 2019-07-12 stsp if (err)
2235 5a58a424 2020-06-23 stsp goto done;
2236 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2237 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2238 fc6346c4 2019-03-27 stsp if (err)
2239 5a58a424 2020-06-23 stsp goto done;
2240 fc6346c4 2019-03-27 stsp }
2241 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2242 708d8e67 2019-03-27 stsp }
2243 5a58a424 2020-06-23 stsp done:
2244 5a58a424 2020-06-23 stsp free(ondisk_path);
2245 fc6346c4 2019-03-27 stsp return err;
2246 708d8e67 2019-03-27 stsp }
2247 708d8e67 2019-03-27 stsp
2248 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2249 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2250 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2251 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2252 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2253 8da9e5f4 2019-01-12 stsp void *progress_arg;
2254 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2255 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2256 8da9e5f4 2019-01-12 stsp };
2257 8da9e5f4 2019-01-12 stsp
2258 512f0d0e 2019-01-02 stsp static const struct got_error *
2259 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2260 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2261 512f0d0e 2019-01-02 stsp {
2262 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2263 0584f854 2019-04-06 stsp
2264 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2266 512f0d0e 2019-01-02 stsp
2267 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2268 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2269 8da9e5f4 2019-01-12 stsp }
2270 512f0d0e 2019-01-02 stsp
2271 8da9e5f4 2019-01-12 stsp static const struct got_error *
2272 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2273 8da9e5f4 2019-01-12 stsp {
2274 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2275 7a9df742 2019-01-08 stsp
2276 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2277 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2278 0584f854 2019-04-06 stsp
2279 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2280 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2281 9d31a1d8 2018-03-11 stsp }
2282 9d31a1d8 2018-03-11 stsp
2283 9d31a1d8 2018-03-11 stsp static const struct got_error *
2284 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2285 9d31a1d8 2018-03-11 stsp {
2286 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2287 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2288 8da9e5f4 2019-01-12 stsp char *path;
2289 9d31a1d8 2018-03-11 stsp
2290 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2292 0584f854 2019-04-06 stsp
2293 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2294 63c5ca5d 2019-08-24 stsp return NULL;
2295 63c5ca5d 2019-08-24 stsp
2296 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2297 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2298 8da9e5f4 2019-01-12 stsp == -1)
2299 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2300 9d31a1d8 2018-03-11 stsp
2301 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2302 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2303 8da9e5f4 2019-01-12 stsp else
2304 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2305 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2306 9d31a1d8 2018-03-11 stsp
2307 8da9e5f4 2019-01-12 stsp free(path);
2308 0cd1c46a 2019-03-11 stsp return err;
2309 0cd1c46a 2019-03-11 stsp }
2310 0cd1c46a 2019-03-11 stsp
2311 b2118c49 2020-07-28 stsp const struct got_error *
2312 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2313 b2118c49 2020-07-28 stsp {
2314 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2315 b2118c49 2020-07-28 stsp
2316 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2317 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2318 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2319 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2320 b2118c49 2020-07-28 stsp }
2321 b2118c49 2020-07-28 stsp
2322 b2118c49 2020-07-28 stsp return NULL;
2323 b2118c49 2020-07-28 stsp }
2324 b2118c49 2020-07-28 stsp
2325 818c7501 2019-07-11 stsp static const struct got_error *
2326 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2327 0cd1c46a 2019-03-11 stsp {
2328 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2329 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2330 0cd1c46a 2019-03-11 stsp
2331 517bab73 2019-03-11 stsp *refname = NULL;
2332 517bab73 2019-03-11 stsp
2333 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2334 b2118c49 2020-07-28 stsp if (err)
2335 b2118c49 2020-07-28 stsp return err;
2336 0cd1c46a 2019-03-11 stsp
2337 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2338 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2339 0647c563 2019-03-11 stsp *refname = NULL;
2340 0cd1c46a 2019-03-11 stsp }
2341 517bab73 2019-03-11 stsp free(uuidstr);
2342 517bab73 2019-03-11 stsp return err;
2343 517bab73 2019-03-11 stsp }
2344 517bab73 2019-03-11 stsp
2345 818c7501 2019-07-11 stsp const struct got_error *
2346 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2347 f6cd0243 2023-01-28 thomas const char *prefix)
2348 f6cd0243 2023-01-28 thomas {
2349 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2350 f6cd0243 2023-01-28 thomas }
2351 f6cd0243 2023-01-28 thomas
2352 f6cd0243 2023-01-28 thomas const struct got_error *
2353 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2354 818c7501 2019-07-11 stsp {
2355 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2356 818c7501 2019-07-11 stsp }
2357 818c7501 2019-07-11 stsp
2358 818c7501 2019-07-11 stsp static const struct got_error *
2359 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2360 818c7501 2019-07-11 stsp {
2361 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2362 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2363 818c7501 2019-07-11 stsp }
2364 818c7501 2019-07-11 stsp
2365 818c7501 2019-07-11 stsp static const struct got_error *
2366 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2367 818c7501 2019-07-11 stsp {
2368 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2369 818c7501 2019-07-11 stsp }
2370 818c7501 2019-07-11 stsp
2371 818c7501 2019-07-11 stsp static const struct got_error *
2372 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2373 818c7501 2019-07-11 stsp {
2374 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2375 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2376 818c7501 2019-07-11 stsp }
2377 818c7501 2019-07-11 stsp
2378 818c7501 2019-07-11 stsp static const struct got_error *
2379 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 818c7501 2019-07-11 stsp {
2381 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2382 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2383 0ebf8283 2019-07-24 stsp }
2384 0ebf8283 2019-07-24 stsp
2385 0ebf8283 2019-07-24 stsp static const struct got_error *
2386 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2387 0ebf8283 2019-07-24 stsp {
2388 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2389 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2390 0ebf8283 2019-07-24 stsp }
2391 0ebf8283 2019-07-24 stsp
2392 0ebf8283 2019-07-24 stsp static const struct got_error *
2393 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2394 0ebf8283 2019-07-24 stsp {
2395 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2396 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2397 0ebf8283 2019-07-24 stsp }
2398 0ebf8283 2019-07-24 stsp
2399 0ebf8283 2019-07-24 stsp static const struct got_error *
2400 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2401 0ebf8283 2019-07-24 stsp {
2402 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2403 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2404 818c7501 2019-07-11 stsp }
2405 818c7501 2019-07-11 stsp
2406 0ebf8283 2019-07-24 stsp static const struct got_error *
2407 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2408 0ebf8283 2019-07-24 stsp {
2409 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2410 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2411 0ebf8283 2019-07-24 stsp }
2412 818c7501 2019-07-11 stsp
2413 0ebf8283 2019-07-24 stsp const struct got_error *
2414 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2415 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2416 0ebf8283 2019-07-24 stsp {
2417 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2418 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2419 0ebf8283 2019-07-24 stsp *path = NULL;
2420 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2421 0ebf8283 2019-07-24 stsp }
2422 0ebf8283 2019-07-24 stsp return NULL;
2423 10604dce 2021-09-24 thomas }
2424 10604dce 2021-09-24 thomas
2425 10604dce 2021-09-24 thomas static const struct got_error *
2426 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2427 10604dce 2021-09-24 thomas {
2428 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2429 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2430 0ebf8283 2019-07-24 stsp }
2431 0ebf8283 2019-07-24 stsp
2432 10604dce 2021-09-24 thomas static const struct got_error *
2433 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 10604dce 2021-09-24 thomas {
2435 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2436 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2437 10604dce 2021-09-24 thomas }
2438 10604dce 2021-09-24 thomas
2439 517bab73 2019-03-11 stsp /*
2440 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2441 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2442 517bab73 2019-03-11 stsp */
2443 517bab73 2019-03-11 stsp static const struct got_error *
2444 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2445 517bab73 2019-03-11 stsp {
2446 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2447 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2448 517bab73 2019-03-11 stsp char *refname;
2449 517bab73 2019-03-11 stsp
2450 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2451 517bab73 2019-03-11 stsp if (err)
2452 517bab73 2019-03-11 stsp return err;
2453 0cd1c46a 2019-03-11 stsp
2454 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2455 0cd1c46a 2019-03-11 stsp if (err)
2456 0cd1c46a 2019-03-11 stsp goto done;
2457 0cd1c46a 2019-03-11 stsp
2458 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2459 0cd1c46a 2019-03-11 stsp done:
2460 0cd1c46a 2019-03-11 stsp free(refname);
2461 0cd1c46a 2019-03-11 stsp if (ref)
2462 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2463 3e3a69f1 2019-07-25 stsp return err;
2464 3e3a69f1 2019-07-25 stsp }
2465 3e3a69f1 2019-07-25 stsp
2466 3e3a69f1 2019-07-25 stsp static const struct got_error *
2467 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2468 3e3a69f1 2019-07-25 stsp {
2469 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2470 3e3a69f1 2019-07-25 stsp
2471 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2472 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2473 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2474 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2475 3e3a69f1 2019-07-25 stsp }
2476 9d31a1d8 2018-03-11 stsp return err;
2477 9d31a1d8 2018-03-11 stsp }
2478 9d31a1d8 2018-03-11 stsp
2479 3e3a69f1 2019-07-25 stsp
2480 ebf99748 2019-05-09 stsp static const struct got_error *
2481 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2482 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2483 ebf99748 2019-05-09 stsp {
2484 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2485 ebf99748 2019-05-09 stsp FILE *index = NULL;
2486 0cd1c46a 2019-03-11 stsp
2487 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2488 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2489 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2490 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2491 ebf99748 2019-05-09 stsp
2492 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2493 3e3a69f1 2019-07-25 stsp if (err)
2494 ebf99748 2019-05-09 stsp goto done;
2495 ebf99748 2019-05-09 stsp
2496 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2497 ebf99748 2019-05-09 stsp if (index == NULL) {
2498 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2499 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2500 ebf99748 2019-05-09 stsp } else {
2501 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2502 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2503 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2504 ebf99748 2019-05-09 stsp }
2505 ebf99748 2019-05-09 stsp done:
2506 ebf99748 2019-05-09 stsp if (err) {
2507 ebf99748 2019-05-09 stsp free(*fileindex_path);
2508 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2509 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2510 ebf99748 2019-05-09 stsp *fileindex = NULL;
2511 ebf99748 2019-05-09 stsp }
2512 ebf99748 2019-05-09 stsp return err;
2513 ebf99748 2019-05-09 stsp }
2514 c932eeeb 2019-05-22 stsp
2515 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2516 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2517 c932eeeb 2019-05-22 stsp const char *path;
2518 c932eeeb 2019-05-22 stsp size_t path_len;
2519 c932eeeb 2019-05-22 stsp const char *entry_name;
2520 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2521 a484d721 2019-06-10 stsp void *progress_arg;
2522 c932eeeb 2019-05-22 stsp };
2523 ebf99748 2019-05-09 stsp
2524 c932eeeb 2019-05-22 stsp static const struct got_error *
2525 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2526 c932eeeb 2019-05-22 stsp {
2527 1ee397ad 2019-07-12 stsp const struct got_error *err;
2528 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2529 c932eeeb 2019-05-22 stsp
2530 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2531 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2532 c932eeeb 2019-05-22 stsp return NULL;
2533 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2534 102ff934 2019-06-11 stsp return NULL;
2535 102ff934 2019-06-11 stsp
2536 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2537 a769b60b 2021-06-27 stsp return NULL;
2538 a769b60b 2021-06-27 stsp
2539 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2540 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2541 c932eeeb 2019-05-22 stsp return NULL;
2542 c932eeeb 2019-05-22 stsp
2543 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2544 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2545 edd02c5e 2019-07-11 stsp ie->path);
2546 1ee397ad 2019-07-12 stsp if (err)
2547 1ee397ad 2019-07-12 stsp return err;
2548 1ee397ad 2019-07-12 stsp }
2549 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2550 c932eeeb 2019-05-22 stsp return NULL;
2551 a615e0e7 2020-12-16 stsp }
2552 a615e0e7 2020-12-16 stsp
2553 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2554 a615e0e7 2020-12-16 stsp static const struct got_error *
2555 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2556 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2557 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2558 a615e0e7 2020-12-16 stsp {
2559 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2560 a615e0e7 2020-12-16 stsp
2561 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2562 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2563 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2564 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2565 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2566 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2567 a615e0e7 2020-12-16 stsp
2568 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2569 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2570 9c6338c4 2019-06-02 stsp }
2571 9c6338c4 2019-06-02 stsp
2572 9c6338c4 2019-06-02 stsp static const struct got_error *
2573 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2574 9c6338c4 2019-06-02 stsp {
2575 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2576 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2577 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2578 867630bb 2020-01-17 stsp struct timespec timeout;
2579 9c6338c4 2019-06-02 stsp
2580 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2581 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2582 9c6338c4 2019-06-02 stsp if (err)
2583 9c6338c4 2019-06-02 stsp goto done;
2584 9c6338c4 2019-06-02 stsp
2585 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2586 9c6338c4 2019-06-02 stsp if (err)
2587 9c6338c4 2019-06-02 stsp goto done;
2588 9c6338c4 2019-06-02 stsp
2589 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2590 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2591 9c6338c4 2019-06-02 stsp fileindex_path);
2592 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2593 9c6338c4 2019-06-02 stsp }
2594 867630bb 2020-01-17 stsp
2595 867630bb 2020-01-17 stsp /*
2596 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2597 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2598 867630bb 2020-01-17 stsp * was recorded in the file index.
2599 867630bb 2020-01-17 stsp */
2600 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2601 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2602 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2603 9c6338c4 2019-06-02 stsp done:
2604 9c6338c4 2019-06-02 stsp if (new_index)
2605 9c6338c4 2019-06-02 stsp fclose(new_index);
2606 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2607 9c6338c4 2019-06-02 stsp return err;
2608 c932eeeb 2019-05-22 stsp }
2609 6ced4176 2019-07-12 stsp
2610 6ced4176 2019-07-12 stsp static const struct got_error *
2611 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2612 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2613 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2614 945f9229 2022-04-16 thomas struct got_repository *repo)
2615 6ced4176 2019-07-12 stsp {
2616 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2617 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2618 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2619 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2620 6ced4176 2019-07-12 stsp
2621 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2622 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2623 6ced4176 2019-07-12 stsp *tree_id = NULL;
2624 6ced4176 2019-07-12 stsp
2625 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2626 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2627 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2628 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2629 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2630 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2631 6ced4176 2019-07-12 stsp goto done;
2632 6ced4176 2019-07-12 stsp }
2633 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2634 945f9229 2022-04-16 thomas worktree->path_prefix);
2635 6ced4176 2019-07-12 stsp if (err)
2636 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp return NULL;
2638 6ced4176 2019-07-12 stsp }
2639 6ced4176 2019-07-12 stsp
2640 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2641 6ced4176 2019-07-12 stsp
2642 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2643 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2644 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2645 6ced4176 2019-07-12 stsp goto done;
2646 6ced4176 2019-07-12 stsp }
2647 6ced4176 2019-07-12 stsp
2648 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2649 6ced4176 2019-07-12 stsp if (err)
2650 6ced4176 2019-07-12 stsp goto done;
2651 6ced4176 2019-07-12 stsp
2652 6ced4176 2019-07-12 stsp free(in_repo_path);
2653 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2654 6ced4176 2019-07-12 stsp
2655 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2656 6ced4176 2019-07-12 stsp if (err)
2657 6ced4176 2019-07-12 stsp goto done;
2658 c932eeeb 2019-05-22 stsp
2659 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2660 6ced4176 2019-07-12 stsp /* Check out a single file. */
2661 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2662 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2663 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2664 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2665 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2666 6ced4176 2019-07-12 stsp goto done;
2667 6ced4176 2019-07-12 stsp }
2668 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2669 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2670 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2671 6ced4176 2019-07-12 stsp goto done;
2672 6ced4176 2019-07-12 stsp }
2673 6ced4176 2019-07-12 stsp } else {
2674 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2675 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2676 6ced4176 2019-07-12 stsp if (err)
2677 6ced4176 2019-07-12 stsp return err;
2678 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2679 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2680 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2681 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2682 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp }
2684 6ced4176 2019-07-12 stsp }
2685 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2686 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2687 6ced4176 2019-07-12 stsp } else {
2688 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2689 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2690 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2691 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2692 6ced4176 2019-07-12 stsp goto done;
2693 6ced4176 2019-07-12 stsp }
2694 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2695 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2696 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2697 6ced4176 2019-07-12 stsp goto done;
2698 6ced4176 2019-07-12 stsp }
2699 6ced4176 2019-07-12 stsp }
2700 6ced4176 2019-07-12 stsp done:
2701 6ced4176 2019-07-12 stsp free(id);
2702 6ced4176 2019-07-12 stsp free(in_repo_path);
2703 6ced4176 2019-07-12 stsp if (err) {
2704 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2705 6ced4176 2019-07-12 stsp free(*tree_relpath);
2706 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2707 6ced4176 2019-07-12 stsp free(*tree_id);
2708 6ced4176 2019-07-12 stsp *tree_id = NULL;
2709 6ced4176 2019-07-12 stsp }
2710 07ed472d 2019-07-12 stsp return err;
2711 07ed472d 2019-07-12 stsp }
2712 07ed472d 2019-07-12 stsp
2713 07ed472d 2019-07-12 stsp static const struct got_error *
2714 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2715 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2716 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2717 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2718 07ed472d 2019-07-12 stsp {
2719 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2720 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2721 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2722 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2723 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2724 07ed472d 2019-07-12 stsp
2725 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2726 7f47418f 2019-12-20 stsp if (err) {
2727 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2728 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2729 7f47418f 2019-12-20 stsp goto done;
2730 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2731 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2732 7f47418f 2019-12-20 stsp if (err)
2733 7f47418f 2019-12-20 stsp return err;
2734 7f47418f 2019-12-20 stsp }
2735 07ed472d 2019-07-12 stsp
2736 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2737 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2738 07ed472d 2019-07-12 stsp if (err)
2739 07ed472d 2019-07-12 stsp goto done;
2740 07ed472d 2019-07-12 stsp
2741 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2742 07ed472d 2019-07-12 stsp if (err)
2743 07ed472d 2019-07-12 stsp goto done;
2744 07ed472d 2019-07-12 stsp
2745 07ed472d 2019-07-12 stsp if (entry_name &&
2746 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2747 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2748 07ed472d 2019-07-12 stsp goto done;
2749 07ed472d 2019-07-12 stsp }
2750 07ed472d 2019-07-12 stsp
2751 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2752 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2753 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2754 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2755 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2756 07ed472d 2019-07-12 stsp arg.repo = repo;
2757 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2758 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2759 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2760 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2761 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2762 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2763 07ed472d 2019-07-12 stsp done:
2764 07ed472d 2019-07-12 stsp if (tree)
2765 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2766 07ed472d 2019-07-12 stsp if (commit)
2767 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2768 6ced4176 2019-07-12 stsp return err;
2769 6ced4176 2019-07-12 stsp }
2770 6ced4176 2019-07-12 stsp
2771 9d31a1d8 2018-03-11 stsp const struct got_error *
2772 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2773 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2774 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2775 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2776 9d31a1d8 2018-03-11 stsp {
2777 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2778 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2779 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2780 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2781 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2782 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2783 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2784 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2785 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2786 f2ea84fa 2019-07-27 stsp int entry_type;
2787 f2ea84fa 2019-07-27 stsp char *relpath;
2788 f2ea84fa 2019-07-27 stsp char *entry_name;
2789 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2790 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2791 9d31a1d8 2018-03-11 stsp
2792 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2793 f2ea84fa 2019-07-27 stsp
2794 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2795 9d31a1d8 2018-03-11 stsp if (err)
2796 9d31a1d8 2018-03-11 stsp return err;
2797 945f9229 2022-04-16 thomas
2798 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2799 945f9229 2022-04-16 thomas worktree->base_commit_id);
2800 945f9229 2022-04-16 thomas if (err)
2801 945f9229 2022-04-16 thomas goto done;
2802 93a30277 2018-12-24 stsp
2803 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2804 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2805 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2806 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2807 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2808 f2ea84fa 2019-07-27 stsp goto done;
2809 f2ea84fa 2019-07-27 stsp }
2810 6ced4176 2019-07-12 stsp
2811 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2812 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2813 945f9229 2022-04-16 thomas worktree, repo);
2814 f2ea84fa 2019-07-27 stsp if (err) {
2815 f2ea84fa 2019-07-27 stsp free(tpd);
2816 6ced4176 2019-07-12 stsp goto done;
2817 6ced4176 2019-07-12 stsp }
2818 f2ea84fa 2019-07-27 stsp
2819 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2820 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2821 f2ea84fa 2019-07-27 stsp if (err) {
2822 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2823 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2824 f2ea84fa 2019-07-27 stsp free(tpd);
2825 f2ea84fa 2019-07-27 stsp goto done;
2826 f2ea84fa 2019-07-27 stsp }
2827 f2ea84fa 2019-07-27 stsp } else
2828 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2829 f2ea84fa 2019-07-27 stsp
2830 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2831 6ced4176 2019-07-12 stsp }
2832 6ced4176 2019-07-12 stsp
2833 51514078 2018-12-25 stsp /*
2834 51514078 2018-12-25 stsp * Read the file index.
2835 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2836 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2837 51514078 2018-12-25 stsp */
2838 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2839 8da9e5f4 2019-01-12 stsp if (err)
2840 c4cdcb68 2019-04-03 stsp goto done;
2841 c4cdcb68 2019-04-03 stsp
2842 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2843 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2844 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2845 f2ea84fa 2019-07-27 stsp
2846 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2847 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2848 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 f2ea84fa 2019-07-27 stsp if (err)
2850 f2ea84fa 2019-07-27 stsp break;
2851 f2ea84fa 2019-07-27 stsp
2852 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2853 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2854 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2855 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2856 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2857 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2858 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2859 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2860 f2ea84fa 2019-07-27 stsp if (err)
2861 f2ea84fa 2019-07-27 stsp break;
2862 f2ea84fa 2019-07-27 stsp
2863 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2864 711d9cd9 2019-07-12 stsp }
2865 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2866 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2867 af12c6b9 2019-06-04 stsp err = sync_err;
2868 9d31a1d8 2018-03-11 stsp done:
2869 9c6338c4 2019-06-02 stsp free(fileindex_path);
2870 edfa7d7f 2018-09-11 stsp if (tree)
2871 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2872 9d31a1d8 2018-03-11 stsp if (commit)
2873 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2874 5ade8233 2019-07-12 stsp if (fileindex)
2875 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2876 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2877 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2878 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2879 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2880 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2881 f2ea84fa 2019-07-27 stsp free(tpd);
2882 f2ea84fa 2019-07-27 stsp }
2883 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2884 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2885 9d31a1d8 2018-03-11 stsp err = unlockerr;
2886 f8d1f275 2019-02-04 stsp return err;
2887 f8d1f275 2019-02-04 stsp }
2888 f8d1f275 2019-02-04 stsp
2889 0529f8df 2023-03-10 thomas static const struct got_error *
2890 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2891 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2892 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2893 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2894 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2895 0529f8df 2023-03-10 thomas struct got_repository *repo,
2896 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2897 0529f8df 2023-03-10 thomas {
2898 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2899 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2900 0529f8df 2023-03-10 thomas
2901 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2902 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2903 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2904 0529f8df 2023-03-10 thomas restoring_missing_file,
2905 0529f8df 2023-03-10 thomas reverting_versioned_file,
2906 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2907 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2908 0529f8df 2023-03-10 thomas } else {
2909 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2910 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2911 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2912 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2913 0529f8df 2023-03-10 thomas }
2914 0529f8df 2023-03-10 thomas if (err)
2915 0529f8df 2023-03-10 thomas return err;
2916 0529f8df 2023-03-10 thomas if (ie == NULL) {
2917 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2918 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2919 0529f8df 2023-03-10 thomas if (err)
2920 0529f8df 2023-03-10 thomas return err;
2921 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2922 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2923 0529f8df 2023-03-10 thomas if (err) {
2924 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2925 0529f8df 2023-03-10 thomas return err;
2926 0529f8df 2023-03-10 thomas }
2927 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2928 0529f8df 2023-03-10 thomas if (err) {
2929 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2930 0529f8df 2023-03-10 thomas return err;
2931 0529f8df 2023-03-10 thomas }
2932 0529f8df 2023-03-10 thomas } else {
2933 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2934 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2935 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2936 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2937 0529f8df 2023-03-10 thomas if (err)
2938 0529f8df 2023-03-10 thomas return err;
2939 0529f8df 2023-03-10 thomas }
2940 0529f8df 2023-03-10 thomas
2941 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2942 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2943 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2944 0529f8df 2023-03-10 thomas }
2945 0529f8df 2023-03-10 thomas
2946 0529f8df 2023-03-10 thomas return NULL;
2947 0529f8df 2023-03-10 thomas }
2948 0529f8df 2023-03-10 thomas
2949 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2950 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2951 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2952 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2953 234035bc 2019-06-01 stsp void *progress_arg;
2954 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2955 234035bc 2019-06-01 stsp void *cancel_arg;
2956 f69721c3 2019-10-21 stsp const char *label_orig;
2957 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2958 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2959 234035bc 2019-06-01 stsp };
2960 234035bc 2019-06-01 stsp
2961 234035bc 2019-06-01 stsp static const struct got_error *
2962 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2963 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2964 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2965 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2966 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2967 234035bc 2019-06-01 stsp {
2968 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2969 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2970 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2971 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2972 234035bc 2019-06-01 stsp struct stat sb;
2973 234035bc 2019-06-01 stsp unsigned char status;
2974 234035bc 2019-06-01 stsp int local_changes_subsumed;
2975 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2976 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2977 234035bc 2019-06-01 stsp
2978 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2979 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2980 d6c87207 2019-08-02 stsp strlen(path2));
2981 1ee397ad 2019-07-12 stsp if (ie == NULL)
2982 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2983 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2984 234035bc 2019-06-01 stsp
2985 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 234035bc 2019-06-01 stsp path2) == -1)
2987 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2988 234035bc 2019-06-01 stsp
2989 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2990 7f91a133 2019-12-13 stsp repo);
2991 234035bc 2019-06-01 stsp if (err)
2992 234035bc 2019-06-01 stsp goto done;
2993 234035bc 2019-06-01 stsp
2994 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2995 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2996 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2997 234035bc 2019-06-01 stsp goto done;
2998 234035bc 2019-06-01 stsp }
2999 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3000 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3001 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3002 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3003 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3004 234035bc 2019-06-01 stsp goto done;
3005 234035bc 2019-06-01 stsp }
3006 234035bc 2019-06-01 stsp
3007 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3008 36bf999c 2020-07-23 stsp char *link_target2;
3009 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3010 36bf999c 2020-07-23 stsp if (err)
3011 36bf999c 2020-07-23 stsp goto done;
3012 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3013 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3014 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3015 36bf999c 2020-07-23 stsp free(link_target2);
3016 af57b12a 2020-07-23 stsp } else {
3017 1af628f4 2021-06-03 stsp int fd;
3018 1af628f4 2021-06-03 stsp
3019 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3020 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3021 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3022 1af628f4 2021-06-03 stsp goto done;
3023 1af628f4 2021-06-03 stsp }
3024 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3025 1af628f4 2021-06-03 stsp f_orig, blob1);
3026 1af628f4 2021-06-03 stsp if (err)
3027 1af628f4 2021-06-03 stsp goto done;
3028 1af628f4 2021-06-03 stsp
3029 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3030 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3031 1af628f4 2021-06-03 stsp goto done;
3032 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3033 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3034 1af628f4 2021-06-03 stsp if (err)
3035 1af628f4 2021-06-03 stsp goto done;
3036 1af628f4 2021-06-03 stsp
3037 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3038 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3039 1af628f4 2021-06-03 stsp if (fd == -1) {
3040 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3041 1af628f4 2021-06-03 stsp ondisk_path);
3042 1af628f4 2021-06-03 stsp goto done;
3043 1af628f4 2021-06-03 stsp }
3044 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3045 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3046 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3047 1af628f4 2021-06-03 stsp ondisk_path);
3048 1af628f4 2021-06-03 stsp close(fd);
3049 1af628f4 2021-06-03 stsp goto done;
3050 1af628f4 2021-06-03 stsp }
3051 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3052 1af628f4 2021-06-03 stsp if (err)
3053 1af628f4 2021-06-03 stsp goto done;
3054 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3055 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3056 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3057 1af628f4 2021-06-03 stsp goto done;
3058 1af628f4 2021-06-03 stsp }
3059 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3060 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3061 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3062 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3063 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3064 af57b12a 2020-07-23 stsp }
3065 234035bc 2019-06-01 stsp } else if (blob1) {
3066 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3067 d6c87207 2019-08-02 stsp strlen(path1));
3068 1ee397ad 2019-07-12 stsp if (ie == NULL)
3069 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3070 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3071 2b92fad7 2019-06-02 stsp
3072 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3073 2b92fad7 2019-06-02 stsp path1) == -1)
3074 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3075 2b92fad7 2019-06-02 stsp
3076 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3077 7f91a133 2019-12-13 stsp repo);
3078 2b92fad7 2019-06-02 stsp if (err)
3079 2b92fad7 2019-06-02 stsp goto done;
3080 2b92fad7 2019-06-02 stsp
3081 2b92fad7 2019-06-02 stsp switch (status) {
3082 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3083 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3084 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3085 1ee397ad 2019-07-12 stsp if (err)
3086 1ee397ad 2019-07-12 stsp goto done;
3087 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3088 2b92fad7 2019-06-02 stsp if (err)
3089 2b92fad7 2019-06-02 stsp goto done;
3090 2b92fad7 2019-06-02 stsp if (ie)
3091 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3092 2b92fad7 2019-06-02 stsp break;
3093 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3094 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3095 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3096 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3097 1ee397ad 2019-07-12 stsp if (err)
3098 1ee397ad 2019-07-12 stsp goto done;
3099 2b92fad7 2019-06-02 stsp if (ie)
3100 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3101 2b92fad7 2019-06-02 stsp break;
3102 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3103 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3104 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3105 e8f02263 2022-01-23 thomas off_t blob1_size;
3106 0a22ca1a 2020-09-23 stsp /*
3107 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3108 0a22ca1a 2020-09-23 stsp * exists in the repository.
3109 0a22ca1a 2020-09-23 stsp */
3110 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3111 e8f02263 2022-01-23 thomas &blob1_size, path1);
3112 0a22ca1a 2020-09-23 stsp if (err)
3113 0a22ca1a 2020-09-23 stsp goto done;
3114 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3115 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3116 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3117 0a22ca1a 2020-09-23 stsp if (err)
3118 0a22ca1a 2020-09-23 stsp goto done;
3119 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3120 0a22ca1a 2020-09-23 stsp path1);
3121 0a22ca1a 2020-09-23 stsp if (err)
3122 0a22ca1a 2020-09-23 stsp goto done;
3123 0a22ca1a 2020-09-23 stsp if (ie)
3124 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3125 0a22ca1a 2020-09-23 stsp ie);
3126 0a22ca1a 2020-09-23 stsp } else {
3127 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3128 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3129 0a22ca1a 2020-09-23 stsp }
3130 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3131 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3132 0a22ca1a 2020-09-23 stsp free(id);
3133 0a22ca1a 2020-09-23 stsp if (err)
3134 0a22ca1a 2020-09-23 stsp goto done;
3135 0a22ca1a 2020-09-23 stsp break;
3136 0a22ca1a 2020-09-23 stsp }
3137 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3138 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3139 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3140 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3141 1ee397ad 2019-07-12 stsp if (err)
3142 1ee397ad 2019-07-12 stsp goto done;
3143 2b92fad7 2019-06-02 stsp break;
3144 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3145 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3146 1ee397ad 2019-07-12 stsp if (err)
3147 1ee397ad 2019-07-12 stsp goto done;
3148 2b92fad7 2019-06-02 stsp break;
3149 2b92fad7 2019-06-02 stsp default:
3150 2b92fad7 2019-06-02 stsp break;
3151 2b92fad7 2019-06-02 stsp }
3152 234035bc 2019-06-01 stsp } else if (blob2) {
3153 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3154 234035bc 2019-06-01 stsp path2) == -1)
3155 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3156 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3157 d6c87207 2019-08-02 stsp strlen(path2));
3158 234035bc 2019-06-01 stsp if (ie) {
3159 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3160 7f91a133 2019-12-13 stsp -1, NULL, repo);
3161 234035bc 2019-06-01 stsp if (err)
3162 234035bc 2019-06-01 stsp goto done;
3163 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3164 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3165 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3166 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3167 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3168 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3169 1ee397ad 2019-07-12 stsp status, path2);
3170 234035bc 2019-06-01 stsp goto done;
3171 234035bc 2019-06-01 stsp }
3172 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3173 36bf999c 2020-07-23 stsp char *link_target2;
3174 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3175 36bf999c 2020-07-23 stsp blob2);
3176 36bf999c 2020-07-23 stsp if (err)
3177 36bf999c 2020-07-23 stsp goto done;
3178 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3179 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3180 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3181 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3182 36bf999c 2020-07-23 stsp free(link_target2);
3183 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3184 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3185 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3186 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3187 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3188 526a746f 2020-07-23 stsp a->progress_arg);
3189 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3190 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3191 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3192 526a746f 2020-07-23 stsp }
3193 dfe9fba0 2020-07-23 stsp if (err)
3194 dfe9fba0 2020-07-23 stsp goto done;
3195 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3196 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3197 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3198 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3199 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3200 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3201 234035bc 2019-06-01 stsp if (err)
3202 234035bc 2019-06-01 stsp goto done;
3203 234035bc 2019-06-01 stsp }
3204 234035bc 2019-06-01 stsp } else {
3205 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3206 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3207 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3208 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3209 234035bc 2019-06-01 stsp if (err)
3210 2b92fad7 2019-06-02 stsp goto done;
3211 234035bc 2019-06-01 stsp }
3212 234035bc 2019-06-01 stsp }
3213 234035bc 2019-06-01 stsp done:
3214 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3215 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3216 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3217 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3218 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3219 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3220 1af628f4 2021-06-03 stsp free(id_str);
3221 54d5be07 2021-06-03 stsp free(label_deriv2);
3222 234035bc 2019-06-01 stsp free(ondisk_path);
3223 234035bc 2019-06-01 stsp return err;
3224 234035bc 2019-06-01 stsp }
3225 234035bc 2019-06-01 stsp
3226 69de9dd4 2021-09-03 stsp static const struct got_error *
3227 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3228 69de9dd4 2021-09-03 stsp {
3229 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3230 69de9dd4 2021-09-03 stsp
3231 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3232 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3233 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3234 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3235 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3236 69de9dd4 2021-09-03 stsp
3237 69de9dd4 2021-09-03 stsp return NULL;
3238 69de9dd4 2021-09-03 stsp }
3239 69de9dd4 2021-09-03 stsp
3240 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3241 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3242 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3243 234035bc 2019-06-01 stsp struct got_repository *repo;
3244 234035bc 2019-06-01 stsp };
3245 234035bc 2019-06-01 stsp
3246 234035bc 2019-06-01 stsp static const struct got_error *
3247 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3248 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3249 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3250 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3251 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3252 234035bc 2019-06-01 stsp {
3253 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3254 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3255 234035bc 2019-06-01 stsp unsigned char status;
3256 234035bc 2019-06-01 stsp struct stat sb;
3257 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3258 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3259 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3260 234035bc 2019-06-01 stsp char *ondisk_path;
3261 234035bc 2019-06-01 stsp
3262 69de9dd4 2021-09-03 stsp if (id == NULL)
3263 69de9dd4 2021-09-03 stsp return NULL;
3264 234035bc 2019-06-01 stsp
3265 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3266 69de9dd4 2021-09-03 stsp if (ie == NULL)
3267 69de9dd4 2021-09-03 stsp return NULL;
3268 69de9dd4 2021-09-03 stsp
3269 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3270 234035bc 2019-06-01 stsp == -1)
3271 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3272 234035bc 2019-06-01 stsp
3273 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3274 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3275 5546d466 2021-09-02 stsp free(ondisk_path);
3276 234035bc 2019-06-01 stsp if (err)
3277 234035bc 2019-06-01 stsp return err;
3278 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3279 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3280 234035bc 2019-06-01 stsp
3281 234035bc 2019-06-01 stsp return NULL;
3282 234035bc 2019-06-01 stsp }
3283 234035bc 2019-06-01 stsp
3284 818c7501 2019-07-11 stsp static const struct got_error *
3285 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3286 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3287 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3288 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3290 234035bc 2019-06-01 stsp {
3291 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3292 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3293 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3294 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3295 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3296 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3297 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3298 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3299 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3300 234035bc 2019-06-01 stsp
3301 03415a1a 2019-06-02 stsp if (commit_id1) {
3302 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3303 945f9229 2022-04-16 thomas if (err)
3304 945f9229 2022-04-16 thomas goto done;
3305 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3306 03415a1a 2019-06-02 stsp worktree->path_prefix);
3307 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3308 03415a1a 2019-06-02 stsp goto done;
3309 69d57f3d 2020-07-31 stsp }
3310 69d57f3d 2020-07-31 stsp if (tree_id1) {
3311 69d57f3d 2020-07-31 stsp char *id_str;
3312 03415a1a 2019-06-02 stsp
3313 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3314 03415a1a 2019-06-02 stsp if (err)
3315 03415a1a 2019-06-02 stsp goto done;
3316 f69721c3 2019-10-21 stsp
3317 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3318 f69721c3 2019-10-21 stsp if (err)
3319 f69721c3 2019-10-21 stsp goto done;
3320 f69721c3 2019-10-21 stsp
3321 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3322 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3323 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3324 f69721c3 2019-10-21 stsp free(id_str);
3325 f69721c3 2019-10-21 stsp goto done;
3326 f69721c3 2019-10-21 stsp }
3327 f69721c3 2019-10-21 stsp free(id_str);
3328 a0f32f33 2022-06-13 thomas
3329 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3330 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3331 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3332 a0f32f33 2022-06-13 thomas goto done;
3333 a0f32f33 2022-06-13 thomas }
3334 03415a1a 2019-06-02 stsp }
3335 234035bc 2019-06-01 stsp
3336 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3337 945f9229 2022-04-16 thomas if (err)
3338 945f9229 2022-04-16 thomas goto done;
3339 945f9229 2022-04-16 thomas
3340 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3341 234035bc 2019-06-01 stsp worktree->path_prefix);
3342 234035bc 2019-06-01 stsp if (err)
3343 234035bc 2019-06-01 stsp goto done;
3344 234035bc 2019-06-01 stsp
3345 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3346 234035bc 2019-06-01 stsp if (err)
3347 234035bc 2019-06-01 stsp goto done;
3348 234035bc 2019-06-01 stsp
3349 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3350 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3351 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3352 19a6a6b5 2022-07-01 thomas goto done;
3353 19a6a6b5 2022-07-01 thomas }
3354 19a6a6b5 2022-07-01 thomas
3355 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3356 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3357 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3358 a0f32f33 2022-06-13 thomas goto done;
3359 a0f32f33 2022-06-13 thomas }
3360 a0f32f33 2022-06-13 thomas
3361 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3362 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3363 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3364 19a6a6b5 2022-07-01 thomas goto done;
3365 19a6a6b5 2022-07-01 thomas }
3366 19a6a6b5 2022-07-01 thomas
3367 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3368 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3369 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3370 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3371 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3372 69de9dd4 2021-09-03 stsp if (err)
3373 69de9dd4 2021-09-03 stsp goto done;
3374 69de9dd4 2021-09-03 stsp
3375 234035bc 2019-06-01 stsp arg.worktree = worktree;
3376 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3377 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3378 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3379 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3380 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3381 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3382 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3383 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3384 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3385 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3386 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3387 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3388 af12c6b9 2019-06-04 stsp err = sync_err;
3389 234035bc 2019-06-01 stsp done:
3390 945f9229 2022-04-16 thomas if (commit1)
3391 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3392 945f9229 2022-04-16 thomas if (commit2)
3393 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3394 234035bc 2019-06-01 stsp if (tree1)
3395 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3396 234035bc 2019-06-01 stsp if (tree2)
3397 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3398 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3399 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3400 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3401 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3402 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3403 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3404 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3405 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3406 f69721c3 2019-10-21 stsp free(label_orig);
3407 818c7501 2019-07-11 stsp return err;
3408 818c7501 2019-07-11 stsp }
3409 234035bc 2019-06-01 stsp
3410 818c7501 2019-07-11 stsp const struct got_error *
3411 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3412 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3413 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3414 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3415 818c7501 2019-07-11 stsp {
3416 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3417 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3418 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3419 818c7501 2019-07-11 stsp
3420 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3421 818c7501 2019-07-11 stsp if (err)
3422 818c7501 2019-07-11 stsp return err;
3423 818c7501 2019-07-11 stsp
3424 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3425 818c7501 2019-07-11 stsp if (err)
3426 818c7501 2019-07-11 stsp goto done;
3427 818c7501 2019-07-11 stsp
3428 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3429 69de9dd4 2021-09-03 stsp worktree);
3430 818c7501 2019-07-11 stsp if (err)
3431 818c7501 2019-07-11 stsp goto done;
3432 818c7501 2019-07-11 stsp
3433 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3434 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3435 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3436 818c7501 2019-07-11 stsp done:
3437 818c7501 2019-07-11 stsp if (fileindex)
3438 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3439 818c7501 2019-07-11 stsp free(fileindex_path);
3440 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3441 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3442 234035bc 2019-06-01 stsp err = unlockerr;
3443 234035bc 2019-06-01 stsp return err;
3444 234035bc 2019-06-01 stsp }
3445 234035bc 2019-06-01 stsp
3446 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3447 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3448 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3449 927df6b7 2019-02-10 stsp const char *status_path;
3450 927df6b7 2019-02-10 stsp size_t status_path_len;
3451 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3452 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3453 f8d1f275 2019-02-04 stsp void *status_arg;
3454 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3455 f8d1f275 2019-02-04 stsp void *cancel_arg;
3456 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3457 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3458 f2a9dc41 2019-12-13 tracey int report_unchanged;
3459 3143d852 2020-06-25 stsp int no_ignores;
3460 f8d1f275 2019-02-04 stsp };
3461 88d0e355 2019-08-03 stsp
3462 f8d1f275 2019-02-04 stsp static const struct got_error *
3463 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3464 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3465 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3466 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3467 927df6b7 2019-02-10 stsp {
3468 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3469 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3470 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3471 927df6b7 2019-02-10 stsp struct stat sb;
3472 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3473 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3474 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3475 927df6b7 2019-02-10 stsp
3476 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3477 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3478 98eaaa12 2019-08-03 stsp if (err)
3479 98eaaa12 2019-08-03 stsp return err;
3480 98eaaa12 2019-08-03 stsp
3481 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3482 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3483 98eaaa12 2019-08-03 stsp return NULL;
3484 98eaaa12 2019-08-03 stsp
3485 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3486 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3487 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3488 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3489 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3490 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3491 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3492 43010591 2023-02-17 thomas &staged_blob_id, ie);
3493 98eaaa12 2019-08-03 stsp }
3494 98eaaa12 2019-08-03 stsp
3495 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3496 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3497 927df6b7 2019-02-10 stsp }
3498 927df6b7 2019-02-10 stsp
3499 927df6b7 2019-02-10 stsp static const struct got_error *
3500 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3501 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3502 f8d1f275 2019-02-04 stsp {
3503 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3504 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3505 f8d1f275 2019-02-04 stsp char *abspath;
3506 f8d1f275 2019-02-04 stsp
3507 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3509 0584f854 2019-04-06 stsp
3510 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3511 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3512 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3513 927df6b7 2019-02-10 stsp return NULL;
3514 927df6b7 2019-02-10 stsp
3515 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3516 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3517 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3518 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3519 f8d1f275 2019-02-04 stsp } else {
3520 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3521 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3522 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3523 f8d1f275 2019-02-04 stsp }
3524 f8d1f275 2019-02-04 stsp
3525 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3526 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3527 f8d1f275 2019-02-04 stsp free(abspath);
3528 9d31a1d8 2018-03-11 stsp return err;
3529 9d31a1d8 2018-03-11 stsp }
3530 f8d1f275 2019-02-04 stsp
3531 f8d1f275 2019-02-04 stsp static const struct got_error *
3532 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3533 f8d1f275 2019-02-04 stsp {
3534 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3535 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3536 2ec1f75b 2019-03-26 stsp unsigned char status;
3537 927df6b7 2019-02-10 stsp
3538 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3539 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3540 0584f854 2019-04-06 stsp
3541 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3542 927df6b7 2019-02-10 stsp return NULL;
3543 927df6b7 2019-02-10 stsp
3544 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3545 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3546 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3547 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3548 2ec1f75b 2019-03-26 stsp else
3549 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3550 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3551 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 6841da00 2019-08-08 stsp }
3553 6841da00 2019-08-08 stsp
3554 ef20f542 2022-06-26 thomas static void
3555 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3556 6841da00 2019-08-08 stsp {
3557 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3558 6841da00 2019-08-08 stsp
3559 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3560 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3561 21c2d8be 2023-01-10 thomas
3562 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3563 6841da00 2019-08-08 stsp }
3564 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp
3567 6841da00 2019-08-08 stsp static const struct got_error *
3568 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3569 6841da00 2019-08-08 stsp {
3570 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3571 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3572 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3573 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3574 6841da00 2019-08-08 stsp size_t linesize = 0;
3575 6841da00 2019-08-08 stsp ssize_t linelen;
3576 6841da00 2019-08-08 stsp
3577 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3578 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3579 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3580 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3581 6841da00 2019-08-08 stsp
3582 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3583 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3584 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3585 bd8de430 2019-10-04 stsp
3586 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3587 bd8de430 2019-10-04 stsp if (line[0] == '#')
3588 bd8de430 2019-10-04 stsp continue;
3589 bd8de430 2019-10-04 stsp
3590 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3591 bd8de430 2019-10-04 stsp if (line[0] == '!')
3592 bd8de430 2019-10-04 stsp continue;
3593 bd8de430 2019-10-04 stsp
3594 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3595 6841da00 2019-08-08 stsp line) == -1) {
3596 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3597 6841da00 2019-08-08 stsp goto done;
3598 6841da00 2019-08-08 stsp }
3599 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3600 6841da00 2019-08-08 stsp if (err)
3601 6841da00 2019-08-08 stsp goto done;
3602 6841da00 2019-08-08 stsp }
3603 6841da00 2019-08-08 stsp if (ferror(f)) {
3604 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3605 6841da00 2019-08-08 stsp goto done;
3606 6841da00 2019-08-08 stsp }
3607 6841da00 2019-08-08 stsp
3608 6841da00 2019-08-08 stsp dirpath = strdup(path);
3609 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3610 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3611 6841da00 2019-08-08 stsp goto done;
3612 6841da00 2019-08-08 stsp }
3613 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3614 6841da00 2019-08-08 stsp done:
3615 6841da00 2019-08-08 stsp free(line);
3616 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3617 6841da00 2019-08-08 stsp free(dirpath);
3618 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3619 6841da00 2019-08-08 stsp }
3620 6841da00 2019-08-08 stsp return err;
3621 1b5d300f 2023-02-20 thomas }
3622 1b5d300f 2023-02-20 thomas
3623 1b5d300f 2023-02-20 thomas static int
3624 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3625 1b5d300f 2023-02-20 thomas int flags)
3626 1b5d300f 2023-02-20 thomas {
3627 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3628 1b5d300f 2023-02-20 thomas
3629 1b5d300f 2023-02-20 thomas /*
3630 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3631 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3632 1b5d300f 2023-02-20 thomas */
3633 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3634 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3635 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3636 1b5d300f 2023-02-20 thomas
3637 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3638 1b5d300f 2023-02-20 thomas }
3639 1b5d300f 2023-02-20 thomas
3640 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3641 6841da00 2019-08-08 stsp }
3642 6841da00 2019-08-08 stsp
3643 ef20f542 2022-06-26 thomas static int
3644 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3645 6841da00 2019-08-08 stsp {
3646 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3647 bd8de430 2019-10-04 stsp
3648 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3649 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3650 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3651 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3652 bd8de430 2019-10-04 stsp
3653 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3654 1b5d300f 2023-02-20 thomas const char *p;
3655 6841da00 2019-08-08 stsp
3656 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3657 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3658 bd8de430 2019-10-04 stsp continue;
3659 bd8de430 2019-10-04 stsp p = path;
3660 bd8de430 2019-10-04 stsp while (*p) {
3661 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3662 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3663 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3664 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3665 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3666 bd8de430 2019-10-04 stsp p++;
3667 bd8de430 2019-10-04 stsp while (*p == '/')
3668 bd8de430 2019-10-04 stsp p++;
3669 bd8de430 2019-10-04 stsp continue;
3670 bd8de430 2019-10-04 stsp }
3671 bd8de430 2019-10-04 stsp return 1;
3672 bd8de430 2019-10-04 stsp }
3673 bd8de430 2019-10-04 stsp }
3674 bd8de430 2019-10-04 stsp }
3675 bd8de430 2019-10-04 stsp
3676 6841da00 2019-08-08 stsp /*
3677 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3678 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3679 6841da00 2019-08-08 stsp * ignores backwards.
3680 6841da00 2019-08-08 stsp */
3681 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3682 6841da00 2019-08-08 stsp while (pe) {
3683 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3684 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3685 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3686 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3687 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3688 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3689 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3690 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3691 1b5d300f 2023-02-20 thomas path, flags))
3692 6841da00 2019-08-08 stsp continue;
3693 6841da00 2019-08-08 stsp return 1;
3694 6841da00 2019-08-08 stsp }
3695 6841da00 2019-08-08 stsp }
3696 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3697 6841da00 2019-08-08 stsp }
3698 6841da00 2019-08-08 stsp
3699 6841da00 2019-08-08 stsp return 0;
3700 6841da00 2019-08-08 stsp }
3701 6841da00 2019-08-08 stsp
3702 6841da00 2019-08-08 stsp static const struct got_error *
3703 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3704 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3705 6841da00 2019-08-08 stsp {
3706 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3707 6841da00 2019-08-08 stsp char *ignorespath;
3708 886cec17 2019-12-15 stsp int fd = -1;
3709 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3710 6841da00 2019-08-08 stsp
3711 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3712 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3713 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3714 6841da00 2019-08-08 stsp
3715 886cec17 2019-12-15 stsp if (dirfd != -1) {
3716 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3717 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3718 886cec17 2019-12-15 stsp if (fd == -1) {
3719 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3720 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3721 886cec17 2019-12-15 stsp ignorespath);
3722 886cec17 2019-12-15 stsp } else {
3723 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3724 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3725 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3726 886cec17 2019-12-15 stsp ignorespath);
3727 886cec17 2019-12-15 stsp else {
3728 886cec17 2019-12-15 stsp fd = -1;
3729 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3730 886cec17 2019-12-15 stsp }
3731 886cec17 2019-12-15 stsp }
3732 886cec17 2019-12-15 stsp } else {
3733 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3734 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3735 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3736 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3737 886cec17 2019-12-15 stsp ignorespath);
3738 886cec17 2019-12-15 stsp } else
3739 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3740 886cec17 2019-12-15 stsp }
3741 6841da00 2019-08-08 stsp
3742 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3743 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3744 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3745 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3746 6841da00 2019-08-08 stsp free(ignorespath);
3747 6841da00 2019-08-08 stsp return err;
3748 f8d1f275 2019-02-04 stsp }
3749 f8d1f275 2019-02-04 stsp
3750 f8d1f275 2019-02-04 stsp static const struct got_error *
3751 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3752 6092c299 2021-10-04 thomas int dirfd)
3753 f8d1f275 2019-02-04 stsp {
3754 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3755 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3756 f8d1f275 2019-02-04 stsp char *path = NULL;
3757 6092c299 2021-10-04 thomas
3758 6092c299 2021-10-04 thomas if (ignore != NULL)
3759 6092c299 2021-10-04 thomas *ignore = 0;
3760 f8d1f275 2019-02-04 stsp
3761 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3762 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3763 0584f854 2019-04-06 stsp
3764 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3765 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3766 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3767 f8d1f275 2019-02-04 stsp } else {
3768 f8d1f275 2019-02-04 stsp path = de->d_name;
3769 f8d1f275 2019-02-04 stsp }
3770 f8d1f275 2019-02-04 stsp
3771 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3772 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3773 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3774 6092c299 2021-10-04 thomas *ignore = 1;
3775 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3776 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3777 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3778 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3779 f8d1f275 2019-02-04 stsp if (parent_path[0])
3780 f8d1f275 2019-02-04 stsp free(path);
3781 b72f483a 2019-02-05 stsp return err;
3782 f8d1f275 2019-02-04 stsp }
3783 f8d1f275 2019-02-04 stsp
3784 347d1d3e 2019-07-12 stsp static const struct got_error *
3785 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3786 3143d852 2020-06-25 stsp {
3787 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3788 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3789 3143d852 2020-06-25 stsp
3790 3143d852 2020-06-25 stsp if (a->no_ignores)
3791 3143d852 2020-06-25 stsp return NULL;
3792 3143d852 2020-06-25 stsp
3793 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3794 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3795 3143d852 2020-06-25 stsp if (err)
3796 3143d852 2020-06-25 stsp return err;
3797 3143d852 2020-06-25 stsp
3798 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3799 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3800 3143d852 2020-06-25 stsp
3801 3143d852 2020-06-25 stsp return err;
3802 3143d852 2020-06-25 stsp }
3803 3143d852 2020-06-25 stsp
3804 3143d852 2020-06-25 stsp static const struct got_error *
3805 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3806 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3807 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3808 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3809 abb4604f 2019-07-27 stsp {
3810 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3811 abb4604f 2019-07-27 stsp struct stat sb;
3812 ff56836b 2021-07-08 stsp
3813 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3814 abb4604f 2019-07-27 stsp if (ie)
3815 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3816 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3817 abb4604f 2019-07-27 stsp
3818 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3819 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3820 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3821 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3822 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3823 abb4604f 2019-07-27 stsp }
3824 abb4604f 2019-07-27 stsp
3825 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3826 fe1d8685 2022-02-12 thomas return NULL;
3827 fe1d8685 2022-02-12 thomas
3828 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3829 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3830 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3831 abb4604f 2019-07-27 stsp
3832 abb4604f 2019-07-27 stsp return NULL;
3833 3143d852 2020-06-25 stsp }
3834 3143d852 2020-06-25 stsp
3835 3143d852 2020-06-25 stsp static const struct got_error *
3836 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3837 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3838 3143d852 2020-06-25 stsp {
3839 3143d852 2020-06-25 stsp const struct got_error *err;
3840 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3841 3143d852 2020-06-25 stsp
3842 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3843 3143d852 2020-06-25 stsp ".cvsignore");
3844 3143d852 2020-06-25 stsp if (err)
3845 3143d852 2020-06-25 stsp return err;
3846 3143d852 2020-06-25 stsp
3847 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3848 3143d852 2020-06-25 stsp ".gitignore");
3849 3143d852 2020-06-25 stsp if (err)
3850 3143d852 2020-06-25 stsp return err;
3851 3143d852 2020-06-25 stsp
3852 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3853 3143d852 2020-06-25 stsp if (err) {
3854 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3855 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3856 3143d852 2020-06-25 stsp return err;
3857 3143d852 2020-06-25 stsp }
3858 3143d852 2020-06-25 stsp for (;;) {
3859 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3860 3143d852 2020-06-25 stsp ".cvsignore");
3861 3143d852 2020-06-25 stsp if (err)
3862 3143d852 2020-06-25 stsp break;
3863 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3864 3143d852 2020-06-25 stsp ".gitignore");
3865 3143d852 2020-06-25 stsp if (err)
3866 3143d852 2020-06-25 stsp break;
3867 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3868 3143d852 2020-06-25 stsp if (err) {
3869 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3870 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3871 3143d852 2020-06-25 stsp break;
3872 3143d852 2020-06-25 stsp }
3873 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3874 ff56836b 2021-07-08 stsp break;
3875 b737c85a 2020-06-26 stsp free(parent_path);
3876 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3877 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3878 3143d852 2020-06-25 stsp }
3879 3143d852 2020-06-25 stsp
3880 b737c85a 2020-06-26 stsp free(parent_path);
3881 b737c85a 2020-06-26 stsp free(next_parent_path);
3882 3143d852 2020-06-25 stsp return err;
3883 abb4604f 2019-07-27 stsp }
3884 f8836425 2023-04-14 thomas
3885 f8836425 2023-04-14 thomas struct find_missing_children_args {
3886 f8836425 2023-04-14 thomas const char *parent_path;
3887 f8836425 2023-04-14 thomas size_t parent_len;
3888 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3889 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3890 f8836425 2023-04-14 thomas void *cancel_arg;
3891 f8836425 2023-04-14 thomas };
3892 f8836425 2023-04-14 thomas
3893 abb4604f 2019-07-27 stsp static const struct got_error *
3894 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3895 f8836425 2023-04-14 thomas {
3896 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3897 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3898 f8836425 2023-04-14 thomas
3899 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3900 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3901 f8836425 2023-04-14 thomas if (err)
3902 f8836425 2023-04-14 thomas return err;
3903 f8836425 2023-04-14 thomas }
3904 f8836425 2023-04-14 thomas
3905 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3906 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3907 f8836425 2023-04-14 thomas
3908 f8836425 2023-04-14 thomas return err;
3909 f8836425 2023-04-14 thomas }
3910 f8836425 2023-04-14 thomas
3911 f8836425 2023-04-14 thomas static const struct got_error *
3912 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3913 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3914 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3915 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3916 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3917 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3918 f8836425 2023-04-14 thomas {
3919 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3920 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3921 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3922 f8836425 2023-04-14 thomas
3923 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3924 f8836425 2023-04-14 thomas if (cancel_cb) {
3925 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3926 f8836425 2023-04-14 thomas if (err)
3927 f8836425 2023-04-14 thomas break;
3928 f8836425 2023-04-14 thomas }
3929 f8836425 2023-04-14 thomas
3930 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3931 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3932 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3933 f8836425 2023-04-14 thomas ondisk_path = NULL;
3934 f8836425 2023-04-14 thomas break;
3935 f8836425 2023-04-14 thomas }
3936 f8836425 2023-04-14 thomas
3937 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3938 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3939 f8836425 2023-04-14 thomas ignores, no_ignores);
3940 f8836425 2023-04-14 thomas if (err)
3941 f8836425 2023-04-14 thomas break;
3942 f8836425 2023-04-14 thomas
3943 f8836425 2023-04-14 thomas free(ondisk_path);
3944 f8836425 2023-04-14 thomas ondisk_path = NULL;
3945 f8836425 2023-04-14 thomas }
3946 f8836425 2023-04-14 thomas
3947 f8836425 2023-04-14 thomas free(ondisk_path);
3948 f8836425 2023-04-14 thomas return err;
3949 f8836425 2023-04-14 thomas }
3950 f8836425 2023-04-14 thomas
3951 f8836425 2023-04-14 thomas static const struct got_error *
3952 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3953 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3954 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3955 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3956 f2a9dc41 2019-12-13 tracey int report_unchanged)
3957 f8d1f275 2019-02-04 stsp {
3958 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3959 6fc93f37 2019-12-13 stsp int fd = -1;
3960 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3961 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3962 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3963 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3964 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3965 3143d852 2020-06-25 stsp
3966 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3967 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3968 f8d1f275 2019-02-04 stsp
3969 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3970 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3971 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3972 8dc303cc 2019-07-27 stsp
3973 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3974 a78810f8 2022-03-13 thomas if (ie) {
3975 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3976 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3977 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3978 a78810f8 2022-03-13 thomas goto done;
3979 f8836425 2023-04-14 thomas } else {
3980 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3981 f8836425 2023-04-14 thomas fmca.parent_path = path;
3982 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3983 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3984 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3985 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3986 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3987 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3988 f8836425 2023-04-14 thomas if (err)
3989 f8836425 2023-04-14 thomas goto done;
3990 a78810f8 2022-03-13 thomas }
3991 a78810f8 2022-03-13 thomas
3992 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3993 6fc93f37 2019-12-13 stsp if (fd == -1) {
3994 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3995 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3996 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3997 ff56836b 2021-07-08 stsp else {
3998 ff56836b 2021-07-08 stsp if (!no_ignores) {
3999 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4000 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4001 ff56836b 2021-07-08 stsp if (err)
4002 ff56836b 2021-07-08 stsp goto done;
4003 ff56836b 2021-07-08 stsp }
4004 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4005 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4006 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4007 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4008 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4009 f8836425 2023-04-14 thomas if (err)
4010 f8836425 2023-04-14 thomas goto done;
4011 f8836425 2023-04-14 thomas } else {
4012 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4013 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4014 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4015 f8836425 2023-04-14 thomas &ignores, no_ignores,
4016 f8836425 2023-04-14 thomas status_cb, status_arg,
4017 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4018 6f97aa83 2023-04-14 thomas if (err)
4019 6f97aa83 2023-04-14 thomas goto done;
4020 f8836425 2023-04-14 thomas }
4021 ff56836b 2021-07-08 stsp }
4022 abb4604f 2019-07-27 stsp } else {
4023 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4024 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4025 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4026 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4027 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4028 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4029 abb4604f 2019-07-27 stsp arg.status_path = path;
4030 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4031 abb4604f 2019-07-27 stsp arg.repo = repo;
4032 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4033 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4034 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4035 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4036 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4037 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4038 022fae89 2019-12-06 tracey if (!no_ignores) {
4039 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4040 3143d852 2020-06-25 stsp worktree->root_path, path);
4041 3143d852 2020-06-25 stsp if (err)
4042 3143d852 2020-06-25 stsp goto done;
4043 022fae89 2019-12-06 tracey }
4044 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4045 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4046 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4047 927df6b7 2019-02-10 stsp }
4048 3143d852 2020-06-25 stsp done:
4049 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4050 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4051 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4052 927df6b7 2019-02-10 stsp free(ondisk_path);
4053 347d1d3e 2019-07-12 stsp return err;
4054 347d1d3e 2019-07-12 stsp }
4055 347d1d3e 2019-07-12 stsp
4056 347d1d3e 2019-07-12 stsp const struct got_error *
4057 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4058 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4059 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4060 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4061 347d1d3e 2019-07-12 stsp {
4062 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4063 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4064 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4065 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4066 347d1d3e 2019-07-12 stsp
4067 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4068 347d1d3e 2019-07-12 stsp if (err)
4069 347d1d3e 2019-07-12 stsp return err;
4070 347d1d3e 2019-07-12 stsp
4071 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4072 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4073 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4074 f6343036 2021-06-22 stsp no_ignores, 0);
4075 72ea6654 2019-07-27 stsp if (err)
4076 72ea6654 2019-07-27 stsp break;
4077 72ea6654 2019-07-27 stsp }
4078 f8d1f275 2019-02-04 stsp free(fileindex_path);
4079 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4080 f8d1f275 2019-02-04 stsp return err;
4081 f8d1f275 2019-02-04 stsp }
4082 6c7ab921 2019-03-18 stsp
4083 6c7ab921 2019-03-18 stsp const struct got_error *
4084 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4085 6c7ab921 2019-03-18 stsp const char *arg)
4086 6c7ab921 2019-03-18 stsp {
4087 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4088 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4089 6c7ab921 2019-03-18 stsp size_t len;
4090 00bb5ea0 2020-07-23 stsp struct stat sb;
4091 01740607 2020-11-04 stsp char *abspath = NULL;
4092 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4093 6c7ab921 2019-03-18 stsp
4094 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4095 6c7ab921 2019-03-18 stsp
4096 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4097 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4098 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4099 00bb5ea0 2020-07-23 stsp
4100 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4101 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4102 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4103 00bb5ea0 2020-07-23 stsp goto done;
4104 00bb5ea0 2020-07-23 stsp }
4105 727173c3 2020-11-06 stsp sb.st_mode = 0;
4106 00bb5ea0 2020-07-23 stsp }
4107 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4108 00bb5ea0 2020-07-23 stsp /*
4109 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4110 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4111 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4112 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4113 00bb5ea0 2020-07-23 stsp */
4114 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4115 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4116 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4117 00bb5ea0 2020-07-23 stsp goto done;
4118 00bb5ea0 2020-07-23 stsp }
4119 00bb5ea0 2020-07-23 stsp
4120 00bb5ea0 2020-07-23 stsp }
4121 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4122 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4123 00bb5ea0 2020-07-23 stsp if (err)
4124 d0710d08 2019-07-22 stsp goto done;
4125 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4126 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4127 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4128 00bb5ea0 2020-07-23 stsp goto done;
4129 00bb5ea0 2020-07-23 stsp }
4130 00bb5ea0 2020-07-23 stsp } else {
4131 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4132 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4133 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4134 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4135 00bb5ea0 2020-07-23 stsp goto done;
4136 00bb5ea0 2020-07-23 stsp }
4137 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4138 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4139 01740607 2020-11-04 stsp goto done;
4140 01740607 2020-11-04 stsp }
4141 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4142 01740607 2020-11-04 stsp sizeof(canonpath));
4143 01740607 2020-11-04 stsp if (err)
4144 00bb5ea0 2020-07-23 stsp goto done;
4145 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4146 01740607 2020-11-04 stsp if (resolved == NULL) {
4147 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4148 01740607 2020-11-04 stsp goto done;
4149 00bb5ea0 2020-07-23 stsp }
4150 d0710d08 2019-07-22 stsp }
4151 d0710d08 2019-07-22 stsp }
4152 6c7ab921 2019-03-18 stsp
4153 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4154 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4155 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4156 6c7ab921 2019-03-18 stsp goto done;
4157 6c7ab921 2019-03-18 stsp }
4158 6c7ab921 2019-03-18 stsp
4159 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4160 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4161 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4162 f6d88e1a 2019-05-29 stsp if (err)
4163 f6d88e1a 2019-05-29 stsp goto done;
4164 f6d88e1a 2019-05-29 stsp } else {
4165 f6d88e1a 2019-05-29 stsp path = strdup("");
4166 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4167 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4168 f6d88e1a 2019-05-29 stsp goto done;
4169 f6d88e1a 2019-05-29 stsp }
4170 6c7ab921 2019-03-18 stsp }
4171 6c7ab921 2019-03-18 stsp
4172 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4173 6c7ab921 2019-03-18 stsp len = strlen(path);
4174 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4175 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4176 6c7ab921 2019-03-18 stsp len--;
4177 6c7ab921 2019-03-18 stsp }
4178 6c7ab921 2019-03-18 stsp done:
4179 01740607 2020-11-04 stsp free(abspath);
4180 6c7ab921 2019-03-18 stsp free(resolved);
4181 d0710d08 2019-07-22 stsp free(cwd);
4182 6c7ab921 2019-03-18 stsp if (err == NULL)
4183 6c7ab921 2019-03-18 stsp *wt_path = path;
4184 6c7ab921 2019-03-18 stsp else
4185 6c7ab921 2019-03-18 stsp free(path);
4186 d00136be 2019-03-26 stsp return err;
4187 d00136be 2019-03-26 stsp }
4188 d00136be 2019-03-26 stsp
4189 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4190 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4191 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4192 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4193 4e68cba3 2019-11-23 stsp void *progress_arg;
4194 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4195 4e68cba3 2019-11-23 stsp };
4196 4e68cba3 2019-11-23 stsp
4197 4e68cba3 2019-11-23 stsp static const struct got_error *
4198 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4199 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4200 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4201 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4202 07f5b47a 2019-06-02 stsp {
4203 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4204 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4205 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4206 6d022e97 2019-08-04 stsp struct stat sb;
4207 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4208 07f5b47a 2019-06-02 stsp
4209 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4210 dbb83fbd 2019-12-12 stsp relpath) == -1)
4211 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4212 dbb83fbd 2019-12-12 stsp
4213 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4214 6d022e97 2019-08-04 stsp if (ie) {
4215 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4216 12463d8b 2019-12-13 stsp de_name, a->repo);
4217 6d022e97 2019-08-04 stsp if (err)
4218 dbb83fbd 2019-12-12 stsp goto done;
4219 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4220 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4221 dbb83fbd 2019-12-12 stsp goto done;
4222 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4223 dbb83fbd 2019-12-12 stsp if (err)
4224 dbb83fbd 2019-12-12 stsp goto done;
4225 6d022e97 2019-08-04 stsp }
4226 07f5b47a 2019-06-02 stsp
4227 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4228 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4229 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4230 84bf00a6 2022-02-12 thomas else
4231 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4232 dbb83fbd 2019-12-12 stsp goto done;
4233 4e68cba3 2019-11-23 stsp }
4234 07f5b47a 2019-06-02 stsp
4235 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4236 4e68cba3 2019-11-23 stsp if (err)
4237 4e68cba3 2019-11-23 stsp goto done;
4238 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4239 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4240 3969253a 2020-03-07 stsp if (err) {
4241 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4242 3969253a 2020-03-07 stsp goto done;
4243 3969253a 2020-03-07 stsp }
4244 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4245 07f5b47a 2019-06-02 stsp if (err) {
4246 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4247 4e68cba3 2019-11-23 stsp goto done;
4248 07f5b47a 2019-06-02 stsp }
4249 4e68cba3 2019-11-23 stsp done:
4250 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4251 dbb83fbd 2019-12-12 stsp if (err)
4252 dbb83fbd 2019-12-12 stsp return err;
4253 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4254 dbb83fbd 2019-12-12 stsp return NULL;
4255 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4256 07f5b47a 2019-06-02 stsp }
4257 07f5b47a 2019-06-02 stsp
4258 d00136be 2019-03-26 stsp const struct got_error *
4259 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4260 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4261 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4262 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4263 d00136be 2019-03-26 stsp {
4264 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4265 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4266 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4267 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4268 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4269 d00136be 2019-03-26 stsp
4270 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4271 d00136be 2019-03-26 stsp if (err)
4272 d00136be 2019-03-26 stsp return err;
4273 d00136be 2019-03-26 stsp
4274 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4275 d00136be 2019-03-26 stsp if (err)
4276 d00136be 2019-03-26 stsp goto done;
4277 d00136be 2019-03-26 stsp
4278 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4279 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4280 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4281 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4282 4e68cba3 2019-11-23 stsp saa.repo = repo;
4283 4e68cba3 2019-11-23 stsp
4284 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4285 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4286 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4287 1dd54920 2019-05-11 stsp if (err)
4288 af12c6b9 2019-06-04 stsp break;
4289 1dd54920 2019-05-11 stsp }
4290 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4291 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4292 af12c6b9 2019-06-04 stsp err = sync_err;
4293 d00136be 2019-03-26 stsp done:
4294 fb399478 2019-07-12 stsp free(fileindex_path);
4295 d00136be 2019-03-26 stsp if (fileindex)
4296 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4297 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4298 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4299 d00136be 2019-03-26 stsp err = unlockerr;
4300 6c7ab921 2019-03-18 stsp return err;
4301 6c7ab921 2019-03-18 stsp }
4302 17ed4618 2019-06-02 stsp
4303 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4304 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4305 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4306 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4307 f2a9dc41 2019-12-13 tracey void *progress_arg;
4308 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4309 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4310 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4311 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4312 8ec7352a 2023-06-01 thomas const char *status_path;
4313 8ec7352a 2023-06-01 thomas size_t status_path_len;
4314 766841c2 2020-08-13 stsp const char *status_codes;
4315 f2a9dc41 2019-12-13 tracey };
4316 f2a9dc41 2019-12-13 tracey
4317 f2a9dc41 2019-12-13 tracey static const struct got_error *
4318 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4319 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4320 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4321 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4322 17ed4618 2019-06-02 stsp {
4323 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4324 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4325 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4326 17ed4618 2019-06-02 stsp struct stat sb;
4327 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4328 d64cc78a 2022-01-25 thomas
4329 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4330 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4331 d64cc78a 2022-01-25 thomas return NULL;
4332 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4333 d64cc78a 2022-01-25 thomas }
4334 17ed4618 2019-06-02 stsp
4335 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4336 17ed4618 2019-06-02 stsp if (ie == NULL)
4337 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4338 2ec1f75b 2019-03-26 stsp
4339 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4340 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4341 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4342 9acbc4fa 2019-08-03 stsp return NULL;
4343 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4344 9acbc4fa 2019-08-03 stsp }
4345 9acbc4fa 2019-08-03 stsp
4346 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4347 f2a9dc41 2019-12-13 tracey relpath) == -1)
4348 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4349 f2a9dc41 2019-12-13 tracey
4350 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4351 12463d8b 2019-12-13 stsp a->repo);
4352 17ed4618 2019-06-02 stsp if (err)
4353 f2a9dc41 2019-12-13 tracey goto done;
4354 17ed4618 2019-06-02 stsp
4355 766841c2 2020-08-13 stsp if (a->status_codes) {
4356 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4357 766841c2 2020-08-13 stsp int i;
4358 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4359 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4360 766841c2 2020-08-13 stsp break;
4361 766841c2 2020-08-13 stsp }
4362 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4363 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4364 766841c2 2020-08-13 stsp free(ondisk_path);
4365 766841c2 2020-08-13 stsp return NULL;
4366 766841c2 2020-08-13 stsp }
4367 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4368 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4369 766841c2 2020-08-13 stsp static char msg[64];
4370 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4371 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4372 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4373 766841c2 2020-08-13 stsp goto done;
4374 766841c2 2020-08-13 stsp }
4375 766841c2 2020-08-13 stsp }
4376 766841c2 2020-08-13 stsp
4377 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4378 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4379 f2a9dc41 2019-12-13 tracey goto done;
4380 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4381 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4382 f2a9dc41 2019-12-13 tracey goto done;
4383 f2a9dc41 2019-12-13 tracey }
4384 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4385 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4386 d64cc78a 2022-01-25 thomas goto done;
4387 d64cc78a 2022-01-25 thomas }
4388 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4389 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4390 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4391 f2a9dc41 2019-12-13 tracey goto done;
4392 f2a9dc41 2019-12-13 tracey }
4393 17ed4618 2019-06-02 stsp }
4394 17ed4618 2019-06-02 stsp
4395 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4396 20a2ad1c 2020-10-20 stsp size_t root_len;
4397 20a2ad1c 2020-10-20 stsp
4398 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4399 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4400 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4401 12463d8b 2019-12-13 stsp ondisk_path);
4402 12463d8b 2019-12-13 stsp goto done;
4403 12463d8b 2019-12-13 stsp }
4404 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4405 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4406 12463d8b 2019-12-13 stsp goto done;
4407 15341bfd 2020-03-05 tracey }
4408 15341bfd 2020-03-05 tracey
4409 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4410 20a2ad1c 2020-10-20 stsp do {
4411 20a2ad1c 2020-10-20 stsp char *parent;
4412 8ec7352a 2023-06-01 thomas
4413 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4414 20a2ad1c 2020-10-20 stsp if (err)
4415 2513f20a 2020-10-20 stsp goto done;
4416 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4417 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4418 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4419 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4420 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4421 8ec7352a 2023-06-01 thomas a->status_path_len))
4422 8ec7352a 2023-06-01 thomas break;
4423 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4424 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4425 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4426 20a2ad1c 2020-10-20 stsp ondisk_path);
4427 15341bfd 2020-03-05 tracey break;
4428 15341bfd 2020-03-05 tracey }
4429 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4430 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4431 f2a9dc41 2019-12-13 tracey }
4432 17ed4618 2019-06-02 stsp
4433 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4434 f2a9dc41 2019-12-13 tracey done:
4435 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4436 f2a9dc41 2019-12-13 tracey if (err)
4437 f2a9dc41 2019-12-13 tracey return err;
4438 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4439 f2a9dc41 2019-12-13 tracey return NULL;
4440 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4441 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4442 17ed4618 2019-06-02 stsp }
4443 17ed4618 2019-06-02 stsp
4444 2ec1f75b 2019-03-26 stsp const struct got_error *
4445 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4446 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4447 766841c2 2020-08-13 stsp const char *status_codes,
4448 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4449 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4450 2ec1f75b 2019-03-26 stsp {
4451 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4452 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4453 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4454 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4455 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4456 2ec1f75b 2019-03-26 stsp
4457 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4458 2ec1f75b 2019-03-26 stsp if (err)
4459 2ec1f75b 2019-03-26 stsp return err;
4460 2ec1f75b 2019-03-26 stsp
4461 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4462 2ec1f75b 2019-03-26 stsp if (err)
4463 2ec1f75b 2019-03-26 stsp goto done;
4464 f2a9dc41 2019-12-13 tracey
4465 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4466 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4467 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4468 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4469 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4470 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4471 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4472 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4473 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4474 2ec1f75b 2019-03-26 stsp
4475 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4476 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4477 8ec7352a 2023-06-01 thomas
4478 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4479 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4480 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4481 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4482 8ec7352a 2023-06-01 thomas goto done;
4483 8ec7352a 2023-06-01 thomas }
4484 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4485 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4486 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4487 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4488 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4489 17ed4618 2019-06-02 stsp if (err)
4490 af12c6b9 2019-06-04 stsp break;
4491 2ec1f75b 2019-03-26 stsp }
4492 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4493 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4494 af12c6b9 2019-06-04 stsp err = sync_err;
4495 2ec1f75b 2019-03-26 stsp done:
4496 fb399478 2019-07-12 stsp free(fileindex_path);
4497 2ec1f75b 2019-03-26 stsp if (fileindex)
4498 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4499 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4500 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4501 2ec1f75b 2019-03-26 stsp err = unlockerr;
4502 33aa809d 2019-08-08 stsp return err;
4503 33aa809d 2019-08-08 stsp }
4504 33aa809d 2019-08-08 stsp
4505 33aa809d 2019-08-08 stsp static const struct got_error *
4506 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4507 33aa809d 2019-08-08 stsp {
4508 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4509 33aa809d 2019-08-08 stsp char *line = NULL;
4510 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4511 33aa809d 2019-08-08 stsp ssize_t linelen;
4512 33aa809d 2019-08-08 stsp
4513 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4514 33aa809d 2019-08-08 stsp if (linelen == -1) {
4515 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4516 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4517 33aa809d 2019-08-08 stsp goto done;
4518 33aa809d 2019-08-08 stsp }
4519 33aa809d 2019-08-08 stsp return NULL;
4520 33aa809d 2019-08-08 stsp }
4521 33aa809d 2019-08-08 stsp if (outfile) {
4522 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4523 33aa809d 2019-08-08 stsp if (n != linelen) {
4524 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4525 33aa809d 2019-08-08 stsp goto done;
4526 33aa809d 2019-08-08 stsp }
4527 33aa809d 2019-08-08 stsp }
4528 33aa809d 2019-08-08 stsp if (rejectfile) {
4529 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4530 33aa809d 2019-08-08 stsp if (n != linelen)
4531 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4532 33aa809d 2019-08-08 stsp }
4533 33aa809d 2019-08-08 stsp done:
4534 33aa809d 2019-08-08 stsp free(line);
4535 2ec1f75b 2019-03-26 stsp return err;
4536 2ec1f75b 2019-03-26 stsp }
4537 1f1abb7e 2019-08-08 stsp
4538 33aa809d 2019-08-08 stsp static const struct got_error *
4539 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4540 33aa809d 2019-08-08 stsp {
4541 33aa809d 2019-08-08 stsp char *line = NULL;
4542 33aa809d 2019-08-08 stsp size_t linesize = 0;
4543 33aa809d 2019-08-08 stsp ssize_t linelen;
4544 33aa809d 2019-08-08 stsp
4545 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4546 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4547 500467ff 2019-09-25 hiltjo if (ferror(f))
4548 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4549 500467ff 2019-09-25 hiltjo return NULL;
4550 500467ff 2019-09-25 hiltjo }
4551 33aa809d 2019-08-08 stsp free(line);
4552 33aa809d 2019-08-08 stsp return NULL;
4553 33aa809d 2019-08-08 stsp }
4554 33aa809d 2019-08-08 stsp
4555 33aa809d 2019-08-08 stsp static const struct got_error *
4556 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4557 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4558 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4559 abc59930 2021-09-05 naddy {
4560 33aa809d 2019-08-08 stsp const struct got_error *err;
4561 33aa809d 2019-08-08 stsp
4562 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4563 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4564 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4565 33aa809d 2019-08-08 stsp if (err)
4566 33aa809d 2019-08-08 stsp return err;
4567 33aa809d 2019-08-08 stsp (*line_cur1)++;
4568 33aa809d 2019-08-08 stsp }
4569 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4570 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4571 33aa809d 2019-08-08 stsp if (rejectfile)
4572 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4573 33aa809d 2019-08-08 stsp else
4574 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4575 33aa809d 2019-08-08 stsp if (err)
4576 33aa809d 2019-08-08 stsp return err;
4577 33aa809d 2019-08-08 stsp (*line_cur2)++;
4578 33aa809d 2019-08-08 stsp }
4579 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4580 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4581 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4582 33aa809d 2019-08-08 stsp if (err)
4583 33aa809d 2019-08-08 stsp return err;
4584 33aa809d 2019-08-08 stsp (*line_cur2)++;
4585 33aa809d 2019-08-08 stsp }
4586 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4587 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4588 33aa809d 2019-08-08 stsp if (rejectfile)
4589 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4590 33aa809d 2019-08-08 stsp else
4591 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4592 33aa809d 2019-08-08 stsp if (err)
4593 33aa809d 2019-08-08 stsp return err;
4594 33aa809d 2019-08-08 stsp (*line_cur1)++;
4595 33aa809d 2019-08-08 stsp }
4596 f1e81a05 2019-08-10 stsp
4597 f1e81a05 2019-08-10 stsp return NULL;
4598 f1e81a05 2019-08-10 stsp }
4599 f1e81a05 2019-08-10 stsp
4600 f1e81a05 2019-08-10 stsp static const struct got_error *
4601 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4602 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4603 f1e81a05 2019-08-10 stsp {
4604 f1e81a05 2019-08-10 stsp const struct got_error *err;
4605 f1e81a05 2019-08-10 stsp
4606 f1e81a05 2019-08-10 stsp if (outfile) {
4607 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4608 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4609 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4610 f1e81a05 2019-08-10 stsp if (err)
4611 f1e81a05 2019-08-10 stsp return err;
4612 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4613 f1e81a05 2019-08-10 stsp }
4614 f1e81a05 2019-08-10 stsp }
4615 f1e81a05 2019-08-10 stsp if (rejectfile) {
4616 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4617 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4618 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4619 f1e81a05 2019-08-10 stsp if (err)
4620 f1e81a05 2019-08-10 stsp return err;
4621 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4622 f1e81a05 2019-08-10 stsp }
4623 33aa809d 2019-08-08 stsp }
4624 33aa809d 2019-08-08 stsp
4625 33aa809d 2019-08-08 stsp return NULL;
4626 33aa809d 2019-08-08 stsp }
4627 33aa809d 2019-08-08 stsp
4628 33aa809d 2019-08-08 stsp static const struct got_error *
4629 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4630 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4631 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4632 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4633 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4634 33aa809d 2019-08-08 stsp {
4635 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4636 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4637 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4638 33aa809d 2019-08-08 stsp FILE *hunkfile;
4639 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4640 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4641 fe621944 2020-11-10 stsp int rc;
4642 33aa809d 2019-08-08 stsp
4643 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4644 33aa809d 2019-08-08 stsp
4645 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4646 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4647 fe621944 2020-11-10 stsp start_old = cc.left.start;
4648 fe621944 2020-11-10 stsp end_old = cc.left.end;
4649 fe621944 2020-11-10 stsp start_new = cc.right.start;
4650 fe621944 2020-11-10 stsp end_new = cc.right.end;
4651 33aa809d 2019-08-08 stsp
4652 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4653 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4654 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4655 33aa809d 2019-08-08 stsp
4656 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4657 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4658 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4659 33aa809d 2019-08-08 stsp
4660 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4661 fe621944 2020-11-10 stsp if (diff_state == NULL)
4662 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4663 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4664 fe621944 2020-11-10 stsp
4665 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4666 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4667 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4668 33aa809d 2019-08-08 stsp goto done;
4669 33aa809d 2019-08-08 stsp }
4670 fe621944 2020-11-10 stsp
4671 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4672 fe621944 2020-11-10 stsp diff_result, &cc);
4673 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4674 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4675 33aa809d 2019-08-08 stsp goto done;
4676 33aa809d 2019-08-08 stsp }
4677 fe621944 2020-11-10 stsp
4678 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4679 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4680 33aa809d 2019-08-08 stsp goto done;
4681 33aa809d 2019-08-08 stsp }
4682 33aa809d 2019-08-08 stsp
4683 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4684 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4685 33aa809d 2019-08-08 stsp if (err)
4686 33aa809d 2019-08-08 stsp goto done;
4687 33aa809d 2019-08-08 stsp
4688 33aa809d 2019-08-08 stsp switch (*choice) {
4689 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4690 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4691 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4692 33aa809d 2019-08-08 stsp break;
4693 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4694 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4695 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4696 33aa809d 2019-08-08 stsp break;
4697 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4698 33aa809d 2019-08-08 stsp break;
4699 33aa809d 2019-08-08 stsp default:
4700 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4701 33aa809d 2019-08-08 stsp break;
4702 33aa809d 2019-08-08 stsp }
4703 33aa809d 2019-08-08 stsp done:
4704 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4705 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4706 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4707 33aa809d 2019-08-08 stsp return err;
4708 33aa809d 2019-08-08 stsp }
4709 33aa809d 2019-08-08 stsp
4710 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4711 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4712 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4713 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4714 1f1abb7e 2019-08-08 stsp void *progress_arg;
4715 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4716 33aa809d 2019-08-08 stsp void *patch_arg;
4717 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4718 10604dce 2021-09-24 thomas int unlink_added_files;
4719 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4720 1f1abb7e 2019-08-08 stsp };
4721 a129376b 2019-03-28 stsp
4722 e20a8b6f 2019-06-04 stsp static const struct got_error *
4723 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4724 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4725 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4726 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4727 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4728 33aa809d 2019-08-08 stsp {
4729 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4730 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4731 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4732 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4733 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4734 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4735 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4736 fe621944 2020-11-10 stsp struct stat sb2;
4737 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4738 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4739 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4740 33aa809d 2019-08-08 stsp
4741 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4742 33aa809d 2019-08-08 stsp
4743 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4744 33aa809d 2019-08-08 stsp if (err)
4745 33aa809d 2019-08-08 stsp return err;
4746 33aa809d 2019-08-08 stsp
4747 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4748 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4749 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4750 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4751 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4752 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4753 fa3cef63 2020-07-23 stsp goto done;
4754 fa3cef63 2020-07-23 stsp }
4755 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4756 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4757 48b4f239 2021-12-31 thomas if (link_len == -1) {
4758 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4759 48b4f239 2021-12-31 thomas path2);
4760 48b4f239 2021-12-31 thomas }
4761 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4762 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4763 12463d8b 2019-12-13 stsp }
4764 12463d8b 2019-12-13 stsp } else {
4765 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4766 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4767 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4768 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4769 fa3cef63 2020-07-23 stsp goto done;
4770 fa3cef63 2020-07-23 stsp }
4771 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4772 fa3cef63 2020-07-23 stsp sizeof(link_target));
4773 fa3cef63 2020-07-23 stsp if (link_len == -1)
4774 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4775 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4776 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4777 12463d8b 2019-12-13 stsp }
4778 1ebedb77 2019-10-19 stsp }
4779 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4780 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4781 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4782 fa3cef63 2020-07-23 stsp goto done;
4783 fa3cef63 2020-07-23 stsp }
4784 1ebedb77 2019-10-19 stsp
4785 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4786 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4787 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4788 fa3cef63 2020-07-23 stsp goto done;
4789 fa3cef63 2020-07-23 stsp }
4790 fa3cef63 2020-07-23 stsp fd2 = -1;
4791 fa3cef63 2020-07-23 stsp } else {
4792 fa3cef63 2020-07-23 stsp size_t n;
4793 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4794 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4795 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4796 fa3cef63 2020-07-23 stsp goto done;
4797 fa3cef63 2020-07-23 stsp }
4798 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4799 fa3cef63 2020-07-23 stsp if (n != link_len) {
4800 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4801 fa3cef63 2020-07-23 stsp goto done;
4802 fa3cef63 2020-07-23 stsp }
4803 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4804 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4805 fa3cef63 2020-07-23 stsp goto done;
4806 fa3cef63 2020-07-23 stsp }
4807 fa3cef63 2020-07-23 stsp rewind(f2);
4808 33aa809d 2019-08-08 stsp }
4809 33aa809d 2019-08-08 stsp
4810 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4811 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4812 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4813 f4ae6ddb 2022-07-01 thomas goto done;
4814 f4ae6ddb 2022-07-01 thomas }
4815 f4ae6ddb 2022-07-01 thomas
4816 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4817 33aa809d 2019-08-08 stsp if (err)
4818 33aa809d 2019-08-08 stsp goto done;
4819 33aa809d 2019-08-08 stsp
4820 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4821 33aa809d 2019-08-08 stsp if (err)
4822 33aa809d 2019-08-08 stsp goto done;
4823 33aa809d 2019-08-08 stsp
4824 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4825 33aa809d 2019-08-08 stsp if (err)
4826 33aa809d 2019-08-08 stsp goto done;
4827 33aa809d 2019-08-08 stsp
4828 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4829 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4830 33aa809d 2019-08-08 stsp if (err)
4831 33aa809d 2019-08-08 stsp goto done;
4832 33aa809d 2019-08-08 stsp
4833 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4834 fc2a50f2 2022-11-01 thomas "");
4835 33aa809d 2019-08-08 stsp if (err)
4836 33aa809d 2019-08-08 stsp goto done;
4837 33aa809d 2019-08-08 stsp
4838 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4839 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4840 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4841 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4842 fe621944 2020-11-10 stsp
4843 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4844 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4845 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4846 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4847 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4848 fe621944 2020-11-10 stsp nchanges++;
4849 fe621944 2020-11-10 stsp }
4850 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4851 33aa809d 2019-08-08 stsp int choice;
4852 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4853 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4854 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4855 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4856 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4857 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4858 33aa809d 2019-08-08 stsp if (err)
4859 33aa809d 2019-08-08 stsp goto done;
4860 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4861 33aa809d 2019-08-08 stsp have_content = 1;
4862 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4863 33aa809d 2019-08-08 stsp break;
4864 33aa809d 2019-08-08 stsp }
4865 1ebedb77 2019-10-19 stsp if (have_content) {
4866 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4867 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4868 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4869 1ebedb77 2019-10-19 stsp if (err)
4870 1ebedb77 2019-10-19 stsp goto done;
4871 1ebedb77 2019-10-19 stsp
4872 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4873 a2c162eb 2022-10-30 thomas mode_t mode;
4874 a2c162eb 2022-10-30 thomas
4875 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4876 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4877 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4878 fa3cef63 2020-07-23 stsp goto done;
4879 fa3cef63 2020-07-23 stsp }
4880 1ebedb77 2019-10-19 stsp }
4881 1ebedb77 2019-10-19 stsp }
4882 33aa809d 2019-08-08 stsp done:
4883 33aa809d 2019-08-08 stsp free(id_str);
4884 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4885 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4886 33aa809d 2019-08-08 stsp if (blob)
4887 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4888 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4889 fe621944 2020-11-10 stsp if (err == NULL)
4890 fe621944 2020-11-10 stsp err = free_err;
4891 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4892 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4893 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4894 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4895 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4896 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4897 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4898 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4899 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4900 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4901 33aa809d 2019-08-08 stsp if (err || !have_content) {
4902 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4903 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4904 33aa809d 2019-08-08 stsp free(*path_outfile);
4905 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4906 33aa809d 2019-08-08 stsp }
4907 33aa809d 2019-08-08 stsp free(path1);
4908 33aa809d 2019-08-08 stsp return err;
4909 33aa809d 2019-08-08 stsp }
4910 33aa809d 2019-08-08 stsp
4911 33aa809d 2019-08-08 stsp static const struct got_error *
4912 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4913 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4914 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4915 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4916 a129376b 2019-03-28 stsp {
4917 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4918 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4919 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4920 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4921 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4922 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4923 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4924 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4925 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4926 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4927 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4928 f4ae6ddb 2022-07-01 thomas int fd = -1;
4929 a129376b 2019-03-28 stsp
4930 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4931 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4932 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4933 d3bcc3d1 2019-08-08 stsp return NULL;
4934 3d69ad8d 2019-08-17 semarie
4935 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4936 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4937 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4938 d3bcc3d1 2019-08-08 stsp
4939 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4940 65084dad 2019-08-08 stsp if (ie == NULL)
4941 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4942 a129376b 2019-03-28 stsp
4943 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4944 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4945 a129376b 2019-03-28 stsp if (err) {
4946 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4947 a129376b 2019-03-28 stsp goto done;
4948 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4949 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4950 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4951 e20a8b6f 2019-06-04 stsp goto done;
4952 e20a8b6f 2019-06-04 stsp }
4953 a129376b 2019-03-28 stsp }
4954 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4955 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4956 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4957 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4958 a129376b 2019-03-28 stsp goto done;
4959 a129376b 2019-03-28 stsp }
4960 a129376b 2019-03-28 stsp } else {
4961 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4962 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4963 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4964 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4965 a129376b 2019-03-28 stsp goto done;
4966 a129376b 2019-03-28 stsp }
4967 a129376b 2019-03-28 stsp } else {
4968 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4969 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4970 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4971 a129376b 2019-03-28 stsp goto done;
4972 a129376b 2019-03-28 stsp }
4973 a129376b 2019-03-28 stsp }
4974 a129376b 2019-03-28 stsp }
4975 a129376b 2019-03-28 stsp
4976 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4977 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4978 945f9229 2022-04-16 thomas if (err)
4979 945f9229 2022-04-16 thomas goto done;
4980 945f9229 2022-04-16 thomas
4981 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4982 a9fa2909 2019-07-27 stsp if (err) {
4983 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4984 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4985 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4986 a9fa2909 2019-07-27 stsp goto done;
4987 a9fa2909 2019-07-27 stsp } else {
4988 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4989 a9fa2909 2019-07-27 stsp if (err)
4990 a9fa2909 2019-07-27 stsp goto done;
4991 a9fa2909 2019-07-27 stsp
4992 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4993 1233e6b6 2020-10-19 stsp if (err)
4994 a9fa2909 2019-07-27 stsp goto done;
4995 a9fa2909 2019-07-27 stsp
4996 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4997 1233e6b6 2020-10-19 stsp free(te_name);
4998 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4999 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5000 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5001 a9fa2909 2019-07-27 stsp goto done;
5002 a9fa2909 2019-07-27 stsp }
5003 a129376b 2019-03-28 stsp }
5004 a129376b 2019-03-28 stsp
5005 a129376b 2019-03-28 stsp switch (status) {
5006 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5007 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5008 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5009 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5010 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5011 33aa809d 2019-08-08 stsp if (err)
5012 33aa809d 2019-08-08 stsp goto done;
5013 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5014 33aa809d 2019-08-08 stsp break;
5015 33aa809d 2019-08-08 stsp }
5016 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5017 1f1abb7e 2019-08-08 stsp ie->path);
5018 1ee397ad 2019-07-12 stsp if (err)
5019 1ee397ad 2019-07-12 stsp goto done;
5020 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5021 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5022 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5023 8641a332 2023-04-14 thomas
5024 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5025 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5026 8641a332 2023-04-14 thomas
5027 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5028 8641a332 2023-04-14 thomas entry) {
5029 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5030 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5031 8641a332 2023-04-14 thomas continue;
5032 8641a332 2023-04-14 thomas do_unlink = 1;
5033 8641a332 2023-04-14 thomas break;
5034 8641a332 2023-04-14 thomas }
5035 10604dce 2021-09-24 thomas }
5036 8641a332 2023-04-14 thomas
5037 8641a332 2023-04-14 thomas if (do_unlink) {
5038 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5039 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5040 8641a332 2023-04-14 thomas relpath) == -1) {
5041 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5042 8641a332 2023-04-14 thomas goto done;
5043 8641a332 2023-04-14 thomas }
5044 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5045 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5046 8641a332 2023-04-14 thomas ondisk_path);
5047 8641a332 2023-04-14 thomas break;
5048 8641a332 2023-04-14 thomas }
5049 10604dce 2021-09-24 thomas }
5050 10604dce 2021-09-24 thomas }
5051 a129376b 2019-03-28 stsp break;
5052 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5053 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5054 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5055 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5056 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5057 33aa809d 2019-08-08 stsp if (err)
5058 33aa809d 2019-08-08 stsp goto done;
5059 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5060 33aa809d 2019-08-08 stsp break;
5061 33aa809d 2019-08-08 stsp }
5062 33aa809d 2019-08-08 stsp /* fall through */
5063 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5064 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5065 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5066 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5067 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5068 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5069 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5070 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5071 43010591 2023-02-17 thomas else
5072 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5073 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5074 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5075 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5076 f4ae6ddb 2022-07-01 thomas goto done;
5077 f4ae6ddb 2022-07-01 thomas }
5078 f4ae6ddb 2022-07-01 thomas
5079 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5080 a129376b 2019-03-28 stsp if (err)
5081 65084dad 2019-08-08 stsp goto done;
5082 65084dad 2019-08-08 stsp
5083 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5084 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5085 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5086 a129376b 2019-03-28 stsp goto done;
5087 65084dad 2019-08-08 stsp }
5088 65084dad 2019-08-08 stsp
5089 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5090 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5091 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5092 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5093 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5094 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5095 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5096 33aa809d 2019-08-08 stsp break;
5097 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5098 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5099 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5100 369fd7e5 2020-07-23 stsp path_content);
5101 369fd7e5 2020-07-23 stsp break;
5102 369fd7e5 2020-07-23 stsp }
5103 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5104 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5105 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5106 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5107 369fd7e5 2020-07-23 stsp } else {
5108 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5109 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5110 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5111 369fd7e5 2020-07-23 stsp goto done;
5112 369fd7e5 2020-07-23 stsp }
5113 33aa809d 2019-08-08 stsp }
5114 33aa809d 2019-08-08 stsp } else {
5115 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5116 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5117 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5118 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5119 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5120 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5121 2e1fa222 2020-07-23 stsp } else {
5122 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5123 2e1fa222 2020-07-23 stsp ie->path,
5124 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5125 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5126 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5127 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5128 2e1fa222 2020-07-23 stsp }
5129 a129376b 2019-03-28 stsp if (err)
5130 a129376b 2019-03-28 stsp goto done;
5131 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5132 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5133 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5134 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5135 437adc9d 2020-12-10 yzhong blob->id.sha1,
5136 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5137 2e1fa222 2020-07-23 stsp if (err)
5138 2e1fa222 2020-07-23 stsp goto done;
5139 2e1fa222 2020-07-23 stsp }
5140 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5141 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5142 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5143 33aa809d 2019-08-08 stsp }
5144 a129376b 2019-03-28 stsp }
5145 a129376b 2019-03-28 stsp break;
5146 e20a8b6f 2019-06-04 stsp }
5147 a129376b 2019-03-28 stsp default:
5148 1f1abb7e 2019-08-08 stsp break;
5149 a129376b 2019-03-28 stsp }
5150 a129376b 2019-03-28 stsp done:
5151 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5152 33aa809d 2019-08-08 stsp free(path_content);
5153 e20a8b6f 2019-06-04 stsp free(parent_path);
5154 a129376b 2019-03-28 stsp free(tree_path);
5155 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5156 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5157 a129376b 2019-03-28 stsp if (blob)
5158 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5159 a129376b 2019-03-28 stsp if (tree)
5160 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5161 a129376b 2019-03-28 stsp free(tree_id);
5162 945f9229 2022-04-16 thomas if (base_commit)
5163 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5164 e20a8b6f 2019-06-04 stsp return err;
5165 e20a8b6f 2019-06-04 stsp }
5166 e20a8b6f 2019-06-04 stsp
5167 e20a8b6f 2019-06-04 stsp const struct got_error *
5168 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5169 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5170 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5171 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5172 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5173 e20a8b6f 2019-06-04 stsp {
5174 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5175 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5176 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5177 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5178 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5179 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5180 e20a8b6f 2019-06-04 stsp
5181 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5182 e20a8b6f 2019-06-04 stsp if (err)
5183 e20a8b6f 2019-06-04 stsp return err;
5184 e20a8b6f 2019-06-04 stsp
5185 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5186 e20a8b6f 2019-06-04 stsp if (err)
5187 e20a8b6f 2019-06-04 stsp goto done;
5188 e20a8b6f 2019-06-04 stsp
5189 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5190 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5191 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5192 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5193 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5194 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5195 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5196 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5197 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5198 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5199 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5200 e20a8b6f 2019-06-04 stsp if (err)
5201 af12c6b9 2019-06-04 stsp break;
5202 e20a8b6f 2019-06-04 stsp }
5203 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5204 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5205 e20a8b6f 2019-06-04 stsp err = sync_err;
5206 af12c6b9 2019-06-04 stsp done:
5207 fb399478 2019-07-12 stsp free(fileindex_path);
5208 a129376b 2019-03-28 stsp if (fileindex)
5209 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5210 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5211 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5212 a129376b 2019-03-28 stsp err = unlockerr;
5213 c4296144 2019-05-09 stsp return err;
5214 c4296144 2019-05-09 stsp }
5215 c4296144 2019-05-09 stsp
5216 cf066bf8 2019-05-09 stsp static void
5217 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5218 cf066bf8 2019-05-09 stsp {
5219 24519714 2019-05-09 stsp free(ct->path);
5220 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5221 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5222 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5223 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5224 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5225 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5226 cf066bf8 2019-05-09 stsp free(ct);
5227 cf066bf8 2019-05-09 stsp }
5228 24519714 2019-05-09 stsp
5229 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5230 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5231 24519714 2019-05-09 stsp struct got_repository *repo;
5232 24519714 2019-05-09 stsp struct got_worktree *worktree;
5233 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5234 5f8a88c6 2019-08-03 stsp int have_staged_files;
5235 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5236 ef899790 2022-11-01 thomas int diff_header_shown;
5237 be94c032 2023-02-20 thomas int commit_conflicts;
5238 ef899790 2022-11-01 thomas FILE *diff_outfile;
5239 ef899790 2022-11-01 thomas FILE *f1;
5240 ef899790 2022-11-01 thomas FILE *f2;
5241 24519714 2019-05-09 stsp };
5242 ef899790 2022-11-01 thomas
5243 ef899790 2022-11-01 thomas /*
5244 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5245 ef899790 2022-11-01 thomas * it as content to the diff engine.
5246 ef899790 2022-11-01 thomas */
5247 ef899790 2022-11-01 thomas static const struct got_error *
5248 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5249 ef899790 2022-11-01 thomas const char *abspath)
5250 ef899790 2022-11-01 thomas {
5251 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5252 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5253 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5254 ef899790 2022-11-01 thomas
5255 ef899790 2022-11-01 thomas *fd = -1;
5256 ef899790 2022-11-01 thomas
5257 ef899790 2022-11-01 thomas if (dirfd != -1) {
5258 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5259 ef899790 2022-11-01 thomas if (target_len == -1)
5260 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5261 ef899790 2022-11-01 thomas } else {
5262 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5263 ef899790 2022-11-01 thomas if (target_len == -1)
5264 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5265 ef899790 2022-11-01 thomas }
5266 ef899790 2022-11-01 thomas
5267 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5268 ef899790 2022-11-01 thomas if (*fd == -1)
5269 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5270 ef899790 2022-11-01 thomas
5271 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5272 ef899790 2022-11-01 thomas if (outlen == -1) {
5273 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5274 ef899790 2022-11-01 thomas goto done;
5275 ef899790 2022-11-01 thomas }
5276 ef899790 2022-11-01 thomas
5277 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5278 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5279 ef899790 2022-11-01 thomas goto done;
5280 ef899790 2022-11-01 thomas }
5281 ef899790 2022-11-01 thomas done:
5282 ef899790 2022-11-01 thomas if (err) {
5283 ef899790 2022-11-01 thomas close(*fd);
5284 ef899790 2022-11-01 thomas *fd = -1;
5285 ef899790 2022-11-01 thomas }
5286 ef899790 2022-11-01 thomas return err;
5287 ef899790 2022-11-01 thomas }
5288 ef899790 2022-11-01 thomas
5289 ef899790 2022-11-01 thomas static const struct got_error *
5290 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5291 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5292 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5293 ef899790 2022-11-01 thomas {
5294 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5295 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5296 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5297 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5298 ef899790 2022-11-01 thomas char *label1 = NULL;
5299 ef899790 2022-11-01 thomas struct stat sb;
5300 ef899790 2022-11-01 thomas off_t size1 = 0;
5301 ef899790 2022-11-01 thomas int f2_exists = 0;
5302 ef899790 2022-11-01 thomas char *id_str = NULL;
5303 ef899790 2022-11-01 thomas
5304 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5305 d259e491 2022-11-01 thomas
5306 d259e491 2022-11-01 thomas if (diff_staged) {
5307 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5308 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5309 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5310 d259e491 2022-11-01 thomas return NULL;
5311 d259e491 2022-11-01 thomas } else {
5312 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5313 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5314 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5315 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5316 d259e491 2022-11-01 thomas return NULL;
5317 d259e491 2022-11-01 thomas }
5318 ef899790 2022-11-01 thomas
5319 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5320 ef899790 2022-11-01 thomas if (err)
5321 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5322 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5323 ef899790 2022-11-01 thomas if (err)
5324 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5325 ef899790 2022-11-01 thomas
5326 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5327 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5328 ef899790 2022-11-01 thomas if (err)
5329 ef899790 2022-11-01 thomas return err;
5330 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5331 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5332 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5333 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5334 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5335 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5336 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5337 ef899790 2022-11-01 thomas }
5338 ef899790 2022-11-01 thomas
5339 ef899790 2022-11-01 thomas if (diff_staged) {
5340 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5341 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5342 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5343 ef899790 2022-11-01 thomas label1 = ct->path;
5344 ef899790 2022-11-01 thomas label2 = ct->path;
5345 ef899790 2022-11-01 thomas break;
5346 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5347 ef899790 2022-11-01 thomas label2 = ct->path;
5348 ef899790 2022-11-01 thomas break;
5349 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5350 ef899790 2022-11-01 thomas label1 = ct->path;
5351 ef899790 2022-11-01 thomas break;
5352 ef899790 2022-11-01 thomas default:
5353 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5354 ef899790 2022-11-01 thomas }
5355 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5356 ef899790 2022-11-01 thomas if (fd1 == -1) {
5357 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5358 ef899790 2022-11-01 thomas goto done;
5359 ef899790 2022-11-01 thomas }
5360 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5361 ef899790 2022-11-01 thomas if (fd2 == -1) {
5362 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5363 ef899790 2022-11-01 thomas goto done;
5364 ef899790 2022-11-01 thomas }
5365 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5366 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5367 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5368 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5369 ef899790 2022-11-01 thomas goto done;
5370 ef899790 2022-11-01 thomas }
5371 ef899790 2022-11-01 thomas
5372 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5373 ef899790 2022-11-01 thomas if (fd1 == -1) {
5374 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5375 ef899790 2022-11-01 thomas goto done;
5376 ef899790 2022-11-01 thomas }
5377 ef899790 2022-11-01 thomas
5378 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5379 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5380 ef899790 2022-11-01 thomas 8192, fd1);
5381 ef899790 2022-11-01 thomas if (err)
5382 ef899790 2022-11-01 thomas goto done;
5383 ef899790 2022-11-01 thomas }
5384 ef899790 2022-11-01 thomas
5385 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5386 ef899790 2022-11-01 thomas if (dirfd != -1) {
5387 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5388 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5389 ef899790 2022-11-01 thomas if (fd == -1) {
5390 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5391 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5392 ef899790 2022-11-01 thomas ct->ondisk_path);
5393 ef899790 2022-11-01 thomas goto done;
5394 ef899790 2022-11-01 thomas }
5395 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5396 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5397 ef899790 2022-11-01 thomas if (err)
5398 ef899790 2022-11-01 thomas goto done;
5399 ef899790 2022-11-01 thomas }
5400 ef899790 2022-11-01 thomas } else {
5401 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5402 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5403 ef899790 2022-11-01 thomas if (fd == -1) {
5404 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5405 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5406 ef899790 2022-11-01 thomas ct->ondisk_path);
5407 ef899790 2022-11-01 thomas goto done;
5408 ef899790 2022-11-01 thomas }
5409 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5410 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5411 ef899790 2022-11-01 thomas if (err)
5412 ef899790 2022-11-01 thomas goto done;
5413 ef899790 2022-11-01 thomas }
5414 ef899790 2022-11-01 thomas }
5415 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5416 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5417 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5418 ef899790 2022-11-01 thomas goto done;
5419 ef899790 2022-11-01 thomas }
5420 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5421 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5422 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5423 ef899790 2022-11-01 thomas goto done;
5424 ef899790 2022-11-01 thomas }
5425 ef899790 2022-11-01 thomas fd = -1;
5426 ef899790 2022-11-01 thomas f2_exists = 1;
5427 ef899790 2022-11-01 thomas }
5428 ef899790 2022-11-01 thomas
5429 ef899790 2022-11-01 thomas if (blob1) {
5430 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5431 ef899790 2022-11-01 thomas f1, blob1);
5432 ef899790 2022-11-01 thomas if (err)
5433 ef899790 2022-11-01 thomas goto done;
5434 ef899790 2022-11-01 thomas }
5435 ef899790 2022-11-01 thomas
5436 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5437 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5438 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5439 ef899790 2022-11-01 thomas done:
5440 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5441 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5442 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5443 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5444 ef899790 2022-11-01 thomas if (blob1)
5445 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5446 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5447 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5448 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5449 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5450 ef899790 2022-11-01 thomas return err;
5451 ef899790 2022-11-01 thomas }
5452 cf066bf8 2019-05-09 stsp
5453 c4296144 2019-05-09 stsp static const struct got_error *
5454 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5455 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5456 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5457 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5458 c4296144 2019-05-09 stsp {
5459 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5460 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5461 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5462 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5463 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5464 768aea60 2019-05-09 stsp struct stat sb;
5465 c4296144 2019-05-09 stsp
5466 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5467 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5468 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5469 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5470 5f8a88c6 2019-08-03 stsp return NULL;
5471 5f8a88c6 2019-08-03 stsp } else {
5472 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5473 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5474 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5475 be94c032 2023-02-20 thomas }
5476 c4296144 2019-05-09 stsp
5477 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5478 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5479 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5480 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5481 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5482 5f8a88c6 2019-08-03 stsp return NULL;
5483 5f8a88c6 2019-08-03 stsp }
5484 0b5cc0d6 2019-05-09 stsp
5485 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5486 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5487 036813ee 2019-05-09 stsp goto done;
5488 036813ee 2019-05-09 stsp }
5489 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5490 036813ee 2019-05-09 stsp parent_path = strdup("");
5491 69960a46 2019-05-09 stsp if (parent_path == NULL)
5492 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5493 69960a46 2019-05-09 stsp } else {
5494 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5495 69960a46 2019-05-09 stsp if (err)
5496 69960a46 2019-05-09 stsp return err;
5497 69960a46 2019-05-09 stsp }
5498 c4296144 2019-05-09 stsp
5499 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5500 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5501 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5502 c4296144 2019-05-09 stsp goto done;
5503 768aea60 2019-05-09 stsp }
5504 768aea60 2019-05-09 stsp
5505 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5506 768aea60 2019-05-09 stsp relpath) == -1) {
5507 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5508 768aea60 2019-05-09 stsp goto done;
5509 768aea60 2019-05-09 stsp }
5510 0aeb8099 2020-07-23 stsp
5511 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5512 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5513 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5514 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5515 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5516 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5517 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5518 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5519 0aeb8099 2020-07-23 stsp break;
5520 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5521 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5522 0aeb8099 2020-07-23 stsp break;
5523 0aeb8099 2020-07-23 stsp default:
5524 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5525 0aeb8099 2020-07-23 stsp goto done;
5526 0aeb8099 2020-07-23 stsp }
5527 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5528 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5529 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5530 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5531 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5532 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5533 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5534 12463d8b 2019-12-13 stsp ct->ondisk_path);
5535 12463d8b 2019-12-13 stsp goto done;
5536 12463d8b 2019-12-13 stsp }
5537 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5538 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5539 768aea60 2019-05-09 stsp goto done;
5540 768aea60 2019-05-09 stsp }
5541 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5542 c4296144 2019-05-09 stsp }
5543 c4296144 2019-05-09 stsp
5544 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5545 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5546 44d03001 2019-05-09 stsp relpath) == -1) {
5547 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5548 44d03001 2019-05-09 stsp goto done;
5549 44d03001 2019-05-09 stsp }
5550 44d03001 2019-05-09 stsp
5551 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5552 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5553 35213c7c 2020-07-23 stsp int is_bad_symlink;
5554 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5555 35213c7c 2020-07-23 stsp ssize_t target_len;
5556 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5557 35213c7c 2020-07-23 stsp sizeof(target_path));
5558 35213c7c 2020-07-23 stsp if (target_len == -1) {
5559 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5560 35213c7c 2020-07-23 stsp ct->ondisk_path);
5561 35213c7c 2020-07-23 stsp goto done;
5562 35213c7c 2020-07-23 stsp }
5563 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5564 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5565 35213c7c 2020-07-23 stsp if (err)
5566 35213c7c 2020-07-23 stsp goto done;
5567 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5568 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5569 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5570 35213c7c 2020-07-23 stsp goto done;
5571 35213c7c 2020-07-23 stsp }
5572 35213c7c 2020-07-23 stsp }
5573 35213c7c 2020-07-23 stsp
5574 35213c7c 2020-07-23 stsp
5575 cf066bf8 2019-05-09 stsp ct->status = status;
5576 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5577 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5578 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5579 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5580 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5581 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5582 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5583 b416585c 2019-05-13 stsp goto done;
5584 b416585c 2019-05-13 stsp }
5585 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5586 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5587 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5588 f0b75401 2019-08-03 stsp goto done;
5589 f0b75401 2019-08-03 stsp }
5590 f0b75401 2019-08-03 stsp }
5591 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5592 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5593 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5594 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5595 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5596 036813ee 2019-05-09 stsp goto done;
5597 036813ee 2019-05-09 stsp }
5598 ca2503ea 2019-05-09 stsp }
5599 24519714 2019-05-09 stsp ct->path = strdup(path);
5600 24519714 2019-05-09 stsp if (ct->path == NULL) {
5601 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5602 24519714 2019-05-09 stsp goto done;
5603 24519714 2019-05-09 stsp }
5604 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5605 ef899790 2022-11-01 thomas if (err)
5606 ef899790 2022-11-01 thomas goto done;
5607 ef899790 2022-11-01 thomas
5608 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5609 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5610 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5611 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5612 ef899790 2022-11-01 thomas if (err)
5613 ef899790 2022-11-01 thomas goto done;
5614 ef899790 2022-11-01 thomas }
5615 c4296144 2019-05-09 stsp done:
5616 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5617 ed175427 2019-05-09 stsp free_commitable(ct);
5618 24519714 2019-05-09 stsp free(parent_path);
5619 036813ee 2019-05-09 stsp free(path);
5620 a129376b 2019-03-28 stsp return err;
5621 a129376b 2019-03-28 stsp }
5622 c4296144 2019-05-09 stsp
5623 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5624 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5625 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5626 036813ee 2019-05-09 stsp struct got_repository *);
5627 ed175427 2019-05-09 stsp
5628 ed175427 2019-05-09 stsp static const struct got_error *
5629 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5630 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5631 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5632 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5633 afa376bf 2019-05-09 stsp struct got_repository *repo)
5634 ed175427 2019-05-09 stsp {
5635 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5636 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5637 036813ee 2019-05-09 stsp char *subpath;
5638 ed175427 2019-05-09 stsp
5639 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5640 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5641 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5642 ed175427 2019-05-09 stsp
5643 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5644 ed175427 2019-05-09 stsp if (err)
5645 ed175427 2019-05-09 stsp return err;
5646 ed175427 2019-05-09 stsp
5647 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5648 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5649 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5650 036813ee 2019-05-09 stsp free(subpath);
5651 036813ee 2019-05-09 stsp return err;
5652 036813ee 2019-05-09 stsp }
5653 ed175427 2019-05-09 stsp
5654 036813ee 2019-05-09 stsp static const struct got_error *
5655 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5656 036813ee 2019-05-09 stsp {
5657 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5658 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5659 ed175427 2019-05-09 stsp
5660 036813ee 2019-05-09 stsp *match = 0;
5661 ed175427 2019-05-09 stsp
5662 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5663 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5664 0f63689d 2019-05-10 stsp return NULL;
5665 036813ee 2019-05-09 stsp }
5666 0b5cc0d6 2019-05-09 stsp
5667 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5668 0f63689d 2019-05-10 stsp if (err)
5669 0f63689d 2019-05-10 stsp return err;
5670 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5671 036813ee 2019-05-09 stsp free(ct_parent_path);
5672 036813ee 2019-05-09 stsp return err;
5673 036813ee 2019-05-09 stsp }
5674 0b5cc0d6 2019-05-09 stsp
5675 768aea60 2019-05-09 stsp static mode_t
5676 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5677 768aea60 2019-05-09 stsp {
5678 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5679 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5680 3d9a4ec4 2020-07-23 stsp
5681 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5682 768aea60 2019-05-09 stsp }
5683 768aea60 2019-05-09 stsp
5684 036813ee 2019-05-09 stsp static const struct got_error *
5685 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5686 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5687 036813ee 2019-05-09 stsp {
5688 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5689 ca2503ea 2019-05-09 stsp
5690 036813ee 2019-05-09 stsp *new_te = NULL;
5691 0b5cc0d6 2019-05-09 stsp
5692 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5693 036813ee 2019-05-09 stsp if (err)
5694 036813ee 2019-05-09 stsp goto done;
5695 0b5cc0d6 2019-05-09 stsp
5696 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5697 036813ee 2019-05-09 stsp
5698 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5699 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5700 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5701 5f8a88c6 2019-08-03 stsp else
5702 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5703 036813ee 2019-05-09 stsp done:
5704 036813ee 2019-05-09 stsp if (err && *new_te) {
5705 56e0773d 2019-11-28 stsp free(*new_te);
5706 036813ee 2019-05-09 stsp *new_te = NULL;
5707 036813ee 2019-05-09 stsp }
5708 036813ee 2019-05-09 stsp return err;
5709 036813ee 2019-05-09 stsp }
5710 036813ee 2019-05-09 stsp
5711 036813ee 2019-05-09 stsp static const struct got_error *
5712 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5713 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5714 036813ee 2019-05-09 stsp {
5715 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5716 102b254e 2020-10-19 stsp char *ct_name = NULL;
5717 036813ee 2019-05-09 stsp
5718 036813ee 2019-05-09 stsp *new_te = NULL;
5719 036813ee 2019-05-09 stsp
5720 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5721 036813ee 2019-05-09 stsp if (*new_te == NULL)
5722 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5723 036813ee 2019-05-09 stsp
5724 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5725 102b254e 2020-10-19 stsp if (err)
5726 036813ee 2019-05-09 stsp goto done;
5727 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5728 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5729 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5730 036813ee 2019-05-09 stsp goto done;
5731 036813ee 2019-05-09 stsp }
5732 036813ee 2019-05-09 stsp
5733 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5734 036813ee 2019-05-09 stsp
5735 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5736 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5737 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5738 5f8a88c6 2019-08-03 stsp else
5739 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5740 036813ee 2019-05-09 stsp done:
5741 102b254e 2020-10-19 stsp free(ct_name);
5742 036813ee 2019-05-09 stsp if (err && *new_te) {
5743 56e0773d 2019-11-28 stsp free(*new_te);
5744 036813ee 2019-05-09 stsp *new_te = NULL;
5745 036813ee 2019-05-09 stsp }
5746 036813ee 2019-05-09 stsp return err;
5747 036813ee 2019-05-09 stsp }
5748 036813ee 2019-05-09 stsp
5749 036813ee 2019-05-09 stsp static const struct got_error *
5750 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5751 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5752 036813ee 2019-05-09 stsp {
5753 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5754 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5755 036813ee 2019-05-09 stsp
5756 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5757 036813ee 2019-05-09 stsp if (err)
5758 036813ee 2019-05-09 stsp return err;
5759 036813ee 2019-05-09 stsp if (new_pe == NULL)
5760 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5761 036813ee 2019-05-09 stsp return NULL;
5762 afa376bf 2019-05-09 stsp }
5763 afa376bf 2019-05-09 stsp
5764 afa376bf 2019-05-09 stsp static const struct got_error *
5765 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5766 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5767 afa376bf 2019-05-09 stsp {
5768 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5769 5f8a88c6 2019-08-03 stsp unsigned char status;
5770 ae1e948a 2021-09-28 thomas
5771 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5772 ae1e948a 2021-09-28 thomas return NULL;
5773 5f8a88c6 2019-08-03 stsp
5774 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5775 afa376bf 2019-05-09 stsp ct_path++;
5776 5f8a88c6 2019-08-03 stsp
5777 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5778 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5779 5f8a88c6 2019-08-03 stsp else
5780 5f8a88c6 2019-08-03 stsp status = ct->status;
5781 5f8a88c6 2019-08-03 stsp
5782 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5783 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5784 036813ee 2019-05-09 stsp }
5785 036813ee 2019-05-09 stsp
5786 036813ee 2019-05-09 stsp static const struct got_error *
5787 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5788 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5789 44d03001 2019-05-09 stsp {
5790 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5791 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5792 44d03001 2019-05-09 stsp char *te_path;
5793 44d03001 2019-05-09 stsp
5794 44d03001 2019-05-09 stsp *modified = 0;
5795 44d03001 2019-05-09 stsp
5796 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5797 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5798 44d03001 2019-05-09 stsp te->name) == -1)
5799 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5800 44d03001 2019-05-09 stsp
5801 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5802 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5803 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5804 44d03001 2019-05-09 stsp strlen(te_path));
5805 62d463ca 2020-10-20 naddy if (*modified)
5806 44d03001 2019-05-09 stsp break;
5807 44d03001 2019-05-09 stsp }
5808 44d03001 2019-05-09 stsp
5809 44d03001 2019-05-09 stsp free(te_path);
5810 44d03001 2019-05-09 stsp return err;
5811 44d03001 2019-05-09 stsp }
5812 44d03001 2019-05-09 stsp
5813 44d03001 2019-05-09 stsp static const struct got_error *
5814 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5815 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5816 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5817 036813ee 2019-05-09 stsp {
5818 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5819 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5820 036813ee 2019-05-09 stsp
5821 036813ee 2019-05-09 stsp *ctp = NULL;
5822 036813ee 2019-05-09 stsp
5823 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5824 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5825 036813ee 2019-05-09 stsp char *ct_name = NULL;
5826 036813ee 2019-05-09 stsp int path_matches;
5827 036813ee 2019-05-09 stsp
5828 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5829 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5830 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5831 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5832 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5833 5f8a88c6 2019-08-03 stsp continue;
5834 5f8a88c6 2019-08-03 stsp } else {
5835 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5836 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5837 5f8a88c6 2019-08-03 stsp continue;
5838 5f8a88c6 2019-08-03 stsp }
5839 036813ee 2019-05-09 stsp
5840 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5841 036813ee 2019-05-09 stsp continue;
5842 036813ee 2019-05-09 stsp
5843 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5844 62d463ca 2020-10-20 naddy if (err)
5845 036813ee 2019-05-09 stsp return err;
5846 036813ee 2019-05-09 stsp if (!path_matches)
5847 036813ee 2019-05-09 stsp continue;
5848 036813ee 2019-05-09 stsp
5849 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5850 d34b633e 2020-10-19 stsp if (err)
5851 d34b633e 2020-10-19 stsp return err;
5852 d34b633e 2020-10-19 stsp
5853 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5854 d34b633e 2020-10-19 stsp free(ct_name);
5855 036813ee 2019-05-09 stsp continue;
5856 d34b633e 2020-10-19 stsp }
5857 d34b633e 2020-10-19 stsp free(ct_name);
5858 036813ee 2019-05-09 stsp
5859 036813ee 2019-05-09 stsp *ctp = ct;
5860 036813ee 2019-05-09 stsp break;
5861 036813ee 2019-05-09 stsp }
5862 2b496619 2019-07-10 stsp
5863 2b496619 2019-07-10 stsp return err;
5864 2b496619 2019-07-10 stsp }
5865 2b496619 2019-07-10 stsp
5866 2b496619 2019-07-10 stsp static const struct got_error *
5867 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5868 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5869 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5870 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5871 2b496619 2019-07-10 stsp struct got_repository *repo)
5872 2b496619 2019-07-10 stsp {
5873 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5874 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5875 2b496619 2019-07-10 stsp char *subtree_path;
5876 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5877 ba580f68 2020-03-22 stsp int nentries;
5878 2b496619 2019-07-10 stsp
5879 2b496619 2019-07-10 stsp *new_tep = NULL;
5880 2b496619 2019-07-10 stsp
5881 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5882 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5883 2b496619 2019-07-10 stsp child_path) == -1)
5884 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5885 036813ee 2019-05-09 stsp
5886 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5887 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5888 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5889 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5890 56e0773d 2019-11-28 stsp
5891 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5892 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5893 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5894 2b496619 2019-07-10 stsp goto done;
5895 2b496619 2019-07-10 stsp }
5896 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5897 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5898 2b496619 2019-07-10 stsp if (err) {
5899 56e0773d 2019-11-28 stsp free(new_te);
5900 2b496619 2019-07-10 stsp goto done;
5901 2b496619 2019-07-10 stsp }
5902 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5903 2b496619 2019-07-10 stsp done:
5904 56e0773d 2019-11-28 stsp free(id);
5905 2b496619 2019-07-10 stsp free(subtree_path);
5906 2b496619 2019-07-10 stsp if (err == NULL)
5907 2b496619 2019-07-10 stsp *new_tep = new_te;
5908 036813ee 2019-05-09 stsp return err;
5909 036813ee 2019-05-09 stsp }
5910 036813ee 2019-05-09 stsp
5911 036813ee 2019-05-09 stsp static const struct got_error *
5912 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5913 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5914 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5915 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5916 036813ee 2019-05-09 stsp struct got_repository *repo)
5917 036813ee 2019-05-09 stsp {
5918 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5919 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5920 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5921 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5922 036813ee 2019-05-09 stsp
5923 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5924 ba580f68 2020-03-22 stsp *nentries = 0;
5925 036813ee 2019-05-09 stsp
5926 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5927 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5928 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5929 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5930 036813ee 2019-05-09 stsp
5931 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5932 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5933 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5934 036813ee 2019-05-09 stsp continue;
5935 036813ee 2019-05-09 stsp
5936 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5937 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5938 036813ee 2019-05-09 stsp continue;
5939 036813ee 2019-05-09 stsp
5940 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5941 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5942 036813ee 2019-05-09 stsp if (err)
5943 036813ee 2019-05-09 stsp goto done;
5944 036813ee 2019-05-09 stsp
5945 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5946 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5947 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5948 036813ee 2019-05-09 stsp if (err)
5949 036813ee 2019-05-09 stsp goto done;
5950 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5951 afa376bf 2019-05-09 stsp if (err)
5952 afa376bf 2019-05-09 stsp goto done;
5953 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5954 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5955 2b496619 2019-07-10 stsp if (err)
5956 2f51b5b3 2019-05-09 stsp goto done;
5957 ba580f68 2020-03-22 stsp (*nentries)++;
5958 2b496619 2019-07-10 stsp } else {
5959 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5960 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5961 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5962 2b496619 2019-07-10 stsp == NULL) {
5963 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5964 2b496619 2019-07-10 stsp child_path, path_base_tree,
5965 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5966 2b496619 2019-07-10 stsp repo);
5967 2b496619 2019-07-10 stsp if (err)
5968 2b496619 2019-07-10 stsp goto done;
5969 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5970 2b496619 2019-07-10 stsp if (err)
5971 2b496619 2019-07-10 stsp goto done;
5972 ba580f68 2020-03-22 stsp (*nentries)++;
5973 9ba0479c 2019-05-10 stsp }
5974 ed175427 2019-05-09 stsp }
5975 2f51b5b3 2019-05-09 stsp }
5976 2f51b5b3 2019-05-09 stsp
5977 2f51b5b3 2019-05-09 stsp if (base_tree) {
5978 56e0773d 2019-11-28 stsp int i, nbase_entries;
5979 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5980 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5981 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5982 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5983 63c5ca5d 2019-08-24 stsp
5984 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5985 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5986 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5987 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5988 63c5ca5d 2019-08-24 stsp if (err)
5989 63c5ca5d 2019-08-24 stsp goto done;
5990 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5991 63c5ca5d 2019-08-24 stsp if (err)
5992 63c5ca5d 2019-08-24 stsp goto done;
5993 ba580f68 2020-03-22 stsp (*nentries)++;
5994 63c5ca5d 2019-08-24 stsp continue;
5995 63c5ca5d 2019-08-24 stsp }
5996 2f51b5b3 2019-05-09 stsp
5997 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5998 44d03001 2019-05-09 stsp int modified;
5999 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6000 036813ee 2019-05-09 stsp if (err)
6001 036813ee 2019-05-09 stsp goto done;
6002 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6003 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6004 2f51b5b3 2019-05-09 stsp if (err)
6005 2f51b5b3 2019-05-09 stsp goto done;
6006 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6007 44d03001 2019-05-09 stsp if (modified) {
6008 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6009 ba580f68 2020-03-22 stsp int nsubentries;
6010 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6011 ba580f68 2020-03-22 stsp &nsubentries, te,
6012 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6013 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6014 44d03001 2019-05-09 stsp if (err)
6015 44d03001 2019-05-09 stsp goto done;
6016 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6017 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6018 ba580f68 2020-03-22 stsp free(new_id);
6019 ba580f68 2020-03-22 stsp continue;
6020 ba580f68 2020-03-22 stsp }
6021 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6022 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6023 56e0773d 2019-11-28 stsp free(new_id);
6024 44d03001 2019-05-09 stsp }
6025 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6026 036813ee 2019-05-09 stsp if (err)
6027 036813ee 2019-05-09 stsp goto done;
6028 ba580f68 2020-03-22 stsp (*nentries)++;
6029 2f51b5b3 2019-05-09 stsp continue;
6030 036813ee 2019-05-09 stsp }
6031 2f51b5b3 2019-05-09 stsp
6032 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6033 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6034 f66c734c 2019-09-22 stsp if (err)
6035 f66c734c 2019-09-22 stsp goto done;
6036 2f51b5b3 2019-05-09 stsp if (ct) {
6037 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6038 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6039 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6040 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6041 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6042 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6043 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6044 2f51b5b3 2019-05-09 stsp if (err)
6045 2f51b5b3 2019-05-09 stsp goto done;
6046 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6047 2f51b5b3 2019-05-09 stsp if (err)
6048 2f51b5b3 2019-05-09 stsp goto done;
6049 ba580f68 2020-03-22 stsp (*nentries)++;
6050 2f51b5b3 2019-05-09 stsp }
6051 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6052 b416585c 2019-05-13 stsp status_arg);
6053 afa376bf 2019-05-09 stsp if (err)
6054 afa376bf 2019-05-09 stsp goto done;
6055 2f51b5b3 2019-05-09 stsp } else {
6056 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6057 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6058 2f51b5b3 2019-05-09 stsp if (err)
6059 2f51b5b3 2019-05-09 stsp goto done;
6060 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6061 2f51b5b3 2019-05-09 stsp if (err)
6062 2f51b5b3 2019-05-09 stsp goto done;
6063 ba580f68 2020-03-22 stsp (*nentries)++;
6064 2f51b5b3 2019-05-09 stsp }
6065 ca2503ea 2019-05-09 stsp }
6066 ed175427 2019-05-09 stsp }
6067 0b5cc0d6 2019-05-09 stsp
6068 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6069 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6070 ed175427 2019-05-09 stsp done:
6071 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6072 2e1fa222 2020-07-23 stsp return err;
6073 2e1fa222 2020-07-23 stsp }
6074 2e1fa222 2020-07-23 stsp
6075 2e1fa222 2020-07-23 stsp static const struct got_error *
6076 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6077 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6078 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6079 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6080 ebf99748 2019-05-09 stsp {
6081 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6082 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6083 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6084 ebf99748 2019-05-09 stsp
6085 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6086 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6087 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6088 ebf99748 2019-05-09 stsp
6089 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6090 437adc9d 2020-12-10 yzhong
6091 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6092 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6093 437adc9d 2020-12-10 yzhong if (err)
6094 437adc9d 2020-12-10 yzhong goto done;
6095 437adc9d 2020-12-10 yzhong
6096 ebf99748 2019-05-09 stsp if (ie) {
6097 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6098 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6099 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6100 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6101 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6102 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6103 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6104 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6105 437adc9d 2020-12-10 yzhong
6106 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6107 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6108 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6109 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6110 72fd46fa 2019-09-06 stsp !have_staged_files);
6111 ebf99748 2019-05-09 stsp } else
6112 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6113 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6114 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6115 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6116 72fd46fa 2019-09-06 stsp !have_staged_files);
6117 ebf99748 2019-05-09 stsp } else {
6118 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6119 ebf99748 2019-05-09 stsp if (err)
6120 437adc9d 2020-12-10 yzhong goto done;
6121 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6122 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6123 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6124 3969253a 2020-03-07 stsp if (err) {
6125 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6126 437adc9d 2020-12-10 yzhong goto done;
6127 3969253a 2020-03-07 stsp }
6128 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6129 3969253a 2020-03-07 stsp if (err) {
6130 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6131 437adc9d 2020-12-10 yzhong goto done;
6132 3969253a 2020-03-07 stsp }
6133 ebf99748 2019-05-09 stsp }
6134 437adc9d 2020-12-10 yzhong free(relpath);
6135 437adc9d 2020-12-10 yzhong relpath = NULL;
6136 ebf99748 2019-05-09 stsp }
6137 437adc9d 2020-12-10 yzhong done:
6138 437adc9d 2020-12-10 yzhong free(relpath);
6139 d56d26ce 2019-05-10 stsp return err;
6140 d56d26ce 2019-05-10 stsp }
6141 735ef5ac 2019-08-03 stsp
6142 d56d26ce 2019-05-10 stsp
6143 d56d26ce 2019-05-10 stsp static const struct got_error *
6144 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6145 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6146 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6147 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6148 735ef5ac 2019-08-03 stsp int ood_errcode)
6149 d56d26ce 2019-05-10 stsp {
6150 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6151 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6152 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6153 1a36436d 2019-06-10 stsp
6154 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6155 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6156 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6157 1a36436d 2019-06-10 stsp return NULL;
6158 9bead371 2019-07-28 stsp /*
6159 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6160 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6161 9bead371 2019-07-28 stsp */
6162 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6163 945f9229 2022-04-16 thomas if (err)
6164 945f9229 2022-04-16 thomas goto done;
6165 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6166 9bead371 2019-07-28 stsp if (err) {
6167 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6168 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6169 1a36436d 2019-06-10 stsp goto done;
6170 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6171 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6172 1a36436d 2019-06-10 stsp } else {
6173 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6174 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6175 945f9229 2022-04-16 thomas if (err)
6176 945f9229 2022-04-16 thomas goto done;
6177 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6178 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6179 1a36436d 2019-06-10 stsp goto done;
6180 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6181 1a36436d 2019-06-10 stsp }
6182 1a36436d 2019-06-10 stsp done:
6183 1a36436d 2019-06-10 stsp free(id);
6184 945f9229 2022-04-16 thomas if (commit)
6185 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6186 1a36436d 2019-06-10 stsp return err;
6187 ebf99748 2019-05-09 stsp }
6188 ebf99748 2019-05-09 stsp
6189 ef20f542 2022-06-26 thomas static const struct got_error *
6190 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6191 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6192 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6193 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6194 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6195 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6196 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6197 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6198 afa376bf 2019-05-09 stsp struct got_repository *repo)
6199 c4296144 2019-05-09 stsp {
6200 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6201 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6202 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6203 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6204 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6205 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6206 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6207 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6208 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6209 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6210 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6211 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6212 892eab0a 2022-07-22 thomas time_t timestamp;
6213 c4296144 2019-05-09 stsp
6214 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6215 c4296144 2019-05-09 stsp
6216 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6217 675c7539 2019-05-09 stsp
6218 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6219 588edf97 2019-05-10 stsp if (err)
6220 588edf97 2019-05-10 stsp goto done;
6221 de18fc63 2019-05-09 stsp
6222 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6223 588edf97 2019-05-10 stsp if (err)
6224 588edf97 2019-05-10 stsp goto done;
6225 588edf97 2019-05-10 stsp
6226 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6227 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6228 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6229 33ad4cbe 2019-05-12 jcs if (err)
6230 33ad4cbe 2019-05-12 jcs goto done;
6231 33ad4cbe 2019-05-12 jcs }
6232 c4296144 2019-05-09 stsp
6233 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6234 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6235 33ad4cbe 2019-05-12 jcs goto done;
6236 33ad4cbe 2019-05-12 jcs }
6237 33ad4cbe 2019-05-12 jcs
6238 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6239 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6240 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6241 cf066bf8 2019-05-09 stsp char *ondisk_path;
6242 cf066bf8 2019-05-09 stsp
6243 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6244 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6245 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6246 5f8a88c6 2019-08-03 stsp continue;
6247 5f8a88c6 2019-08-03 stsp
6248 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6249 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6250 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6251 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6252 cf066bf8 2019-05-09 stsp continue;
6253 cf066bf8 2019-05-09 stsp
6254 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6255 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6256 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6257 cf066bf8 2019-05-09 stsp goto done;
6258 cf066bf8 2019-05-09 stsp }
6259 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6260 2e1fa222 2020-07-23 stsp free(ondisk_path);
6261 2e1fa222 2020-07-23 stsp if (err)
6262 cf066bf8 2019-05-09 stsp goto done;
6263 cf066bf8 2019-05-09 stsp }
6264 036813ee 2019-05-09 stsp
6265 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6266 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6267 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6268 036813ee 2019-05-09 stsp if (err)
6269 036813ee 2019-05-09 stsp goto done;
6270 036813ee 2019-05-09 stsp
6271 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6272 de18fc63 2019-05-09 stsp if (err)
6273 de18fc63 2019-05-09 stsp goto done;
6274 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6275 10604dce 2021-09-24 thomas nparents++;
6276 10604dce 2021-09-24 thomas if (parent_id2) {
6277 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6278 10604dce 2021-09-24 thomas if (err)
6279 10604dce 2021-09-24 thomas goto done;
6280 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6281 10604dce 2021-09-24 thomas nparents++;
6282 10604dce 2021-09-24 thomas }
6283 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6284 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6285 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6286 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6287 33ad4cbe 2019-05-12 jcs free(logmsg);
6288 9d40349a 2019-05-09 stsp if (err)
6289 9d40349a 2019-05-09 stsp goto done;
6290 9d40349a 2019-05-09 stsp
6291 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6292 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6293 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6294 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6295 09f5bd90 2019-05-09 stsp goto done;
6296 09f5bd90 2019-05-09 stsp }
6297 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6298 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6299 09f5bd90 2019-05-09 stsp if (err)
6300 09f5bd90 2019-05-09 stsp goto done;
6301 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6302 ebf99748 2019-05-09 stsp if (err)
6303 ebf99748 2019-05-09 stsp goto done;
6304 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6305 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6306 09f5bd90 2019-05-09 stsp goto done;
6307 09f5bd90 2019-05-09 stsp }
6308 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6309 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6310 f2c16586 2019-05-09 stsp if (err)
6311 f2c16586 2019-05-09 stsp goto done;
6312 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6313 f2c16586 2019-05-09 stsp if (err)
6314 f2c16586 2019-05-09 stsp goto done;
6315 f2c16586 2019-05-09 stsp
6316 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6317 09f5bd90 2019-05-09 stsp if (err)
6318 09f5bd90 2019-05-09 stsp goto done;
6319 09f5bd90 2019-05-09 stsp
6320 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6321 ebf99748 2019-05-09 stsp if (err)
6322 ebf99748 2019-05-09 stsp goto done;
6323 c4296144 2019-05-09 stsp done:
6324 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6325 588edf97 2019-05-10 stsp if (head_tree)
6326 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6327 588edf97 2019-05-10 stsp if (head_commit)
6328 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6329 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6330 2f17228e 2019-05-12 stsp if (head_ref2) {
6331 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6332 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6333 2f17228e 2019-05-12 stsp err = unlockerr;
6334 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6335 39cd0ff6 2019-07-12 stsp }
6336 39cd0ff6 2019-07-12 stsp return err;
6337 39cd0ff6 2019-07-12 stsp }
6338 5c1e53bc 2019-07-28 stsp
6339 5c1e53bc 2019-07-28 stsp static const struct got_error *
6340 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6341 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6342 5c1e53bc 2019-07-28 stsp {
6343 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6344 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6345 39cd0ff6 2019-07-12 stsp
6346 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6347 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6348 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6349 5c1e53bc 2019-07-28 stsp
6350 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6351 5c1e53bc 2019-07-28 stsp ct_path++;
6352 5c1e53bc 2019-07-28 stsp
6353 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6354 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6355 5c1e53bc 2019-07-28 stsp break;
6356 5c1e53bc 2019-07-28 stsp }
6357 5c1e53bc 2019-07-28 stsp
6358 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6359 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6360 5c1e53bc 2019-07-28 stsp
6361 5c1e53bc 2019-07-28 stsp return NULL;
6362 5c1e53bc 2019-07-28 stsp }
6363 5c1e53bc 2019-07-28 stsp
6364 f0b75401 2019-08-03 stsp static const struct got_error *
6365 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6366 f0b75401 2019-08-03 stsp {
6367 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6368 f0b75401 2019-08-03 stsp
6369 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6370 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6371 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6372 f0b75401 2019-08-03 stsp }
6373 f0b75401 2019-08-03 stsp
6374 f0b75401 2019-08-03 stsp return NULL;
6375 f0b75401 2019-08-03 stsp }
6376 f0b75401 2019-08-03 stsp
6377 5f8a88c6 2019-08-03 stsp static const struct got_error *
6378 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6379 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6380 5f8a88c6 2019-08-03 stsp {
6381 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6382 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6383 5f8a88c6 2019-08-03 stsp
6384 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6385 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6386 5f8a88c6 2019-08-03 stsp continue;
6387 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6388 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6389 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6390 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6391 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6392 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6393 5f8a88c6 2019-08-03 stsp }
6394 5f8a88c6 2019-08-03 stsp
6395 5f8a88c6 2019-08-03 stsp return NULL;
6396 5f8a88c6 2019-08-03 stsp }
6397 5f8a88c6 2019-08-03 stsp
6398 39cd0ff6 2019-07-12 stsp const struct got_error *
6399 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6400 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6401 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6402 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6403 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6404 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6405 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6406 39cd0ff6 2019-07-12 stsp {
6407 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6408 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6409 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6410 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6411 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6412 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6413 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6414 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6415 ef899790 2022-11-01 thomas char *diff_path = NULL;
6416 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6417 39cd0ff6 2019-07-12 stsp
6418 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6419 39cd0ff6 2019-07-12 stsp
6420 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6421 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6422 39cd0ff6 2019-07-12 stsp
6423 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6424 39cd0ff6 2019-07-12 stsp if (err)
6425 39cd0ff6 2019-07-12 stsp goto done;
6426 39cd0ff6 2019-07-12 stsp
6427 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6428 39cd0ff6 2019-07-12 stsp if (err)
6429 39cd0ff6 2019-07-12 stsp goto done;
6430 39cd0ff6 2019-07-12 stsp
6431 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6432 39cd0ff6 2019-07-12 stsp if (err)
6433 39cd0ff6 2019-07-12 stsp goto done;
6434 39cd0ff6 2019-07-12 stsp
6435 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6436 39cd0ff6 2019-07-12 stsp if (err)
6437 39cd0ff6 2019-07-12 stsp goto done;
6438 39cd0ff6 2019-07-12 stsp
6439 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6440 5f8a88c6 2019-08-03 stsp &have_staged_files);
6441 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6442 5f8a88c6 2019-08-03 stsp goto done;
6443 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6444 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6445 5f8a88c6 2019-08-03 stsp if (err)
6446 5f8a88c6 2019-08-03 stsp goto done;
6447 5f8a88c6 2019-08-03 stsp }
6448 5f8a88c6 2019-08-03 stsp
6449 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6450 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6451 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6452 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6453 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6454 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6455 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6456 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6457 ef899790 2022-11-01 thomas if (show_diff) {
6458 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6459 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6460 ef899790 2022-11-01 thomas if (err)
6461 ef899790 2022-11-01 thomas goto done;
6462 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6463 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6464 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6465 ef899790 2022-11-01 thomas goto done;
6466 ef899790 2022-11-01 thomas }
6467 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6468 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6469 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6470 ef899790 2022-11-01 thomas goto done;
6471 ef899790 2022-11-01 thomas }
6472 ef899790 2022-11-01 thomas }
6473 ef899790 2022-11-01 thomas
6474 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6475 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6476 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6477 5c1e53bc 2019-07-28 stsp if (err)
6478 5c1e53bc 2019-07-28 stsp goto done;
6479 5c1e53bc 2019-07-28 stsp }
6480 39cd0ff6 2019-07-12 stsp
6481 ef899790 2022-11-01 thomas if (show_diff) {
6482 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6483 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6484 ef899790 2022-11-01 thomas goto done;
6485 ef899790 2022-11-01 thomas }
6486 ef899790 2022-11-01 thomas }
6487 ef899790 2022-11-01 thomas
6488 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6489 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6490 39cd0ff6 2019-07-12 stsp goto done;
6491 5c1e53bc 2019-07-28 stsp }
6492 5c1e53bc 2019-07-28 stsp
6493 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6494 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6495 5c1e53bc 2019-07-28 stsp if (err)
6496 5c1e53bc 2019-07-28 stsp goto done;
6497 39cd0ff6 2019-07-12 stsp }
6498 f0b75401 2019-08-03 stsp
6499 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6500 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6501 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6502 f0b75401 2019-08-03 stsp
6503 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6504 f0b75401 2019-08-03 stsp ct_path++;
6505 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6506 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6507 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6508 b50cabdf 2019-07-12 stsp if (err)
6509 b50cabdf 2019-07-12 stsp goto done;
6510 f0b75401 2019-08-03 stsp
6511 b50cabdf 2019-07-12 stsp }
6512 b50cabdf 2019-07-12 stsp
6513 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6514 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6515 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6516 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6517 39cd0ff6 2019-07-12 stsp if (err)
6518 39cd0ff6 2019-07-12 stsp goto done;
6519 39cd0ff6 2019-07-12 stsp
6520 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6521 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6522 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6523 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6524 39cd0ff6 2019-07-12 stsp err = sync_err;
6525 39cd0ff6 2019-07-12 stsp done:
6526 39cd0ff6 2019-07-12 stsp if (fileindex)
6527 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6528 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6529 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6530 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6531 39cd0ff6 2019-07-12 stsp err = unlockerr;
6532 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6533 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6534 21c2d8be 2023-01-10 thomas
6535 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6536 39cd0ff6 2019-07-12 stsp }
6537 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6538 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6539 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6540 ef899790 2022-11-01 thomas free(diff_path);
6541 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6542 ef899790 2022-11-01 thomas err == NULL)
6543 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6544 c4296144 2019-05-09 stsp return err;
6545 8656d6c4 2019-05-20 stsp }
6546 8656d6c4 2019-05-20 stsp
6547 8656d6c4 2019-05-20 stsp const char *
6548 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6549 8656d6c4 2019-05-20 stsp {
6550 8656d6c4 2019-05-20 stsp return ct->path;
6551 8656d6c4 2019-05-20 stsp }
6552 8656d6c4 2019-05-20 stsp
6553 8656d6c4 2019-05-20 stsp unsigned int
6554 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6555 8656d6c4 2019-05-20 stsp {
6556 8656d6c4 2019-05-20 stsp return ct->status;
6557 818c7501 2019-07-11 stsp }
6558 818c7501 2019-07-11 stsp
6559 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6560 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6561 818c7501 2019-07-11 stsp struct got_repository *repo;
6562 818c7501 2019-07-11 stsp };
6563 818c7501 2019-07-11 stsp
6564 818c7501 2019-07-11 stsp static const struct got_error *
6565 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6566 818c7501 2019-07-11 stsp {
6567 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6568 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6569 818c7501 2019-07-11 stsp unsigned char status;
6570 818c7501 2019-07-11 stsp struct stat sb;
6571 818c7501 2019-07-11 stsp char *ondisk_path;
6572 818c7501 2019-07-11 stsp
6573 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6574 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6575 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6576 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6577 818c7501 2019-07-11 stsp
6578 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6579 818c7501 2019-07-11 stsp == -1)
6580 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6581 818c7501 2019-07-11 stsp
6582 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6583 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6584 818c7501 2019-07-11 stsp free(ondisk_path);
6585 818c7501 2019-07-11 stsp if (err)
6586 818c7501 2019-07-11 stsp return err;
6587 818c7501 2019-07-11 stsp
6588 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6589 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6590 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6591 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6592 818c7501 2019-07-11 stsp
6593 818c7501 2019-07-11 stsp return NULL;
6594 818c7501 2019-07-11 stsp }
6595 818c7501 2019-07-11 stsp
6596 818c7501 2019-07-11 stsp const struct got_error *
6597 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6598 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6599 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6600 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6601 818c7501 2019-07-11 stsp {
6602 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6603 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6604 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6605 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6606 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6607 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6608 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6609 818c7501 2019-07-11 stsp
6610 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6611 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6612 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6613 818c7501 2019-07-11 stsp
6614 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6615 818c7501 2019-07-11 stsp if (err)
6616 818c7501 2019-07-11 stsp return err;
6617 818c7501 2019-07-11 stsp
6618 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 818c7501 2019-07-11 stsp if (err)
6620 818c7501 2019-07-11 stsp goto done;
6621 818c7501 2019-07-11 stsp
6622 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6623 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6624 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6625 818c7501 2019-07-11 stsp &ok_arg);
6626 818c7501 2019-07-11 stsp if (err)
6627 818c7501 2019-07-11 stsp goto done;
6628 818c7501 2019-07-11 stsp
6629 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6630 818c7501 2019-07-11 stsp if (err)
6631 818c7501 2019-07-11 stsp goto done;
6632 818c7501 2019-07-11 stsp
6633 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6634 818c7501 2019-07-11 stsp if (err)
6635 818c7501 2019-07-11 stsp goto done;
6636 818c7501 2019-07-11 stsp
6637 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6638 818c7501 2019-07-11 stsp if (err)
6639 818c7501 2019-07-11 stsp goto done;
6640 818c7501 2019-07-11 stsp
6641 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6642 818c7501 2019-07-11 stsp 0);
6643 e51d7b55 2020-01-04 stsp if (err)
6644 e51d7b55 2020-01-04 stsp goto done;
6645 e51d7b55 2020-01-04 stsp
6646 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6647 818c7501 2019-07-11 stsp if (err)
6648 818c7501 2019-07-11 stsp goto done;
6649 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6650 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6651 e51d7b55 2020-01-04 stsp goto done;
6652 e51d7b55 2020-01-04 stsp }
6653 818c7501 2019-07-11 stsp
6654 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6655 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6656 818c7501 2019-07-11 stsp if (err)
6657 818c7501 2019-07-11 stsp goto done;
6658 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6659 818c7501 2019-07-11 stsp if (err)
6660 818c7501 2019-07-11 stsp goto done;
6661 818c7501 2019-07-11 stsp
6662 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6663 818c7501 2019-07-11 stsp
6664 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6665 818c7501 2019-07-11 stsp if (err)
6666 818c7501 2019-07-11 stsp goto done;
6667 818c7501 2019-07-11 stsp
6668 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6669 818c7501 2019-07-11 stsp if (err)
6670 818c7501 2019-07-11 stsp goto done;
6671 818c7501 2019-07-11 stsp
6672 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6673 818c7501 2019-07-11 stsp worktree->base_commit_id);
6674 818c7501 2019-07-11 stsp if (err)
6675 818c7501 2019-07-11 stsp goto done;
6676 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6677 818c7501 2019-07-11 stsp if (err)
6678 818c7501 2019-07-11 stsp goto done;
6679 818c7501 2019-07-11 stsp
6680 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6681 818c7501 2019-07-11 stsp if (err)
6682 818c7501 2019-07-11 stsp goto done;
6683 818c7501 2019-07-11 stsp done:
6684 818c7501 2019-07-11 stsp free(fileindex_path);
6685 818c7501 2019-07-11 stsp free(tmp_branch_name);
6686 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6687 818c7501 2019-07-11 stsp free(branch_ref_name);
6688 818c7501 2019-07-11 stsp if (branch_ref)
6689 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6690 818c7501 2019-07-11 stsp if (wt_branch)
6691 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6692 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6693 818c7501 2019-07-11 stsp if (err) {
6694 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6695 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6696 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6697 818c7501 2019-07-11 stsp }
6698 818c7501 2019-07-11 stsp if (*tmp_branch) {
6699 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6700 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6701 818c7501 2019-07-11 stsp }
6702 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6703 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6704 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6705 3e3a69f1 2019-07-25 stsp }
6706 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6707 818c7501 2019-07-11 stsp }
6708 818c7501 2019-07-11 stsp return err;
6709 818c7501 2019-07-11 stsp }
6710 818c7501 2019-07-11 stsp
6711 818c7501 2019-07-11 stsp const struct got_error *
6712 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6713 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6714 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6715 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6716 818c7501 2019-07-11 stsp {
6717 818c7501 2019-07-11 stsp const struct got_error *err;
6718 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6719 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6720 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6721 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6722 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6723 818c7501 2019-07-11 stsp
6724 818c7501 2019-07-11 stsp *commit_id = NULL;
6725 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6726 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6727 3e3a69f1 2019-07-25 stsp *branch = NULL;
6728 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6729 3e3a69f1 2019-07-25 stsp
6730 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6731 3e3a69f1 2019-07-25 stsp if (err)
6732 3e3a69f1 2019-07-25 stsp return err;
6733 818c7501 2019-07-11 stsp
6734 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6735 3e3a69f1 2019-07-25 stsp if (err)
6736 f032f1f7 2019-08-04 stsp goto done;
6737 f032f1f7 2019-08-04 stsp
6738 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6739 f032f1f7 2019-08-04 stsp &have_staged_files);
6740 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6741 f032f1f7 2019-08-04 stsp goto done;
6742 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6743 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6744 3e3a69f1 2019-07-25 stsp goto done;
6745 f032f1f7 2019-08-04 stsp }
6746 3e3a69f1 2019-07-25 stsp
6747 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6748 818c7501 2019-07-11 stsp if (err)
6749 f032f1f7 2019-08-04 stsp goto done;
6750 818c7501 2019-07-11 stsp
6751 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6752 818c7501 2019-07-11 stsp if (err)
6753 818c7501 2019-07-11 stsp goto done;
6754 818c7501 2019-07-11 stsp
6755 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6756 818c7501 2019-07-11 stsp if (err)
6757 818c7501 2019-07-11 stsp goto done;
6758 818c7501 2019-07-11 stsp
6759 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6760 818c7501 2019-07-11 stsp if (err)
6761 818c7501 2019-07-11 stsp goto done;
6762 818c7501 2019-07-11 stsp
6763 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6764 818c7501 2019-07-11 stsp if (err)
6765 818c7501 2019-07-11 stsp goto done;
6766 818c7501 2019-07-11 stsp
6767 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6768 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6769 818c7501 2019-07-11 stsp if (err)
6770 818c7501 2019-07-11 stsp goto done;
6771 818c7501 2019-07-11 stsp
6772 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6773 818c7501 2019-07-11 stsp if (err)
6774 818c7501 2019-07-11 stsp goto done;
6775 818c7501 2019-07-11 stsp
6776 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6777 818c7501 2019-07-11 stsp if (err)
6778 818c7501 2019-07-11 stsp goto done;
6779 818c7501 2019-07-11 stsp
6780 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6781 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6782 818c7501 2019-07-11 stsp if (err)
6783 818c7501 2019-07-11 stsp goto done;
6784 818c7501 2019-07-11 stsp
6785 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6786 818c7501 2019-07-11 stsp if (err)
6787 818c7501 2019-07-11 stsp goto done;
6788 818c7501 2019-07-11 stsp done:
6789 818c7501 2019-07-11 stsp free(commit_ref_name);
6790 818c7501 2019-07-11 stsp free(branch_ref_name);
6791 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6792 818c7501 2019-07-11 stsp if (commit_ref)
6793 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6794 818c7501 2019-07-11 stsp if (branch_ref)
6795 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6796 818c7501 2019-07-11 stsp if (err) {
6797 818c7501 2019-07-11 stsp free(*commit_id);
6798 818c7501 2019-07-11 stsp *commit_id = NULL;
6799 818c7501 2019-07-11 stsp if (*tmp_branch) {
6800 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6801 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6802 818c7501 2019-07-11 stsp }
6803 818c7501 2019-07-11 stsp if (*new_base_branch) {
6804 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6805 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6806 818c7501 2019-07-11 stsp }
6807 818c7501 2019-07-11 stsp if (*branch) {
6808 818c7501 2019-07-11 stsp got_ref_close(*branch);
6809 818c7501 2019-07-11 stsp *branch = NULL;
6810 818c7501 2019-07-11 stsp }
6811 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6812 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6813 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6814 3e3a69f1 2019-07-25 stsp }
6815 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6816 818c7501 2019-07-11 stsp }
6817 818c7501 2019-07-11 stsp return err;
6818 c4296144 2019-05-09 stsp }
6819 818c7501 2019-07-11 stsp
6820 818c7501 2019-07-11 stsp const struct got_error *
6821 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6822 818c7501 2019-07-11 stsp {
6823 818c7501 2019-07-11 stsp const struct got_error *err;
6824 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6825 818c7501 2019-07-11 stsp
6826 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6827 818c7501 2019-07-11 stsp if (err)
6828 818c7501 2019-07-11 stsp return err;
6829 818c7501 2019-07-11 stsp
6830 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6831 818c7501 2019-07-11 stsp free(tmp_branch_name);
6832 818c7501 2019-07-11 stsp return NULL;
6833 818c7501 2019-07-11 stsp }
6834 818c7501 2019-07-11 stsp
6835 818c7501 2019-07-11 stsp static const struct got_error *
6836 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6837 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6838 818c7501 2019-07-11 stsp {
6839 0ebf8283 2019-07-24 stsp *logmsg = arg;
6840 818c7501 2019-07-11 stsp return NULL;
6841 818c7501 2019-07-11 stsp }
6842 818c7501 2019-07-11 stsp
6843 818c7501 2019-07-11 stsp static const struct got_error *
6844 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6845 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6846 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6847 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6848 818c7501 2019-07-11 stsp {
6849 818c7501 2019-07-11 stsp return NULL;
6850 01757395 2019-07-12 stsp }
6851 01757395 2019-07-12 stsp
6852 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6853 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6854 01757395 2019-07-12 stsp void *progress_arg;
6855 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6856 01757395 2019-07-12 stsp };
6857 01757395 2019-07-12 stsp
6858 01757395 2019-07-12 stsp static const struct got_error *
6859 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6860 01757395 2019-07-12 stsp {
6861 01757395 2019-07-12 stsp const struct got_error *err;
6862 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6863 01757395 2019-07-12 stsp char *p;
6864 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6865 01757395 2019-07-12 stsp
6866 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6867 01757395 2019-07-12 stsp if (err)
6868 01757395 2019-07-12 stsp return err;
6869 01757395 2019-07-12 stsp
6870 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6871 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6872 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6873 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6874 01757395 2019-07-12 stsp return NULL;
6875 01757395 2019-07-12 stsp
6876 01757395 2019-07-12 stsp p = strdup(path);
6877 01757395 2019-07-12 stsp if (p == NULL)
6878 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6879 01757395 2019-07-12 stsp
6880 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6881 01757395 2019-07-12 stsp if (err || new == NULL)
6882 01757395 2019-07-12 stsp free(p);
6883 01757395 2019-07-12 stsp return err;
6884 818c7501 2019-07-11 stsp }
6885 818c7501 2019-07-11 stsp
6886 0ebf8283 2019-07-24 stsp static const struct got_error *
6887 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6888 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6889 818c7501 2019-07-11 stsp {
6890 818c7501 2019-07-11 stsp const struct got_error *err;
6891 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6892 818c7501 2019-07-11 stsp
6893 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6894 818c7501 2019-07-11 stsp if (err) {
6895 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6896 818c7501 2019-07-11 stsp goto done;
6897 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6898 818c7501 2019-07-11 stsp if (err)
6899 818c7501 2019-07-11 stsp goto done;
6900 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6901 818c7501 2019-07-11 stsp if (err)
6902 818c7501 2019-07-11 stsp goto done;
6903 de05890f 2020-03-05 stsp } else if (is_rebase) {
6904 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6905 818c7501 2019-07-11 stsp int cmp;
6906 818c7501 2019-07-11 stsp
6907 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6908 818c7501 2019-07-11 stsp if (err)
6909 818c7501 2019-07-11 stsp goto done;
6910 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6911 818c7501 2019-07-11 stsp free(stored_id);
6912 818c7501 2019-07-11 stsp if (cmp != 0) {
6913 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6914 818c7501 2019-07-11 stsp goto done;
6915 818c7501 2019-07-11 stsp }
6916 818c7501 2019-07-11 stsp }
6917 0ebf8283 2019-07-24 stsp done:
6918 0ebf8283 2019-07-24 stsp if (commit_ref)
6919 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6920 0ebf8283 2019-07-24 stsp return err;
6921 0ebf8283 2019-07-24 stsp }
6922 0ebf8283 2019-07-24 stsp
6923 0ebf8283 2019-07-24 stsp static const struct got_error *
6924 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6925 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6926 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6927 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6928 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6929 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6930 0ebf8283 2019-07-24 stsp {
6931 0ebf8283 2019-07-24 stsp const struct got_error *err;
6932 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6933 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6934 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6935 818c7501 2019-07-11 stsp
6936 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6937 0ebf8283 2019-07-24 stsp
6938 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6939 0ebf8283 2019-07-24 stsp if (err)
6940 0ebf8283 2019-07-24 stsp return err;
6941 0ebf8283 2019-07-24 stsp
6942 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6943 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6944 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6945 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6946 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6947 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6948 818c7501 2019-07-11 stsp if (commit_ref)
6949 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6950 818c7501 2019-07-11 stsp return err;
6951 818c7501 2019-07-11 stsp }
6952 818c7501 2019-07-11 stsp
6953 818c7501 2019-07-11 stsp const struct got_error *
6954 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6955 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6956 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6957 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6958 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6959 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6960 818c7501 2019-07-11 stsp {
6961 0ebf8283 2019-07-24 stsp const struct got_error *err;
6962 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6965 0ebf8283 2019-07-24 stsp if (err)
6966 0ebf8283 2019-07-24 stsp return err;
6967 0ebf8283 2019-07-24 stsp
6968 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6969 0ebf8283 2019-07-24 stsp if (err)
6970 0ebf8283 2019-07-24 stsp goto done;
6971 0ebf8283 2019-07-24 stsp
6972 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6973 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6974 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6975 0ebf8283 2019-07-24 stsp done:
6976 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6977 0ebf8283 2019-07-24 stsp return err;
6978 0ebf8283 2019-07-24 stsp }
6979 0ebf8283 2019-07-24 stsp
6980 0ebf8283 2019-07-24 stsp const struct got_error *
6981 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6982 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6983 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6984 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6985 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6986 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6987 0ebf8283 2019-07-24 stsp {
6988 0ebf8283 2019-07-24 stsp const struct got_error *err;
6989 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6992 0ebf8283 2019-07-24 stsp if (err)
6993 0ebf8283 2019-07-24 stsp return err;
6994 0ebf8283 2019-07-24 stsp
6995 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6996 0ebf8283 2019-07-24 stsp if (err)
6997 0ebf8283 2019-07-24 stsp goto done;
6998 0ebf8283 2019-07-24 stsp
6999 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7000 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7001 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7002 0ebf8283 2019-07-24 stsp done:
7003 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7004 0ebf8283 2019-07-24 stsp return err;
7005 0ebf8283 2019-07-24 stsp }
7006 0ebf8283 2019-07-24 stsp
7007 0ebf8283 2019-07-24 stsp static const struct got_error *
7008 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7009 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7010 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7011 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7012 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7013 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7014 0ebf8283 2019-07-24 stsp {
7015 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7016 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7017 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7018 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7019 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7020 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7021 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7022 818c7501 2019-07-11 stsp
7023 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7024 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7025 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7026 a0e95631 2019-07-12 stsp
7027 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7028 818c7501 2019-07-11 stsp
7029 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7030 a0e95631 2019-07-12 stsp if (err)
7031 3e3a69f1 2019-07-25 stsp return err;
7032 a0e95631 2019-07-12 stsp
7033 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7034 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7035 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7036 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7037 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7038 01757395 2019-07-12 stsp /*
7039 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7040 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7041 6c13b005 2021-09-02 stsp *
7042 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7043 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7044 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7045 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7046 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7047 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7048 01757395 2019-07-12 stsp */
7049 01757395 2019-07-12 stsp if (merged_paths) {
7050 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7051 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7052 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7053 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7054 f2a9dc41 2019-12-13 tracey 0);
7055 01757395 2019-07-12 stsp if (err)
7056 01757395 2019-07-12 stsp goto done;
7057 01757395 2019-07-12 stsp }
7058 01757395 2019-07-12 stsp } else {
7059 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7060 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7061 01757395 2019-07-12 stsp if (err)
7062 01757395 2019-07-12 stsp goto done;
7063 01757395 2019-07-12 stsp }
7064 a0e95631 2019-07-12 stsp
7065 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7066 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7067 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7068 a0e95631 2019-07-12 stsp if (err)
7069 a0e95631 2019-07-12 stsp goto done;
7070 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7071 a0e95631 2019-07-12 stsp goto done;
7072 ff0d2220 2019-07-11 stsp }
7073 818c7501 2019-07-11 stsp
7074 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7075 edd02c5e 2019-07-11 stsp if (err)
7076 edd02c5e 2019-07-11 stsp goto done;
7077 edd02c5e 2019-07-11 stsp
7078 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7079 818c7501 2019-07-11 stsp if (err)
7080 818c7501 2019-07-11 stsp goto done;
7081 818c7501 2019-07-11 stsp
7082 5943eee2 2019-08-13 stsp if (new_logmsg) {
7083 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7084 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7085 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7086 5943eee2 2019-08-13 stsp goto done;
7087 5943eee2 2019-08-13 stsp }
7088 5943eee2 2019-08-13 stsp } else {
7089 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7090 5943eee2 2019-08-13 stsp if (err)
7091 5943eee2 2019-08-13 stsp goto done;
7092 5943eee2 2019-08-13 stsp }
7093 0ebf8283 2019-07-24 stsp
7094 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7095 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7096 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7097 8db00f97 2022-07-22 thomas committer ? committer :
7098 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7099 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7100 a0e95631 2019-07-12 stsp if (err)
7101 a0e95631 2019-07-12 stsp goto done;
7102 a0e95631 2019-07-12 stsp
7103 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7104 a0e95631 2019-07-12 stsp if (err)
7105 a0e95631 2019-07-12 stsp goto done;
7106 a0e95631 2019-07-12 stsp
7107 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7108 a0e95631 2019-07-12 stsp if (err)
7109 a0e95631 2019-07-12 stsp goto done;
7110 a0e95631 2019-07-12 stsp
7111 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7112 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7113 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7114 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7115 a0e95631 2019-07-12 stsp err = sync_err;
7116 818c7501 2019-07-11 stsp done:
7117 a0e95631 2019-07-12 stsp free(fileindex_path);
7118 a0e95631 2019-07-12 stsp free(head_commit_id);
7119 a0e95631 2019-07-12 stsp if (head_ref)
7120 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7121 818c7501 2019-07-11 stsp if (err) {
7122 818c7501 2019-07-11 stsp free(*new_commit_id);
7123 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7124 818c7501 2019-07-11 stsp }
7125 818c7501 2019-07-11 stsp return err;
7126 818c7501 2019-07-11 stsp }
7127 818c7501 2019-07-11 stsp
7128 818c7501 2019-07-11 stsp const struct got_error *
7129 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7130 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7131 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7132 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7133 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7134 be94c032 2023-02-20 thomas struct got_repository *repo)
7135 0ebf8283 2019-07-24 stsp {
7136 0ebf8283 2019-07-24 stsp const struct got_error *err;
7137 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7138 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7139 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp return err;
7144 0ebf8283 2019-07-24 stsp
7145 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7146 0ebf8283 2019-07-24 stsp if (err)
7147 0ebf8283 2019-07-24 stsp goto done;
7148 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7149 0ebf8283 2019-07-24 stsp if (err)
7150 0ebf8283 2019-07-24 stsp goto done;
7151 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7152 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7153 0ebf8283 2019-07-24 stsp goto done;
7154 0ebf8283 2019-07-24 stsp }
7155 0ebf8283 2019-07-24 stsp
7156 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7157 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7158 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7159 0ebf8283 2019-07-24 stsp done:
7160 0ebf8283 2019-07-24 stsp if (commit_ref)
7161 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7162 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7163 0ebf8283 2019-07-24 stsp free(commit_id);
7164 0ebf8283 2019-07-24 stsp return err;
7165 0ebf8283 2019-07-24 stsp }
7166 0ebf8283 2019-07-24 stsp
7167 0ebf8283 2019-07-24 stsp const struct got_error *
7168 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7169 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7170 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7171 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7172 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7173 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7174 0ebf8283 2019-07-24 stsp {
7175 0ebf8283 2019-07-24 stsp const struct got_error *err;
7176 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7177 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7178 0ebf8283 2019-07-24 stsp
7179 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7180 0ebf8283 2019-07-24 stsp if (err)
7181 0ebf8283 2019-07-24 stsp return err;
7182 0ebf8283 2019-07-24 stsp
7183 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7184 0ebf8283 2019-07-24 stsp if (err)
7185 0ebf8283 2019-07-24 stsp goto done;
7186 0ebf8283 2019-07-24 stsp
7187 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7188 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7189 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7190 0ebf8283 2019-07-24 stsp done:
7191 0ebf8283 2019-07-24 stsp if (commit_ref)
7192 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7193 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7194 0ebf8283 2019-07-24 stsp return err;
7195 0ebf8283 2019-07-24 stsp }
7196 0ebf8283 2019-07-24 stsp
7197 0ebf8283 2019-07-24 stsp const struct got_error *
7198 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7199 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7200 818c7501 2019-07-11 stsp {
7201 3e3a69f1 2019-07-25 stsp if (fileindex)
7202 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7203 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7204 69844fba 2019-07-11 stsp }
7205 69844fba 2019-07-11 stsp
7206 69844fba 2019-07-11 stsp static const struct got_error *
7207 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7208 69844fba 2019-07-11 stsp {
7209 69844fba 2019-07-11 stsp const struct got_error *err;
7210 69844fba 2019-07-11 stsp struct got_reference *ref;
7211 69844fba 2019-07-11 stsp
7212 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7213 69844fba 2019-07-11 stsp if (err) {
7214 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7215 69844fba 2019-07-11 stsp return NULL;
7216 69844fba 2019-07-11 stsp return err;
7217 69844fba 2019-07-11 stsp }
7218 69844fba 2019-07-11 stsp
7219 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7220 69844fba 2019-07-11 stsp got_ref_close(ref);
7221 69844fba 2019-07-11 stsp return err;
7222 818c7501 2019-07-11 stsp }
7223 818c7501 2019-07-11 stsp
7224 69844fba 2019-07-11 stsp static const struct got_error *
7225 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7226 69844fba 2019-07-11 stsp {
7227 69844fba 2019-07-11 stsp const struct got_error *err;
7228 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7229 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7230 69844fba 2019-07-11 stsp
7231 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7232 69844fba 2019-07-11 stsp if (err)
7233 69844fba 2019-07-11 stsp goto done;
7234 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7235 69844fba 2019-07-11 stsp if (err)
7236 69844fba 2019-07-11 stsp goto done;
7237 69844fba 2019-07-11 stsp
7238 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7239 69844fba 2019-07-11 stsp if (err)
7240 69844fba 2019-07-11 stsp goto done;
7241 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7242 69844fba 2019-07-11 stsp if (err)
7243 69844fba 2019-07-11 stsp goto done;
7244 69844fba 2019-07-11 stsp
7245 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7246 69844fba 2019-07-11 stsp if (err)
7247 69844fba 2019-07-11 stsp goto done;
7248 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7249 69844fba 2019-07-11 stsp if (err)
7250 69844fba 2019-07-11 stsp goto done;
7251 69844fba 2019-07-11 stsp
7252 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7253 69844fba 2019-07-11 stsp if (err)
7254 69844fba 2019-07-11 stsp goto done;
7255 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7256 69844fba 2019-07-11 stsp if (err)
7257 69844fba 2019-07-11 stsp goto done;
7258 69844fba 2019-07-11 stsp
7259 69844fba 2019-07-11 stsp done:
7260 69844fba 2019-07-11 stsp free(tmp_branch_name);
7261 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7262 69844fba 2019-07-11 stsp free(branch_ref_name);
7263 69844fba 2019-07-11 stsp free(commit_ref_name);
7264 e600f124 2021-03-21 stsp return err;
7265 e600f124 2021-03-21 stsp }
7266 e600f124 2021-03-21 stsp
7267 ef20f542 2022-06-26 thomas static const struct got_error *
7268 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7269 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7270 e600f124 2021-03-21 stsp {
7271 e600f124 2021-03-21 stsp const struct got_error *err;
7272 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7273 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7274 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7275 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7276 e600f124 2021-03-21 stsp char *refname = NULL;
7277 e600f124 2021-03-21 stsp
7278 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7279 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7280 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7281 e600f124 2021-03-21 stsp branch_name += 11;
7282 e600f124 2021-03-21 stsp
7283 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7284 e600f124 2021-03-21 stsp if (err)
7285 e600f124 2021-03-21 stsp return err;
7286 e600f124 2021-03-21 stsp
7287 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7288 e600f124 2021-03-21 stsp new_id_str) == -1) {
7289 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7290 e600f124 2021-03-21 stsp goto done;
7291 e600f124 2021-03-21 stsp }
7292 e600f124 2021-03-21 stsp
7293 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7294 e600f124 2021-03-21 stsp if (err)
7295 e600f124 2021-03-21 stsp goto done;
7296 e600f124 2021-03-21 stsp
7297 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7298 e600f124 2021-03-21 stsp if (err)
7299 e600f124 2021-03-21 stsp goto done;
7300 e600f124 2021-03-21 stsp
7301 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7302 e600f124 2021-03-21 stsp done:
7303 e600f124 2021-03-21 stsp free(new_id_str);
7304 e600f124 2021-03-21 stsp free(refname);
7305 e600f124 2021-03-21 stsp free(old_commit_id);
7306 e600f124 2021-03-21 stsp if (ref)
7307 e600f124 2021-03-21 stsp got_ref_close(ref);
7308 69844fba 2019-07-11 stsp return err;
7309 69844fba 2019-07-11 stsp }
7310 69844fba 2019-07-11 stsp
7311 818c7501 2019-07-11 stsp const struct got_error *
7312 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7313 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7314 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7315 f08f28be 2023-02-03 thomas int create_backup)
7316 818c7501 2019-07-11 stsp {
7317 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7318 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7319 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7320 818c7501 2019-07-11 stsp
7321 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7322 818c7501 2019-07-11 stsp if (err)
7323 818c7501 2019-07-11 stsp return err;
7324 e600f124 2021-03-21 stsp
7325 e600f124 2021-03-21 stsp if (create_backup) {
7326 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7327 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7328 e600f124 2021-03-21 stsp if (err)
7329 e600f124 2021-03-21 stsp goto done;
7330 e600f124 2021-03-21 stsp }
7331 818c7501 2019-07-11 stsp
7332 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7333 818c7501 2019-07-11 stsp if (err)
7334 818c7501 2019-07-11 stsp goto done;
7335 818c7501 2019-07-11 stsp
7336 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7337 818c7501 2019-07-11 stsp if (err)
7338 818c7501 2019-07-11 stsp goto done;
7339 818c7501 2019-07-11 stsp
7340 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7341 818c7501 2019-07-11 stsp if (err)
7342 818c7501 2019-07-11 stsp goto done;
7343 818c7501 2019-07-11 stsp
7344 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7345 a615e0e7 2020-12-16 stsp if (err)
7346 a615e0e7 2020-12-16 stsp goto done;
7347 a615e0e7 2020-12-16 stsp
7348 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7349 a615e0e7 2020-12-16 stsp if (err)
7350 a615e0e7 2020-12-16 stsp goto done;
7351 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7352 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7353 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7354 a615e0e7 2020-12-16 stsp err = sync_err;
7355 818c7501 2019-07-11 stsp done:
7356 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7357 a615e0e7 2020-12-16 stsp free(fileindex_path);
7358 818c7501 2019-07-11 stsp free(new_head_commit_id);
7359 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7360 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7361 818c7501 2019-07-11 stsp err = unlockerr;
7362 818c7501 2019-07-11 stsp return err;
7363 818c7501 2019-07-11 stsp }
7364 8641a332 2023-04-14 thomas
7365 8641a332 2023-04-14 thomas static const struct got_error *
7366 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7367 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7368 8641a332 2023-04-14 thomas struct got_repository *repo)
7369 8641a332 2023-04-14 thomas {
7370 8641a332 2023-04-14 thomas const struct got_error *err;
7371 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7372 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7373 8641a332 2023-04-14 thomas
7374 8641a332 2023-04-14 thomas if (id1) {
7375 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7376 8641a332 2023-04-14 thomas if (err)
7377 8641a332 2023-04-14 thomas goto done;
7378 8641a332 2023-04-14 thomas
7379 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7380 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7381 8641a332 2023-04-14 thomas if (err)
7382 8641a332 2023-04-14 thomas goto done;
7383 8641a332 2023-04-14 thomas }
7384 818c7501 2019-07-11 stsp
7385 8641a332 2023-04-14 thomas if (id2) {
7386 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7387 8641a332 2023-04-14 thomas if (err)
7388 8641a332 2023-04-14 thomas goto done;
7389 8641a332 2023-04-14 thomas
7390 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7391 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7392 8641a332 2023-04-14 thomas if (err)
7393 8641a332 2023-04-14 thomas goto done;
7394 8641a332 2023-04-14 thomas }
7395 8641a332 2023-04-14 thomas
7396 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7397 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7398 8641a332 2023-04-14 thomas if (err)
7399 8641a332 2023-04-14 thomas goto done;
7400 8641a332 2023-04-14 thomas done:
7401 8641a332 2023-04-14 thomas if (commit1)
7402 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7403 8641a332 2023-04-14 thomas if (commit2)
7404 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7405 8641a332 2023-04-14 thomas if (tree1)
7406 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7407 8641a332 2023-04-14 thomas if (tree2)
7408 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7409 8641a332 2023-04-14 thomas return err;
7410 8641a332 2023-04-14 thomas }
7411 8641a332 2023-04-14 thomas
7412 8641a332 2023-04-14 thomas static const struct got_error *
7413 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7414 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7415 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7416 8641a332 2023-04-14 thomas {
7417 8641a332 2023-04-14 thomas const struct got_error *err;
7418 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7419 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7420 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7421 8641a332 2023-04-14 thomas
7422 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7423 8641a332 2023-04-14 thomas
7424 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7425 8641a332 2023-04-14 thomas if (err)
7426 8641a332 2023-04-14 thomas goto done;
7427 8641a332 2023-04-14 thomas
7428 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7429 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7430 8641a332 2023-04-14 thomas
7431 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7432 8641a332 2023-04-14 thomas continue;
7433 8641a332 2023-04-14 thomas
7434 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7435 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7436 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7437 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7438 8641a332 2023-04-14 thomas goto done;
7439 8641a332 2023-04-14 thomas }
7440 8641a332 2023-04-14 thomas } else {
7441 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7442 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7443 8641a332 2023-04-14 thomas goto done;
7444 8641a332 2023-04-14 thomas }
7445 8641a332 2023-04-14 thomas
7446 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7447 8641a332 2023-04-14 thomas path_prefix, abspath);
7448 8641a332 2023-04-14 thomas if (err)
7449 8641a332 2023-04-14 thomas goto done;
7450 8641a332 2023-04-14 thomas free(abspath);
7451 8641a332 2023-04-14 thomas abspath = NULL;
7452 8641a332 2023-04-14 thomas }
7453 8641a332 2023-04-14 thomas
7454 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7455 8641a332 2023-04-14 thomas if (err)
7456 8641a332 2023-04-14 thomas goto done;
7457 8641a332 2023-04-14 thomas wt_path = NULL;
7458 8641a332 2023-04-14 thomas }
7459 8641a332 2023-04-14 thomas
7460 8641a332 2023-04-14 thomas done:
7461 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7462 8641a332 2023-04-14 thomas free(abspath);
7463 8641a332 2023-04-14 thomas free(wt_path);
7464 8641a332 2023-04-14 thomas return err;
7465 8641a332 2023-04-14 thomas }
7466 8641a332 2023-04-14 thomas
7467 8641a332 2023-04-14 thomas static const struct got_error *
7468 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7469 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7470 8641a332 2023-04-14 thomas struct got_repository *repo)
7471 8641a332 2023-04-14 thomas {
7472 8641a332 2023-04-14 thomas const struct got_error *err;
7473 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7474 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7475 8641a332 2023-04-14 thomas
7476 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7477 8641a332 2023-04-14 thomas if (err)
7478 8641a332 2023-04-14 thomas goto done;
7479 8641a332 2023-04-14 thomas
7480 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7481 8641a332 2023-04-14 thomas
7482 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7483 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7484 8641a332 2023-04-14 thomas if (err)
7485 8641a332 2023-04-14 thomas goto done;
7486 8641a332 2023-04-14 thomas done:
7487 8641a332 2023-04-14 thomas if (commit)
7488 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7489 8641a332 2023-04-14 thomas return err;
7490 8641a332 2023-04-14 thomas }
7491 8641a332 2023-04-14 thomas
7492 818c7501 2019-07-11 stsp const struct got_error *
7493 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7494 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7495 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7496 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7497 818c7501 2019-07-11 stsp {
7498 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7499 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7500 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7501 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7502 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7503 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7504 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7505 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7506 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7507 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7508 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7509 818c7501 2019-07-11 stsp
7510 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7511 8641a332 2023-04-14 thomas
7512 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7513 818c7501 2019-07-11 stsp if (err)
7514 818c7501 2019-07-11 stsp return err;
7515 8641a332 2023-04-14 thomas
7516 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7517 8641a332 2023-04-14 thomas if (err)
7518 8641a332 2023-04-14 thomas goto done;
7519 8641a332 2023-04-14 thomas
7520 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7521 8641a332 2023-04-14 thomas if (err)
7522 8641a332 2023-04-14 thomas goto done;
7523 8641a332 2023-04-14 thomas
7524 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7525 8641a332 2023-04-14 thomas if (err)
7526 8641a332 2023-04-14 thomas goto done;
7527 945f9229 2022-04-16 thomas
7528 8641a332 2023-04-14 thomas /*
7529 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7530 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7531 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7532 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7533 8641a332 2023-04-14 thomas */
7534 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7535 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7536 8641a332 2023-04-14 thomas if (err)
7537 8641a332 2023-04-14 thomas goto done;
7538 8641a332 2023-04-14 thomas
7539 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7540 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7541 818c7501 2019-07-11 stsp if (err)
7542 818c7501 2019-07-11 stsp goto done;
7543 818c7501 2019-07-11 stsp
7544 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7545 818c7501 2019-07-11 stsp if (err)
7546 818c7501 2019-07-11 stsp goto done;
7547 818c7501 2019-07-11 stsp
7548 818c7501 2019-07-11 stsp /*
7549 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7550 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7551 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7552 818c7501 2019-07-11 stsp */
7553 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7554 818c7501 2019-07-11 stsp if (err)
7555 818c7501 2019-07-11 stsp goto done;
7556 818c7501 2019-07-11 stsp
7557 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7558 25a102ed 2023-04-14 thomas if (err)
7559 25a102ed 2023-04-14 thomas goto done;
7560 25a102ed 2023-04-14 thomas
7561 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7562 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7563 818c7501 2019-07-11 stsp if (err)
7564 818c7501 2019-07-11 stsp goto done;
7565 818c7501 2019-07-11 stsp
7566 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7567 945f9229 2022-04-16 thomas worktree->path_prefix);
7568 ca355955 2019-07-12 stsp if (err)
7569 ca355955 2019-07-12 stsp goto done;
7570 ca355955 2019-07-12 stsp
7571 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7572 ca355955 2019-07-12 stsp if (err)
7573 ca355955 2019-07-12 stsp goto done;
7574 ca355955 2019-07-12 stsp
7575 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7576 818c7501 2019-07-11 stsp if (err)
7577 818c7501 2019-07-11 stsp goto done;
7578 818c7501 2019-07-11 stsp
7579 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7580 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7581 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7582 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7583 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7584 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7585 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7586 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7587 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7588 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7589 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7590 55bd499d 2019-07-12 stsp if (err)
7591 1f1abb7e 2019-08-08 stsp goto sync;
7592 55bd499d 2019-07-12 stsp
7593 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7594 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7595 ca355955 2019-07-12 stsp sync:
7596 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7597 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7598 ca355955 2019-07-12 stsp err = sync_err;
7599 818c7501 2019-07-11 stsp done:
7600 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7601 818c7501 2019-07-11 stsp got_ref_close(resolved);
7602 a3a2faf2 2019-07-12 stsp free(tree_id);
7603 818c7501 2019-07-11 stsp free(commit_id);
7604 8641a332 2023-04-14 thomas free(merged_commit_id);
7605 945f9229 2022-04-16 thomas if (commit)
7606 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7607 0ebf8283 2019-07-24 stsp if (fileindex)
7608 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7609 0ebf8283 2019-07-24 stsp free(fileindex_path);
7610 8641a332 2023-04-14 thomas free(commit_ref_name);
7611 8641a332 2023-04-14 thomas if (commit_ref)
7612 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7613 0ebf8283 2019-07-24 stsp
7614 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7615 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7616 0ebf8283 2019-07-24 stsp err = unlockerr;
7617 0ebf8283 2019-07-24 stsp return err;
7618 0ebf8283 2019-07-24 stsp }
7619 0ebf8283 2019-07-24 stsp
7620 0ebf8283 2019-07-24 stsp const struct got_error *
7621 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7622 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7623 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7624 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7625 0ebf8283 2019-07-24 stsp {
7626 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7627 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7628 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7629 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7630 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7631 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7632 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7633 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7634 0ebf8283 2019-07-24 stsp
7635 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7636 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7637 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7638 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7639 0ebf8283 2019-07-24 stsp
7640 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7641 0ebf8283 2019-07-24 stsp if (err)
7642 0ebf8283 2019-07-24 stsp return err;
7643 0ebf8283 2019-07-24 stsp
7644 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7645 0ebf8283 2019-07-24 stsp if (err)
7646 0ebf8283 2019-07-24 stsp goto done;
7647 0ebf8283 2019-07-24 stsp
7648 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7649 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7650 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7651 0ebf8283 2019-07-24 stsp &ok_arg);
7652 0ebf8283 2019-07-24 stsp if (err)
7653 0ebf8283 2019-07-24 stsp goto done;
7654 0ebf8283 2019-07-24 stsp
7655 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7656 0ebf8283 2019-07-24 stsp if (err)
7657 0ebf8283 2019-07-24 stsp goto done;
7658 0ebf8283 2019-07-24 stsp
7659 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7660 0ebf8283 2019-07-24 stsp if (err)
7661 0ebf8283 2019-07-24 stsp goto done;
7662 0ebf8283 2019-07-24 stsp
7663 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7664 0ebf8283 2019-07-24 stsp worktree);
7665 0ebf8283 2019-07-24 stsp if (err)
7666 0ebf8283 2019-07-24 stsp goto done;
7667 0ebf8283 2019-07-24 stsp
7668 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7669 0ebf8283 2019-07-24 stsp 0);
7670 0ebf8283 2019-07-24 stsp if (err)
7671 0ebf8283 2019-07-24 stsp goto done;
7672 0ebf8283 2019-07-24 stsp
7673 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7674 0ebf8283 2019-07-24 stsp if (err)
7675 0ebf8283 2019-07-24 stsp goto done;
7676 0ebf8283 2019-07-24 stsp
7677 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
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 = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7682 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7683 0ebf8283 2019-07-24 stsp if (err)
7684 0ebf8283 2019-07-24 stsp goto done;
7685 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7686 0ebf8283 2019-07-24 stsp if (err)
7687 0ebf8283 2019-07-24 stsp goto done;
7688 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7689 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7690 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7691 0ebf8283 2019-07-24 stsp goto done;
7692 0ebf8283 2019-07-24 stsp }
7693 0ebf8283 2019-07-24 stsp
7694 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7695 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7696 0ebf8283 2019-07-24 stsp if (err)
7697 0ebf8283 2019-07-24 stsp goto done;
7698 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7699 0ebf8283 2019-07-24 stsp if (err)
7700 0ebf8283 2019-07-24 stsp goto done;
7701 0ebf8283 2019-07-24 stsp
7702 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7703 0ebf8283 2019-07-24 stsp if (err)
7704 0ebf8283 2019-07-24 stsp goto done;
7705 0ebf8283 2019-07-24 stsp done:
7706 0ebf8283 2019-07-24 stsp free(fileindex_path);
7707 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7708 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7709 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7710 0ebf8283 2019-07-24 stsp if (wt_branch)
7711 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7712 0ebf8283 2019-07-24 stsp if (err) {
7713 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7714 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7715 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7716 0ebf8283 2019-07-24 stsp }
7717 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7718 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7719 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7720 0ebf8283 2019-07-24 stsp }
7721 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7722 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7723 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7724 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7725 3e3a69f1 2019-07-25 stsp }
7726 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7727 0ebf8283 2019-07-24 stsp }
7728 0ebf8283 2019-07-24 stsp return err;
7729 0ebf8283 2019-07-24 stsp }
7730 0ebf8283 2019-07-24 stsp
7731 0ebf8283 2019-07-24 stsp const struct got_error *
7732 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7733 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7734 0ebf8283 2019-07-24 stsp {
7735 3e3a69f1 2019-07-25 stsp if (fileindex)
7736 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7737 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7738 0ebf8283 2019-07-24 stsp }
7739 0ebf8283 2019-07-24 stsp
7740 0ebf8283 2019-07-24 stsp const struct got_error *
7741 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7742 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7743 0ebf8283 2019-07-24 stsp {
7744 0ebf8283 2019-07-24 stsp const struct got_error *err;
7745 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7746 0ebf8283 2019-07-24 stsp
7747 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7748 0ebf8283 2019-07-24 stsp if (err)
7749 0ebf8283 2019-07-24 stsp return err;
7750 0ebf8283 2019-07-24 stsp
7751 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7752 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7753 0ebf8283 2019-07-24 stsp return NULL;
7754 0ebf8283 2019-07-24 stsp }
7755 0ebf8283 2019-07-24 stsp
7756 0ebf8283 2019-07-24 stsp const struct got_error *
7757 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7758 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7759 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7760 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7761 0ebf8283 2019-07-24 stsp {
7762 0ebf8283 2019-07-24 stsp const struct got_error *err;
7763 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7764 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7765 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7766 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7767 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7768 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7769 0ebf8283 2019-07-24 stsp
7770 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7771 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7772 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7773 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7774 0ebf8283 2019-07-24 stsp
7775 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7776 3e3a69f1 2019-07-25 stsp if (err)
7777 3e3a69f1 2019-07-25 stsp return err;
7778 3e3a69f1 2019-07-25 stsp
7779 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7780 3e3a69f1 2019-07-25 stsp if (err)
7781 f032f1f7 2019-08-04 stsp goto done;
7782 f032f1f7 2019-08-04 stsp
7783 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7784 f032f1f7 2019-08-04 stsp &have_staged_files);
7785 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7786 f032f1f7 2019-08-04 stsp goto done;
7787 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7788 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7789 3e3a69f1 2019-07-25 stsp goto done;
7790 f032f1f7 2019-08-04 stsp }
7791 3e3a69f1 2019-07-25 stsp
7792 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7793 0ebf8283 2019-07-24 stsp if (err)
7794 f032f1f7 2019-08-04 stsp goto done;
7795 0ebf8283 2019-07-24 stsp
7796 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7797 0ebf8283 2019-07-24 stsp if (err)
7798 0ebf8283 2019-07-24 stsp goto done;
7799 0ebf8283 2019-07-24 stsp
7800 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7801 0ebf8283 2019-07-24 stsp if (err)
7802 0ebf8283 2019-07-24 stsp goto done;
7803 0ebf8283 2019-07-24 stsp
7804 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7805 0ebf8283 2019-07-24 stsp worktree);
7806 0ebf8283 2019-07-24 stsp if (err)
7807 0ebf8283 2019-07-24 stsp goto done;
7808 0ebf8283 2019-07-24 stsp
7809 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7810 0ebf8283 2019-07-24 stsp if (err)
7811 0ebf8283 2019-07-24 stsp goto done;
7812 0ebf8283 2019-07-24 stsp
7813 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7814 0ebf8283 2019-07-24 stsp if (err)
7815 0ebf8283 2019-07-24 stsp goto done;
7816 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7817 0ebf8283 2019-07-24 stsp if (err)
7818 0ebf8283 2019-07-24 stsp goto done;
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7821 0ebf8283 2019-07-24 stsp if (err)
7822 0ebf8283 2019-07-24 stsp goto done;
7823 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7824 0ebf8283 2019-07-24 stsp if (err)
7825 0ebf8283 2019-07-24 stsp goto done;
7826 0ebf8283 2019-07-24 stsp
7827 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7828 0ebf8283 2019-07-24 stsp if (err)
7829 0ebf8283 2019-07-24 stsp goto done;
7830 0ebf8283 2019-07-24 stsp done:
7831 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7832 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7833 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7834 0ebf8283 2019-07-24 stsp if (commit_ref)
7835 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7836 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7837 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7838 0ebf8283 2019-07-24 stsp if (err) {
7839 0ebf8283 2019-07-24 stsp free(*commit_id);
7840 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7841 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7842 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7843 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7844 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7845 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7846 0ebf8283 2019-07-24 stsp }
7847 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7848 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7849 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7850 3e3a69f1 2019-07-25 stsp }
7851 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7852 0ebf8283 2019-07-24 stsp }
7853 0ebf8283 2019-07-24 stsp return err;
7854 0ebf8283 2019-07-24 stsp }
7855 0ebf8283 2019-07-24 stsp
7856 0ebf8283 2019-07-24 stsp static const struct got_error *
7857 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7858 0ebf8283 2019-07-24 stsp {
7859 0ebf8283 2019-07-24 stsp const struct got_error *err;
7860 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7861 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7862 0ebf8283 2019-07-24 stsp
7863 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7864 0ebf8283 2019-07-24 stsp if (err)
7865 0ebf8283 2019-07-24 stsp goto done;
7866 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7867 0ebf8283 2019-07-24 stsp if (err)
7868 0ebf8283 2019-07-24 stsp goto done;
7869 0ebf8283 2019-07-24 stsp
7870 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7871 0ebf8283 2019-07-24 stsp worktree);
7872 0ebf8283 2019-07-24 stsp if (err)
7873 0ebf8283 2019-07-24 stsp goto done;
7874 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7875 0ebf8283 2019-07-24 stsp if (err)
7876 0ebf8283 2019-07-24 stsp goto done;
7877 0ebf8283 2019-07-24 stsp
7878 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7879 0ebf8283 2019-07-24 stsp if (err)
7880 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7882 0ebf8283 2019-07-24 stsp if (err)
7883 0ebf8283 2019-07-24 stsp goto done;
7884 0ebf8283 2019-07-24 stsp
7885 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_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(commit_ref_name, repo);
7889 0ebf8283 2019-07-24 stsp if (err)
7890 0ebf8283 2019-07-24 stsp goto done;
7891 0ebf8283 2019-07-24 stsp done:
7892 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7893 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7894 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7895 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7896 0ebf8283 2019-07-24 stsp return err;
7897 0ebf8283 2019-07-24 stsp }
7898 0ebf8283 2019-07-24 stsp
7899 0ebf8283 2019-07-24 stsp const struct got_error *
7900 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7901 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7902 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7903 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7904 0ebf8283 2019-07-24 stsp {
7905 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7906 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7907 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7908 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7909 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7910 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7911 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7912 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7913 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7914 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7915 0ebf8283 2019-07-24 stsp
7916 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7917 8641a332 2023-04-14 thomas
7918 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7919 0ebf8283 2019-07-24 stsp if (err)
7920 0ebf8283 2019-07-24 stsp return err;
7921 945f9229 2022-04-16 thomas
7922 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7923 8641a332 2023-04-14 thomas if (err)
7924 8641a332 2023-04-14 thomas goto done;
7925 8641a332 2023-04-14 thomas
7926 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7927 8641a332 2023-04-14 thomas if (err) {
7928 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7929 8641a332 2023-04-14 thomas goto done;
7930 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7931 8641a332 2023-04-14 thomas commit_ref = NULL;
7932 8641a332 2023-04-14 thomas }
7933 8641a332 2023-04-14 thomas
7934 8641a332 2023-04-14 thomas if (commit_ref) {
7935 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7936 8641a332 2023-04-14 thomas if (err)
7937 8641a332 2023-04-14 thomas goto done;
7938 8641a332 2023-04-14 thomas
7939 8641a332 2023-04-14 thomas /*
7940 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7941 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7942 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7943 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7944 8641a332 2023-04-14 thomas */
7945 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7946 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7947 8641a332 2023-04-14 thomas if (err)
7948 8641a332 2023-04-14 thomas goto done;
7949 8641a332 2023-04-14 thomas }
7950 8641a332 2023-04-14 thomas
7951 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7952 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7953 0ebf8283 2019-07-24 stsp if (err)
7954 0ebf8283 2019-07-24 stsp goto done;
7955 0ebf8283 2019-07-24 stsp
7956 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7957 0ebf8283 2019-07-24 stsp if (err)
7958 0ebf8283 2019-07-24 stsp goto done;
7959 0ebf8283 2019-07-24 stsp
7960 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7961 0ebf8283 2019-07-24 stsp if (err)
7962 0ebf8283 2019-07-24 stsp goto done;
7963 0ebf8283 2019-07-24 stsp
7964 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7965 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7966 25a102ed 2023-04-14 thomas if (err)
7967 25a102ed 2023-04-14 thomas goto done;
7968 25a102ed 2023-04-14 thomas
7969 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7970 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7971 0ebf8283 2019-07-24 stsp if (err)
7972 0ebf8283 2019-07-24 stsp goto done;
7973 0ebf8283 2019-07-24 stsp
7974 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7975 0ebf8283 2019-07-24 stsp if (err)
7976 0ebf8283 2019-07-24 stsp goto done;
7977 0ebf8283 2019-07-24 stsp
7978 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7979 0ebf8283 2019-07-24 stsp if (err)
7980 0ebf8283 2019-07-24 stsp goto done;
7981 0ebf8283 2019-07-24 stsp
7982 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7983 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7984 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7985 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7986 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7987 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7988 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7989 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7990 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7991 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7992 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7993 0ebf8283 2019-07-24 stsp if (err)
7994 1f1abb7e 2019-08-08 stsp goto sync;
7995 0ebf8283 2019-07-24 stsp
7996 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7997 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7998 0ebf8283 2019-07-24 stsp sync:
7999 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8000 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8001 0ebf8283 2019-07-24 stsp err = sync_err;
8002 0ebf8283 2019-07-24 stsp done:
8003 8641a332 2023-04-14 thomas if (resolved)
8004 8641a332 2023-04-14 thomas got_ref_close(resolved);
8005 8641a332 2023-04-14 thomas if (commit_ref)
8006 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8007 8641a332 2023-04-14 thomas free(merged_commit_id);
8008 0ebf8283 2019-07-24 stsp free(tree_id);
8009 818c7501 2019-07-11 stsp free(fileindex_path);
8010 8641a332 2023-04-14 thomas free(commit_ref_name);
8011 818c7501 2019-07-11 stsp
8012 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8013 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8014 818c7501 2019-07-11 stsp err = unlockerr;
8015 818c7501 2019-07-11 stsp return err;
8016 818c7501 2019-07-11 stsp }
8017 0ebf8283 2019-07-24 stsp
8018 0ebf8283 2019-07-24 stsp const struct got_error *
8019 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8020 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8021 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8022 0ebf8283 2019-07-24 stsp {
8023 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8024 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8025 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8026 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8027 0ebf8283 2019-07-24 stsp
8028 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8029 0ebf8283 2019-07-24 stsp if (err)
8030 0ebf8283 2019-07-24 stsp return err;
8031 0ebf8283 2019-07-24 stsp
8032 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8033 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8034 e600f124 2021-03-21 stsp if (err)
8035 e600f124 2021-03-21 stsp goto done;
8036 e600f124 2021-03-21 stsp
8037 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8038 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8039 0ebf8283 2019-07-24 stsp if (err)
8040 0ebf8283 2019-07-24 stsp goto done;
8041 0ebf8283 2019-07-24 stsp
8042 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8043 0ebf8283 2019-07-24 stsp if (err)
8044 0ebf8283 2019-07-24 stsp goto done;
8045 0ebf8283 2019-07-24 stsp
8046 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8047 0ebf8283 2019-07-24 stsp if (err)
8048 0ebf8283 2019-07-24 stsp goto done;
8049 0ebf8283 2019-07-24 stsp
8050 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8051 0ebf8283 2019-07-24 stsp if (err)
8052 0ebf8283 2019-07-24 stsp goto done;
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8055 a615e0e7 2020-12-16 stsp if (err)
8056 a615e0e7 2020-12-16 stsp goto done;
8057 a615e0e7 2020-12-16 stsp
8058 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8059 a615e0e7 2020-12-16 stsp if (err)
8060 a615e0e7 2020-12-16 stsp goto done;
8061 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8062 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8063 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8064 a615e0e7 2020-12-16 stsp err = sync_err;
8065 0ebf8283 2019-07-24 stsp done:
8066 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8067 a615e0e7 2020-12-16 stsp free(fileindex_path);
8068 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8069 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8070 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8071 0ebf8283 2019-07-24 stsp err = unlockerr;
8072 0ebf8283 2019-07-24 stsp return err;
8073 0ebf8283 2019-07-24 stsp }
8074 0ebf8283 2019-07-24 stsp
8075 0ebf8283 2019-07-24 stsp const struct got_error *
8076 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8077 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8078 0ebf8283 2019-07-24 stsp {
8079 0ebf8283 2019-07-24 stsp const struct got_error *err;
8080 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8081 0ebf8283 2019-07-24 stsp
8082 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8083 0ebf8283 2019-07-24 stsp if (err)
8084 0ebf8283 2019-07-24 stsp return err;
8085 0ebf8283 2019-07-24 stsp
8086 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8087 0ebf8283 2019-07-24 stsp if (err)
8088 0ebf8283 2019-07-24 stsp goto done;
8089 0ebf8283 2019-07-24 stsp
8090 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8091 0ebf8283 2019-07-24 stsp done:
8092 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8093 2822a352 2019-10-15 stsp return err;
8094 2822a352 2019-10-15 stsp }
8095 2822a352 2019-10-15 stsp
8096 2822a352 2019-10-15 stsp const struct got_error *
8097 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8098 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8099 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8100 2822a352 2019-10-15 stsp struct got_repository *repo)
8101 2822a352 2019-10-15 stsp {
8102 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8103 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8104 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8105 2822a352 2019-10-15 stsp
8106 2822a352 2019-10-15 stsp *fileindex = NULL;
8107 2822a352 2019-10-15 stsp *branch_ref = NULL;
8108 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8109 2822a352 2019-10-15 stsp
8110 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8111 2822a352 2019-10-15 stsp if (err)
8112 2822a352 2019-10-15 stsp return err;
8113 2822a352 2019-10-15 stsp
8114 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8115 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8116 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8117 2822a352 2019-10-15 stsp "update -b or different branch name required");
8118 2822a352 2019-10-15 stsp goto done;
8119 2822a352 2019-10-15 stsp }
8120 2822a352 2019-10-15 stsp
8121 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8122 2822a352 2019-10-15 stsp if (err)
8123 2822a352 2019-10-15 stsp goto done;
8124 2822a352 2019-10-15 stsp
8125 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8126 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8127 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8128 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8129 2822a352 2019-10-15 stsp &ok_arg);
8130 2822a352 2019-10-15 stsp if (err)
8131 2822a352 2019-10-15 stsp goto done;
8132 2822a352 2019-10-15 stsp
8133 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8134 2822a352 2019-10-15 stsp if (err)
8135 2822a352 2019-10-15 stsp goto done;
8136 2822a352 2019-10-15 stsp
8137 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8138 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8139 2822a352 2019-10-15 stsp done:
8140 2822a352 2019-10-15 stsp if (err) {
8141 2822a352 2019-10-15 stsp if (*branch_ref) {
8142 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8143 2822a352 2019-10-15 stsp *branch_ref = NULL;
8144 2822a352 2019-10-15 stsp }
8145 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8146 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8147 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8148 2822a352 2019-10-15 stsp }
8149 2822a352 2019-10-15 stsp if (*fileindex) {
8150 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8151 2822a352 2019-10-15 stsp *fileindex = NULL;
8152 2822a352 2019-10-15 stsp }
8153 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8154 2822a352 2019-10-15 stsp }
8155 0cb83759 2019-08-03 stsp return err;
8156 0cb83759 2019-08-03 stsp }
8157 0cb83759 2019-08-03 stsp
8158 2822a352 2019-10-15 stsp const struct got_error *
8159 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8160 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8161 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8162 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8163 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8164 2822a352 2019-10-15 stsp {
8165 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8166 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8167 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8168 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8169 2822a352 2019-10-15 stsp
8170 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8171 2822a352 2019-10-15 stsp if (err)
8172 2822a352 2019-10-15 stsp goto done;
8173 2822a352 2019-10-15 stsp
8174 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8175 2822a352 2019-10-15 stsp if (err)
8176 2822a352 2019-10-15 stsp goto done;
8177 2822a352 2019-10-15 stsp
8178 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8179 945f9229 2022-04-16 thomas if (err)
8180 945f9229 2022-04-16 thomas goto done;
8181 945f9229 2022-04-16 thomas
8182 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8183 2822a352 2019-10-15 stsp worktree->path_prefix);
8184 2822a352 2019-10-15 stsp if (err)
8185 2822a352 2019-10-15 stsp goto done;
8186 2822a352 2019-10-15 stsp
8187 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8188 2822a352 2019-10-15 stsp if (err)
8189 2822a352 2019-10-15 stsp goto done;
8190 2822a352 2019-10-15 stsp
8191 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8192 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8193 2822a352 2019-10-15 stsp if (err)
8194 2822a352 2019-10-15 stsp goto sync;
8195 2822a352 2019-10-15 stsp
8196 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8197 2822a352 2019-10-15 stsp if (err)
8198 2822a352 2019-10-15 stsp goto sync;
8199 2822a352 2019-10-15 stsp
8200 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8201 6e210706 2021-01-22 stsp if (err)
8202 6e210706 2021-01-22 stsp goto sync;
8203 6e210706 2021-01-22 stsp
8204 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8205 2822a352 2019-10-15 stsp sync:
8206 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8207 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8208 2822a352 2019-10-15 stsp err = sync_err;
8209 2822a352 2019-10-15 stsp
8210 2822a352 2019-10-15 stsp done:
8211 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8212 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8213 2822a352 2019-10-15 stsp err = unlockerr;
8214 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8215 2822a352 2019-10-15 stsp
8216 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8217 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8218 2822a352 2019-10-15 stsp err = unlockerr;
8219 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8220 2822a352 2019-10-15 stsp
8221 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8222 2822a352 2019-10-15 stsp free(fileindex_path);
8223 2822a352 2019-10-15 stsp free(tree_id);
8224 945f9229 2022-04-16 thomas if (commit)
8225 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8226 2822a352 2019-10-15 stsp
8227 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8228 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8229 2822a352 2019-10-15 stsp err = unlockerr;
8230 2822a352 2019-10-15 stsp return err;
8231 2822a352 2019-10-15 stsp }
8232 2822a352 2019-10-15 stsp
8233 2822a352 2019-10-15 stsp const struct got_error *
8234 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8235 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8236 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8237 2822a352 2019-10-15 stsp {
8238 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8239 8b692cd0 2019-10-21 stsp
8240 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8241 8b692cd0 2019-10-21 stsp
8242 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8243 8b692cd0 2019-10-21 stsp
8244 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8245 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8246 8b692cd0 2019-10-21 stsp err = unlockerr;
8247 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8248 8b692cd0 2019-10-21 stsp
8249 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8250 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8251 8b692cd0 2019-10-21 stsp err = unlockerr;
8252 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8253 10604dce 2021-09-24 thomas
8254 10604dce 2021-09-24 thomas return err;
8255 10604dce 2021-09-24 thomas }
8256 10604dce 2021-09-24 thomas
8257 10604dce 2021-09-24 thomas const struct got_error *
8258 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8259 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8260 10604dce 2021-09-24 thomas {
8261 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8262 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8263 10604dce 2021-09-24 thomas
8264 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8265 10604dce 2021-09-24 thomas if (err)
8266 10604dce 2021-09-24 thomas goto done;
8267 8b692cd0 2019-10-21 stsp
8268 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8269 10604dce 2021-09-24 thomas
8270 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8271 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8272 10604dce 2021-09-24 thomas err = sync_err;
8273 10604dce 2021-09-24 thomas done:
8274 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8275 10604dce 2021-09-24 thomas free(fileindex_path);
8276 8b692cd0 2019-10-21 stsp return err;
8277 2822a352 2019-10-15 stsp }
8278 2822a352 2019-10-15 stsp
8279 10604dce 2021-09-24 thomas static const struct got_error *
8280 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8281 10604dce 2021-09-24 thomas {
8282 10604dce 2021-09-24 thomas const struct got_error *err;
8283 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8284 10604dce 2021-09-24 thomas
8285 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8286 10604dce 2021-09-24 thomas if (err)
8287 10604dce 2021-09-24 thomas goto done;
8288 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8289 10604dce 2021-09-24 thomas if (err)
8290 10604dce 2021-09-24 thomas goto done;
8291 10604dce 2021-09-24 thomas
8292 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8293 10604dce 2021-09-24 thomas if (err)
8294 10604dce 2021-09-24 thomas goto done;
8295 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8296 10604dce 2021-09-24 thomas if (err)
8297 10604dce 2021-09-24 thomas goto done;
8298 10604dce 2021-09-24 thomas
8299 10604dce 2021-09-24 thomas done:
8300 10604dce 2021-09-24 thomas free(branch_refname);
8301 10604dce 2021-09-24 thomas free(commit_refname);
8302 10604dce 2021-09-24 thomas return err;
8303 10604dce 2021-09-24 thomas }
8304 10604dce 2021-09-24 thomas
8305 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8306 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8307 10604dce 2021-09-24 thomas const char *branch_name;
8308 10604dce 2021-09-24 thomas };
8309 10604dce 2021-09-24 thomas
8310 10604dce 2021-09-24 thomas static const struct got_error *
8311 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8312 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8313 10604dce 2021-09-24 thomas {
8314 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8315 10604dce 2021-09-24 thomas
8316 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8317 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8318 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8319 10604dce 2021-09-24 thomas
8320 10604dce 2021-09-24 thomas return NULL;
8321 10604dce 2021-09-24 thomas }
8322 10604dce 2021-09-24 thomas
8323 10604dce 2021-09-24 thomas
8324 10604dce 2021-09-24 thomas const struct got_error *
8325 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8326 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8327 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8328 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8329 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8330 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8331 10604dce 2021-09-24 thomas {
8332 10604dce 2021-09-24 thomas const struct got_error *err;
8333 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8334 10604dce 2021-09-24 thomas
8335 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8336 10604dce 2021-09-24 thomas if (err)
8337 10604dce 2021-09-24 thomas goto done;
8338 10604dce 2021-09-24 thomas
8339 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8340 10604dce 2021-09-24 thomas worktree);
8341 10604dce 2021-09-24 thomas if (err)
8342 10604dce 2021-09-24 thomas goto done;
8343 10604dce 2021-09-24 thomas
8344 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8345 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8346 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8347 10604dce 2021-09-24 thomas done:
8348 10604dce 2021-09-24 thomas free(fileindex_path);
8349 10604dce 2021-09-24 thomas return err;
8350 10604dce 2021-09-24 thomas }
8351 10604dce 2021-09-24 thomas
8352 10604dce 2021-09-24 thomas const struct got_error *
8353 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8354 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8355 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8356 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8357 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8358 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8359 ae1e948a 2021-09-28 thomas
8360 10604dce 2021-09-24 thomas {
8361 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8362 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8363 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8364 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8365 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8366 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8367 10604dce 2021-09-24 thomas int have_staged_files = 0;
8368 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8369 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8370 10604dce 2021-09-24 thomas
8371 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8372 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8373 10604dce 2021-09-24 thomas
8374 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8375 10604dce 2021-09-24 thomas
8376 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8377 10604dce 2021-09-24 thomas if (err)
8378 10604dce 2021-09-24 thomas goto done;
8379 10604dce 2021-09-24 thomas
8380 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8381 10604dce 2021-09-24 thomas if (err)
8382 10604dce 2021-09-24 thomas goto done;
8383 10604dce 2021-09-24 thomas
8384 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8385 10604dce 2021-09-24 thomas if (err)
8386 10604dce 2021-09-24 thomas goto done;
8387 10604dce 2021-09-24 thomas
8388 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8389 10604dce 2021-09-24 thomas &have_staged_files);
8390 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8391 10604dce 2021-09-24 thomas goto done;
8392 10604dce 2021-09-24 thomas if (have_staged_files) {
8393 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8394 10604dce 2021-09-24 thomas goto done;
8395 10604dce 2021-09-24 thomas }
8396 10604dce 2021-09-24 thomas
8397 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8398 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8399 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8400 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8401 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8402 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8403 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8404 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8405 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8406 10604dce 2021-09-24 thomas if (err)
8407 10604dce 2021-09-24 thomas goto done;
8408 10604dce 2021-09-24 thomas
8409 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8410 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8411 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8412 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8413 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8414 10604dce 2021-09-24 thomas if (err)
8415 10604dce 2021-09-24 thomas goto done;
8416 10604dce 2021-09-24 thomas
8417 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8418 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8419 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8420 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8421 10604dce 2021-09-24 thomas err = sync_err;
8422 10604dce 2021-09-24 thomas done:
8423 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8424 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8425 21c2d8be 2023-01-10 thomas
8426 10604dce 2021-09-24 thomas free_commitable(ct);
8427 10604dce 2021-09-24 thomas }
8428 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8429 10604dce 2021-09-24 thomas free(fileindex_path);
8430 10604dce 2021-09-24 thomas return err;
8431 10604dce 2021-09-24 thomas }
8432 10604dce 2021-09-24 thomas
8433 10604dce 2021-09-24 thomas const struct got_error *
8434 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8435 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8436 10604dce 2021-09-24 thomas {
8437 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8438 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8439 10604dce 2021-09-24 thomas
8440 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8441 10604dce 2021-09-24 thomas if (err)
8442 10604dce 2021-09-24 thomas goto done;
8443 10604dce 2021-09-24 thomas
8444 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8445 10604dce 2021-09-24 thomas if (err)
8446 10604dce 2021-09-24 thomas goto done;
8447 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8448 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8449 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8450 10604dce 2021-09-24 thomas err = sync_err;
8451 10604dce 2021-09-24 thomas done:
8452 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8453 10604dce 2021-09-24 thomas free(fileindex_path);
8454 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8455 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8456 10604dce 2021-09-24 thomas err = unlockerr;
8457 10604dce 2021-09-24 thomas return err;
8458 10604dce 2021-09-24 thomas }
8459 10604dce 2021-09-24 thomas
8460 10604dce 2021-09-24 thomas const struct got_error *
8461 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8462 10604dce 2021-09-24 thomas struct got_repository *repo)
8463 10604dce 2021-09-24 thomas {
8464 10604dce 2021-09-24 thomas const struct got_error *err;
8465 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8466 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8467 10604dce 2021-09-24 thomas
8468 10604dce 2021-09-24 thomas *in_progress = 0;
8469 10604dce 2021-09-24 thomas
8470 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8471 10604dce 2021-09-24 thomas if (err)
8472 10604dce 2021-09-24 thomas return err;
8473 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8474 dfe86d1f 2021-09-24 thomas free(branch_refname);
8475 10604dce 2021-09-24 thomas if (err) {
8476 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8477 10604dce 2021-09-24 thomas return err;
8478 10604dce 2021-09-24 thomas } else
8479 10604dce 2021-09-24 thomas *in_progress = 1;
8480 10604dce 2021-09-24 thomas
8481 10604dce 2021-09-24 thomas return NULL;
8482 10604dce 2021-09-24 thomas }
8483 10604dce 2021-09-24 thomas
8484 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8485 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8486 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8487 10604dce 2021-09-24 thomas {
8488 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8489 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8490 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8491 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8492 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8493 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8494 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8495 10604dce 2021-09-24 thomas
8496 10604dce 2021-09-24 thomas *fileindex = NULL;
8497 10604dce 2021-09-24 thomas
8498 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8499 10604dce 2021-09-24 thomas if (err)
8500 10604dce 2021-09-24 thomas return err;
8501 10604dce 2021-09-24 thomas
8502 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8503 10604dce 2021-09-24 thomas if (err)
8504 10604dce 2021-09-24 thomas goto done;
8505 10604dce 2021-09-24 thomas
8506 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8507 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8508 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8509 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8510 10604dce 2021-09-24 thomas &ok_arg);
8511 10604dce 2021-09-24 thomas if (err)
8512 10604dce 2021-09-24 thomas goto done;
8513 10604dce 2021-09-24 thomas
8514 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8515 10604dce 2021-09-24 thomas if (err)
8516 10604dce 2021-09-24 thomas return err;
8517 10604dce 2021-09-24 thomas
8518 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
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 = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8523 10604dce 2021-09-24 thomas 0);
8524 10604dce 2021-09-24 thomas if (err)
8525 10604dce 2021-09-24 thomas goto done;
8526 10604dce 2021-09-24 thomas
8527 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8528 10604dce 2021-09-24 thomas if (err)
8529 10604dce 2021-09-24 thomas goto done;
8530 10604dce 2021-09-24 thomas
8531 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8532 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8533 10604dce 2021-09-24 thomas goto done;
8534 10604dce 2021-09-24 thomas }
8535 10604dce 2021-09-24 thomas
8536 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8537 10604dce 2021-09-24 thomas if (err)
8538 10604dce 2021-09-24 thomas goto done;
8539 10604dce 2021-09-24 thomas
8540 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8541 10604dce 2021-09-24 thomas if (err)
8542 10604dce 2021-09-24 thomas goto done;
8543 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8544 10604dce 2021-09-24 thomas if (err)
8545 10604dce 2021-09-24 thomas goto done;
8546 10604dce 2021-09-24 thomas
8547 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8548 10604dce 2021-09-24 thomas if (err)
8549 10604dce 2021-09-24 thomas goto done;
8550 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8551 10604dce 2021-09-24 thomas if (err)
8552 10604dce 2021-09-24 thomas goto done;
8553 10604dce 2021-09-24 thomas
8554 10604dce 2021-09-24 thomas done:
8555 10604dce 2021-09-24 thomas free(branch_refname);
8556 10604dce 2021-09-24 thomas free(commit_refname);
8557 10604dce 2021-09-24 thomas free(fileindex_path);
8558 10604dce 2021-09-24 thomas if (branch_ref)
8559 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8560 10604dce 2021-09-24 thomas if (commit_ref)
8561 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8562 10604dce 2021-09-24 thomas if (wt_branch)
8563 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8564 10604dce 2021-09-24 thomas free(wt_branch_tip);
8565 10604dce 2021-09-24 thomas if (err) {
8566 10604dce 2021-09-24 thomas if (*fileindex) {
8567 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8568 10604dce 2021-09-24 thomas *fileindex = NULL;
8569 10604dce 2021-09-24 thomas }
8570 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8571 10604dce 2021-09-24 thomas }
8572 10604dce 2021-09-24 thomas return err;
8573 10604dce 2021-09-24 thomas }
8574 10604dce 2021-09-24 thomas
8575 10604dce 2021-09-24 thomas const struct got_error *
8576 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8577 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8578 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8579 10604dce 2021-09-24 thomas {
8580 10604dce 2021-09-24 thomas const struct got_error *err;
8581 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8582 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8583 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8584 10604dce 2021-09-24 thomas int have_staged_files = 0;
8585 10604dce 2021-09-24 thomas
8586 10604dce 2021-09-24 thomas *branch_name = NULL;
8587 10604dce 2021-09-24 thomas *branch_tip = NULL;
8588 10604dce 2021-09-24 thomas *fileindex = NULL;
8589 10604dce 2021-09-24 thomas
8590 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8591 10604dce 2021-09-24 thomas if (err)
8592 10604dce 2021-09-24 thomas return err;
8593 10604dce 2021-09-24 thomas
8594 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
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 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8599 10604dce 2021-09-24 thomas &have_staged_files);
8600 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8601 10604dce 2021-09-24 thomas goto done;
8602 10604dce 2021-09-24 thomas if (have_staged_files) {
8603 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8604 10604dce 2021-09-24 thomas goto done;
8605 10604dce 2021-09-24 thomas }
8606 10604dce 2021-09-24 thomas
8607 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8608 10604dce 2021-09-24 thomas if (err)
8609 10604dce 2021-09-24 thomas goto done;
8610 10604dce 2021-09-24 thomas
8611 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8612 10604dce 2021-09-24 thomas if (err)
8613 10604dce 2021-09-24 thomas goto done;
8614 10604dce 2021-09-24 thomas
8615 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8616 10604dce 2021-09-24 thomas if (err)
8617 10604dce 2021-09-24 thomas goto done;
8618 10604dce 2021-09-24 thomas
8619 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8620 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8621 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8622 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8623 10604dce 2021-09-24 thomas goto done;
8624 10604dce 2021-09-24 thomas }
8625 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8626 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8627 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8628 10604dce 2021-09-24 thomas goto done;
8629 10604dce 2021-09-24 thomas }
8630 10604dce 2021-09-24 thomas
8631 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8632 10604dce 2021-09-24 thomas if (err)
8633 10604dce 2021-09-24 thomas goto done;
8634 10604dce 2021-09-24 thomas
8635 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8636 10604dce 2021-09-24 thomas if (err)
8637 10604dce 2021-09-24 thomas goto done;
8638 10604dce 2021-09-24 thomas done:
8639 10604dce 2021-09-24 thomas free(commit_refname);
8640 10604dce 2021-09-24 thomas free(branch_refname);
8641 10604dce 2021-09-24 thomas free(fileindex_path);
8642 10604dce 2021-09-24 thomas if (commit_ref)
8643 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8644 10604dce 2021-09-24 thomas if (branch_ref)
8645 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8646 10604dce 2021-09-24 thomas if (err) {
8647 10604dce 2021-09-24 thomas if (*branch_name) {
8648 10604dce 2021-09-24 thomas free(*branch_name);
8649 10604dce 2021-09-24 thomas *branch_name = NULL;
8650 10604dce 2021-09-24 thomas }
8651 10604dce 2021-09-24 thomas free(*branch_tip);
8652 10604dce 2021-09-24 thomas *branch_tip = NULL;
8653 10604dce 2021-09-24 thomas if (*fileindex) {
8654 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8655 10604dce 2021-09-24 thomas *fileindex = NULL;
8656 10604dce 2021-09-24 thomas }
8657 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8658 10604dce 2021-09-24 thomas }
8659 10604dce 2021-09-24 thomas return err;
8660 10604dce 2021-09-24 thomas }
8661 10604dce 2021-09-24 thomas
8662 10604dce 2021-09-24 thomas const struct got_error *
8663 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8664 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8665 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8666 10604dce 2021-09-24 thomas {
8667 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8668 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8669 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8670 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8671 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8672 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8673 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8674 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8675 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8676 8641a332 2023-04-14 thomas
8677 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8678 8641a332 2023-04-14 thomas
8679 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8680 8641a332 2023-04-14 thomas if (err)
8681 8641a332 2023-04-14 thomas goto done;
8682 8641a332 2023-04-14 thomas
8683 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8684 8641a332 2023-04-14 thomas if (err)
8685 8641a332 2023-04-14 thomas goto done;
8686 8641a332 2023-04-14 thomas
8687 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8688 8641a332 2023-04-14 thomas if (err)
8689 8641a332 2023-04-14 thomas goto done;
8690 8641a332 2023-04-14 thomas
8691 8641a332 2023-04-14 thomas /*
8692 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8693 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8694 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8695 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8696 8641a332 2023-04-14 thomas */
8697 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8698 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8699 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8700 8641a332 2023-04-14 thomas if (err)
8701 8641a332 2023-04-14 thomas goto done;
8702 10604dce 2021-09-24 thomas
8703 8641a332 2023-04-14 thomas
8704 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8705 945f9229 2022-04-16 thomas worktree->base_commit_id);
8706 945f9229 2022-04-16 thomas if (err)
8707 945f9229 2022-04-16 thomas goto done;
8708 945f9229 2022-04-16 thomas
8709 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8710 945f9229 2022-04-16 thomas worktree->path_prefix);
8711 10604dce 2021-09-24 thomas if (err)
8712 10604dce 2021-09-24 thomas goto done;
8713 10604dce 2021-09-24 thomas
8714 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8715 10604dce 2021-09-24 thomas if (err)
8716 10604dce 2021-09-24 thomas goto done;
8717 10604dce 2021-09-24 thomas
8718 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8719 10604dce 2021-09-24 thomas if (err)
8720 10604dce 2021-09-24 thomas goto done;
8721 10604dce 2021-09-24 thomas
8722 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8723 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8724 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8725 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8726 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8727 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8728 10604dce 2021-09-24 thomas rfa.repo = repo;
8729 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8730 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8731 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8732 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8733 10604dce 2021-09-24 thomas if (err)
8734 10604dce 2021-09-24 thomas goto sync;
8735 10604dce 2021-09-24 thomas
8736 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8737 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8738 10604dce 2021-09-24 thomas sync:
8739 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8740 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8741 10604dce 2021-09-24 thomas err = sync_err;
8742 10604dce 2021-09-24 thomas done:
8743 10604dce 2021-09-24 thomas free(tree_id);
8744 8641a332 2023-04-14 thomas free(merged_commit_id);
8745 945f9229 2022-04-16 thomas if (commit)
8746 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8747 10604dce 2021-09-24 thomas if (fileindex)
8748 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8749 10604dce 2021-09-24 thomas free(fileindex_path);
8750 8641a332 2023-04-14 thomas if (commit_ref)
8751 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8752 8641a332 2023-04-14 thomas free(commit_ref_name);
8753 10604dce 2021-09-24 thomas
8754 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8755 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8756 10604dce 2021-09-24 thomas err = unlockerr;
8757 10604dce 2021-09-24 thomas return err;
8758 10604dce 2021-09-24 thomas }
8759 10604dce 2021-09-24 thomas
8760 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8761 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8762 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8763 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8764 2db2652d 2019-08-07 stsp struct got_repository *repo;
8765 2db2652d 2019-08-07 stsp int have_changes;
8766 2db2652d 2019-08-07 stsp };
8767 2db2652d 2019-08-07 stsp
8768 ef20f542 2022-06-26 thomas static const struct got_error *
8769 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8770 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8771 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8772 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8773 735ef5ac 2019-08-03 stsp {
8774 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8775 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8776 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8777 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8778 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8779 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8780 8b13ce36 2019-08-08 stsp
8781 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8782 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8783 8b13ce36 2019-08-08 stsp return NULL;
8784 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8785 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8786 735ef5ac 2019-08-03 stsp
8787 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8788 735ef5ac 2019-08-03 stsp if (ie == NULL)
8789 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8790 735ef5ac 2019-08-03 stsp
8791 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8792 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8793 735ef5ac 2019-08-03 stsp relpath) == -1)
8794 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8795 735ef5ac 2019-08-03 stsp
8796 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8797 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8798 43010591 2023-02-17 thomas &base_commit_id, ie);
8799 735ef5ac 2019-08-03 stsp }
8800 735ef5ac 2019-08-03 stsp
8801 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8802 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8803 3aa5969e 2019-08-06 stsp goto done;
8804 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8805 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8806 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8807 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8808 735ef5ac 2019-08-03 stsp goto done;
8809 3aa5969e 2019-08-06 stsp }
8810 735ef5ac 2019-08-03 stsp
8811 2db2652d 2019-08-07 stsp a->have_changes = 1;
8812 2db2652d 2019-08-07 stsp
8813 735ef5ac 2019-08-03 stsp p = in_repo_path;
8814 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8815 735ef5ac 2019-08-03 stsp p++;
8816 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8817 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8818 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8819 735ef5ac 2019-08-03 stsp done:
8820 735ef5ac 2019-08-03 stsp free(in_repo_path);
8821 dc424a06 2019-08-07 stsp return err;
8822 dc424a06 2019-08-07 stsp }
8823 dc424a06 2019-08-07 stsp
8824 2db2652d 2019-08-07 stsp struct stage_path_arg {
8825 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8826 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8827 2db2652d 2019-08-07 stsp struct got_repository *repo;
8828 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8829 2db2652d 2019-08-07 stsp void *status_arg;
8830 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8831 2db2652d 2019-08-07 stsp void *patch_arg;
8832 7b5dc508 2019-10-28 stsp int staged_something;
8833 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8834 2db2652d 2019-08-07 stsp };
8835 2db2652d 2019-08-07 stsp
8836 2db2652d 2019-08-07 stsp static const struct got_error *
8837 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8838 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8839 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8840 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8841 0cb83759 2019-08-03 stsp {
8842 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8843 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8844 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8845 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8846 0cb83759 2019-08-03 stsp uint32_t stage;
8847 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8848 0aeb8099 2020-07-23 stsp struct stat sb;
8849 8b13ce36 2019-08-08 stsp
8850 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8851 8b13ce36 2019-08-08 stsp return NULL;
8852 0cb83759 2019-08-03 stsp
8853 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8854 d3e7c587 2019-08-03 stsp if (ie == NULL)
8855 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8856 0cb83759 2019-08-03 stsp
8857 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8858 2db2652d 2019-08-07 stsp relpath)== -1)
8859 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8860 0cb83759 2019-08-03 stsp
8861 0cb83759 2019-08-03 stsp switch (status) {
8862 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8863 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8864 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8865 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8866 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8867 0aeb8099 2020-07-23 stsp break;
8868 0aeb8099 2020-07-23 stsp }
8869 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8870 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8871 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8872 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8873 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8874 dc424a06 2019-08-07 stsp if (err)
8875 dc424a06 2019-08-07 stsp break;
8876 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8877 dc424a06 2019-08-07 stsp break;
8878 dc424a06 2019-08-07 stsp } else {
8879 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8880 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8881 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8882 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8883 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8884 dc424a06 2019-08-07 stsp break;
8885 dc424a06 2019-08-07 stsp }
8886 dc424a06 2019-08-07 stsp }
8887 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8888 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8889 0cb83759 2019-08-03 stsp if (err)
8890 dc424a06 2019-08-07 stsp break;
8891 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8892 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8893 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8894 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8895 0cb83759 2019-08-03 stsp else
8896 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8897 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8898 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8899 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8900 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8901 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8902 35213c7c 2020-07-23 stsp ssize_t target_len;
8903 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8904 35213c7c 2020-07-23 stsp sizeof(target_path));
8905 35213c7c 2020-07-23 stsp if (target_len == -1) {
8906 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8907 35213c7c 2020-07-23 stsp ondisk_path);
8908 35213c7c 2020-07-23 stsp break;
8909 35213c7c 2020-07-23 stsp }
8910 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8911 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8912 35213c7c 2020-07-23 stsp a->worktree->root_path);
8913 35213c7c 2020-07-23 stsp if (err)
8914 35213c7c 2020-07-23 stsp break;
8915 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8916 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8917 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8918 35213c7c 2020-07-23 stsp break;
8919 35213c7c 2020-07-23 stsp }
8920 35213c7c 2020-07-23 stsp }
8921 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8922 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8923 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8924 35213c7c 2020-07-23 stsp else
8925 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8926 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8927 0aeb8099 2020-07-23 stsp } else {
8928 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8929 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8930 0aeb8099 2020-07-23 stsp }
8931 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8932 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8933 dc424a06 2019-08-07 stsp break;
8934 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8935 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8936 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8937 f289c82c 2022-06-13 thomas if (err)
8938 f289c82c 2022-06-13 thomas break;
8939 f289c82c 2022-06-13 thomas /*
8940 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8941 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8942 f289c82c 2022-06-13 thomas */
8943 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8944 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8945 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8946 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8947 f289c82c 2022-06-13 thomas }
8948 0cb83759 2019-08-03 stsp break;
8949 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8950 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8951 d3e7c587 2019-08-03 stsp break;
8952 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8953 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8954 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8955 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8956 dc424a06 2019-08-07 stsp if (err)
8957 dc424a06 2019-08-07 stsp break;
8958 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8959 88f33a19 2019-08-08 stsp break;
8960 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8961 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8962 dc424a06 2019-08-07 stsp break;
8963 88f33a19 2019-08-08 stsp }
8964 dc424a06 2019-08-07 stsp }
8965 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8966 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8967 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8968 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8969 dc424a06 2019-08-07 stsp break;
8970 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8971 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8972 12463d8b 2019-12-13 stsp de_name);
8973 0cb83759 2019-08-03 stsp break;
8974 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8975 d3e7c587 2019-08-03 stsp break;
8976 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8977 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8978 ebf48fd5 2019-08-03 stsp break;
8979 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8980 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8981 2a06fe5f 2019-08-24 stsp break;
8982 0cb83759 2019-08-03 stsp default:
8983 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8984 537ac44b 2019-08-03 stsp break;
8985 0cb83759 2019-08-03 stsp }
8986 dc424a06 2019-08-07 stsp
8987 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8988 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8989 dc424a06 2019-08-07 stsp free(path_content);
8990 2db2652d 2019-08-07 stsp free(ondisk_path);
8991 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8992 0ebf8283 2019-07-24 stsp return err;
8993 0ebf8283 2019-07-24 stsp }
8994 0cb83759 2019-08-03 stsp
8995 0cb83759 2019-08-03 stsp const struct got_error *
8996 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8997 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8998 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8999 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9000 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9001 0cb83759 2019-08-03 stsp {
9002 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9003 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9004 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9005 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9006 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9007 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9008 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9009 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9010 0cb83759 2019-08-03 stsp
9011 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9012 0cb83759 2019-08-03 stsp if (err)
9013 0cb83759 2019-08-03 stsp return err;
9014 0cb83759 2019-08-03 stsp
9015 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9016 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9017 735ef5ac 2019-08-03 stsp if (err)
9018 735ef5ac 2019-08-03 stsp goto done;
9019 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9020 735ef5ac 2019-08-03 stsp if (err)
9021 735ef5ac 2019-08-03 stsp goto done;
9022 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9023 0cb83759 2019-08-03 stsp if (err)
9024 0cb83759 2019-08-03 stsp goto done;
9025 0cb83759 2019-08-03 stsp
9026 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9027 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9028 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9029 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9030 2db2652d 2019-08-07 stsp oka.repo = repo;
9031 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9032 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9033 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9034 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9035 735ef5ac 2019-08-03 stsp if (err)
9036 735ef5ac 2019-08-03 stsp goto done;
9037 735ef5ac 2019-08-03 stsp }
9038 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9039 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9040 2db2652d 2019-08-07 stsp goto done;
9041 2db2652d 2019-08-07 stsp }
9042 735ef5ac 2019-08-03 stsp
9043 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9044 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9045 2db2652d 2019-08-07 stsp spa.repo = repo;
9046 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9047 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9048 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9049 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9050 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9051 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9052 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9053 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9054 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9055 42005733 2019-08-03 stsp if (err)
9056 2db2652d 2019-08-07 stsp goto done;
9057 7b5dc508 2019-10-28 stsp }
9058 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9059 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9060 7b5dc508 2019-10-28 stsp goto done;
9061 0cb83759 2019-08-03 stsp }
9062 0cb83759 2019-08-03 stsp
9063 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9064 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9065 0cb83759 2019-08-03 stsp err = sync_err;
9066 0cb83759 2019-08-03 stsp done:
9067 735ef5ac 2019-08-03 stsp if (head_ref)
9068 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9069 735ef5ac 2019-08-03 stsp free(head_commit_id);
9070 0cb83759 2019-08-03 stsp free(fileindex_path);
9071 0cb83759 2019-08-03 stsp if (fileindex)
9072 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9073 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9074 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9075 0cb83759 2019-08-03 stsp err = unlockerr;
9076 0cb83759 2019-08-03 stsp return err;
9077 0cb83759 2019-08-03 stsp }
9078 ad493afc 2019-08-03 stsp
9079 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9080 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9081 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9082 ad493afc 2019-08-03 stsp struct got_repository *repo;
9083 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9084 ad493afc 2019-08-03 stsp void *progress_arg;
9085 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9086 2e1f37b0 2019-08-08 stsp void *patch_arg;
9087 ad493afc 2019-08-03 stsp };
9088 2e1f37b0 2019-08-08 stsp
9089 2e1f37b0 2019-08-08 stsp static const struct got_error *
9090 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9091 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9092 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9093 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9094 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9095 2e1f37b0 2019-08-08 stsp {
9096 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9097 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9098 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9099 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9100 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9101 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9102 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9103 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9104 2e1f37b0 2019-08-08 stsp
9105 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9106 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9107 2e1f37b0 2019-08-08 stsp
9108 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9109 2e1f37b0 2019-08-08 stsp if (err)
9110 2e1f37b0 2019-08-08 stsp return err;
9111 f4ae6ddb 2022-07-01 thomas
9112 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9113 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9114 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9115 f4ae6ddb 2022-07-01 thomas goto done;
9116 f4ae6ddb 2022-07-01 thomas }
9117 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9118 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9119 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9120 f4ae6ddb 2022-07-01 thomas goto done;
9121 f4ae6ddb 2022-07-01 thomas }
9122 f4ae6ddb 2022-07-01 thomas
9123 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9124 2e1f37b0 2019-08-08 stsp if (err)
9125 2e1f37b0 2019-08-08 stsp goto done;
9126 2e1f37b0 2019-08-08 stsp
9127 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9128 2e1f37b0 2019-08-08 stsp if (err)
9129 2e1f37b0 2019-08-08 stsp goto done;
9130 2e1f37b0 2019-08-08 stsp
9131 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9132 2e1f37b0 2019-08-08 stsp if (err)
9133 2e1f37b0 2019-08-08 stsp goto done;
9134 2e1f37b0 2019-08-08 stsp
9135 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9136 f4ae6ddb 2022-07-01 thomas fd2);
9137 2e1f37b0 2019-08-08 stsp if (err)
9138 2e1f37b0 2019-08-08 stsp goto done;
9139 2e1f37b0 2019-08-08 stsp
9140 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9141 2e1f37b0 2019-08-08 stsp if (err)
9142 2e1f37b0 2019-08-08 stsp goto done;
9143 ad493afc 2019-08-03 stsp
9144 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9145 2e1f37b0 2019-08-08 stsp if (err)
9146 2e1f37b0 2019-08-08 stsp goto done;
9147 2e1f37b0 2019-08-08 stsp
9148 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9149 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9150 2e1f37b0 2019-08-08 stsp if (err)
9151 2e1f37b0 2019-08-08 stsp goto done;
9152 2e1f37b0 2019-08-08 stsp
9153 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9154 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9155 2e1f37b0 2019-08-08 stsp if (err)
9156 2e1f37b0 2019-08-08 stsp goto done;
9157 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9158 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9159 2e1f37b0 2019-08-08 stsp if (err)
9160 2e1f37b0 2019-08-08 stsp goto done;
9161 2e1f37b0 2019-08-08 stsp
9162 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9163 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9164 2e1f37b0 2019-08-08 stsp goto done;
9165 2e1f37b0 2019-08-08 stsp }
9166 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9167 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9168 2e1f37b0 2019-08-08 stsp goto done;
9169 2e1f37b0 2019-08-08 stsp }
9170 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9171 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9172 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9173 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9174 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9175 fe621944 2020-11-10 stsp nchanges++;
9176 fe621944 2020-11-10 stsp }
9177 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9178 2e1f37b0 2019-08-08 stsp int choice;
9179 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9180 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9181 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9182 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9183 2e1f37b0 2019-08-08 stsp if (err)
9184 2e1f37b0 2019-08-08 stsp goto done;
9185 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9186 2e1f37b0 2019-08-08 stsp have_content = 1;
9187 19e4b907 2019-08-08 stsp else
9188 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9189 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9190 2e1f37b0 2019-08-08 stsp break;
9191 2e1f37b0 2019-08-08 stsp }
9192 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9193 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9194 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9195 2e1f37b0 2019-08-08 stsp done:
9196 2e1f37b0 2019-08-08 stsp free(label1);
9197 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9198 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9199 2e1f37b0 2019-08-08 stsp if (blob)
9200 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9201 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9202 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9203 2e1f37b0 2019-08-08 stsp if (staged_blob)
9204 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9205 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9206 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9207 fe621944 2020-11-10 stsp err = free_err;
9208 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9209 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9210 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9211 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9212 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9213 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9214 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9215 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9216 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9217 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9218 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9219 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9220 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9221 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9222 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9223 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9224 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9225 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9226 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9227 2e1f37b0 2019-08-08 stsp }
9228 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9229 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9230 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9231 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9232 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9233 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9234 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9235 2e1f37b0 2019-08-08 stsp }
9236 2e1f37b0 2019-08-08 stsp free(path1);
9237 2e1f37b0 2019-08-08 stsp free(path2);
9238 fda8017d 2020-07-23 stsp return err;
9239 fda8017d 2020-07-23 stsp }
9240 fda8017d 2020-07-23 stsp
9241 fda8017d 2020-07-23 stsp static const struct got_error *
9242 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9243 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9244 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9245 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9246 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9247 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9248 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9249 fda8017d 2020-07-23 stsp {
9250 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9251 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9252 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9253 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9254 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9255 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9256 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9257 fda8017d 2020-07-23 stsp struct stat sb;
9258 fda8017d 2020-07-23 stsp
9259 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9260 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9261 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9262 fda8017d 2020-07-23 stsp if (err)
9263 fda8017d 2020-07-23 stsp return err;
9264 fda8017d 2020-07-23 stsp
9265 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9266 fda8017d 2020-07-23 stsp return NULL;
9267 fda8017d 2020-07-23 stsp
9268 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9269 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9270 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9271 fda8017d 2020-07-23 stsp if (err)
9272 fda8017d 2020-07-23 stsp goto done;
9273 fda8017d 2020-07-23 stsp }
9274 fda8017d 2020-07-23 stsp
9275 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9276 fda8017d 2020-07-23 stsp if (f == NULL) {
9277 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9278 fda8017d 2020-07-23 stsp path_unstaged_content);
9279 fda8017d 2020-07-23 stsp goto done;
9280 fda8017d 2020-07-23 stsp }
9281 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9282 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9283 fda8017d 2020-07-23 stsp goto done;
9284 fda8017d 2020-07-23 stsp }
9285 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9286 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9287 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9288 fda8017d 2020-07-23 stsp size_t r;
9289 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9290 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9291 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9292 fda8017d 2020-07-23 stsp goto done;
9293 fda8017d 2020-07-23 stsp }
9294 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9295 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9296 fda8017d 2020-07-23 stsp goto done;
9297 fda8017d 2020-07-23 stsp }
9298 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9299 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9300 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9301 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9302 fda8017d 2020-07-23 stsp progress_arg);
9303 fda8017d 2020-07-23 stsp } else {
9304 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9305 67a66647 2021-05-31 stsp
9306 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9307 67a66647 2021-05-31 stsp if (err)
9308 67a66647 2021-05-31 stsp return err;
9309 67a66647 2021-05-31 stsp
9310 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9311 67a66647 2021-05-31 stsp parent) == -1) {
9312 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9313 67a66647 2021-05-31 stsp base_path = NULL;
9314 67a66647 2021-05-31 stsp goto done;
9315 67a66647 2021-05-31 stsp }
9316 67a66647 2021-05-31 stsp
9317 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9318 fc2a50f2 2022-11-01 thomas base_path, "");
9319 67a66647 2021-05-31 stsp if (err)
9320 67a66647 2021-05-31 stsp goto done;
9321 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9322 67a66647 2021-05-31 stsp blob_base);
9323 67a66647 2021-05-31 stsp if (err)
9324 67a66647 2021-05-31 stsp goto done;
9325 67a66647 2021-05-31 stsp
9326 b6b86fd1 2022-08-30 thomas /*
9327 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9328 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9329 eec2f5a9 2021-06-03 stsp */
9330 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9331 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9332 eec2f5a9 2021-06-03 stsp ondisk_path);
9333 eec2f5a9 2021-06-03 stsp if (err)
9334 eec2f5a9 2021-06-03 stsp goto done;
9335 eec2f5a9 2021-06-03 stsp } else {
9336 eec2f5a9 2021-06-03 stsp int fd;
9337 06340621 2021-12-31 thomas fd = open(ondisk_path,
9338 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9339 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9340 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9341 eec2f5a9 2021-06-03 stsp goto done;
9342 eec2f5a9 2021-06-03 stsp }
9343 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9344 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9345 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9346 eec2f5a9 2021-06-03 stsp close(fd);
9347 eec2f5a9 2021-06-03 stsp goto done;
9348 eec2f5a9 2021-06-03 stsp }
9349 eec2f5a9 2021-06-03 stsp }
9350 eec2f5a9 2021-06-03 stsp
9351 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9352 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9353 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9354 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9355 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9356 fda8017d 2020-07-23 stsp }
9357 fda8017d 2020-07-23 stsp if (err)
9358 fda8017d 2020-07-23 stsp goto done;
9359 fda8017d 2020-07-23 stsp
9360 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9361 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9362 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9363 2ac8aa02 2020-11-09 stsp } else {
9364 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9365 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9366 2ac8aa02 2020-11-09 stsp }
9367 fda8017d 2020-07-23 stsp done:
9368 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9369 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9370 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9371 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9372 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9373 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9374 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9375 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9376 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9377 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9378 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9379 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9380 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9381 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9382 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9383 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9384 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9385 67a66647 2021-05-31 stsp free(blob_base_path);
9386 67a66647 2021-05-31 stsp free(parent);
9387 67a66647 2021-05-31 stsp free(base_path);
9388 2e1f37b0 2019-08-08 stsp return err;
9389 2e1f37b0 2019-08-08 stsp }
9390 2e1f37b0 2019-08-08 stsp
9391 ad493afc 2019-08-03 stsp static const struct got_error *
9392 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9393 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9394 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9395 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9396 ad493afc 2019-08-03 stsp {
9397 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9398 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9399 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9400 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9401 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9402 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9403 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9404 9bc94a15 2019-08-03 stsp struct stat sb;
9405 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9406 ad493afc 2019-08-03 stsp
9407 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9408 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9409 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9410 2e1f37b0 2019-08-08 stsp return NULL;
9411 2e1f37b0 2019-08-08 stsp
9412 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9413 ad493afc 2019-08-03 stsp if (ie == NULL)
9414 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9415 9bc94a15 2019-08-03 stsp
9416 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9417 9bc94a15 2019-08-03 stsp == -1)
9418 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9419 ad493afc 2019-08-03 stsp
9420 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9421 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9422 f69721c3 2019-10-21 stsp if (err)
9423 f69721c3 2019-10-21 stsp goto done;
9424 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9425 f69721c3 2019-10-21 stsp id_str) == -1) {
9426 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9427 f4ae6ddb 2022-07-01 thomas goto done;
9428 f4ae6ddb 2022-07-01 thomas }
9429 f4ae6ddb 2022-07-01 thomas
9430 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9431 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9432 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9433 f4ae6ddb 2022-07-01 thomas goto done;
9434 f4ae6ddb 2022-07-01 thomas }
9435 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9436 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9437 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9438 f69721c3 2019-10-21 stsp goto done;
9439 f69721c3 2019-10-21 stsp }
9440 f69721c3 2019-10-21 stsp
9441 ad493afc 2019-08-03 stsp switch (staged_status) {
9442 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9443 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9444 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9445 ad493afc 2019-08-03 stsp if (err)
9446 ad493afc 2019-08-03 stsp break;
9447 ad493afc 2019-08-03 stsp /* fall through */
9448 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9449 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9450 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9451 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9452 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9453 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9454 2e1f37b0 2019-08-08 stsp if (err)
9455 2e1f37b0 2019-08-08 stsp break;
9456 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9457 2e1f37b0 2019-08-08 stsp break;
9458 2e1f37b0 2019-08-08 stsp } else {
9459 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9460 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9461 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9462 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9463 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9464 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9465 2e1f37b0 2019-08-08 stsp }
9466 2e1f37b0 2019-08-08 stsp }
9467 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9468 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9469 ad493afc 2019-08-03 stsp if (err)
9470 ad493afc 2019-08-03 stsp break;
9471 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9472 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9473 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9474 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9475 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9476 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9477 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9478 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9479 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9480 ea7786be 2020-07-23 stsp break;
9481 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9482 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9483 36bf999c 2020-07-23 stsp char *staged_target;
9484 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9485 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9486 36bf999c 2020-07-23 stsp if (err)
9487 36bf999c 2020-07-23 stsp goto done;
9488 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9489 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9490 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9491 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9492 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9493 36bf999c 2020-07-23 stsp free(staged_target);
9494 dfe9fba0 2020-07-23 stsp } else {
9495 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9496 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9497 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9498 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9499 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9500 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9501 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9502 dfe9fba0 2020-07-23 stsp }
9503 ea7786be 2020-07-23 stsp break;
9504 ea7786be 2020-07-23 stsp default:
9505 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9506 ea7786be 2020-07-23 stsp break;
9507 ea7786be 2020-07-23 stsp }
9508 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9509 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9510 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9511 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9512 2ac8aa02 2020-11-09 stsp }
9513 ad493afc 2019-08-03 stsp break;
9514 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9515 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9516 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9517 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9518 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9519 2e1f37b0 2019-08-08 stsp if (err)
9520 2e1f37b0 2019-08-08 stsp break;
9521 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9522 2e1f37b0 2019-08-08 stsp break;
9523 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9524 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9525 2e1f37b0 2019-08-08 stsp break;
9526 2e1f37b0 2019-08-08 stsp }
9527 2e1f37b0 2019-08-08 stsp }
9528 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9529 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9530 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9531 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9532 9bc94a15 2019-08-03 stsp if (err)
9533 9bc94a15 2019-08-03 stsp break;
9534 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9535 ad493afc 2019-08-03 stsp break;
9536 ad493afc 2019-08-03 stsp }
9537 f69721c3 2019-10-21 stsp done:
9538 ad493afc 2019-08-03 stsp free(ondisk_path);
9539 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9540 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9541 ad493afc 2019-08-03 stsp if (blob_base)
9542 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9543 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9544 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9545 ad493afc 2019-08-03 stsp if (blob_staged)
9546 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9547 f69721c3 2019-10-21 stsp free(id_str);
9548 f69721c3 2019-10-21 stsp free(label_orig);
9549 ad493afc 2019-08-03 stsp return err;
9550 ad493afc 2019-08-03 stsp }
9551 ad493afc 2019-08-03 stsp
9552 ad493afc 2019-08-03 stsp const struct got_error *
9553 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9554 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9555 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9556 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9557 ad493afc 2019-08-03 stsp struct got_repository *repo)
9558 ad493afc 2019-08-03 stsp {
9559 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9560 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9561 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9562 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9563 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9564 ad493afc 2019-08-03 stsp
9565 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9566 ad493afc 2019-08-03 stsp if (err)
9567 ad493afc 2019-08-03 stsp return err;
9568 ad493afc 2019-08-03 stsp
9569 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9570 ad493afc 2019-08-03 stsp if (err)
9571 ad493afc 2019-08-03 stsp goto done;
9572 ad493afc 2019-08-03 stsp
9573 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9574 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9575 ad493afc 2019-08-03 stsp upa.repo = repo;
9576 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9577 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9578 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9579 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9580 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9581 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9582 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9583 ad493afc 2019-08-03 stsp if (err)
9584 ad493afc 2019-08-03 stsp goto done;
9585 ad493afc 2019-08-03 stsp }
9586 ad493afc 2019-08-03 stsp
9587 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9588 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9589 ad493afc 2019-08-03 stsp err = sync_err;
9590 ad493afc 2019-08-03 stsp done:
9591 ad493afc 2019-08-03 stsp free(fileindex_path);
9592 ad493afc 2019-08-03 stsp if (fileindex)
9593 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9594 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9595 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9596 ad493afc 2019-08-03 stsp err = unlockerr;
9597 ad493afc 2019-08-03 stsp return err;
9598 ad493afc 2019-08-03 stsp }
9599 b2118c49 2020-07-28 stsp
9600 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9601 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9602 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9603 b2118c49 2020-07-28 stsp void *info_arg;
9604 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9605 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9606 b2118c49 2020-07-28 stsp void *cancel_arg;
9607 b2118c49 2020-07-28 stsp };
9608 b2118c49 2020-07-28 stsp
9609 b2118c49 2020-07-28 stsp static const struct got_error *
9610 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9611 b2118c49 2020-07-28 stsp {
9612 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9613 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9614 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9615 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9616 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9617 b2118c49 2020-07-28 stsp int stage;
9618 b2118c49 2020-07-28 stsp
9619 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9620 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9621 b2118c49 2020-07-28 stsp
9622 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9623 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9624 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9625 b2118c49 2020-07-28 stsp break;
9626 b2118c49 2020-07-28 stsp }
9627 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9628 b2118c49 2020-07-28 stsp return NULL;
9629 b2118c49 2020-07-28 stsp
9630 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9631 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9632 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9633 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9634 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9635 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9636 43010591 2023-02-17 thomas &staged_blob_id, ie);
9637 b2118c49 2020-07-28 stsp }
9638 b2118c49 2020-07-28 stsp
9639 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9640 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9641 b2118c49 2020-07-28 stsp
9642 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9643 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9644 b2118c49 2020-07-28 stsp }
9645 b2118c49 2020-07-28 stsp
9646 b2118c49 2020-07-28 stsp const struct got_error *
9647 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9648 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9649 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9650 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9651 b2118c49 2020-07-28 stsp
9652 b2118c49 2020-07-28 stsp {
9653 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9654 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9655 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9656 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9657 b2118c49 2020-07-28 stsp
9658 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9659 b2118c49 2020-07-28 stsp if (err)
9660 b2118c49 2020-07-28 stsp return err;
9661 b2118c49 2020-07-28 stsp
9662 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9663 b2118c49 2020-07-28 stsp if (err)
9664 b2118c49 2020-07-28 stsp goto done;
9665 b2118c49 2020-07-28 stsp
9666 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9667 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9668 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9669 b2118c49 2020-07-28 stsp arg.paths = paths;
9670 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9671 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9672 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9673 b2118c49 2020-07-28 stsp &arg);
9674 b2118c49 2020-07-28 stsp done:
9675 b2118c49 2020-07-28 stsp free(fileindex_path);
9676 b2118c49 2020-07-28 stsp if (fileindex)
9677 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9678 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9679 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9680 b2118c49 2020-07-28 stsp err = unlockerr;
9681 b2118c49 2020-07-28 stsp return err;
9682 b2118c49 2020-07-28 stsp }
9683 814624e7 2022-03-22 thomas
9684 814624e7 2022-03-22 thomas static const struct got_error *
9685 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9686 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9687 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9688 814624e7 2022-03-22 thomas {
9689 814624e7 2022-03-22 thomas const struct got_error *err;
9690 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9691 814624e7 2022-03-22 thomas struct stat sb;
9692 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9693 814624e7 2022-03-22 thomas
9694 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9695 814624e7 2022-03-22 thomas if (err)
9696 814624e7 2022-03-22 thomas return err;
9697 814624e7 2022-03-22 thomas
9698 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9699 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9700 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9701 814624e7 2022-03-22 thomas
9702 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9703 814624e7 2022-03-22 thomas if (ie) {
9704 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9705 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9706 12de5570 2022-05-01 thomas repo);
9707 814624e7 2022-03-22 thomas if (err)
9708 814624e7 2022-03-22 thomas goto done;
9709 814624e7 2022-03-22 thomas } else {
9710 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9711 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9712 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9713 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9714 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9715 814624e7 2022-03-22 thomas ondisk_path);
9716 814624e7 2022-03-22 thomas goto done;
9717 814624e7 2022-03-22 thomas }
9718 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9719 814624e7 2022-03-22 thomas }
9720 814624e7 2022-03-22 thomas }
9721 814624e7 2022-03-22 thomas
9722 814624e7 2022-03-22 thomas done:
9723 814624e7 2022-03-22 thomas free(ondisk_path);
9724 814624e7 2022-03-22 thomas return err;
9725 814624e7 2022-03-22 thomas }
9726 814624e7 2022-03-22 thomas
9727 814624e7 2022-03-22 thomas static const struct got_error *
9728 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9729 814624e7 2022-03-22 thomas unsigned char staged_status)
9730 814624e7 2022-03-22 thomas {
9731 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9732 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9733 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9734 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9735 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9736 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9737 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9738 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9739 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9740 814624e7 2022-03-22 thomas return NULL;
9741 814624e7 2022-03-22 thomas }
9742 814624e7 2022-03-22 thomas
9743 814624e7 2022-03-22 thomas static const struct got_error *
9744 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9745 814624e7 2022-03-22 thomas {
9746 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9747 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9748 814624e7 2022-03-22 thomas return NULL;
9749 814624e7 2022-03-22 thomas }
9750 814624e7 2022-03-22 thomas
9751 814624e7 2022-03-22 thomas static const struct got_error *
9752 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9753 814624e7 2022-03-22 thomas unsigned char staged_status)
9754 814624e7 2022-03-22 thomas {
9755 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9756 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9757 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9758 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9759 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9760 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9762 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9763 814624e7 2022-03-22 thomas return NULL;
9764 814624e7 2022-03-22 thomas }
9765 814624e7 2022-03-22 thomas
9766 814624e7 2022-03-22 thomas const struct got_error *
9767 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9768 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9769 814624e7 2022-03-22 thomas {
9770 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9771 814624e7 2022-03-22 thomas }
9772 814624e7 2022-03-22 thomas
9773 814624e7 2022-03-22 thomas const struct got_error *
9774 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9775 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9776 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9777 814624e7 2022-03-22 thomas {
9778 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9779 814624e7 2022-03-22 thomas int file_renamed = 0;
9780 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9781 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9782 814624e7 2022-03-22 thomas
9783 814624e7 2022-03-22 thomas *oldpath = NULL;
9784 814624e7 2022-03-22 thomas *newpath = NULL;
9785 814624e7 2022-03-22 thomas
9786 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9787 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9788 814624e7 2022-03-22 thomas if (err)
9789 814624e7 2022-03-22 thomas goto done;
9790 814624e7 2022-03-22 thomas
9791 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9792 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9793 814624e7 2022-03-22 thomas if (err)
9794 814624e7 2022-03-22 thomas goto done;
9795 814624e7 2022-03-22 thomas
9796 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9797 814624e7 2022-03-22 thomas file_renamed = 1;
9798 814624e7 2022-03-22 thomas
9799 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9800 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9801 814624e7 2022-03-22 thomas else if (file_renamed) {
9802 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9803 814624e7 2022-03-22 thomas if (err == NULL)
9804 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9805 814624e7 2022-03-22 thomas } else if (old == NULL)
9806 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9807 814624e7 2022-03-22 thomas else
9808 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9809 814624e7 2022-03-22 thomas
9810 814624e7 2022-03-22 thomas done:
9811 814624e7 2022-03-22 thomas if (err) {
9812 814624e7 2022-03-22 thomas free(*oldpath);
9813 814624e7 2022-03-22 thomas *oldpath = NULL;
9814 814624e7 2022-03-22 thomas free(*newpath);
9815 814624e7 2022-03-22 thomas *newpath = NULL;
9816 814624e7 2022-03-22 thomas }
9817 814624e7 2022-03-22 thomas return err;
9818 814624e7 2022-03-22 thomas }
9819 814624e7 2022-03-22 thomas
9820 814624e7 2022-03-22 thomas const struct got_error *
9821 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9822 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9823 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9824 814624e7 2022-03-22 thomas {
9825 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9826 5e22b737 2022-05-31 thomas
9827 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9828 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9829 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9830 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9831 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9832 5e22b737 2022-05-31 thomas saa.repo = repo;
9833 5e22b737 2022-05-31 thomas
9834 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9835 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9836 814624e7 2022-03-22 thomas }
9837 5e22b737 2022-05-31 thomas
9838 5e22b737 2022-05-31 thomas const struct got_error *
9839 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9840 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9841 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9842 5e22b737 2022-05-31 thomas {
9843 8ec7352a 2023-06-01 thomas const struct got_error *err;
9844 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9845 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9846 5e22b737 2022-05-31 thomas
9847 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9848 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9849 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9850 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9851 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9852 5e22b737 2022-05-31 thomas sda.repo = repo;
9853 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9854 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9855 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9856 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9857 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9858 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9859 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9860 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9861 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9862 8ec7352a 2023-06-01 thomas
9863 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9864 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9865 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9866 8ec7352a 2023-06-01 thomas return err;
9867 5e22b737 2022-05-31 thomas }
9868 5e22b737 2022-05-31 thomas
9869 5e22b737 2022-05-31 thomas const struct got_error *
9870 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9871 36832a8e 2022-05-31 thomas const char *fileindex_path)
9872 5e22b737 2022-05-31 thomas {
9873 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9874 5e22b737 2022-05-31 thomas
9875 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9876 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9877 5e22b737 2022-05-31 thomas
9878 5e22b737 2022-05-31 thomas return err;
9879 5e22b737 2022-05-31 thomas }