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 21908da4 2019-01-13 stsp if (errno == ENOENT) {
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 f5375317 2020-10-20 stsp free(parent);
1415 21908da4 2019-01-13 stsp if (err)
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 a2c162eb 2022-10-30 thomas mode);
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1428 3b9f0f87 2020-07-23 stsp }
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1435 d70b8e30 2018-12-27 stsp } else {
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1438 507dc3bb 2018-12-29 stsp if (err)
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1441 a2c162eb 2022-10-30 thomas
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1446 a2c162eb 2022-10-30 thomas }
1447 9d31a1d8 2018-03-11 stsp }
1448 507dc3bb 2018-12-29 stsp } else
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1450 3818e3c4 2020-11-01 naddy }
1451 3818e3c4 2020-11-01 naddy
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 bd6aa359 2020-07-23 stsp path);
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 bd6aa359 2020-07-23 stsp if (err)
1463 bd6aa359 2020-07-23 stsp goto done;
1464 bd6aa359 2020-07-23 stsp }
1465 d7b62c98 2018-12-27 stsp
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1467 9d31a1d8 2018-03-11 stsp do {
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1470 9d31a1d8 2018-03-11 stsp if (err)
1471 9d31a1d8 2018-03-11 stsp break;
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1481 9d31a1d8 2018-03-11 stsp }
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1485 9d31a1d8 2018-03-11 stsp
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1489 816dc654 2019-02-16 stsp }
1490 9d31a1d8 2018-03-11 stsp
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1495 f6d8c0ac 2020-11-09 stsp }
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1500 68ed9ba5 2019-02-10 stsp }
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1503 507dc3bb 2018-12-29 stsp }
1504 507dc3bb 2018-12-29 stsp
1505 9d31a1d8 2018-03-11 stsp done:
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1512 6353ad76 2019-02-08 stsp }
1513 6353ad76 2019-02-08 stsp
1514 be94c032 2023-02-20 thomas /*
1515 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1517 be94c032 2023-02-20 thomas */
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1520 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1521 be94c032 2023-02-20 thomas FILE *ondisk_file)
1522 7154f6ce 2019-03-27 stsp {
1523 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1528 7154f6ce 2019-03-27 stsp };
1529 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1530 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1531 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1532 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1533 9bdd68dd 2020-01-02 naddy char *line = NULL;
1534 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1535 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1536 7154f6ce 2019-03-27 stsp
1537 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1538 8de9d8ad 2023-02-20 thomas return NULL;
1539 be94c032 2023-02-20 thomas
1540 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1541 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1542 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1543 be94c032 2023-02-20 thomas
1544 8de9d8ad 2023-02-20 thomas if (blob) {
1545 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1546 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 be94c032 2023-02-20 thomas if (err)
1548 be94c032 2023-02-20 thomas goto done;
1549 8de9d8ad 2023-02-20 thomas }
1550 be94c032 2023-02-20 thomas
1551 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 8de9d8ad 2023-02-20 thomas if (err)
1554 8de9d8ad 2023-02-20 thomas goto done;
1555 8de9d8ad 2023-02-20 thomas
1556 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1560 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1561 450d9f6b 2023-02-20 thomas off_t pos;
1562 8de9d8ad 2023-02-20 thomas
1563 8de9d8ad 2023-02-20 thomas /*
1564 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1565 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1566 8de9d8ad 2023-02-20 thomas */
1567 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1568 8de9d8ad 2023-02-20 thomas
1569 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1571 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1572 7154f6ce 2019-03-27 stsp }
1573 7154f6ce 2019-03-27 stsp
1574 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1575 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 450d9f6b 2023-02-20 thomas goto done;
1578 7154f6ce 2019-03-27 stsp }
1579 8de9d8ad 2023-02-20 thomas
1580 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1582 450d9f6b 2023-02-20 thomas
1583 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1584 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1585 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1586 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1587 8de9d8ad 2023-02-20 thomas break;
1588 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas }
1591 8de9d8ad 2023-02-20 thomas
1592 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1593 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1594 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1595 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1597 8de9d8ad 2023-02-20 thomas goto done;
1598 8de9d8ad 2023-02-20 thomas } else
1599 8de9d8ad 2023-02-20 thomas i++;
1600 8de9d8ad 2023-02-20 thomas }
1601 8de9d8ad 2023-02-20 thomas ++ln;
1602 8de9d8ad 2023-02-20 thomas }
1603 7154f6ce 2019-03-27 stsp }
1604 be94c032 2023-02-20 thomas
1605 be94c032 2023-02-20 thomas done:
1606 9bdd68dd 2020-01-02 naddy free(line);
1607 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1609 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1610 8de9d8ad 2023-02-20 thomas if (err == NULL)
1611 8de9d8ad 2023-02-20 thomas err = free_err;
1612 7154f6ce 2019-03-27 stsp
1613 7154f6ce 2019-03-27 stsp return err;
1614 e2b1e152 2019-07-13 stsp }
1615 e2b1e152 2019-07-13 stsp
1616 e2b1e152 2019-07-13 stsp static int
1617 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1618 1ebedb77 2019-10-19 stsp {
1619 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1621 1ebedb77 2019-10-19 stsp }
1622 1ebedb77 2019-10-19 stsp
1623 1ebedb77 2019-10-19 stsp static int
1624 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1625 e2b1e152 2019-07-13 stsp {
1626 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1631 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1632 c363b2c1 2019-08-03 stsp }
1633 c363b2c1 2019-08-03 stsp
1634 c363b2c1 2019-08-03 stsp static unsigned char
1635 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1636 c363b2c1 2019-08-03 stsp {
1637 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1638 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1639 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1644 c363b2c1 2019-08-03 stsp default:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1646 a919d5c4 2020-07-23 stsp }
1647 a919d5c4 2020-07-23 stsp }
1648 a919d5c4 2020-07-23 stsp
1649 a919d5c4 2020-07-23 stsp static const struct got_error *
1650 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1651 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1652 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1653 a919d5c4 2020-07-23 stsp {
1654 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1655 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1656 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1657 a919d5c4 2020-07-23 stsp ssize_t elen;
1658 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1659 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1665 a919d5c4 2020-07-23 stsp do {
1666 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1667 a919d5c4 2020-07-23 stsp if (err)
1668 a919d5c4 2020-07-23 stsp return err;
1669 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1670 a919d5c4 2020-07-23 stsp /*
1671 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1672 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1673 a919d5c4 2020-07-23 stsp */
1674 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1675 a919d5c4 2020-07-23 stsp }
1676 a919d5c4 2020-07-23 stsp if (len > 0) {
1677 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1678 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1679 a919d5c4 2020-07-23 stsp len - hdrlen);
1680 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1681 a919d5c4 2020-07-23 stsp hdrlen = 0;
1682 a919d5c4 2020-07-23 stsp }
1683 a919d5c4 2020-07-23 stsp } while (len != 0);
1684 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1685 a919d5c4 2020-07-23 stsp
1686 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1687 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 a919d5c4 2020-07-23 stsp if (elen == -1)
1689 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1690 a919d5c4 2020-07-23 stsp } else {
1691 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1692 a919d5c4 2020-07-23 stsp if (elen == -1)
1693 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1694 c363b2c1 2019-08-03 stsp }
1695 a919d5c4 2020-07-23 stsp
1696 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp return NULL;
1700 7154f6ce 2019-03-27 stsp }
1701 7154f6ce 2019-03-27 stsp
1702 7154f6ce 2019-03-27 stsp static const struct got_error *
1703 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1704 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1705 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1706 6353ad76 2019-02-08 stsp {
1707 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1708 6353ad76 2019-02-08 stsp struct got_object_id id;
1709 6353ad76 2019-02-08 stsp size_t hdrlen;
1710 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1711 6353ad76 2019-02-08 stsp FILE *f = NULL;
1712 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1713 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1714 6353ad76 2019-02-08 stsp size_t flen, blen;
1715 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1716 6353ad76 2019-02-08 stsp
1717 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1718 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1719 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1720 6353ad76 2019-02-08 stsp
1721 7f91a133 2019-12-13 stsp /*
1722 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1723 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1724 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1725 7f91a133 2019-12-13 stsp */
1726 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1727 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1729 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1730 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1731 882ef1b9 2019-12-13 stsp else
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1733 882ef1b9 2019-12-13 stsp goto done;
1734 882ef1b9 2019-12-13 stsp }
1735 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1736 3d35a492 2019-12-13 stsp goto done;
1737 3d35a492 2019-12-13 stsp }
1738 7f91a133 2019-12-13 stsp } else {
1739 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1741 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1742 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1743 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1745 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1746 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1748 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1749 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1750 3d35a492 2019-12-13 stsp else
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1752 3d35a492 2019-12-13 stsp goto done;
1753 3d35a492 2019-12-13 stsp }
1754 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1755 1338848f 2019-12-13 stsp goto done;
1756 a378724f 2019-02-10 stsp }
1757 a378724f 2019-02-10 stsp }
1758 6353ad76 2019-02-08 stsp
1759 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1761 1338848f 2019-12-13 stsp goto done;
1762 3f148bc6 2019-07-27 stsp }
1763 efdd40df 2019-07-27 stsp
1764 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1766 1338848f 2019-12-13 stsp goto done;
1767 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1768 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1769 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1770 1338848f 2019-12-13 stsp goto done;
1771 d00136be 2019-03-26 stsp }
1772 b8f41171 2019-02-10 stsp
1773 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1774 f179e66d 2020-07-23 stsp goto done;
1775 f179e66d 2020-07-23 stsp
1776 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1777 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1779 1338848f 2019-12-13 stsp goto done;
1780 f179e66d 2020-07-23 stsp }
1781 6353ad76 2019-02-08 stsp
1782 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1783 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1784 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1785 c363b2c1 2019-08-03 stsp else
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp
1788 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1789 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1790 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1791 f4ae6ddb 2022-07-01 thomas goto done;
1792 f4ae6ddb 2022-07-01 thomas }
1793 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1794 6353ad76 2019-02-08 stsp if (err)
1795 1338848f 2019-12-13 stsp goto done;
1796 6353ad76 2019-02-08 stsp
1797 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1798 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1799 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1800 a919d5c4 2020-07-23 stsp goto done;
1801 a919d5c4 2020-07-23 stsp }
1802 a919d5c4 2020-07-23 stsp
1803 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1804 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 ab0d4361 2019-12-13 stsp if (fd == -1) {
1806 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1807 ab0d4361 2019-12-13 stsp goto done;
1808 ab0d4361 2019-12-13 stsp }
1809 3d35a492 2019-12-13 stsp }
1810 3d35a492 2019-12-13 stsp
1811 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1812 6353ad76 2019-02-08 stsp if (f == NULL) {
1813 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1814 6353ad76 2019-02-08 stsp goto done;
1815 6353ad76 2019-02-08 stsp }
1816 1338848f 2019-12-13 stsp fd = -1;
1817 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1818 656b1f76 2019-05-11 jcs for (;;) {
1819 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1821 6353ad76 2019-02-08 stsp if (err)
1822 7154f6ce 2019-03-27 stsp goto done;
1823 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1824 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1826 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1827 7154f6ce 2019-03-27 stsp goto done;
1828 80c5c120 2019-02-19 stsp }
1829 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1830 6353ad76 2019-02-08 stsp if (flen != 0)
1831 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1832 6353ad76 2019-02-08 stsp break;
1833 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1835 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1836 6353ad76 2019-02-08 stsp break;
1837 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1838 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1839 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1841 6353ad76 2019-02-08 stsp break;
1842 6353ad76 2019-02-08 stsp }
1843 6353ad76 2019-02-08 stsp } else {
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp }
1847 6353ad76 2019-02-08 stsp hdrlen = 0;
1848 6353ad76 2019-02-08 stsp }
1849 7154f6ce 2019-03-27 stsp
1850 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1851 7154f6ce 2019-03-27 stsp rewind(f);
1852 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1853 be94c032 2023-02-20 thomas sb, f);
1854 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1855 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1856 6353ad76 2019-02-08 stsp done:
1857 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1859 6353ad76 2019-02-08 stsp if (blob)
1860 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1861 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1862 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1863 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1864 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1865 9d31a1d8 2018-03-11 stsp return err;
1866 e2b1e152 2019-07-13 stsp }
1867 e2b1e152 2019-07-13 stsp
1868 e2b1e152 2019-07-13 stsp /*
1869 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1870 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1871 e2b1e152 2019-07-13 stsp */
1872 e2b1e152 2019-07-13 stsp static const struct got_error *
1873 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1875 e2b1e152 2019-07-13 stsp {
1876 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1878 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1879 e2b1e152 2019-07-13 stsp
1880 e2b1e152 2019-07-13 stsp return NULL;
1881 9d31a1d8 2018-03-11 stsp }
1882 9d31a1d8 2018-03-11 stsp
1883 9d31a1d8 2018-03-11 stsp static const struct got_error *
1884 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1885 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1887 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1888 0584f854 2019-04-06 stsp void *progress_arg)
1889 9d31a1d8 2018-03-11 stsp {
1890 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1891 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1892 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1893 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1894 b8f41171 2019-02-10 stsp struct stat sb;
1895 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1896 9d31a1d8 2018-03-11 stsp
1897 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1899 6353ad76 2019-02-08 stsp
1900 abb4604f 2019-07-27 stsp if (ie) {
1901 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1903 a76c42e6 2019-08-03 stsp goto done;
1904 a76c42e6 2019-08-03 stsp }
1905 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1906 7f91a133 2019-12-13 stsp repo);
1907 abb4604f 2019-07-27 stsp if (err)
1908 abb4604f 2019-07-27 stsp goto done;
1909 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1911 c90c8ce3 2020-07-23 stsp } else {
1912 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1913 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1914 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1915 e6f4ba31 2021-09-24 thomas ondisk_path);
1916 e6f4ba31 2021-09-24 thomas goto done;
1917 e6f4ba31 2021-09-24 thomas }
1918 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1919 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1920 e6f4ba31 2021-09-24 thomas } else {
1921 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1922 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1923 e6f4ba31 2021-09-24 thomas else
1924 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1925 e6f4ba31 2021-09-24 thomas }
1926 c90c8ce3 2020-07-23 stsp }
1927 6353ad76 2019-02-08 stsp
1928 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1929 2c41dce7 2021-06-27 stsp if (ie)
1930 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1931 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1932 b8f41171 2019-02-10 stsp goto done;
1933 b8f41171 2019-02-10 stsp }
1934 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1935 a769b60b 2021-06-27 stsp if (ie)
1936 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1937 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1938 5036ab18 2020-04-18 stsp path);
1939 5036ab18 2020-04-18 stsp goto done;
1940 5036ab18 2020-04-18 stsp }
1941 b8f41171 2019-02-10 stsp
1942 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1943 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1944 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1945 c6e8a826 2021-04-05 stsp /*
1946 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1947 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1948 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1949 c6e8a826 2021-04-05 stsp * updating contents of this file.
1950 c6e8a826 2021-04-05 stsp */
1951 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1952 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1953 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1954 c6e8a826 2021-04-05 stsp /* Same commit. */
1955 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1956 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1957 e2b1e152 2019-07-13 stsp if (err)
1958 e2b1e152 2019-07-13 stsp goto done;
1959 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1960 b8f41171 2019-02-10 stsp path);
1961 6353ad76 2019-02-08 stsp goto done;
1962 a378724f 2019-02-10 stsp }
1963 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1964 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1965 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1966 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1967 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1968 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1969 0f58026f 2021-04-05 stsp if (err)
1970 0f58026f 2021-04-05 stsp goto done;
1971 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1972 0f58026f 2021-04-05 stsp path);
1973 b8f41171 2019-02-10 stsp goto done;
1974 e2b1e152 2019-07-13 stsp }
1975 9d31a1d8 2018-03-11 stsp }
1976 9d31a1d8 2018-03-11 stsp
1977 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1978 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1979 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1980 f4ae6ddb 2022-07-01 thomas goto done;
1981 f4ae6ddb 2022-07-01 thomas }
1982 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1983 8da9e5f4 2019-01-12 stsp if (err)
1984 6353ad76 2019-02-08 stsp goto done;
1985 512f0d0e 2019-01-02 stsp
1986 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1987 234035bc 2019-06-01 stsp int update_timestamps;
1988 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1989 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1990 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1991 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1992 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1993 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1994 f4ae6ddb 2022-07-01 thomas goto done;
1995 f4ae6ddb 2022-07-01 thomas }
1996 234035bc 2019-06-01 stsp struct got_object_id id2;
1997 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
1998 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1999 f4ae6ddb 2022-07-01 thomas fd2);
2000 234035bc 2019-06-01 stsp if (err)
2001 f69721c3 2019-10-21 stsp goto done;
2002 f69721c3 2019-10-21 stsp }
2003 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2004 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2005 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2006 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2007 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2008 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2009 f69721c3 2019-10-21 stsp goto done;
2010 f69721c3 2019-10-21 stsp }
2011 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2012 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2013 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2014 234035bc 2019-06-01 stsp goto done;
2015 f69721c3 2019-10-21 stsp }
2016 234035bc 2019-06-01 stsp }
2017 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2018 36bf999c 2020-07-23 stsp char *link_target;
2019 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2020 36bf999c 2020-07-23 stsp if (err)
2021 36bf999c 2020-07-23 stsp goto done;
2022 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2023 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2024 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2025 36bf999c 2020-07-23 stsp free(link_target);
2026 993e2a1b 2020-07-23 stsp } else {
2027 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2028 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2029 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2030 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2031 993e2a1b 2020-07-23 stsp }
2032 f69721c3 2019-10-21 stsp free(label_orig);
2033 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2034 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2035 f4ae6ddb 2022-07-01 thomas goto done;
2036 f4ae6ddb 2022-07-01 thomas }
2037 234035bc 2019-06-01 stsp if (blob2)
2038 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2039 909d120e 2019-09-22 stsp if (err)
2040 909d120e 2019-09-22 stsp goto done;
2041 234035bc 2019-06-01 stsp /*
2042 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2043 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2044 234035bc 2019-06-01 stsp * unmodified files again.
2045 234035bc 2019-06-01 stsp */
2046 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2047 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2048 234035bc 2019-06-01 stsp update_timestamps);
2049 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2050 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2052 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2053 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2054 1ee397ad 2019-07-12 stsp if (err)
2055 1ee397ad 2019-07-12 stsp goto done;
2056 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 13d9040b 2019-03-27 stsp if (err)
2059 13d9040b 2019-03-27 stsp goto done;
2060 13d9040b 2019-03-27 stsp } else {
2061 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2062 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2063 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2064 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2065 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2066 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2067 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2068 2e1fa222 2020-07-23 stsp } else {
2069 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2070 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2071 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2072 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2073 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2074 2e1fa222 2020-07-23 stsp }
2075 13d9040b 2019-03-27 stsp if (err)
2076 13d9040b 2019-03-27 stsp goto done;
2077 65b05cec 2020-07-23 stsp
2078 054041d0 2020-06-24 stsp if (ie) {
2079 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2080 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2081 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2082 054041d0 2020-06-24 stsp } else {
2083 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2084 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2085 054041d0 2020-06-24 stsp &blob->id);
2086 054041d0 2020-06-24 stsp }
2087 13d9040b 2019-03-27 stsp if (err)
2088 13d9040b 2019-03-27 stsp goto done;
2089 65b05cec 2020-07-23 stsp
2090 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2091 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2092 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2093 2e1fa222 2020-07-23 stsp }
2094 13d9040b 2019-03-27 stsp }
2095 f4ae6ddb 2022-07-01 thomas
2096 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2097 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2098 f4ae6ddb 2022-07-01 thomas goto done;
2099 f4ae6ddb 2022-07-01 thomas }
2100 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2101 6353ad76 2019-02-08 stsp done:
2102 6353ad76 2019-02-08 stsp free(ondisk_path);
2103 8da9e5f4 2019-01-12 stsp return err;
2104 90285c3b 2019-01-08 stsp }
2105 90285c3b 2019-01-08 stsp
2106 90285c3b 2019-01-08 stsp static const struct got_error *
2107 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2108 90285c3b 2019-01-08 stsp {
2109 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2110 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2111 90285c3b 2019-01-08 stsp
2112 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2113 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2114 90285c3b 2019-01-08 stsp
2115 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2116 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2117 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2118 c97665ae 2019-01-13 stsp } else {
2119 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2120 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2121 1c4cdd89 2021-06-20 stsp if (err)
2122 1c4cdd89 2021-06-20 stsp goto done;
2123 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2124 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2125 fddefe3b 2020-10-20 stsp free(ondisk_path);
2126 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2127 1c4cdd89 2021-06-20 stsp parent = NULL;
2128 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2129 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2130 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2131 fddefe3b 2020-10-20 stsp ondisk_path);
2132 90285c3b 2019-01-08 stsp break;
2133 90285c3b 2019-01-08 stsp }
2134 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2135 1c4cdd89 2021-06-20 stsp if (err)
2136 1c4cdd89 2021-06-20 stsp break;
2137 1c4cdd89 2021-06-20 stsp }
2138 90285c3b 2019-01-08 stsp }
2139 1c4cdd89 2021-06-20 stsp done:
2140 90285c3b 2019-01-08 stsp free(ondisk_path);
2141 1c4cdd89 2021-06-20 stsp free(parent);
2142 90285c3b 2019-01-08 stsp return err;
2143 512f0d0e 2019-01-02 stsp }
2144 512f0d0e 2019-01-02 stsp
2145 708d8e67 2019-03-27 stsp static const struct got_error *
2146 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2147 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2148 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2149 708d8e67 2019-03-27 stsp {
2150 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2151 708d8e67 2019-03-27 stsp unsigned char status;
2152 708d8e67 2019-03-27 stsp struct stat sb;
2153 708d8e67 2019-03-27 stsp char *ondisk_path;
2154 708d8e67 2019-03-27 stsp
2155 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2156 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2157 a76c42e6 2019-08-03 stsp
2158 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2159 708d8e67 2019-03-27 stsp == -1)
2160 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2161 708d8e67 2019-03-27 stsp
2162 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2163 708d8e67 2019-03-27 stsp if (err)
2164 5a58a424 2020-06-23 stsp goto done;
2165 708d8e67 2019-03-27 stsp
2166 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2167 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2168 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2169 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2170 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2171 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2172 993e2a1b 2020-07-23 stsp goto done;
2173 993e2a1b 2020-07-23 stsp }
2174 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2175 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2176 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2177 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2178 993e2a1b 2020-07-23 stsp if (err)
2179 993e2a1b 2020-07-23 stsp goto done;
2180 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2181 993e2a1b 2020-07-23 stsp ie->path);
2182 993e2a1b 2020-07-23 stsp goto done;
2183 993e2a1b 2020-07-23 stsp }
2184 993e2a1b 2020-07-23 stsp
2185 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2186 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2187 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2188 1ee397ad 2019-07-12 stsp if (err)
2189 5a58a424 2020-06-23 stsp goto done;
2190 fc6346c4 2019-03-27 stsp /*
2191 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2192 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2193 fc6346c4 2019-03-27 stsp */
2194 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2195 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2196 fc6346c4 2019-03-27 stsp } else {
2197 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2198 1ee397ad 2019-07-12 stsp if (err)
2199 5a58a424 2020-06-23 stsp goto done;
2200 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2201 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2202 fc6346c4 2019-03-27 stsp if (err)
2203 5a58a424 2020-06-23 stsp goto done;
2204 fc6346c4 2019-03-27 stsp }
2205 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2206 708d8e67 2019-03-27 stsp }
2207 5a58a424 2020-06-23 stsp done:
2208 5a58a424 2020-06-23 stsp free(ondisk_path);
2209 fc6346c4 2019-03-27 stsp return err;
2210 708d8e67 2019-03-27 stsp }
2211 708d8e67 2019-03-27 stsp
2212 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2213 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2214 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2215 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2216 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2217 8da9e5f4 2019-01-12 stsp void *progress_arg;
2218 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2219 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2220 8da9e5f4 2019-01-12 stsp };
2221 8da9e5f4 2019-01-12 stsp
2222 512f0d0e 2019-01-02 stsp static const struct got_error *
2223 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2224 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2225 512f0d0e 2019-01-02 stsp {
2226 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2227 0584f854 2019-04-06 stsp
2228 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2229 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2230 512f0d0e 2019-01-02 stsp
2231 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2232 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2233 8da9e5f4 2019-01-12 stsp }
2234 512f0d0e 2019-01-02 stsp
2235 8da9e5f4 2019-01-12 stsp static const struct got_error *
2236 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 8da9e5f4 2019-01-12 stsp {
2238 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2239 7a9df742 2019-01-08 stsp
2240 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2242 0584f854 2019-04-06 stsp
2243 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2244 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2245 9d31a1d8 2018-03-11 stsp }
2246 9d31a1d8 2018-03-11 stsp
2247 9d31a1d8 2018-03-11 stsp static const struct got_error *
2248 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2249 9d31a1d8 2018-03-11 stsp {
2250 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2251 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2252 8da9e5f4 2019-01-12 stsp char *path;
2253 9d31a1d8 2018-03-11 stsp
2254 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2255 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2256 0584f854 2019-04-06 stsp
2257 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2258 63c5ca5d 2019-08-24 stsp return NULL;
2259 63c5ca5d 2019-08-24 stsp
2260 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2261 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2262 8da9e5f4 2019-01-12 stsp == -1)
2263 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2264 9d31a1d8 2018-03-11 stsp
2265 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2266 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2267 8da9e5f4 2019-01-12 stsp else
2268 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2269 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2270 9d31a1d8 2018-03-11 stsp
2271 8da9e5f4 2019-01-12 stsp free(path);
2272 0cd1c46a 2019-03-11 stsp return err;
2273 0cd1c46a 2019-03-11 stsp }
2274 0cd1c46a 2019-03-11 stsp
2275 b2118c49 2020-07-28 stsp const struct got_error *
2276 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2277 b2118c49 2020-07-28 stsp {
2278 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2279 b2118c49 2020-07-28 stsp
2280 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2281 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2282 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2283 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2284 b2118c49 2020-07-28 stsp }
2285 b2118c49 2020-07-28 stsp
2286 b2118c49 2020-07-28 stsp return NULL;
2287 b2118c49 2020-07-28 stsp }
2288 b2118c49 2020-07-28 stsp
2289 818c7501 2019-07-11 stsp static const struct got_error *
2290 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2291 0cd1c46a 2019-03-11 stsp {
2292 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2293 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2294 0cd1c46a 2019-03-11 stsp
2295 517bab73 2019-03-11 stsp *refname = NULL;
2296 517bab73 2019-03-11 stsp
2297 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2298 b2118c49 2020-07-28 stsp if (err)
2299 b2118c49 2020-07-28 stsp return err;
2300 0cd1c46a 2019-03-11 stsp
2301 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2302 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2303 0647c563 2019-03-11 stsp *refname = NULL;
2304 0cd1c46a 2019-03-11 stsp }
2305 517bab73 2019-03-11 stsp free(uuidstr);
2306 517bab73 2019-03-11 stsp return err;
2307 517bab73 2019-03-11 stsp }
2308 517bab73 2019-03-11 stsp
2309 818c7501 2019-07-11 stsp const struct got_error *
2310 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2311 f6cd0243 2023-01-28 thomas const char *prefix)
2312 f6cd0243 2023-01-28 thomas {
2313 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2314 f6cd0243 2023-01-28 thomas }
2315 f6cd0243 2023-01-28 thomas
2316 f6cd0243 2023-01-28 thomas const struct got_error *
2317 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2318 818c7501 2019-07-11 stsp {
2319 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2320 818c7501 2019-07-11 stsp }
2321 818c7501 2019-07-11 stsp
2322 818c7501 2019-07-11 stsp static const struct got_error *
2323 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2324 818c7501 2019-07-11 stsp {
2325 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2326 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2327 818c7501 2019-07-11 stsp }
2328 818c7501 2019-07-11 stsp
2329 818c7501 2019-07-11 stsp static const struct got_error *
2330 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2331 818c7501 2019-07-11 stsp {
2332 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2333 818c7501 2019-07-11 stsp }
2334 818c7501 2019-07-11 stsp
2335 818c7501 2019-07-11 stsp static const struct got_error *
2336 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2337 818c7501 2019-07-11 stsp {
2338 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2339 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2340 818c7501 2019-07-11 stsp }
2341 818c7501 2019-07-11 stsp
2342 818c7501 2019-07-11 stsp static const struct got_error *
2343 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2344 818c7501 2019-07-11 stsp {
2345 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2346 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2347 0ebf8283 2019-07-24 stsp }
2348 0ebf8283 2019-07-24 stsp
2349 0ebf8283 2019-07-24 stsp static const struct got_error *
2350 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2351 0ebf8283 2019-07-24 stsp {
2352 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2353 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2354 0ebf8283 2019-07-24 stsp }
2355 0ebf8283 2019-07-24 stsp
2356 0ebf8283 2019-07-24 stsp static const struct got_error *
2357 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2358 0ebf8283 2019-07-24 stsp {
2359 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2360 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2361 0ebf8283 2019-07-24 stsp }
2362 0ebf8283 2019-07-24 stsp
2363 0ebf8283 2019-07-24 stsp static const struct got_error *
2364 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2365 0ebf8283 2019-07-24 stsp {
2366 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2367 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2368 818c7501 2019-07-11 stsp }
2369 818c7501 2019-07-11 stsp
2370 0ebf8283 2019-07-24 stsp static const struct got_error *
2371 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2372 0ebf8283 2019-07-24 stsp {
2373 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2374 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2375 0ebf8283 2019-07-24 stsp }
2376 818c7501 2019-07-11 stsp
2377 0ebf8283 2019-07-24 stsp const struct got_error *
2378 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2379 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2380 0ebf8283 2019-07-24 stsp {
2381 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2382 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2383 0ebf8283 2019-07-24 stsp *path = NULL;
2384 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2385 0ebf8283 2019-07-24 stsp }
2386 0ebf8283 2019-07-24 stsp return NULL;
2387 10604dce 2021-09-24 thomas }
2388 10604dce 2021-09-24 thomas
2389 10604dce 2021-09-24 thomas static const struct got_error *
2390 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2391 10604dce 2021-09-24 thomas {
2392 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2393 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2394 0ebf8283 2019-07-24 stsp }
2395 0ebf8283 2019-07-24 stsp
2396 10604dce 2021-09-24 thomas static const struct got_error *
2397 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2398 10604dce 2021-09-24 thomas {
2399 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2400 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2401 10604dce 2021-09-24 thomas }
2402 10604dce 2021-09-24 thomas
2403 517bab73 2019-03-11 stsp /*
2404 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2405 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2406 517bab73 2019-03-11 stsp */
2407 517bab73 2019-03-11 stsp static const struct got_error *
2408 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2409 517bab73 2019-03-11 stsp {
2410 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2411 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2412 517bab73 2019-03-11 stsp char *refname;
2413 517bab73 2019-03-11 stsp
2414 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2415 517bab73 2019-03-11 stsp if (err)
2416 517bab73 2019-03-11 stsp return err;
2417 0cd1c46a 2019-03-11 stsp
2418 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2419 0cd1c46a 2019-03-11 stsp if (err)
2420 0cd1c46a 2019-03-11 stsp goto done;
2421 0cd1c46a 2019-03-11 stsp
2422 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2423 0cd1c46a 2019-03-11 stsp done:
2424 0cd1c46a 2019-03-11 stsp free(refname);
2425 0cd1c46a 2019-03-11 stsp if (ref)
2426 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2427 3e3a69f1 2019-07-25 stsp return err;
2428 3e3a69f1 2019-07-25 stsp }
2429 3e3a69f1 2019-07-25 stsp
2430 3e3a69f1 2019-07-25 stsp static const struct got_error *
2431 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2432 3e3a69f1 2019-07-25 stsp {
2433 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2434 3e3a69f1 2019-07-25 stsp
2435 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2436 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2437 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2438 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2439 3e3a69f1 2019-07-25 stsp }
2440 9d31a1d8 2018-03-11 stsp return err;
2441 9d31a1d8 2018-03-11 stsp }
2442 9d31a1d8 2018-03-11 stsp
2443 3e3a69f1 2019-07-25 stsp
2444 ebf99748 2019-05-09 stsp static const struct got_error *
2445 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2446 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2447 ebf99748 2019-05-09 stsp {
2448 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2449 ebf99748 2019-05-09 stsp FILE *index = NULL;
2450 0cd1c46a 2019-03-11 stsp
2451 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2452 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2453 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2454 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2455 ebf99748 2019-05-09 stsp
2456 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2457 3e3a69f1 2019-07-25 stsp if (err)
2458 ebf99748 2019-05-09 stsp goto done;
2459 ebf99748 2019-05-09 stsp
2460 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2461 ebf99748 2019-05-09 stsp if (index == NULL) {
2462 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2463 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2464 ebf99748 2019-05-09 stsp } else {
2465 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2466 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2467 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2468 ebf99748 2019-05-09 stsp }
2469 ebf99748 2019-05-09 stsp done:
2470 ebf99748 2019-05-09 stsp if (err) {
2471 ebf99748 2019-05-09 stsp free(*fileindex_path);
2472 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2473 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2474 ebf99748 2019-05-09 stsp *fileindex = NULL;
2475 ebf99748 2019-05-09 stsp }
2476 ebf99748 2019-05-09 stsp return err;
2477 ebf99748 2019-05-09 stsp }
2478 c932eeeb 2019-05-22 stsp
2479 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2480 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2481 c932eeeb 2019-05-22 stsp const char *path;
2482 c932eeeb 2019-05-22 stsp size_t path_len;
2483 c932eeeb 2019-05-22 stsp const char *entry_name;
2484 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2485 a484d721 2019-06-10 stsp void *progress_arg;
2486 c932eeeb 2019-05-22 stsp };
2487 ebf99748 2019-05-09 stsp
2488 c932eeeb 2019-05-22 stsp static const struct got_error *
2489 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2490 c932eeeb 2019-05-22 stsp {
2491 1ee397ad 2019-07-12 stsp const struct got_error *err;
2492 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2493 c932eeeb 2019-05-22 stsp
2494 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2495 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2496 c932eeeb 2019-05-22 stsp return NULL;
2497 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2498 102ff934 2019-06-11 stsp return NULL;
2499 102ff934 2019-06-11 stsp
2500 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2501 a769b60b 2021-06-27 stsp return NULL;
2502 a769b60b 2021-06-27 stsp
2503 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2504 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2505 c932eeeb 2019-05-22 stsp return NULL;
2506 c932eeeb 2019-05-22 stsp
2507 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2508 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2509 edd02c5e 2019-07-11 stsp ie->path);
2510 1ee397ad 2019-07-12 stsp if (err)
2511 1ee397ad 2019-07-12 stsp return err;
2512 1ee397ad 2019-07-12 stsp }
2513 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2514 c932eeeb 2019-05-22 stsp return NULL;
2515 a615e0e7 2020-12-16 stsp }
2516 a615e0e7 2020-12-16 stsp
2517 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2518 a615e0e7 2020-12-16 stsp static const struct got_error *
2519 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2520 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2521 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2522 a615e0e7 2020-12-16 stsp {
2523 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2524 a615e0e7 2020-12-16 stsp
2525 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2526 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2527 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2528 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2529 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2530 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2531 a615e0e7 2020-12-16 stsp
2532 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2533 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2534 9c6338c4 2019-06-02 stsp }
2535 9c6338c4 2019-06-02 stsp
2536 9c6338c4 2019-06-02 stsp static const struct got_error *
2537 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2538 9c6338c4 2019-06-02 stsp {
2539 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2540 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2541 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2542 867630bb 2020-01-17 stsp struct timespec timeout;
2543 9c6338c4 2019-06-02 stsp
2544 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2545 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2546 9c6338c4 2019-06-02 stsp if (err)
2547 9c6338c4 2019-06-02 stsp goto done;
2548 9c6338c4 2019-06-02 stsp
2549 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2550 9c6338c4 2019-06-02 stsp if (err)
2551 9c6338c4 2019-06-02 stsp goto done;
2552 9c6338c4 2019-06-02 stsp
2553 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2554 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2555 9c6338c4 2019-06-02 stsp fileindex_path);
2556 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2557 9c6338c4 2019-06-02 stsp }
2558 867630bb 2020-01-17 stsp
2559 867630bb 2020-01-17 stsp /*
2560 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2561 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2562 867630bb 2020-01-17 stsp * was recorded in the file index.
2563 867630bb 2020-01-17 stsp */
2564 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2565 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2566 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2567 9c6338c4 2019-06-02 stsp done:
2568 9c6338c4 2019-06-02 stsp if (new_index)
2569 9c6338c4 2019-06-02 stsp fclose(new_index);
2570 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2571 9c6338c4 2019-06-02 stsp return err;
2572 c932eeeb 2019-05-22 stsp }
2573 6ced4176 2019-07-12 stsp
2574 6ced4176 2019-07-12 stsp static const struct got_error *
2575 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2576 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2577 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2578 945f9229 2022-04-16 thomas struct got_repository *repo)
2579 6ced4176 2019-07-12 stsp {
2580 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2581 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2582 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2583 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2584 6ced4176 2019-07-12 stsp
2585 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2586 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2587 6ced4176 2019-07-12 stsp *tree_id = NULL;
2588 6ced4176 2019-07-12 stsp
2589 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2590 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2591 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2592 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2593 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2594 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2595 6ced4176 2019-07-12 stsp goto done;
2596 6ced4176 2019-07-12 stsp }
2597 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2598 945f9229 2022-04-16 thomas worktree->path_prefix);
2599 6ced4176 2019-07-12 stsp if (err)
2600 6ced4176 2019-07-12 stsp goto done;
2601 6ced4176 2019-07-12 stsp return NULL;
2602 6ced4176 2019-07-12 stsp }
2603 6ced4176 2019-07-12 stsp
2604 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2605 6ced4176 2019-07-12 stsp
2606 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2607 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2608 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2609 6ced4176 2019-07-12 stsp goto done;
2610 6ced4176 2019-07-12 stsp }
2611 6ced4176 2019-07-12 stsp
2612 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2613 6ced4176 2019-07-12 stsp if (err)
2614 6ced4176 2019-07-12 stsp goto done;
2615 6ced4176 2019-07-12 stsp
2616 6ced4176 2019-07-12 stsp free(in_repo_path);
2617 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2618 6ced4176 2019-07-12 stsp
2619 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2620 6ced4176 2019-07-12 stsp if (err)
2621 6ced4176 2019-07-12 stsp goto done;
2622 c932eeeb 2019-05-22 stsp
2623 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2624 6ced4176 2019-07-12 stsp /* Check out a single file. */
2625 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2626 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2627 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2628 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2629 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2630 6ced4176 2019-07-12 stsp goto done;
2631 6ced4176 2019-07-12 stsp }
2632 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2633 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2634 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2635 6ced4176 2019-07-12 stsp goto done;
2636 6ced4176 2019-07-12 stsp }
2637 6ced4176 2019-07-12 stsp } else {
2638 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2639 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2640 6ced4176 2019-07-12 stsp if (err)
2641 6ced4176 2019-07-12 stsp return err;
2642 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2643 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2644 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2645 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2646 6ced4176 2019-07-12 stsp goto done;
2647 6ced4176 2019-07-12 stsp }
2648 6ced4176 2019-07-12 stsp }
2649 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2650 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2651 6ced4176 2019-07-12 stsp } else {
2652 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2653 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2654 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2655 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2656 6ced4176 2019-07-12 stsp goto done;
2657 6ced4176 2019-07-12 stsp }
2658 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2659 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2660 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2661 6ced4176 2019-07-12 stsp goto done;
2662 6ced4176 2019-07-12 stsp }
2663 6ced4176 2019-07-12 stsp }
2664 6ced4176 2019-07-12 stsp done:
2665 6ced4176 2019-07-12 stsp free(id);
2666 6ced4176 2019-07-12 stsp free(in_repo_path);
2667 6ced4176 2019-07-12 stsp if (err) {
2668 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2669 6ced4176 2019-07-12 stsp free(*tree_relpath);
2670 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2671 6ced4176 2019-07-12 stsp free(*tree_id);
2672 6ced4176 2019-07-12 stsp *tree_id = NULL;
2673 6ced4176 2019-07-12 stsp }
2674 07ed472d 2019-07-12 stsp return err;
2675 07ed472d 2019-07-12 stsp }
2676 07ed472d 2019-07-12 stsp
2677 07ed472d 2019-07-12 stsp static const struct got_error *
2678 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2679 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2680 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2681 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2682 07ed472d 2019-07-12 stsp {
2683 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2684 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2685 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2686 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2687 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2688 07ed472d 2019-07-12 stsp
2689 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2690 7f47418f 2019-12-20 stsp if (err) {
2691 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2692 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2693 7f47418f 2019-12-20 stsp goto done;
2694 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2695 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2696 7f47418f 2019-12-20 stsp if (err)
2697 7f47418f 2019-12-20 stsp return err;
2698 7f47418f 2019-12-20 stsp }
2699 07ed472d 2019-07-12 stsp
2700 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2701 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2702 07ed472d 2019-07-12 stsp if (err)
2703 07ed472d 2019-07-12 stsp goto done;
2704 07ed472d 2019-07-12 stsp
2705 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2706 07ed472d 2019-07-12 stsp if (err)
2707 07ed472d 2019-07-12 stsp goto done;
2708 07ed472d 2019-07-12 stsp
2709 07ed472d 2019-07-12 stsp if (entry_name &&
2710 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2711 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2712 07ed472d 2019-07-12 stsp goto done;
2713 07ed472d 2019-07-12 stsp }
2714 07ed472d 2019-07-12 stsp
2715 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2716 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2717 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2718 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2719 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2720 07ed472d 2019-07-12 stsp arg.repo = repo;
2721 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2722 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2723 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2724 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2725 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2726 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2727 07ed472d 2019-07-12 stsp done:
2728 07ed472d 2019-07-12 stsp if (tree)
2729 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2730 07ed472d 2019-07-12 stsp if (commit)
2731 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2732 6ced4176 2019-07-12 stsp return err;
2733 6ced4176 2019-07-12 stsp }
2734 6ced4176 2019-07-12 stsp
2735 9d31a1d8 2018-03-11 stsp const struct got_error *
2736 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2737 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2738 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2739 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2740 9d31a1d8 2018-03-11 stsp {
2741 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2742 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2743 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2744 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2745 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2746 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2747 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2748 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2749 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2750 f2ea84fa 2019-07-27 stsp int entry_type;
2751 f2ea84fa 2019-07-27 stsp char *relpath;
2752 f2ea84fa 2019-07-27 stsp char *entry_name;
2753 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2754 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2755 9d31a1d8 2018-03-11 stsp
2756 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2757 f2ea84fa 2019-07-27 stsp
2758 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2759 9d31a1d8 2018-03-11 stsp if (err)
2760 9d31a1d8 2018-03-11 stsp return err;
2761 945f9229 2022-04-16 thomas
2762 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2763 945f9229 2022-04-16 thomas worktree->base_commit_id);
2764 945f9229 2022-04-16 thomas if (err)
2765 945f9229 2022-04-16 thomas goto done;
2766 93a30277 2018-12-24 stsp
2767 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2768 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2769 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2770 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2771 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2772 f2ea84fa 2019-07-27 stsp goto done;
2773 f2ea84fa 2019-07-27 stsp }
2774 6ced4176 2019-07-12 stsp
2775 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2776 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2777 945f9229 2022-04-16 thomas worktree, repo);
2778 f2ea84fa 2019-07-27 stsp if (err) {
2779 f2ea84fa 2019-07-27 stsp free(tpd);
2780 6ced4176 2019-07-12 stsp goto done;
2781 6ced4176 2019-07-12 stsp }
2782 f2ea84fa 2019-07-27 stsp
2783 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2785 f2ea84fa 2019-07-27 stsp if (err) {
2786 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2787 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2788 f2ea84fa 2019-07-27 stsp free(tpd);
2789 f2ea84fa 2019-07-27 stsp goto done;
2790 f2ea84fa 2019-07-27 stsp }
2791 f2ea84fa 2019-07-27 stsp } else
2792 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2793 f2ea84fa 2019-07-27 stsp
2794 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2795 6ced4176 2019-07-12 stsp }
2796 6ced4176 2019-07-12 stsp
2797 51514078 2018-12-25 stsp /*
2798 51514078 2018-12-25 stsp * Read the file index.
2799 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2800 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2801 51514078 2018-12-25 stsp */
2802 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 8da9e5f4 2019-01-12 stsp if (err)
2804 c4cdcb68 2019-04-03 stsp goto done;
2805 c4cdcb68 2019-04-03 stsp
2806 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2808 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2809 f2ea84fa 2019-07-27 stsp
2810 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2811 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2812 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 f2ea84fa 2019-07-27 stsp if (err)
2814 f2ea84fa 2019-07-27 stsp break;
2815 f2ea84fa 2019-07-27 stsp
2816 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2817 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2818 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2819 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2820 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2821 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2822 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2823 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2824 f2ea84fa 2019-07-27 stsp if (err)
2825 f2ea84fa 2019-07-27 stsp break;
2826 f2ea84fa 2019-07-27 stsp
2827 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2828 711d9cd9 2019-07-12 stsp }
2829 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2830 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2831 af12c6b9 2019-06-04 stsp err = sync_err;
2832 9d31a1d8 2018-03-11 stsp done:
2833 9c6338c4 2019-06-02 stsp free(fileindex_path);
2834 edfa7d7f 2018-09-11 stsp if (tree)
2835 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2836 9d31a1d8 2018-03-11 stsp if (commit)
2837 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2838 5ade8233 2019-07-12 stsp if (fileindex)
2839 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2840 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2841 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2842 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2844 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2845 f2ea84fa 2019-07-27 stsp free(tpd);
2846 f2ea84fa 2019-07-27 stsp }
2847 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2848 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2849 9d31a1d8 2018-03-11 stsp err = unlockerr;
2850 f8d1f275 2019-02-04 stsp return err;
2851 f8d1f275 2019-02-04 stsp }
2852 f8d1f275 2019-02-04 stsp
2853 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2854 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2855 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2856 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2857 234035bc 2019-06-01 stsp void *progress_arg;
2858 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2859 234035bc 2019-06-01 stsp void *cancel_arg;
2860 f69721c3 2019-10-21 stsp const char *label_orig;
2861 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2862 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2863 234035bc 2019-06-01 stsp };
2864 234035bc 2019-06-01 stsp
2865 234035bc 2019-06-01 stsp static const struct got_error *
2866 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2867 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2868 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2869 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2870 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2871 234035bc 2019-06-01 stsp {
2872 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2873 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2874 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2875 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2876 234035bc 2019-06-01 stsp struct stat sb;
2877 234035bc 2019-06-01 stsp unsigned char status;
2878 234035bc 2019-06-01 stsp int local_changes_subsumed;
2879 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2880 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2881 234035bc 2019-06-01 stsp
2882 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2883 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2884 d6c87207 2019-08-02 stsp strlen(path2));
2885 1ee397ad 2019-07-12 stsp if (ie == NULL)
2886 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2887 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2888 234035bc 2019-06-01 stsp
2889 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 234035bc 2019-06-01 stsp path2) == -1)
2891 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2892 234035bc 2019-06-01 stsp
2893 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2894 7f91a133 2019-12-13 stsp repo);
2895 234035bc 2019-06-01 stsp if (err)
2896 234035bc 2019-06-01 stsp goto done;
2897 234035bc 2019-06-01 stsp
2898 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2899 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2900 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2901 234035bc 2019-06-01 stsp goto done;
2902 234035bc 2019-06-01 stsp }
2903 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2904 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2905 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2906 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2907 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2908 234035bc 2019-06-01 stsp goto done;
2909 234035bc 2019-06-01 stsp }
2910 234035bc 2019-06-01 stsp
2911 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2912 36bf999c 2020-07-23 stsp char *link_target2;
2913 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2914 36bf999c 2020-07-23 stsp if (err)
2915 36bf999c 2020-07-23 stsp goto done;
2916 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2917 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2918 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2919 36bf999c 2020-07-23 stsp free(link_target2);
2920 af57b12a 2020-07-23 stsp } else {
2921 1af628f4 2021-06-03 stsp int fd;
2922 1af628f4 2021-06-03 stsp
2923 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2924 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2925 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2926 1af628f4 2021-06-03 stsp goto done;
2927 1af628f4 2021-06-03 stsp }
2928 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2929 1af628f4 2021-06-03 stsp f_orig, blob1);
2930 1af628f4 2021-06-03 stsp if (err)
2931 1af628f4 2021-06-03 stsp goto done;
2932 1af628f4 2021-06-03 stsp
2933 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2934 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2935 1af628f4 2021-06-03 stsp goto done;
2936 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2937 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2938 1af628f4 2021-06-03 stsp if (err)
2939 1af628f4 2021-06-03 stsp goto done;
2940 1af628f4 2021-06-03 stsp
2941 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2942 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2943 1af628f4 2021-06-03 stsp if (fd == -1) {
2944 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2945 1af628f4 2021-06-03 stsp ondisk_path);
2946 1af628f4 2021-06-03 stsp goto done;
2947 1af628f4 2021-06-03 stsp }
2948 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2949 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2950 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2951 1af628f4 2021-06-03 stsp ondisk_path);
2952 1af628f4 2021-06-03 stsp close(fd);
2953 1af628f4 2021-06-03 stsp goto done;
2954 1af628f4 2021-06-03 stsp }
2955 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2956 1af628f4 2021-06-03 stsp if (err)
2957 1af628f4 2021-06-03 stsp goto done;
2958 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2959 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2961 1af628f4 2021-06-03 stsp goto done;
2962 1af628f4 2021-06-03 stsp }
2963 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2964 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
2966 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2968 af57b12a 2020-07-23 stsp }
2969 234035bc 2019-06-01 stsp } else if (blob1) {
2970 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2971 d6c87207 2019-08-02 stsp strlen(path1));
2972 1ee397ad 2019-07-12 stsp if (ie == NULL)
2973 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2974 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2975 2b92fad7 2019-06-02 stsp
2976 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2977 2b92fad7 2019-06-02 stsp path1) == -1)
2978 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2979 2b92fad7 2019-06-02 stsp
2980 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2981 7f91a133 2019-12-13 stsp repo);
2982 2b92fad7 2019-06-02 stsp if (err)
2983 2b92fad7 2019-06-02 stsp goto done;
2984 2b92fad7 2019-06-02 stsp
2985 2b92fad7 2019-06-02 stsp switch (status) {
2986 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2987 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2988 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2989 1ee397ad 2019-07-12 stsp if (err)
2990 1ee397ad 2019-07-12 stsp goto done;
2991 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2992 2b92fad7 2019-06-02 stsp if (err)
2993 2b92fad7 2019-06-02 stsp goto done;
2994 2b92fad7 2019-06-02 stsp if (ie)
2995 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2996 2b92fad7 2019-06-02 stsp break;
2997 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2998 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2999 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3000 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3001 1ee397ad 2019-07-12 stsp if (err)
3002 1ee397ad 2019-07-12 stsp goto done;
3003 2b92fad7 2019-06-02 stsp if (ie)
3004 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3005 2b92fad7 2019-06-02 stsp break;
3006 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3007 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3008 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3009 e8f02263 2022-01-23 thomas off_t blob1_size;
3010 0a22ca1a 2020-09-23 stsp /*
3011 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3012 0a22ca1a 2020-09-23 stsp * exists in the repository.
3013 0a22ca1a 2020-09-23 stsp */
3014 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3015 e8f02263 2022-01-23 thomas &blob1_size, path1);
3016 0a22ca1a 2020-09-23 stsp if (err)
3017 0a22ca1a 2020-09-23 stsp goto done;
3018 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3019 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3020 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3021 0a22ca1a 2020-09-23 stsp if (err)
3022 0a22ca1a 2020-09-23 stsp goto done;
3023 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3024 0a22ca1a 2020-09-23 stsp path1);
3025 0a22ca1a 2020-09-23 stsp if (err)
3026 0a22ca1a 2020-09-23 stsp goto done;
3027 0a22ca1a 2020-09-23 stsp if (ie)
3028 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3029 0a22ca1a 2020-09-23 stsp ie);
3030 0a22ca1a 2020-09-23 stsp } else {
3031 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3032 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3033 0a22ca1a 2020-09-23 stsp }
3034 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3035 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3036 0a22ca1a 2020-09-23 stsp free(id);
3037 0a22ca1a 2020-09-23 stsp if (err)
3038 0a22ca1a 2020-09-23 stsp goto done;
3039 0a22ca1a 2020-09-23 stsp break;
3040 0a22ca1a 2020-09-23 stsp }
3041 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3042 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3043 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3044 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3045 1ee397ad 2019-07-12 stsp if (err)
3046 1ee397ad 2019-07-12 stsp goto done;
3047 2b92fad7 2019-06-02 stsp break;
3048 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3049 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3050 1ee397ad 2019-07-12 stsp if (err)
3051 1ee397ad 2019-07-12 stsp goto done;
3052 2b92fad7 2019-06-02 stsp break;
3053 2b92fad7 2019-06-02 stsp default:
3054 2b92fad7 2019-06-02 stsp break;
3055 2b92fad7 2019-06-02 stsp }
3056 234035bc 2019-06-01 stsp } else if (blob2) {
3057 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3058 234035bc 2019-06-01 stsp path2) == -1)
3059 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3060 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3061 d6c87207 2019-08-02 stsp strlen(path2));
3062 234035bc 2019-06-01 stsp if (ie) {
3063 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3064 7f91a133 2019-12-13 stsp -1, NULL, repo);
3065 234035bc 2019-06-01 stsp if (err)
3066 234035bc 2019-06-01 stsp goto done;
3067 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3068 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3069 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3070 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3071 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3072 1ee397ad 2019-07-12 stsp status, path2);
3073 234035bc 2019-06-01 stsp goto done;
3074 234035bc 2019-06-01 stsp }
3075 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3076 36bf999c 2020-07-23 stsp char *link_target2;
3077 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3078 36bf999c 2020-07-23 stsp blob2);
3079 36bf999c 2020-07-23 stsp if (err)
3080 36bf999c 2020-07-23 stsp goto done;
3081 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3082 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3083 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3084 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3085 36bf999c 2020-07-23 stsp free(link_target2);
3086 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3087 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3088 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3089 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3090 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3091 526a746f 2020-07-23 stsp a->progress_arg);
3092 dfe9fba0 2020-07-23 stsp } else {
3093 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3094 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3095 526a746f 2020-07-23 stsp }
3096 dfe9fba0 2020-07-23 stsp if (err)
3097 dfe9fba0 2020-07-23 stsp goto done;
3098 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3099 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3100 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3101 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3102 234035bc 2019-06-01 stsp if (err)
3103 234035bc 2019-06-01 stsp goto done;
3104 234035bc 2019-06-01 stsp }
3105 234035bc 2019-06-01 stsp } else {
3106 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3107 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3108 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3109 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3110 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3111 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3112 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3113 2e1fa222 2020-07-23 stsp } else {
3114 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3115 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3116 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3117 2e1fa222 2020-07-23 stsp }
3118 234035bc 2019-06-01 stsp if (err)
3119 234035bc 2019-06-01 stsp goto done;
3120 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3121 234035bc 2019-06-01 stsp if (err)
3122 234035bc 2019-06-01 stsp goto done;
3123 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3124 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3125 3969253a 2020-03-07 stsp if (err) {
3126 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3127 3969253a 2020-03-07 stsp goto done;
3128 3969253a 2020-03-07 stsp }
3129 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3130 2b92fad7 2019-06-02 stsp if (err) {
3131 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3132 2b92fad7 2019-06-02 stsp goto done;
3133 2b92fad7 2019-06-02 stsp }
3134 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3135 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3136 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3137 2e1fa222 2020-07-23 stsp }
3138 234035bc 2019-06-01 stsp }
3139 234035bc 2019-06-01 stsp }
3140 234035bc 2019-06-01 stsp done:
3141 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3142 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3143 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3144 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3145 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3146 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3147 1af628f4 2021-06-03 stsp free(id_str);
3148 54d5be07 2021-06-03 stsp free(label_deriv2);
3149 234035bc 2019-06-01 stsp free(ondisk_path);
3150 234035bc 2019-06-01 stsp return err;
3151 234035bc 2019-06-01 stsp }
3152 234035bc 2019-06-01 stsp
3153 69de9dd4 2021-09-03 stsp static const struct got_error *
3154 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3155 69de9dd4 2021-09-03 stsp {
3156 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3157 69de9dd4 2021-09-03 stsp
3158 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3159 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3160 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3161 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3162 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3163 69de9dd4 2021-09-03 stsp
3164 69de9dd4 2021-09-03 stsp return NULL;
3165 69de9dd4 2021-09-03 stsp }
3166 69de9dd4 2021-09-03 stsp
3167 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3168 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3169 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3170 234035bc 2019-06-01 stsp struct got_repository *repo;
3171 234035bc 2019-06-01 stsp };
3172 234035bc 2019-06-01 stsp
3173 234035bc 2019-06-01 stsp static const struct got_error *
3174 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3175 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3176 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3177 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3178 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3179 234035bc 2019-06-01 stsp {
3180 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3181 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3182 234035bc 2019-06-01 stsp unsigned char status;
3183 234035bc 2019-06-01 stsp struct stat sb;
3184 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3185 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3186 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3187 234035bc 2019-06-01 stsp char *ondisk_path;
3188 234035bc 2019-06-01 stsp
3189 69de9dd4 2021-09-03 stsp if (id == NULL)
3190 69de9dd4 2021-09-03 stsp return NULL;
3191 234035bc 2019-06-01 stsp
3192 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3193 69de9dd4 2021-09-03 stsp if (ie == NULL)
3194 69de9dd4 2021-09-03 stsp return NULL;
3195 69de9dd4 2021-09-03 stsp
3196 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3197 234035bc 2019-06-01 stsp == -1)
3198 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3199 234035bc 2019-06-01 stsp
3200 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3201 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3202 5546d466 2021-09-02 stsp free(ondisk_path);
3203 234035bc 2019-06-01 stsp if (err)
3204 234035bc 2019-06-01 stsp return err;
3205 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3206 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3207 234035bc 2019-06-01 stsp
3208 234035bc 2019-06-01 stsp return NULL;
3209 234035bc 2019-06-01 stsp }
3210 234035bc 2019-06-01 stsp
3211 818c7501 2019-07-11 stsp static const struct got_error *
3212 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3213 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3214 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3215 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3216 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3217 234035bc 2019-06-01 stsp {
3218 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3219 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3220 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3221 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3222 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3223 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3224 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3225 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3226 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3227 234035bc 2019-06-01 stsp
3228 03415a1a 2019-06-02 stsp if (commit_id1) {
3229 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3230 945f9229 2022-04-16 thomas if (err)
3231 945f9229 2022-04-16 thomas goto done;
3232 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3233 03415a1a 2019-06-02 stsp worktree->path_prefix);
3234 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3235 03415a1a 2019-06-02 stsp goto done;
3236 69d57f3d 2020-07-31 stsp }
3237 69d57f3d 2020-07-31 stsp if (tree_id1) {
3238 69d57f3d 2020-07-31 stsp char *id_str;
3239 03415a1a 2019-06-02 stsp
3240 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3241 03415a1a 2019-06-02 stsp if (err)
3242 03415a1a 2019-06-02 stsp goto done;
3243 f69721c3 2019-10-21 stsp
3244 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3245 f69721c3 2019-10-21 stsp if (err)
3246 f69721c3 2019-10-21 stsp goto done;
3247 f69721c3 2019-10-21 stsp
3248 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3249 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3250 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3251 f69721c3 2019-10-21 stsp free(id_str);
3252 f69721c3 2019-10-21 stsp goto done;
3253 f69721c3 2019-10-21 stsp }
3254 f69721c3 2019-10-21 stsp free(id_str);
3255 a0f32f33 2022-06-13 thomas
3256 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3257 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3258 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3259 a0f32f33 2022-06-13 thomas goto done;
3260 a0f32f33 2022-06-13 thomas }
3261 03415a1a 2019-06-02 stsp }
3262 234035bc 2019-06-01 stsp
3263 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3264 945f9229 2022-04-16 thomas if (err)
3265 945f9229 2022-04-16 thomas goto done;
3266 945f9229 2022-04-16 thomas
3267 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3268 234035bc 2019-06-01 stsp worktree->path_prefix);
3269 234035bc 2019-06-01 stsp if (err)
3270 234035bc 2019-06-01 stsp goto done;
3271 234035bc 2019-06-01 stsp
3272 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3273 234035bc 2019-06-01 stsp if (err)
3274 234035bc 2019-06-01 stsp goto done;
3275 234035bc 2019-06-01 stsp
3276 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3277 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3278 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3279 19a6a6b5 2022-07-01 thomas goto done;
3280 19a6a6b5 2022-07-01 thomas }
3281 19a6a6b5 2022-07-01 thomas
3282 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3283 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3284 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3285 a0f32f33 2022-06-13 thomas goto done;
3286 a0f32f33 2022-06-13 thomas }
3287 a0f32f33 2022-06-13 thomas
3288 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3289 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3290 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3291 19a6a6b5 2022-07-01 thomas goto done;
3292 19a6a6b5 2022-07-01 thomas }
3293 19a6a6b5 2022-07-01 thomas
3294 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3295 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3296 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3297 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3298 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3299 69de9dd4 2021-09-03 stsp if (err)
3300 69de9dd4 2021-09-03 stsp goto done;
3301 69de9dd4 2021-09-03 stsp
3302 234035bc 2019-06-01 stsp arg.worktree = worktree;
3303 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3304 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3305 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3306 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3307 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3308 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3309 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3310 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3311 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3312 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3313 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3314 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3315 af12c6b9 2019-06-04 stsp err = sync_err;
3316 234035bc 2019-06-01 stsp done:
3317 945f9229 2022-04-16 thomas if (commit1)
3318 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3319 945f9229 2022-04-16 thomas if (commit2)
3320 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3321 234035bc 2019-06-01 stsp if (tree1)
3322 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3323 234035bc 2019-06-01 stsp if (tree2)
3324 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3325 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3326 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3327 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3328 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3329 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3330 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3331 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3332 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3333 f69721c3 2019-10-21 stsp free(label_orig);
3334 818c7501 2019-07-11 stsp return err;
3335 818c7501 2019-07-11 stsp }
3336 234035bc 2019-06-01 stsp
3337 818c7501 2019-07-11 stsp const struct got_error *
3338 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3339 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3340 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3341 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3342 818c7501 2019-07-11 stsp {
3343 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3344 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3345 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3346 818c7501 2019-07-11 stsp
3347 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3348 818c7501 2019-07-11 stsp if (err)
3349 818c7501 2019-07-11 stsp return err;
3350 818c7501 2019-07-11 stsp
3351 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3352 818c7501 2019-07-11 stsp if (err)
3353 818c7501 2019-07-11 stsp goto done;
3354 818c7501 2019-07-11 stsp
3355 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3356 69de9dd4 2021-09-03 stsp worktree);
3357 818c7501 2019-07-11 stsp if (err)
3358 818c7501 2019-07-11 stsp goto done;
3359 818c7501 2019-07-11 stsp
3360 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3361 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3362 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3363 818c7501 2019-07-11 stsp done:
3364 818c7501 2019-07-11 stsp if (fileindex)
3365 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3366 818c7501 2019-07-11 stsp free(fileindex_path);
3367 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3368 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3369 234035bc 2019-06-01 stsp err = unlockerr;
3370 234035bc 2019-06-01 stsp return err;
3371 234035bc 2019-06-01 stsp }
3372 234035bc 2019-06-01 stsp
3373 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3374 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3375 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3376 927df6b7 2019-02-10 stsp const char *status_path;
3377 927df6b7 2019-02-10 stsp size_t status_path_len;
3378 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3379 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3380 f8d1f275 2019-02-04 stsp void *status_arg;
3381 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3382 f8d1f275 2019-02-04 stsp void *cancel_arg;
3383 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3384 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3385 f2a9dc41 2019-12-13 tracey int report_unchanged;
3386 3143d852 2020-06-25 stsp int no_ignores;
3387 f8d1f275 2019-02-04 stsp };
3388 88d0e355 2019-08-03 stsp
3389 f8d1f275 2019-02-04 stsp static const struct got_error *
3390 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3391 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3392 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3393 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3394 927df6b7 2019-02-10 stsp {
3395 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3396 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3397 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3398 927df6b7 2019-02-10 stsp struct stat sb;
3399 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3400 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3401 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3402 927df6b7 2019-02-10 stsp
3403 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3404 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3405 98eaaa12 2019-08-03 stsp if (err)
3406 98eaaa12 2019-08-03 stsp return err;
3407 98eaaa12 2019-08-03 stsp
3408 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3409 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3410 98eaaa12 2019-08-03 stsp return NULL;
3411 98eaaa12 2019-08-03 stsp
3412 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3413 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3414 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3415 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3416 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3417 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3418 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3419 43010591 2023-02-17 thomas &staged_blob_id, ie);
3420 98eaaa12 2019-08-03 stsp }
3421 98eaaa12 2019-08-03 stsp
3422 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3423 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3424 927df6b7 2019-02-10 stsp }
3425 927df6b7 2019-02-10 stsp
3426 927df6b7 2019-02-10 stsp static const struct got_error *
3427 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3428 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3429 f8d1f275 2019-02-04 stsp {
3430 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3431 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3432 f8d1f275 2019-02-04 stsp char *abspath;
3433 f8d1f275 2019-02-04 stsp
3434 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3435 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3436 0584f854 2019-04-06 stsp
3437 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3438 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3439 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3440 927df6b7 2019-02-10 stsp return NULL;
3441 927df6b7 2019-02-10 stsp
3442 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3443 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3444 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3445 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3446 f8d1f275 2019-02-04 stsp } else {
3447 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3448 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3449 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3450 f8d1f275 2019-02-04 stsp }
3451 f8d1f275 2019-02-04 stsp
3452 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3453 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3454 f8d1f275 2019-02-04 stsp free(abspath);
3455 9d31a1d8 2018-03-11 stsp return err;
3456 9d31a1d8 2018-03-11 stsp }
3457 f8d1f275 2019-02-04 stsp
3458 f8d1f275 2019-02-04 stsp static const struct got_error *
3459 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3460 f8d1f275 2019-02-04 stsp {
3461 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3462 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3463 2ec1f75b 2019-03-26 stsp unsigned char status;
3464 927df6b7 2019-02-10 stsp
3465 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3466 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3467 0584f854 2019-04-06 stsp
3468 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3469 927df6b7 2019-02-10 stsp return NULL;
3470 927df6b7 2019-02-10 stsp
3471 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3472 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3473 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3474 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3475 2ec1f75b 2019-03-26 stsp else
3476 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3477 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3478 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3479 6841da00 2019-08-08 stsp }
3480 6841da00 2019-08-08 stsp
3481 ef20f542 2022-06-26 thomas static void
3482 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3483 6841da00 2019-08-08 stsp {
3484 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3485 6841da00 2019-08-08 stsp
3486 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3487 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3488 21c2d8be 2023-01-10 thomas
3489 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3490 6841da00 2019-08-08 stsp }
3491 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3492 6841da00 2019-08-08 stsp }
3493 6841da00 2019-08-08 stsp
3494 6841da00 2019-08-08 stsp static const struct got_error *
3495 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3496 6841da00 2019-08-08 stsp {
3497 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3498 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3499 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3500 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3501 6841da00 2019-08-08 stsp size_t linesize = 0;
3502 6841da00 2019-08-08 stsp ssize_t linelen;
3503 6841da00 2019-08-08 stsp
3504 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3505 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3506 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3507 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3508 6841da00 2019-08-08 stsp
3509 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3510 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3511 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3512 bd8de430 2019-10-04 stsp
3513 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3514 bd8de430 2019-10-04 stsp if (line[0] == '#')
3515 bd8de430 2019-10-04 stsp continue;
3516 bd8de430 2019-10-04 stsp
3517 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3518 bd8de430 2019-10-04 stsp if (line[0] == '!')
3519 bd8de430 2019-10-04 stsp continue;
3520 bd8de430 2019-10-04 stsp
3521 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3522 6841da00 2019-08-08 stsp line) == -1) {
3523 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3524 6841da00 2019-08-08 stsp goto done;
3525 6841da00 2019-08-08 stsp }
3526 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3527 6841da00 2019-08-08 stsp if (err)
3528 6841da00 2019-08-08 stsp goto done;
3529 6841da00 2019-08-08 stsp }
3530 6841da00 2019-08-08 stsp if (ferror(f)) {
3531 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3532 6841da00 2019-08-08 stsp goto done;
3533 6841da00 2019-08-08 stsp }
3534 6841da00 2019-08-08 stsp
3535 6841da00 2019-08-08 stsp dirpath = strdup(path);
3536 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3537 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3538 6841da00 2019-08-08 stsp goto done;
3539 6841da00 2019-08-08 stsp }
3540 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3541 6841da00 2019-08-08 stsp done:
3542 6841da00 2019-08-08 stsp free(line);
3543 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3544 6841da00 2019-08-08 stsp free(dirpath);
3545 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3546 6841da00 2019-08-08 stsp }
3547 6841da00 2019-08-08 stsp return err;
3548 1b5d300f 2023-02-20 thomas }
3549 1b5d300f 2023-02-20 thomas
3550 1b5d300f 2023-02-20 thomas static int
3551 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3552 1b5d300f 2023-02-20 thomas int flags)
3553 1b5d300f 2023-02-20 thomas {
3554 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3555 1b5d300f 2023-02-20 thomas
3556 1b5d300f 2023-02-20 thomas /*
3557 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3558 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3559 1b5d300f 2023-02-20 thomas */
3560 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3561 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3562 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3563 1b5d300f 2023-02-20 thomas
3564 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3565 1b5d300f 2023-02-20 thomas }
3566 1b5d300f 2023-02-20 thomas
3567 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3568 6841da00 2019-08-08 stsp }
3569 6841da00 2019-08-08 stsp
3570 ef20f542 2022-06-26 thomas static int
3571 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3572 6841da00 2019-08-08 stsp {
3573 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3574 bd8de430 2019-10-04 stsp
3575 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3576 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3577 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3578 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3579 bd8de430 2019-10-04 stsp
3580 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3581 1b5d300f 2023-02-20 thomas const char *p;
3582 6841da00 2019-08-08 stsp
3583 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3584 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3585 bd8de430 2019-10-04 stsp continue;
3586 bd8de430 2019-10-04 stsp p = path;
3587 bd8de430 2019-10-04 stsp while (*p) {
3588 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3589 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3590 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3591 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3592 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3593 bd8de430 2019-10-04 stsp p++;
3594 bd8de430 2019-10-04 stsp while (*p == '/')
3595 bd8de430 2019-10-04 stsp p++;
3596 bd8de430 2019-10-04 stsp continue;
3597 bd8de430 2019-10-04 stsp }
3598 bd8de430 2019-10-04 stsp return 1;
3599 bd8de430 2019-10-04 stsp }
3600 bd8de430 2019-10-04 stsp }
3601 bd8de430 2019-10-04 stsp }
3602 bd8de430 2019-10-04 stsp
3603 6841da00 2019-08-08 stsp /*
3604 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3605 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3606 6841da00 2019-08-08 stsp * ignores backwards.
3607 6841da00 2019-08-08 stsp */
3608 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3609 6841da00 2019-08-08 stsp while (pe) {
3610 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3611 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3612 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3613 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3614 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3615 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3616 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3617 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3618 1b5d300f 2023-02-20 thomas path, flags))
3619 6841da00 2019-08-08 stsp continue;
3620 6841da00 2019-08-08 stsp return 1;
3621 6841da00 2019-08-08 stsp }
3622 6841da00 2019-08-08 stsp }
3623 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3624 6841da00 2019-08-08 stsp }
3625 6841da00 2019-08-08 stsp
3626 6841da00 2019-08-08 stsp return 0;
3627 6841da00 2019-08-08 stsp }
3628 6841da00 2019-08-08 stsp
3629 6841da00 2019-08-08 stsp static const struct got_error *
3630 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3631 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3632 6841da00 2019-08-08 stsp {
3633 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3634 6841da00 2019-08-08 stsp char *ignorespath;
3635 886cec17 2019-12-15 stsp int fd = -1;
3636 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3637 6841da00 2019-08-08 stsp
3638 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3639 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3640 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3641 6841da00 2019-08-08 stsp
3642 886cec17 2019-12-15 stsp if (dirfd != -1) {
3643 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3644 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3645 886cec17 2019-12-15 stsp if (fd == -1) {
3646 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3647 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3648 886cec17 2019-12-15 stsp ignorespath);
3649 886cec17 2019-12-15 stsp } else {
3650 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3651 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3652 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3653 886cec17 2019-12-15 stsp ignorespath);
3654 886cec17 2019-12-15 stsp else {
3655 886cec17 2019-12-15 stsp fd = -1;
3656 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3657 886cec17 2019-12-15 stsp }
3658 886cec17 2019-12-15 stsp }
3659 886cec17 2019-12-15 stsp } else {
3660 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3661 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3662 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3663 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3664 886cec17 2019-12-15 stsp ignorespath);
3665 886cec17 2019-12-15 stsp } else
3666 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3667 886cec17 2019-12-15 stsp }
3668 6841da00 2019-08-08 stsp
3669 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3670 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3671 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3672 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3673 6841da00 2019-08-08 stsp free(ignorespath);
3674 6841da00 2019-08-08 stsp return err;
3675 f8d1f275 2019-02-04 stsp }
3676 f8d1f275 2019-02-04 stsp
3677 f8d1f275 2019-02-04 stsp static const struct got_error *
3678 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3679 6092c299 2021-10-04 thomas int dirfd)
3680 f8d1f275 2019-02-04 stsp {
3681 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3682 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3683 f8d1f275 2019-02-04 stsp char *path = NULL;
3684 6092c299 2021-10-04 thomas
3685 6092c299 2021-10-04 thomas if (ignore != NULL)
3686 6092c299 2021-10-04 thomas *ignore = 0;
3687 f8d1f275 2019-02-04 stsp
3688 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3689 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3690 0584f854 2019-04-06 stsp
3691 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3692 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3693 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3694 f8d1f275 2019-02-04 stsp } else {
3695 f8d1f275 2019-02-04 stsp path = de->d_name;
3696 f8d1f275 2019-02-04 stsp }
3697 f8d1f275 2019-02-04 stsp
3698 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3699 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3700 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3701 6092c299 2021-10-04 thomas *ignore = 1;
3702 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3703 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3704 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3705 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3706 f8d1f275 2019-02-04 stsp if (parent_path[0])
3707 f8d1f275 2019-02-04 stsp free(path);
3708 b72f483a 2019-02-05 stsp return err;
3709 f8d1f275 2019-02-04 stsp }
3710 f8d1f275 2019-02-04 stsp
3711 347d1d3e 2019-07-12 stsp static const struct got_error *
3712 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3713 3143d852 2020-06-25 stsp {
3714 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3715 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3716 3143d852 2020-06-25 stsp
3717 3143d852 2020-06-25 stsp if (a->no_ignores)
3718 3143d852 2020-06-25 stsp return NULL;
3719 3143d852 2020-06-25 stsp
3720 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3721 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3722 3143d852 2020-06-25 stsp if (err)
3723 3143d852 2020-06-25 stsp return err;
3724 3143d852 2020-06-25 stsp
3725 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3726 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3727 3143d852 2020-06-25 stsp
3728 3143d852 2020-06-25 stsp return err;
3729 3143d852 2020-06-25 stsp }
3730 3143d852 2020-06-25 stsp
3731 3143d852 2020-06-25 stsp static const struct got_error *
3732 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3733 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3734 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3735 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3736 abb4604f 2019-07-27 stsp {
3737 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3738 abb4604f 2019-07-27 stsp struct stat sb;
3739 ff56836b 2021-07-08 stsp
3740 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3741 abb4604f 2019-07-27 stsp if (ie)
3742 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3743 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3744 abb4604f 2019-07-27 stsp
3745 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3746 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3747 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3748 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3749 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3750 abb4604f 2019-07-27 stsp }
3751 abb4604f 2019-07-27 stsp
3752 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3753 fe1d8685 2022-02-12 thomas return NULL;
3754 fe1d8685 2022-02-12 thomas
3755 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3756 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3757 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3758 abb4604f 2019-07-27 stsp
3759 abb4604f 2019-07-27 stsp return NULL;
3760 3143d852 2020-06-25 stsp }
3761 3143d852 2020-06-25 stsp
3762 3143d852 2020-06-25 stsp static const struct got_error *
3763 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3764 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3765 3143d852 2020-06-25 stsp {
3766 3143d852 2020-06-25 stsp const struct got_error *err;
3767 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3768 3143d852 2020-06-25 stsp
3769 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3770 3143d852 2020-06-25 stsp ".cvsignore");
3771 3143d852 2020-06-25 stsp if (err)
3772 3143d852 2020-06-25 stsp return err;
3773 3143d852 2020-06-25 stsp
3774 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3775 3143d852 2020-06-25 stsp ".gitignore");
3776 3143d852 2020-06-25 stsp if (err)
3777 3143d852 2020-06-25 stsp return err;
3778 3143d852 2020-06-25 stsp
3779 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3780 3143d852 2020-06-25 stsp if (err) {
3781 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3782 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3783 3143d852 2020-06-25 stsp return err;
3784 3143d852 2020-06-25 stsp }
3785 3143d852 2020-06-25 stsp for (;;) {
3786 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3787 3143d852 2020-06-25 stsp ".cvsignore");
3788 3143d852 2020-06-25 stsp if (err)
3789 3143d852 2020-06-25 stsp break;
3790 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3791 3143d852 2020-06-25 stsp ".gitignore");
3792 3143d852 2020-06-25 stsp if (err)
3793 3143d852 2020-06-25 stsp break;
3794 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3795 3143d852 2020-06-25 stsp if (err) {
3796 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3797 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3798 3143d852 2020-06-25 stsp break;
3799 3143d852 2020-06-25 stsp }
3800 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3801 ff56836b 2021-07-08 stsp break;
3802 b737c85a 2020-06-26 stsp free(parent_path);
3803 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3804 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3805 3143d852 2020-06-25 stsp }
3806 3143d852 2020-06-25 stsp
3807 b737c85a 2020-06-26 stsp free(parent_path);
3808 b737c85a 2020-06-26 stsp free(next_parent_path);
3809 3143d852 2020-06-25 stsp return err;
3810 abb4604f 2019-07-27 stsp }
3811 abb4604f 2019-07-27 stsp
3812 abb4604f 2019-07-27 stsp static const struct got_error *
3813 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3814 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3815 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3816 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3817 f2a9dc41 2019-12-13 tracey int report_unchanged)
3818 f8d1f275 2019-02-04 stsp {
3819 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3820 6fc93f37 2019-12-13 stsp int fd = -1;
3821 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3822 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3823 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3824 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3825 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3826 3143d852 2020-06-25 stsp
3827 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3828 f8d1f275 2019-02-04 stsp
3829 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3830 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3831 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3832 8dc303cc 2019-07-27 stsp
3833 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3834 a78810f8 2022-03-13 thomas if (ie) {
3835 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3836 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3837 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3838 a78810f8 2022-03-13 thomas goto done;
3839 a78810f8 2022-03-13 thomas }
3840 a78810f8 2022-03-13 thomas
3841 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3842 6fc93f37 2019-12-13 stsp if (fd == -1) {
3843 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3844 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3845 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3846 ff56836b 2021-07-08 stsp else {
3847 ff56836b 2021-07-08 stsp if (!no_ignores) {
3848 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3849 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3850 ff56836b 2021-07-08 stsp if (err)
3851 ff56836b 2021-07-08 stsp goto done;
3852 ff56836b 2021-07-08 stsp }
3853 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3854 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3855 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3856 ff56836b 2021-07-08 stsp }
3857 abb4604f 2019-07-27 stsp } else {
3858 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3859 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3860 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3861 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3862 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3863 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3864 abb4604f 2019-07-27 stsp arg.status_path = path;
3865 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3866 abb4604f 2019-07-27 stsp arg.repo = repo;
3867 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3868 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3869 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3870 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3871 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3872 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3873 022fae89 2019-12-06 tracey if (!no_ignores) {
3874 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3875 3143d852 2020-06-25 stsp worktree->root_path, path);
3876 3143d852 2020-06-25 stsp if (err)
3877 3143d852 2020-06-25 stsp goto done;
3878 022fae89 2019-12-06 tracey }
3879 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3880 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3881 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3882 927df6b7 2019-02-10 stsp }
3883 3143d852 2020-06-25 stsp done:
3884 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3885 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3886 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3887 927df6b7 2019-02-10 stsp free(ondisk_path);
3888 347d1d3e 2019-07-12 stsp return err;
3889 347d1d3e 2019-07-12 stsp }
3890 347d1d3e 2019-07-12 stsp
3891 347d1d3e 2019-07-12 stsp const struct got_error *
3892 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3893 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3894 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3895 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3896 347d1d3e 2019-07-12 stsp {
3897 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3898 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3899 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3900 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3901 347d1d3e 2019-07-12 stsp
3902 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3903 347d1d3e 2019-07-12 stsp if (err)
3904 347d1d3e 2019-07-12 stsp return err;
3905 347d1d3e 2019-07-12 stsp
3906 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3907 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3908 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3909 f6343036 2021-06-22 stsp no_ignores, 0);
3910 72ea6654 2019-07-27 stsp if (err)
3911 72ea6654 2019-07-27 stsp break;
3912 72ea6654 2019-07-27 stsp }
3913 f8d1f275 2019-02-04 stsp free(fileindex_path);
3914 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3915 f8d1f275 2019-02-04 stsp return err;
3916 f8d1f275 2019-02-04 stsp }
3917 6c7ab921 2019-03-18 stsp
3918 6c7ab921 2019-03-18 stsp const struct got_error *
3919 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3920 6c7ab921 2019-03-18 stsp const char *arg)
3921 6c7ab921 2019-03-18 stsp {
3922 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3923 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3924 6c7ab921 2019-03-18 stsp size_t len;
3925 00bb5ea0 2020-07-23 stsp struct stat sb;
3926 01740607 2020-11-04 stsp char *abspath = NULL;
3927 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3928 6c7ab921 2019-03-18 stsp
3929 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3930 6c7ab921 2019-03-18 stsp
3931 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3932 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3933 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3934 00bb5ea0 2020-07-23 stsp
3935 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3936 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3937 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3938 00bb5ea0 2020-07-23 stsp goto done;
3939 00bb5ea0 2020-07-23 stsp }
3940 727173c3 2020-11-06 stsp sb.st_mode = 0;
3941 00bb5ea0 2020-07-23 stsp }
3942 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3943 00bb5ea0 2020-07-23 stsp /*
3944 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3945 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3946 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3947 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3948 00bb5ea0 2020-07-23 stsp */
3949 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3950 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3951 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3952 00bb5ea0 2020-07-23 stsp goto done;
3953 00bb5ea0 2020-07-23 stsp }
3954 00bb5ea0 2020-07-23 stsp
3955 00bb5ea0 2020-07-23 stsp }
3956 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3957 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3958 00bb5ea0 2020-07-23 stsp if (err)
3959 d0710d08 2019-07-22 stsp goto done;
3960 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3961 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3962 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3963 00bb5ea0 2020-07-23 stsp goto done;
3964 00bb5ea0 2020-07-23 stsp }
3965 00bb5ea0 2020-07-23 stsp } else {
3966 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3967 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3968 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3969 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3970 00bb5ea0 2020-07-23 stsp goto done;
3971 00bb5ea0 2020-07-23 stsp }
3972 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3973 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3974 01740607 2020-11-04 stsp goto done;
3975 01740607 2020-11-04 stsp }
3976 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3977 01740607 2020-11-04 stsp sizeof(canonpath));
3978 01740607 2020-11-04 stsp if (err)
3979 00bb5ea0 2020-07-23 stsp goto done;
3980 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3981 01740607 2020-11-04 stsp if (resolved == NULL) {
3982 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3983 01740607 2020-11-04 stsp goto done;
3984 00bb5ea0 2020-07-23 stsp }
3985 d0710d08 2019-07-22 stsp }
3986 d0710d08 2019-07-22 stsp }
3987 6c7ab921 2019-03-18 stsp
3988 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3989 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3990 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3991 6c7ab921 2019-03-18 stsp goto done;
3992 6c7ab921 2019-03-18 stsp }
3993 6c7ab921 2019-03-18 stsp
3994 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3995 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3996 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3997 f6d88e1a 2019-05-29 stsp if (err)
3998 f6d88e1a 2019-05-29 stsp goto done;
3999 f6d88e1a 2019-05-29 stsp } else {
4000 f6d88e1a 2019-05-29 stsp path = strdup("");
4001 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4002 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4003 f6d88e1a 2019-05-29 stsp goto done;
4004 f6d88e1a 2019-05-29 stsp }
4005 6c7ab921 2019-03-18 stsp }
4006 6c7ab921 2019-03-18 stsp
4007 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4008 6c7ab921 2019-03-18 stsp len = strlen(path);
4009 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4010 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4011 6c7ab921 2019-03-18 stsp len--;
4012 6c7ab921 2019-03-18 stsp }
4013 6c7ab921 2019-03-18 stsp done:
4014 01740607 2020-11-04 stsp free(abspath);
4015 6c7ab921 2019-03-18 stsp free(resolved);
4016 d0710d08 2019-07-22 stsp free(cwd);
4017 6c7ab921 2019-03-18 stsp if (err == NULL)
4018 6c7ab921 2019-03-18 stsp *wt_path = path;
4019 6c7ab921 2019-03-18 stsp else
4020 6c7ab921 2019-03-18 stsp free(path);
4021 d00136be 2019-03-26 stsp return err;
4022 d00136be 2019-03-26 stsp }
4023 d00136be 2019-03-26 stsp
4024 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4025 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4026 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4027 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4028 4e68cba3 2019-11-23 stsp void *progress_arg;
4029 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4030 4e68cba3 2019-11-23 stsp };
4031 4e68cba3 2019-11-23 stsp
4032 4e68cba3 2019-11-23 stsp static const struct got_error *
4033 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4034 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4035 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4036 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4037 07f5b47a 2019-06-02 stsp {
4038 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4039 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4040 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4041 6d022e97 2019-08-04 stsp struct stat sb;
4042 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4043 07f5b47a 2019-06-02 stsp
4044 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4045 dbb83fbd 2019-12-12 stsp relpath) == -1)
4046 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4047 dbb83fbd 2019-12-12 stsp
4048 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4049 6d022e97 2019-08-04 stsp if (ie) {
4050 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4051 12463d8b 2019-12-13 stsp de_name, a->repo);
4052 6d022e97 2019-08-04 stsp if (err)
4053 dbb83fbd 2019-12-12 stsp goto done;
4054 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4055 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4056 dbb83fbd 2019-12-12 stsp goto done;
4057 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4058 dbb83fbd 2019-12-12 stsp if (err)
4059 dbb83fbd 2019-12-12 stsp goto done;
4060 6d022e97 2019-08-04 stsp }
4061 07f5b47a 2019-06-02 stsp
4062 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4063 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4064 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4065 84bf00a6 2022-02-12 thomas else
4066 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4067 dbb83fbd 2019-12-12 stsp goto done;
4068 4e68cba3 2019-11-23 stsp }
4069 07f5b47a 2019-06-02 stsp
4070 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4071 4e68cba3 2019-11-23 stsp if (err)
4072 4e68cba3 2019-11-23 stsp goto done;
4073 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4074 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4075 3969253a 2020-03-07 stsp if (err) {
4076 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4077 3969253a 2020-03-07 stsp goto done;
4078 3969253a 2020-03-07 stsp }
4079 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4080 07f5b47a 2019-06-02 stsp if (err) {
4081 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4082 4e68cba3 2019-11-23 stsp goto done;
4083 07f5b47a 2019-06-02 stsp }
4084 4e68cba3 2019-11-23 stsp done:
4085 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4086 dbb83fbd 2019-12-12 stsp if (err)
4087 dbb83fbd 2019-12-12 stsp return err;
4088 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4089 dbb83fbd 2019-12-12 stsp return NULL;
4090 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4091 07f5b47a 2019-06-02 stsp }
4092 07f5b47a 2019-06-02 stsp
4093 d00136be 2019-03-26 stsp const struct got_error *
4094 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4095 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4096 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4097 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4098 d00136be 2019-03-26 stsp {
4099 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4100 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4101 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4102 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4103 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4104 d00136be 2019-03-26 stsp
4105 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4106 d00136be 2019-03-26 stsp if (err)
4107 d00136be 2019-03-26 stsp return err;
4108 d00136be 2019-03-26 stsp
4109 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4110 d00136be 2019-03-26 stsp if (err)
4111 d00136be 2019-03-26 stsp goto done;
4112 d00136be 2019-03-26 stsp
4113 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4114 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4115 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4116 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4117 4e68cba3 2019-11-23 stsp saa.repo = repo;
4118 4e68cba3 2019-11-23 stsp
4119 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4120 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4121 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4122 1dd54920 2019-05-11 stsp if (err)
4123 af12c6b9 2019-06-04 stsp break;
4124 1dd54920 2019-05-11 stsp }
4125 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4126 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4127 af12c6b9 2019-06-04 stsp err = sync_err;
4128 d00136be 2019-03-26 stsp done:
4129 fb399478 2019-07-12 stsp free(fileindex_path);
4130 d00136be 2019-03-26 stsp if (fileindex)
4131 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4132 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4133 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4134 d00136be 2019-03-26 stsp err = unlockerr;
4135 6c7ab921 2019-03-18 stsp return err;
4136 6c7ab921 2019-03-18 stsp }
4137 17ed4618 2019-06-02 stsp
4138 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4139 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4140 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4141 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4142 f2a9dc41 2019-12-13 tracey void *progress_arg;
4143 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4144 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4145 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4146 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4147 766841c2 2020-08-13 stsp const char *status_codes;
4148 f2a9dc41 2019-12-13 tracey };
4149 f2a9dc41 2019-12-13 tracey
4150 f2a9dc41 2019-12-13 tracey static const struct got_error *
4151 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4152 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4153 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4154 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4155 17ed4618 2019-06-02 stsp {
4156 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4157 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4158 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4159 17ed4618 2019-06-02 stsp struct stat sb;
4160 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4161 d64cc78a 2022-01-25 thomas
4162 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4163 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4164 d64cc78a 2022-01-25 thomas return NULL;
4165 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4166 d64cc78a 2022-01-25 thomas }
4167 17ed4618 2019-06-02 stsp
4168 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4169 17ed4618 2019-06-02 stsp if (ie == NULL)
4170 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4171 2ec1f75b 2019-03-26 stsp
4172 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4173 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4174 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4175 9acbc4fa 2019-08-03 stsp return NULL;
4176 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4177 9acbc4fa 2019-08-03 stsp }
4178 9acbc4fa 2019-08-03 stsp
4179 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4180 f2a9dc41 2019-12-13 tracey relpath) == -1)
4181 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4182 f2a9dc41 2019-12-13 tracey
4183 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4184 12463d8b 2019-12-13 stsp a->repo);
4185 17ed4618 2019-06-02 stsp if (err)
4186 f2a9dc41 2019-12-13 tracey goto done;
4187 17ed4618 2019-06-02 stsp
4188 766841c2 2020-08-13 stsp if (a->status_codes) {
4189 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4190 766841c2 2020-08-13 stsp int i;
4191 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4192 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4193 766841c2 2020-08-13 stsp break;
4194 766841c2 2020-08-13 stsp }
4195 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4196 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4197 766841c2 2020-08-13 stsp free(ondisk_path);
4198 766841c2 2020-08-13 stsp return NULL;
4199 766841c2 2020-08-13 stsp }
4200 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4201 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4202 766841c2 2020-08-13 stsp static char msg[64];
4203 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4204 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4205 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4206 766841c2 2020-08-13 stsp goto done;
4207 766841c2 2020-08-13 stsp }
4208 766841c2 2020-08-13 stsp }
4209 766841c2 2020-08-13 stsp
4210 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4211 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4212 f2a9dc41 2019-12-13 tracey goto done;
4213 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4214 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4215 f2a9dc41 2019-12-13 tracey goto done;
4216 f2a9dc41 2019-12-13 tracey }
4217 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4218 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4219 d64cc78a 2022-01-25 thomas goto done;
4220 d64cc78a 2022-01-25 thomas }
4221 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4222 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4223 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4224 f2a9dc41 2019-12-13 tracey goto done;
4225 f2a9dc41 2019-12-13 tracey }
4226 17ed4618 2019-06-02 stsp }
4227 17ed4618 2019-06-02 stsp
4228 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4229 20a2ad1c 2020-10-20 stsp size_t root_len;
4230 20a2ad1c 2020-10-20 stsp
4231 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4232 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4233 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4234 12463d8b 2019-12-13 stsp ondisk_path);
4235 12463d8b 2019-12-13 stsp goto done;
4236 12463d8b 2019-12-13 stsp }
4237 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4238 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4239 12463d8b 2019-12-13 stsp goto done;
4240 15341bfd 2020-03-05 tracey }
4241 15341bfd 2020-03-05 tracey
4242 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4243 20a2ad1c 2020-10-20 stsp do {
4244 20a2ad1c 2020-10-20 stsp char *parent;
4245 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4246 20a2ad1c 2020-10-20 stsp if (err)
4247 2513f20a 2020-10-20 stsp goto done;
4248 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4249 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4250 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4251 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4252 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4253 20a2ad1c 2020-10-20 stsp ondisk_path);
4254 15341bfd 2020-03-05 tracey break;
4255 15341bfd 2020-03-05 tracey }
4256 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4257 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4258 f2a9dc41 2019-12-13 tracey }
4259 17ed4618 2019-06-02 stsp
4260 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4261 f2a9dc41 2019-12-13 tracey done:
4262 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4263 f2a9dc41 2019-12-13 tracey if (err)
4264 f2a9dc41 2019-12-13 tracey return err;
4265 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4266 f2a9dc41 2019-12-13 tracey return NULL;
4267 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4268 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4269 17ed4618 2019-06-02 stsp }
4270 17ed4618 2019-06-02 stsp
4271 2ec1f75b 2019-03-26 stsp const struct got_error *
4272 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4273 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4274 766841c2 2020-08-13 stsp const char *status_codes,
4275 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4276 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4277 2ec1f75b 2019-03-26 stsp {
4278 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4279 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4280 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4281 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4282 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4283 2ec1f75b 2019-03-26 stsp
4284 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4285 2ec1f75b 2019-03-26 stsp if (err)
4286 2ec1f75b 2019-03-26 stsp return err;
4287 2ec1f75b 2019-03-26 stsp
4288 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4289 2ec1f75b 2019-03-26 stsp if (err)
4290 2ec1f75b 2019-03-26 stsp goto done;
4291 f2a9dc41 2019-12-13 tracey
4292 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4293 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4294 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4295 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4296 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4297 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4298 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4299 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4300 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4301 2ec1f75b 2019-03-26 stsp
4302 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4303 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4304 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4305 17ed4618 2019-06-02 stsp if (err)
4306 af12c6b9 2019-06-04 stsp break;
4307 2ec1f75b 2019-03-26 stsp }
4308 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4309 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4310 af12c6b9 2019-06-04 stsp err = sync_err;
4311 2ec1f75b 2019-03-26 stsp done:
4312 fb399478 2019-07-12 stsp free(fileindex_path);
4313 2ec1f75b 2019-03-26 stsp if (fileindex)
4314 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4315 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4316 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4317 2ec1f75b 2019-03-26 stsp err = unlockerr;
4318 33aa809d 2019-08-08 stsp return err;
4319 33aa809d 2019-08-08 stsp }
4320 33aa809d 2019-08-08 stsp
4321 33aa809d 2019-08-08 stsp static const struct got_error *
4322 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4323 33aa809d 2019-08-08 stsp {
4324 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4325 33aa809d 2019-08-08 stsp char *line = NULL;
4326 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4327 33aa809d 2019-08-08 stsp ssize_t linelen;
4328 33aa809d 2019-08-08 stsp
4329 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4330 33aa809d 2019-08-08 stsp if (linelen == -1) {
4331 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4332 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4333 33aa809d 2019-08-08 stsp goto done;
4334 33aa809d 2019-08-08 stsp }
4335 33aa809d 2019-08-08 stsp return NULL;
4336 33aa809d 2019-08-08 stsp }
4337 33aa809d 2019-08-08 stsp if (outfile) {
4338 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4339 33aa809d 2019-08-08 stsp if (n != linelen) {
4340 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4341 33aa809d 2019-08-08 stsp goto done;
4342 33aa809d 2019-08-08 stsp }
4343 33aa809d 2019-08-08 stsp }
4344 33aa809d 2019-08-08 stsp if (rejectfile) {
4345 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4346 33aa809d 2019-08-08 stsp if (n != linelen)
4347 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4348 33aa809d 2019-08-08 stsp }
4349 33aa809d 2019-08-08 stsp done:
4350 33aa809d 2019-08-08 stsp free(line);
4351 2ec1f75b 2019-03-26 stsp return err;
4352 2ec1f75b 2019-03-26 stsp }
4353 1f1abb7e 2019-08-08 stsp
4354 33aa809d 2019-08-08 stsp static const struct got_error *
4355 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4356 33aa809d 2019-08-08 stsp {
4357 33aa809d 2019-08-08 stsp char *line = NULL;
4358 33aa809d 2019-08-08 stsp size_t linesize = 0;
4359 33aa809d 2019-08-08 stsp ssize_t linelen;
4360 33aa809d 2019-08-08 stsp
4361 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4362 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4363 500467ff 2019-09-25 hiltjo if (ferror(f))
4364 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4365 500467ff 2019-09-25 hiltjo return NULL;
4366 500467ff 2019-09-25 hiltjo }
4367 33aa809d 2019-08-08 stsp free(line);
4368 33aa809d 2019-08-08 stsp return NULL;
4369 33aa809d 2019-08-08 stsp }
4370 33aa809d 2019-08-08 stsp
4371 33aa809d 2019-08-08 stsp static const struct got_error *
4372 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4373 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4374 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4375 abc59930 2021-09-05 naddy {
4376 33aa809d 2019-08-08 stsp const struct got_error *err;
4377 33aa809d 2019-08-08 stsp
4378 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4379 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4380 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4381 33aa809d 2019-08-08 stsp if (err)
4382 33aa809d 2019-08-08 stsp return err;
4383 33aa809d 2019-08-08 stsp (*line_cur1)++;
4384 33aa809d 2019-08-08 stsp }
4385 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4386 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4387 33aa809d 2019-08-08 stsp if (rejectfile)
4388 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4389 33aa809d 2019-08-08 stsp else
4390 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4391 33aa809d 2019-08-08 stsp if (err)
4392 33aa809d 2019-08-08 stsp return err;
4393 33aa809d 2019-08-08 stsp (*line_cur2)++;
4394 33aa809d 2019-08-08 stsp }
4395 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4396 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4397 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4398 33aa809d 2019-08-08 stsp if (err)
4399 33aa809d 2019-08-08 stsp return err;
4400 33aa809d 2019-08-08 stsp (*line_cur2)++;
4401 33aa809d 2019-08-08 stsp }
4402 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4403 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4404 33aa809d 2019-08-08 stsp if (rejectfile)
4405 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4406 33aa809d 2019-08-08 stsp else
4407 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4408 33aa809d 2019-08-08 stsp if (err)
4409 33aa809d 2019-08-08 stsp return err;
4410 33aa809d 2019-08-08 stsp (*line_cur1)++;
4411 33aa809d 2019-08-08 stsp }
4412 f1e81a05 2019-08-10 stsp
4413 f1e81a05 2019-08-10 stsp return NULL;
4414 f1e81a05 2019-08-10 stsp }
4415 f1e81a05 2019-08-10 stsp
4416 f1e81a05 2019-08-10 stsp static const struct got_error *
4417 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4418 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4419 f1e81a05 2019-08-10 stsp {
4420 f1e81a05 2019-08-10 stsp const struct got_error *err;
4421 f1e81a05 2019-08-10 stsp
4422 f1e81a05 2019-08-10 stsp if (outfile) {
4423 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4424 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4425 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4426 f1e81a05 2019-08-10 stsp if (err)
4427 f1e81a05 2019-08-10 stsp return err;
4428 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4429 f1e81a05 2019-08-10 stsp }
4430 f1e81a05 2019-08-10 stsp }
4431 f1e81a05 2019-08-10 stsp if (rejectfile) {
4432 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4433 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4434 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4435 f1e81a05 2019-08-10 stsp if (err)
4436 f1e81a05 2019-08-10 stsp return err;
4437 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4438 f1e81a05 2019-08-10 stsp }
4439 33aa809d 2019-08-08 stsp }
4440 33aa809d 2019-08-08 stsp
4441 33aa809d 2019-08-08 stsp return NULL;
4442 33aa809d 2019-08-08 stsp }
4443 33aa809d 2019-08-08 stsp
4444 33aa809d 2019-08-08 stsp static const struct got_error *
4445 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4446 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4447 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4448 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4449 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4450 33aa809d 2019-08-08 stsp {
4451 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4452 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4453 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4454 33aa809d 2019-08-08 stsp FILE *hunkfile;
4455 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4456 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4457 fe621944 2020-11-10 stsp int rc;
4458 33aa809d 2019-08-08 stsp
4459 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4460 33aa809d 2019-08-08 stsp
4461 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4462 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4463 fe621944 2020-11-10 stsp start_old = cc.left.start;
4464 fe621944 2020-11-10 stsp end_old = cc.left.end;
4465 fe621944 2020-11-10 stsp start_new = cc.right.start;
4466 fe621944 2020-11-10 stsp end_new = cc.right.end;
4467 33aa809d 2019-08-08 stsp
4468 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4469 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4470 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4471 33aa809d 2019-08-08 stsp
4472 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4473 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4474 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4475 33aa809d 2019-08-08 stsp
4476 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4477 fe621944 2020-11-10 stsp if (diff_state == NULL)
4478 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4479 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4480 fe621944 2020-11-10 stsp
4481 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4482 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4483 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4484 33aa809d 2019-08-08 stsp goto done;
4485 33aa809d 2019-08-08 stsp }
4486 fe621944 2020-11-10 stsp
4487 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4488 fe621944 2020-11-10 stsp diff_result, &cc);
4489 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4490 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4491 33aa809d 2019-08-08 stsp goto done;
4492 33aa809d 2019-08-08 stsp }
4493 fe621944 2020-11-10 stsp
4494 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4495 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4496 33aa809d 2019-08-08 stsp goto done;
4497 33aa809d 2019-08-08 stsp }
4498 33aa809d 2019-08-08 stsp
4499 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4500 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4501 33aa809d 2019-08-08 stsp if (err)
4502 33aa809d 2019-08-08 stsp goto done;
4503 33aa809d 2019-08-08 stsp
4504 33aa809d 2019-08-08 stsp switch (*choice) {
4505 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4506 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4507 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4508 33aa809d 2019-08-08 stsp break;
4509 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4510 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4511 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4512 33aa809d 2019-08-08 stsp break;
4513 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4514 33aa809d 2019-08-08 stsp break;
4515 33aa809d 2019-08-08 stsp default:
4516 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4517 33aa809d 2019-08-08 stsp break;
4518 33aa809d 2019-08-08 stsp }
4519 33aa809d 2019-08-08 stsp done:
4520 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4521 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4522 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4523 33aa809d 2019-08-08 stsp return err;
4524 33aa809d 2019-08-08 stsp }
4525 33aa809d 2019-08-08 stsp
4526 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4527 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4528 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4529 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4530 1f1abb7e 2019-08-08 stsp void *progress_arg;
4531 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4532 33aa809d 2019-08-08 stsp void *patch_arg;
4533 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4534 10604dce 2021-09-24 thomas int unlink_added_files;
4535 1f1abb7e 2019-08-08 stsp };
4536 a129376b 2019-03-28 stsp
4537 e20a8b6f 2019-06-04 stsp static const struct got_error *
4538 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4539 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4540 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4541 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4542 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4543 33aa809d 2019-08-08 stsp {
4544 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4545 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4546 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4547 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4548 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4549 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4550 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4551 fe621944 2020-11-10 stsp struct stat sb2;
4552 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4553 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4554 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4555 33aa809d 2019-08-08 stsp
4556 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4557 33aa809d 2019-08-08 stsp
4558 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4559 33aa809d 2019-08-08 stsp if (err)
4560 33aa809d 2019-08-08 stsp return err;
4561 33aa809d 2019-08-08 stsp
4562 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4563 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4564 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4565 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4566 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4567 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4568 fa3cef63 2020-07-23 stsp goto done;
4569 fa3cef63 2020-07-23 stsp }
4570 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4571 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4572 48b4f239 2021-12-31 thomas if (link_len == -1) {
4573 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4574 48b4f239 2021-12-31 thomas path2);
4575 48b4f239 2021-12-31 thomas }
4576 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4577 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4578 12463d8b 2019-12-13 stsp }
4579 12463d8b 2019-12-13 stsp } else {
4580 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4581 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4582 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4583 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4584 fa3cef63 2020-07-23 stsp goto done;
4585 fa3cef63 2020-07-23 stsp }
4586 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4587 fa3cef63 2020-07-23 stsp sizeof(link_target));
4588 fa3cef63 2020-07-23 stsp if (link_len == -1)
4589 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4590 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4591 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4592 12463d8b 2019-12-13 stsp }
4593 1ebedb77 2019-10-19 stsp }
4594 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4595 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4596 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4597 fa3cef63 2020-07-23 stsp goto done;
4598 fa3cef63 2020-07-23 stsp }
4599 1ebedb77 2019-10-19 stsp
4600 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4601 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4602 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4603 fa3cef63 2020-07-23 stsp goto done;
4604 fa3cef63 2020-07-23 stsp }
4605 fa3cef63 2020-07-23 stsp fd2 = -1;
4606 fa3cef63 2020-07-23 stsp } else {
4607 fa3cef63 2020-07-23 stsp size_t n;
4608 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4609 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4610 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4611 fa3cef63 2020-07-23 stsp goto done;
4612 fa3cef63 2020-07-23 stsp }
4613 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4614 fa3cef63 2020-07-23 stsp if (n != link_len) {
4615 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4616 fa3cef63 2020-07-23 stsp goto done;
4617 fa3cef63 2020-07-23 stsp }
4618 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4619 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4620 fa3cef63 2020-07-23 stsp goto done;
4621 fa3cef63 2020-07-23 stsp }
4622 fa3cef63 2020-07-23 stsp rewind(f2);
4623 33aa809d 2019-08-08 stsp }
4624 33aa809d 2019-08-08 stsp
4625 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4626 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4627 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4628 f4ae6ddb 2022-07-01 thomas goto done;
4629 f4ae6ddb 2022-07-01 thomas }
4630 f4ae6ddb 2022-07-01 thomas
4631 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4632 33aa809d 2019-08-08 stsp if (err)
4633 33aa809d 2019-08-08 stsp goto done;
4634 33aa809d 2019-08-08 stsp
4635 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4636 33aa809d 2019-08-08 stsp if (err)
4637 33aa809d 2019-08-08 stsp goto done;
4638 33aa809d 2019-08-08 stsp
4639 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4640 33aa809d 2019-08-08 stsp if (err)
4641 33aa809d 2019-08-08 stsp goto done;
4642 33aa809d 2019-08-08 stsp
4643 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4644 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4645 33aa809d 2019-08-08 stsp if (err)
4646 33aa809d 2019-08-08 stsp goto done;
4647 33aa809d 2019-08-08 stsp
4648 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4649 fc2a50f2 2022-11-01 thomas "");
4650 33aa809d 2019-08-08 stsp if (err)
4651 33aa809d 2019-08-08 stsp goto done;
4652 33aa809d 2019-08-08 stsp
4653 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4654 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4655 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4656 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4657 fe621944 2020-11-10 stsp
4658 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4659 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4660 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4661 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4662 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4663 fe621944 2020-11-10 stsp nchanges++;
4664 fe621944 2020-11-10 stsp }
4665 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4666 33aa809d 2019-08-08 stsp int choice;
4667 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4668 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4669 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4670 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4671 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4672 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4673 33aa809d 2019-08-08 stsp if (err)
4674 33aa809d 2019-08-08 stsp goto done;
4675 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4676 33aa809d 2019-08-08 stsp have_content = 1;
4677 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4678 33aa809d 2019-08-08 stsp break;
4679 33aa809d 2019-08-08 stsp }
4680 1ebedb77 2019-10-19 stsp if (have_content) {
4681 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4682 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4683 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4684 1ebedb77 2019-10-19 stsp if (err)
4685 1ebedb77 2019-10-19 stsp goto done;
4686 1ebedb77 2019-10-19 stsp
4687 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4688 a2c162eb 2022-10-30 thomas mode_t mode;
4689 a2c162eb 2022-10-30 thomas
4690 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4691 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4692 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4693 fa3cef63 2020-07-23 stsp goto done;
4694 fa3cef63 2020-07-23 stsp }
4695 1ebedb77 2019-10-19 stsp }
4696 1ebedb77 2019-10-19 stsp }
4697 33aa809d 2019-08-08 stsp done:
4698 33aa809d 2019-08-08 stsp free(id_str);
4699 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4700 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4701 33aa809d 2019-08-08 stsp if (blob)
4702 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4703 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4704 fe621944 2020-11-10 stsp if (err == NULL)
4705 fe621944 2020-11-10 stsp err = free_err;
4706 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4707 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4708 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4709 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4710 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4711 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4712 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4713 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4714 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4715 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4716 33aa809d 2019-08-08 stsp if (err || !have_content) {
4717 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4718 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4719 33aa809d 2019-08-08 stsp free(*path_outfile);
4720 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4721 33aa809d 2019-08-08 stsp }
4722 33aa809d 2019-08-08 stsp free(path1);
4723 33aa809d 2019-08-08 stsp return err;
4724 33aa809d 2019-08-08 stsp }
4725 33aa809d 2019-08-08 stsp
4726 33aa809d 2019-08-08 stsp static const struct got_error *
4727 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4728 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4729 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4730 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4731 a129376b 2019-03-28 stsp {
4732 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4733 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4734 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4735 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4736 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4737 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4738 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4739 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4740 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4741 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4742 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4743 f4ae6ddb 2022-07-01 thomas int fd = -1;
4744 a129376b 2019-03-28 stsp
4745 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4746 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4747 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4748 d3bcc3d1 2019-08-08 stsp return NULL;
4749 3d69ad8d 2019-08-17 semarie
4750 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4751 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4752 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4753 d3bcc3d1 2019-08-08 stsp
4754 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4755 65084dad 2019-08-08 stsp if (ie == NULL)
4756 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4757 a129376b 2019-03-28 stsp
4758 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4759 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4760 a129376b 2019-03-28 stsp if (err) {
4761 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4762 a129376b 2019-03-28 stsp goto done;
4763 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4764 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4765 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4766 e20a8b6f 2019-06-04 stsp goto done;
4767 e20a8b6f 2019-06-04 stsp }
4768 a129376b 2019-03-28 stsp }
4769 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4770 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4771 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4772 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4773 a129376b 2019-03-28 stsp goto done;
4774 a129376b 2019-03-28 stsp }
4775 a129376b 2019-03-28 stsp } else {
4776 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4777 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4778 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4779 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4780 a129376b 2019-03-28 stsp goto done;
4781 a129376b 2019-03-28 stsp }
4782 a129376b 2019-03-28 stsp } else {
4783 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4784 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4786 a129376b 2019-03-28 stsp goto done;
4787 a129376b 2019-03-28 stsp }
4788 a129376b 2019-03-28 stsp }
4789 a129376b 2019-03-28 stsp }
4790 a129376b 2019-03-28 stsp
4791 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4792 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4793 945f9229 2022-04-16 thomas if (err)
4794 945f9229 2022-04-16 thomas goto done;
4795 945f9229 2022-04-16 thomas
4796 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4797 a9fa2909 2019-07-27 stsp if (err) {
4798 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4799 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4800 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4801 a9fa2909 2019-07-27 stsp goto done;
4802 a9fa2909 2019-07-27 stsp } else {
4803 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4804 a9fa2909 2019-07-27 stsp if (err)
4805 a9fa2909 2019-07-27 stsp goto done;
4806 a9fa2909 2019-07-27 stsp
4807 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4808 1233e6b6 2020-10-19 stsp if (err)
4809 a9fa2909 2019-07-27 stsp goto done;
4810 a9fa2909 2019-07-27 stsp
4811 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4812 1233e6b6 2020-10-19 stsp free(te_name);
4813 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4814 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4815 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4816 a9fa2909 2019-07-27 stsp goto done;
4817 a9fa2909 2019-07-27 stsp }
4818 a129376b 2019-03-28 stsp }
4819 a129376b 2019-03-28 stsp
4820 a129376b 2019-03-28 stsp switch (status) {
4821 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4822 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4823 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4824 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4825 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4826 33aa809d 2019-08-08 stsp if (err)
4827 33aa809d 2019-08-08 stsp goto done;
4828 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4829 33aa809d 2019-08-08 stsp break;
4830 33aa809d 2019-08-08 stsp }
4831 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4832 1f1abb7e 2019-08-08 stsp ie->path);
4833 1ee397ad 2019-07-12 stsp if (err)
4834 1ee397ad 2019-07-12 stsp goto done;
4835 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4836 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4837 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4838 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4839 10604dce 2021-09-24 thomas relpath) == -1) {
4840 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4841 10604dce 2021-09-24 thomas goto done;
4842 10604dce 2021-09-24 thomas }
4843 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4844 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4845 10604dce 2021-09-24 thomas ondisk_path);
4846 10604dce 2021-09-24 thomas break;
4847 10604dce 2021-09-24 thomas }
4848 10604dce 2021-09-24 thomas }
4849 a129376b 2019-03-28 stsp break;
4850 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4851 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4852 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4853 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4854 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4855 33aa809d 2019-08-08 stsp if (err)
4856 33aa809d 2019-08-08 stsp goto done;
4857 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4858 33aa809d 2019-08-08 stsp break;
4859 33aa809d 2019-08-08 stsp }
4860 33aa809d 2019-08-08 stsp /* fall through */
4861 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4862 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4863 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4864 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4865 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4866 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4867 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4868 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4869 43010591 2023-02-17 thomas else
4870 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4871 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4872 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4873 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4874 f4ae6ddb 2022-07-01 thomas goto done;
4875 f4ae6ddb 2022-07-01 thomas }
4876 f4ae6ddb 2022-07-01 thomas
4877 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4878 a129376b 2019-03-28 stsp if (err)
4879 65084dad 2019-08-08 stsp goto done;
4880 65084dad 2019-08-08 stsp
4881 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4882 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4883 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4884 a129376b 2019-03-28 stsp goto done;
4885 65084dad 2019-08-08 stsp }
4886 65084dad 2019-08-08 stsp
4887 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4888 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4889 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4890 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4891 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4892 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4893 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4894 33aa809d 2019-08-08 stsp break;
4895 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4896 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4897 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4898 369fd7e5 2020-07-23 stsp path_content);
4899 369fd7e5 2020-07-23 stsp break;
4900 369fd7e5 2020-07-23 stsp }
4901 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4902 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4903 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4904 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4905 369fd7e5 2020-07-23 stsp } else {
4906 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4907 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4908 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4909 369fd7e5 2020-07-23 stsp goto done;
4910 369fd7e5 2020-07-23 stsp }
4911 33aa809d 2019-08-08 stsp }
4912 33aa809d 2019-08-08 stsp } else {
4913 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4914 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4915 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4916 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4917 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4918 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4919 2e1fa222 2020-07-23 stsp } else {
4920 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4921 2e1fa222 2020-07-23 stsp ie->path,
4922 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4923 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4924 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4925 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4926 2e1fa222 2020-07-23 stsp }
4927 a129376b 2019-03-28 stsp if (err)
4928 a129376b 2019-03-28 stsp goto done;
4929 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4930 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4931 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4932 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4933 437adc9d 2020-12-10 yzhong blob->id.sha1,
4934 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4935 2e1fa222 2020-07-23 stsp if (err)
4936 2e1fa222 2020-07-23 stsp goto done;
4937 2e1fa222 2020-07-23 stsp }
4938 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4939 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4940 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4941 33aa809d 2019-08-08 stsp }
4942 a129376b 2019-03-28 stsp }
4943 a129376b 2019-03-28 stsp break;
4944 e20a8b6f 2019-06-04 stsp }
4945 a129376b 2019-03-28 stsp default:
4946 1f1abb7e 2019-08-08 stsp break;
4947 a129376b 2019-03-28 stsp }
4948 a129376b 2019-03-28 stsp done:
4949 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4950 33aa809d 2019-08-08 stsp free(path_content);
4951 e20a8b6f 2019-06-04 stsp free(parent_path);
4952 a129376b 2019-03-28 stsp free(tree_path);
4953 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4954 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4955 a129376b 2019-03-28 stsp if (blob)
4956 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4957 a129376b 2019-03-28 stsp if (tree)
4958 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4959 a129376b 2019-03-28 stsp free(tree_id);
4960 945f9229 2022-04-16 thomas if (base_commit)
4961 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4962 e20a8b6f 2019-06-04 stsp return err;
4963 e20a8b6f 2019-06-04 stsp }
4964 e20a8b6f 2019-06-04 stsp
4965 e20a8b6f 2019-06-04 stsp const struct got_error *
4966 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4967 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4968 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4969 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4970 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4971 e20a8b6f 2019-06-04 stsp {
4972 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4973 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4974 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4975 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4976 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4977 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4978 e20a8b6f 2019-06-04 stsp
4979 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4980 e20a8b6f 2019-06-04 stsp if (err)
4981 e20a8b6f 2019-06-04 stsp return err;
4982 e20a8b6f 2019-06-04 stsp
4983 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4984 e20a8b6f 2019-06-04 stsp if (err)
4985 e20a8b6f 2019-06-04 stsp goto done;
4986 e20a8b6f 2019-06-04 stsp
4987 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4988 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4989 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4990 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4991 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4992 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4993 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4994 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4995 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4996 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4997 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4998 e20a8b6f 2019-06-04 stsp if (err)
4999 af12c6b9 2019-06-04 stsp break;
5000 e20a8b6f 2019-06-04 stsp }
5001 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5002 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5003 e20a8b6f 2019-06-04 stsp err = sync_err;
5004 af12c6b9 2019-06-04 stsp done:
5005 fb399478 2019-07-12 stsp free(fileindex_path);
5006 a129376b 2019-03-28 stsp if (fileindex)
5007 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5008 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5009 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5010 a129376b 2019-03-28 stsp err = unlockerr;
5011 c4296144 2019-05-09 stsp return err;
5012 c4296144 2019-05-09 stsp }
5013 c4296144 2019-05-09 stsp
5014 cf066bf8 2019-05-09 stsp static void
5015 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5016 cf066bf8 2019-05-09 stsp {
5017 24519714 2019-05-09 stsp free(ct->path);
5018 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5019 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5020 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5021 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5022 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5023 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5024 cf066bf8 2019-05-09 stsp free(ct);
5025 cf066bf8 2019-05-09 stsp }
5026 24519714 2019-05-09 stsp
5027 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5028 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5029 24519714 2019-05-09 stsp struct got_repository *repo;
5030 24519714 2019-05-09 stsp struct got_worktree *worktree;
5031 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5032 5f8a88c6 2019-08-03 stsp int have_staged_files;
5033 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5034 ef899790 2022-11-01 thomas int diff_header_shown;
5035 be94c032 2023-02-20 thomas int commit_conflicts;
5036 ef899790 2022-11-01 thomas FILE *diff_outfile;
5037 ef899790 2022-11-01 thomas FILE *f1;
5038 ef899790 2022-11-01 thomas FILE *f2;
5039 24519714 2019-05-09 stsp };
5040 ef899790 2022-11-01 thomas
5041 ef899790 2022-11-01 thomas /*
5042 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5043 ef899790 2022-11-01 thomas * it as content to the diff engine.
5044 ef899790 2022-11-01 thomas */
5045 ef899790 2022-11-01 thomas static const struct got_error *
5046 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5047 ef899790 2022-11-01 thomas const char *abspath)
5048 ef899790 2022-11-01 thomas {
5049 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5050 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5051 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5052 ef899790 2022-11-01 thomas
5053 ef899790 2022-11-01 thomas *fd = -1;
5054 ef899790 2022-11-01 thomas
5055 ef899790 2022-11-01 thomas if (dirfd != -1) {
5056 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5057 ef899790 2022-11-01 thomas if (target_len == -1)
5058 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5059 ef899790 2022-11-01 thomas } else {
5060 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5061 ef899790 2022-11-01 thomas if (target_len == -1)
5062 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5063 ef899790 2022-11-01 thomas }
5064 ef899790 2022-11-01 thomas
5065 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5066 ef899790 2022-11-01 thomas if (*fd == -1)
5067 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5068 ef899790 2022-11-01 thomas
5069 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5070 ef899790 2022-11-01 thomas if (outlen == -1) {
5071 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5072 ef899790 2022-11-01 thomas goto done;
5073 ef899790 2022-11-01 thomas }
5074 ef899790 2022-11-01 thomas
5075 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5076 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5077 ef899790 2022-11-01 thomas goto done;
5078 ef899790 2022-11-01 thomas }
5079 ef899790 2022-11-01 thomas done:
5080 ef899790 2022-11-01 thomas if (err) {
5081 ef899790 2022-11-01 thomas close(*fd);
5082 ef899790 2022-11-01 thomas *fd = -1;
5083 ef899790 2022-11-01 thomas }
5084 ef899790 2022-11-01 thomas return err;
5085 ef899790 2022-11-01 thomas }
5086 ef899790 2022-11-01 thomas
5087 ef899790 2022-11-01 thomas static const struct got_error *
5088 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5089 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5090 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5091 ef899790 2022-11-01 thomas {
5092 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5093 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5094 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5095 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5096 ef899790 2022-11-01 thomas char *label1 = NULL;
5097 ef899790 2022-11-01 thomas struct stat sb;
5098 ef899790 2022-11-01 thomas off_t size1 = 0;
5099 ef899790 2022-11-01 thomas int f2_exists = 0;
5100 ef899790 2022-11-01 thomas char *id_str = NULL;
5101 ef899790 2022-11-01 thomas
5102 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5103 d259e491 2022-11-01 thomas
5104 d259e491 2022-11-01 thomas if (diff_staged) {
5105 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5106 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5107 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5108 d259e491 2022-11-01 thomas return NULL;
5109 d259e491 2022-11-01 thomas } else {
5110 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5111 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5112 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5113 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5114 d259e491 2022-11-01 thomas return NULL;
5115 d259e491 2022-11-01 thomas }
5116 ef899790 2022-11-01 thomas
5117 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5118 ef899790 2022-11-01 thomas if (err)
5119 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5120 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5121 ef899790 2022-11-01 thomas if (err)
5122 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5123 ef899790 2022-11-01 thomas
5124 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5125 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5126 ef899790 2022-11-01 thomas if (err)
5127 ef899790 2022-11-01 thomas return err;
5128 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5129 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5130 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5131 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5132 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5133 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5134 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5135 ef899790 2022-11-01 thomas }
5136 ef899790 2022-11-01 thomas
5137 ef899790 2022-11-01 thomas if (diff_staged) {
5138 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5139 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5140 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5141 ef899790 2022-11-01 thomas label1 = ct->path;
5142 ef899790 2022-11-01 thomas label2 = ct->path;
5143 ef899790 2022-11-01 thomas break;
5144 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5145 ef899790 2022-11-01 thomas label2 = ct->path;
5146 ef899790 2022-11-01 thomas break;
5147 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5148 ef899790 2022-11-01 thomas label1 = ct->path;
5149 ef899790 2022-11-01 thomas break;
5150 ef899790 2022-11-01 thomas default:
5151 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5152 ef899790 2022-11-01 thomas }
5153 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5154 ef899790 2022-11-01 thomas if (fd1 == -1) {
5155 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5156 ef899790 2022-11-01 thomas goto done;
5157 ef899790 2022-11-01 thomas }
5158 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5159 ef899790 2022-11-01 thomas if (fd2 == -1) {
5160 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5161 ef899790 2022-11-01 thomas goto done;
5162 ef899790 2022-11-01 thomas }
5163 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5164 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5165 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5166 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5167 ef899790 2022-11-01 thomas goto done;
5168 ef899790 2022-11-01 thomas }
5169 ef899790 2022-11-01 thomas
5170 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5171 ef899790 2022-11-01 thomas if (fd1 == -1) {
5172 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5173 ef899790 2022-11-01 thomas goto done;
5174 ef899790 2022-11-01 thomas }
5175 ef899790 2022-11-01 thomas
5176 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5177 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5178 ef899790 2022-11-01 thomas 8192, fd1);
5179 ef899790 2022-11-01 thomas if (err)
5180 ef899790 2022-11-01 thomas goto done;
5181 ef899790 2022-11-01 thomas }
5182 ef899790 2022-11-01 thomas
5183 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5184 ef899790 2022-11-01 thomas if (dirfd != -1) {
5185 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5186 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5187 ef899790 2022-11-01 thomas if (fd == -1) {
5188 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5189 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5190 ef899790 2022-11-01 thomas ct->ondisk_path);
5191 ef899790 2022-11-01 thomas goto done;
5192 ef899790 2022-11-01 thomas }
5193 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5194 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5195 ef899790 2022-11-01 thomas if (err)
5196 ef899790 2022-11-01 thomas goto done;
5197 ef899790 2022-11-01 thomas }
5198 ef899790 2022-11-01 thomas } else {
5199 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5200 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5201 ef899790 2022-11-01 thomas if (fd == -1) {
5202 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5203 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5204 ef899790 2022-11-01 thomas ct->ondisk_path);
5205 ef899790 2022-11-01 thomas goto done;
5206 ef899790 2022-11-01 thomas }
5207 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5208 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5209 ef899790 2022-11-01 thomas if (err)
5210 ef899790 2022-11-01 thomas goto done;
5211 ef899790 2022-11-01 thomas }
5212 ef899790 2022-11-01 thomas }
5213 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5214 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5215 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5216 ef899790 2022-11-01 thomas goto done;
5217 ef899790 2022-11-01 thomas }
5218 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5219 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5220 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5221 ef899790 2022-11-01 thomas goto done;
5222 ef899790 2022-11-01 thomas }
5223 ef899790 2022-11-01 thomas fd = -1;
5224 ef899790 2022-11-01 thomas f2_exists = 1;
5225 ef899790 2022-11-01 thomas }
5226 ef899790 2022-11-01 thomas
5227 ef899790 2022-11-01 thomas if (blob1) {
5228 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5229 ef899790 2022-11-01 thomas f1, blob1);
5230 ef899790 2022-11-01 thomas if (err)
5231 ef899790 2022-11-01 thomas goto done;
5232 ef899790 2022-11-01 thomas }
5233 ef899790 2022-11-01 thomas
5234 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5235 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5236 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5237 ef899790 2022-11-01 thomas done:
5238 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5239 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5240 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5241 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5242 ef899790 2022-11-01 thomas if (blob1)
5243 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5244 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5245 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5246 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5247 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5248 ef899790 2022-11-01 thomas return err;
5249 ef899790 2022-11-01 thomas }
5250 cf066bf8 2019-05-09 stsp
5251 c4296144 2019-05-09 stsp static const struct got_error *
5252 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5253 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5254 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5255 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5256 c4296144 2019-05-09 stsp {
5257 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5258 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5259 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5260 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5261 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5262 768aea60 2019-05-09 stsp struct stat sb;
5263 c4296144 2019-05-09 stsp
5264 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5265 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5266 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5267 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5268 5f8a88c6 2019-08-03 stsp return NULL;
5269 5f8a88c6 2019-08-03 stsp } else {
5270 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5271 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5272 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5273 be94c032 2023-02-20 thomas }
5274 c4296144 2019-05-09 stsp
5275 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5276 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5277 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5278 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5279 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5280 5f8a88c6 2019-08-03 stsp return NULL;
5281 5f8a88c6 2019-08-03 stsp }
5282 0b5cc0d6 2019-05-09 stsp
5283 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5284 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5285 036813ee 2019-05-09 stsp goto done;
5286 036813ee 2019-05-09 stsp }
5287 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5288 036813ee 2019-05-09 stsp parent_path = strdup("");
5289 69960a46 2019-05-09 stsp if (parent_path == NULL)
5290 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5291 69960a46 2019-05-09 stsp } else {
5292 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5293 69960a46 2019-05-09 stsp if (err)
5294 69960a46 2019-05-09 stsp return err;
5295 69960a46 2019-05-09 stsp }
5296 c4296144 2019-05-09 stsp
5297 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5298 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5299 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5300 c4296144 2019-05-09 stsp goto done;
5301 768aea60 2019-05-09 stsp }
5302 768aea60 2019-05-09 stsp
5303 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5304 768aea60 2019-05-09 stsp relpath) == -1) {
5305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5306 768aea60 2019-05-09 stsp goto done;
5307 768aea60 2019-05-09 stsp }
5308 0aeb8099 2020-07-23 stsp
5309 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5310 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5311 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5312 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5313 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5314 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5315 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5316 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5317 0aeb8099 2020-07-23 stsp break;
5318 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5319 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5320 0aeb8099 2020-07-23 stsp break;
5321 0aeb8099 2020-07-23 stsp default:
5322 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5323 0aeb8099 2020-07-23 stsp goto done;
5324 0aeb8099 2020-07-23 stsp }
5325 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5326 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5327 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5328 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5329 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5330 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5331 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5332 12463d8b 2019-12-13 stsp ct->ondisk_path);
5333 12463d8b 2019-12-13 stsp goto done;
5334 12463d8b 2019-12-13 stsp }
5335 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5336 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5337 768aea60 2019-05-09 stsp goto done;
5338 768aea60 2019-05-09 stsp }
5339 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5340 c4296144 2019-05-09 stsp }
5341 c4296144 2019-05-09 stsp
5342 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5343 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5344 44d03001 2019-05-09 stsp relpath) == -1) {
5345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5346 44d03001 2019-05-09 stsp goto done;
5347 44d03001 2019-05-09 stsp }
5348 44d03001 2019-05-09 stsp
5349 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5350 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5351 35213c7c 2020-07-23 stsp int is_bad_symlink;
5352 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5353 35213c7c 2020-07-23 stsp ssize_t target_len;
5354 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5355 35213c7c 2020-07-23 stsp sizeof(target_path));
5356 35213c7c 2020-07-23 stsp if (target_len == -1) {
5357 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5358 35213c7c 2020-07-23 stsp ct->ondisk_path);
5359 35213c7c 2020-07-23 stsp goto done;
5360 35213c7c 2020-07-23 stsp }
5361 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5362 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5363 35213c7c 2020-07-23 stsp if (err)
5364 35213c7c 2020-07-23 stsp goto done;
5365 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5366 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5367 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5368 35213c7c 2020-07-23 stsp goto done;
5369 35213c7c 2020-07-23 stsp }
5370 35213c7c 2020-07-23 stsp }
5371 35213c7c 2020-07-23 stsp
5372 35213c7c 2020-07-23 stsp
5373 cf066bf8 2019-05-09 stsp ct->status = status;
5374 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5375 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5376 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5377 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5378 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5379 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5380 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5381 b416585c 2019-05-13 stsp goto done;
5382 b416585c 2019-05-13 stsp }
5383 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5384 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5385 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5386 f0b75401 2019-08-03 stsp goto done;
5387 f0b75401 2019-08-03 stsp }
5388 f0b75401 2019-08-03 stsp }
5389 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5390 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5391 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5392 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5393 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5394 036813ee 2019-05-09 stsp goto done;
5395 036813ee 2019-05-09 stsp }
5396 ca2503ea 2019-05-09 stsp }
5397 24519714 2019-05-09 stsp ct->path = strdup(path);
5398 24519714 2019-05-09 stsp if (ct->path == NULL) {
5399 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5400 24519714 2019-05-09 stsp goto done;
5401 24519714 2019-05-09 stsp }
5402 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5403 ef899790 2022-11-01 thomas if (err)
5404 ef899790 2022-11-01 thomas goto done;
5405 ef899790 2022-11-01 thomas
5406 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5407 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5408 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5409 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5410 ef899790 2022-11-01 thomas if (err)
5411 ef899790 2022-11-01 thomas goto done;
5412 ef899790 2022-11-01 thomas }
5413 c4296144 2019-05-09 stsp done:
5414 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5415 ed175427 2019-05-09 stsp free_commitable(ct);
5416 24519714 2019-05-09 stsp free(parent_path);
5417 036813ee 2019-05-09 stsp free(path);
5418 a129376b 2019-03-28 stsp return err;
5419 a129376b 2019-03-28 stsp }
5420 c4296144 2019-05-09 stsp
5421 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5422 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5423 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5424 036813ee 2019-05-09 stsp struct got_repository *);
5425 ed175427 2019-05-09 stsp
5426 ed175427 2019-05-09 stsp static const struct got_error *
5427 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5428 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5429 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5430 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5431 afa376bf 2019-05-09 stsp struct got_repository *repo)
5432 ed175427 2019-05-09 stsp {
5433 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5434 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5435 036813ee 2019-05-09 stsp char *subpath;
5436 ed175427 2019-05-09 stsp
5437 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5438 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5439 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5440 ed175427 2019-05-09 stsp
5441 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5442 ed175427 2019-05-09 stsp if (err)
5443 ed175427 2019-05-09 stsp return err;
5444 ed175427 2019-05-09 stsp
5445 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5446 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5447 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5448 036813ee 2019-05-09 stsp free(subpath);
5449 036813ee 2019-05-09 stsp return err;
5450 036813ee 2019-05-09 stsp }
5451 ed175427 2019-05-09 stsp
5452 036813ee 2019-05-09 stsp static const struct got_error *
5453 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5454 036813ee 2019-05-09 stsp {
5455 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5456 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5457 ed175427 2019-05-09 stsp
5458 036813ee 2019-05-09 stsp *match = 0;
5459 ed175427 2019-05-09 stsp
5460 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5461 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5462 0f63689d 2019-05-10 stsp return NULL;
5463 036813ee 2019-05-09 stsp }
5464 0b5cc0d6 2019-05-09 stsp
5465 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5466 0f63689d 2019-05-10 stsp if (err)
5467 0f63689d 2019-05-10 stsp return err;
5468 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5469 036813ee 2019-05-09 stsp free(ct_parent_path);
5470 036813ee 2019-05-09 stsp return err;
5471 036813ee 2019-05-09 stsp }
5472 0b5cc0d6 2019-05-09 stsp
5473 768aea60 2019-05-09 stsp static mode_t
5474 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5475 768aea60 2019-05-09 stsp {
5476 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5477 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5478 3d9a4ec4 2020-07-23 stsp
5479 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5480 768aea60 2019-05-09 stsp }
5481 768aea60 2019-05-09 stsp
5482 036813ee 2019-05-09 stsp static const struct got_error *
5483 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5484 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5485 036813ee 2019-05-09 stsp {
5486 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5487 ca2503ea 2019-05-09 stsp
5488 036813ee 2019-05-09 stsp *new_te = NULL;
5489 0b5cc0d6 2019-05-09 stsp
5490 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5491 036813ee 2019-05-09 stsp if (err)
5492 036813ee 2019-05-09 stsp goto done;
5493 0b5cc0d6 2019-05-09 stsp
5494 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5495 036813ee 2019-05-09 stsp
5496 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5497 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5498 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5499 5f8a88c6 2019-08-03 stsp else
5500 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5501 036813ee 2019-05-09 stsp done:
5502 036813ee 2019-05-09 stsp if (err && *new_te) {
5503 56e0773d 2019-11-28 stsp free(*new_te);
5504 036813ee 2019-05-09 stsp *new_te = NULL;
5505 036813ee 2019-05-09 stsp }
5506 036813ee 2019-05-09 stsp return err;
5507 036813ee 2019-05-09 stsp }
5508 036813ee 2019-05-09 stsp
5509 036813ee 2019-05-09 stsp static const struct got_error *
5510 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5511 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5512 036813ee 2019-05-09 stsp {
5513 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5514 102b254e 2020-10-19 stsp char *ct_name = NULL;
5515 036813ee 2019-05-09 stsp
5516 036813ee 2019-05-09 stsp *new_te = NULL;
5517 036813ee 2019-05-09 stsp
5518 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5519 036813ee 2019-05-09 stsp if (*new_te == NULL)
5520 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5521 036813ee 2019-05-09 stsp
5522 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5523 102b254e 2020-10-19 stsp if (err)
5524 036813ee 2019-05-09 stsp goto done;
5525 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5526 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5527 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5528 036813ee 2019-05-09 stsp goto done;
5529 036813ee 2019-05-09 stsp }
5530 036813ee 2019-05-09 stsp
5531 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5532 036813ee 2019-05-09 stsp
5533 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5534 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5535 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5536 5f8a88c6 2019-08-03 stsp else
5537 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5538 036813ee 2019-05-09 stsp done:
5539 102b254e 2020-10-19 stsp free(ct_name);
5540 036813ee 2019-05-09 stsp if (err && *new_te) {
5541 56e0773d 2019-11-28 stsp free(*new_te);
5542 036813ee 2019-05-09 stsp *new_te = NULL;
5543 036813ee 2019-05-09 stsp }
5544 036813ee 2019-05-09 stsp return err;
5545 036813ee 2019-05-09 stsp }
5546 036813ee 2019-05-09 stsp
5547 036813ee 2019-05-09 stsp static const struct got_error *
5548 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5549 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5550 036813ee 2019-05-09 stsp {
5551 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5552 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5553 036813ee 2019-05-09 stsp
5554 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5555 036813ee 2019-05-09 stsp if (err)
5556 036813ee 2019-05-09 stsp return err;
5557 036813ee 2019-05-09 stsp if (new_pe == NULL)
5558 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5559 036813ee 2019-05-09 stsp return NULL;
5560 afa376bf 2019-05-09 stsp }
5561 afa376bf 2019-05-09 stsp
5562 afa376bf 2019-05-09 stsp static const struct got_error *
5563 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5564 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5565 afa376bf 2019-05-09 stsp {
5566 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5567 5f8a88c6 2019-08-03 stsp unsigned char status;
5568 ae1e948a 2021-09-28 thomas
5569 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5570 ae1e948a 2021-09-28 thomas return NULL;
5571 5f8a88c6 2019-08-03 stsp
5572 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5573 afa376bf 2019-05-09 stsp ct_path++;
5574 5f8a88c6 2019-08-03 stsp
5575 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5576 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5577 5f8a88c6 2019-08-03 stsp else
5578 5f8a88c6 2019-08-03 stsp status = ct->status;
5579 5f8a88c6 2019-08-03 stsp
5580 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5581 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5582 036813ee 2019-05-09 stsp }
5583 036813ee 2019-05-09 stsp
5584 036813ee 2019-05-09 stsp static const struct got_error *
5585 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5586 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5587 44d03001 2019-05-09 stsp {
5588 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5589 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5590 44d03001 2019-05-09 stsp char *te_path;
5591 44d03001 2019-05-09 stsp
5592 44d03001 2019-05-09 stsp *modified = 0;
5593 44d03001 2019-05-09 stsp
5594 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5595 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5596 44d03001 2019-05-09 stsp te->name) == -1)
5597 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5598 44d03001 2019-05-09 stsp
5599 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5600 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5601 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5602 44d03001 2019-05-09 stsp strlen(te_path));
5603 62d463ca 2020-10-20 naddy if (*modified)
5604 44d03001 2019-05-09 stsp break;
5605 44d03001 2019-05-09 stsp }
5606 44d03001 2019-05-09 stsp
5607 44d03001 2019-05-09 stsp free(te_path);
5608 44d03001 2019-05-09 stsp return err;
5609 44d03001 2019-05-09 stsp }
5610 44d03001 2019-05-09 stsp
5611 44d03001 2019-05-09 stsp static const struct got_error *
5612 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5613 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5614 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5615 036813ee 2019-05-09 stsp {
5616 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5617 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5618 036813ee 2019-05-09 stsp
5619 036813ee 2019-05-09 stsp *ctp = NULL;
5620 036813ee 2019-05-09 stsp
5621 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5622 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5623 036813ee 2019-05-09 stsp char *ct_name = NULL;
5624 036813ee 2019-05-09 stsp int path_matches;
5625 036813ee 2019-05-09 stsp
5626 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5627 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5628 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5629 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5630 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5631 5f8a88c6 2019-08-03 stsp continue;
5632 5f8a88c6 2019-08-03 stsp } else {
5633 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5634 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5635 5f8a88c6 2019-08-03 stsp continue;
5636 5f8a88c6 2019-08-03 stsp }
5637 036813ee 2019-05-09 stsp
5638 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5639 036813ee 2019-05-09 stsp continue;
5640 036813ee 2019-05-09 stsp
5641 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5642 62d463ca 2020-10-20 naddy if (err)
5643 036813ee 2019-05-09 stsp return err;
5644 036813ee 2019-05-09 stsp if (!path_matches)
5645 036813ee 2019-05-09 stsp continue;
5646 036813ee 2019-05-09 stsp
5647 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5648 d34b633e 2020-10-19 stsp if (err)
5649 d34b633e 2020-10-19 stsp return err;
5650 d34b633e 2020-10-19 stsp
5651 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5652 d34b633e 2020-10-19 stsp free(ct_name);
5653 036813ee 2019-05-09 stsp continue;
5654 d34b633e 2020-10-19 stsp }
5655 d34b633e 2020-10-19 stsp free(ct_name);
5656 036813ee 2019-05-09 stsp
5657 036813ee 2019-05-09 stsp *ctp = ct;
5658 036813ee 2019-05-09 stsp break;
5659 036813ee 2019-05-09 stsp }
5660 2b496619 2019-07-10 stsp
5661 2b496619 2019-07-10 stsp return err;
5662 2b496619 2019-07-10 stsp }
5663 2b496619 2019-07-10 stsp
5664 2b496619 2019-07-10 stsp static const struct got_error *
5665 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5666 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5667 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5668 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5669 2b496619 2019-07-10 stsp struct got_repository *repo)
5670 2b496619 2019-07-10 stsp {
5671 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5672 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5673 2b496619 2019-07-10 stsp char *subtree_path;
5674 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5675 ba580f68 2020-03-22 stsp int nentries;
5676 2b496619 2019-07-10 stsp
5677 2b496619 2019-07-10 stsp *new_tep = NULL;
5678 2b496619 2019-07-10 stsp
5679 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5680 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5681 2b496619 2019-07-10 stsp child_path) == -1)
5682 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5683 036813ee 2019-05-09 stsp
5684 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5685 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5686 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5687 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5688 56e0773d 2019-11-28 stsp
5689 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5690 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5691 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5692 2b496619 2019-07-10 stsp goto done;
5693 2b496619 2019-07-10 stsp }
5694 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5695 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5696 2b496619 2019-07-10 stsp if (err) {
5697 56e0773d 2019-11-28 stsp free(new_te);
5698 2b496619 2019-07-10 stsp goto done;
5699 2b496619 2019-07-10 stsp }
5700 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5701 2b496619 2019-07-10 stsp done:
5702 56e0773d 2019-11-28 stsp free(id);
5703 2b496619 2019-07-10 stsp free(subtree_path);
5704 2b496619 2019-07-10 stsp if (err == NULL)
5705 2b496619 2019-07-10 stsp *new_tep = new_te;
5706 036813ee 2019-05-09 stsp return err;
5707 036813ee 2019-05-09 stsp }
5708 036813ee 2019-05-09 stsp
5709 036813ee 2019-05-09 stsp static const struct got_error *
5710 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5711 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5712 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5713 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5714 036813ee 2019-05-09 stsp struct got_repository *repo)
5715 036813ee 2019-05-09 stsp {
5716 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5717 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5718 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5719 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5720 036813ee 2019-05-09 stsp
5721 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5722 ba580f68 2020-03-22 stsp *nentries = 0;
5723 036813ee 2019-05-09 stsp
5724 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5725 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5726 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5727 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5728 036813ee 2019-05-09 stsp
5729 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5730 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5731 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5732 036813ee 2019-05-09 stsp continue;
5733 036813ee 2019-05-09 stsp
5734 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5735 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5736 036813ee 2019-05-09 stsp continue;
5737 036813ee 2019-05-09 stsp
5738 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5739 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5740 036813ee 2019-05-09 stsp if (err)
5741 036813ee 2019-05-09 stsp goto done;
5742 036813ee 2019-05-09 stsp
5743 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5744 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5745 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5746 036813ee 2019-05-09 stsp if (err)
5747 036813ee 2019-05-09 stsp goto done;
5748 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5749 afa376bf 2019-05-09 stsp if (err)
5750 afa376bf 2019-05-09 stsp goto done;
5751 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5752 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5753 2b496619 2019-07-10 stsp if (err)
5754 2f51b5b3 2019-05-09 stsp goto done;
5755 ba580f68 2020-03-22 stsp (*nentries)++;
5756 2b496619 2019-07-10 stsp } else {
5757 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5758 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5759 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5760 2b496619 2019-07-10 stsp == NULL) {
5761 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5762 2b496619 2019-07-10 stsp child_path, path_base_tree,
5763 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5764 2b496619 2019-07-10 stsp repo);
5765 2b496619 2019-07-10 stsp if (err)
5766 2b496619 2019-07-10 stsp goto done;
5767 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5768 2b496619 2019-07-10 stsp if (err)
5769 2b496619 2019-07-10 stsp goto done;
5770 ba580f68 2020-03-22 stsp (*nentries)++;
5771 9ba0479c 2019-05-10 stsp }
5772 ed175427 2019-05-09 stsp }
5773 2f51b5b3 2019-05-09 stsp }
5774 2f51b5b3 2019-05-09 stsp
5775 2f51b5b3 2019-05-09 stsp if (base_tree) {
5776 56e0773d 2019-11-28 stsp int i, nbase_entries;
5777 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5778 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5779 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5780 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5781 63c5ca5d 2019-08-24 stsp
5782 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5783 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5784 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5785 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5786 63c5ca5d 2019-08-24 stsp if (err)
5787 63c5ca5d 2019-08-24 stsp goto done;
5788 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5789 63c5ca5d 2019-08-24 stsp if (err)
5790 63c5ca5d 2019-08-24 stsp goto done;
5791 ba580f68 2020-03-22 stsp (*nentries)++;
5792 63c5ca5d 2019-08-24 stsp continue;
5793 63c5ca5d 2019-08-24 stsp }
5794 2f51b5b3 2019-05-09 stsp
5795 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5796 44d03001 2019-05-09 stsp int modified;
5797 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5798 036813ee 2019-05-09 stsp if (err)
5799 036813ee 2019-05-09 stsp goto done;
5800 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5801 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5802 2f51b5b3 2019-05-09 stsp if (err)
5803 2f51b5b3 2019-05-09 stsp goto done;
5804 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5805 44d03001 2019-05-09 stsp if (modified) {
5806 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5807 ba580f68 2020-03-22 stsp int nsubentries;
5808 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5809 ba580f68 2020-03-22 stsp &nsubentries, te,
5810 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5811 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5812 44d03001 2019-05-09 stsp if (err)
5813 44d03001 2019-05-09 stsp goto done;
5814 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5815 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5816 ba580f68 2020-03-22 stsp free(new_id);
5817 ba580f68 2020-03-22 stsp continue;
5818 ba580f68 2020-03-22 stsp }
5819 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5820 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5821 56e0773d 2019-11-28 stsp free(new_id);
5822 44d03001 2019-05-09 stsp }
5823 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5824 036813ee 2019-05-09 stsp if (err)
5825 036813ee 2019-05-09 stsp goto done;
5826 ba580f68 2020-03-22 stsp (*nentries)++;
5827 2f51b5b3 2019-05-09 stsp continue;
5828 036813ee 2019-05-09 stsp }
5829 2f51b5b3 2019-05-09 stsp
5830 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5831 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5832 f66c734c 2019-09-22 stsp if (err)
5833 f66c734c 2019-09-22 stsp goto done;
5834 2f51b5b3 2019-05-09 stsp if (ct) {
5835 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5836 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5837 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5838 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5839 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5840 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5841 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5842 2f51b5b3 2019-05-09 stsp if (err)
5843 2f51b5b3 2019-05-09 stsp goto done;
5844 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5845 2f51b5b3 2019-05-09 stsp if (err)
5846 2f51b5b3 2019-05-09 stsp goto done;
5847 ba580f68 2020-03-22 stsp (*nentries)++;
5848 2f51b5b3 2019-05-09 stsp }
5849 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5850 b416585c 2019-05-13 stsp status_arg);
5851 afa376bf 2019-05-09 stsp if (err)
5852 afa376bf 2019-05-09 stsp goto done;
5853 2f51b5b3 2019-05-09 stsp } else {
5854 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5855 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5856 2f51b5b3 2019-05-09 stsp if (err)
5857 2f51b5b3 2019-05-09 stsp goto done;
5858 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5859 2f51b5b3 2019-05-09 stsp if (err)
5860 2f51b5b3 2019-05-09 stsp goto done;
5861 ba580f68 2020-03-22 stsp (*nentries)++;
5862 2f51b5b3 2019-05-09 stsp }
5863 ca2503ea 2019-05-09 stsp }
5864 ed175427 2019-05-09 stsp }
5865 0b5cc0d6 2019-05-09 stsp
5866 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5867 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5868 ed175427 2019-05-09 stsp done:
5869 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5870 2e1fa222 2020-07-23 stsp return err;
5871 2e1fa222 2020-07-23 stsp }
5872 2e1fa222 2020-07-23 stsp
5873 2e1fa222 2020-07-23 stsp static const struct got_error *
5874 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5875 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5876 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5877 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5878 ebf99748 2019-05-09 stsp {
5879 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5880 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5881 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5882 ebf99748 2019-05-09 stsp
5883 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5884 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5885 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5886 ebf99748 2019-05-09 stsp
5887 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5888 437adc9d 2020-12-10 yzhong
5889 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5890 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5891 437adc9d 2020-12-10 yzhong if (err)
5892 437adc9d 2020-12-10 yzhong goto done;
5893 437adc9d 2020-12-10 yzhong
5894 ebf99748 2019-05-09 stsp if (ie) {
5895 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5896 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5897 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5898 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5899 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5900 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5901 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5902 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5903 437adc9d 2020-12-10 yzhong
5904 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5905 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5906 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5907 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5908 72fd46fa 2019-09-06 stsp !have_staged_files);
5909 ebf99748 2019-05-09 stsp } else
5910 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5911 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5912 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5913 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5914 72fd46fa 2019-09-06 stsp !have_staged_files);
5915 ebf99748 2019-05-09 stsp } else {
5916 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5917 ebf99748 2019-05-09 stsp if (err)
5918 437adc9d 2020-12-10 yzhong goto done;
5919 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5920 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5921 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5922 3969253a 2020-03-07 stsp if (err) {
5923 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5924 437adc9d 2020-12-10 yzhong goto done;
5925 3969253a 2020-03-07 stsp }
5926 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5927 3969253a 2020-03-07 stsp if (err) {
5928 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5929 437adc9d 2020-12-10 yzhong goto done;
5930 3969253a 2020-03-07 stsp }
5931 ebf99748 2019-05-09 stsp }
5932 437adc9d 2020-12-10 yzhong free(relpath);
5933 437adc9d 2020-12-10 yzhong relpath = NULL;
5934 ebf99748 2019-05-09 stsp }
5935 437adc9d 2020-12-10 yzhong done:
5936 437adc9d 2020-12-10 yzhong free(relpath);
5937 d56d26ce 2019-05-10 stsp return err;
5938 d56d26ce 2019-05-10 stsp }
5939 735ef5ac 2019-08-03 stsp
5940 d56d26ce 2019-05-10 stsp
5941 d56d26ce 2019-05-10 stsp static const struct got_error *
5942 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5943 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5944 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5945 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5946 735ef5ac 2019-08-03 stsp int ood_errcode)
5947 d56d26ce 2019-05-10 stsp {
5948 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5949 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5950 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5951 1a36436d 2019-06-10 stsp
5952 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5953 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5954 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5955 1a36436d 2019-06-10 stsp return NULL;
5956 9bead371 2019-07-28 stsp /*
5957 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5958 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5959 9bead371 2019-07-28 stsp */
5960 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5961 945f9229 2022-04-16 thomas if (err)
5962 945f9229 2022-04-16 thomas goto done;
5963 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5964 9bead371 2019-07-28 stsp if (err) {
5965 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5966 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5967 1a36436d 2019-06-10 stsp goto done;
5968 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5969 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5970 1a36436d 2019-06-10 stsp } else {
5971 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5972 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5973 945f9229 2022-04-16 thomas if (err)
5974 945f9229 2022-04-16 thomas goto done;
5975 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5976 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5977 1a36436d 2019-06-10 stsp goto done;
5978 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5979 1a36436d 2019-06-10 stsp }
5980 1a36436d 2019-06-10 stsp done:
5981 1a36436d 2019-06-10 stsp free(id);
5982 945f9229 2022-04-16 thomas if (commit)
5983 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5984 1a36436d 2019-06-10 stsp return err;
5985 ebf99748 2019-05-09 stsp }
5986 ebf99748 2019-05-09 stsp
5987 ef20f542 2022-06-26 thomas static const struct got_error *
5988 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5989 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5990 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5991 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5992 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5993 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5994 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5995 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5996 afa376bf 2019-05-09 stsp struct got_repository *repo)
5997 c4296144 2019-05-09 stsp {
5998 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5999 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6000 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6001 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6002 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6003 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6004 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6005 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6006 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6007 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6008 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6009 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6010 892eab0a 2022-07-22 thomas time_t timestamp;
6011 c4296144 2019-05-09 stsp
6012 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6013 c4296144 2019-05-09 stsp
6014 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6015 675c7539 2019-05-09 stsp
6016 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6017 588edf97 2019-05-10 stsp if (err)
6018 588edf97 2019-05-10 stsp goto done;
6019 de18fc63 2019-05-09 stsp
6020 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6021 588edf97 2019-05-10 stsp if (err)
6022 588edf97 2019-05-10 stsp goto done;
6023 588edf97 2019-05-10 stsp
6024 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6025 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6026 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6027 33ad4cbe 2019-05-12 jcs if (err)
6028 33ad4cbe 2019-05-12 jcs goto done;
6029 33ad4cbe 2019-05-12 jcs }
6030 c4296144 2019-05-09 stsp
6031 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6032 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6033 33ad4cbe 2019-05-12 jcs goto done;
6034 33ad4cbe 2019-05-12 jcs }
6035 33ad4cbe 2019-05-12 jcs
6036 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6037 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6038 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6039 cf066bf8 2019-05-09 stsp char *ondisk_path;
6040 cf066bf8 2019-05-09 stsp
6041 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6042 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6043 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6044 5f8a88c6 2019-08-03 stsp continue;
6045 5f8a88c6 2019-08-03 stsp
6046 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6047 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6048 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6049 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6050 cf066bf8 2019-05-09 stsp continue;
6051 cf066bf8 2019-05-09 stsp
6052 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6053 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6054 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6055 cf066bf8 2019-05-09 stsp goto done;
6056 cf066bf8 2019-05-09 stsp }
6057 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6058 2e1fa222 2020-07-23 stsp free(ondisk_path);
6059 2e1fa222 2020-07-23 stsp if (err)
6060 cf066bf8 2019-05-09 stsp goto done;
6061 cf066bf8 2019-05-09 stsp }
6062 036813ee 2019-05-09 stsp
6063 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6064 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6065 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6066 036813ee 2019-05-09 stsp if (err)
6067 036813ee 2019-05-09 stsp goto done;
6068 036813ee 2019-05-09 stsp
6069 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6070 de18fc63 2019-05-09 stsp if (err)
6071 de18fc63 2019-05-09 stsp goto done;
6072 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6073 10604dce 2021-09-24 thomas nparents++;
6074 10604dce 2021-09-24 thomas if (parent_id2) {
6075 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6076 10604dce 2021-09-24 thomas if (err)
6077 10604dce 2021-09-24 thomas goto done;
6078 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6079 10604dce 2021-09-24 thomas nparents++;
6080 10604dce 2021-09-24 thomas }
6081 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6082 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6083 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6084 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6085 33ad4cbe 2019-05-12 jcs free(logmsg);
6086 9d40349a 2019-05-09 stsp if (err)
6087 9d40349a 2019-05-09 stsp goto done;
6088 9d40349a 2019-05-09 stsp
6089 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6090 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6091 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6092 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6093 09f5bd90 2019-05-09 stsp goto done;
6094 09f5bd90 2019-05-09 stsp }
6095 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6096 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6097 09f5bd90 2019-05-09 stsp if (err)
6098 09f5bd90 2019-05-09 stsp goto done;
6099 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6100 ebf99748 2019-05-09 stsp if (err)
6101 ebf99748 2019-05-09 stsp goto done;
6102 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6103 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6104 09f5bd90 2019-05-09 stsp goto done;
6105 09f5bd90 2019-05-09 stsp }
6106 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6107 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6108 f2c16586 2019-05-09 stsp if (err)
6109 f2c16586 2019-05-09 stsp goto done;
6110 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6111 f2c16586 2019-05-09 stsp if (err)
6112 f2c16586 2019-05-09 stsp goto done;
6113 f2c16586 2019-05-09 stsp
6114 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6115 09f5bd90 2019-05-09 stsp if (err)
6116 09f5bd90 2019-05-09 stsp goto done;
6117 09f5bd90 2019-05-09 stsp
6118 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6119 ebf99748 2019-05-09 stsp if (err)
6120 ebf99748 2019-05-09 stsp goto done;
6121 c4296144 2019-05-09 stsp done:
6122 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6123 588edf97 2019-05-10 stsp if (head_tree)
6124 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6125 588edf97 2019-05-10 stsp if (head_commit)
6126 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6127 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6128 2f17228e 2019-05-12 stsp if (head_ref2) {
6129 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6130 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6131 2f17228e 2019-05-12 stsp err = unlockerr;
6132 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6133 39cd0ff6 2019-07-12 stsp }
6134 39cd0ff6 2019-07-12 stsp return err;
6135 39cd0ff6 2019-07-12 stsp }
6136 5c1e53bc 2019-07-28 stsp
6137 5c1e53bc 2019-07-28 stsp static const struct got_error *
6138 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6139 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6140 5c1e53bc 2019-07-28 stsp {
6141 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6142 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6143 39cd0ff6 2019-07-12 stsp
6144 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6145 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6146 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6147 5c1e53bc 2019-07-28 stsp
6148 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6149 5c1e53bc 2019-07-28 stsp ct_path++;
6150 5c1e53bc 2019-07-28 stsp
6151 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6152 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6153 5c1e53bc 2019-07-28 stsp break;
6154 5c1e53bc 2019-07-28 stsp }
6155 5c1e53bc 2019-07-28 stsp
6156 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6157 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6158 5c1e53bc 2019-07-28 stsp
6159 5c1e53bc 2019-07-28 stsp return NULL;
6160 5c1e53bc 2019-07-28 stsp }
6161 5c1e53bc 2019-07-28 stsp
6162 f0b75401 2019-08-03 stsp static const struct got_error *
6163 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6164 f0b75401 2019-08-03 stsp {
6165 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6166 f0b75401 2019-08-03 stsp
6167 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6168 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6169 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6170 f0b75401 2019-08-03 stsp }
6171 f0b75401 2019-08-03 stsp
6172 f0b75401 2019-08-03 stsp return NULL;
6173 f0b75401 2019-08-03 stsp }
6174 f0b75401 2019-08-03 stsp
6175 5f8a88c6 2019-08-03 stsp static const struct got_error *
6176 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6177 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6178 5f8a88c6 2019-08-03 stsp {
6179 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6180 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6181 5f8a88c6 2019-08-03 stsp
6182 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6183 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6184 5f8a88c6 2019-08-03 stsp continue;
6185 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6186 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6187 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6188 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6189 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6190 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6191 5f8a88c6 2019-08-03 stsp }
6192 5f8a88c6 2019-08-03 stsp
6193 5f8a88c6 2019-08-03 stsp return NULL;
6194 5f8a88c6 2019-08-03 stsp }
6195 5f8a88c6 2019-08-03 stsp
6196 39cd0ff6 2019-07-12 stsp const struct got_error *
6197 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6198 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6199 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6200 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6201 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6202 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6203 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6204 39cd0ff6 2019-07-12 stsp {
6205 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6206 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6207 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6208 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6209 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6210 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6211 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6212 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6213 ef899790 2022-11-01 thomas char *diff_path = NULL;
6214 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6215 39cd0ff6 2019-07-12 stsp
6216 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6217 39cd0ff6 2019-07-12 stsp
6218 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6219 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6220 39cd0ff6 2019-07-12 stsp
6221 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6222 39cd0ff6 2019-07-12 stsp if (err)
6223 39cd0ff6 2019-07-12 stsp goto done;
6224 39cd0ff6 2019-07-12 stsp
6225 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6226 39cd0ff6 2019-07-12 stsp if (err)
6227 39cd0ff6 2019-07-12 stsp goto done;
6228 39cd0ff6 2019-07-12 stsp
6229 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6230 39cd0ff6 2019-07-12 stsp if (err)
6231 39cd0ff6 2019-07-12 stsp goto done;
6232 39cd0ff6 2019-07-12 stsp
6233 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6234 39cd0ff6 2019-07-12 stsp if (err)
6235 39cd0ff6 2019-07-12 stsp goto done;
6236 39cd0ff6 2019-07-12 stsp
6237 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6238 5f8a88c6 2019-08-03 stsp &have_staged_files);
6239 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6240 5f8a88c6 2019-08-03 stsp goto done;
6241 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6242 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6243 5f8a88c6 2019-08-03 stsp if (err)
6244 5f8a88c6 2019-08-03 stsp goto done;
6245 5f8a88c6 2019-08-03 stsp }
6246 5f8a88c6 2019-08-03 stsp
6247 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6248 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6249 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6250 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6251 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6252 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6253 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6254 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6255 ef899790 2022-11-01 thomas if (show_diff) {
6256 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6257 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6258 ef899790 2022-11-01 thomas if (err)
6259 ef899790 2022-11-01 thomas goto done;
6260 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6261 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6262 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6263 ef899790 2022-11-01 thomas goto done;
6264 ef899790 2022-11-01 thomas }
6265 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6266 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6267 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6268 ef899790 2022-11-01 thomas goto done;
6269 ef899790 2022-11-01 thomas }
6270 ef899790 2022-11-01 thomas }
6271 ef899790 2022-11-01 thomas
6272 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6273 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6274 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6275 5c1e53bc 2019-07-28 stsp if (err)
6276 5c1e53bc 2019-07-28 stsp goto done;
6277 5c1e53bc 2019-07-28 stsp }
6278 39cd0ff6 2019-07-12 stsp
6279 ef899790 2022-11-01 thomas if (show_diff) {
6280 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6281 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6282 ef899790 2022-11-01 thomas goto done;
6283 ef899790 2022-11-01 thomas }
6284 ef899790 2022-11-01 thomas }
6285 ef899790 2022-11-01 thomas
6286 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6287 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6288 39cd0ff6 2019-07-12 stsp goto done;
6289 5c1e53bc 2019-07-28 stsp }
6290 5c1e53bc 2019-07-28 stsp
6291 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6292 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6293 5c1e53bc 2019-07-28 stsp if (err)
6294 5c1e53bc 2019-07-28 stsp goto done;
6295 39cd0ff6 2019-07-12 stsp }
6296 f0b75401 2019-08-03 stsp
6297 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6298 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6299 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6300 f0b75401 2019-08-03 stsp
6301 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6302 f0b75401 2019-08-03 stsp ct_path++;
6303 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6304 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6305 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6306 b50cabdf 2019-07-12 stsp if (err)
6307 b50cabdf 2019-07-12 stsp goto done;
6308 f0b75401 2019-08-03 stsp
6309 b50cabdf 2019-07-12 stsp }
6310 b50cabdf 2019-07-12 stsp
6311 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6312 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6313 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6314 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6315 39cd0ff6 2019-07-12 stsp if (err)
6316 39cd0ff6 2019-07-12 stsp goto done;
6317 39cd0ff6 2019-07-12 stsp
6318 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6319 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6320 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6321 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6322 39cd0ff6 2019-07-12 stsp err = sync_err;
6323 39cd0ff6 2019-07-12 stsp done:
6324 39cd0ff6 2019-07-12 stsp if (fileindex)
6325 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6326 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6327 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6328 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6329 39cd0ff6 2019-07-12 stsp err = unlockerr;
6330 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6331 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6332 21c2d8be 2023-01-10 thomas
6333 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6334 39cd0ff6 2019-07-12 stsp }
6335 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6336 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6337 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6338 ef899790 2022-11-01 thomas free(diff_path);
6339 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6340 ef899790 2022-11-01 thomas err == NULL)
6341 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6342 c4296144 2019-05-09 stsp return err;
6343 8656d6c4 2019-05-20 stsp }
6344 8656d6c4 2019-05-20 stsp
6345 8656d6c4 2019-05-20 stsp const char *
6346 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6347 8656d6c4 2019-05-20 stsp {
6348 8656d6c4 2019-05-20 stsp return ct->path;
6349 8656d6c4 2019-05-20 stsp }
6350 8656d6c4 2019-05-20 stsp
6351 8656d6c4 2019-05-20 stsp unsigned int
6352 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6353 8656d6c4 2019-05-20 stsp {
6354 8656d6c4 2019-05-20 stsp return ct->status;
6355 818c7501 2019-07-11 stsp }
6356 818c7501 2019-07-11 stsp
6357 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6358 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6359 818c7501 2019-07-11 stsp struct got_repository *repo;
6360 818c7501 2019-07-11 stsp };
6361 818c7501 2019-07-11 stsp
6362 818c7501 2019-07-11 stsp static const struct got_error *
6363 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6364 818c7501 2019-07-11 stsp {
6365 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6366 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6367 818c7501 2019-07-11 stsp unsigned char status;
6368 818c7501 2019-07-11 stsp struct stat sb;
6369 818c7501 2019-07-11 stsp char *ondisk_path;
6370 818c7501 2019-07-11 stsp
6371 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6372 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6373 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6374 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6375 818c7501 2019-07-11 stsp
6376 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6377 818c7501 2019-07-11 stsp == -1)
6378 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6379 818c7501 2019-07-11 stsp
6380 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6381 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6382 818c7501 2019-07-11 stsp free(ondisk_path);
6383 818c7501 2019-07-11 stsp if (err)
6384 818c7501 2019-07-11 stsp return err;
6385 818c7501 2019-07-11 stsp
6386 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6387 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6388 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6389 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6390 818c7501 2019-07-11 stsp
6391 818c7501 2019-07-11 stsp return NULL;
6392 818c7501 2019-07-11 stsp }
6393 818c7501 2019-07-11 stsp
6394 818c7501 2019-07-11 stsp const struct got_error *
6395 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6396 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6397 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6398 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6399 818c7501 2019-07-11 stsp {
6400 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6401 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6402 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6403 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6404 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6405 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6406 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6407 818c7501 2019-07-11 stsp
6408 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6409 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6411 818c7501 2019-07-11 stsp
6412 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6413 818c7501 2019-07-11 stsp if (err)
6414 818c7501 2019-07-11 stsp return err;
6415 818c7501 2019-07-11 stsp
6416 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6417 818c7501 2019-07-11 stsp if (err)
6418 818c7501 2019-07-11 stsp goto done;
6419 818c7501 2019-07-11 stsp
6420 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6421 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6422 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6423 818c7501 2019-07-11 stsp &ok_arg);
6424 818c7501 2019-07-11 stsp if (err)
6425 818c7501 2019-07-11 stsp goto done;
6426 818c7501 2019-07-11 stsp
6427 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6428 818c7501 2019-07-11 stsp if (err)
6429 818c7501 2019-07-11 stsp goto done;
6430 818c7501 2019-07-11 stsp
6431 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6432 818c7501 2019-07-11 stsp if (err)
6433 818c7501 2019-07-11 stsp goto done;
6434 818c7501 2019-07-11 stsp
6435 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6436 818c7501 2019-07-11 stsp if (err)
6437 818c7501 2019-07-11 stsp goto done;
6438 818c7501 2019-07-11 stsp
6439 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6440 818c7501 2019-07-11 stsp 0);
6441 e51d7b55 2020-01-04 stsp if (err)
6442 e51d7b55 2020-01-04 stsp goto done;
6443 e51d7b55 2020-01-04 stsp
6444 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6445 818c7501 2019-07-11 stsp if (err)
6446 818c7501 2019-07-11 stsp goto done;
6447 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6448 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6449 e51d7b55 2020-01-04 stsp goto done;
6450 e51d7b55 2020-01-04 stsp }
6451 818c7501 2019-07-11 stsp
6452 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6453 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6454 818c7501 2019-07-11 stsp if (err)
6455 818c7501 2019-07-11 stsp goto done;
6456 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6457 818c7501 2019-07-11 stsp if (err)
6458 818c7501 2019-07-11 stsp goto done;
6459 818c7501 2019-07-11 stsp
6460 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6461 818c7501 2019-07-11 stsp
6462 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6463 818c7501 2019-07-11 stsp if (err)
6464 818c7501 2019-07-11 stsp goto done;
6465 818c7501 2019-07-11 stsp
6466 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6467 818c7501 2019-07-11 stsp if (err)
6468 818c7501 2019-07-11 stsp goto done;
6469 818c7501 2019-07-11 stsp
6470 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6471 818c7501 2019-07-11 stsp worktree->base_commit_id);
6472 818c7501 2019-07-11 stsp if (err)
6473 818c7501 2019-07-11 stsp goto done;
6474 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6475 818c7501 2019-07-11 stsp if (err)
6476 818c7501 2019-07-11 stsp goto done;
6477 818c7501 2019-07-11 stsp
6478 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6479 818c7501 2019-07-11 stsp if (err)
6480 818c7501 2019-07-11 stsp goto done;
6481 818c7501 2019-07-11 stsp done:
6482 818c7501 2019-07-11 stsp free(fileindex_path);
6483 818c7501 2019-07-11 stsp free(tmp_branch_name);
6484 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6485 818c7501 2019-07-11 stsp free(branch_ref_name);
6486 818c7501 2019-07-11 stsp if (branch_ref)
6487 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6488 818c7501 2019-07-11 stsp if (wt_branch)
6489 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6490 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6491 818c7501 2019-07-11 stsp if (err) {
6492 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6493 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6494 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6495 818c7501 2019-07-11 stsp }
6496 818c7501 2019-07-11 stsp if (*tmp_branch) {
6497 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6498 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6499 818c7501 2019-07-11 stsp }
6500 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6501 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6502 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6503 3e3a69f1 2019-07-25 stsp }
6504 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6505 818c7501 2019-07-11 stsp }
6506 818c7501 2019-07-11 stsp return err;
6507 818c7501 2019-07-11 stsp }
6508 818c7501 2019-07-11 stsp
6509 818c7501 2019-07-11 stsp const struct got_error *
6510 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6511 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6512 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6513 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6514 818c7501 2019-07-11 stsp {
6515 818c7501 2019-07-11 stsp const struct got_error *err;
6516 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6517 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6518 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6519 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6520 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6521 818c7501 2019-07-11 stsp
6522 818c7501 2019-07-11 stsp *commit_id = NULL;
6523 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6524 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6525 3e3a69f1 2019-07-25 stsp *branch = NULL;
6526 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6527 3e3a69f1 2019-07-25 stsp
6528 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6529 3e3a69f1 2019-07-25 stsp if (err)
6530 3e3a69f1 2019-07-25 stsp return err;
6531 818c7501 2019-07-11 stsp
6532 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6533 3e3a69f1 2019-07-25 stsp if (err)
6534 f032f1f7 2019-08-04 stsp goto done;
6535 f032f1f7 2019-08-04 stsp
6536 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6537 f032f1f7 2019-08-04 stsp &have_staged_files);
6538 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6539 f032f1f7 2019-08-04 stsp goto done;
6540 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6541 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6542 3e3a69f1 2019-07-25 stsp goto done;
6543 f032f1f7 2019-08-04 stsp }
6544 3e3a69f1 2019-07-25 stsp
6545 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6546 818c7501 2019-07-11 stsp if (err)
6547 f032f1f7 2019-08-04 stsp goto done;
6548 818c7501 2019-07-11 stsp
6549 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6550 818c7501 2019-07-11 stsp if (err)
6551 818c7501 2019-07-11 stsp goto done;
6552 818c7501 2019-07-11 stsp
6553 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6554 818c7501 2019-07-11 stsp if (err)
6555 818c7501 2019-07-11 stsp goto done;
6556 818c7501 2019-07-11 stsp
6557 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6558 818c7501 2019-07-11 stsp if (err)
6559 818c7501 2019-07-11 stsp goto done;
6560 818c7501 2019-07-11 stsp
6561 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6562 818c7501 2019-07-11 stsp if (err)
6563 818c7501 2019-07-11 stsp goto done;
6564 818c7501 2019-07-11 stsp
6565 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6566 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6567 818c7501 2019-07-11 stsp if (err)
6568 818c7501 2019-07-11 stsp goto done;
6569 818c7501 2019-07-11 stsp
6570 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6571 818c7501 2019-07-11 stsp if (err)
6572 818c7501 2019-07-11 stsp goto done;
6573 818c7501 2019-07-11 stsp
6574 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6575 818c7501 2019-07-11 stsp if (err)
6576 818c7501 2019-07-11 stsp goto done;
6577 818c7501 2019-07-11 stsp
6578 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6579 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6580 818c7501 2019-07-11 stsp if (err)
6581 818c7501 2019-07-11 stsp goto done;
6582 818c7501 2019-07-11 stsp
6583 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6584 818c7501 2019-07-11 stsp if (err)
6585 818c7501 2019-07-11 stsp goto done;
6586 818c7501 2019-07-11 stsp done:
6587 818c7501 2019-07-11 stsp free(commit_ref_name);
6588 818c7501 2019-07-11 stsp free(branch_ref_name);
6589 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6590 818c7501 2019-07-11 stsp if (commit_ref)
6591 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6592 818c7501 2019-07-11 stsp if (branch_ref)
6593 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6594 818c7501 2019-07-11 stsp if (err) {
6595 818c7501 2019-07-11 stsp free(*commit_id);
6596 818c7501 2019-07-11 stsp *commit_id = NULL;
6597 818c7501 2019-07-11 stsp if (*tmp_branch) {
6598 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6599 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6600 818c7501 2019-07-11 stsp }
6601 818c7501 2019-07-11 stsp if (*new_base_branch) {
6602 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6603 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6604 818c7501 2019-07-11 stsp }
6605 818c7501 2019-07-11 stsp if (*branch) {
6606 818c7501 2019-07-11 stsp got_ref_close(*branch);
6607 818c7501 2019-07-11 stsp *branch = NULL;
6608 818c7501 2019-07-11 stsp }
6609 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6610 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6611 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6612 3e3a69f1 2019-07-25 stsp }
6613 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6614 818c7501 2019-07-11 stsp }
6615 818c7501 2019-07-11 stsp return err;
6616 c4296144 2019-05-09 stsp }
6617 818c7501 2019-07-11 stsp
6618 818c7501 2019-07-11 stsp const struct got_error *
6619 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6620 818c7501 2019-07-11 stsp {
6621 818c7501 2019-07-11 stsp const struct got_error *err;
6622 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6623 818c7501 2019-07-11 stsp
6624 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6625 818c7501 2019-07-11 stsp if (err)
6626 818c7501 2019-07-11 stsp return err;
6627 818c7501 2019-07-11 stsp
6628 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6629 818c7501 2019-07-11 stsp free(tmp_branch_name);
6630 818c7501 2019-07-11 stsp return NULL;
6631 818c7501 2019-07-11 stsp }
6632 818c7501 2019-07-11 stsp
6633 818c7501 2019-07-11 stsp static const struct got_error *
6634 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6635 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6636 818c7501 2019-07-11 stsp {
6637 0ebf8283 2019-07-24 stsp *logmsg = arg;
6638 818c7501 2019-07-11 stsp return NULL;
6639 818c7501 2019-07-11 stsp }
6640 818c7501 2019-07-11 stsp
6641 818c7501 2019-07-11 stsp static const struct got_error *
6642 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6643 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6644 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6645 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6646 818c7501 2019-07-11 stsp {
6647 818c7501 2019-07-11 stsp return NULL;
6648 01757395 2019-07-12 stsp }
6649 01757395 2019-07-12 stsp
6650 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6651 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6652 01757395 2019-07-12 stsp void *progress_arg;
6653 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6654 01757395 2019-07-12 stsp };
6655 01757395 2019-07-12 stsp
6656 01757395 2019-07-12 stsp static const struct got_error *
6657 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6658 01757395 2019-07-12 stsp {
6659 01757395 2019-07-12 stsp const struct got_error *err;
6660 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6661 01757395 2019-07-12 stsp char *p;
6662 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6663 01757395 2019-07-12 stsp
6664 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6665 01757395 2019-07-12 stsp if (err)
6666 01757395 2019-07-12 stsp return err;
6667 01757395 2019-07-12 stsp
6668 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6669 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6670 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6671 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6672 01757395 2019-07-12 stsp return NULL;
6673 01757395 2019-07-12 stsp
6674 01757395 2019-07-12 stsp p = strdup(path);
6675 01757395 2019-07-12 stsp if (p == NULL)
6676 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6677 01757395 2019-07-12 stsp
6678 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6679 01757395 2019-07-12 stsp if (err || new == NULL)
6680 01757395 2019-07-12 stsp free(p);
6681 01757395 2019-07-12 stsp return err;
6682 818c7501 2019-07-11 stsp }
6683 818c7501 2019-07-11 stsp
6684 0ebf8283 2019-07-24 stsp static const struct got_error *
6685 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6686 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6687 818c7501 2019-07-11 stsp {
6688 818c7501 2019-07-11 stsp const struct got_error *err;
6689 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6690 818c7501 2019-07-11 stsp
6691 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6692 818c7501 2019-07-11 stsp if (err) {
6693 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6694 818c7501 2019-07-11 stsp goto done;
6695 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6696 818c7501 2019-07-11 stsp if (err)
6697 818c7501 2019-07-11 stsp goto done;
6698 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6699 818c7501 2019-07-11 stsp if (err)
6700 818c7501 2019-07-11 stsp goto done;
6701 de05890f 2020-03-05 stsp } else if (is_rebase) {
6702 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6703 818c7501 2019-07-11 stsp int cmp;
6704 818c7501 2019-07-11 stsp
6705 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6706 818c7501 2019-07-11 stsp if (err)
6707 818c7501 2019-07-11 stsp goto done;
6708 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6709 818c7501 2019-07-11 stsp free(stored_id);
6710 818c7501 2019-07-11 stsp if (cmp != 0) {
6711 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6712 818c7501 2019-07-11 stsp goto done;
6713 818c7501 2019-07-11 stsp }
6714 818c7501 2019-07-11 stsp }
6715 0ebf8283 2019-07-24 stsp done:
6716 0ebf8283 2019-07-24 stsp if (commit_ref)
6717 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6718 0ebf8283 2019-07-24 stsp return err;
6719 0ebf8283 2019-07-24 stsp }
6720 0ebf8283 2019-07-24 stsp
6721 0ebf8283 2019-07-24 stsp static const struct got_error *
6722 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6723 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6724 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6725 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6726 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6727 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6728 0ebf8283 2019-07-24 stsp {
6729 0ebf8283 2019-07-24 stsp const struct got_error *err;
6730 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6731 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6732 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6733 818c7501 2019-07-11 stsp
6734 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6735 0ebf8283 2019-07-24 stsp
6736 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6737 0ebf8283 2019-07-24 stsp if (err)
6738 0ebf8283 2019-07-24 stsp return err;
6739 0ebf8283 2019-07-24 stsp
6740 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6741 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6742 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6743 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6744 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6745 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6746 818c7501 2019-07-11 stsp if (commit_ref)
6747 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6748 818c7501 2019-07-11 stsp return err;
6749 818c7501 2019-07-11 stsp }
6750 818c7501 2019-07-11 stsp
6751 818c7501 2019-07-11 stsp const struct got_error *
6752 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6753 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6754 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6755 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6756 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6757 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6758 818c7501 2019-07-11 stsp {
6759 0ebf8283 2019-07-24 stsp const struct got_error *err;
6760 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6761 0ebf8283 2019-07-24 stsp
6762 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6763 0ebf8283 2019-07-24 stsp if (err)
6764 0ebf8283 2019-07-24 stsp return err;
6765 0ebf8283 2019-07-24 stsp
6766 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6767 0ebf8283 2019-07-24 stsp if (err)
6768 0ebf8283 2019-07-24 stsp goto done;
6769 0ebf8283 2019-07-24 stsp
6770 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6771 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6772 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6773 0ebf8283 2019-07-24 stsp done:
6774 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6775 0ebf8283 2019-07-24 stsp return err;
6776 0ebf8283 2019-07-24 stsp }
6777 0ebf8283 2019-07-24 stsp
6778 0ebf8283 2019-07-24 stsp const struct got_error *
6779 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6780 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6781 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6782 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6783 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6784 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6785 0ebf8283 2019-07-24 stsp {
6786 0ebf8283 2019-07-24 stsp const struct got_error *err;
6787 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6788 0ebf8283 2019-07-24 stsp
6789 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6790 0ebf8283 2019-07-24 stsp if (err)
6791 0ebf8283 2019-07-24 stsp return err;
6792 0ebf8283 2019-07-24 stsp
6793 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6794 0ebf8283 2019-07-24 stsp if (err)
6795 0ebf8283 2019-07-24 stsp goto done;
6796 0ebf8283 2019-07-24 stsp
6797 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6798 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6799 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6800 0ebf8283 2019-07-24 stsp done:
6801 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6802 0ebf8283 2019-07-24 stsp return err;
6803 0ebf8283 2019-07-24 stsp }
6804 0ebf8283 2019-07-24 stsp
6805 0ebf8283 2019-07-24 stsp static const struct got_error *
6806 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6807 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6808 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6809 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6810 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6811 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6812 0ebf8283 2019-07-24 stsp {
6813 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6814 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6815 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6816 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6817 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6818 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6819 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6820 818c7501 2019-07-11 stsp
6821 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6822 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6823 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6824 a0e95631 2019-07-12 stsp
6825 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6826 818c7501 2019-07-11 stsp
6827 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6828 a0e95631 2019-07-12 stsp if (err)
6829 3e3a69f1 2019-07-25 stsp return err;
6830 a0e95631 2019-07-12 stsp
6831 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6832 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6833 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6834 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6835 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6836 01757395 2019-07-12 stsp /*
6837 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6838 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6839 6c13b005 2021-09-02 stsp *
6840 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6841 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6842 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6843 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6844 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6845 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6846 01757395 2019-07-12 stsp */
6847 01757395 2019-07-12 stsp if (merged_paths) {
6848 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6849 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6850 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6851 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6852 f2a9dc41 2019-12-13 tracey 0);
6853 01757395 2019-07-12 stsp if (err)
6854 01757395 2019-07-12 stsp goto done;
6855 01757395 2019-07-12 stsp }
6856 01757395 2019-07-12 stsp } else {
6857 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6858 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6859 01757395 2019-07-12 stsp if (err)
6860 01757395 2019-07-12 stsp goto done;
6861 01757395 2019-07-12 stsp }
6862 a0e95631 2019-07-12 stsp
6863 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6864 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6865 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6866 a0e95631 2019-07-12 stsp if (err)
6867 a0e95631 2019-07-12 stsp goto done;
6868 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6869 a0e95631 2019-07-12 stsp goto done;
6870 ff0d2220 2019-07-11 stsp }
6871 818c7501 2019-07-11 stsp
6872 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6873 edd02c5e 2019-07-11 stsp if (err)
6874 edd02c5e 2019-07-11 stsp goto done;
6875 edd02c5e 2019-07-11 stsp
6876 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6877 818c7501 2019-07-11 stsp if (err)
6878 818c7501 2019-07-11 stsp goto done;
6879 818c7501 2019-07-11 stsp
6880 5943eee2 2019-08-13 stsp if (new_logmsg) {
6881 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6882 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6883 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6884 5943eee2 2019-08-13 stsp goto done;
6885 5943eee2 2019-08-13 stsp }
6886 5943eee2 2019-08-13 stsp } else {
6887 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6888 5943eee2 2019-08-13 stsp if (err)
6889 5943eee2 2019-08-13 stsp goto done;
6890 5943eee2 2019-08-13 stsp }
6891 0ebf8283 2019-07-24 stsp
6892 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6893 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6894 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6895 8db00f97 2022-07-22 thomas committer ? committer :
6896 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6897 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6898 a0e95631 2019-07-12 stsp if (err)
6899 a0e95631 2019-07-12 stsp goto done;
6900 a0e95631 2019-07-12 stsp
6901 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6902 a0e95631 2019-07-12 stsp if (err)
6903 a0e95631 2019-07-12 stsp goto done;
6904 a0e95631 2019-07-12 stsp
6905 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6906 a0e95631 2019-07-12 stsp if (err)
6907 a0e95631 2019-07-12 stsp goto done;
6908 a0e95631 2019-07-12 stsp
6909 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6910 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6911 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6912 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6913 a0e95631 2019-07-12 stsp err = sync_err;
6914 818c7501 2019-07-11 stsp done:
6915 a0e95631 2019-07-12 stsp free(fileindex_path);
6916 a0e95631 2019-07-12 stsp free(head_commit_id);
6917 a0e95631 2019-07-12 stsp if (head_ref)
6918 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6919 818c7501 2019-07-11 stsp if (err) {
6920 818c7501 2019-07-11 stsp free(*new_commit_id);
6921 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6922 818c7501 2019-07-11 stsp }
6923 818c7501 2019-07-11 stsp return err;
6924 818c7501 2019-07-11 stsp }
6925 818c7501 2019-07-11 stsp
6926 818c7501 2019-07-11 stsp const struct got_error *
6927 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6928 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6929 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6930 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6931 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6932 be94c032 2023-02-20 thomas struct got_repository *repo)
6933 0ebf8283 2019-07-24 stsp {
6934 0ebf8283 2019-07-24 stsp const struct got_error *err;
6935 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6936 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6937 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6938 0ebf8283 2019-07-24 stsp
6939 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6940 0ebf8283 2019-07-24 stsp if (err)
6941 0ebf8283 2019-07-24 stsp return err;
6942 0ebf8283 2019-07-24 stsp
6943 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6944 0ebf8283 2019-07-24 stsp if (err)
6945 0ebf8283 2019-07-24 stsp goto done;
6946 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6947 0ebf8283 2019-07-24 stsp if (err)
6948 0ebf8283 2019-07-24 stsp goto done;
6949 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6950 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6951 0ebf8283 2019-07-24 stsp goto done;
6952 0ebf8283 2019-07-24 stsp }
6953 0ebf8283 2019-07-24 stsp
6954 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6955 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6956 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
6957 0ebf8283 2019-07-24 stsp done:
6958 0ebf8283 2019-07-24 stsp if (commit_ref)
6959 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6960 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6961 0ebf8283 2019-07-24 stsp free(commit_id);
6962 0ebf8283 2019-07-24 stsp return err;
6963 0ebf8283 2019-07-24 stsp }
6964 0ebf8283 2019-07-24 stsp
6965 0ebf8283 2019-07-24 stsp const struct got_error *
6966 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6967 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6968 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6969 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6970 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6971 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6972 0ebf8283 2019-07-24 stsp {
6973 0ebf8283 2019-07-24 stsp const struct got_error *err;
6974 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6975 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6976 0ebf8283 2019-07-24 stsp
6977 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6978 0ebf8283 2019-07-24 stsp if (err)
6979 0ebf8283 2019-07-24 stsp return err;
6980 0ebf8283 2019-07-24 stsp
6981 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6982 0ebf8283 2019-07-24 stsp if (err)
6983 0ebf8283 2019-07-24 stsp goto done;
6984 0ebf8283 2019-07-24 stsp
6985 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6986 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6987 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
6988 0ebf8283 2019-07-24 stsp done:
6989 0ebf8283 2019-07-24 stsp if (commit_ref)
6990 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6991 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6992 0ebf8283 2019-07-24 stsp return err;
6993 0ebf8283 2019-07-24 stsp }
6994 0ebf8283 2019-07-24 stsp
6995 0ebf8283 2019-07-24 stsp const struct got_error *
6996 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6997 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6998 818c7501 2019-07-11 stsp {
6999 3e3a69f1 2019-07-25 stsp if (fileindex)
7000 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7001 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7002 69844fba 2019-07-11 stsp }
7003 69844fba 2019-07-11 stsp
7004 69844fba 2019-07-11 stsp static const struct got_error *
7005 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7006 69844fba 2019-07-11 stsp {
7007 69844fba 2019-07-11 stsp const struct got_error *err;
7008 69844fba 2019-07-11 stsp struct got_reference *ref;
7009 69844fba 2019-07-11 stsp
7010 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7011 69844fba 2019-07-11 stsp if (err) {
7012 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7013 69844fba 2019-07-11 stsp return NULL;
7014 69844fba 2019-07-11 stsp return err;
7015 69844fba 2019-07-11 stsp }
7016 69844fba 2019-07-11 stsp
7017 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7018 69844fba 2019-07-11 stsp got_ref_close(ref);
7019 69844fba 2019-07-11 stsp return err;
7020 818c7501 2019-07-11 stsp }
7021 818c7501 2019-07-11 stsp
7022 69844fba 2019-07-11 stsp static const struct got_error *
7023 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7024 69844fba 2019-07-11 stsp {
7025 69844fba 2019-07-11 stsp const struct got_error *err;
7026 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7027 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7028 69844fba 2019-07-11 stsp
7029 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7030 69844fba 2019-07-11 stsp if (err)
7031 69844fba 2019-07-11 stsp goto done;
7032 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7033 69844fba 2019-07-11 stsp if (err)
7034 69844fba 2019-07-11 stsp goto done;
7035 69844fba 2019-07-11 stsp
7036 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7037 69844fba 2019-07-11 stsp if (err)
7038 69844fba 2019-07-11 stsp goto done;
7039 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7040 69844fba 2019-07-11 stsp if (err)
7041 69844fba 2019-07-11 stsp goto done;
7042 69844fba 2019-07-11 stsp
7043 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7044 69844fba 2019-07-11 stsp if (err)
7045 69844fba 2019-07-11 stsp goto done;
7046 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7047 69844fba 2019-07-11 stsp if (err)
7048 69844fba 2019-07-11 stsp goto done;
7049 69844fba 2019-07-11 stsp
7050 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7051 69844fba 2019-07-11 stsp if (err)
7052 69844fba 2019-07-11 stsp goto done;
7053 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7054 69844fba 2019-07-11 stsp if (err)
7055 69844fba 2019-07-11 stsp goto done;
7056 69844fba 2019-07-11 stsp
7057 69844fba 2019-07-11 stsp done:
7058 69844fba 2019-07-11 stsp free(tmp_branch_name);
7059 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7060 69844fba 2019-07-11 stsp free(branch_ref_name);
7061 69844fba 2019-07-11 stsp free(commit_ref_name);
7062 e600f124 2021-03-21 stsp return err;
7063 e600f124 2021-03-21 stsp }
7064 e600f124 2021-03-21 stsp
7065 ef20f542 2022-06-26 thomas static const struct got_error *
7066 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7067 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7068 e600f124 2021-03-21 stsp {
7069 e600f124 2021-03-21 stsp const struct got_error *err;
7070 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7071 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7072 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7073 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7074 e600f124 2021-03-21 stsp char *refname = NULL;
7075 e600f124 2021-03-21 stsp
7076 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7077 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7078 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7079 e600f124 2021-03-21 stsp branch_name += 11;
7080 e600f124 2021-03-21 stsp
7081 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7082 e600f124 2021-03-21 stsp if (err)
7083 e600f124 2021-03-21 stsp return err;
7084 e600f124 2021-03-21 stsp
7085 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7086 e600f124 2021-03-21 stsp new_id_str) == -1) {
7087 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7088 e600f124 2021-03-21 stsp goto done;
7089 e600f124 2021-03-21 stsp }
7090 e600f124 2021-03-21 stsp
7091 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7092 e600f124 2021-03-21 stsp if (err)
7093 e600f124 2021-03-21 stsp goto done;
7094 e600f124 2021-03-21 stsp
7095 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7096 e600f124 2021-03-21 stsp if (err)
7097 e600f124 2021-03-21 stsp goto done;
7098 e600f124 2021-03-21 stsp
7099 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7100 e600f124 2021-03-21 stsp done:
7101 e600f124 2021-03-21 stsp free(new_id_str);
7102 e600f124 2021-03-21 stsp free(refname);
7103 e600f124 2021-03-21 stsp free(old_commit_id);
7104 e600f124 2021-03-21 stsp if (ref)
7105 e600f124 2021-03-21 stsp got_ref_close(ref);
7106 69844fba 2019-07-11 stsp return err;
7107 69844fba 2019-07-11 stsp }
7108 69844fba 2019-07-11 stsp
7109 818c7501 2019-07-11 stsp const struct got_error *
7110 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7111 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7112 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7113 f08f28be 2023-02-03 thomas int create_backup)
7114 818c7501 2019-07-11 stsp {
7115 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7116 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7117 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7118 818c7501 2019-07-11 stsp
7119 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7120 818c7501 2019-07-11 stsp if (err)
7121 818c7501 2019-07-11 stsp return err;
7122 e600f124 2021-03-21 stsp
7123 e600f124 2021-03-21 stsp if (create_backup) {
7124 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7125 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7126 e600f124 2021-03-21 stsp if (err)
7127 e600f124 2021-03-21 stsp goto done;
7128 e600f124 2021-03-21 stsp }
7129 818c7501 2019-07-11 stsp
7130 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7131 818c7501 2019-07-11 stsp if (err)
7132 818c7501 2019-07-11 stsp goto done;
7133 818c7501 2019-07-11 stsp
7134 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7135 818c7501 2019-07-11 stsp if (err)
7136 818c7501 2019-07-11 stsp goto done;
7137 818c7501 2019-07-11 stsp
7138 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7139 818c7501 2019-07-11 stsp if (err)
7140 818c7501 2019-07-11 stsp goto done;
7141 818c7501 2019-07-11 stsp
7142 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7143 a615e0e7 2020-12-16 stsp if (err)
7144 a615e0e7 2020-12-16 stsp goto done;
7145 a615e0e7 2020-12-16 stsp
7146 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7147 a615e0e7 2020-12-16 stsp if (err)
7148 a615e0e7 2020-12-16 stsp goto done;
7149 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7150 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7151 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7152 a615e0e7 2020-12-16 stsp err = sync_err;
7153 818c7501 2019-07-11 stsp done:
7154 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7155 a615e0e7 2020-12-16 stsp free(fileindex_path);
7156 818c7501 2019-07-11 stsp free(new_head_commit_id);
7157 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7158 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7159 818c7501 2019-07-11 stsp err = unlockerr;
7160 818c7501 2019-07-11 stsp return err;
7161 818c7501 2019-07-11 stsp }
7162 818c7501 2019-07-11 stsp
7163 818c7501 2019-07-11 stsp const struct got_error *
7164 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7165 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7166 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7167 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7168 818c7501 2019-07-11 stsp {
7169 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7170 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7171 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7172 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7173 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7174 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7175 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7176 818c7501 2019-07-11 stsp
7177 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7178 818c7501 2019-07-11 stsp if (err)
7179 818c7501 2019-07-11 stsp return err;
7180 818c7501 2019-07-11 stsp
7181 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7182 945f9229 2022-04-16 thomas worktree->base_commit_id);
7183 945f9229 2022-04-16 thomas if (err)
7184 945f9229 2022-04-16 thomas goto done;
7185 945f9229 2022-04-16 thomas
7186 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7187 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7188 818c7501 2019-07-11 stsp if (err)
7189 818c7501 2019-07-11 stsp goto done;
7190 818c7501 2019-07-11 stsp
7191 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7192 818c7501 2019-07-11 stsp if (err)
7193 818c7501 2019-07-11 stsp goto done;
7194 818c7501 2019-07-11 stsp
7195 818c7501 2019-07-11 stsp /*
7196 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7197 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7198 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7199 818c7501 2019-07-11 stsp */
7200 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7201 818c7501 2019-07-11 stsp if (err)
7202 818c7501 2019-07-11 stsp goto done;
7203 818c7501 2019-07-11 stsp
7204 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7205 818c7501 2019-07-11 stsp if (err)
7206 818c7501 2019-07-11 stsp goto done;
7207 818c7501 2019-07-11 stsp
7208 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7209 945f9229 2022-04-16 thomas worktree->path_prefix);
7210 ca355955 2019-07-12 stsp if (err)
7211 ca355955 2019-07-12 stsp goto done;
7212 ca355955 2019-07-12 stsp
7213 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7214 ca355955 2019-07-12 stsp if (err)
7215 ca355955 2019-07-12 stsp goto done;
7216 ca355955 2019-07-12 stsp
7217 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7218 818c7501 2019-07-11 stsp if (err)
7219 818c7501 2019-07-11 stsp goto done;
7220 818c7501 2019-07-11 stsp
7221 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7222 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7223 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7224 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7225 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7226 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7227 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7228 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7229 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7230 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7231 55bd499d 2019-07-12 stsp if (err)
7232 1f1abb7e 2019-08-08 stsp goto sync;
7233 55bd499d 2019-07-12 stsp
7234 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7235 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7236 ca355955 2019-07-12 stsp sync:
7237 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7238 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7239 ca355955 2019-07-12 stsp err = sync_err;
7240 818c7501 2019-07-11 stsp done:
7241 818c7501 2019-07-11 stsp got_ref_close(resolved);
7242 a3a2faf2 2019-07-12 stsp free(tree_id);
7243 818c7501 2019-07-11 stsp free(commit_id);
7244 945f9229 2022-04-16 thomas if (commit)
7245 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7246 0ebf8283 2019-07-24 stsp if (fileindex)
7247 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7248 0ebf8283 2019-07-24 stsp free(fileindex_path);
7249 0ebf8283 2019-07-24 stsp
7250 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7251 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7252 0ebf8283 2019-07-24 stsp err = unlockerr;
7253 0ebf8283 2019-07-24 stsp return err;
7254 0ebf8283 2019-07-24 stsp }
7255 0ebf8283 2019-07-24 stsp
7256 0ebf8283 2019-07-24 stsp const struct got_error *
7257 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7258 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7259 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7260 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7261 0ebf8283 2019-07-24 stsp {
7262 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7263 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7264 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7265 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7266 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7267 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7268 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7269 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7270 0ebf8283 2019-07-24 stsp
7271 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7272 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7273 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7274 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7275 0ebf8283 2019-07-24 stsp
7276 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7277 0ebf8283 2019-07-24 stsp if (err)
7278 0ebf8283 2019-07-24 stsp return err;
7279 0ebf8283 2019-07-24 stsp
7280 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7281 0ebf8283 2019-07-24 stsp if (err)
7282 0ebf8283 2019-07-24 stsp goto done;
7283 0ebf8283 2019-07-24 stsp
7284 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7285 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7286 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7287 0ebf8283 2019-07-24 stsp &ok_arg);
7288 0ebf8283 2019-07-24 stsp if (err)
7289 0ebf8283 2019-07-24 stsp goto done;
7290 0ebf8283 2019-07-24 stsp
7291 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7292 0ebf8283 2019-07-24 stsp if (err)
7293 0ebf8283 2019-07-24 stsp goto done;
7294 0ebf8283 2019-07-24 stsp
7295 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7296 0ebf8283 2019-07-24 stsp if (err)
7297 0ebf8283 2019-07-24 stsp goto done;
7298 0ebf8283 2019-07-24 stsp
7299 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7300 0ebf8283 2019-07-24 stsp worktree);
7301 0ebf8283 2019-07-24 stsp if (err)
7302 0ebf8283 2019-07-24 stsp goto done;
7303 0ebf8283 2019-07-24 stsp
7304 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7305 0ebf8283 2019-07-24 stsp 0);
7306 0ebf8283 2019-07-24 stsp if (err)
7307 0ebf8283 2019-07-24 stsp goto done;
7308 0ebf8283 2019-07-24 stsp
7309 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7310 0ebf8283 2019-07-24 stsp if (err)
7311 0ebf8283 2019-07-24 stsp goto done;
7312 0ebf8283 2019-07-24 stsp
7313 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7314 0ebf8283 2019-07-24 stsp if (err)
7315 0ebf8283 2019-07-24 stsp goto done;
7316 0ebf8283 2019-07-24 stsp
7317 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7318 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7319 0ebf8283 2019-07-24 stsp if (err)
7320 0ebf8283 2019-07-24 stsp goto done;
7321 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7322 0ebf8283 2019-07-24 stsp if (err)
7323 0ebf8283 2019-07-24 stsp goto done;
7324 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7325 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7326 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7327 0ebf8283 2019-07-24 stsp goto done;
7328 0ebf8283 2019-07-24 stsp }
7329 0ebf8283 2019-07-24 stsp
7330 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7331 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7332 0ebf8283 2019-07-24 stsp if (err)
7333 0ebf8283 2019-07-24 stsp goto done;
7334 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7335 0ebf8283 2019-07-24 stsp if (err)
7336 0ebf8283 2019-07-24 stsp goto done;
7337 0ebf8283 2019-07-24 stsp
7338 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7339 0ebf8283 2019-07-24 stsp if (err)
7340 0ebf8283 2019-07-24 stsp goto done;
7341 0ebf8283 2019-07-24 stsp done:
7342 0ebf8283 2019-07-24 stsp free(fileindex_path);
7343 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7344 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7345 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7346 0ebf8283 2019-07-24 stsp if (wt_branch)
7347 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7348 0ebf8283 2019-07-24 stsp if (err) {
7349 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7350 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7351 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7352 0ebf8283 2019-07-24 stsp }
7353 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7354 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7355 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7356 0ebf8283 2019-07-24 stsp }
7357 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7358 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7359 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7360 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7361 3e3a69f1 2019-07-25 stsp }
7362 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7363 0ebf8283 2019-07-24 stsp }
7364 0ebf8283 2019-07-24 stsp return err;
7365 0ebf8283 2019-07-24 stsp }
7366 0ebf8283 2019-07-24 stsp
7367 0ebf8283 2019-07-24 stsp const struct got_error *
7368 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7369 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7370 0ebf8283 2019-07-24 stsp {
7371 3e3a69f1 2019-07-25 stsp if (fileindex)
7372 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7373 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7374 0ebf8283 2019-07-24 stsp }
7375 0ebf8283 2019-07-24 stsp
7376 0ebf8283 2019-07-24 stsp const struct got_error *
7377 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7378 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7379 0ebf8283 2019-07-24 stsp {
7380 0ebf8283 2019-07-24 stsp const struct got_error *err;
7381 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7382 0ebf8283 2019-07-24 stsp
7383 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7384 0ebf8283 2019-07-24 stsp if (err)
7385 0ebf8283 2019-07-24 stsp return err;
7386 0ebf8283 2019-07-24 stsp
7387 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7388 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7389 0ebf8283 2019-07-24 stsp return NULL;
7390 0ebf8283 2019-07-24 stsp }
7391 0ebf8283 2019-07-24 stsp
7392 0ebf8283 2019-07-24 stsp const struct got_error *
7393 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7394 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7395 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7396 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7397 0ebf8283 2019-07-24 stsp {
7398 0ebf8283 2019-07-24 stsp const struct got_error *err;
7399 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7400 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7401 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7402 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7403 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7404 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7405 0ebf8283 2019-07-24 stsp
7406 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7407 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7408 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7409 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7410 0ebf8283 2019-07-24 stsp
7411 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7412 3e3a69f1 2019-07-25 stsp if (err)
7413 3e3a69f1 2019-07-25 stsp return err;
7414 3e3a69f1 2019-07-25 stsp
7415 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7416 3e3a69f1 2019-07-25 stsp if (err)
7417 f032f1f7 2019-08-04 stsp goto done;
7418 f032f1f7 2019-08-04 stsp
7419 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7420 f032f1f7 2019-08-04 stsp &have_staged_files);
7421 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7422 f032f1f7 2019-08-04 stsp goto done;
7423 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7424 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7425 3e3a69f1 2019-07-25 stsp goto done;
7426 f032f1f7 2019-08-04 stsp }
7427 3e3a69f1 2019-07-25 stsp
7428 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7429 0ebf8283 2019-07-24 stsp if (err)
7430 f032f1f7 2019-08-04 stsp goto done;
7431 0ebf8283 2019-07-24 stsp
7432 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7433 0ebf8283 2019-07-24 stsp if (err)
7434 0ebf8283 2019-07-24 stsp goto done;
7435 0ebf8283 2019-07-24 stsp
7436 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7437 0ebf8283 2019-07-24 stsp if (err)
7438 0ebf8283 2019-07-24 stsp goto done;
7439 0ebf8283 2019-07-24 stsp
7440 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7441 0ebf8283 2019-07-24 stsp worktree);
7442 0ebf8283 2019-07-24 stsp if (err)
7443 0ebf8283 2019-07-24 stsp goto done;
7444 0ebf8283 2019-07-24 stsp
7445 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7446 0ebf8283 2019-07-24 stsp if (err)
7447 0ebf8283 2019-07-24 stsp goto done;
7448 0ebf8283 2019-07-24 stsp
7449 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7450 0ebf8283 2019-07-24 stsp if (err)
7451 0ebf8283 2019-07-24 stsp goto done;
7452 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7453 0ebf8283 2019-07-24 stsp if (err)
7454 0ebf8283 2019-07-24 stsp goto done;
7455 0ebf8283 2019-07-24 stsp
7456 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7457 0ebf8283 2019-07-24 stsp if (err)
7458 0ebf8283 2019-07-24 stsp goto done;
7459 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7460 0ebf8283 2019-07-24 stsp if (err)
7461 0ebf8283 2019-07-24 stsp goto done;
7462 0ebf8283 2019-07-24 stsp
7463 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7464 0ebf8283 2019-07-24 stsp if (err)
7465 0ebf8283 2019-07-24 stsp goto done;
7466 0ebf8283 2019-07-24 stsp done:
7467 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7468 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7469 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7470 0ebf8283 2019-07-24 stsp if (commit_ref)
7471 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7472 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7473 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7474 0ebf8283 2019-07-24 stsp if (err) {
7475 0ebf8283 2019-07-24 stsp free(*commit_id);
7476 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7477 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7478 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7479 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7480 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7481 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7482 0ebf8283 2019-07-24 stsp }
7483 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7484 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7485 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7486 3e3a69f1 2019-07-25 stsp }
7487 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7488 0ebf8283 2019-07-24 stsp }
7489 0ebf8283 2019-07-24 stsp return err;
7490 0ebf8283 2019-07-24 stsp }
7491 0ebf8283 2019-07-24 stsp
7492 0ebf8283 2019-07-24 stsp static const struct got_error *
7493 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7494 0ebf8283 2019-07-24 stsp {
7495 0ebf8283 2019-07-24 stsp const struct got_error *err;
7496 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7497 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7498 0ebf8283 2019-07-24 stsp
7499 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7500 0ebf8283 2019-07-24 stsp if (err)
7501 0ebf8283 2019-07-24 stsp goto done;
7502 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7503 0ebf8283 2019-07-24 stsp if (err)
7504 0ebf8283 2019-07-24 stsp goto done;
7505 0ebf8283 2019-07-24 stsp
7506 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7507 0ebf8283 2019-07-24 stsp worktree);
7508 0ebf8283 2019-07-24 stsp if (err)
7509 0ebf8283 2019-07-24 stsp goto done;
7510 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7511 0ebf8283 2019-07-24 stsp if (err)
7512 0ebf8283 2019-07-24 stsp goto done;
7513 0ebf8283 2019-07-24 stsp
7514 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7515 0ebf8283 2019-07-24 stsp if (err)
7516 0ebf8283 2019-07-24 stsp goto done;
7517 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7518 0ebf8283 2019-07-24 stsp if (err)
7519 0ebf8283 2019-07-24 stsp goto done;
7520 0ebf8283 2019-07-24 stsp
7521 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7522 0ebf8283 2019-07-24 stsp if (err)
7523 0ebf8283 2019-07-24 stsp goto done;
7524 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7525 0ebf8283 2019-07-24 stsp if (err)
7526 0ebf8283 2019-07-24 stsp goto done;
7527 0ebf8283 2019-07-24 stsp done:
7528 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7529 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7530 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7531 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7532 0ebf8283 2019-07-24 stsp return err;
7533 0ebf8283 2019-07-24 stsp }
7534 0ebf8283 2019-07-24 stsp
7535 0ebf8283 2019-07-24 stsp const struct got_error *
7536 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7537 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7538 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7539 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7540 0ebf8283 2019-07-24 stsp {
7541 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7542 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7543 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7544 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7545 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7546 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7547 0ebf8283 2019-07-24 stsp
7548 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7549 0ebf8283 2019-07-24 stsp if (err)
7550 0ebf8283 2019-07-24 stsp return err;
7551 0ebf8283 2019-07-24 stsp
7552 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7553 945f9229 2022-04-16 thomas worktree->base_commit_id);
7554 945f9229 2022-04-16 thomas if (err)
7555 945f9229 2022-04-16 thomas goto done;
7556 945f9229 2022-04-16 thomas
7557 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7558 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7559 0ebf8283 2019-07-24 stsp if (err)
7560 0ebf8283 2019-07-24 stsp goto done;
7561 0ebf8283 2019-07-24 stsp
7562 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7563 0ebf8283 2019-07-24 stsp if (err)
7564 0ebf8283 2019-07-24 stsp goto done;
7565 0ebf8283 2019-07-24 stsp
7566 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7567 0ebf8283 2019-07-24 stsp if (err)
7568 0ebf8283 2019-07-24 stsp goto done;
7569 0ebf8283 2019-07-24 stsp
7570 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7571 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7572 0ebf8283 2019-07-24 stsp if (err)
7573 0ebf8283 2019-07-24 stsp goto done;
7574 0ebf8283 2019-07-24 stsp
7575 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7576 0ebf8283 2019-07-24 stsp if (err)
7577 0ebf8283 2019-07-24 stsp goto done;
7578 0ebf8283 2019-07-24 stsp
7579 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7580 0ebf8283 2019-07-24 stsp if (err)
7581 0ebf8283 2019-07-24 stsp goto done;
7582 0ebf8283 2019-07-24 stsp
7583 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7584 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7585 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7586 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7587 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7588 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7589 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7590 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7591 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7592 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7593 0ebf8283 2019-07-24 stsp if (err)
7594 1f1abb7e 2019-08-08 stsp goto sync;
7595 0ebf8283 2019-07-24 stsp
7596 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7597 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7598 0ebf8283 2019-07-24 stsp sync:
7599 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7600 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7601 0ebf8283 2019-07-24 stsp err = sync_err;
7602 0ebf8283 2019-07-24 stsp done:
7603 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7604 0ebf8283 2019-07-24 stsp free(tree_id);
7605 818c7501 2019-07-11 stsp free(fileindex_path);
7606 818c7501 2019-07-11 stsp
7607 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7608 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7609 818c7501 2019-07-11 stsp err = unlockerr;
7610 818c7501 2019-07-11 stsp return err;
7611 818c7501 2019-07-11 stsp }
7612 0ebf8283 2019-07-24 stsp
7613 0ebf8283 2019-07-24 stsp const struct got_error *
7614 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7615 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7616 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7617 0ebf8283 2019-07-24 stsp {
7618 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7619 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7620 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7621 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7622 0ebf8283 2019-07-24 stsp
7623 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7624 0ebf8283 2019-07-24 stsp if (err)
7625 0ebf8283 2019-07-24 stsp return err;
7626 0ebf8283 2019-07-24 stsp
7627 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7628 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7629 e600f124 2021-03-21 stsp if (err)
7630 e600f124 2021-03-21 stsp goto done;
7631 e600f124 2021-03-21 stsp
7632 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7633 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7634 0ebf8283 2019-07-24 stsp if (err)
7635 0ebf8283 2019-07-24 stsp goto done;
7636 0ebf8283 2019-07-24 stsp
7637 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7638 0ebf8283 2019-07-24 stsp if (err)
7639 0ebf8283 2019-07-24 stsp goto done;
7640 0ebf8283 2019-07-24 stsp
7641 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7642 0ebf8283 2019-07-24 stsp if (err)
7643 0ebf8283 2019-07-24 stsp goto done;
7644 0ebf8283 2019-07-24 stsp
7645 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7646 0ebf8283 2019-07-24 stsp if (err)
7647 0ebf8283 2019-07-24 stsp goto done;
7648 0ebf8283 2019-07-24 stsp
7649 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7650 a615e0e7 2020-12-16 stsp if (err)
7651 a615e0e7 2020-12-16 stsp goto done;
7652 a615e0e7 2020-12-16 stsp
7653 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7654 a615e0e7 2020-12-16 stsp if (err)
7655 a615e0e7 2020-12-16 stsp goto done;
7656 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7657 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7658 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7659 a615e0e7 2020-12-16 stsp err = sync_err;
7660 0ebf8283 2019-07-24 stsp done:
7661 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7662 a615e0e7 2020-12-16 stsp free(fileindex_path);
7663 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7664 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7665 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7666 0ebf8283 2019-07-24 stsp err = unlockerr;
7667 0ebf8283 2019-07-24 stsp return err;
7668 0ebf8283 2019-07-24 stsp }
7669 0ebf8283 2019-07-24 stsp
7670 0ebf8283 2019-07-24 stsp const struct got_error *
7671 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7672 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7673 0ebf8283 2019-07-24 stsp {
7674 0ebf8283 2019-07-24 stsp const struct got_error *err;
7675 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7676 0ebf8283 2019-07-24 stsp
7677 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7678 0ebf8283 2019-07-24 stsp if (err)
7679 0ebf8283 2019-07-24 stsp return err;
7680 0ebf8283 2019-07-24 stsp
7681 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7682 0ebf8283 2019-07-24 stsp if (err)
7683 0ebf8283 2019-07-24 stsp goto done;
7684 0ebf8283 2019-07-24 stsp
7685 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7686 0ebf8283 2019-07-24 stsp done:
7687 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7688 2822a352 2019-10-15 stsp return err;
7689 2822a352 2019-10-15 stsp }
7690 2822a352 2019-10-15 stsp
7691 2822a352 2019-10-15 stsp const struct got_error *
7692 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7693 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7694 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7695 2822a352 2019-10-15 stsp struct got_repository *repo)
7696 2822a352 2019-10-15 stsp {
7697 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7698 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7699 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7700 2822a352 2019-10-15 stsp
7701 2822a352 2019-10-15 stsp *fileindex = NULL;
7702 2822a352 2019-10-15 stsp *branch_ref = NULL;
7703 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7704 2822a352 2019-10-15 stsp
7705 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7706 2822a352 2019-10-15 stsp if (err)
7707 2822a352 2019-10-15 stsp return err;
7708 2822a352 2019-10-15 stsp
7709 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7710 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7711 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7712 2822a352 2019-10-15 stsp "update -b or different branch name required");
7713 2822a352 2019-10-15 stsp goto done;
7714 2822a352 2019-10-15 stsp }
7715 2822a352 2019-10-15 stsp
7716 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7717 2822a352 2019-10-15 stsp if (err)
7718 2822a352 2019-10-15 stsp goto done;
7719 2822a352 2019-10-15 stsp
7720 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7721 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7722 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7723 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7724 2822a352 2019-10-15 stsp &ok_arg);
7725 2822a352 2019-10-15 stsp if (err)
7726 2822a352 2019-10-15 stsp goto done;
7727 2822a352 2019-10-15 stsp
7728 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7729 2822a352 2019-10-15 stsp if (err)
7730 2822a352 2019-10-15 stsp goto done;
7731 2822a352 2019-10-15 stsp
7732 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7733 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7734 2822a352 2019-10-15 stsp done:
7735 2822a352 2019-10-15 stsp if (err) {
7736 2822a352 2019-10-15 stsp if (*branch_ref) {
7737 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7738 2822a352 2019-10-15 stsp *branch_ref = NULL;
7739 2822a352 2019-10-15 stsp }
7740 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7741 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7742 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7743 2822a352 2019-10-15 stsp }
7744 2822a352 2019-10-15 stsp if (*fileindex) {
7745 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7746 2822a352 2019-10-15 stsp *fileindex = NULL;
7747 2822a352 2019-10-15 stsp }
7748 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7749 2822a352 2019-10-15 stsp }
7750 0cb83759 2019-08-03 stsp return err;
7751 0cb83759 2019-08-03 stsp }
7752 0cb83759 2019-08-03 stsp
7753 2822a352 2019-10-15 stsp const struct got_error *
7754 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7755 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7756 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7757 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7758 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7759 2822a352 2019-10-15 stsp {
7760 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7761 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7762 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7763 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7764 2822a352 2019-10-15 stsp
7765 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7766 2822a352 2019-10-15 stsp if (err)
7767 2822a352 2019-10-15 stsp goto done;
7768 2822a352 2019-10-15 stsp
7769 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7770 2822a352 2019-10-15 stsp if (err)
7771 2822a352 2019-10-15 stsp goto done;
7772 2822a352 2019-10-15 stsp
7773 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7774 945f9229 2022-04-16 thomas if (err)
7775 945f9229 2022-04-16 thomas goto done;
7776 945f9229 2022-04-16 thomas
7777 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7778 2822a352 2019-10-15 stsp worktree->path_prefix);
7779 2822a352 2019-10-15 stsp if (err)
7780 2822a352 2019-10-15 stsp goto done;
7781 2822a352 2019-10-15 stsp
7782 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7783 2822a352 2019-10-15 stsp if (err)
7784 2822a352 2019-10-15 stsp goto done;
7785 2822a352 2019-10-15 stsp
7786 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7787 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7788 2822a352 2019-10-15 stsp if (err)
7789 2822a352 2019-10-15 stsp goto sync;
7790 2822a352 2019-10-15 stsp
7791 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7792 2822a352 2019-10-15 stsp if (err)
7793 2822a352 2019-10-15 stsp goto sync;
7794 2822a352 2019-10-15 stsp
7795 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7796 6e210706 2021-01-22 stsp if (err)
7797 6e210706 2021-01-22 stsp goto sync;
7798 6e210706 2021-01-22 stsp
7799 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7800 2822a352 2019-10-15 stsp sync:
7801 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7802 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7803 2822a352 2019-10-15 stsp err = sync_err;
7804 2822a352 2019-10-15 stsp
7805 2822a352 2019-10-15 stsp done:
7806 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7807 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7808 2822a352 2019-10-15 stsp err = unlockerr;
7809 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7810 2822a352 2019-10-15 stsp
7811 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7812 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7813 2822a352 2019-10-15 stsp err = unlockerr;
7814 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7815 2822a352 2019-10-15 stsp
7816 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7817 2822a352 2019-10-15 stsp free(fileindex_path);
7818 2822a352 2019-10-15 stsp free(tree_id);
7819 945f9229 2022-04-16 thomas if (commit)
7820 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7821 2822a352 2019-10-15 stsp
7822 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7823 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7824 2822a352 2019-10-15 stsp err = unlockerr;
7825 2822a352 2019-10-15 stsp return err;
7826 2822a352 2019-10-15 stsp }
7827 2822a352 2019-10-15 stsp
7828 2822a352 2019-10-15 stsp const struct got_error *
7829 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7830 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7831 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7832 2822a352 2019-10-15 stsp {
7833 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7834 8b692cd0 2019-10-21 stsp
7835 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7836 8b692cd0 2019-10-21 stsp
7837 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7838 8b692cd0 2019-10-21 stsp
7839 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7840 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7841 8b692cd0 2019-10-21 stsp err = unlockerr;
7842 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7843 8b692cd0 2019-10-21 stsp
7844 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7845 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7846 8b692cd0 2019-10-21 stsp err = unlockerr;
7847 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7848 10604dce 2021-09-24 thomas
7849 10604dce 2021-09-24 thomas return err;
7850 10604dce 2021-09-24 thomas }
7851 10604dce 2021-09-24 thomas
7852 10604dce 2021-09-24 thomas const struct got_error *
7853 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7854 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7855 10604dce 2021-09-24 thomas {
7856 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7857 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7858 10604dce 2021-09-24 thomas
7859 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7860 10604dce 2021-09-24 thomas if (err)
7861 10604dce 2021-09-24 thomas goto done;
7862 8b692cd0 2019-10-21 stsp
7863 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7864 10604dce 2021-09-24 thomas
7865 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7866 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7867 10604dce 2021-09-24 thomas err = sync_err;
7868 10604dce 2021-09-24 thomas done:
7869 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7870 10604dce 2021-09-24 thomas free(fileindex_path);
7871 8b692cd0 2019-10-21 stsp return err;
7872 2822a352 2019-10-15 stsp }
7873 2822a352 2019-10-15 stsp
7874 10604dce 2021-09-24 thomas static const struct got_error *
7875 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7876 10604dce 2021-09-24 thomas {
7877 10604dce 2021-09-24 thomas const struct got_error *err;
7878 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7879 10604dce 2021-09-24 thomas
7880 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7881 10604dce 2021-09-24 thomas if (err)
7882 10604dce 2021-09-24 thomas goto done;
7883 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7884 10604dce 2021-09-24 thomas if (err)
7885 10604dce 2021-09-24 thomas goto done;
7886 10604dce 2021-09-24 thomas
7887 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7888 10604dce 2021-09-24 thomas if (err)
7889 10604dce 2021-09-24 thomas goto done;
7890 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7891 10604dce 2021-09-24 thomas if (err)
7892 10604dce 2021-09-24 thomas goto done;
7893 10604dce 2021-09-24 thomas
7894 10604dce 2021-09-24 thomas done:
7895 10604dce 2021-09-24 thomas free(branch_refname);
7896 10604dce 2021-09-24 thomas free(commit_refname);
7897 10604dce 2021-09-24 thomas return err;
7898 10604dce 2021-09-24 thomas }
7899 10604dce 2021-09-24 thomas
7900 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7901 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7902 10604dce 2021-09-24 thomas const char *branch_name;
7903 10604dce 2021-09-24 thomas };
7904 10604dce 2021-09-24 thomas
7905 10604dce 2021-09-24 thomas static const struct got_error *
7906 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7907 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7908 10604dce 2021-09-24 thomas {
7909 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7910 10604dce 2021-09-24 thomas
7911 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7912 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7913 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7914 10604dce 2021-09-24 thomas
7915 10604dce 2021-09-24 thomas return NULL;
7916 10604dce 2021-09-24 thomas }
7917 10604dce 2021-09-24 thomas
7918 10604dce 2021-09-24 thomas
7919 10604dce 2021-09-24 thomas const struct got_error *
7920 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7921 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7922 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7923 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7924 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7925 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7926 10604dce 2021-09-24 thomas {
7927 10604dce 2021-09-24 thomas const struct got_error *err;
7928 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7929 10604dce 2021-09-24 thomas
7930 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7931 10604dce 2021-09-24 thomas if (err)
7932 10604dce 2021-09-24 thomas goto done;
7933 10604dce 2021-09-24 thomas
7934 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7935 10604dce 2021-09-24 thomas worktree);
7936 10604dce 2021-09-24 thomas if (err)
7937 10604dce 2021-09-24 thomas goto done;
7938 10604dce 2021-09-24 thomas
7939 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7940 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7941 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7942 10604dce 2021-09-24 thomas done:
7943 10604dce 2021-09-24 thomas free(fileindex_path);
7944 10604dce 2021-09-24 thomas return err;
7945 10604dce 2021-09-24 thomas }
7946 10604dce 2021-09-24 thomas
7947 10604dce 2021-09-24 thomas const struct got_error *
7948 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7949 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7950 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7951 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7952 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
7953 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7954 ae1e948a 2021-09-28 thomas
7955 10604dce 2021-09-24 thomas {
7956 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7957 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7958 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7959 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7960 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7961 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7962 10604dce 2021-09-24 thomas int have_staged_files = 0;
7963 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7964 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7965 10604dce 2021-09-24 thomas
7966 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7967 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7968 10604dce 2021-09-24 thomas
7969 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7970 10604dce 2021-09-24 thomas
7971 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7972 10604dce 2021-09-24 thomas if (err)
7973 10604dce 2021-09-24 thomas goto done;
7974 10604dce 2021-09-24 thomas
7975 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7976 10604dce 2021-09-24 thomas if (err)
7977 10604dce 2021-09-24 thomas goto done;
7978 10604dce 2021-09-24 thomas
7979 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7980 10604dce 2021-09-24 thomas if (err)
7981 10604dce 2021-09-24 thomas goto done;
7982 10604dce 2021-09-24 thomas
7983 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7984 10604dce 2021-09-24 thomas &have_staged_files);
7985 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7986 10604dce 2021-09-24 thomas goto done;
7987 10604dce 2021-09-24 thomas if (have_staged_files) {
7988 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7989 10604dce 2021-09-24 thomas goto done;
7990 10604dce 2021-09-24 thomas }
7991 10604dce 2021-09-24 thomas
7992 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7993 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7994 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7995 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7996 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7997 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7998 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7999 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8000 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8001 10604dce 2021-09-24 thomas if (err)
8002 10604dce 2021-09-24 thomas goto done;
8003 10604dce 2021-09-24 thomas
8004 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
8005 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8006 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
8007 10604dce 2021-09-24 thomas goto done;
8008 10604dce 2021-09-24 thomas }
8009 10604dce 2021-09-24 thomas
8010 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8011 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8012 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
8013 10604dce 2021-09-24 thomas
8014 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
8015 10604dce 2021-09-24 thomas ct_path++;
8016 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
8017 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8018 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8019 10604dce 2021-09-24 thomas if (err)
8020 10604dce 2021-09-24 thomas goto done;
8021 10604dce 2021-09-24 thomas
8022 10604dce 2021-09-24 thomas }
8023 10604dce 2021-09-24 thomas
8024 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8025 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8026 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8027 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8028 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8029 10604dce 2021-09-24 thomas if (err)
8030 10604dce 2021-09-24 thomas goto done;
8031 10604dce 2021-09-24 thomas
8032 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8033 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8034 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8035 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8036 10604dce 2021-09-24 thomas err = sync_err;
8037 10604dce 2021-09-24 thomas done:
8038 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8039 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8040 21c2d8be 2023-01-10 thomas
8041 10604dce 2021-09-24 thomas free_commitable(ct);
8042 10604dce 2021-09-24 thomas }
8043 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8044 10604dce 2021-09-24 thomas free(fileindex_path);
8045 10604dce 2021-09-24 thomas return err;
8046 10604dce 2021-09-24 thomas }
8047 10604dce 2021-09-24 thomas
8048 10604dce 2021-09-24 thomas const struct got_error *
8049 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8050 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8051 10604dce 2021-09-24 thomas {
8052 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8053 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8054 10604dce 2021-09-24 thomas
8055 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8056 10604dce 2021-09-24 thomas if (err)
8057 10604dce 2021-09-24 thomas goto done;
8058 10604dce 2021-09-24 thomas
8059 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8060 10604dce 2021-09-24 thomas if (err)
8061 10604dce 2021-09-24 thomas goto done;
8062 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8063 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8064 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8065 10604dce 2021-09-24 thomas err = sync_err;
8066 10604dce 2021-09-24 thomas done:
8067 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8068 10604dce 2021-09-24 thomas free(fileindex_path);
8069 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8070 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8071 10604dce 2021-09-24 thomas err = unlockerr;
8072 10604dce 2021-09-24 thomas return err;
8073 10604dce 2021-09-24 thomas }
8074 10604dce 2021-09-24 thomas
8075 10604dce 2021-09-24 thomas const struct got_error *
8076 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8077 10604dce 2021-09-24 thomas struct got_repository *repo)
8078 10604dce 2021-09-24 thomas {
8079 10604dce 2021-09-24 thomas const struct got_error *err;
8080 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8081 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8082 10604dce 2021-09-24 thomas
8083 10604dce 2021-09-24 thomas *in_progress = 0;
8084 10604dce 2021-09-24 thomas
8085 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8086 10604dce 2021-09-24 thomas if (err)
8087 10604dce 2021-09-24 thomas return err;
8088 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8089 dfe86d1f 2021-09-24 thomas free(branch_refname);
8090 10604dce 2021-09-24 thomas if (err) {
8091 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8092 10604dce 2021-09-24 thomas return err;
8093 10604dce 2021-09-24 thomas } else
8094 10604dce 2021-09-24 thomas *in_progress = 1;
8095 10604dce 2021-09-24 thomas
8096 10604dce 2021-09-24 thomas return NULL;
8097 10604dce 2021-09-24 thomas }
8098 10604dce 2021-09-24 thomas
8099 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8100 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8101 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8102 10604dce 2021-09-24 thomas {
8103 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8104 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8105 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8106 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8107 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8108 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8109 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8110 10604dce 2021-09-24 thomas
8111 10604dce 2021-09-24 thomas *fileindex = NULL;
8112 10604dce 2021-09-24 thomas
8113 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8114 10604dce 2021-09-24 thomas if (err)
8115 10604dce 2021-09-24 thomas return err;
8116 10604dce 2021-09-24 thomas
8117 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8118 10604dce 2021-09-24 thomas if (err)
8119 10604dce 2021-09-24 thomas goto done;
8120 10604dce 2021-09-24 thomas
8121 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8122 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8123 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8124 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8125 10604dce 2021-09-24 thomas &ok_arg);
8126 10604dce 2021-09-24 thomas if (err)
8127 10604dce 2021-09-24 thomas goto done;
8128 10604dce 2021-09-24 thomas
8129 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8130 10604dce 2021-09-24 thomas if (err)
8131 10604dce 2021-09-24 thomas return err;
8132 10604dce 2021-09-24 thomas
8133 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8134 10604dce 2021-09-24 thomas if (err)
8135 10604dce 2021-09-24 thomas return err;
8136 10604dce 2021-09-24 thomas
8137 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8138 10604dce 2021-09-24 thomas 0);
8139 10604dce 2021-09-24 thomas if (err)
8140 10604dce 2021-09-24 thomas goto done;
8141 10604dce 2021-09-24 thomas
8142 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8143 10604dce 2021-09-24 thomas if (err)
8144 10604dce 2021-09-24 thomas goto done;
8145 10604dce 2021-09-24 thomas
8146 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8147 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8148 10604dce 2021-09-24 thomas goto done;
8149 10604dce 2021-09-24 thomas }
8150 10604dce 2021-09-24 thomas
8151 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8152 10604dce 2021-09-24 thomas if (err)
8153 10604dce 2021-09-24 thomas goto done;
8154 10604dce 2021-09-24 thomas
8155 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8156 10604dce 2021-09-24 thomas if (err)
8157 10604dce 2021-09-24 thomas goto done;
8158 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8159 10604dce 2021-09-24 thomas if (err)
8160 10604dce 2021-09-24 thomas goto done;
8161 10604dce 2021-09-24 thomas
8162 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8163 10604dce 2021-09-24 thomas if (err)
8164 10604dce 2021-09-24 thomas goto done;
8165 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8166 10604dce 2021-09-24 thomas if (err)
8167 10604dce 2021-09-24 thomas goto done;
8168 10604dce 2021-09-24 thomas
8169 10604dce 2021-09-24 thomas done:
8170 10604dce 2021-09-24 thomas free(branch_refname);
8171 10604dce 2021-09-24 thomas free(commit_refname);
8172 10604dce 2021-09-24 thomas free(fileindex_path);
8173 10604dce 2021-09-24 thomas if (branch_ref)
8174 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8175 10604dce 2021-09-24 thomas if (commit_ref)
8176 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8177 10604dce 2021-09-24 thomas if (wt_branch)
8178 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8179 10604dce 2021-09-24 thomas free(wt_branch_tip);
8180 10604dce 2021-09-24 thomas if (err) {
8181 10604dce 2021-09-24 thomas if (*fileindex) {
8182 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8183 10604dce 2021-09-24 thomas *fileindex = NULL;
8184 10604dce 2021-09-24 thomas }
8185 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8186 10604dce 2021-09-24 thomas }
8187 10604dce 2021-09-24 thomas return err;
8188 10604dce 2021-09-24 thomas }
8189 10604dce 2021-09-24 thomas
8190 10604dce 2021-09-24 thomas const struct got_error *
8191 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8192 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8193 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8194 10604dce 2021-09-24 thomas {
8195 10604dce 2021-09-24 thomas const struct got_error *err;
8196 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8197 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8198 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8199 10604dce 2021-09-24 thomas int have_staged_files = 0;
8200 10604dce 2021-09-24 thomas
8201 10604dce 2021-09-24 thomas *branch_name = NULL;
8202 10604dce 2021-09-24 thomas *branch_tip = NULL;
8203 10604dce 2021-09-24 thomas *fileindex = NULL;
8204 10604dce 2021-09-24 thomas
8205 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8206 10604dce 2021-09-24 thomas if (err)
8207 10604dce 2021-09-24 thomas return err;
8208 10604dce 2021-09-24 thomas
8209 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8210 10604dce 2021-09-24 thomas if (err)
8211 10604dce 2021-09-24 thomas goto done;
8212 10604dce 2021-09-24 thomas
8213 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8214 10604dce 2021-09-24 thomas &have_staged_files);
8215 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8216 10604dce 2021-09-24 thomas goto done;
8217 10604dce 2021-09-24 thomas if (have_staged_files) {
8218 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8219 10604dce 2021-09-24 thomas goto done;
8220 10604dce 2021-09-24 thomas }
8221 10604dce 2021-09-24 thomas
8222 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8223 10604dce 2021-09-24 thomas if (err)
8224 10604dce 2021-09-24 thomas goto done;
8225 10604dce 2021-09-24 thomas
8226 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8227 10604dce 2021-09-24 thomas if (err)
8228 10604dce 2021-09-24 thomas goto done;
8229 10604dce 2021-09-24 thomas
8230 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8231 10604dce 2021-09-24 thomas if (err)
8232 10604dce 2021-09-24 thomas goto done;
8233 10604dce 2021-09-24 thomas
8234 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8235 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8236 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8237 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8238 10604dce 2021-09-24 thomas goto done;
8239 10604dce 2021-09-24 thomas }
8240 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8241 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8242 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8243 10604dce 2021-09-24 thomas goto done;
8244 10604dce 2021-09-24 thomas }
8245 10604dce 2021-09-24 thomas
8246 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8247 10604dce 2021-09-24 thomas if (err)
8248 10604dce 2021-09-24 thomas goto done;
8249 10604dce 2021-09-24 thomas
8250 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8251 10604dce 2021-09-24 thomas if (err)
8252 10604dce 2021-09-24 thomas goto done;
8253 10604dce 2021-09-24 thomas done:
8254 10604dce 2021-09-24 thomas free(commit_refname);
8255 10604dce 2021-09-24 thomas free(branch_refname);
8256 10604dce 2021-09-24 thomas free(fileindex_path);
8257 10604dce 2021-09-24 thomas if (commit_ref)
8258 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8259 10604dce 2021-09-24 thomas if (branch_ref)
8260 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8261 10604dce 2021-09-24 thomas if (err) {
8262 10604dce 2021-09-24 thomas if (*branch_name) {
8263 10604dce 2021-09-24 thomas free(*branch_name);
8264 10604dce 2021-09-24 thomas *branch_name = NULL;
8265 10604dce 2021-09-24 thomas }
8266 10604dce 2021-09-24 thomas free(*branch_tip);
8267 10604dce 2021-09-24 thomas *branch_tip = NULL;
8268 10604dce 2021-09-24 thomas if (*fileindex) {
8269 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8270 10604dce 2021-09-24 thomas *fileindex = NULL;
8271 10604dce 2021-09-24 thomas }
8272 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8273 10604dce 2021-09-24 thomas }
8274 10604dce 2021-09-24 thomas return err;
8275 10604dce 2021-09-24 thomas }
8276 10604dce 2021-09-24 thomas
8277 10604dce 2021-09-24 thomas const struct got_error *
8278 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8279 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8280 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8281 10604dce 2021-09-24 thomas {
8282 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8283 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8284 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8285 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8286 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8287 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8288 10604dce 2021-09-24 thomas
8289 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8290 945f9229 2022-04-16 thomas worktree->base_commit_id);
8291 945f9229 2022-04-16 thomas if (err)
8292 945f9229 2022-04-16 thomas goto done;
8293 945f9229 2022-04-16 thomas
8294 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8295 945f9229 2022-04-16 thomas worktree->path_prefix);
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 err = delete_merge_refs(worktree, repo);
8300 10604dce 2021-09-24 thomas if (err)
8301 10604dce 2021-09-24 thomas goto done;
8302 10604dce 2021-09-24 thomas
8303 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8304 10604dce 2021-09-24 thomas if (err)
8305 10604dce 2021-09-24 thomas goto done;
8306 10604dce 2021-09-24 thomas
8307 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8308 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8309 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8310 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8311 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8312 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8313 10604dce 2021-09-24 thomas rfa.repo = repo;
8314 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8315 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8316 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8317 10604dce 2021-09-24 thomas if (err)
8318 10604dce 2021-09-24 thomas goto sync;
8319 10604dce 2021-09-24 thomas
8320 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8321 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8322 10604dce 2021-09-24 thomas sync:
8323 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8324 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8325 10604dce 2021-09-24 thomas err = sync_err;
8326 10604dce 2021-09-24 thomas done:
8327 10604dce 2021-09-24 thomas free(tree_id);
8328 10604dce 2021-09-24 thomas free(commit_id);
8329 945f9229 2022-04-16 thomas if (commit)
8330 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8331 10604dce 2021-09-24 thomas if (fileindex)
8332 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8333 10604dce 2021-09-24 thomas free(fileindex_path);
8334 10604dce 2021-09-24 thomas
8335 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8336 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8337 10604dce 2021-09-24 thomas err = unlockerr;
8338 10604dce 2021-09-24 thomas return err;
8339 10604dce 2021-09-24 thomas }
8340 10604dce 2021-09-24 thomas
8341 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8342 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8343 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8344 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8345 2db2652d 2019-08-07 stsp struct got_repository *repo;
8346 2db2652d 2019-08-07 stsp int have_changes;
8347 2db2652d 2019-08-07 stsp };
8348 2db2652d 2019-08-07 stsp
8349 ef20f542 2022-06-26 thomas static const struct got_error *
8350 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8351 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8352 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8353 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8354 735ef5ac 2019-08-03 stsp {
8355 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8356 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8357 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8358 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8359 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8360 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8361 8b13ce36 2019-08-08 stsp
8362 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8363 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8364 8b13ce36 2019-08-08 stsp return NULL;
8365 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8366 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8367 735ef5ac 2019-08-03 stsp
8368 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8369 735ef5ac 2019-08-03 stsp if (ie == NULL)
8370 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8371 735ef5ac 2019-08-03 stsp
8372 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8373 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8374 735ef5ac 2019-08-03 stsp relpath) == -1)
8375 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8376 735ef5ac 2019-08-03 stsp
8377 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8378 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8379 43010591 2023-02-17 thomas &base_commit_id, ie);
8380 735ef5ac 2019-08-03 stsp }
8381 735ef5ac 2019-08-03 stsp
8382 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8383 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8384 3aa5969e 2019-08-06 stsp goto done;
8385 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8386 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8387 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8388 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8389 735ef5ac 2019-08-03 stsp goto done;
8390 3aa5969e 2019-08-06 stsp }
8391 735ef5ac 2019-08-03 stsp
8392 2db2652d 2019-08-07 stsp a->have_changes = 1;
8393 2db2652d 2019-08-07 stsp
8394 735ef5ac 2019-08-03 stsp p = in_repo_path;
8395 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8396 735ef5ac 2019-08-03 stsp p++;
8397 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8398 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8399 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8400 735ef5ac 2019-08-03 stsp done:
8401 735ef5ac 2019-08-03 stsp free(in_repo_path);
8402 dc424a06 2019-08-07 stsp return err;
8403 dc424a06 2019-08-07 stsp }
8404 dc424a06 2019-08-07 stsp
8405 2db2652d 2019-08-07 stsp struct stage_path_arg {
8406 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8407 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8408 2db2652d 2019-08-07 stsp struct got_repository *repo;
8409 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8410 2db2652d 2019-08-07 stsp void *status_arg;
8411 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8412 2db2652d 2019-08-07 stsp void *patch_arg;
8413 7b5dc508 2019-10-28 stsp int staged_something;
8414 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8415 2db2652d 2019-08-07 stsp };
8416 2db2652d 2019-08-07 stsp
8417 2db2652d 2019-08-07 stsp static const struct got_error *
8418 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8419 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8420 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8421 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8422 0cb83759 2019-08-03 stsp {
8423 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8424 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8425 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8426 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8427 0cb83759 2019-08-03 stsp uint32_t stage;
8428 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8429 0aeb8099 2020-07-23 stsp struct stat sb;
8430 8b13ce36 2019-08-08 stsp
8431 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8432 8b13ce36 2019-08-08 stsp return NULL;
8433 0cb83759 2019-08-03 stsp
8434 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8435 d3e7c587 2019-08-03 stsp if (ie == NULL)
8436 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8437 0cb83759 2019-08-03 stsp
8438 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8439 2db2652d 2019-08-07 stsp relpath)== -1)
8440 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8441 0cb83759 2019-08-03 stsp
8442 0cb83759 2019-08-03 stsp switch (status) {
8443 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8444 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8445 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8446 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8447 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8448 0aeb8099 2020-07-23 stsp break;
8449 0aeb8099 2020-07-23 stsp }
8450 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8451 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8452 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8453 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8454 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8455 dc424a06 2019-08-07 stsp if (err)
8456 dc424a06 2019-08-07 stsp break;
8457 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8458 dc424a06 2019-08-07 stsp break;
8459 dc424a06 2019-08-07 stsp } else {
8460 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8461 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8462 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8463 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8464 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8465 dc424a06 2019-08-07 stsp break;
8466 dc424a06 2019-08-07 stsp }
8467 dc424a06 2019-08-07 stsp }
8468 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8469 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8470 0cb83759 2019-08-03 stsp if (err)
8471 dc424a06 2019-08-07 stsp break;
8472 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8473 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8474 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8475 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8476 0cb83759 2019-08-03 stsp else
8477 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8478 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8479 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8480 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8481 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8482 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8483 35213c7c 2020-07-23 stsp ssize_t target_len;
8484 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8485 35213c7c 2020-07-23 stsp sizeof(target_path));
8486 35213c7c 2020-07-23 stsp if (target_len == -1) {
8487 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8488 35213c7c 2020-07-23 stsp ondisk_path);
8489 35213c7c 2020-07-23 stsp break;
8490 35213c7c 2020-07-23 stsp }
8491 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8492 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8493 35213c7c 2020-07-23 stsp a->worktree->root_path);
8494 35213c7c 2020-07-23 stsp if (err)
8495 35213c7c 2020-07-23 stsp break;
8496 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8497 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8498 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8499 35213c7c 2020-07-23 stsp break;
8500 35213c7c 2020-07-23 stsp }
8501 35213c7c 2020-07-23 stsp }
8502 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8503 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8504 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8505 35213c7c 2020-07-23 stsp else
8506 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8507 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8508 0aeb8099 2020-07-23 stsp } else {
8509 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8510 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8511 0aeb8099 2020-07-23 stsp }
8512 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8513 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8514 dc424a06 2019-08-07 stsp break;
8515 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8516 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8517 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8518 f289c82c 2022-06-13 thomas if (err)
8519 f289c82c 2022-06-13 thomas break;
8520 f289c82c 2022-06-13 thomas /*
8521 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8522 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8523 f289c82c 2022-06-13 thomas */
8524 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8525 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8526 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8527 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8528 f289c82c 2022-06-13 thomas }
8529 0cb83759 2019-08-03 stsp break;
8530 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8531 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8532 d3e7c587 2019-08-03 stsp break;
8533 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8534 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8535 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8536 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8537 dc424a06 2019-08-07 stsp if (err)
8538 dc424a06 2019-08-07 stsp break;
8539 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8540 88f33a19 2019-08-08 stsp break;
8541 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8542 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8543 dc424a06 2019-08-07 stsp break;
8544 88f33a19 2019-08-08 stsp }
8545 dc424a06 2019-08-07 stsp }
8546 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8547 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8548 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8549 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8550 dc424a06 2019-08-07 stsp break;
8551 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8552 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8553 12463d8b 2019-12-13 stsp de_name);
8554 0cb83759 2019-08-03 stsp break;
8555 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8556 d3e7c587 2019-08-03 stsp break;
8557 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8558 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8559 ebf48fd5 2019-08-03 stsp break;
8560 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8561 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8562 2a06fe5f 2019-08-24 stsp break;
8563 0cb83759 2019-08-03 stsp default:
8564 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8565 537ac44b 2019-08-03 stsp break;
8566 0cb83759 2019-08-03 stsp }
8567 dc424a06 2019-08-07 stsp
8568 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8569 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8570 dc424a06 2019-08-07 stsp free(path_content);
8571 2db2652d 2019-08-07 stsp free(ondisk_path);
8572 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8573 0ebf8283 2019-07-24 stsp return err;
8574 0ebf8283 2019-07-24 stsp }
8575 0cb83759 2019-08-03 stsp
8576 0cb83759 2019-08-03 stsp const struct got_error *
8577 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8578 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8579 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8580 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8581 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8582 0cb83759 2019-08-03 stsp {
8583 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8584 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8585 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8586 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8587 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8588 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8589 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8590 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8591 0cb83759 2019-08-03 stsp
8592 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8593 0cb83759 2019-08-03 stsp if (err)
8594 0cb83759 2019-08-03 stsp return err;
8595 0cb83759 2019-08-03 stsp
8596 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8597 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8598 735ef5ac 2019-08-03 stsp if (err)
8599 735ef5ac 2019-08-03 stsp goto done;
8600 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8601 735ef5ac 2019-08-03 stsp if (err)
8602 735ef5ac 2019-08-03 stsp goto done;
8603 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8604 0cb83759 2019-08-03 stsp if (err)
8605 0cb83759 2019-08-03 stsp goto done;
8606 0cb83759 2019-08-03 stsp
8607 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8608 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8609 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8610 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8611 2db2652d 2019-08-07 stsp oka.repo = repo;
8612 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8613 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8614 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8615 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8616 735ef5ac 2019-08-03 stsp if (err)
8617 735ef5ac 2019-08-03 stsp goto done;
8618 735ef5ac 2019-08-03 stsp }
8619 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8620 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8621 2db2652d 2019-08-07 stsp goto done;
8622 2db2652d 2019-08-07 stsp }
8623 735ef5ac 2019-08-03 stsp
8624 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8625 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8626 2db2652d 2019-08-07 stsp spa.repo = repo;
8627 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8628 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8629 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8630 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8631 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8632 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8633 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8634 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8635 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8636 42005733 2019-08-03 stsp if (err)
8637 2db2652d 2019-08-07 stsp goto done;
8638 7b5dc508 2019-10-28 stsp }
8639 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8640 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8641 7b5dc508 2019-10-28 stsp goto done;
8642 0cb83759 2019-08-03 stsp }
8643 0cb83759 2019-08-03 stsp
8644 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8645 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8646 0cb83759 2019-08-03 stsp err = sync_err;
8647 0cb83759 2019-08-03 stsp done:
8648 735ef5ac 2019-08-03 stsp if (head_ref)
8649 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8650 735ef5ac 2019-08-03 stsp free(head_commit_id);
8651 0cb83759 2019-08-03 stsp free(fileindex_path);
8652 0cb83759 2019-08-03 stsp if (fileindex)
8653 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8654 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8655 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8656 0cb83759 2019-08-03 stsp err = unlockerr;
8657 0cb83759 2019-08-03 stsp return err;
8658 0cb83759 2019-08-03 stsp }
8659 ad493afc 2019-08-03 stsp
8660 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8661 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8662 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8663 ad493afc 2019-08-03 stsp struct got_repository *repo;
8664 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8665 ad493afc 2019-08-03 stsp void *progress_arg;
8666 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8667 2e1f37b0 2019-08-08 stsp void *patch_arg;
8668 ad493afc 2019-08-03 stsp };
8669 2e1f37b0 2019-08-08 stsp
8670 2e1f37b0 2019-08-08 stsp static const struct got_error *
8671 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8672 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8673 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8674 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8675 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8676 2e1f37b0 2019-08-08 stsp {
8677 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8678 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8679 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8680 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8681 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8682 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8683 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8684 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8685 2e1f37b0 2019-08-08 stsp
8686 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8687 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8688 2e1f37b0 2019-08-08 stsp
8689 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8690 2e1f37b0 2019-08-08 stsp if (err)
8691 2e1f37b0 2019-08-08 stsp return err;
8692 f4ae6ddb 2022-07-01 thomas
8693 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8694 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8695 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8696 f4ae6ddb 2022-07-01 thomas goto done;
8697 f4ae6ddb 2022-07-01 thomas }
8698 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8699 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8700 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8701 f4ae6ddb 2022-07-01 thomas goto done;
8702 f4ae6ddb 2022-07-01 thomas }
8703 f4ae6ddb 2022-07-01 thomas
8704 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8705 2e1f37b0 2019-08-08 stsp if (err)
8706 2e1f37b0 2019-08-08 stsp goto done;
8707 2e1f37b0 2019-08-08 stsp
8708 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8709 2e1f37b0 2019-08-08 stsp if (err)
8710 2e1f37b0 2019-08-08 stsp goto done;
8711 2e1f37b0 2019-08-08 stsp
8712 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8713 2e1f37b0 2019-08-08 stsp if (err)
8714 2e1f37b0 2019-08-08 stsp goto done;
8715 2e1f37b0 2019-08-08 stsp
8716 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8717 f4ae6ddb 2022-07-01 thomas fd2);
8718 2e1f37b0 2019-08-08 stsp if (err)
8719 2e1f37b0 2019-08-08 stsp goto done;
8720 2e1f37b0 2019-08-08 stsp
8721 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8722 2e1f37b0 2019-08-08 stsp if (err)
8723 2e1f37b0 2019-08-08 stsp goto done;
8724 ad493afc 2019-08-03 stsp
8725 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8726 2e1f37b0 2019-08-08 stsp if (err)
8727 2e1f37b0 2019-08-08 stsp goto done;
8728 2e1f37b0 2019-08-08 stsp
8729 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8730 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8731 2e1f37b0 2019-08-08 stsp if (err)
8732 2e1f37b0 2019-08-08 stsp goto done;
8733 2e1f37b0 2019-08-08 stsp
8734 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8735 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8736 2e1f37b0 2019-08-08 stsp if (err)
8737 2e1f37b0 2019-08-08 stsp goto done;
8738 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8739 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8740 2e1f37b0 2019-08-08 stsp if (err)
8741 2e1f37b0 2019-08-08 stsp goto done;
8742 2e1f37b0 2019-08-08 stsp
8743 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8744 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8745 2e1f37b0 2019-08-08 stsp goto done;
8746 2e1f37b0 2019-08-08 stsp }
8747 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8748 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8749 2e1f37b0 2019-08-08 stsp goto done;
8750 2e1f37b0 2019-08-08 stsp }
8751 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8752 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8753 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8754 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8755 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8756 fe621944 2020-11-10 stsp nchanges++;
8757 fe621944 2020-11-10 stsp }
8758 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8759 2e1f37b0 2019-08-08 stsp int choice;
8760 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8761 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8762 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8763 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8764 2e1f37b0 2019-08-08 stsp if (err)
8765 2e1f37b0 2019-08-08 stsp goto done;
8766 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8767 2e1f37b0 2019-08-08 stsp have_content = 1;
8768 19e4b907 2019-08-08 stsp else
8769 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8770 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8771 2e1f37b0 2019-08-08 stsp break;
8772 2e1f37b0 2019-08-08 stsp }
8773 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8774 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8775 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8776 2e1f37b0 2019-08-08 stsp done:
8777 2e1f37b0 2019-08-08 stsp free(label1);
8778 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8779 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8780 2e1f37b0 2019-08-08 stsp if (blob)
8781 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8782 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8783 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8784 2e1f37b0 2019-08-08 stsp if (staged_blob)
8785 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8786 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8787 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8788 fe621944 2020-11-10 stsp err = free_err;
8789 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8790 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8791 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8792 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8793 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8794 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8795 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8796 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8797 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8798 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8799 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8800 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8801 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8802 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8803 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8804 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8805 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8806 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8807 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8808 2e1f37b0 2019-08-08 stsp }
8809 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8810 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8811 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8812 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8813 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8814 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8815 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8816 2e1f37b0 2019-08-08 stsp }
8817 2e1f37b0 2019-08-08 stsp free(path1);
8818 2e1f37b0 2019-08-08 stsp free(path2);
8819 fda8017d 2020-07-23 stsp return err;
8820 fda8017d 2020-07-23 stsp }
8821 fda8017d 2020-07-23 stsp
8822 fda8017d 2020-07-23 stsp static const struct got_error *
8823 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8824 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8825 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8826 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8827 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8828 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8829 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8830 fda8017d 2020-07-23 stsp {
8831 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8832 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8833 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8834 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8835 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8836 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8837 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8838 fda8017d 2020-07-23 stsp struct stat sb;
8839 fda8017d 2020-07-23 stsp
8840 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8841 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8842 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8843 fda8017d 2020-07-23 stsp if (err)
8844 fda8017d 2020-07-23 stsp return err;
8845 fda8017d 2020-07-23 stsp
8846 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8847 fda8017d 2020-07-23 stsp return NULL;
8848 fda8017d 2020-07-23 stsp
8849 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8850 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8851 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8852 fda8017d 2020-07-23 stsp if (err)
8853 fda8017d 2020-07-23 stsp goto done;
8854 fda8017d 2020-07-23 stsp }
8855 fda8017d 2020-07-23 stsp
8856 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8857 fda8017d 2020-07-23 stsp if (f == NULL) {
8858 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8859 fda8017d 2020-07-23 stsp path_unstaged_content);
8860 fda8017d 2020-07-23 stsp goto done;
8861 fda8017d 2020-07-23 stsp }
8862 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8863 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8864 fda8017d 2020-07-23 stsp goto done;
8865 fda8017d 2020-07-23 stsp }
8866 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8867 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8868 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8869 fda8017d 2020-07-23 stsp size_t r;
8870 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8871 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8872 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8873 fda8017d 2020-07-23 stsp goto done;
8874 fda8017d 2020-07-23 stsp }
8875 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8876 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8877 fda8017d 2020-07-23 stsp goto done;
8878 fda8017d 2020-07-23 stsp }
8879 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8880 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8881 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8882 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8883 fda8017d 2020-07-23 stsp progress_arg);
8884 fda8017d 2020-07-23 stsp } else {
8885 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8886 67a66647 2021-05-31 stsp
8887 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8888 67a66647 2021-05-31 stsp if (err)
8889 67a66647 2021-05-31 stsp return err;
8890 67a66647 2021-05-31 stsp
8891 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8892 67a66647 2021-05-31 stsp parent) == -1) {
8893 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8894 67a66647 2021-05-31 stsp base_path = NULL;
8895 67a66647 2021-05-31 stsp goto done;
8896 67a66647 2021-05-31 stsp }
8897 67a66647 2021-05-31 stsp
8898 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8899 fc2a50f2 2022-11-01 thomas base_path, "");
8900 67a66647 2021-05-31 stsp if (err)
8901 67a66647 2021-05-31 stsp goto done;
8902 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8903 67a66647 2021-05-31 stsp blob_base);
8904 67a66647 2021-05-31 stsp if (err)
8905 67a66647 2021-05-31 stsp goto done;
8906 67a66647 2021-05-31 stsp
8907 b6b86fd1 2022-08-30 thomas /*
8908 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8909 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8910 eec2f5a9 2021-06-03 stsp */
8911 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8912 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8913 eec2f5a9 2021-06-03 stsp ondisk_path);
8914 eec2f5a9 2021-06-03 stsp if (err)
8915 eec2f5a9 2021-06-03 stsp goto done;
8916 eec2f5a9 2021-06-03 stsp } else {
8917 eec2f5a9 2021-06-03 stsp int fd;
8918 06340621 2021-12-31 thomas fd = open(ondisk_path,
8919 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8920 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8921 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8922 eec2f5a9 2021-06-03 stsp goto done;
8923 eec2f5a9 2021-06-03 stsp }
8924 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8925 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8926 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8927 eec2f5a9 2021-06-03 stsp close(fd);
8928 eec2f5a9 2021-06-03 stsp goto done;
8929 eec2f5a9 2021-06-03 stsp }
8930 eec2f5a9 2021-06-03 stsp }
8931 eec2f5a9 2021-06-03 stsp
8932 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8933 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8934 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8935 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8936 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8937 fda8017d 2020-07-23 stsp }
8938 fda8017d 2020-07-23 stsp if (err)
8939 fda8017d 2020-07-23 stsp goto done;
8940 fda8017d 2020-07-23 stsp
8941 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8942 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8943 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8944 2ac8aa02 2020-11-09 stsp } else {
8945 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8946 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8947 2ac8aa02 2020-11-09 stsp }
8948 fda8017d 2020-07-23 stsp done:
8949 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8950 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8951 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8952 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8953 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8954 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8955 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8956 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8957 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8958 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8959 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8960 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8961 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8962 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8963 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8964 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8965 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8966 67a66647 2021-05-31 stsp free(blob_base_path);
8967 67a66647 2021-05-31 stsp free(parent);
8968 67a66647 2021-05-31 stsp free(base_path);
8969 2e1f37b0 2019-08-08 stsp return err;
8970 2e1f37b0 2019-08-08 stsp }
8971 2e1f37b0 2019-08-08 stsp
8972 ad493afc 2019-08-03 stsp static const struct got_error *
8973 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8974 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8975 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8976 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8977 ad493afc 2019-08-03 stsp {
8978 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8979 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8980 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8981 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8982 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8983 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8984 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8985 9bc94a15 2019-08-03 stsp struct stat sb;
8986 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8987 ad493afc 2019-08-03 stsp
8988 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8989 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8990 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8991 2e1f37b0 2019-08-08 stsp return NULL;
8992 2e1f37b0 2019-08-08 stsp
8993 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8994 ad493afc 2019-08-03 stsp if (ie == NULL)
8995 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8996 9bc94a15 2019-08-03 stsp
8997 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8998 9bc94a15 2019-08-03 stsp == -1)
8999 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9000 ad493afc 2019-08-03 stsp
9001 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9002 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9003 f69721c3 2019-10-21 stsp if (err)
9004 f69721c3 2019-10-21 stsp goto done;
9005 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9006 f69721c3 2019-10-21 stsp id_str) == -1) {
9007 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9008 f4ae6ddb 2022-07-01 thomas goto done;
9009 f4ae6ddb 2022-07-01 thomas }
9010 f4ae6ddb 2022-07-01 thomas
9011 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9012 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9013 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9014 f4ae6ddb 2022-07-01 thomas goto done;
9015 f4ae6ddb 2022-07-01 thomas }
9016 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9017 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9018 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9019 f69721c3 2019-10-21 stsp goto done;
9020 f69721c3 2019-10-21 stsp }
9021 f69721c3 2019-10-21 stsp
9022 ad493afc 2019-08-03 stsp switch (staged_status) {
9023 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9024 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9025 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9026 ad493afc 2019-08-03 stsp if (err)
9027 ad493afc 2019-08-03 stsp break;
9028 ad493afc 2019-08-03 stsp /* fall through */
9029 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9030 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9031 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9032 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9033 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9034 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9035 2e1f37b0 2019-08-08 stsp if (err)
9036 2e1f37b0 2019-08-08 stsp break;
9037 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9038 2e1f37b0 2019-08-08 stsp break;
9039 2e1f37b0 2019-08-08 stsp } else {
9040 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9041 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9042 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9043 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9044 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9045 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9046 2e1f37b0 2019-08-08 stsp }
9047 2e1f37b0 2019-08-08 stsp }
9048 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9049 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9050 ad493afc 2019-08-03 stsp if (err)
9051 ad493afc 2019-08-03 stsp break;
9052 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9053 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9054 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9055 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9056 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9057 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9058 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9059 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9060 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9061 ea7786be 2020-07-23 stsp break;
9062 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9063 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9064 36bf999c 2020-07-23 stsp char *staged_target;
9065 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9066 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9067 36bf999c 2020-07-23 stsp if (err)
9068 36bf999c 2020-07-23 stsp goto done;
9069 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9070 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9071 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9072 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9073 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9074 36bf999c 2020-07-23 stsp free(staged_target);
9075 dfe9fba0 2020-07-23 stsp } else {
9076 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9077 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9078 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9079 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9080 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9081 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9082 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9083 dfe9fba0 2020-07-23 stsp }
9084 ea7786be 2020-07-23 stsp break;
9085 ea7786be 2020-07-23 stsp default:
9086 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9087 ea7786be 2020-07-23 stsp break;
9088 ea7786be 2020-07-23 stsp }
9089 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9090 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9091 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9092 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9093 2ac8aa02 2020-11-09 stsp }
9094 ad493afc 2019-08-03 stsp break;
9095 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9096 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9097 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9098 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9099 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9100 2e1f37b0 2019-08-08 stsp if (err)
9101 2e1f37b0 2019-08-08 stsp break;
9102 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9103 2e1f37b0 2019-08-08 stsp break;
9104 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9105 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9106 2e1f37b0 2019-08-08 stsp break;
9107 2e1f37b0 2019-08-08 stsp }
9108 2e1f37b0 2019-08-08 stsp }
9109 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9110 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9111 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9112 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9113 9bc94a15 2019-08-03 stsp if (err)
9114 9bc94a15 2019-08-03 stsp break;
9115 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9116 ad493afc 2019-08-03 stsp break;
9117 ad493afc 2019-08-03 stsp }
9118 f69721c3 2019-10-21 stsp done:
9119 ad493afc 2019-08-03 stsp free(ondisk_path);
9120 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9121 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9122 ad493afc 2019-08-03 stsp if (blob_base)
9123 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9124 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9125 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9126 ad493afc 2019-08-03 stsp if (blob_staged)
9127 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9128 f69721c3 2019-10-21 stsp free(id_str);
9129 f69721c3 2019-10-21 stsp free(label_orig);
9130 ad493afc 2019-08-03 stsp return err;
9131 ad493afc 2019-08-03 stsp }
9132 ad493afc 2019-08-03 stsp
9133 ad493afc 2019-08-03 stsp const struct got_error *
9134 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9135 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9136 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9137 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9138 ad493afc 2019-08-03 stsp struct got_repository *repo)
9139 ad493afc 2019-08-03 stsp {
9140 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9141 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9142 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9143 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9144 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9145 ad493afc 2019-08-03 stsp
9146 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9147 ad493afc 2019-08-03 stsp if (err)
9148 ad493afc 2019-08-03 stsp return err;
9149 ad493afc 2019-08-03 stsp
9150 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9151 ad493afc 2019-08-03 stsp if (err)
9152 ad493afc 2019-08-03 stsp goto done;
9153 ad493afc 2019-08-03 stsp
9154 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9155 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9156 ad493afc 2019-08-03 stsp upa.repo = repo;
9157 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9158 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9159 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9160 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9161 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9162 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9163 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9164 ad493afc 2019-08-03 stsp if (err)
9165 ad493afc 2019-08-03 stsp goto done;
9166 ad493afc 2019-08-03 stsp }
9167 ad493afc 2019-08-03 stsp
9168 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9169 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9170 ad493afc 2019-08-03 stsp err = sync_err;
9171 ad493afc 2019-08-03 stsp done:
9172 ad493afc 2019-08-03 stsp free(fileindex_path);
9173 ad493afc 2019-08-03 stsp if (fileindex)
9174 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9175 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9176 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9177 ad493afc 2019-08-03 stsp err = unlockerr;
9178 ad493afc 2019-08-03 stsp return err;
9179 ad493afc 2019-08-03 stsp }
9180 b2118c49 2020-07-28 stsp
9181 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9182 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9183 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9184 b2118c49 2020-07-28 stsp void *info_arg;
9185 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9186 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9187 b2118c49 2020-07-28 stsp void *cancel_arg;
9188 b2118c49 2020-07-28 stsp };
9189 b2118c49 2020-07-28 stsp
9190 b2118c49 2020-07-28 stsp static const struct got_error *
9191 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9192 b2118c49 2020-07-28 stsp {
9193 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9194 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9195 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9196 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9197 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9198 b2118c49 2020-07-28 stsp int stage;
9199 b2118c49 2020-07-28 stsp
9200 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9201 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9202 b2118c49 2020-07-28 stsp
9203 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9204 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9205 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9206 b2118c49 2020-07-28 stsp break;
9207 b2118c49 2020-07-28 stsp }
9208 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9209 b2118c49 2020-07-28 stsp return NULL;
9210 b2118c49 2020-07-28 stsp
9211 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9212 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9213 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9214 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9215 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9216 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9217 43010591 2023-02-17 thomas &staged_blob_id, ie);
9218 b2118c49 2020-07-28 stsp }
9219 b2118c49 2020-07-28 stsp
9220 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9221 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9222 b2118c49 2020-07-28 stsp
9223 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9224 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9225 b2118c49 2020-07-28 stsp }
9226 b2118c49 2020-07-28 stsp
9227 b2118c49 2020-07-28 stsp const struct got_error *
9228 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9229 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9230 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9231 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9232 b2118c49 2020-07-28 stsp
9233 b2118c49 2020-07-28 stsp {
9234 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9235 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9236 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9237 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9238 b2118c49 2020-07-28 stsp
9239 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9240 b2118c49 2020-07-28 stsp if (err)
9241 b2118c49 2020-07-28 stsp return err;
9242 b2118c49 2020-07-28 stsp
9243 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9244 b2118c49 2020-07-28 stsp if (err)
9245 b2118c49 2020-07-28 stsp goto done;
9246 b2118c49 2020-07-28 stsp
9247 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9248 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9249 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9250 b2118c49 2020-07-28 stsp arg.paths = paths;
9251 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9252 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9253 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9254 b2118c49 2020-07-28 stsp &arg);
9255 b2118c49 2020-07-28 stsp done:
9256 b2118c49 2020-07-28 stsp free(fileindex_path);
9257 b2118c49 2020-07-28 stsp if (fileindex)
9258 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9259 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9260 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9261 b2118c49 2020-07-28 stsp err = unlockerr;
9262 b2118c49 2020-07-28 stsp return err;
9263 b2118c49 2020-07-28 stsp }
9264 814624e7 2022-03-22 thomas
9265 814624e7 2022-03-22 thomas static const struct got_error *
9266 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9267 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9268 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9269 814624e7 2022-03-22 thomas {
9270 814624e7 2022-03-22 thomas const struct got_error *err;
9271 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9272 814624e7 2022-03-22 thomas struct stat sb;
9273 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9274 814624e7 2022-03-22 thomas
9275 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9276 814624e7 2022-03-22 thomas if (err)
9277 814624e7 2022-03-22 thomas return err;
9278 814624e7 2022-03-22 thomas
9279 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9280 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9281 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9282 814624e7 2022-03-22 thomas
9283 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9284 814624e7 2022-03-22 thomas if (ie) {
9285 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9286 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9287 12de5570 2022-05-01 thomas repo);
9288 814624e7 2022-03-22 thomas if (err)
9289 814624e7 2022-03-22 thomas goto done;
9290 814624e7 2022-03-22 thomas } else {
9291 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9292 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9293 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9294 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9295 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9296 814624e7 2022-03-22 thomas ondisk_path);
9297 814624e7 2022-03-22 thomas goto done;
9298 814624e7 2022-03-22 thomas }
9299 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9300 814624e7 2022-03-22 thomas }
9301 814624e7 2022-03-22 thomas }
9302 814624e7 2022-03-22 thomas
9303 814624e7 2022-03-22 thomas done:
9304 814624e7 2022-03-22 thomas free(ondisk_path);
9305 814624e7 2022-03-22 thomas return err;
9306 814624e7 2022-03-22 thomas }
9307 814624e7 2022-03-22 thomas
9308 814624e7 2022-03-22 thomas static const struct got_error *
9309 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9310 814624e7 2022-03-22 thomas unsigned char staged_status)
9311 814624e7 2022-03-22 thomas {
9312 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9313 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9314 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9315 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9316 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9317 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9318 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9319 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9320 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9321 814624e7 2022-03-22 thomas return NULL;
9322 814624e7 2022-03-22 thomas }
9323 814624e7 2022-03-22 thomas
9324 814624e7 2022-03-22 thomas static const struct got_error *
9325 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9326 814624e7 2022-03-22 thomas {
9327 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9328 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9329 814624e7 2022-03-22 thomas return NULL;
9330 814624e7 2022-03-22 thomas }
9331 814624e7 2022-03-22 thomas
9332 814624e7 2022-03-22 thomas static const struct got_error *
9333 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9334 814624e7 2022-03-22 thomas unsigned char staged_status)
9335 814624e7 2022-03-22 thomas {
9336 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9337 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9338 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9339 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9340 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9341 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9342 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9343 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9344 814624e7 2022-03-22 thomas return NULL;
9345 814624e7 2022-03-22 thomas }
9346 814624e7 2022-03-22 thomas
9347 814624e7 2022-03-22 thomas const struct got_error *
9348 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9349 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9350 814624e7 2022-03-22 thomas {
9351 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9352 814624e7 2022-03-22 thomas }
9353 814624e7 2022-03-22 thomas
9354 814624e7 2022-03-22 thomas const struct got_error *
9355 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9356 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9357 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9358 814624e7 2022-03-22 thomas {
9359 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9360 814624e7 2022-03-22 thomas int file_renamed = 0;
9361 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9362 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9363 814624e7 2022-03-22 thomas
9364 814624e7 2022-03-22 thomas *oldpath = NULL;
9365 814624e7 2022-03-22 thomas *newpath = NULL;
9366 814624e7 2022-03-22 thomas
9367 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9368 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9369 814624e7 2022-03-22 thomas if (err)
9370 814624e7 2022-03-22 thomas goto done;
9371 814624e7 2022-03-22 thomas
9372 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9373 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9374 814624e7 2022-03-22 thomas if (err)
9375 814624e7 2022-03-22 thomas goto done;
9376 814624e7 2022-03-22 thomas
9377 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9378 814624e7 2022-03-22 thomas file_renamed = 1;
9379 814624e7 2022-03-22 thomas
9380 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9381 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9382 814624e7 2022-03-22 thomas else if (file_renamed) {
9383 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9384 814624e7 2022-03-22 thomas if (err == NULL)
9385 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9386 814624e7 2022-03-22 thomas } else if (old == NULL)
9387 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9388 814624e7 2022-03-22 thomas else
9389 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9390 814624e7 2022-03-22 thomas
9391 814624e7 2022-03-22 thomas done:
9392 814624e7 2022-03-22 thomas if (err) {
9393 814624e7 2022-03-22 thomas free(*oldpath);
9394 814624e7 2022-03-22 thomas *oldpath = NULL;
9395 814624e7 2022-03-22 thomas free(*newpath);
9396 814624e7 2022-03-22 thomas *newpath = NULL;
9397 814624e7 2022-03-22 thomas }
9398 814624e7 2022-03-22 thomas return err;
9399 814624e7 2022-03-22 thomas }
9400 814624e7 2022-03-22 thomas
9401 814624e7 2022-03-22 thomas const struct got_error *
9402 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9403 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9404 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9405 814624e7 2022-03-22 thomas {
9406 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9407 5e22b737 2022-05-31 thomas
9408 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9409 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9410 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9411 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9412 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9413 5e22b737 2022-05-31 thomas saa.repo = repo;
9414 5e22b737 2022-05-31 thomas
9415 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9416 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9417 814624e7 2022-03-22 thomas }
9418 5e22b737 2022-05-31 thomas
9419 5e22b737 2022-05-31 thomas const struct got_error *
9420 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9421 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9422 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9423 5e22b737 2022-05-31 thomas {
9424 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9425 5e22b737 2022-05-31 thomas
9426 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9427 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9428 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9429 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9430 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9431 5e22b737 2022-05-31 thomas sda.repo = repo;
9432 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9433 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9434 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9435 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9436 5e22b737 2022-05-31 thomas
9437 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9438 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9439 5e22b737 2022-05-31 thomas }
9440 5e22b737 2022-05-31 thomas
9441 5e22b737 2022-05-31 thomas const struct got_error *
9442 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9443 36832a8e 2022-05-31 thomas const char *fileindex_path)
9444 5e22b737 2022-05-31 thomas {
9445 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9446 5e22b737 2022-05-31 thomas
9447 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9448 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9449 5e22b737 2022-05-31 thomas
9450 5e22b737 2022-05-31 thomas return err;
9451 5e22b737 2022-05-31 thomas }