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 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 86c3caaf 2018-03-09 stsp
19 d1f6d47b 2019-02-04 stsp #include <dirent.h>
20 12ce7a6c 2019-08-12 stsp #include <limits.h>
21 f5c49f82 2019-01-06 stsp #include <stddef.h>
22 86c3caaf 2018-03-09 stsp #include <string.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 303e14b5 2019-09-23 stsp #include <time.h>
26 86c3caaf 2018-03-09 stsp #include <fcntl.h>
27 86c3caaf 2018-03-09 stsp #include <errno.h>
28 86c3caaf 2018-03-09 stsp #include <unistd.h>
29 9d31a1d8 2018-03-11 stsp #include <zlib.h>
30 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
31 512f0d0e 2019-01-02 stsp #include <libgen.h>
32 dd038bc6 2021-09-21 thomas.ad
33 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
34 86c3caaf 2018-03-09 stsp
35 86c3caaf 2018-03-09 stsp #include "got_error.h"
36 86c3caaf 2018-03-09 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9d31a1d8 2018-03-11 stsp #include "got_object.h"
39 1dd54920 2019-05-11 stsp #include "got_path.h"
40 e6209546 2019-08-22 stsp #include "got_cancel.h"
41 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 234035bc 2019-06-01 stsp #include "got_diff.h"
44 86c3caaf 2018-03-09 stsp
45 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
52 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
53 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
54 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
55 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
56 9d31a1d8 2018-03-11 stsp
57 9d31a1d8 2018-03-11 stsp #ifndef MIN
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 9d31a1d8 2018-03-11 stsp #endif
60 f69721c3 2019-10-21 stsp
61 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
63 86c3caaf 2018-03-09 stsp
64 99724ed4 2018-03-10 stsp static const struct got_error *
65 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
66 99724ed4 2018-03-10 stsp {
67 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
68 99724ed4 2018-03-10 stsp char *path;
69 99724ed4 2018-03-10 stsp
70 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
74 99724ed4 2018-03-10 stsp free(path);
75 507dc3bb 2018-12-29 stsp return err;
76 507dc3bb 2018-12-29 stsp }
77 507dc3bb 2018-12-29 stsp
78 507dc3bb 2018-12-29 stsp static const struct got_error *
79 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
80 507dc3bb 2018-12-29 stsp {
81 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
82 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
83 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
84 507dc3bb 2018-12-29 stsp char *path = NULL;
85 507dc3bb 2018-12-29 stsp
86 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
88 507dc3bb 2018-12-29 stsp path = NULL;
89 507dc3bb 2018-12-29 stsp goto done;
90 507dc3bb 2018-12-29 stsp }
91 507dc3bb 2018-12-29 stsp
92 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
93 507dc3bb 2018-12-29 stsp if (err)
94 507dc3bb 2018-12-29 stsp goto done;
95 507dc3bb 2018-12-29 stsp
96 507dc3bb 2018-12-29 stsp if (content) {
97 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
98 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
99 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
100 507dc3bb 2018-12-29 stsp goto done;
101 507dc3bb 2018-12-29 stsp }
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp
104 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
105 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
106 2a57020b 2019-02-20 stsp unlink(tmppath);
107 507dc3bb 2018-12-29 stsp goto done;
108 507dc3bb 2018-12-29 stsp }
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp done:
111 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
112 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
113 230a42bd 2019-05-11 jcs free(tmppath);
114 09fe317a 2018-03-11 stsp return err;
115 09fe317a 2018-03-11 stsp }
116 09fe317a 2018-03-11 stsp
117 024e9686 2019-05-14 stsp static const struct got_error *
118 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
119 024e9686 2019-05-14 stsp {
120 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
121 024e9686 2019-05-14 stsp char *refstr = NULL;
122 024e9686 2019-05-14 stsp
123 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
124 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
125 024e9686 2019-05-14 stsp if (refstr == NULL)
126 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
127 024e9686 2019-05-14 stsp } else {
128 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
129 024e9686 2019-05-14 stsp if (refstr == NULL)
130 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
131 024e9686 2019-05-14 stsp }
132 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
133 024e9686 2019-05-14 stsp free(refstr);
134 024e9686 2019-05-14 stsp return err;
135 024e9686 2019-05-14 stsp }
136 024e9686 2019-05-14 stsp
137 86c3caaf 2018-03-09 stsp const struct got_error *
138 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
139 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
140 86c3caaf 2018-03-09 stsp {
141 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
142 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
143 ec22038e 2019-03-10 stsp uuid_t uuid;
144 ec22038e 2019-03-10 stsp uint32_t uuid_status;
145 65596e15 2018-12-24 stsp int obj_type;
146 7ac97322 2018-03-11 stsp char *path_got = NULL;
147 1451e70d 2018-03-10 stsp char *formatstr = NULL;
148 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
149 65596e15 2018-12-24 stsp char *basestr = NULL;
150 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
151 65596e15 2018-12-24 stsp
152 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
154 0c48fee2 2019-03-11 stsp goto done;
155 0c48fee2 2019-03-11 stsp }
156 0c48fee2 2019-03-11 stsp
157 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
158 65596e15 2018-12-24 stsp if (err)
159 65596e15 2018-12-24 stsp return err;
160 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
165 86c3caaf 2018-03-09 stsp
166 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
167 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
168 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
169 0bb8a95e 2018-03-12 stsp }
170 577ec78f 2018-03-11 stsp
171 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
172 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
174 86c3caaf 2018-03-09 stsp goto done;
175 86c3caaf 2018-03-09 stsp }
176 86c3caaf 2018-03-09 stsp
177 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
178 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
180 86c3caaf 2018-03-09 stsp goto done;
181 86c3caaf 2018-03-09 stsp }
182 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
184 86c3caaf 2018-03-09 stsp goto done;
185 86c3caaf 2018-03-09 stsp }
186 86c3caaf 2018-03-09 stsp
187 056e7441 2018-03-11 stsp /* Create an empty lock file. */
188 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 056e7441 2018-03-11 stsp if (err)
190 056e7441 2018-03-11 stsp goto done;
191 056e7441 2018-03-11 stsp
192 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 99724ed4 2018-03-10 stsp if (err)
195 86c3caaf 2018-03-09 stsp goto done;
196 86c3caaf 2018-03-09 stsp
197 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
198 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
199 65596e15 2018-12-24 stsp if (err)
200 65596e15 2018-12-24 stsp goto done;
201 65596e15 2018-12-24 stsp
202 65596e15 2018-12-24 stsp /* Record our base commit. */
203 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
204 65596e15 2018-12-24 stsp if (err)
205 65596e15 2018-12-24 stsp goto done;
206 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
207 99724ed4 2018-03-10 stsp if (err)
208 86c3caaf 2018-03-09 stsp goto done;
209 86c3caaf 2018-03-09 stsp
210 1451e70d 2018-03-10 stsp /* Store path to repository. */
211 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
213 99724ed4 2018-03-10 stsp if (err)
214 86c3caaf 2018-03-09 stsp goto done;
215 86c3caaf 2018-03-09 stsp
216 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
217 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
219 ec22038e 2019-03-10 stsp if (err)
220 ec22038e 2019-03-10 stsp goto done;
221 ec22038e 2019-03-10 stsp
222 ec22038e 2019-03-10 stsp /* Generate UUID. */
223 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
224 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
225 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
226 ec22038e 2019-03-10 stsp goto done;
227 ec22038e 2019-03-10 stsp }
228 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
230 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
231 ec22038e 2019-03-10 stsp goto done;
232 ec22038e 2019-03-10 stsp }
233 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
234 577ec78f 2018-03-11 stsp if (err)
235 577ec78f 2018-03-11 stsp goto done;
236 577ec78f 2018-03-11 stsp
237 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
238 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
240 1451e70d 2018-03-10 stsp goto done;
241 1451e70d 2018-03-10 stsp }
242 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
243 99724ed4 2018-03-10 stsp if (err)
244 1451e70d 2018-03-10 stsp goto done;
245 1451e70d 2018-03-10 stsp
246 86c3caaf 2018-03-09 stsp done:
247 65596e15 2018-12-24 stsp free(commit_id);
248 7ac97322 2018-03-11 stsp free(path_got);
249 1451e70d 2018-03-10 stsp free(formatstr);
250 0bb8a95e 2018-03-12 stsp free(absprefix);
251 65596e15 2018-12-24 stsp free(basestr);
252 ec22038e 2019-03-10 stsp free(uuidstr);
253 86c3caaf 2018-03-09 stsp return err;
254 86c3caaf 2018-03-09 stsp }
255 86c3caaf 2018-03-09 stsp
256 247140b2 2019-02-05 stsp const struct got_error *
257 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 e5dc7198 2018-12-29 stsp const char *path_prefix)
259 e5dc7198 2018-12-29 stsp {
260 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
261 e5dc7198 2018-12-29 stsp
262 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
263 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
265 e5dc7198 2018-12-29 stsp }
266 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
267 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
268 e5dc7198 2018-12-29 stsp free(absprefix);
269 e5dc7198 2018-12-29 stsp return NULL;
270 86c3caaf 2018-03-09 stsp }
271 86c3caaf 2018-03-09 stsp
272 bc70eb79 2019-05-09 stsp const char *
273 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
274 86c3caaf 2018-03-09 stsp {
275 36a38700 2019-05-10 stsp return worktree->head_ref_name;
276 024e9686 2019-05-14 stsp }
277 024e9686 2019-05-14 stsp
278 024e9686 2019-05-14 stsp const struct got_error *
279 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
280 024e9686 2019-05-14 stsp struct got_reference *head_ref)
281 024e9686 2019-05-14 stsp {
282 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
283 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
284 024e9686 2019-05-14 stsp
285 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
287 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
288 024e9686 2019-05-14 stsp path_got = NULL;
289 024e9686 2019-05-14 stsp goto done;
290 024e9686 2019-05-14 stsp }
291 024e9686 2019-05-14 stsp
292 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
293 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
294 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
295 024e9686 2019-05-14 stsp goto done;
296 024e9686 2019-05-14 stsp }
297 024e9686 2019-05-14 stsp
298 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
299 024e9686 2019-05-14 stsp if (err)
300 024e9686 2019-05-14 stsp goto done;
301 024e9686 2019-05-14 stsp
302 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
303 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
304 024e9686 2019-05-14 stsp done:
305 024e9686 2019-05-14 stsp free(path_got);
306 024e9686 2019-05-14 stsp if (err)
307 024e9686 2019-05-14 stsp free(head_ref_name);
308 024e9686 2019-05-14 stsp return err;
309 507dc3bb 2018-12-29 stsp }
310 507dc3bb 2018-12-29 stsp
311 b72f483a 2019-02-05 stsp struct got_object_id *
312 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
313 507dc3bb 2018-12-29 stsp {
314 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
315 86c3caaf 2018-03-09 stsp }
316 86c3caaf 2018-03-09 stsp
317 507dc3bb 2018-12-29 stsp const struct got_error *
318 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
320 507dc3bb 2018-12-29 stsp {
321 507dc3bb 2018-12-29 stsp const struct got_error *err;
322 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
323 507dc3bb 2018-12-29 stsp char *id_str = NULL;
324 507dc3bb 2018-12-29 stsp char *path_got = NULL;
325 507dc3bb 2018-12-29 stsp
326 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
328 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
329 507dc3bb 2018-12-29 stsp path_got = NULL;
330 507dc3bb 2018-12-29 stsp goto done;
331 507dc3bb 2018-12-29 stsp }
332 507dc3bb 2018-12-29 stsp
333 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
334 507dc3bb 2018-12-29 stsp if (err)
335 507dc3bb 2018-12-29 stsp return err;
336 507dc3bb 2018-12-29 stsp
337 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
339 507dc3bb 2018-12-29 stsp goto done;
340 507dc3bb 2018-12-29 stsp }
341 507dc3bb 2018-12-29 stsp
342 507dc3bb 2018-12-29 stsp /* Record our base commit. */
343 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
344 507dc3bb 2018-12-29 stsp if (err)
345 507dc3bb 2018-12-29 stsp goto done;
346 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp
350 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
351 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
352 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
353 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
354 507dc3bb 2018-12-29 stsp goto done;
355 507dc3bb 2018-12-29 stsp }
356 507dc3bb 2018-12-29 stsp done:
357 507dc3bb 2018-12-29 stsp if (obj)
358 507dc3bb 2018-12-29 stsp got_object_close(obj);
359 507dc3bb 2018-12-29 stsp free(id_str);
360 507dc3bb 2018-12-29 stsp free(path_got);
361 507dc3bb 2018-12-29 stsp return err;
362 50b0790e 2020-09-11 stsp }
363 50b0790e 2020-09-11 stsp
364 50b0790e 2020-09-11 stsp const struct got_gotconfig *
365 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
366 50b0790e 2020-09-11 stsp {
367 50b0790e 2020-09-11 stsp return worktree->gotconfig;
368 507dc3bb 2018-12-29 stsp }
369 507dc3bb 2018-12-29 stsp
370 9d31a1d8 2018-03-11 stsp static const struct got_error *
371 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
372 86c3caaf 2018-03-09 stsp {
373 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
376 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
377 86c3caaf 2018-03-09 stsp return NULL;
378 21908da4 2019-01-13 stsp }
379 21908da4 2019-01-13 stsp
380 21908da4 2019-01-13 stsp static const struct got_error *
381 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
382 4a1ddfc2 2019-01-12 stsp {
383 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
384 4a1ddfc2 2019-01-12 stsp char *abspath;
385 4a1ddfc2 2019-01-12 stsp
386 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
388 4a1ddfc2 2019-01-12 stsp
389 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
390 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
391 ddcd8544 2019-03-11 stsp struct stat sb;
392 ddcd8544 2019-03-11 stsp err = NULL;
393 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
394 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
395 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
396 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
397 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
398 ddcd8544 2019-03-11 stsp }
399 ddcd8544 2019-03-11 stsp }
400 4a1ddfc2 2019-01-12 stsp free(abspath);
401 68c76935 2019-02-19 stsp return err;
402 68c76935 2019-02-19 stsp }
403 68c76935 2019-02-19 stsp
404 68c76935 2019-02-19 stsp static const struct got_error *
405 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
406 68c76935 2019-02-19 stsp {
407 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
408 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
409 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
410 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
411 68c76935 2019-02-19 stsp
412 68c76935 2019-02-19 stsp *same = 1;
413 68c76935 2019-02-19 stsp
414 230a42bd 2019-05-11 jcs for (;;) {
415 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
417 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
418 68c76935 2019-02-19 stsp break;
419 68c76935 2019-02-19 stsp }
420 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
423 68c76935 2019-02-19 stsp break;
424 68c76935 2019-02-19 stsp }
425 68c76935 2019-02-19 stsp if (flen1 == 0) {
426 68c76935 2019-02-19 stsp if (flen2 != 0)
427 68c76935 2019-02-19 stsp *same = 0;
428 68c76935 2019-02-19 stsp break;
429 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
430 68c76935 2019-02-19 stsp if (flen1 != 0)
431 68c76935 2019-02-19 stsp *same = 0;
432 68c76935 2019-02-19 stsp break;
433 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
434 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
435 68c76935 2019-02-19 stsp *same = 0;
436 68c76935 2019-02-19 stsp break;
437 68c76935 2019-02-19 stsp }
438 68c76935 2019-02-19 stsp } else {
439 68c76935 2019-02-19 stsp *same = 0;
440 68c76935 2019-02-19 stsp break;
441 68c76935 2019-02-19 stsp }
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp
444 68c76935 2019-02-19 stsp return err;
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp static const struct got_error *
448 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
449 68c76935 2019-02-19 stsp {
450 68c76935 2019-02-19 stsp struct stat sb;
451 68c76935 2019-02-19 stsp size_t size1, size2;
452 68c76935 2019-02-19 stsp
453 68c76935 2019-02-19 stsp *same = 1;
454 68c76935 2019-02-19 stsp
455 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
456 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
457 68c76935 2019-02-19 stsp size1 = sb.st_size;
458 68c76935 2019-02-19 stsp
459 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
460 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
461 68c76935 2019-02-19 stsp size2 = sb.st_size;
462 68c76935 2019-02-19 stsp
463 68c76935 2019-02-19 stsp if (size1 != size2) {
464 68c76935 2019-02-19 stsp *same = 0;
465 68c76935 2019-02-19 stsp return NULL;
466 68c76935 2019-02-19 stsp }
467 68c76935 2019-02-19 stsp
468 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
469 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
470 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
471 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
474 24f136e0 2021-11-20 thomas }
475 24f136e0 2021-11-20 thomas
476 24f136e0 2021-11-20 thomas static const struct got_error *
477 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
478 24f136e0 2021-11-20 thomas {
479 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
480 24f136e0 2021-11-20 thomas size_t flen;
481 24f136e0 2021-11-20 thomas ssize_t outlen;
482 24f136e0 2021-11-20 thomas
483 24f136e0 2021-11-20 thomas *outsize = 0;
484 24f136e0 2021-11-20 thomas
485 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
486 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas for (;;) {
489 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
490 24f136e0 2021-11-20 thomas if (flen == 0) {
491 24f136e0 2021-11-20 thomas if (ferror(f))
492 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
493 24f136e0 2021-11-20 thomas if (feof(f))
494 24f136e0 2021-11-20 thomas break;
495 24f136e0 2021-11-20 thomas }
496 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
497 24f136e0 2021-11-20 thomas if (outlen == -1)
498 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
499 24f136e0 2021-11-20 thomas if (outlen != flen)
500 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
501 24f136e0 2021-11-20 thomas *outsize += outlen;
502 24f136e0 2021-11-20 thomas }
503 24f136e0 2021-11-20 thomas
504 24f136e0 2021-11-20 thomas return NULL;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas static const struct got_error *
508 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
509 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 24f136e0 2021-11-20 thomas const char *ondisk_path)
512 24f136e0 2021-11-20 thomas {
513 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
514 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
515 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
519 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
520 24f136e0 2021-11-20 thomas
521 24f136e0 2021-11-20 thomas *overlapcnt = 0;
522 24f136e0 2021-11-20 thomas
523 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
524 24f136e0 2021-11-20 thomas if (err)
525 24f136e0 2021-11-20 thomas return err;
526 24f136e0 2021-11-20 thomas
527 24f136e0 2021-11-20 thomas if (same_content)
528 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
531 24f136e0 2021-11-20 thomas if (err)
532 24f136e0 2021-11-20 thomas return err;
533 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
534 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
535 24f136e0 2021-11-20 thomas if (err)
536 24f136e0 2021-11-20 thomas return err;
537 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
538 24f136e0 2021-11-20 thomas
539 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
540 24f136e0 2021-11-20 thomas *overlapcnt = 1;
541 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
543 24f136e0 2021-11-20 thomas goto done;
544 24f136e0 2021-11-20 thomas }
545 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
547 24f136e0 2021-11-20 thomas goto done;
548 24f136e0 2021-11-20 thomas }
549 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
551 24f136e0 2021-11-20 thomas goto done;
552 24f136e0 2021-11-20 thomas }
553 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
554 24f136e0 2021-11-20 thomas base_path_orig);
555 24f136e0 2021-11-20 thomas if (err)
556 24f136e0 2021-11-20 thomas goto done;
557 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
558 24f136e0 2021-11-20 thomas base_path_deriv);
559 24f136e0 2021-11-20 thomas if (err)
560 24f136e0 2021-11-20 thomas goto done;
561 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
562 24f136e0 2021-11-20 thomas base_path_deriv2);
563 24f136e0 2021-11-20 thomas if (err)
564 24f136e0 2021-11-20 thomas goto done;
565 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
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_deriv, f_deriv, fd_deriv);
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_deriv2, f_deriv2, fd_deriv2);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
575 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
576 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
577 24f136e0 2021-11-20 thomas goto done;
578 24f136e0 2021-11-20 thomas }
579 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
582 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
583 24f136e0 2021-11-20 thomas path_deriv) < 0) {
584 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
585 24f136e0 2021-11-20 thomas goto done;
586 24f136e0 2021-11-20 thomas }
587 24f136e0 2021-11-20 thomas if (size_orig > 0) {
588 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
590 24f136e0 2021-11-20 thomas label_orig ? " " : "",
591 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
592 24f136e0 2021-11-20 thomas path_orig) < 0) {
593 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
594 24f136e0 2021-11-20 thomas goto done;
595 24f136e0 2021-11-20 thomas }
596 24f136e0 2021-11-20 thomas }
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
599 24f136e0 2021-11-20 thomas path_deriv2,
600 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
601 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
602 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
603 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
604 24f136e0 2021-11-20 thomas goto done;
605 24f136e0 2021-11-20 thomas }
606 24f136e0 2021-11-20 thomas } else if (changed_deriv)
607 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 24f136e0 2021-11-20 thomas else if (changed_deriv2)
609 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
610 24f136e0 2021-11-20 thomas done:
611 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
612 24f136e0 2021-11-20 thomas err == NULL)
613 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
614 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
616 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
618 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
620 24f136e0 2021-11-20 thomas free(path_orig);
621 24f136e0 2021-11-20 thomas free(path_deriv);
622 24f136e0 2021-11-20 thomas free(path_deriv2);
623 24f136e0 2021-11-20 thomas free(base_path_orig);
624 24f136e0 2021-11-20 thomas free(base_path_deriv);
625 24f136e0 2021-11-20 thomas free(base_path_deriv2);
626 24f136e0 2021-11-20 thomas return err;
627 6353ad76 2019-02-08 stsp }
628 6353ad76 2019-02-08 stsp
629 6353ad76 2019-02-08 stsp /*
630 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
631 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
632 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
633 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
634 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
635 6353ad76 2019-02-08 stsp */
636 6353ad76 2019-02-08 stsp static const struct got_error *
637 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
640 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
643 6353ad76 2019-02-08 stsp {
644 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
645 6353ad76 2019-02-08 stsp int merged_fd = -1;
646 db590691 2021-06-02 stsp FILE *f_merged = NULL;
647 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
648 234035bc 2019-06-01 stsp int overlapcnt = 0;
649 3524bbf9 2020-10-20 stsp char *parent = NULL;
650 6353ad76 2019-02-08 stsp
651 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
652 234035bc 2019-06-01 stsp
653 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
654 3524bbf9 2020-10-20 stsp if (err)
655 3524bbf9 2020-10-20 stsp return err;
656 af54ae4a 2019-02-19 stsp
657 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
659 3524bbf9 2020-10-20 stsp goto done;
660 3524bbf9 2020-10-20 stsp }
661 af54ae4a 2019-02-19 stsp
662 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
663 6353ad76 2019-02-08 stsp if (err)
664 6353ad76 2019-02-08 stsp goto done;
665 3b9f0f87 2020-07-23 stsp
666 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
668 24f136e0 2021-11-20 thomas if (err) {
669 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
670 24f136e0 2021-11-20 thomas goto done;
671 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
673 24f136e0 2021-11-20 thomas ondisk_path);
674 24f136e0 2021-11-20 thomas if (err)
675 24f136e0 2021-11-20 thomas goto done;
676 24f136e0 2021-11-20 thomas }
677 6353ad76 2019-02-08 stsp
678 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
679 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
680 1ee397ad 2019-07-12 stsp if (err)
681 1ee397ad 2019-07-12 stsp goto done;
682 6353ad76 2019-02-08 stsp
683 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
684 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
685 816dc654 2019-02-16 stsp goto done;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp
688 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
689 db590691 2021-06-02 stsp if (f_merged == NULL) {
690 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
691 db590691 2021-06-02 stsp goto done;
692 db590691 2021-06-02 stsp }
693 db590691 2021-06-02 stsp merged_fd = -1;
694 db590691 2021-06-02 stsp
695 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
696 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
697 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
698 db590691 2021-06-02 stsp f_merged);
699 68c76935 2019-02-19 stsp if (err)
700 68c76935 2019-02-19 stsp goto done;
701 816dc654 2019-02-16 stsp }
702 6353ad76 2019-02-08 stsp
703 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
704 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
705 70a0c8ec 2019-02-20 stsp goto done;
706 70a0c8ec 2019-02-20 stsp }
707 70a0c8ec 2019-02-20 stsp
708 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
710 230a42bd 2019-05-11 jcs ondisk_path);
711 6353ad76 2019-02-08 stsp goto done;
712 6353ad76 2019-02-08 stsp }
713 6353ad76 2019-02-08 stsp done:
714 fdcb7daf 2019-12-15 stsp if (err) {
715 fdcb7daf 2019-12-15 stsp if (merged_path)
716 fdcb7daf 2019-12-15 stsp unlink(merged_path);
717 3b9f0f87 2020-07-23 stsp }
718 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
720 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
722 6353ad76 2019-02-08 stsp free(merged_path);
723 af54ae4a 2019-02-19 stsp free(base_path);
724 3524bbf9 2020-10-20 stsp free(parent);
725 af57b12a 2020-07-23 stsp return err;
726 af57b12a 2020-07-23 stsp }
727 af57b12a 2020-07-23 stsp
728 af57b12a 2020-07-23 stsp static const struct got_error *
729 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
730 af57b12a 2020-07-23 stsp size_t target_len)
731 af57b12a 2020-07-23 stsp {
732 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
733 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
734 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
735 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
736 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
737 af57b12a 2020-07-23 stsp ondisk_path);
738 af57b12a 2020-07-23 stsp return NULL;
739 af57b12a 2020-07-23 stsp }
740 af57b12a 2020-07-23 stsp
741 af57b12a 2020-07-23 stsp /*
742 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
744 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
745 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
746 993e2a1b 2020-07-23 stsp *
747 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
748 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
751 993e2a1b 2020-07-23 stsp * has deleted this symlink.
752 11cc08c1 2020-07-23 stsp */
753 11cc08c1 2020-07-23 stsp static const struct got_error *
754 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
755 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
757 11cc08c1 2020-07-23 stsp {
758 11cc08c1 2020-07-23 stsp const struct got_error *err;
759 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
760 11cc08c1 2020-07-23 stsp FILE *f = NULL;
761 11cc08c1 2020-07-23 stsp
762 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
763 11cc08c1 2020-07-23 stsp if (err)
764 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
765 11cc08c1 2020-07-23 stsp
766 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
767 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
769 11cc08c1 2020-07-23 stsp goto done;
770 11cc08c1 2020-07-23 stsp }
771 11cc08c1 2020-07-23 stsp
772 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
773 11cc08c1 2020-07-23 stsp if (err)
774 3818e3c4 2020-11-01 naddy goto done;
775 3818e3c4 2020-11-01 naddy
776 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
778 11cc08c1 2020-07-23 stsp goto done;
779 3818e3c4 2020-11-01 naddy }
780 11cc08c1 2020-07-23 stsp
781 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
784 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
785 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
786 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
787 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
788 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
789 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
791 11cc08c1 2020-07-23 stsp goto done;
792 11cc08c1 2020-07-23 stsp }
793 11cc08c1 2020-07-23 stsp
794 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
795 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
796 11cc08c1 2020-07-23 stsp goto done;
797 11cc08c1 2020-07-23 stsp }
798 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
799 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
800 11cc08c1 2020-07-23 stsp goto done;
801 11cc08c1 2020-07-23 stsp }
802 11cc08c1 2020-07-23 stsp done:
803 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
805 11cc08c1 2020-07-23 stsp free(path);
806 11cc08c1 2020-07-23 stsp free(id_str);
807 11cc08c1 2020-07-23 stsp free(label_deriv);
808 11cc08c1 2020-07-23 stsp return err;
809 11cc08c1 2020-07-23 stsp }
810 d219f183 2020-07-23 stsp
811 d219f183 2020-07-23 stsp /* forward declaration */
812 d219f183 2020-07-23 stsp static const struct got_error *
813 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
815 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
816 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
817 11cc08c1 2020-07-23 stsp
818 11cc08c1 2020-07-23 stsp /*
819 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
820 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
821 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
822 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
823 af57b12a 2020-07-23 stsp */
824 af57b12a 2020-07-23 stsp static const struct got_error *
825 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
826 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
827 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
828 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
830 af57b12a 2020-07-23 stsp {
831 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
832 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
833 af57b12a 2020-07-23 stsp struct stat sb;
834 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
835 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
836 11cc08c1 2020-07-23 stsp int have_local_change = 0;
837 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
838 af57b12a 2020-07-23 stsp
839 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
840 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
843 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
844 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
845 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
846 af57b12a 2020-07-23 stsp ondisk_path);
847 af57b12a 2020-07-23 stsp goto done;
848 af57b12a 2020-07-23 stsp }
849 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
850 af57b12a 2020-07-23 stsp
851 526a746f 2020-07-23 stsp if (blob_orig) {
852 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
853 526a746f 2020-07-23 stsp if (err)
854 526a746f 2020-07-23 stsp goto done;
855 526a746f 2020-07-23 stsp }
856 af57b12a 2020-07-23 stsp
857 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
858 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
859 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 11cc08c1 2020-07-23 stsp have_local_change = 1;
861 11cc08c1 2020-07-23 stsp
862 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
863 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
864 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
865 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
867 11cc08c1 2020-07-23 stsp
868 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
869 11cc08c1 2020-07-23 stsp if (ancestor_target) {
870 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
871 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
872 11cc08c1 2020-07-23 stsp path);
873 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
874 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
876 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 11cc08c1 2020-07-23 stsp path);
878 11cc08c1 2020-07-23 stsp } else {
879 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
880 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
881 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
882 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
883 11cc08c1 2020-07-23 stsp if (err)
884 11cc08c1 2020-07-23 stsp goto done;
885 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
886 11cc08c1 2020-07-23 stsp path);
887 11cc08c1 2020-07-23 stsp }
888 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
889 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
890 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
891 af57b12a 2020-07-23 stsp strlen(deriv_target));
892 af57b12a 2020-07-23 stsp if (err)
893 af57b12a 2020-07-23 stsp goto done;
894 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
896 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
897 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
899 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 ea7786be 2020-07-23 stsp path);
901 ea7786be 2020-07-23 stsp } else {
902 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
903 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
904 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
905 ea7786be 2020-07-23 stsp if (err)
906 ea7786be 2020-07-23 stsp goto done;
907 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
908 ea7786be 2020-07-23 stsp path);
909 ea7786be 2020-07-23 stsp }
910 6353ad76 2019-02-08 stsp }
911 11cc08c1 2020-07-23 stsp
912 af57b12a 2020-07-23 stsp done:
913 af57b12a 2020-07-23 stsp free(ancestor_target);
914 eec2f5a9 2021-06-03 stsp return err;
915 eec2f5a9 2021-06-03 stsp }
916 eec2f5a9 2021-06-03 stsp
917 eec2f5a9 2021-06-03 stsp static const struct got_error *
918 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
919 eec2f5a9 2021-06-03 stsp {
920 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
921 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
922 eec2f5a9 2021-06-03 stsp ssize_t target_len;
923 eec2f5a9 2021-06-03 stsp size_t n;
924 eec2f5a9 2021-06-03 stsp FILE *f;
925 eec2f5a9 2021-06-03 stsp
926 eec2f5a9 2021-06-03 stsp *outfile = NULL;
927 eec2f5a9 2021-06-03 stsp
928 eec2f5a9 2021-06-03 stsp f = got_opentemp();
929 eec2f5a9 2021-06-03 stsp if (f == NULL)
930 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
931 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
933 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
934 eec2f5a9 2021-06-03 stsp goto done;
935 eec2f5a9 2021-06-03 stsp }
936 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
937 eec2f5a9 2021-06-03 stsp if (n != target_len) {
938 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
939 eec2f5a9 2021-06-03 stsp goto done;
940 eec2f5a9 2021-06-03 stsp }
941 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
942 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
943 eec2f5a9 2021-06-03 stsp goto done;
944 eec2f5a9 2021-06-03 stsp }
945 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
946 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
947 eec2f5a9 2021-06-03 stsp goto done;
948 eec2f5a9 2021-06-03 stsp }
949 eec2f5a9 2021-06-03 stsp done:
950 eec2f5a9 2021-06-03 stsp if (err)
951 eec2f5a9 2021-06-03 stsp fclose(f);
952 eec2f5a9 2021-06-03 stsp else
953 eec2f5a9 2021-06-03 stsp *outfile = f;
954 14c901f1 2019-08-08 stsp return err;
955 14c901f1 2019-08-08 stsp }
956 14c901f1 2019-08-08 stsp
957 14c901f1 2019-08-08 stsp /*
958 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
960 14c901f1 2019-08-08 stsp * acts as the second derived version.
961 14c901f1 2019-08-08 stsp */
962 14c901f1 2019-08-08 stsp static const struct got_error *
963 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
965 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
966 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
967 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
969 14c901f1 2019-08-08 stsp {
970 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
971 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
973 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
975 14c901f1 2019-08-08 stsp
976 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
977 14c901f1 2019-08-08 stsp
978 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
979 ed6b5030 2020-10-20 stsp if (err)
980 ed6b5030 2020-10-20 stsp return err;
981 14c901f1 2019-08-08 stsp
982 67a66647 2021-05-31 stsp if (blob_orig) {
983 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
984 67a66647 2021-05-31 stsp parent) == -1) {
985 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
986 67a66647 2021-05-31 stsp base_path = NULL;
987 67a66647 2021-05-31 stsp goto done;
988 67a66647 2021-05-31 stsp }
989 67a66647 2021-05-31 stsp
990 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
991 67a66647 2021-05-31 stsp if (err)
992 67a66647 2021-05-31 stsp goto done;
993 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
994 67a66647 2021-05-31 stsp blob_orig);
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp free(base_path);
998 db590691 2021-06-02 stsp } else {
999 db590691 2021-06-02 stsp /*
1000 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1001 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1002 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1003 db590691 2021-06-02 stsp */
1004 db590691 2021-06-02 stsp f_orig = got_opentemp();
1005 db590691 2021-06-02 stsp if (f_orig == NULL) {
1006 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1007 db590691 2021-06-02 stsp goto done;
1008 db590691 2021-06-02 stsp }
1009 67a66647 2021-05-31 stsp }
1010 67a66647 2021-05-31 stsp
1011 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1013 14c901f1 2019-08-08 stsp base_path = NULL;
1014 14c901f1 2019-08-08 stsp goto done;
1015 14c901f1 2019-08-08 stsp }
1016 14c901f1 2019-08-08 stsp
1017 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1018 14c901f1 2019-08-08 stsp if (err)
1019 14c901f1 2019-08-08 stsp goto done;
1020 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1021 14c901f1 2019-08-08 stsp blob_deriv);
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp
1025 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1029 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1031 14c901f1 2019-08-08 stsp goto done;
1032 eec2f5a9 2021-06-03 stsp }
1033 eec2f5a9 2021-06-03 stsp
1034 eec2f5a9 2021-06-03 stsp /*
1035 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1036 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1037 eec2f5a9 2021-06-03 stsp */
1038 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1039 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1040 eec2f5a9 2021-06-03 stsp if (err)
1041 eec2f5a9 2021-06-03 stsp goto done;
1042 eec2f5a9 2021-06-03 stsp } else {
1043 eec2f5a9 2021-06-03 stsp int fd;
1044 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1045 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1046 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1047 eec2f5a9 2021-06-03 stsp goto done;
1048 eec2f5a9 2021-06-03 stsp }
1049 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1050 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1051 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1052 eec2f5a9 2021-06-03 stsp close(fd);
1053 eec2f5a9 2021-06-03 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp }
1055 14c901f1 2019-08-08 stsp }
1056 14c901f1 2019-08-08 stsp
1057 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1060 14c901f1 2019-08-08 stsp done:
1061 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1063 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1065 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1067 14c901f1 2019-08-08 stsp free(base_path);
1068 67a66647 2021-05-31 stsp if (blob_orig_path) {
1069 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1070 67a66647 2021-05-31 stsp free(blob_orig_path);
1071 67a66647 2021-05-31 stsp }
1072 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1073 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1074 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1075 14c901f1 2019-08-08 stsp }
1076 6353ad76 2019-02-08 stsp free(id_str);
1077 818c7501 2019-07-11 stsp free(label_deriv);
1078 ed6b5030 2020-10-20 stsp free(parent);
1079 4a1ddfc2 2019-01-12 stsp return err;
1080 4a1ddfc2 2019-01-12 stsp }
1081 4a1ddfc2 2019-01-12 stsp
1082 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1083 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1086 13d9040b 2019-03-27 stsp {
1087 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1088 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1089 13d9040b 2019-03-27 stsp
1090 65b05cec 2020-07-23 stsp *new_iep = NULL;
1091 65b05cec 2020-07-23 stsp
1092 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1093 054041d0 2020-06-24 stsp if (err)
1094 054041d0 2020-06-24 stsp return err;
1095 054041d0 2020-06-24 stsp
1096 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1098 054041d0 2020-06-24 stsp if (err)
1099 054041d0 2020-06-24 stsp goto done;
1100 054041d0 2020-06-24 stsp
1101 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1102 054041d0 2020-06-24 stsp done:
1103 054041d0 2020-06-24 stsp if (err)
1104 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1105 65b05cec 2020-07-23 stsp else
1106 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1107 13d9040b 2019-03-27 stsp return err;
1108 1ebedb77 2019-10-19 stsp }
1109 1ebedb77 2019-10-19 stsp
1110 1ebedb77 2019-10-19 stsp static mode_t
1111 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1112 1ebedb77 2019-10-19 stsp {
1113 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1114 1ebedb77 2019-10-19 stsp
1115 1ebedb77 2019-10-19 stsp if (executable) {
1116 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1117 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1118 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1119 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1120 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1121 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1122 1ebedb77 2019-10-19 stsp }
1123 1ebedb77 2019-10-19 stsp
1124 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1125 13d9040b 2019-03-27 stsp }
1126 13d9040b 2019-03-27 stsp
1127 8ba819a3 2020-07-23 stsp /* forward declaration */
1128 13d9040b 2019-03-27 stsp static const struct got_error *
1129 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1131 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1132 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1133 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1134 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1135 8ba819a3 2020-07-23 stsp
1136 5a1fbc73 2020-07-23 stsp /*
1137 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1138 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1139 5a1fbc73 2020-07-23 stsp */
1140 8ba819a3 2020-07-23 stsp static const struct got_error *
1141 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1143 5a1fbc73 2020-07-23 stsp {
1144 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1145 5a1fbc73 2020-07-23 stsp ssize_t elen;
1146 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1147 5a1fbc73 2020-07-23 stsp int fd;
1148 5a1fbc73 2020-07-23 stsp
1149 c6e8a826 2021-04-05 stsp *did_something = 0;
1150 c6e8a826 2021-04-05 stsp
1151 5a1fbc73 2020-07-23 stsp /*
1152 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1153 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1154 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1155 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1156 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1157 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1158 5a1fbc73 2020-07-23 stsp */
1159 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1160 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1161 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1162 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1163 5a1fbc73 2020-07-23 stsp
1164 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1165 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 5a1fbc73 2020-07-23 stsp if (elen == -1)
1167 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1168 5a1fbc73 2020-07-23 stsp
1169 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1170 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1171 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1172 5a1fbc73 2020-07-23 stsp }
1173 5a1fbc73 2020-07-23 stsp
1174 c6e8a826 2021-04-05 stsp *did_something = 1;
1175 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1176 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1177 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1178 5a1fbc73 2020-07-23 stsp return err;
1179 3c1ec782 2020-07-23 stsp }
1180 3c1ec782 2020-07-23 stsp
1181 3c1ec782 2020-07-23 stsp static const struct got_error *
1182 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1184 3c1ec782 2020-07-23 stsp {
1185 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1186 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1187 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1188 3c1ec782 2020-07-23 stsp
1189 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1190 3c1ec782 2020-07-23 stsp
1191 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1192 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1193 3c1ec782 2020-07-23 stsp return NULL;
1194 3c1ec782 2020-07-23 stsp }
1195 3c1ec782 2020-07-23 stsp
1196 3c1ec782 2020-07-23 stsp /*
1197 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1198 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1199 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1200 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1201 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1202 3c1ec782 2020-07-23 stsp */
1203 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1204 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1205 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1206 ce031e9e 2020-10-20 stsp if (err)
1207 ce031e9e 2020-10-20 stsp return err;
1208 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1209 ce031e9e 2020-10-20 stsp free(parent);
1210 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1211 ce031e9e 2020-10-20 stsp }
1212 ce031e9e 2020-10-20 stsp free(parent);
1213 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1214 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1215 3c1ec782 2020-07-23 stsp free(abspath);
1216 3c1ec782 2020-07-23 stsp return err;
1217 3c1ec782 2020-07-23 stsp }
1218 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1219 3c1ec782 2020-07-23 stsp free(abspath);
1220 3c1ec782 2020-07-23 stsp if (err)
1221 3c1ec782 2020-07-23 stsp return err;
1222 3c1ec782 2020-07-23 stsp } else {
1223 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1224 3c1ec782 2020-07-23 stsp if (err)
1225 3c1ec782 2020-07-23 stsp return err;
1226 3c1ec782 2020-07-23 stsp }
1227 3c1ec782 2020-07-23 stsp
1228 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1229 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1231 3c1ec782 2020-07-23 stsp return NULL;
1232 3c1ec782 2020-07-23 stsp }
1233 3c1ec782 2020-07-23 stsp
1234 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1235 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1237 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1238 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1240 3c1ec782 2020-07-23 stsp
1241 3c1ec782 2020-07-23 stsp free(path_got);
1242 3c1ec782 2020-07-23 stsp return NULL;
1243 5a1fbc73 2020-07-23 stsp }
1244 5a1fbc73 2020-07-23 stsp
1245 5a1fbc73 2020-07-23 stsp static const struct got_error *
1246 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1249 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1250 ace90326 2021-09-27 thomas struct got_repository *repo,
1251 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1252 9d31a1d8 2018-03-11 stsp {
1253 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1254 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1255 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1256 906c123b 2020-07-23 stsp char *path_got = NULL;
1257 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1259 8ba819a3 2020-07-23 stsp
1260 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1261 2e1fa222 2020-07-23 stsp
1262 8ba819a3 2020-07-23 stsp /*
1263 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1264 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1265 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1266 8ba819a3 2020-07-23 stsp * in the blob object.
1267 8ba819a3 2020-07-23 stsp */
1268 8ba819a3 2020-07-23 stsp do {
1269 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1270 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1271 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1272 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1273 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1274 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1275 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1277 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1278 3b9f0f87 2020-07-23 stsp progress_arg);
1279 8ba819a3 2020-07-23 stsp }
1280 8ba819a3 2020-07-23 stsp if (len > 0) {
1281 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1282 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1283 8ba819a3 2020-07-23 stsp len - hdrlen);
1284 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1285 8ba819a3 2020-07-23 stsp hdrlen = 0;
1286 8ba819a3 2020-07-23 stsp }
1287 8ba819a3 2020-07-23 stsp } while (len != 0);
1288 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1289 8ba819a3 2020-07-23 stsp
1290 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1292 3c1ec782 2020-07-23 stsp if (err)
1293 3c1ec782 2020-07-23 stsp return err;
1294 8ba819a3 2020-07-23 stsp
1295 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1296 906c123b 2020-07-23 stsp /* install as a regular file */
1297 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1298 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1299 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1301 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1302 906c123b 2020-07-23 stsp goto done;
1303 906c123b 2020-07-23 stsp }
1304 906c123b 2020-07-23 stsp
1305 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1306 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1307 c6e8a826 2021-04-05 stsp int symlink_replaced;
1308 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1309 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1310 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1311 c90c8ce3 2020-07-23 stsp goto done;
1312 c90c8ce3 2020-07-23 stsp }
1313 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1314 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1315 5a1fbc73 2020-07-23 stsp if (err)
1316 f35fa46a 2020-07-23 stsp goto done;
1317 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1318 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1319 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1320 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1321 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1322 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1323 c6e8a826 2021-04-05 stsp } else {
1324 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1326 c6e8a826 2021-04-05 stsp }
1327 f35fa46a 2020-07-23 stsp }
1328 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1329 f35fa46a 2020-07-23 stsp }
1330 f35fa46a 2020-07-23 stsp
1331 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1332 f4994adc 2020-10-20 stsp char *parent;
1333 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1334 f4994adc 2020-10-20 stsp if (err)
1335 f4994adc 2020-10-20 stsp goto done;
1336 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1337 f4994adc 2020-10-20 stsp free(parent);
1338 f35fa46a 2020-07-23 stsp if (err)
1339 f35fa46a 2020-07-23 stsp goto done;
1340 f35fa46a 2020-07-23 stsp /*
1341 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1342 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1343 f35fa46a 2020-07-23 stsp */
1344 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1345 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1346 f35fa46a 2020-07-23 stsp goto done;
1347 f35fa46a 2020-07-23 stsp }
1348 f35fa46a 2020-07-23 stsp }
1349 f35fa46a 2020-07-23 stsp
1350 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1351 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1352 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1353 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1354 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1355 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1356 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1358 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1359 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1360 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1361 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1362 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1363 8ba819a3 2020-07-23 stsp } else {
1364 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1365 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1366 8ba819a3 2020-07-23 stsp }
1367 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1368 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1370 8ba819a3 2020-07-23 stsp done:
1371 906c123b 2020-07-23 stsp free(path_got);
1372 8ba819a3 2020-07-23 stsp return err;
1373 8ba819a3 2020-07-23 stsp }
1374 8ba819a3 2020-07-23 stsp
1375 8ba819a3 2020-07-23 stsp static const struct got_error *
1376 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1378 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1379 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1380 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1381 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1382 8ba819a3 2020-07-23 stsp {
1383 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1384 507dc3bb 2018-12-29 stsp int fd = -1;
1385 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1386 507dc3bb 2018-12-29 stsp int update = 0;
1387 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1388 8ba819a3 2020-07-23 stsp
1389 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 06340621 2021-12-31 thomas O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1391 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1392 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1393 f5375317 2020-10-20 stsp char *parent;
1394 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1395 f5375317 2020-10-20 stsp if (err)
1396 f5375317 2020-10-20 stsp return err;
1397 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1398 f5375317 2020-10-20 stsp free(parent);
1399 21908da4 2019-01-13 stsp if (err)
1400 21908da4 2019-01-13 stsp return err;
1401 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1402 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1404 21908da4 2019-01-13 stsp if (fd == -1)
1405 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1406 230a42bd 2019-05-11 jcs ondisk_path);
1407 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1408 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1409 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1410 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1411 3b9f0f87 2020-07-23 stsp goto done;
1412 3b9f0f87 2020-07-23 stsp }
1413 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1416 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1417 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1418 507dc3bb 2018-12-29 stsp goto done;
1419 d70b8e30 2018-12-27 stsp } else {
1420 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1421 507dc3bb 2018-12-29 stsp ondisk_path);
1422 507dc3bb 2018-12-29 stsp if (err)
1423 507dc3bb 2018-12-29 stsp goto done;
1424 507dc3bb 2018-12-29 stsp update = 1;
1425 9d31a1d8 2018-03-11 stsp }
1426 507dc3bb 2018-12-29 stsp } else
1427 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1428 9d31a1d8 2018-03-11 stsp }
1429 9d31a1d8 2018-03-11 stsp
1430 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1432 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1433 3818e3c4 2020-11-01 naddy goto done;
1434 3818e3c4 2020-11-01 naddy }
1435 3818e3c4 2020-11-01 naddy
1436 bd6aa359 2020-07-23 stsp if (progress_cb) {
1437 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1438 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1439 bd6aa359 2020-07-23 stsp path);
1440 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1441 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1442 bd6aa359 2020-07-23 stsp path);
1443 bd6aa359 2020-07-23 stsp else
1444 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1445 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1446 bd6aa359 2020-07-23 stsp if (err)
1447 bd6aa359 2020-07-23 stsp goto done;
1448 bd6aa359 2020-07-23 stsp }
1449 d7b62c98 2018-12-27 stsp
1450 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1451 9d31a1d8 2018-03-11 stsp do {
1452 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1454 9d31a1d8 2018-03-11 stsp if (err)
1455 9d31a1d8 2018-03-11 stsp break;
1456 9d31a1d8 2018-03-11 stsp if (len > 0) {
1457 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1458 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1459 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1460 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1461 b87c6f83 2018-12-24 stsp goto done;
1462 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1463 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1464 b87c6f83 2018-12-24 stsp goto done;
1465 9d31a1d8 2018-03-11 stsp }
1466 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1467 9d31a1d8 2018-03-11 stsp }
1468 9d31a1d8 2018-03-11 stsp } while (len != 0);
1469 9d31a1d8 2018-03-11 stsp
1470 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1471 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1472 816dc654 2019-02-16 stsp goto done;
1473 816dc654 2019-02-16 stsp }
1474 9d31a1d8 2018-03-11 stsp
1475 507dc3bb 2018-12-29 stsp if (update) {
1476 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1478 f6d8c0ac 2020-11-09 stsp goto done;
1479 f6d8c0ac 2020-11-09 stsp }
1480 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1481 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1482 230a42bd 2019-05-11 jcs ondisk_path);
1483 68ed9ba5 2019-02-10 stsp goto done;
1484 68ed9ba5 2019-02-10 stsp }
1485 63df146d 2020-11-09 stsp free(tmppath);
1486 63df146d 2020-11-09 stsp tmppath = NULL;
1487 507dc3bb 2018-12-29 stsp }
1488 507dc3bb 2018-12-29 stsp
1489 9d31a1d8 2018-03-11 stsp done:
1490 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1491 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1492 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1494 507dc3bb 2018-12-29 stsp free(tmppath);
1495 6353ad76 2019-02-08 stsp return err;
1496 6353ad76 2019-02-08 stsp }
1497 6353ad76 2019-02-08 stsp
1498 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 6353ad76 2019-02-08 stsp static const struct got_error *
1500 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1501 7154f6ce 2019-03-27 stsp {
1502 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1503 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1504 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1506 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1507 7154f6ce 2019-03-27 stsp };
1508 7154f6ce 2019-03-27 stsp int i = 0;
1509 9bdd68dd 2020-01-02 naddy char *line = NULL;
1510 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1511 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1512 7154f6ce 2019-03-27 stsp
1513 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1514 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1515 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1516 7154f6ce 2019-03-27 stsp if (feof(f))
1517 7154f6ce 2019-03-27 stsp break;
1518 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1519 7154f6ce 2019-03-27 stsp break;
1520 7154f6ce 2019-03-27 stsp }
1521 7154f6ce 2019-03-27 stsp
1522 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1524 19332e6d 2019-05-13 stsp == 0)
1525 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1526 7154f6ce 2019-03-27 stsp else
1527 7154f6ce 2019-03-27 stsp i++;
1528 7154f6ce 2019-03-27 stsp }
1529 7154f6ce 2019-03-27 stsp }
1530 9bdd68dd 2020-01-02 naddy free(line);
1531 7154f6ce 2019-03-27 stsp
1532 7154f6ce 2019-03-27 stsp return err;
1533 e2b1e152 2019-07-13 stsp }
1534 e2b1e152 2019-07-13 stsp
1535 e2b1e152 2019-07-13 stsp static int
1536 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1537 1ebedb77 2019-10-19 stsp {
1538 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1540 1ebedb77 2019-10-19 stsp }
1541 1ebedb77 2019-10-19 stsp
1542 1ebedb77 2019-10-19 stsp static int
1543 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1544 e2b1e152 2019-07-13 stsp {
1545 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1550 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1551 c363b2c1 2019-08-03 stsp }
1552 c363b2c1 2019-08-03 stsp
1553 c363b2c1 2019-08-03 stsp static unsigned char
1554 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1555 c363b2c1 2019-08-03 stsp {
1556 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1557 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1558 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1559 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1560 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1561 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1562 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1563 c363b2c1 2019-08-03 stsp default:
1564 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1565 a919d5c4 2020-07-23 stsp }
1566 a919d5c4 2020-07-23 stsp }
1567 a919d5c4 2020-07-23 stsp
1568 a919d5c4 2020-07-23 stsp static const struct got_error *
1569 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1570 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1571 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1572 a919d5c4 2020-07-23 stsp {
1573 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1574 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1575 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1576 a919d5c4 2020-07-23 stsp ssize_t elen;
1577 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1578 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1580 a919d5c4 2020-07-23 stsp
1581 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1582 a919d5c4 2020-07-23 stsp
1583 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1584 a919d5c4 2020-07-23 stsp do {
1585 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1586 a919d5c4 2020-07-23 stsp if (err)
1587 a919d5c4 2020-07-23 stsp return err;
1588 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1589 a919d5c4 2020-07-23 stsp /*
1590 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1591 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1592 a919d5c4 2020-07-23 stsp */
1593 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1594 a919d5c4 2020-07-23 stsp }
1595 a919d5c4 2020-07-23 stsp if (len > 0) {
1596 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1597 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1598 a919d5c4 2020-07-23 stsp len - hdrlen);
1599 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1600 a919d5c4 2020-07-23 stsp hdrlen = 0;
1601 a919d5c4 2020-07-23 stsp }
1602 a919d5c4 2020-07-23 stsp } while (len != 0);
1603 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1604 a919d5c4 2020-07-23 stsp
1605 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1606 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1607 a919d5c4 2020-07-23 stsp if (elen == -1)
1608 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1609 a919d5c4 2020-07-23 stsp } else {
1610 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1611 a919d5c4 2020-07-23 stsp if (elen == -1)
1612 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1613 c363b2c1 2019-08-03 stsp }
1614 a919d5c4 2020-07-23 stsp
1615 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1617 a919d5c4 2020-07-23 stsp
1618 a919d5c4 2020-07-23 stsp return NULL;
1619 7154f6ce 2019-03-27 stsp }
1620 7154f6ce 2019-03-27 stsp
1621 7154f6ce 2019-03-27 stsp static const struct got_error *
1622 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1623 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1624 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1625 6353ad76 2019-02-08 stsp {
1626 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1627 6353ad76 2019-02-08 stsp struct got_object_id id;
1628 6353ad76 2019-02-08 stsp size_t hdrlen;
1629 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1630 6353ad76 2019-02-08 stsp FILE *f = NULL;
1631 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1632 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1633 6353ad76 2019-02-08 stsp size_t flen, blen;
1634 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1635 6353ad76 2019-02-08 stsp
1636 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1637 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1638 6353ad76 2019-02-08 stsp
1639 7f91a133 2019-12-13 stsp /*
1640 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1641 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1642 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1643 7f91a133 2019-12-13 stsp */
1644 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1645 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1647 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1648 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1649 882ef1b9 2019-12-13 stsp else
1650 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1651 882ef1b9 2019-12-13 stsp goto done;
1652 882ef1b9 2019-12-13 stsp }
1653 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1654 3d35a492 2019-12-13 stsp goto done;
1655 3d35a492 2019-12-13 stsp }
1656 7f91a133 2019-12-13 stsp } else {
1657 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1659 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1660 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1661 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1663 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1664 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1666 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1667 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1668 3d35a492 2019-12-13 stsp else
1669 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1670 3d35a492 2019-12-13 stsp goto done;
1671 3d35a492 2019-12-13 stsp }
1672 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1673 1338848f 2019-12-13 stsp goto done;
1674 a378724f 2019-02-10 stsp }
1675 a378724f 2019-02-10 stsp }
1676 6353ad76 2019-02-08 stsp
1677 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1679 1338848f 2019-12-13 stsp goto done;
1680 3f148bc6 2019-07-27 stsp }
1681 efdd40df 2019-07-27 stsp
1682 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1684 1338848f 2019-12-13 stsp goto done;
1685 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1686 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1687 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1688 1338848f 2019-12-13 stsp goto done;
1689 d00136be 2019-03-26 stsp }
1690 b8f41171 2019-02-10 stsp
1691 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1692 f179e66d 2020-07-23 stsp goto done;
1693 f179e66d 2020-07-23 stsp
1694 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1695 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1697 1338848f 2019-12-13 stsp goto done;
1698 f179e66d 2020-07-23 stsp }
1699 6353ad76 2019-02-08 stsp
1700 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1701 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1702 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1703 c363b2c1 2019-08-03 stsp else
1704 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1705 c363b2c1 2019-08-03 stsp
1706 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1707 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1708 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1709 f4ae6ddb 2022-07-01 thomas goto done;
1710 f4ae6ddb 2022-07-01 thomas }
1711 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1712 6353ad76 2019-02-08 stsp if (err)
1713 1338848f 2019-12-13 stsp goto done;
1714 6353ad76 2019-02-08 stsp
1715 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1716 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1717 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1718 a919d5c4 2020-07-23 stsp goto done;
1719 a919d5c4 2020-07-23 stsp }
1720 a919d5c4 2020-07-23 stsp
1721 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1722 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1723 ab0d4361 2019-12-13 stsp if (fd == -1) {
1724 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1725 ab0d4361 2019-12-13 stsp goto done;
1726 ab0d4361 2019-12-13 stsp }
1727 3d35a492 2019-12-13 stsp }
1728 3d35a492 2019-12-13 stsp
1729 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1730 6353ad76 2019-02-08 stsp if (f == NULL) {
1731 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1732 6353ad76 2019-02-08 stsp goto done;
1733 6353ad76 2019-02-08 stsp }
1734 1338848f 2019-12-13 stsp fd = -1;
1735 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1736 656b1f76 2019-05-11 jcs for (;;) {
1737 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1738 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1739 6353ad76 2019-02-08 stsp if (err)
1740 7154f6ce 2019-03-27 stsp goto done;
1741 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1742 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1743 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1744 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1745 7154f6ce 2019-03-27 stsp goto done;
1746 80c5c120 2019-02-19 stsp }
1747 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1748 6353ad76 2019-02-08 stsp if (flen != 0)
1749 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1750 6353ad76 2019-02-08 stsp break;
1751 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1752 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1753 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1754 6353ad76 2019-02-08 stsp break;
1755 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1756 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1757 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1758 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1759 6353ad76 2019-02-08 stsp break;
1760 6353ad76 2019-02-08 stsp }
1761 6353ad76 2019-02-08 stsp } else {
1762 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1763 6353ad76 2019-02-08 stsp break;
1764 6353ad76 2019-02-08 stsp }
1765 6353ad76 2019-02-08 stsp hdrlen = 0;
1766 6353ad76 2019-02-08 stsp }
1767 7154f6ce 2019-03-27 stsp
1768 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1769 7154f6ce 2019-03-27 stsp rewind(f);
1770 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1771 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1772 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1773 6353ad76 2019-02-08 stsp done:
1774 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1775 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1776 6353ad76 2019-02-08 stsp if (blob)
1777 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1778 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1779 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1780 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1781 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1782 9d31a1d8 2018-03-11 stsp return err;
1783 e2b1e152 2019-07-13 stsp }
1784 e2b1e152 2019-07-13 stsp
1785 e2b1e152 2019-07-13 stsp /*
1786 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1787 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1788 e2b1e152 2019-07-13 stsp */
1789 e2b1e152 2019-07-13 stsp static const struct got_error *
1790 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1791 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1792 e2b1e152 2019-07-13 stsp {
1793 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1794 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1795 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1796 e2b1e152 2019-07-13 stsp
1797 e2b1e152 2019-07-13 stsp return NULL;
1798 9d31a1d8 2018-03-11 stsp }
1799 9d31a1d8 2018-03-11 stsp
1800 9d31a1d8 2018-03-11 stsp static const struct got_error *
1801 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1802 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1803 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1804 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1805 0584f854 2019-04-06 stsp void *progress_arg)
1806 9d31a1d8 2018-03-11 stsp {
1807 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1808 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1809 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1810 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1811 b8f41171 2019-02-10 stsp struct stat sb;
1812 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1813 9d31a1d8 2018-03-11 stsp
1814 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1815 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1816 6353ad76 2019-02-08 stsp
1817 abb4604f 2019-07-27 stsp if (ie) {
1818 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1819 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1820 a76c42e6 2019-08-03 stsp goto done;
1821 a76c42e6 2019-08-03 stsp }
1822 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1823 7f91a133 2019-12-13 stsp repo);
1824 abb4604f 2019-07-27 stsp if (err)
1825 abb4604f 2019-07-27 stsp goto done;
1826 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1827 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1828 c90c8ce3 2020-07-23 stsp } else {
1829 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1830 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1831 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1832 e6f4ba31 2021-09-24 thomas ondisk_path);
1833 e6f4ba31 2021-09-24 thomas goto done;
1834 e6f4ba31 2021-09-24 thomas }
1835 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1836 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1837 e6f4ba31 2021-09-24 thomas } else {
1838 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1839 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1840 e6f4ba31 2021-09-24 thomas else
1841 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1842 e6f4ba31 2021-09-24 thomas }
1843 c90c8ce3 2020-07-23 stsp }
1844 6353ad76 2019-02-08 stsp
1845 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1846 2c41dce7 2021-06-27 stsp if (ie)
1847 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1848 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1849 b8f41171 2019-02-10 stsp goto done;
1850 b8f41171 2019-02-10 stsp }
1851 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1852 a769b60b 2021-06-27 stsp if (ie)
1853 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1854 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1855 5036ab18 2020-04-18 stsp path);
1856 5036ab18 2020-04-18 stsp goto done;
1857 5036ab18 2020-04-18 stsp }
1858 b8f41171 2019-02-10 stsp
1859 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1860 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1861 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1862 c6e8a826 2021-04-05 stsp /*
1863 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1864 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1865 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1866 c6e8a826 2021-04-05 stsp * updating contents of this file.
1867 c6e8a826 2021-04-05 stsp */
1868 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1869 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1871 c6e8a826 2021-04-05 stsp /* Same commit. */
1872 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1873 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1874 e2b1e152 2019-07-13 stsp if (err)
1875 e2b1e152 2019-07-13 stsp goto done;
1876 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1877 b8f41171 2019-02-10 stsp path);
1878 6353ad76 2019-02-08 stsp goto done;
1879 a378724f 2019-02-10 stsp }
1880 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1881 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1882 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1883 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1884 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1885 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1886 0f58026f 2021-04-05 stsp if (err)
1887 0f58026f 2021-04-05 stsp goto done;
1888 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 0f58026f 2021-04-05 stsp path);
1890 b8f41171 2019-02-10 stsp goto done;
1891 e2b1e152 2019-07-13 stsp }
1892 9d31a1d8 2018-03-11 stsp }
1893 9d31a1d8 2018-03-11 stsp
1894 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1895 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1896 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1897 f4ae6ddb 2022-07-01 thomas goto done;
1898 f4ae6ddb 2022-07-01 thomas }
1899 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1900 8da9e5f4 2019-01-12 stsp if (err)
1901 6353ad76 2019-02-08 stsp goto done;
1902 512f0d0e 2019-01-02 stsp
1903 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1904 234035bc 2019-06-01 stsp int update_timestamps;
1905 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1906 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1907 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1908 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1909 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1910 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1911 f4ae6ddb 2022-07-01 thomas goto done;
1912 f4ae6ddb 2022-07-01 thomas }
1913 234035bc 2019-06-01 stsp struct got_object_id id2;
1914 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1915 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1916 f4ae6ddb 2022-07-01 thomas fd2);
1917 234035bc 2019-06-01 stsp if (err)
1918 f69721c3 2019-10-21 stsp goto done;
1919 f69721c3 2019-10-21 stsp }
1920 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1921 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1922 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1923 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1924 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1925 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1926 f69721c3 2019-10-21 stsp goto done;
1927 f69721c3 2019-10-21 stsp }
1928 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1929 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1930 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1931 234035bc 2019-06-01 stsp goto done;
1932 f69721c3 2019-10-21 stsp }
1933 234035bc 2019-06-01 stsp }
1934 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1935 36bf999c 2020-07-23 stsp char *link_target;
1936 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1937 36bf999c 2020-07-23 stsp if (err)
1938 36bf999c 2020-07-23 stsp goto done;
1939 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1940 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1941 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1942 36bf999c 2020-07-23 stsp free(link_target);
1943 993e2a1b 2020-07-23 stsp } else {
1944 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1945 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1946 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1947 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1948 993e2a1b 2020-07-23 stsp }
1949 f69721c3 2019-10-21 stsp free(label_orig);
1950 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1951 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1952 f4ae6ddb 2022-07-01 thomas goto done;
1953 f4ae6ddb 2022-07-01 thomas }
1954 234035bc 2019-06-01 stsp if (blob2)
1955 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1956 909d120e 2019-09-22 stsp if (err)
1957 909d120e 2019-09-22 stsp goto done;
1958 234035bc 2019-06-01 stsp /*
1959 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1960 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1961 234035bc 2019-06-01 stsp * unmodified files again.
1962 234035bc 2019-06-01 stsp */
1963 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1964 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1965 234035bc 2019-06-01 stsp update_timestamps);
1966 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1967 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1968 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1969 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1970 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1971 1ee397ad 2019-07-12 stsp if (err)
1972 1ee397ad 2019-07-12 stsp goto done;
1973 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1974 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1975 13d9040b 2019-03-27 stsp if (err)
1976 13d9040b 2019-03-27 stsp goto done;
1977 13d9040b 2019-03-27 stsp } else {
1978 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1979 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1980 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1981 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1982 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1983 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1984 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
1985 2e1fa222 2020-07-23 stsp } else {
1986 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1987 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1988 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1989 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1990 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1991 2e1fa222 2020-07-23 stsp }
1992 13d9040b 2019-03-27 stsp if (err)
1993 13d9040b 2019-03-27 stsp goto done;
1994 65b05cec 2020-07-23 stsp
1995 054041d0 2020-06-24 stsp if (ie) {
1996 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1997 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1998 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1999 054041d0 2020-06-24 stsp } else {
2000 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2001 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2002 054041d0 2020-06-24 stsp &blob->id);
2003 054041d0 2020-06-24 stsp }
2004 13d9040b 2019-03-27 stsp if (err)
2005 13d9040b 2019-03-27 stsp goto done;
2006 65b05cec 2020-07-23 stsp
2007 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2008 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2009 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2010 2e1fa222 2020-07-23 stsp }
2011 13d9040b 2019-03-27 stsp }
2012 f4ae6ddb 2022-07-01 thomas
2013 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2014 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2015 f4ae6ddb 2022-07-01 thomas goto done;
2016 f4ae6ddb 2022-07-01 thomas }
2017 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2018 6353ad76 2019-02-08 stsp done:
2019 6353ad76 2019-02-08 stsp free(ondisk_path);
2020 8da9e5f4 2019-01-12 stsp return err;
2021 90285c3b 2019-01-08 stsp }
2022 90285c3b 2019-01-08 stsp
2023 90285c3b 2019-01-08 stsp static const struct got_error *
2024 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2025 90285c3b 2019-01-08 stsp {
2026 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2027 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2028 90285c3b 2019-01-08 stsp
2029 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2030 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2031 90285c3b 2019-01-08 stsp
2032 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2033 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2034 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2035 c97665ae 2019-01-13 stsp } else {
2036 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2037 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2038 1c4cdd89 2021-06-20 stsp if (err)
2039 1c4cdd89 2021-06-20 stsp goto done;
2040 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2041 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2042 fddefe3b 2020-10-20 stsp free(ondisk_path);
2043 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2044 1c4cdd89 2021-06-20 stsp parent = NULL;
2045 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2046 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2047 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2048 fddefe3b 2020-10-20 stsp ondisk_path);
2049 90285c3b 2019-01-08 stsp break;
2050 90285c3b 2019-01-08 stsp }
2051 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2052 1c4cdd89 2021-06-20 stsp if (err)
2053 1c4cdd89 2021-06-20 stsp break;
2054 1c4cdd89 2021-06-20 stsp }
2055 90285c3b 2019-01-08 stsp }
2056 1c4cdd89 2021-06-20 stsp done:
2057 90285c3b 2019-01-08 stsp free(ondisk_path);
2058 1c4cdd89 2021-06-20 stsp free(parent);
2059 90285c3b 2019-01-08 stsp return err;
2060 512f0d0e 2019-01-02 stsp }
2061 512f0d0e 2019-01-02 stsp
2062 708d8e67 2019-03-27 stsp static const struct got_error *
2063 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2064 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2065 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2066 708d8e67 2019-03-27 stsp {
2067 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2068 708d8e67 2019-03-27 stsp unsigned char status;
2069 708d8e67 2019-03-27 stsp struct stat sb;
2070 708d8e67 2019-03-27 stsp char *ondisk_path;
2071 708d8e67 2019-03-27 stsp
2072 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2073 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2074 a76c42e6 2019-08-03 stsp
2075 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2076 708d8e67 2019-03-27 stsp == -1)
2077 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2078 708d8e67 2019-03-27 stsp
2079 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2080 708d8e67 2019-03-27 stsp if (err)
2081 5a58a424 2020-06-23 stsp goto done;
2082 708d8e67 2019-03-27 stsp
2083 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2084 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2085 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2086 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2087 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2088 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2089 993e2a1b 2020-07-23 stsp goto done;
2090 993e2a1b 2020-07-23 stsp }
2091 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2092 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2093 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2094 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2095 993e2a1b 2020-07-23 stsp if (err)
2096 993e2a1b 2020-07-23 stsp goto done;
2097 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2098 993e2a1b 2020-07-23 stsp ie->path);
2099 993e2a1b 2020-07-23 stsp goto done;
2100 993e2a1b 2020-07-23 stsp }
2101 993e2a1b 2020-07-23 stsp
2102 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2103 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2104 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2105 1ee397ad 2019-07-12 stsp if (err)
2106 5a58a424 2020-06-23 stsp goto done;
2107 fc6346c4 2019-03-27 stsp /*
2108 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2109 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2110 fc6346c4 2019-03-27 stsp */
2111 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2112 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2113 fc6346c4 2019-03-27 stsp } else {
2114 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2115 1ee397ad 2019-07-12 stsp if (err)
2116 5a58a424 2020-06-23 stsp goto done;
2117 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2118 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2119 fc6346c4 2019-03-27 stsp if (err)
2120 5a58a424 2020-06-23 stsp goto done;
2121 fc6346c4 2019-03-27 stsp }
2122 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2123 708d8e67 2019-03-27 stsp }
2124 5a58a424 2020-06-23 stsp done:
2125 5a58a424 2020-06-23 stsp free(ondisk_path);
2126 fc6346c4 2019-03-27 stsp return err;
2127 708d8e67 2019-03-27 stsp }
2128 708d8e67 2019-03-27 stsp
2129 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2130 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2131 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2132 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2133 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2134 8da9e5f4 2019-01-12 stsp void *progress_arg;
2135 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2136 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2137 8da9e5f4 2019-01-12 stsp };
2138 8da9e5f4 2019-01-12 stsp
2139 512f0d0e 2019-01-02 stsp static const struct got_error *
2140 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2141 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2142 512f0d0e 2019-01-02 stsp {
2143 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2144 0584f854 2019-04-06 stsp
2145 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2147 512f0d0e 2019-01-02 stsp
2148 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2149 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2150 8da9e5f4 2019-01-12 stsp }
2151 512f0d0e 2019-01-02 stsp
2152 8da9e5f4 2019-01-12 stsp static const struct got_error *
2153 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2154 8da9e5f4 2019-01-12 stsp {
2155 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2156 7a9df742 2019-01-08 stsp
2157 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2158 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2159 0584f854 2019-04-06 stsp
2160 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2161 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2162 9d31a1d8 2018-03-11 stsp }
2163 9d31a1d8 2018-03-11 stsp
2164 9d31a1d8 2018-03-11 stsp static const struct got_error *
2165 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2166 9d31a1d8 2018-03-11 stsp {
2167 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2168 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2169 8da9e5f4 2019-01-12 stsp char *path;
2170 9d31a1d8 2018-03-11 stsp
2171 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2172 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2173 0584f854 2019-04-06 stsp
2174 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2175 63c5ca5d 2019-08-24 stsp return NULL;
2176 63c5ca5d 2019-08-24 stsp
2177 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2178 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2179 8da9e5f4 2019-01-12 stsp == -1)
2180 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2181 9d31a1d8 2018-03-11 stsp
2182 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2183 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2184 8da9e5f4 2019-01-12 stsp else
2185 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2186 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2187 9d31a1d8 2018-03-11 stsp
2188 8da9e5f4 2019-01-12 stsp free(path);
2189 0cd1c46a 2019-03-11 stsp return err;
2190 0cd1c46a 2019-03-11 stsp }
2191 0cd1c46a 2019-03-11 stsp
2192 b2118c49 2020-07-28 stsp const struct got_error *
2193 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2194 b2118c49 2020-07-28 stsp {
2195 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2196 b2118c49 2020-07-28 stsp
2197 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2198 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2199 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2200 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2201 b2118c49 2020-07-28 stsp }
2202 b2118c49 2020-07-28 stsp
2203 b2118c49 2020-07-28 stsp return NULL;
2204 b2118c49 2020-07-28 stsp }
2205 b2118c49 2020-07-28 stsp
2206 818c7501 2019-07-11 stsp static const struct got_error *
2207 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2208 0cd1c46a 2019-03-11 stsp {
2209 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2210 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2211 0cd1c46a 2019-03-11 stsp
2212 517bab73 2019-03-11 stsp *refname = NULL;
2213 517bab73 2019-03-11 stsp
2214 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2215 b2118c49 2020-07-28 stsp if (err)
2216 b2118c49 2020-07-28 stsp return err;
2217 0cd1c46a 2019-03-11 stsp
2218 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2219 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2220 0647c563 2019-03-11 stsp *refname = NULL;
2221 0cd1c46a 2019-03-11 stsp }
2222 517bab73 2019-03-11 stsp free(uuidstr);
2223 517bab73 2019-03-11 stsp return err;
2224 517bab73 2019-03-11 stsp }
2225 517bab73 2019-03-11 stsp
2226 818c7501 2019-07-11 stsp const struct got_error *
2227 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2228 818c7501 2019-07-11 stsp {
2229 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2230 818c7501 2019-07-11 stsp }
2231 818c7501 2019-07-11 stsp
2232 818c7501 2019-07-11 stsp static const struct got_error *
2233 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 818c7501 2019-07-11 stsp {
2235 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2236 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2237 818c7501 2019-07-11 stsp }
2238 818c7501 2019-07-11 stsp
2239 818c7501 2019-07-11 stsp static const struct got_error *
2240 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2241 818c7501 2019-07-11 stsp {
2242 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2243 818c7501 2019-07-11 stsp }
2244 818c7501 2019-07-11 stsp
2245 818c7501 2019-07-11 stsp static const struct got_error *
2246 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2247 818c7501 2019-07-11 stsp {
2248 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2249 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2250 818c7501 2019-07-11 stsp }
2251 818c7501 2019-07-11 stsp
2252 818c7501 2019-07-11 stsp static const struct got_error *
2253 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2254 818c7501 2019-07-11 stsp {
2255 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2256 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2257 0ebf8283 2019-07-24 stsp }
2258 0ebf8283 2019-07-24 stsp
2259 0ebf8283 2019-07-24 stsp static const struct got_error *
2260 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 0ebf8283 2019-07-24 stsp {
2262 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2263 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2264 0ebf8283 2019-07-24 stsp }
2265 0ebf8283 2019-07-24 stsp
2266 0ebf8283 2019-07-24 stsp static const struct got_error *
2267 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2268 0ebf8283 2019-07-24 stsp {
2269 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2270 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2271 0ebf8283 2019-07-24 stsp }
2272 0ebf8283 2019-07-24 stsp
2273 0ebf8283 2019-07-24 stsp static const struct got_error *
2274 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2275 0ebf8283 2019-07-24 stsp {
2276 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2277 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2278 818c7501 2019-07-11 stsp }
2279 818c7501 2019-07-11 stsp
2280 0ebf8283 2019-07-24 stsp static const struct got_error *
2281 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 0ebf8283 2019-07-24 stsp {
2283 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2284 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2285 0ebf8283 2019-07-24 stsp }
2286 818c7501 2019-07-11 stsp
2287 0ebf8283 2019-07-24 stsp const struct got_error *
2288 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2289 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2290 0ebf8283 2019-07-24 stsp {
2291 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2292 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2293 0ebf8283 2019-07-24 stsp *path = NULL;
2294 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2295 0ebf8283 2019-07-24 stsp }
2296 0ebf8283 2019-07-24 stsp return NULL;
2297 10604dce 2021-09-24 thomas }
2298 10604dce 2021-09-24 thomas
2299 10604dce 2021-09-24 thomas static const struct got_error *
2300 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2301 10604dce 2021-09-24 thomas {
2302 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2303 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2304 0ebf8283 2019-07-24 stsp }
2305 0ebf8283 2019-07-24 stsp
2306 10604dce 2021-09-24 thomas static const struct got_error *
2307 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2308 10604dce 2021-09-24 thomas {
2309 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2310 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2311 10604dce 2021-09-24 thomas }
2312 10604dce 2021-09-24 thomas
2313 517bab73 2019-03-11 stsp /*
2314 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2315 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2316 517bab73 2019-03-11 stsp */
2317 517bab73 2019-03-11 stsp static const struct got_error *
2318 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2319 517bab73 2019-03-11 stsp {
2320 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2321 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2322 517bab73 2019-03-11 stsp char *refname;
2323 517bab73 2019-03-11 stsp
2324 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2325 517bab73 2019-03-11 stsp if (err)
2326 517bab73 2019-03-11 stsp return err;
2327 0cd1c46a 2019-03-11 stsp
2328 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2329 0cd1c46a 2019-03-11 stsp if (err)
2330 0cd1c46a 2019-03-11 stsp goto done;
2331 0cd1c46a 2019-03-11 stsp
2332 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2333 0cd1c46a 2019-03-11 stsp done:
2334 0cd1c46a 2019-03-11 stsp free(refname);
2335 0cd1c46a 2019-03-11 stsp if (ref)
2336 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2337 3e3a69f1 2019-07-25 stsp return err;
2338 3e3a69f1 2019-07-25 stsp }
2339 3e3a69f1 2019-07-25 stsp
2340 3e3a69f1 2019-07-25 stsp static const struct got_error *
2341 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2342 3e3a69f1 2019-07-25 stsp {
2343 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2344 3e3a69f1 2019-07-25 stsp
2345 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2346 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2347 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2348 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2349 3e3a69f1 2019-07-25 stsp }
2350 9d31a1d8 2018-03-11 stsp return err;
2351 9d31a1d8 2018-03-11 stsp }
2352 9d31a1d8 2018-03-11 stsp
2353 3e3a69f1 2019-07-25 stsp
2354 ebf99748 2019-05-09 stsp static const struct got_error *
2355 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2356 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2357 ebf99748 2019-05-09 stsp {
2358 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2359 ebf99748 2019-05-09 stsp FILE *index = NULL;
2360 0cd1c46a 2019-03-11 stsp
2361 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2362 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2363 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2364 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2365 ebf99748 2019-05-09 stsp
2366 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2367 3e3a69f1 2019-07-25 stsp if (err)
2368 ebf99748 2019-05-09 stsp goto done;
2369 ebf99748 2019-05-09 stsp
2370 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2371 ebf99748 2019-05-09 stsp if (index == NULL) {
2372 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2373 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2374 ebf99748 2019-05-09 stsp } else {
2375 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2376 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2377 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2378 ebf99748 2019-05-09 stsp }
2379 ebf99748 2019-05-09 stsp done:
2380 ebf99748 2019-05-09 stsp if (err) {
2381 ebf99748 2019-05-09 stsp free(*fileindex_path);
2382 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2383 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2384 ebf99748 2019-05-09 stsp *fileindex = NULL;
2385 ebf99748 2019-05-09 stsp }
2386 ebf99748 2019-05-09 stsp return err;
2387 ebf99748 2019-05-09 stsp }
2388 c932eeeb 2019-05-22 stsp
2389 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2390 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2391 c932eeeb 2019-05-22 stsp const char *path;
2392 c932eeeb 2019-05-22 stsp size_t path_len;
2393 c932eeeb 2019-05-22 stsp const char *entry_name;
2394 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2395 a484d721 2019-06-10 stsp void *progress_arg;
2396 c932eeeb 2019-05-22 stsp };
2397 ebf99748 2019-05-09 stsp
2398 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2399 c932eeeb 2019-05-22 stsp static const struct got_error *
2400 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2401 c932eeeb 2019-05-22 stsp {
2402 1ee397ad 2019-07-12 stsp const struct got_error *err;
2403 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2404 c932eeeb 2019-05-22 stsp
2405 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2406 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2407 c932eeeb 2019-05-22 stsp return NULL;
2408 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2409 102ff934 2019-06-11 stsp return NULL;
2410 102ff934 2019-06-11 stsp
2411 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2412 a769b60b 2021-06-27 stsp return NULL;
2413 a769b60b 2021-06-27 stsp
2414 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2415 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2416 c932eeeb 2019-05-22 stsp return NULL;
2417 c932eeeb 2019-05-22 stsp
2418 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2419 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2420 edd02c5e 2019-07-11 stsp ie->path);
2421 1ee397ad 2019-07-12 stsp if (err)
2422 1ee397ad 2019-07-12 stsp return err;
2423 1ee397ad 2019-07-12 stsp }
2424 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2425 c932eeeb 2019-05-22 stsp return NULL;
2426 a615e0e7 2020-12-16 stsp }
2427 a615e0e7 2020-12-16 stsp
2428 a615e0e7 2020-12-16 stsp static const struct got_error *
2429 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2430 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2431 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2432 a615e0e7 2020-12-16 stsp {
2433 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2434 a615e0e7 2020-12-16 stsp
2435 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2436 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2437 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2438 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2439 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2440 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2441 a615e0e7 2020-12-16 stsp
2442 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2443 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2444 9c6338c4 2019-06-02 stsp }
2445 9c6338c4 2019-06-02 stsp
2446 9c6338c4 2019-06-02 stsp static const struct got_error *
2447 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2448 9c6338c4 2019-06-02 stsp {
2449 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2450 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2451 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2452 867630bb 2020-01-17 stsp struct timespec timeout;
2453 9c6338c4 2019-06-02 stsp
2454 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2455 9c6338c4 2019-06-02 stsp fileindex_path);
2456 9c6338c4 2019-06-02 stsp if (err)
2457 9c6338c4 2019-06-02 stsp goto done;
2458 9c6338c4 2019-06-02 stsp
2459 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2460 9c6338c4 2019-06-02 stsp if (err)
2461 9c6338c4 2019-06-02 stsp goto done;
2462 9c6338c4 2019-06-02 stsp
2463 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2464 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2465 9c6338c4 2019-06-02 stsp fileindex_path);
2466 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2467 9c6338c4 2019-06-02 stsp }
2468 867630bb 2020-01-17 stsp
2469 867630bb 2020-01-17 stsp /*
2470 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2471 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2472 867630bb 2020-01-17 stsp * was recorded in the file index.
2473 867630bb 2020-01-17 stsp */
2474 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2475 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2476 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2477 9c6338c4 2019-06-02 stsp done:
2478 9c6338c4 2019-06-02 stsp if (new_index)
2479 9c6338c4 2019-06-02 stsp fclose(new_index);
2480 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2481 9c6338c4 2019-06-02 stsp return err;
2482 c932eeeb 2019-05-22 stsp }
2483 6ced4176 2019-07-12 stsp
2484 6ced4176 2019-07-12 stsp static const struct got_error *
2485 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2486 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2487 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2488 945f9229 2022-04-16 thomas struct got_repository *repo)
2489 6ced4176 2019-07-12 stsp {
2490 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2491 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2492 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2493 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2494 6ced4176 2019-07-12 stsp
2495 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2496 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2497 6ced4176 2019-07-12 stsp *tree_id = NULL;
2498 6ced4176 2019-07-12 stsp
2499 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2500 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2501 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2502 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2503 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2504 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2505 6ced4176 2019-07-12 stsp goto done;
2506 6ced4176 2019-07-12 stsp }
2507 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2508 945f9229 2022-04-16 thomas worktree->path_prefix);
2509 6ced4176 2019-07-12 stsp if (err)
2510 6ced4176 2019-07-12 stsp goto done;
2511 6ced4176 2019-07-12 stsp return NULL;
2512 6ced4176 2019-07-12 stsp }
2513 6ced4176 2019-07-12 stsp
2514 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2515 6ced4176 2019-07-12 stsp
2516 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2517 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2518 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2519 6ced4176 2019-07-12 stsp goto done;
2520 6ced4176 2019-07-12 stsp }
2521 6ced4176 2019-07-12 stsp
2522 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2523 6ced4176 2019-07-12 stsp if (err)
2524 6ced4176 2019-07-12 stsp goto done;
2525 6ced4176 2019-07-12 stsp
2526 6ced4176 2019-07-12 stsp free(in_repo_path);
2527 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2528 6ced4176 2019-07-12 stsp
2529 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2530 6ced4176 2019-07-12 stsp if (err)
2531 6ced4176 2019-07-12 stsp goto done;
2532 c932eeeb 2019-05-22 stsp
2533 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2534 6ced4176 2019-07-12 stsp /* Check out a single file. */
2535 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2536 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2537 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2538 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2539 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2540 6ced4176 2019-07-12 stsp goto done;
2541 6ced4176 2019-07-12 stsp }
2542 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2543 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2544 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2545 6ced4176 2019-07-12 stsp goto done;
2546 6ced4176 2019-07-12 stsp }
2547 6ced4176 2019-07-12 stsp } else {
2548 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2549 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2550 6ced4176 2019-07-12 stsp if (err)
2551 6ced4176 2019-07-12 stsp return err;
2552 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2553 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2554 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2555 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2556 6ced4176 2019-07-12 stsp goto done;
2557 6ced4176 2019-07-12 stsp }
2558 6ced4176 2019-07-12 stsp }
2559 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2560 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2561 6ced4176 2019-07-12 stsp } else {
2562 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2563 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2564 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2565 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2566 6ced4176 2019-07-12 stsp goto done;
2567 6ced4176 2019-07-12 stsp }
2568 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2569 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2570 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2571 6ced4176 2019-07-12 stsp goto done;
2572 6ced4176 2019-07-12 stsp }
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp done:
2575 6ced4176 2019-07-12 stsp free(id);
2576 6ced4176 2019-07-12 stsp free(in_repo_path);
2577 6ced4176 2019-07-12 stsp if (err) {
2578 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2579 6ced4176 2019-07-12 stsp free(*tree_relpath);
2580 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2581 6ced4176 2019-07-12 stsp free(*tree_id);
2582 6ced4176 2019-07-12 stsp *tree_id = NULL;
2583 6ced4176 2019-07-12 stsp }
2584 07ed472d 2019-07-12 stsp return err;
2585 07ed472d 2019-07-12 stsp }
2586 07ed472d 2019-07-12 stsp
2587 07ed472d 2019-07-12 stsp static const struct got_error *
2588 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2589 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2590 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2591 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2592 07ed472d 2019-07-12 stsp {
2593 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2594 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2595 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2596 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2597 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2598 07ed472d 2019-07-12 stsp
2599 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2600 7f47418f 2019-12-20 stsp if (err) {
2601 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2602 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2603 7f47418f 2019-12-20 stsp goto done;
2604 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2605 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2606 7f47418f 2019-12-20 stsp if (err)
2607 7f47418f 2019-12-20 stsp return err;
2608 7f47418f 2019-12-20 stsp }
2609 07ed472d 2019-07-12 stsp
2610 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2611 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2612 07ed472d 2019-07-12 stsp if (err)
2613 07ed472d 2019-07-12 stsp goto done;
2614 07ed472d 2019-07-12 stsp
2615 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2616 07ed472d 2019-07-12 stsp if (err)
2617 07ed472d 2019-07-12 stsp goto done;
2618 07ed472d 2019-07-12 stsp
2619 07ed472d 2019-07-12 stsp if (entry_name &&
2620 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2621 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2622 07ed472d 2019-07-12 stsp goto done;
2623 07ed472d 2019-07-12 stsp }
2624 07ed472d 2019-07-12 stsp
2625 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2626 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2627 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2628 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2629 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2630 07ed472d 2019-07-12 stsp arg.repo = repo;
2631 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2632 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2633 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2634 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2635 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2636 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2637 07ed472d 2019-07-12 stsp done:
2638 07ed472d 2019-07-12 stsp if (tree)
2639 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2640 07ed472d 2019-07-12 stsp if (commit)
2641 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2642 6ced4176 2019-07-12 stsp return err;
2643 6ced4176 2019-07-12 stsp }
2644 6ced4176 2019-07-12 stsp
2645 9d31a1d8 2018-03-11 stsp const struct got_error *
2646 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2647 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2648 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2649 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2650 9d31a1d8 2018-03-11 stsp {
2651 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2652 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2653 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2654 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2655 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2656 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2657 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2658 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2659 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2660 f2ea84fa 2019-07-27 stsp int entry_type;
2661 f2ea84fa 2019-07-27 stsp char *relpath;
2662 f2ea84fa 2019-07-27 stsp char *entry_name;
2663 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2664 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2665 9d31a1d8 2018-03-11 stsp
2666 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2667 f2ea84fa 2019-07-27 stsp
2668 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2669 9d31a1d8 2018-03-11 stsp if (err)
2670 9d31a1d8 2018-03-11 stsp return err;
2671 945f9229 2022-04-16 thomas
2672 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2673 945f9229 2022-04-16 thomas worktree->base_commit_id);
2674 945f9229 2022-04-16 thomas if (err)
2675 945f9229 2022-04-16 thomas goto done;
2676 93a30277 2018-12-24 stsp
2677 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2678 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2679 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2680 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2681 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2682 f2ea84fa 2019-07-27 stsp goto done;
2683 f2ea84fa 2019-07-27 stsp }
2684 6ced4176 2019-07-12 stsp
2685 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2686 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2687 945f9229 2022-04-16 thomas worktree, repo);
2688 f2ea84fa 2019-07-27 stsp if (err) {
2689 f2ea84fa 2019-07-27 stsp free(tpd);
2690 6ced4176 2019-07-12 stsp goto done;
2691 6ced4176 2019-07-12 stsp }
2692 f2ea84fa 2019-07-27 stsp
2693 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2694 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2695 f2ea84fa 2019-07-27 stsp if (err) {
2696 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2697 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2698 f2ea84fa 2019-07-27 stsp free(tpd);
2699 f2ea84fa 2019-07-27 stsp goto done;
2700 f2ea84fa 2019-07-27 stsp }
2701 f2ea84fa 2019-07-27 stsp } else
2702 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2703 f2ea84fa 2019-07-27 stsp
2704 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2705 6ced4176 2019-07-12 stsp }
2706 6ced4176 2019-07-12 stsp
2707 51514078 2018-12-25 stsp /*
2708 51514078 2018-12-25 stsp * Read the file index.
2709 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2710 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2711 51514078 2018-12-25 stsp */
2712 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2713 8da9e5f4 2019-01-12 stsp if (err)
2714 c4cdcb68 2019-04-03 stsp goto done;
2715 c4cdcb68 2019-04-03 stsp
2716 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2717 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2718 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2719 f2ea84fa 2019-07-27 stsp
2720 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2721 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2722 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2723 f2ea84fa 2019-07-27 stsp if (err)
2724 f2ea84fa 2019-07-27 stsp break;
2725 f2ea84fa 2019-07-27 stsp
2726 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2727 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2728 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2729 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2730 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2731 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2732 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2733 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2734 f2ea84fa 2019-07-27 stsp if (err)
2735 f2ea84fa 2019-07-27 stsp break;
2736 f2ea84fa 2019-07-27 stsp
2737 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2738 711d9cd9 2019-07-12 stsp }
2739 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2740 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2741 af12c6b9 2019-06-04 stsp err = sync_err;
2742 9d31a1d8 2018-03-11 stsp done:
2743 9c6338c4 2019-06-02 stsp free(fileindex_path);
2744 edfa7d7f 2018-09-11 stsp if (tree)
2745 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2746 9d31a1d8 2018-03-11 stsp if (commit)
2747 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2748 5ade8233 2019-07-12 stsp if (fileindex)
2749 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2750 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2751 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2752 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2753 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2754 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2755 f2ea84fa 2019-07-27 stsp free(tpd);
2756 f2ea84fa 2019-07-27 stsp }
2757 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2758 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2759 9d31a1d8 2018-03-11 stsp err = unlockerr;
2760 f8d1f275 2019-02-04 stsp return err;
2761 f8d1f275 2019-02-04 stsp }
2762 f8d1f275 2019-02-04 stsp
2763 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2764 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2765 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2766 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2767 234035bc 2019-06-01 stsp void *progress_arg;
2768 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2769 234035bc 2019-06-01 stsp void *cancel_arg;
2770 f69721c3 2019-10-21 stsp const char *label_orig;
2771 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2772 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2773 234035bc 2019-06-01 stsp };
2774 234035bc 2019-06-01 stsp
2775 234035bc 2019-06-01 stsp static const struct got_error *
2776 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2777 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2778 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2779 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2780 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2781 234035bc 2019-06-01 stsp {
2782 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2783 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2784 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2785 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2786 234035bc 2019-06-01 stsp struct stat sb;
2787 234035bc 2019-06-01 stsp unsigned char status;
2788 234035bc 2019-06-01 stsp int local_changes_subsumed;
2789 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2790 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2791 234035bc 2019-06-01 stsp
2792 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2793 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2794 d6c87207 2019-08-02 stsp strlen(path2));
2795 1ee397ad 2019-07-12 stsp if (ie == NULL)
2796 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2797 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2798 234035bc 2019-06-01 stsp
2799 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2800 234035bc 2019-06-01 stsp path2) == -1)
2801 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2802 234035bc 2019-06-01 stsp
2803 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2804 7f91a133 2019-12-13 stsp repo);
2805 234035bc 2019-06-01 stsp if (err)
2806 234035bc 2019-06-01 stsp goto done;
2807 234035bc 2019-06-01 stsp
2808 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2809 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2810 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2811 234035bc 2019-06-01 stsp goto done;
2812 234035bc 2019-06-01 stsp }
2813 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2814 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2815 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2816 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2817 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2818 234035bc 2019-06-01 stsp goto done;
2819 234035bc 2019-06-01 stsp }
2820 234035bc 2019-06-01 stsp
2821 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2822 36bf999c 2020-07-23 stsp char *link_target2;
2823 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2824 36bf999c 2020-07-23 stsp if (err)
2825 36bf999c 2020-07-23 stsp goto done;
2826 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2827 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2828 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2829 36bf999c 2020-07-23 stsp free(link_target2);
2830 af57b12a 2020-07-23 stsp } else {
2831 1af628f4 2021-06-03 stsp int fd;
2832 1af628f4 2021-06-03 stsp
2833 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2834 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2835 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2836 1af628f4 2021-06-03 stsp goto done;
2837 1af628f4 2021-06-03 stsp }
2838 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2839 1af628f4 2021-06-03 stsp f_orig, blob1);
2840 1af628f4 2021-06-03 stsp if (err)
2841 1af628f4 2021-06-03 stsp goto done;
2842 1af628f4 2021-06-03 stsp
2843 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2844 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2845 1af628f4 2021-06-03 stsp goto done;
2846 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2847 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2848 1af628f4 2021-06-03 stsp if (err)
2849 1af628f4 2021-06-03 stsp goto done;
2850 1af628f4 2021-06-03 stsp
2851 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2852 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2853 1af628f4 2021-06-03 stsp if (fd == -1) {
2854 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2855 1af628f4 2021-06-03 stsp ondisk_path);
2856 1af628f4 2021-06-03 stsp goto done;
2857 1af628f4 2021-06-03 stsp }
2858 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2859 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2860 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2861 1af628f4 2021-06-03 stsp ondisk_path);
2862 1af628f4 2021-06-03 stsp close(fd);
2863 1af628f4 2021-06-03 stsp goto done;
2864 1af628f4 2021-06-03 stsp }
2865 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2866 1af628f4 2021-06-03 stsp if (err)
2867 1af628f4 2021-06-03 stsp goto done;
2868 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2869 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2870 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2871 1af628f4 2021-06-03 stsp goto done;
2872 1af628f4 2021-06-03 stsp }
2873 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2874 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2875 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2876 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2877 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2878 af57b12a 2020-07-23 stsp }
2879 234035bc 2019-06-01 stsp } else if (blob1) {
2880 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2881 d6c87207 2019-08-02 stsp strlen(path1));
2882 1ee397ad 2019-07-12 stsp if (ie == NULL)
2883 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2884 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2885 2b92fad7 2019-06-02 stsp
2886 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2887 2b92fad7 2019-06-02 stsp path1) == -1)
2888 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2889 2b92fad7 2019-06-02 stsp
2890 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2891 7f91a133 2019-12-13 stsp repo);
2892 2b92fad7 2019-06-02 stsp if (err)
2893 2b92fad7 2019-06-02 stsp goto done;
2894 2b92fad7 2019-06-02 stsp
2895 2b92fad7 2019-06-02 stsp switch (status) {
2896 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2897 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2898 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2899 1ee397ad 2019-07-12 stsp if (err)
2900 1ee397ad 2019-07-12 stsp goto done;
2901 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2902 2b92fad7 2019-06-02 stsp if (err)
2903 2b92fad7 2019-06-02 stsp goto done;
2904 2b92fad7 2019-06-02 stsp if (ie)
2905 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2906 2b92fad7 2019-06-02 stsp break;
2907 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2908 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2909 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2910 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2911 1ee397ad 2019-07-12 stsp if (err)
2912 1ee397ad 2019-07-12 stsp goto done;
2913 2b92fad7 2019-06-02 stsp if (ie)
2914 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2915 2b92fad7 2019-06-02 stsp break;
2916 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2917 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2918 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2919 e8f02263 2022-01-23 thomas off_t blob1_size;
2920 0a22ca1a 2020-09-23 stsp /*
2921 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2922 0a22ca1a 2020-09-23 stsp * exists in the repository.
2923 0a22ca1a 2020-09-23 stsp */
2924 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2925 e8f02263 2022-01-23 thomas &blob1_size, path1);
2926 0a22ca1a 2020-09-23 stsp if (err)
2927 0a22ca1a 2020-09-23 stsp goto done;
2928 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2929 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2930 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2931 0a22ca1a 2020-09-23 stsp if (err)
2932 0a22ca1a 2020-09-23 stsp goto done;
2933 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2934 0a22ca1a 2020-09-23 stsp path1);
2935 0a22ca1a 2020-09-23 stsp if (err)
2936 0a22ca1a 2020-09-23 stsp goto done;
2937 0a22ca1a 2020-09-23 stsp if (ie)
2938 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2939 0a22ca1a 2020-09-23 stsp ie);
2940 0a22ca1a 2020-09-23 stsp } else {
2941 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2942 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2943 0a22ca1a 2020-09-23 stsp }
2944 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2945 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2946 0a22ca1a 2020-09-23 stsp free(id);
2947 0a22ca1a 2020-09-23 stsp if (err)
2948 0a22ca1a 2020-09-23 stsp goto done;
2949 0a22ca1a 2020-09-23 stsp break;
2950 0a22ca1a 2020-09-23 stsp }
2951 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2952 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2953 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2954 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2955 1ee397ad 2019-07-12 stsp if (err)
2956 1ee397ad 2019-07-12 stsp goto done;
2957 2b92fad7 2019-06-02 stsp break;
2958 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2959 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2960 1ee397ad 2019-07-12 stsp if (err)
2961 1ee397ad 2019-07-12 stsp goto done;
2962 2b92fad7 2019-06-02 stsp break;
2963 2b92fad7 2019-06-02 stsp default:
2964 2b92fad7 2019-06-02 stsp break;
2965 2b92fad7 2019-06-02 stsp }
2966 234035bc 2019-06-01 stsp } else if (blob2) {
2967 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2968 234035bc 2019-06-01 stsp path2) == -1)
2969 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2970 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2971 d6c87207 2019-08-02 stsp strlen(path2));
2972 234035bc 2019-06-01 stsp if (ie) {
2973 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2974 7f91a133 2019-12-13 stsp -1, NULL, repo);
2975 234035bc 2019-06-01 stsp if (err)
2976 234035bc 2019-06-01 stsp goto done;
2977 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2978 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2979 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2980 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2981 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2982 1ee397ad 2019-07-12 stsp status, path2);
2983 234035bc 2019-06-01 stsp goto done;
2984 234035bc 2019-06-01 stsp }
2985 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2986 36bf999c 2020-07-23 stsp char *link_target2;
2987 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2988 36bf999c 2020-07-23 stsp blob2);
2989 36bf999c 2020-07-23 stsp if (err)
2990 36bf999c 2020-07-23 stsp goto done;
2991 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2992 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2993 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2994 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2995 36bf999c 2020-07-23 stsp free(link_target2);
2996 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2997 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2998 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2999 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3000 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3001 526a746f 2020-07-23 stsp a->progress_arg);
3002 dfe9fba0 2020-07-23 stsp } else {
3003 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3004 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3005 526a746f 2020-07-23 stsp }
3006 dfe9fba0 2020-07-23 stsp if (err)
3007 dfe9fba0 2020-07-23 stsp goto done;
3008 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3009 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3010 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3011 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3012 234035bc 2019-06-01 stsp if (err)
3013 234035bc 2019-06-01 stsp goto done;
3014 234035bc 2019-06-01 stsp }
3015 234035bc 2019-06-01 stsp } else {
3016 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3017 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3018 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3019 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3020 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3021 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3022 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3023 2e1fa222 2020-07-23 stsp } else {
3024 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3025 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3026 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3027 2e1fa222 2020-07-23 stsp }
3028 234035bc 2019-06-01 stsp if (err)
3029 234035bc 2019-06-01 stsp goto done;
3030 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3031 234035bc 2019-06-01 stsp if (err)
3032 234035bc 2019-06-01 stsp goto done;
3033 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3034 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3035 3969253a 2020-03-07 stsp if (err) {
3036 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3037 3969253a 2020-03-07 stsp goto done;
3038 3969253a 2020-03-07 stsp }
3039 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3040 2b92fad7 2019-06-02 stsp if (err) {
3041 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3042 2b92fad7 2019-06-02 stsp goto done;
3043 2b92fad7 2019-06-02 stsp }
3044 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3045 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3046 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3047 2e1fa222 2020-07-23 stsp }
3048 234035bc 2019-06-01 stsp }
3049 234035bc 2019-06-01 stsp }
3050 234035bc 2019-06-01 stsp done:
3051 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3052 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3053 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3054 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3055 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3056 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3057 1af628f4 2021-06-03 stsp free(id_str);
3058 54d5be07 2021-06-03 stsp free(label_deriv2);
3059 234035bc 2019-06-01 stsp free(ondisk_path);
3060 234035bc 2019-06-01 stsp return err;
3061 234035bc 2019-06-01 stsp }
3062 234035bc 2019-06-01 stsp
3063 69de9dd4 2021-09-03 stsp static const struct got_error *
3064 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3065 69de9dd4 2021-09-03 stsp {
3066 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3067 69de9dd4 2021-09-03 stsp
3068 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3069 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3070 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3071 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3072 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3073 69de9dd4 2021-09-03 stsp
3074 69de9dd4 2021-09-03 stsp return NULL;
3075 69de9dd4 2021-09-03 stsp }
3076 69de9dd4 2021-09-03 stsp
3077 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3078 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3079 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3080 234035bc 2019-06-01 stsp struct got_repository *repo;
3081 234035bc 2019-06-01 stsp };
3082 234035bc 2019-06-01 stsp
3083 234035bc 2019-06-01 stsp static const struct got_error *
3084 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3085 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3086 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3087 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3088 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3089 234035bc 2019-06-01 stsp {
3090 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3091 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3092 234035bc 2019-06-01 stsp unsigned char status;
3093 234035bc 2019-06-01 stsp struct stat sb;
3094 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3095 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3096 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3097 234035bc 2019-06-01 stsp char *ondisk_path;
3098 234035bc 2019-06-01 stsp
3099 69de9dd4 2021-09-03 stsp if (id == NULL)
3100 69de9dd4 2021-09-03 stsp return NULL;
3101 234035bc 2019-06-01 stsp
3102 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3103 69de9dd4 2021-09-03 stsp if (ie == NULL)
3104 69de9dd4 2021-09-03 stsp return NULL;
3105 69de9dd4 2021-09-03 stsp
3106 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3107 234035bc 2019-06-01 stsp == -1)
3108 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3109 234035bc 2019-06-01 stsp
3110 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3111 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3112 5546d466 2021-09-02 stsp free(ondisk_path);
3113 234035bc 2019-06-01 stsp if (err)
3114 234035bc 2019-06-01 stsp return err;
3115 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3116 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3117 234035bc 2019-06-01 stsp
3118 234035bc 2019-06-01 stsp return NULL;
3119 234035bc 2019-06-01 stsp }
3120 234035bc 2019-06-01 stsp
3121 818c7501 2019-07-11 stsp static const struct got_error *
3122 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3123 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3124 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3125 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3126 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3127 234035bc 2019-06-01 stsp {
3128 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3129 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3130 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3131 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3132 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3133 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3134 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3135 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3136 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3137 234035bc 2019-06-01 stsp
3138 03415a1a 2019-06-02 stsp if (commit_id1) {
3139 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3140 945f9229 2022-04-16 thomas if (err)
3141 945f9229 2022-04-16 thomas goto done;
3142 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3143 03415a1a 2019-06-02 stsp worktree->path_prefix);
3144 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3145 03415a1a 2019-06-02 stsp goto done;
3146 69d57f3d 2020-07-31 stsp }
3147 69d57f3d 2020-07-31 stsp if (tree_id1) {
3148 69d57f3d 2020-07-31 stsp char *id_str;
3149 03415a1a 2019-06-02 stsp
3150 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3151 03415a1a 2019-06-02 stsp if (err)
3152 03415a1a 2019-06-02 stsp goto done;
3153 f69721c3 2019-10-21 stsp
3154 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3155 f69721c3 2019-10-21 stsp if (err)
3156 f69721c3 2019-10-21 stsp goto done;
3157 f69721c3 2019-10-21 stsp
3158 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3159 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3160 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3161 f69721c3 2019-10-21 stsp free(id_str);
3162 f69721c3 2019-10-21 stsp goto done;
3163 f69721c3 2019-10-21 stsp }
3164 f69721c3 2019-10-21 stsp free(id_str);
3165 a0f32f33 2022-06-13 thomas
3166 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3167 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3168 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3169 a0f32f33 2022-06-13 thomas goto done;
3170 a0f32f33 2022-06-13 thomas }
3171 03415a1a 2019-06-02 stsp }
3172 234035bc 2019-06-01 stsp
3173 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3174 945f9229 2022-04-16 thomas if (err)
3175 945f9229 2022-04-16 thomas goto done;
3176 945f9229 2022-04-16 thomas
3177 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3178 234035bc 2019-06-01 stsp worktree->path_prefix);
3179 234035bc 2019-06-01 stsp if (err)
3180 234035bc 2019-06-01 stsp goto done;
3181 234035bc 2019-06-01 stsp
3182 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3183 234035bc 2019-06-01 stsp if (err)
3184 234035bc 2019-06-01 stsp goto done;
3185 234035bc 2019-06-01 stsp
3186 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3187 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3188 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3189 19a6a6b5 2022-07-01 thomas goto done;
3190 19a6a6b5 2022-07-01 thomas }
3191 19a6a6b5 2022-07-01 thomas
3192 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3193 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3194 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3195 a0f32f33 2022-06-13 thomas goto done;
3196 a0f32f33 2022-06-13 thomas }
3197 a0f32f33 2022-06-13 thomas
3198 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3199 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3200 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3201 19a6a6b5 2022-07-01 thomas goto done;
3202 19a6a6b5 2022-07-01 thomas }
3203 19a6a6b5 2022-07-01 thomas
3204 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3205 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3206 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3207 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3208 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3209 69de9dd4 2021-09-03 stsp if (err)
3210 69de9dd4 2021-09-03 stsp goto done;
3211 69de9dd4 2021-09-03 stsp
3212 234035bc 2019-06-01 stsp arg.worktree = worktree;
3213 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3214 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3215 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3216 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3217 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3218 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3219 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3220 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3221 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3222 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3223 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3224 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3225 af12c6b9 2019-06-04 stsp err = sync_err;
3226 234035bc 2019-06-01 stsp done:
3227 945f9229 2022-04-16 thomas if (commit1)
3228 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3229 945f9229 2022-04-16 thomas if (commit2)
3230 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3231 234035bc 2019-06-01 stsp if (tree1)
3232 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3233 234035bc 2019-06-01 stsp if (tree2)
3234 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3235 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3236 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3237 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3238 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3239 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3240 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3241 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3242 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3243 f69721c3 2019-10-21 stsp free(label_orig);
3244 818c7501 2019-07-11 stsp return err;
3245 818c7501 2019-07-11 stsp }
3246 234035bc 2019-06-01 stsp
3247 818c7501 2019-07-11 stsp const struct got_error *
3248 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3249 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3250 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3251 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3252 818c7501 2019-07-11 stsp {
3253 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3254 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3255 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3256 818c7501 2019-07-11 stsp
3257 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3258 818c7501 2019-07-11 stsp if (err)
3259 818c7501 2019-07-11 stsp return err;
3260 818c7501 2019-07-11 stsp
3261 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3262 818c7501 2019-07-11 stsp if (err)
3263 818c7501 2019-07-11 stsp goto done;
3264 818c7501 2019-07-11 stsp
3265 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3266 69de9dd4 2021-09-03 stsp worktree);
3267 818c7501 2019-07-11 stsp if (err)
3268 818c7501 2019-07-11 stsp goto done;
3269 818c7501 2019-07-11 stsp
3270 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3271 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3272 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3273 818c7501 2019-07-11 stsp done:
3274 818c7501 2019-07-11 stsp if (fileindex)
3275 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3276 818c7501 2019-07-11 stsp free(fileindex_path);
3277 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3278 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3279 234035bc 2019-06-01 stsp err = unlockerr;
3280 234035bc 2019-06-01 stsp return err;
3281 234035bc 2019-06-01 stsp }
3282 234035bc 2019-06-01 stsp
3283 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3284 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3285 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3286 927df6b7 2019-02-10 stsp const char *status_path;
3287 927df6b7 2019-02-10 stsp size_t status_path_len;
3288 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3289 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3290 f8d1f275 2019-02-04 stsp void *status_arg;
3291 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3292 f8d1f275 2019-02-04 stsp void *cancel_arg;
3293 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3294 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3295 f2a9dc41 2019-12-13 tracey int report_unchanged;
3296 3143d852 2020-06-25 stsp int no_ignores;
3297 f8d1f275 2019-02-04 stsp };
3298 88d0e355 2019-08-03 stsp
3299 f8d1f275 2019-02-04 stsp static const struct got_error *
3300 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3301 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3302 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3303 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3304 927df6b7 2019-02-10 stsp {
3305 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3306 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3307 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3308 927df6b7 2019-02-10 stsp struct stat sb;
3309 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3310 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3311 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3312 927df6b7 2019-02-10 stsp
3313 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3314 98eaaa12 2019-08-03 stsp if (err)
3315 98eaaa12 2019-08-03 stsp return err;
3316 98eaaa12 2019-08-03 stsp
3317 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3318 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3319 98eaaa12 2019-08-03 stsp return NULL;
3320 98eaaa12 2019-08-03 stsp
3321 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3322 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3323 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3324 98eaaa12 2019-08-03 stsp }
3325 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3326 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3327 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3328 927df6b7 2019-02-10 stsp }
3329 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3330 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3331 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3332 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3333 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3334 98eaaa12 2019-08-03 stsp }
3335 98eaaa12 2019-08-03 stsp
3336 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3337 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3338 927df6b7 2019-02-10 stsp }
3339 927df6b7 2019-02-10 stsp
3340 927df6b7 2019-02-10 stsp static const struct got_error *
3341 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3342 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3343 f8d1f275 2019-02-04 stsp {
3344 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3345 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3346 f8d1f275 2019-02-04 stsp char *abspath;
3347 f8d1f275 2019-02-04 stsp
3348 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3349 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3350 0584f854 2019-04-06 stsp
3351 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3352 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3353 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3354 927df6b7 2019-02-10 stsp return NULL;
3355 927df6b7 2019-02-10 stsp
3356 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3357 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3358 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3359 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3360 f8d1f275 2019-02-04 stsp } else {
3361 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3362 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3363 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3364 f8d1f275 2019-02-04 stsp }
3365 f8d1f275 2019-02-04 stsp
3366 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3367 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3368 f8d1f275 2019-02-04 stsp free(abspath);
3369 9d31a1d8 2018-03-11 stsp return err;
3370 9d31a1d8 2018-03-11 stsp }
3371 f8d1f275 2019-02-04 stsp
3372 f8d1f275 2019-02-04 stsp static const struct got_error *
3373 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3374 f8d1f275 2019-02-04 stsp {
3375 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3376 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3377 2ec1f75b 2019-03-26 stsp unsigned char status;
3378 927df6b7 2019-02-10 stsp
3379 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3380 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3381 0584f854 2019-04-06 stsp
3382 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3383 927df6b7 2019-02-10 stsp return NULL;
3384 927df6b7 2019-02-10 stsp
3385 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3386 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3387 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3388 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3389 2ec1f75b 2019-03-26 stsp else
3390 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3391 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3392 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3393 6841da00 2019-08-08 stsp }
3394 6841da00 2019-08-08 stsp
3395 ef20f542 2022-06-26 thomas static void
3396 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3397 6841da00 2019-08-08 stsp {
3398 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3399 6841da00 2019-08-08 stsp
3400 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3401 6841da00 2019-08-08 stsp free((char *)pe->path);
3402 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3403 6841da00 2019-08-08 stsp }
3404 6841da00 2019-08-08 stsp
3405 ef20f542 2022-06-26 thomas static void
3406 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3407 6841da00 2019-08-08 stsp {
3408 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3409 6841da00 2019-08-08 stsp
3410 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3411 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3412 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3413 6841da00 2019-08-08 stsp free((char *)pe->path);
3414 6841da00 2019-08-08 stsp }
3415 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3416 6841da00 2019-08-08 stsp }
3417 6841da00 2019-08-08 stsp
3418 6841da00 2019-08-08 stsp static const struct got_error *
3419 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3420 6841da00 2019-08-08 stsp {
3421 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3422 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3423 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3424 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3425 6841da00 2019-08-08 stsp size_t linesize = 0;
3426 6841da00 2019-08-08 stsp ssize_t linelen;
3427 6841da00 2019-08-08 stsp
3428 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3429 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3430 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3431 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3432 6841da00 2019-08-08 stsp
3433 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3434 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3435 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3436 bd8de430 2019-10-04 stsp
3437 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3438 bd8de430 2019-10-04 stsp if (line[0] == '#')
3439 bd8de430 2019-10-04 stsp continue;
3440 bd8de430 2019-10-04 stsp
3441 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3442 bd8de430 2019-10-04 stsp if (line[0] == '!')
3443 bd8de430 2019-10-04 stsp continue;
3444 bd8de430 2019-10-04 stsp
3445 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3446 6841da00 2019-08-08 stsp line) == -1) {
3447 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3448 6841da00 2019-08-08 stsp goto done;
3449 6841da00 2019-08-08 stsp }
3450 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3451 6841da00 2019-08-08 stsp if (err)
3452 6841da00 2019-08-08 stsp goto done;
3453 6841da00 2019-08-08 stsp }
3454 6841da00 2019-08-08 stsp if (ferror(f)) {
3455 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3456 6841da00 2019-08-08 stsp goto done;
3457 6841da00 2019-08-08 stsp }
3458 6841da00 2019-08-08 stsp
3459 6841da00 2019-08-08 stsp dirpath = strdup(path);
3460 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3461 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3462 6841da00 2019-08-08 stsp goto done;
3463 6841da00 2019-08-08 stsp }
3464 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3465 6841da00 2019-08-08 stsp done:
3466 6841da00 2019-08-08 stsp free(line);
3467 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3468 6841da00 2019-08-08 stsp free(dirpath);
3469 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3470 6841da00 2019-08-08 stsp }
3471 6841da00 2019-08-08 stsp return err;
3472 6841da00 2019-08-08 stsp }
3473 6841da00 2019-08-08 stsp
3474 ef20f542 2022-06-26 thomas static int
3475 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3476 6841da00 2019-08-08 stsp {
3477 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3478 bd8de430 2019-10-04 stsp
3479 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3480 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3481 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3482 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3483 bd8de430 2019-10-04 stsp
3484 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3485 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3486 6841da00 2019-08-08 stsp
3487 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3488 bd8de430 2019-10-04 stsp continue;
3489 bd8de430 2019-10-04 stsp pattern += 3;
3490 bd8de430 2019-10-04 stsp p = path;
3491 bd8de430 2019-10-04 stsp while (*p) {
3492 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3493 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3494 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3495 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3496 bd8de430 2019-10-04 stsp p++;
3497 bd8de430 2019-10-04 stsp while (*p == '/')
3498 bd8de430 2019-10-04 stsp p++;
3499 bd8de430 2019-10-04 stsp continue;
3500 bd8de430 2019-10-04 stsp }
3501 bd8de430 2019-10-04 stsp return 1;
3502 bd8de430 2019-10-04 stsp }
3503 bd8de430 2019-10-04 stsp }
3504 bd8de430 2019-10-04 stsp }
3505 bd8de430 2019-10-04 stsp
3506 6841da00 2019-08-08 stsp /*
3507 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3508 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3509 6841da00 2019-08-08 stsp * ignores backwards.
3510 6841da00 2019-08-08 stsp */
3511 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3512 6841da00 2019-08-08 stsp while (pe) {
3513 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3514 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3515 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3516 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3517 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3518 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3519 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3520 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3521 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3522 6841da00 2019-08-08 stsp continue;
3523 6841da00 2019-08-08 stsp return 1;
3524 6841da00 2019-08-08 stsp }
3525 6841da00 2019-08-08 stsp }
3526 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3527 6841da00 2019-08-08 stsp }
3528 6841da00 2019-08-08 stsp
3529 6841da00 2019-08-08 stsp return 0;
3530 6841da00 2019-08-08 stsp }
3531 6841da00 2019-08-08 stsp
3532 6841da00 2019-08-08 stsp static const struct got_error *
3533 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3534 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3535 6841da00 2019-08-08 stsp {
3536 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3537 6841da00 2019-08-08 stsp char *ignorespath;
3538 886cec17 2019-12-15 stsp int fd = -1;
3539 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3540 6841da00 2019-08-08 stsp
3541 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3542 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3543 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3544 6841da00 2019-08-08 stsp
3545 886cec17 2019-12-15 stsp if (dirfd != -1) {
3546 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3547 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3548 886cec17 2019-12-15 stsp if (fd == -1) {
3549 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3550 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3551 886cec17 2019-12-15 stsp ignorespath);
3552 886cec17 2019-12-15 stsp } else {
3553 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3554 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3555 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3556 886cec17 2019-12-15 stsp ignorespath);
3557 886cec17 2019-12-15 stsp else {
3558 886cec17 2019-12-15 stsp fd = -1;
3559 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3560 886cec17 2019-12-15 stsp }
3561 886cec17 2019-12-15 stsp }
3562 886cec17 2019-12-15 stsp } else {
3563 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3564 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3565 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3566 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3567 886cec17 2019-12-15 stsp ignorespath);
3568 886cec17 2019-12-15 stsp } else
3569 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3570 886cec17 2019-12-15 stsp }
3571 6841da00 2019-08-08 stsp
3572 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3573 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3574 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3575 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3576 6841da00 2019-08-08 stsp free(ignorespath);
3577 6841da00 2019-08-08 stsp return err;
3578 f8d1f275 2019-02-04 stsp }
3579 f8d1f275 2019-02-04 stsp
3580 f8d1f275 2019-02-04 stsp static const struct got_error *
3581 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3582 6092c299 2021-10-04 thomas int dirfd)
3583 f8d1f275 2019-02-04 stsp {
3584 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3585 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3586 f8d1f275 2019-02-04 stsp char *path = NULL;
3587 6092c299 2021-10-04 thomas
3588 6092c299 2021-10-04 thomas if (ignore != NULL)
3589 6092c299 2021-10-04 thomas *ignore = 0;
3590 f8d1f275 2019-02-04 stsp
3591 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3592 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3593 0584f854 2019-04-06 stsp
3594 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3595 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3596 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3597 f8d1f275 2019-02-04 stsp } else {
3598 f8d1f275 2019-02-04 stsp path = de->d_name;
3599 f8d1f275 2019-02-04 stsp }
3600 f8d1f275 2019-02-04 stsp
3601 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3602 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3603 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3604 6092c299 2021-10-04 thomas *ignore = 1;
3605 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3606 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3607 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3608 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3609 f8d1f275 2019-02-04 stsp if (parent_path[0])
3610 f8d1f275 2019-02-04 stsp free(path);
3611 b72f483a 2019-02-05 stsp return err;
3612 f8d1f275 2019-02-04 stsp }
3613 f8d1f275 2019-02-04 stsp
3614 347d1d3e 2019-07-12 stsp static const struct got_error *
3615 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3616 3143d852 2020-06-25 stsp {
3617 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3618 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3619 3143d852 2020-06-25 stsp
3620 3143d852 2020-06-25 stsp if (a->no_ignores)
3621 3143d852 2020-06-25 stsp return NULL;
3622 3143d852 2020-06-25 stsp
3623 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3624 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3625 3143d852 2020-06-25 stsp if (err)
3626 3143d852 2020-06-25 stsp return err;
3627 3143d852 2020-06-25 stsp
3628 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3629 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3630 3143d852 2020-06-25 stsp
3631 3143d852 2020-06-25 stsp return err;
3632 3143d852 2020-06-25 stsp }
3633 3143d852 2020-06-25 stsp
3634 3143d852 2020-06-25 stsp static const struct got_error *
3635 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3636 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3637 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3638 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3639 abb4604f 2019-07-27 stsp {
3640 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3641 abb4604f 2019-07-27 stsp struct stat sb;
3642 ff56836b 2021-07-08 stsp
3643 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3644 abb4604f 2019-07-27 stsp if (ie)
3645 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3646 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3647 abb4604f 2019-07-27 stsp
3648 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3649 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3650 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3651 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3652 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3653 abb4604f 2019-07-27 stsp }
3654 abb4604f 2019-07-27 stsp
3655 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3656 fe1d8685 2022-02-12 thomas return NULL;
3657 fe1d8685 2022-02-12 thomas
3658 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3659 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3660 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3661 abb4604f 2019-07-27 stsp
3662 abb4604f 2019-07-27 stsp return NULL;
3663 3143d852 2020-06-25 stsp }
3664 3143d852 2020-06-25 stsp
3665 3143d852 2020-06-25 stsp static const struct got_error *
3666 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3667 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3668 3143d852 2020-06-25 stsp {
3669 3143d852 2020-06-25 stsp const struct got_error *err;
3670 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3671 3143d852 2020-06-25 stsp
3672 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3673 3143d852 2020-06-25 stsp ".cvsignore");
3674 3143d852 2020-06-25 stsp if (err)
3675 3143d852 2020-06-25 stsp return err;
3676 3143d852 2020-06-25 stsp
3677 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3678 3143d852 2020-06-25 stsp ".gitignore");
3679 3143d852 2020-06-25 stsp if (err)
3680 3143d852 2020-06-25 stsp return err;
3681 3143d852 2020-06-25 stsp
3682 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3683 3143d852 2020-06-25 stsp if (err) {
3684 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3685 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3686 3143d852 2020-06-25 stsp return err;
3687 3143d852 2020-06-25 stsp }
3688 3143d852 2020-06-25 stsp for (;;) {
3689 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3690 3143d852 2020-06-25 stsp ".cvsignore");
3691 3143d852 2020-06-25 stsp if (err)
3692 3143d852 2020-06-25 stsp break;
3693 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3694 3143d852 2020-06-25 stsp ".gitignore");
3695 3143d852 2020-06-25 stsp if (err)
3696 3143d852 2020-06-25 stsp break;
3697 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3698 3143d852 2020-06-25 stsp if (err) {
3699 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3700 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3701 3143d852 2020-06-25 stsp break;
3702 3143d852 2020-06-25 stsp }
3703 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3704 ff56836b 2021-07-08 stsp break;
3705 b737c85a 2020-06-26 stsp free(parent_path);
3706 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3707 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3708 3143d852 2020-06-25 stsp }
3709 3143d852 2020-06-25 stsp
3710 b737c85a 2020-06-26 stsp free(parent_path);
3711 b737c85a 2020-06-26 stsp free(next_parent_path);
3712 3143d852 2020-06-25 stsp return err;
3713 abb4604f 2019-07-27 stsp }
3714 abb4604f 2019-07-27 stsp
3715 abb4604f 2019-07-27 stsp static const struct got_error *
3716 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3717 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3718 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3719 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3720 f2a9dc41 2019-12-13 tracey int report_unchanged)
3721 f8d1f275 2019-02-04 stsp {
3722 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3723 6fc93f37 2019-12-13 stsp int fd = -1;
3724 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3725 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3726 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3727 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3728 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3729 3143d852 2020-06-25 stsp
3730 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3731 f8d1f275 2019-02-04 stsp
3732 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3733 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3734 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3735 8dc303cc 2019-07-27 stsp
3736 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3737 a78810f8 2022-03-13 thomas if (ie) {
3738 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3739 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3740 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3741 a78810f8 2022-03-13 thomas goto done;
3742 a78810f8 2022-03-13 thomas }
3743 a78810f8 2022-03-13 thomas
3744 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3745 6fc93f37 2019-12-13 stsp if (fd == -1) {
3746 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3747 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3748 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3749 ff56836b 2021-07-08 stsp else {
3750 ff56836b 2021-07-08 stsp if (!no_ignores) {
3751 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3752 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3753 ff56836b 2021-07-08 stsp if (err)
3754 ff56836b 2021-07-08 stsp goto done;
3755 ff56836b 2021-07-08 stsp }
3756 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3757 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3758 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3759 ff56836b 2021-07-08 stsp }
3760 abb4604f 2019-07-27 stsp } else {
3761 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3762 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3763 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3764 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3765 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3766 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3767 abb4604f 2019-07-27 stsp arg.status_path = path;
3768 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3769 abb4604f 2019-07-27 stsp arg.repo = repo;
3770 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3771 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3772 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3773 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3774 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3775 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3776 022fae89 2019-12-06 tracey if (!no_ignores) {
3777 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3778 3143d852 2020-06-25 stsp worktree->root_path, path);
3779 3143d852 2020-06-25 stsp if (err)
3780 3143d852 2020-06-25 stsp goto done;
3781 022fae89 2019-12-06 tracey }
3782 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3783 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3784 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3785 927df6b7 2019-02-10 stsp }
3786 3143d852 2020-06-25 stsp done:
3787 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3788 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3789 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3790 927df6b7 2019-02-10 stsp free(ondisk_path);
3791 347d1d3e 2019-07-12 stsp return err;
3792 347d1d3e 2019-07-12 stsp }
3793 347d1d3e 2019-07-12 stsp
3794 347d1d3e 2019-07-12 stsp const struct got_error *
3795 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3796 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3797 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3798 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3799 347d1d3e 2019-07-12 stsp {
3800 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3801 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3802 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3803 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3804 347d1d3e 2019-07-12 stsp
3805 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3806 347d1d3e 2019-07-12 stsp if (err)
3807 347d1d3e 2019-07-12 stsp return err;
3808 347d1d3e 2019-07-12 stsp
3809 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3810 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3811 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3812 f6343036 2021-06-22 stsp no_ignores, 0);
3813 72ea6654 2019-07-27 stsp if (err)
3814 72ea6654 2019-07-27 stsp break;
3815 72ea6654 2019-07-27 stsp }
3816 f8d1f275 2019-02-04 stsp free(fileindex_path);
3817 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3818 f8d1f275 2019-02-04 stsp return err;
3819 f8d1f275 2019-02-04 stsp }
3820 6c7ab921 2019-03-18 stsp
3821 6c7ab921 2019-03-18 stsp const struct got_error *
3822 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3823 6c7ab921 2019-03-18 stsp const char *arg)
3824 6c7ab921 2019-03-18 stsp {
3825 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3826 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3827 6c7ab921 2019-03-18 stsp size_t len;
3828 00bb5ea0 2020-07-23 stsp struct stat sb;
3829 01740607 2020-11-04 stsp char *abspath = NULL;
3830 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3831 6c7ab921 2019-03-18 stsp
3832 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3833 6c7ab921 2019-03-18 stsp
3834 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3835 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3836 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3837 00bb5ea0 2020-07-23 stsp
3838 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3839 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3840 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3841 00bb5ea0 2020-07-23 stsp goto done;
3842 00bb5ea0 2020-07-23 stsp }
3843 727173c3 2020-11-06 stsp sb.st_mode = 0;
3844 00bb5ea0 2020-07-23 stsp }
3845 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3846 00bb5ea0 2020-07-23 stsp /*
3847 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3848 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3849 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3850 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3851 00bb5ea0 2020-07-23 stsp */
3852 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3853 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3854 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3855 00bb5ea0 2020-07-23 stsp goto done;
3856 00bb5ea0 2020-07-23 stsp }
3857 00bb5ea0 2020-07-23 stsp
3858 00bb5ea0 2020-07-23 stsp }
3859 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3860 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3861 00bb5ea0 2020-07-23 stsp if (err)
3862 d0710d08 2019-07-22 stsp goto done;
3863 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3864 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3865 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3866 00bb5ea0 2020-07-23 stsp goto done;
3867 00bb5ea0 2020-07-23 stsp }
3868 00bb5ea0 2020-07-23 stsp } else {
3869 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3870 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3871 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3872 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3873 00bb5ea0 2020-07-23 stsp goto done;
3874 00bb5ea0 2020-07-23 stsp }
3875 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3876 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3877 01740607 2020-11-04 stsp goto done;
3878 01740607 2020-11-04 stsp }
3879 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3880 01740607 2020-11-04 stsp sizeof(canonpath));
3881 01740607 2020-11-04 stsp if (err)
3882 00bb5ea0 2020-07-23 stsp goto done;
3883 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3884 01740607 2020-11-04 stsp if (resolved == NULL) {
3885 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3886 01740607 2020-11-04 stsp goto done;
3887 00bb5ea0 2020-07-23 stsp }
3888 d0710d08 2019-07-22 stsp }
3889 d0710d08 2019-07-22 stsp }
3890 6c7ab921 2019-03-18 stsp
3891 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3892 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3893 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3894 6c7ab921 2019-03-18 stsp goto done;
3895 6c7ab921 2019-03-18 stsp }
3896 6c7ab921 2019-03-18 stsp
3897 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3898 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3899 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3900 f6d88e1a 2019-05-29 stsp if (err)
3901 f6d88e1a 2019-05-29 stsp goto done;
3902 f6d88e1a 2019-05-29 stsp } else {
3903 f6d88e1a 2019-05-29 stsp path = strdup("");
3904 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3905 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3906 f6d88e1a 2019-05-29 stsp goto done;
3907 f6d88e1a 2019-05-29 stsp }
3908 6c7ab921 2019-03-18 stsp }
3909 6c7ab921 2019-03-18 stsp
3910 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3911 6c7ab921 2019-03-18 stsp len = strlen(path);
3912 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3913 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3914 6c7ab921 2019-03-18 stsp len--;
3915 6c7ab921 2019-03-18 stsp }
3916 6c7ab921 2019-03-18 stsp done:
3917 01740607 2020-11-04 stsp free(abspath);
3918 6c7ab921 2019-03-18 stsp free(resolved);
3919 d0710d08 2019-07-22 stsp free(cwd);
3920 6c7ab921 2019-03-18 stsp if (err == NULL)
3921 6c7ab921 2019-03-18 stsp *wt_path = path;
3922 6c7ab921 2019-03-18 stsp else
3923 6c7ab921 2019-03-18 stsp free(path);
3924 d00136be 2019-03-26 stsp return err;
3925 d00136be 2019-03-26 stsp }
3926 d00136be 2019-03-26 stsp
3927 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3928 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3929 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3930 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3931 4e68cba3 2019-11-23 stsp void *progress_arg;
3932 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3933 4e68cba3 2019-11-23 stsp };
3934 4e68cba3 2019-11-23 stsp
3935 4e68cba3 2019-11-23 stsp static const struct got_error *
3936 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3937 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3938 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3939 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3940 07f5b47a 2019-06-02 stsp {
3941 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3942 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3943 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3944 6d022e97 2019-08-04 stsp struct stat sb;
3945 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3946 07f5b47a 2019-06-02 stsp
3947 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3948 dbb83fbd 2019-12-12 stsp relpath) == -1)
3949 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3950 dbb83fbd 2019-12-12 stsp
3951 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3952 6d022e97 2019-08-04 stsp if (ie) {
3953 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3954 12463d8b 2019-12-13 stsp de_name, a->repo);
3955 6d022e97 2019-08-04 stsp if (err)
3956 dbb83fbd 2019-12-12 stsp goto done;
3957 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3958 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3959 dbb83fbd 2019-12-12 stsp goto done;
3960 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3961 dbb83fbd 2019-12-12 stsp if (err)
3962 dbb83fbd 2019-12-12 stsp goto done;
3963 6d022e97 2019-08-04 stsp }
3964 07f5b47a 2019-06-02 stsp
3965 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3966 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3967 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3968 84bf00a6 2022-02-12 thomas else
3969 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3970 dbb83fbd 2019-12-12 stsp goto done;
3971 4e68cba3 2019-11-23 stsp }
3972 07f5b47a 2019-06-02 stsp
3973 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3974 4e68cba3 2019-11-23 stsp if (err)
3975 4e68cba3 2019-11-23 stsp goto done;
3976 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3977 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3978 3969253a 2020-03-07 stsp if (err) {
3979 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3980 3969253a 2020-03-07 stsp goto done;
3981 3969253a 2020-03-07 stsp }
3982 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3983 07f5b47a 2019-06-02 stsp if (err) {
3984 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3985 4e68cba3 2019-11-23 stsp goto done;
3986 07f5b47a 2019-06-02 stsp }
3987 4e68cba3 2019-11-23 stsp done:
3988 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3989 dbb83fbd 2019-12-12 stsp if (err)
3990 dbb83fbd 2019-12-12 stsp return err;
3991 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3992 dbb83fbd 2019-12-12 stsp return NULL;
3993 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3994 07f5b47a 2019-06-02 stsp }
3995 07f5b47a 2019-06-02 stsp
3996 d00136be 2019-03-26 stsp const struct got_error *
3997 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3998 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3999 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4000 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4001 d00136be 2019-03-26 stsp {
4002 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4003 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4004 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4005 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4006 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4007 d00136be 2019-03-26 stsp
4008 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4009 d00136be 2019-03-26 stsp if (err)
4010 d00136be 2019-03-26 stsp return err;
4011 d00136be 2019-03-26 stsp
4012 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4013 d00136be 2019-03-26 stsp if (err)
4014 d00136be 2019-03-26 stsp goto done;
4015 d00136be 2019-03-26 stsp
4016 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4017 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4018 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4019 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4020 4e68cba3 2019-11-23 stsp saa.repo = repo;
4021 4e68cba3 2019-11-23 stsp
4022 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4023 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4024 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4025 1dd54920 2019-05-11 stsp if (err)
4026 af12c6b9 2019-06-04 stsp break;
4027 1dd54920 2019-05-11 stsp }
4028 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4029 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4030 af12c6b9 2019-06-04 stsp err = sync_err;
4031 d00136be 2019-03-26 stsp done:
4032 fb399478 2019-07-12 stsp free(fileindex_path);
4033 d00136be 2019-03-26 stsp if (fileindex)
4034 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4035 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4036 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4037 d00136be 2019-03-26 stsp err = unlockerr;
4038 6c7ab921 2019-03-18 stsp return err;
4039 6c7ab921 2019-03-18 stsp }
4040 17ed4618 2019-06-02 stsp
4041 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4042 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4043 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4044 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4045 f2a9dc41 2019-12-13 tracey void *progress_arg;
4046 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4047 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4048 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4049 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4050 766841c2 2020-08-13 stsp const char *status_codes;
4051 f2a9dc41 2019-12-13 tracey };
4052 f2a9dc41 2019-12-13 tracey
4053 f2a9dc41 2019-12-13 tracey static const struct got_error *
4054 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4055 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4056 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4057 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4058 17ed4618 2019-06-02 stsp {
4059 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4060 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4061 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4062 17ed4618 2019-06-02 stsp struct stat sb;
4063 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4064 d64cc78a 2022-01-25 thomas
4065 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4066 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4067 d64cc78a 2022-01-25 thomas return NULL;
4068 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4069 d64cc78a 2022-01-25 thomas }
4070 17ed4618 2019-06-02 stsp
4071 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4072 17ed4618 2019-06-02 stsp if (ie == NULL)
4073 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4074 2ec1f75b 2019-03-26 stsp
4075 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4076 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4077 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4078 9acbc4fa 2019-08-03 stsp return NULL;
4079 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4080 9acbc4fa 2019-08-03 stsp }
4081 9acbc4fa 2019-08-03 stsp
4082 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4083 f2a9dc41 2019-12-13 tracey relpath) == -1)
4084 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4085 f2a9dc41 2019-12-13 tracey
4086 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4087 12463d8b 2019-12-13 stsp a->repo);
4088 17ed4618 2019-06-02 stsp if (err)
4089 f2a9dc41 2019-12-13 tracey goto done;
4090 17ed4618 2019-06-02 stsp
4091 766841c2 2020-08-13 stsp if (a->status_codes) {
4092 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4093 766841c2 2020-08-13 stsp int i;
4094 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4095 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4096 766841c2 2020-08-13 stsp break;
4097 766841c2 2020-08-13 stsp }
4098 766841c2 2020-08-13 stsp if (i == ncodes) {
4099 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4100 766841c2 2020-08-13 stsp free(ondisk_path);
4101 766841c2 2020-08-13 stsp return NULL;
4102 766841c2 2020-08-13 stsp }
4103 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4104 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4105 766841c2 2020-08-13 stsp static char msg[64];
4106 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4107 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4108 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4109 766841c2 2020-08-13 stsp goto done;
4110 766841c2 2020-08-13 stsp }
4111 766841c2 2020-08-13 stsp }
4112 766841c2 2020-08-13 stsp
4113 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4114 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4115 f2a9dc41 2019-12-13 tracey goto done;
4116 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4117 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4118 f2a9dc41 2019-12-13 tracey goto done;
4119 f2a9dc41 2019-12-13 tracey }
4120 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4121 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4122 d64cc78a 2022-01-25 thomas goto done;
4123 d64cc78a 2022-01-25 thomas }
4124 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4125 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4126 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4127 f2a9dc41 2019-12-13 tracey goto done;
4128 f2a9dc41 2019-12-13 tracey }
4129 17ed4618 2019-06-02 stsp }
4130 17ed4618 2019-06-02 stsp
4131 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4132 20a2ad1c 2020-10-20 stsp size_t root_len;
4133 20a2ad1c 2020-10-20 stsp
4134 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4135 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4136 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4137 12463d8b 2019-12-13 stsp ondisk_path);
4138 12463d8b 2019-12-13 stsp goto done;
4139 12463d8b 2019-12-13 stsp }
4140 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4141 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4142 12463d8b 2019-12-13 stsp goto done;
4143 15341bfd 2020-03-05 tracey }
4144 15341bfd 2020-03-05 tracey
4145 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4146 20a2ad1c 2020-10-20 stsp do {
4147 20a2ad1c 2020-10-20 stsp char *parent;
4148 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4149 20a2ad1c 2020-10-20 stsp if (err)
4150 2513f20a 2020-10-20 stsp goto done;
4151 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4152 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4153 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4154 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4155 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4156 20a2ad1c 2020-10-20 stsp ondisk_path);
4157 15341bfd 2020-03-05 tracey break;
4158 15341bfd 2020-03-05 tracey }
4159 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4160 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4161 f2a9dc41 2019-12-13 tracey }
4162 17ed4618 2019-06-02 stsp
4163 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4164 f2a9dc41 2019-12-13 tracey done:
4165 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4166 f2a9dc41 2019-12-13 tracey if (err)
4167 f2a9dc41 2019-12-13 tracey return err;
4168 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4169 f2a9dc41 2019-12-13 tracey return NULL;
4170 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4171 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4172 17ed4618 2019-06-02 stsp }
4173 17ed4618 2019-06-02 stsp
4174 2ec1f75b 2019-03-26 stsp const struct got_error *
4175 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4176 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4177 766841c2 2020-08-13 stsp const char *status_codes,
4178 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4179 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4180 2ec1f75b 2019-03-26 stsp {
4181 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4182 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4183 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4184 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4185 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4186 2ec1f75b 2019-03-26 stsp
4187 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4188 2ec1f75b 2019-03-26 stsp if (err)
4189 2ec1f75b 2019-03-26 stsp return err;
4190 2ec1f75b 2019-03-26 stsp
4191 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4192 2ec1f75b 2019-03-26 stsp if (err)
4193 2ec1f75b 2019-03-26 stsp goto done;
4194 f2a9dc41 2019-12-13 tracey
4195 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4196 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4197 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4198 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4199 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4200 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4201 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4202 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4203 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4204 2ec1f75b 2019-03-26 stsp
4205 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4206 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4207 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4208 17ed4618 2019-06-02 stsp if (err)
4209 af12c6b9 2019-06-04 stsp break;
4210 2ec1f75b 2019-03-26 stsp }
4211 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4212 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4213 af12c6b9 2019-06-04 stsp err = sync_err;
4214 2ec1f75b 2019-03-26 stsp done:
4215 fb399478 2019-07-12 stsp free(fileindex_path);
4216 2ec1f75b 2019-03-26 stsp if (fileindex)
4217 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4218 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4219 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4220 2ec1f75b 2019-03-26 stsp err = unlockerr;
4221 33aa809d 2019-08-08 stsp return err;
4222 33aa809d 2019-08-08 stsp }
4223 33aa809d 2019-08-08 stsp
4224 33aa809d 2019-08-08 stsp static const struct got_error *
4225 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4226 33aa809d 2019-08-08 stsp {
4227 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4228 33aa809d 2019-08-08 stsp char *line = NULL;
4229 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4230 33aa809d 2019-08-08 stsp ssize_t linelen;
4231 33aa809d 2019-08-08 stsp
4232 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4233 33aa809d 2019-08-08 stsp if (linelen == -1) {
4234 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4235 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4236 33aa809d 2019-08-08 stsp goto done;
4237 33aa809d 2019-08-08 stsp }
4238 33aa809d 2019-08-08 stsp return NULL;
4239 33aa809d 2019-08-08 stsp }
4240 33aa809d 2019-08-08 stsp if (outfile) {
4241 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4242 33aa809d 2019-08-08 stsp if (n != linelen) {
4243 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4244 33aa809d 2019-08-08 stsp goto done;
4245 33aa809d 2019-08-08 stsp }
4246 33aa809d 2019-08-08 stsp }
4247 33aa809d 2019-08-08 stsp if (rejectfile) {
4248 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4249 33aa809d 2019-08-08 stsp if (n != linelen)
4250 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4251 33aa809d 2019-08-08 stsp }
4252 33aa809d 2019-08-08 stsp done:
4253 33aa809d 2019-08-08 stsp free(line);
4254 2ec1f75b 2019-03-26 stsp return err;
4255 2ec1f75b 2019-03-26 stsp }
4256 1f1abb7e 2019-08-08 stsp
4257 33aa809d 2019-08-08 stsp static const struct got_error *
4258 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4259 33aa809d 2019-08-08 stsp {
4260 33aa809d 2019-08-08 stsp char *line = NULL;
4261 33aa809d 2019-08-08 stsp size_t linesize = 0;
4262 33aa809d 2019-08-08 stsp ssize_t linelen;
4263 33aa809d 2019-08-08 stsp
4264 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4265 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4266 500467ff 2019-09-25 hiltjo if (ferror(f))
4267 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4268 500467ff 2019-09-25 hiltjo return NULL;
4269 500467ff 2019-09-25 hiltjo }
4270 33aa809d 2019-08-08 stsp free(line);
4271 33aa809d 2019-08-08 stsp return NULL;
4272 33aa809d 2019-08-08 stsp }
4273 33aa809d 2019-08-08 stsp
4274 33aa809d 2019-08-08 stsp static const struct got_error *
4275 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4276 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4277 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4278 abc59930 2021-09-05 naddy {
4279 33aa809d 2019-08-08 stsp const struct got_error *err;
4280 33aa809d 2019-08-08 stsp
4281 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4282 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4283 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4284 33aa809d 2019-08-08 stsp if (err)
4285 33aa809d 2019-08-08 stsp return err;
4286 33aa809d 2019-08-08 stsp (*line_cur1)++;
4287 33aa809d 2019-08-08 stsp }
4288 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4289 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4290 33aa809d 2019-08-08 stsp if (rejectfile)
4291 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4292 33aa809d 2019-08-08 stsp else
4293 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4294 33aa809d 2019-08-08 stsp if (err)
4295 33aa809d 2019-08-08 stsp return err;
4296 33aa809d 2019-08-08 stsp (*line_cur2)++;
4297 33aa809d 2019-08-08 stsp }
4298 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4299 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4300 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4301 33aa809d 2019-08-08 stsp if (err)
4302 33aa809d 2019-08-08 stsp return err;
4303 33aa809d 2019-08-08 stsp (*line_cur2)++;
4304 33aa809d 2019-08-08 stsp }
4305 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4306 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4307 33aa809d 2019-08-08 stsp if (rejectfile)
4308 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4309 33aa809d 2019-08-08 stsp else
4310 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4311 33aa809d 2019-08-08 stsp if (err)
4312 33aa809d 2019-08-08 stsp return err;
4313 33aa809d 2019-08-08 stsp (*line_cur1)++;
4314 33aa809d 2019-08-08 stsp }
4315 f1e81a05 2019-08-10 stsp
4316 f1e81a05 2019-08-10 stsp return NULL;
4317 f1e81a05 2019-08-10 stsp }
4318 f1e81a05 2019-08-10 stsp
4319 f1e81a05 2019-08-10 stsp static const struct got_error *
4320 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4321 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4322 f1e81a05 2019-08-10 stsp {
4323 f1e81a05 2019-08-10 stsp const struct got_error *err;
4324 f1e81a05 2019-08-10 stsp
4325 f1e81a05 2019-08-10 stsp if (outfile) {
4326 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4327 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4328 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4329 f1e81a05 2019-08-10 stsp if (err)
4330 f1e81a05 2019-08-10 stsp return err;
4331 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4332 f1e81a05 2019-08-10 stsp }
4333 f1e81a05 2019-08-10 stsp }
4334 f1e81a05 2019-08-10 stsp if (rejectfile) {
4335 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4336 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4337 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4338 f1e81a05 2019-08-10 stsp if (err)
4339 f1e81a05 2019-08-10 stsp return err;
4340 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4341 f1e81a05 2019-08-10 stsp }
4342 33aa809d 2019-08-08 stsp }
4343 33aa809d 2019-08-08 stsp
4344 33aa809d 2019-08-08 stsp return NULL;
4345 33aa809d 2019-08-08 stsp }
4346 33aa809d 2019-08-08 stsp
4347 33aa809d 2019-08-08 stsp static const struct got_error *
4348 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4349 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4350 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4351 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4352 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4353 33aa809d 2019-08-08 stsp {
4354 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4355 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4356 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4357 33aa809d 2019-08-08 stsp FILE *hunkfile;
4358 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4359 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4360 fe621944 2020-11-10 stsp int rc;
4361 33aa809d 2019-08-08 stsp
4362 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4363 33aa809d 2019-08-08 stsp
4364 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4365 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4366 fe621944 2020-11-10 stsp start_old = cc.left.start;
4367 fe621944 2020-11-10 stsp end_old = cc.left.end;
4368 fe621944 2020-11-10 stsp start_new = cc.right.start;
4369 fe621944 2020-11-10 stsp end_new = cc.right.end;
4370 33aa809d 2019-08-08 stsp
4371 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4372 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4373 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4374 33aa809d 2019-08-08 stsp
4375 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4376 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4377 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4378 33aa809d 2019-08-08 stsp
4379 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4380 fe621944 2020-11-10 stsp if (diff_state == NULL)
4381 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4382 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4383 fe621944 2020-11-10 stsp
4384 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4385 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4386 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4387 33aa809d 2019-08-08 stsp goto done;
4388 33aa809d 2019-08-08 stsp }
4389 fe621944 2020-11-10 stsp
4390 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4391 fe621944 2020-11-10 stsp diff_result, &cc);
4392 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4393 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4394 33aa809d 2019-08-08 stsp goto done;
4395 33aa809d 2019-08-08 stsp }
4396 fe621944 2020-11-10 stsp
4397 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4398 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4399 33aa809d 2019-08-08 stsp goto done;
4400 33aa809d 2019-08-08 stsp }
4401 33aa809d 2019-08-08 stsp
4402 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4403 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4404 33aa809d 2019-08-08 stsp if (err)
4405 33aa809d 2019-08-08 stsp goto done;
4406 33aa809d 2019-08-08 stsp
4407 33aa809d 2019-08-08 stsp switch (*choice) {
4408 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4409 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4410 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4411 33aa809d 2019-08-08 stsp break;
4412 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4413 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4414 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4415 33aa809d 2019-08-08 stsp break;
4416 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4417 33aa809d 2019-08-08 stsp break;
4418 33aa809d 2019-08-08 stsp default:
4419 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4420 33aa809d 2019-08-08 stsp break;
4421 33aa809d 2019-08-08 stsp }
4422 33aa809d 2019-08-08 stsp done:
4423 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4424 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4425 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4426 33aa809d 2019-08-08 stsp return err;
4427 33aa809d 2019-08-08 stsp }
4428 33aa809d 2019-08-08 stsp
4429 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4430 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4431 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4432 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4433 1f1abb7e 2019-08-08 stsp void *progress_arg;
4434 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4435 33aa809d 2019-08-08 stsp void *patch_arg;
4436 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4437 10604dce 2021-09-24 thomas int unlink_added_files;
4438 1f1abb7e 2019-08-08 stsp };
4439 a129376b 2019-03-28 stsp
4440 e20a8b6f 2019-06-04 stsp static const struct got_error *
4441 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4442 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4443 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4444 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4445 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4446 33aa809d 2019-08-08 stsp {
4447 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4448 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4449 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4450 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4451 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4452 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4453 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4454 fe621944 2020-11-10 stsp struct stat sb2;
4455 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4456 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4457 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4458 33aa809d 2019-08-08 stsp
4459 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4460 33aa809d 2019-08-08 stsp
4461 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4462 33aa809d 2019-08-08 stsp if (err)
4463 33aa809d 2019-08-08 stsp return err;
4464 33aa809d 2019-08-08 stsp
4465 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4466 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4467 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4468 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4469 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4470 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4471 fa3cef63 2020-07-23 stsp goto done;
4472 fa3cef63 2020-07-23 stsp }
4473 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4474 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4475 48b4f239 2021-12-31 thomas if (link_len == -1) {
4476 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4477 48b4f239 2021-12-31 thomas path2);
4478 48b4f239 2021-12-31 thomas }
4479 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4480 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4481 12463d8b 2019-12-13 stsp }
4482 12463d8b 2019-12-13 stsp } else {
4483 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4484 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4485 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4486 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4487 fa3cef63 2020-07-23 stsp goto done;
4488 fa3cef63 2020-07-23 stsp }
4489 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4490 fa3cef63 2020-07-23 stsp sizeof(link_target));
4491 fa3cef63 2020-07-23 stsp if (link_len == -1)
4492 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4493 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4494 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4495 12463d8b 2019-12-13 stsp }
4496 1ebedb77 2019-10-19 stsp }
4497 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4498 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4499 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4500 fa3cef63 2020-07-23 stsp goto done;
4501 fa3cef63 2020-07-23 stsp }
4502 1ebedb77 2019-10-19 stsp
4503 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4504 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4505 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4506 fa3cef63 2020-07-23 stsp goto done;
4507 fa3cef63 2020-07-23 stsp }
4508 fa3cef63 2020-07-23 stsp fd2 = -1;
4509 fa3cef63 2020-07-23 stsp } else {
4510 fa3cef63 2020-07-23 stsp size_t n;
4511 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4512 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4513 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4514 fa3cef63 2020-07-23 stsp goto done;
4515 fa3cef63 2020-07-23 stsp }
4516 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4517 fa3cef63 2020-07-23 stsp if (n != link_len) {
4518 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4519 fa3cef63 2020-07-23 stsp goto done;
4520 fa3cef63 2020-07-23 stsp }
4521 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4522 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4523 fa3cef63 2020-07-23 stsp goto done;
4524 fa3cef63 2020-07-23 stsp }
4525 fa3cef63 2020-07-23 stsp rewind(f2);
4526 33aa809d 2019-08-08 stsp }
4527 33aa809d 2019-08-08 stsp
4528 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4529 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4530 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4531 f4ae6ddb 2022-07-01 thomas goto done;
4532 f4ae6ddb 2022-07-01 thomas }
4533 f4ae6ddb 2022-07-01 thomas
4534 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4535 33aa809d 2019-08-08 stsp if (err)
4536 33aa809d 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp
4538 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4539 33aa809d 2019-08-08 stsp if (err)
4540 33aa809d 2019-08-08 stsp goto done;
4541 33aa809d 2019-08-08 stsp
4542 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4543 33aa809d 2019-08-08 stsp if (err)
4544 33aa809d 2019-08-08 stsp goto done;
4545 33aa809d 2019-08-08 stsp
4546 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4547 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4548 33aa809d 2019-08-08 stsp if (err)
4549 33aa809d 2019-08-08 stsp goto done;
4550 33aa809d 2019-08-08 stsp
4551 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4552 33aa809d 2019-08-08 stsp if (err)
4553 33aa809d 2019-08-08 stsp goto done;
4554 33aa809d 2019-08-08 stsp
4555 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4556 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4557 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4558 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4559 fe621944 2020-11-10 stsp
4560 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4561 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4562 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4563 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4564 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4565 fe621944 2020-11-10 stsp nchanges++;
4566 fe621944 2020-11-10 stsp }
4567 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4568 33aa809d 2019-08-08 stsp int choice;
4569 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4570 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4571 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4572 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4573 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4574 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4575 33aa809d 2019-08-08 stsp if (err)
4576 33aa809d 2019-08-08 stsp goto done;
4577 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4578 33aa809d 2019-08-08 stsp have_content = 1;
4579 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4580 33aa809d 2019-08-08 stsp break;
4581 33aa809d 2019-08-08 stsp }
4582 1ebedb77 2019-10-19 stsp if (have_content) {
4583 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4584 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4585 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4586 1ebedb77 2019-10-19 stsp if (err)
4587 1ebedb77 2019-10-19 stsp goto done;
4588 1ebedb77 2019-10-19 stsp
4589 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4590 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4591 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4592 fa3cef63 2020-07-23 stsp goto done;
4593 fa3cef63 2020-07-23 stsp }
4594 1ebedb77 2019-10-19 stsp }
4595 1ebedb77 2019-10-19 stsp }
4596 33aa809d 2019-08-08 stsp done:
4597 33aa809d 2019-08-08 stsp free(id_str);
4598 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4599 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4600 33aa809d 2019-08-08 stsp if (blob)
4601 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4602 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4603 fe621944 2020-11-10 stsp if (err == NULL)
4604 fe621944 2020-11-10 stsp err = free_err;
4605 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4606 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4607 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4608 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4609 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4610 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4611 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4612 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4613 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4614 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4615 33aa809d 2019-08-08 stsp if (err || !have_content) {
4616 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4617 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4618 33aa809d 2019-08-08 stsp free(*path_outfile);
4619 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4620 33aa809d 2019-08-08 stsp }
4621 33aa809d 2019-08-08 stsp free(path1);
4622 33aa809d 2019-08-08 stsp return err;
4623 33aa809d 2019-08-08 stsp }
4624 33aa809d 2019-08-08 stsp
4625 33aa809d 2019-08-08 stsp static const struct got_error *
4626 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4627 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4628 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4629 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4630 a129376b 2019-03-28 stsp {
4631 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4632 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4633 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4634 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4635 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4636 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4637 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4638 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4639 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4640 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4641 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4642 f4ae6ddb 2022-07-01 thomas int fd = -1;
4643 a129376b 2019-03-28 stsp
4644 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4645 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4646 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4647 d3bcc3d1 2019-08-08 stsp return NULL;
4648 3d69ad8d 2019-08-17 semarie
4649 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4650 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4651 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4652 d3bcc3d1 2019-08-08 stsp
4653 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4654 65084dad 2019-08-08 stsp if (ie == NULL)
4655 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4656 a129376b 2019-03-28 stsp
4657 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4658 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4659 a129376b 2019-03-28 stsp if (err) {
4660 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4661 a129376b 2019-03-28 stsp goto done;
4662 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4663 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4664 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4665 e20a8b6f 2019-06-04 stsp goto done;
4666 e20a8b6f 2019-06-04 stsp }
4667 a129376b 2019-03-28 stsp }
4668 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4669 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4670 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4671 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4672 a129376b 2019-03-28 stsp goto done;
4673 a129376b 2019-03-28 stsp }
4674 a129376b 2019-03-28 stsp } else {
4675 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4676 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4677 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4678 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4679 a129376b 2019-03-28 stsp goto done;
4680 a129376b 2019-03-28 stsp }
4681 a129376b 2019-03-28 stsp } else {
4682 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4683 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4685 a129376b 2019-03-28 stsp goto done;
4686 a129376b 2019-03-28 stsp }
4687 a129376b 2019-03-28 stsp }
4688 a129376b 2019-03-28 stsp }
4689 a129376b 2019-03-28 stsp
4690 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4691 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4692 945f9229 2022-04-16 thomas if (err)
4693 945f9229 2022-04-16 thomas goto done;
4694 945f9229 2022-04-16 thomas
4695 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4696 a9fa2909 2019-07-27 stsp if (err) {
4697 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4698 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4699 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4700 a9fa2909 2019-07-27 stsp goto done;
4701 a9fa2909 2019-07-27 stsp } else {
4702 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4703 a9fa2909 2019-07-27 stsp if (err)
4704 a9fa2909 2019-07-27 stsp goto done;
4705 a9fa2909 2019-07-27 stsp
4706 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4707 1233e6b6 2020-10-19 stsp if (err)
4708 a9fa2909 2019-07-27 stsp goto done;
4709 a9fa2909 2019-07-27 stsp
4710 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4711 1233e6b6 2020-10-19 stsp free(te_name);
4712 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4713 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4714 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4715 a9fa2909 2019-07-27 stsp goto done;
4716 a9fa2909 2019-07-27 stsp }
4717 a129376b 2019-03-28 stsp }
4718 a129376b 2019-03-28 stsp
4719 a129376b 2019-03-28 stsp switch (status) {
4720 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4721 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4722 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4723 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4724 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4725 33aa809d 2019-08-08 stsp if (err)
4726 33aa809d 2019-08-08 stsp goto done;
4727 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4728 33aa809d 2019-08-08 stsp break;
4729 33aa809d 2019-08-08 stsp }
4730 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4731 1f1abb7e 2019-08-08 stsp ie->path);
4732 1ee397ad 2019-07-12 stsp if (err)
4733 1ee397ad 2019-07-12 stsp goto done;
4734 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4735 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4736 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4737 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4738 10604dce 2021-09-24 thomas relpath) == -1) {
4739 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4740 10604dce 2021-09-24 thomas goto done;
4741 10604dce 2021-09-24 thomas }
4742 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4743 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4744 10604dce 2021-09-24 thomas ondisk_path);
4745 10604dce 2021-09-24 thomas break;
4746 10604dce 2021-09-24 thomas }
4747 10604dce 2021-09-24 thomas }
4748 a129376b 2019-03-28 stsp break;
4749 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4750 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4751 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4752 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4753 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4754 33aa809d 2019-08-08 stsp if (err)
4755 33aa809d 2019-08-08 stsp goto done;
4756 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4757 33aa809d 2019-08-08 stsp break;
4758 33aa809d 2019-08-08 stsp }
4759 33aa809d 2019-08-08 stsp /* fall through */
4760 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4761 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4762 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4763 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4764 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4765 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4766 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4767 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4768 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4769 24278f30 2019-08-03 stsp } else
4770 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4771 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4772 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4773 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4774 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4775 f4ae6ddb 2022-07-01 thomas goto done;
4776 f4ae6ddb 2022-07-01 thomas }
4777 f4ae6ddb 2022-07-01 thomas
4778 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4779 a129376b 2019-03-28 stsp if (err)
4780 65084dad 2019-08-08 stsp goto done;
4781 65084dad 2019-08-08 stsp
4782 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4783 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4784 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4785 a129376b 2019-03-28 stsp goto done;
4786 65084dad 2019-08-08 stsp }
4787 65084dad 2019-08-08 stsp
4788 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4789 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4790 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4791 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4792 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4793 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4794 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4795 33aa809d 2019-08-08 stsp break;
4796 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4797 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4798 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4799 369fd7e5 2020-07-23 stsp path_content);
4800 369fd7e5 2020-07-23 stsp break;
4801 369fd7e5 2020-07-23 stsp }
4802 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4803 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4804 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4805 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4806 369fd7e5 2020-07-23 stsp } else {
4807 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4808 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4809 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4810 369fd7e5 2020-07-23 stsp goto done;
4811 369fd7e5 2020-07-23 stsp }
4812 33aa809d 2019-08-08 stsp }
4813 33aa809d 2019-08-08 stsp } else {
4814 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4815 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4816 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4817 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4818 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4819 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4820 2e1fa222 2020-07-23 stsp } else {
4821 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4822 2e1fa222 2020-07-23 stsp ie->path,
4823 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4824 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4825 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4826 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4827 2e1fa222 2020-07-23 stsp }
4828 a129376b 2019-03-28 stsp if (err)
4829 a129376b 2019-03-28 stsp goto done;
4830 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4831 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4832 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4833 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4834 437adc9d 2020-12-10 yzhong blob->id.sha1,
4835 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4836 2e1fa222 2020-07-23 stsp if (err)
4837 2e1fa222 2020-07-23 stsp goto done;
4838 2e1fa222 2020-07-23 stsp }
4839 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4840 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4841 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4842 33aa809d 2019-08-08 stsp }
4843 a129376b 2019-03-28 stsp }
4844 a129376b 2019-03-28 stsp break;
4845 e20a8b6f 2019-06-04 stsp }
4846 a129376b 2019-03-28 stsp default:
4847 1f1abb7e 2019-08-08 stsp break;
4848 a129376b 2019-03-28 stsp }
4849 a129376b 2019-03-28 stsp done:
4850 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4851 33aa809d 2019-08-08 stsp free(path_content);
4852 e20a8b6f 2019-06-04 stsp free(parent_path);
4853 a129376b 2019-03-28 stsp free(tree_path);
4854 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4855 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4856 a129376b 2019-03-28 stsp if (blob)
4857 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4858 a129376b 2019-03-28 stsp if (tree)
4859 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4860 a129376b 2019-03-28 stsp free(tree_id);
4861 945f9229 2022-04-16 thomas if (base_commit)
4862 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4863 e20a8b6f 2019-06-04 stsp return err;
4864 e20a8b6f 2019-06-04 stsp }
4865 e20a8b6f 2019-06-04 stsp
4866 e20a8b6f 2019-06-04 stsp const struct got_error *
4867 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4868 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4869 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4870 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4871 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4872 e20a8b6f 2019-06-04 stsp {
4873 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4874 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4875 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4876 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4877 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4878 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4879 e20a8b6f 2019-06-04 stsp
4880 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4881 e20a8b6f 2019-06-04 stsp if (err)
4882 e20a8b6f 2019-06-04 stsp return err;
4883 e20a8b6f 2019-06-04 stsp
4884 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4885 e20a8b6f 2019-06-04 stsp if (err)
4886 e20a8b6f 2019-06-04 stsp goto done;
4887 e20a8b6f 2019-06-04 stsp
4888 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4889 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4890 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4891 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4892 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4893 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4894 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4895 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4896 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4897 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4898 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4899 e20a8b6f 2019-06-04 stsp if (err)
4900 af12c6b9 2019-06-04 stsp break;
4901 e20a8b6f 2019-06-04 stsp }
4902 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4903 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4904 e20a8b6f 2019-06-04 stsp err = sync_err;
4905 af12c6b9 2019-06-04 stsp done:
4906 fb399478 2019-07-12 stsp free(fileindex_path);
4907 a129376b 2019-03-28 stsp if (fileindex)
4908 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4909 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4910 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4911 a129376b 2019-03-28 stsp err = unlockerr;
4912 c4296144 2019-05-09 stsp return err;
4913 c4296144 2019-05-09 stsp }
4914 c4296144 2019-05-09 stsp
4915 cf066bf8 2019-05-09 stsp static void
4916 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4917 cf066bf8 2019-05-09 stsp {
4918 24519714 2019-05-09 stsp free(ct->path);
4919 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4920 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4921 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4922 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4923 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4924 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4925 cf066bf8 2019-05-09 stsp free(ct);
4926 cf066bf8 2019-05-09 stsp }
4927 24519714 2019-05-09 stsp
4928 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4929 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4930 24519714 2019-05-09 stsp struct got_repository *repo;
4931 24519714 2019-05-09 stsp struct got_worktree *worktree;
4932 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4933 5f8a88c6 2019-08-03 stsp int have_staged_files;
4934 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4935 24519714 2019-05-09 stsp };
4936 cf066bf8 2019-05-09 stsp
4937 c4296144 2019-05-09 stsp static const struct got_error *
4938 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4939 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4940 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4941 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4942 c4296144 2019-05-09 stsp {
4943 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4944 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4945 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4946 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4947 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4948 768aea60 2019-05-09 stsp struct stat sb;
4949 c4296144 2019-05-09 stsp
4950 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4951 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4952 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4953 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4954 5f8a88c6 2019-08-03 stsp return NULL;
4955 5f8a88c6 2019-08-03 stsp } else {
4956 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4957 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4958 c4296144 2019-05-09 stsp
4959 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4960 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4961 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4962 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4963 5f8a88c6 2019-08-03 stsp return NULL;
4964 5f8a88c6 2019-08-03 stsp }
4965 0b5cc0d6 2019-05-09 stsp
4966 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4967 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4968 036813ee 2019-05-09 stsp goto done;
4969 036813ee 2019-05-09 stsp }
4970 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4971 036813ee 2019-05-09 stsp parent_path = strdup("");
4972 69960a46 2019-05-09 stsp if (parent_path == NULL)
4973 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4974 69960a46 2019-05-09 stsp } else {
4975 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4976 69960a46 2019-05-09 stsp if (err)
4977 69960a46 2019-05-09 stsp return err;
4978 69960a46 2019-05-09 stsp }
4979 c4296144 2019-05-09 stsp
4980 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4981 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4982 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4983 c4296144 2019-05-09 stsp goto done;
4984 768aea60 2019-05-09 stsp }
4985 768aea60 2019-05-09 stsp
4986 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4987 768aea60 2019-05-09 stsp relpath) == -1) {
4988 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4989 768aea60 2019-05-09 stsp goto done;
4990 768aea60 2019-05-09 stsp }
4991 0aeb8099 2020-07-23 stsp
4992 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4993 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4994 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4995 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4996 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4997 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4998 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4999 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5000 0aeb8099 2020-07-23 stsp break;
5001 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5002 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5003 0aeb8099 2020-07-23 stsp break;
5004 0aeb8099 2020-07-23 stsp default:
5005 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5006 0aeb8099 2020-07-23 stsp goto done;
5007 0aeb8099 2020-07-23 stsp }
5008 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5009 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5010 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5011 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5012 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5013 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5014 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5015 12463d8b 2019-12-13 stsp ct->ondisk_path);
5016 12463d8b 2019-12-13 stsp goto done;
5017 12463d8b 2019-12-13 stsp }
5018 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5019 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5020 768aea60 2019-05-09 stsp goto done;
5021 768aea60 2019-05-09 stsp }
5022 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5023 c4296144 2019-05-09 stsp }
5024 c4296144 2019-05-09 stsp
5025 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5026 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5027 44d03001 2019-05-09 stsp relpath) == -1) {
5028 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5029 44d03001 2019-05-09 stsp goto done;
5030 44d03001 2019-05-09 stsp }
5031 44d03001 2019-05-09 stsp
5032 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5033 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5034 35213c7c 2020-07-23 stsp int is_bad_symlink;
5035 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5036 35213c7c 2020-07-23 stsp ssize_t target_len;
5037 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5038 35213c7c 2020-07-23 stsp sizeof(target_path));
5039 35213c7c 2020-07-23 stsp if (target_len == -1) {
5040 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5041 35213c7c 2020-07-23 stsp ct->ondisk_path);
5042 35213c7c 2020-07-23 stsp goto done;
5043 35213c7c 2020-07-23 stsp }
5044 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5045 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5046 35213c7c 2020-07-23 stsp if (err)
5047 35213c7c 2020-07-23 stsp goto done;
5048 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5049 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5050 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5051 35213c7c 2020-07-23 stsp goto done;
5052 35213c7c 2020-07-23 stsp }
5053 35213c7c 2020-07-23 stsp }
5054 35213c7c 2020-07-23 stsp
5055 35213c7c 2020-07-23 stsp
5056 cf066bf8 2019-05-09 stsp ct->status = status;
5057 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5058 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5059 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5060 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5061 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5062 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5063 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5064 b416585c 2019-05-13 stsp goto done;
5065 b416585c 2019-05-13 stsp }
5066 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5067 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5068 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5069 f0b75401 2019-08-03 stsp goto done;
5070 f0b75401 2019-08-03 stsp }
5071 f0b75401 2019-08-03 stsp }
5072 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5073 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5074 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5075 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5076 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5077 036813ee 2019-05-09 stsp goto done;
5078 036813ee 2019-05-09 stsp }
5079 ca2503ea 2019-05-09 stsp }
5080 24519714 2019-05-09 stsp ct->path = strdup(path);
5081 24519714 2019-05-09 stsp if (ct->path == NULL) {
5082 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5083 24519714 2019-05-09 stsp goto done;
5084 24519714 2019-05-09 stsp }
5085 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5086 c4296144 2019-05-09 stsp done:
5087 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5088 ed175427 2019-05-09 stsp free_commitable(ct);
5089 24519714 2019-05-09 stsp free(parent_path);
5090 036813ee 2019-05-09 stsp free(path);
5091 a129376b 2019-03-28 stsp return err;
5092 a129376b 2019-03-28 stsp }
5093 c4296144 2019-05-09 stsp
5094 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5095 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5096 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5097 036813ee 2019-05-09 stsp struct got_repository *);
5098 ed175427 2019-05-09 stsp
5099 ed175427 2019-05-09 stsp static const struct got_error *
5100 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5101 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5102 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5103 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5104 afa376bf 2019-05-09 stsp struct got_repository *repo)
5105 ed175427 2019-05-09 stsp {
5106 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5107 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5108 036813ee 2019-05-09 stsp char *subpath;
5109 ed175427 2019-05-09 stsp
5110 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5111 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5112 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5113 ed175427 2019-05-09 stsp
5114 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5115 ed175427 2019-05-09 stsp if (err)
5116 ed175427 2019-05-09 stsp return err;
5117 ed175427 2019-05-09 stsp
5118 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5119 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5120 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5121 036813ee 2019-05-09 stsp free(subpath);
5122 036813ee 2019-05-09 stsp return err;
5123 036813ee 2019-05-09 stsp }
5124 ed175427 2019-05-09 stsp
5125 036813ee 2019-05-09 stsp static const struct got_error *
5126 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5127 036813ee 2019-05-09 stsp {
5128 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5129 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5130 ed175427 2019-05-09 stsp
5131 036813ee 2019-05-09 stsp *match = 0;
5132 ed175427 2019-05-09 stsp
5133 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5134 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5135 0f63689d 2019-05-10 stsp return NULL;
5136 036813ee 2019-05-09 stsp }
5137 0b5cc0d6 2019-05-09 stsp
5138 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5139 0f63689d 2019-05-10 stsp if (err)
5140 0f63689d 2019-05-10 stsp return err;
5141 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5142 036813ee 2019-05-09 stsp free(ct_parent_path);
5143 036813ee 2019-05-09 stsp return err;
5144 036813ee 2019-05-09 stsp }
5145 0b5cc0d6 2019-05-09 stsp
5146 768aea60 2019-05-09 stsp static mode_t
5147 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5148 768aea60 2019-05-09 stsp {
5149 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5150 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5151 3d9a4ec4 2020-07-23 stsp
5152 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5153 768aea60 2019-05-09 stsp }
5154 768aea60 2019-05-09 stsp
5155 036813ee 2019-05-09 stsp static const struct got_error *
5156 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5157 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5158 036813ee 2019-05-09 stsp {
5159 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5160 ca2503ea 2019-05-09 stsp
5161 036813ee 2019-05-09 stsp *new_te = NULL;
5162 0b5cc0d6 2019-05-09 stsp
5163 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5164 036813ee 2019-05-09 stsp if (err)
5165 036813ee 2019-05-09 stsp goto done;
5166 0b5cc0d6 2019-05-09 stsp
5167 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5168 036813ee 2019-05-09 stsp
5169 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5170 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5171 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5172 5f8a88c6 2019-08-03 stsp else
5173 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5174 036813ee 2019-05-09 stsp done:
5175 036813ee 2019-05-09 stsp if (err && *new_te) {
5176 56e0773d 2019-11-28 stsp free(*new_te);
5177 036813ee 2019-05-09 stsp *new_te = NULL;
5178 036813ee 2019-05-09 stsp }
5179 036813ee 2019-05-09 stsp return err;
5180 036813ee 2019-05-09 stsp }
5181 036813ee 2019-05-09 stsp
5182 036813ee 2019-05-09 stsp static const struct got_error *
5183 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5184 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5185 036813ee 2019-05-09 stsp {
5186 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5187 102b254e 2020-10-19 stsp char *ct_name = NULL;
5188 036813ee 2019-05-09 stsp
5189 036813ee 2019-05-09 stsp *new_te = NULL;
5190 036813ee 2019-05-09 stsp
5191 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5192 036813ee 2019-05-09 stsp if (*new_te == NULL)
5193 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5194 036813ee 2019-05-09 stsp
5195 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5196 102b254e 2020-10-19 stsp if (err)
5197 036813ee 2019-05-09 stsp goto done;
5198 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5199 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5200 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5201 036813ee 2019-05-09 stsp goto done;
5202 036813ee 2019-05-09 stsp }
5203 036813ee 2019-05-09 stsp
5204 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5205 036813ee 2019-05-09 stsp
5206 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5207 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5208 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5209 5f8a88c6 2019-08-03 stsp else
5210 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5211 036813ee 2019-05-09 stsp done:
5212 102b254e 2020-10-19 stsp free(ct_name);
5213 036813ee 2019-05-09 stsp if (err && *new_te) {
5214 56e0773d 2019-11-28 stsp free(*new_te);
5215 036813ee 2019-05-09 stsp *new_te = NULL;
5216 036813ee 2019-05-09 stsp }
5217 036813ee 2019-05-09 stsp return err;
5218 036813ee 2019-05-09 stsp }
5219 036813ee 2019-05-09 stsp
5220 036813ee 2019-05-09 stsp static const struct got_error *
5221 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5222 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5223 036813ee 2019-05-09 stsp {
5224 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5225 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5226 036813ee 2019-05-09 stsp
5227 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5228 036813ee 2019-05-09 stsp if (err)
5229 036813ee 2019-05-09 stsp return err;
5230 036813ee 2019-05-09 stsp if (new_pe == NULL)
5231 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5232 036813ee 2019-05-09 stsp return NULL;
5233 afa376bf 2019-05-09 stsp }
5234 afa376bf 2019-05-09 stsp
5235 afa376bf 2019-05-09 stsp static const struct got_error *
5236 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5237 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5238 afa376bf 2019-05-09 stsp {
5239 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5240 5f8a88c6 2019-08-03 stsp unsigned char status;
5241 ae1e948a 2021-09-28 thomas
5242 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5243 ae1e948a 2021-09-28 thomas return NULL;
5244 5f8a88c6 2019-08-03 stsp
5245 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5246 afa376bf 2019-05-09 stsp ct_path++;
5247 5f8a88c6 2019-08-03 stsp
5248 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5249 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5250 5f8a88c6 2019-08-03 stsp else
5251 5f8a88c6 2019-08-03 stsp status = ct->status;
5252 5f8a88c6 2019-08-03 stsp
5253 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5254 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5255 036813ee 2019-05-09 stsp }
5256 036813ee 2019-05-09 stsp
5257 036813ee 2019-05-09 stsp static const struct got_error *
5258 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5259 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5260 44d03001 2019-05-09 stsp {
5261 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5262 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5263 44d03001 2019-05-09 stsp char *te_path;
5264 44d03001 2019-05-09 stsp
5265 44d03001 2019-05-09 stsp *modified = 0;
5266 44d03001 2019-05-09 stsp
5267 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5268 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5269 44d03001 2019-05-09 stsp te->name) == -1)
5270 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5271 44d03001 2019-05-09 stsp
5272 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5273 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5274 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5275 44d03001 2019-05-09 stsp strlen(te_path));
5276 62d463ca 2020-10-20 naddy if (*modified)
5277 44d03001 2019-05-09 stsp break;
5278 44d03001 2019-05-09 stsp }
5279 44d03001 2019-05-09 stsp
5280 44d03001 2019-05-09 stsp free(te_path);
5281 44d03001 2019-05-09 stsp return err;
5282 44d03001 2019-05-09 stsp }
5283 44d03001 2019-05-09 stsp
5284 44d03001 2019-05-09 stsp static const struct got_error *
5285 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5286 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5287 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5288 036813ee 2019-05-09 stsp {
5289 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5290 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5291 036813ee 2019-05-09 stsp
5292 036813ee 2019-05-09 stsp *ctp = NULL;
5293 036813ee 2019-05-09 stsp
5294 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5295 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5296 036813ee 2019-05-09 stsp char *ct_name = NULL;
5297 036813ee 2019-05-09 stsp int path_matches;
5298 036813ee 2019-05-09 stsp
5299 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5300 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5301 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5302 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5303 5f8a88c6 2019-08-03 stsp continue;
5304 5f8a88c6 2019-08-03 stsp } else {
5305 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5306 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5307 5f8a88c6 2019-08-03 stsp continue;
5308 5f8a88c6 2019-08-03 stsp }
5309 036813ee 2019-05-09 stsp
5310 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5311 036813ee 2019-05-09 stsp continue;
5312 036813ee 2019-05-09 stsp
5313 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5314 62d463ca 2020-10-20 naddy if (err)
5315 036813ee 2019-05-09 stsp return err;
5316 036813ee 2019-05-09 stsp if (!path_matches)
5317 036813ee 2019-05-09 stsp continue;
5318 036813ee 2019-05-09 stsp
5319 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5320 d34b633e 2020-10-19 stsp if (err)
5321 d34b633e 2020-10-19 stsp return err;
5322 d34b633e 2020-10-19 stsp
5323 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5324 d34b633e 2020-10-19 stsp free(ct_name);
5325 036813ee 2019-05-09 stsp continue;
5326 d34b633e 2020-10-19 stsp }
5327 d34b633e 2020-10-19 stsp free(ct_name);
5328 036813ee 2019-05-09 stsp
5329 036813ee 2019-05-09 stsp *ctp = ct;
5330 036813ee 2019-05-09 stsp break;
5331 036813ee 2019-05-09 stsp }
5332 2b496619 2019-07-10 stsp
5333 2b496619 2019-07-10 stsp return err;
5334 2b496619 2019-07-10 stsp }
5335 2b496619 2019-07-10 stsp
5336 2b496619 2019-07-10 stsp static const struct got_error *
5337 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5338 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5339 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5340 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5341 2b496619 2019-07-10 stsp struct got_repository *repo)
5342 2b496619 2019-07-10 stsp {
5343 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5344 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5345 2b496619 2019-07-10 stsp char *subtree_path;
5346 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5347 ba580f68 2020-03-22 stsp int nentries;
5348 2b496619 2019-07-10 stsp
5349 2b496619 2019-07-10 stsp *new_tep = NULL;
5350 2b496619 2019-07-10 stsp
5351 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5352 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5353 2b496619 2019-07-10 stsp child_path) == -1)
5354 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5355 036813ee 2019-05-09 stsp
5356 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5357 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5358 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5359 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5360 56e0773d 2019-11-28 stsp
5361 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5362 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5363 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5364 2b496619 2019-07-10 stsp goto done;
5365 2b496619 2019-07-10 stsp }
5366 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5367 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5368 2b496619 2019-07-10 stsp if (err) {
5369 56e0773d 2019-11-28 stsp free(new_te);
5370 2b496619 2019-07-10 stsp goto done;
5371 2b496619 2019-07-10 stsp }
5372 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5373 2b496619 2019-07-10 stsp done:
5374 56e0773d 2019-11-28 stsp free(id);
5375 2b496619 2019-07-10 stsp free(subtree_path);
5376 2b496619 2019-07-10 stsp if (err == NULL)
5377 2b496619 2019-07-10 stsp *new_tep = new_te;
5378 036813ee 2019-05-09 stsp return err;
5379 036813ee 2019-05-09 stsp }
5380 036813ee 2019-05-09 stsp
5381 036813ee 2019-05-09 stsp static const struct got_error *
5382 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5383 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5384 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5385 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5386 036813ee 2019-05-09 stsp struct got_repository *repo)
5387 036813ee 2019-05-09 stsp {
5388 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5389 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5390 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5391 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5392 036813ee 2019-05-09 stsp
5393 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5394 ba580f68 2020-03-22 stsp *nentries = 0;
5395 036813ee 2019-05-09 stsp
5396 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5397 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5398 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5399 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5400 036813ee 2019-05-09 stsp
5401 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5402 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5403 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5404 036813ee 2019-05-09 stsp continue;
5405 036813ee 2019-05-09 stsp
5406 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5407 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5408 036813ee 2019-05-09 stsp continue;
5409 036813ee 2019-05-09 stsp
5410 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5411 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5412 036813ee 2019-05-09 stsp if (err)
5413 036813ee 2019-05-09 stsp goto done;
5414 036813ee 2019-05-09 stsp
5415 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5416 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5417 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5418 036813ee 2019-05-09 stsp if (err)
5419 036813ee 2019-05-09 stsp goto done;
5420 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5421 afa376bf 2019-05-09 stsp if (err)
5422 afa376bf 2019-05-09 stsp goto done;
5423 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5424 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5425 2b496619 2019-07-10 stsp if (err)
5426 2f51b5b3 2019-05-09 stsp goto done;
5427 ba580f68 2020-03-22 stsp (*nentries)++;
5428 2b496619 2019-07-10 stsp } else {
5429 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5430 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5431 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5432 2b496619 2019-07-10 stsp == NULL) {
5433 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5434 2b496619 2019-07-10 stsp child_path, path_base_tree,
5435 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5436 2b496619 2019-07-10 stsp repo);
5437 2b496619 2019-07-10 stsp if (err)
5438 2b496619 2019-07-10 stsp goto done;
5439 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5440 2b496619 2019-07-10 stsp if (err)
5441 2b496619 2019-07-10 stsp goto done;
5442 ba580f68 2020-03-22 stsp (*nentries)++;
5443 9ba0479c 2019-05-10 stsp }
5444 ed175427 2019-05-09 stsp }
5445 2f51b5b3 2019-05-09 stsp }
5446 2f51b5b3 2019-05-09 stsp
5447 2f51b5b3 2019-05-09 stsp if (base_tree) {
5448 56e0773d 2019-11-28 stsp int i, nbase_entries;
5449 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5450 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5451 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5452 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5453 63c5ca5d 2019-08-24 stsp
5454 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5455 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5456 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5457 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5458 63c5ca5d 2019-08-24 stsp if (err)
5459 63c5ca5d 2019-08-24 stsp goto done;
5460 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5461 63c5ca5d 2019-08-24 stsp if (err)
5462 63c5ca5d 2019-08-24 stsp goto done;
5463 ba580f68 2020-03-22 stsp (*nentries)++;
5464 63c5ca5d 2019-08-24 stsp continue;
5465 63c5ca5d 2019-08-24 stsp }
5466 2f51b5b3 2019-05-09 stsp
5467 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5468 44d03001 2019-05-09 stsp int modified;
5469 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5470 036813ee 2019-05-09 stsp if (err)
5471 036813ee 2019-05-09 stsp goto done;
5472 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5473 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5474 2f51b5b3 2019-05-09 stsp if (err)
5475 2f51b5b3 2019-05-09 stsp goto done;
5476 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5477 44d03001 2019-05-09 stsp if (modified) {
5478 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5479 ba580f68 2020-03-22 stsp int nsubentries;
5480 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5481 ba580f68 2020-03-22 stsp &nsubentries, te,
5482 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5483 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5484 44d03001 2019-05-09 stsp if (err)
5485 44d03001 2019-05-09 stsp goto done;
5486 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5487 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5488 ba580f68 2020-03-22 stsp free(new_id);
5489 ba580f68 2020-03-22 stsp continue;
5490 ba580f68 2020-03-22 stsp }
5491 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5492 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5493 56e0773d 2019-11-28 stsp free(new_id);
5494 44d03001 2019-05-09 stsp }
5495 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5496 036813ee 2019-05-09 stsp if (err)
5497 036813ee 2019-05-09 stsp goto done;
5498 ba580f68 2020-03-22 stsp (*nentries)++;
5499 2f51b5b3 2019-05-09 stsp continue;
5500 036813ee 2019-05-09 stsp }
5501 2f51b5b3 2019-05-09 stsp
5502 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5503 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5504 f66c734c 2019-09-22 stsp if (err)
5505 f66c734c 2019-09-22 stsp goto done;
5506 2f51b5b3 2019-05-09 stsp if (ct) {
5507 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5508 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5509 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5510 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5511 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5512 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5513 2f51b5b3 2019-05-09 stsp if (err)
5514 2f51b5b3 2019-05-09 stsp goto done;
5515 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5516 2f51b5b3 2019-05-09 stsp if (err)
5517 2f51b5b3 2019-05-09 stsp goto done;
5518 ba580f68 2020-03-22 stsp (*nentries)++;
5519 2f51b5b3 2019-05-09 stsp }
5520 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5521 b416585c 2019-05-13 stsp status_arg);
5522 afa376bf 2019-05-09 stsp if (err)
5523 afa376bf 2019-05-09 stsp goto done;
5524 2f51b5b3 2019-05-09 stsp } else {
5525 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5526 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5527 2f51b5b3 2019-05-09 stsp if (err)
5528 2f51b5b3 2019-05-09 stsp goto done;
5529 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5530 2f51b5b3 2019-05-09 stsp if (err)
5531 2f51b5b3 2019-05-09 stsp goto done;
5532 ba580f68 2020-03-22 stsp (*nentries)++;
5533 2f51b5b3 2019-05-09 stsp }
5534 ca2503ea 2019-05-09 stsp }
5535 ed175427 2019-05-09 stsp }
5536 0b5cc0d6 2019-05-09 stsp
5537 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5538 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5539 ed175427 2019-05-09 stsp done:
5540 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5541 2e1fa222 2020-07-23 stsp return err;
5542 2e1fa222 2020-07-23 stsp }
5543 2e1fa222 2020-07-23 stsp
5544 2e1fa222 2020-07-23 stsp static const struct got_error *
5545 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5546 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5547 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5548 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5549 ebf99748 2019-05-09 stsp {
5550 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5551 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5552 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5553 ebf99748 2019-05-09 stsp
5554 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5555 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5556 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5557 ebf99748 2019-05-09 stsp
5558 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5559 437adc9d 2020-12-10 yzhong
5560 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5561 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5562 437adc9d 2020-12-10 yzhong if (err)
5563 437adc9d 2020-12-10 yzhong goto done;
5564 437adc9d 2020-12-10 yzhong
5565 ebf99748 2019-05-09 stsp if (ie) {
5566 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5567 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5568 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5569 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5570 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5571 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5572 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5573 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5574 437adc9d 2020-12-10 yzhong
5575 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5576 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5577 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5578 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5579 72fd46fa 2019-09-06 stsp !have_staged_files);
5580 ebf99748 2019-05-09 stsp } else
5581 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5582 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5583 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5584 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5585 72fd46fa 2019-09-06 stsp !have_staged_files);
5586 ebf99748 2019-05-09 stsp } else {
5587 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5588 ebf99748 2019-05-09 stsp if (err)
5589 437adc9d 2020-12-10 yzhong goto done;
5590 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5591 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5592 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5593 3969253a 2020-03-07 stsp if (err) {
5594 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5595 437adc9d 2020-12-10 yzhong goto done;
5596 3969253a 2020-03-07 stsp }
5597 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5598 3969253a 2020-03-07 stsp if (err) {
5599 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5600 437adc9d 2020-12-10 yzhong goto done;
5601 3969253a 2020-03-07 stsp }
5602 ebf99748 2019-05-09 stsp }
5603 437adc9d 2020-12-10 yzhong free(relpath);
5604 437adc9d 2020-12-10 yzhong relpath = NULL;
5605 ebf99748 2019-05-09 stsp }
5606 437adc9d 2020-12-10 yzhong done:
5607 437adc9d 2020-12-10 yzhong free(relpath);
5608 d56d26ce 2019-05-10 stsp return err;
5609 d56d26ce 2019-05-10 stsp }
5610 735ef5ac 2019-08-03 stsp
5611 d56d26ce 2019-05-10 stsp
5612 d56d26ce 2019-05-10 stsp static const struct got_error *
5613 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5614 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5615 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5616 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5617 735ef5ac 2019-08-03 stsp int ood_errcode)
5618 d56d26ce 2019-05-10 stsp {
5619 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5620 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5621 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5622 1a36436d 2019-06-10 stsp
5623 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5624 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5625 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5626 1a36436d 2019-06-10 stsp return NULL;
5627 9bead371 2019-07-28 stsp /*
5628 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5629 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5630 9bead371 2019-07-28 stsp */
5631 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5632 945f9229 2022-04-16 thomas if (err)
5633 945f9229 2022-04-16 thomas goto done;
5634 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5635 9bead371 2019-07-28 stsp if (err) {
5636 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5637 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5638 1a36436d 2019-06-10 stsp goto done;
5639 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5640 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5641 1a36436d 2019-06-10 stsp } else {
5642 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5643 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5644 945f9229 2022-04-16 thomas if (err)
5645 945f9229 2022-04-16 thomas goto done;
5646 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5647 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5648 1a36436d 2019-06-10 stsp goto done;
5649 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5650 1a36436d 2019-06-10 stsp }
5651 1a36436d 2019-06-10 stsp done:
5652 1a36436d 2019-06-10 stsp free(id);
5653 945f9229 2022-04-16 thomas if (commit)
5654 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5655 1a36436d 2019-06-10 stsp return err;
5656 ebf99748 2019-05-09 stsp }
5657 ebf99748 2019-05-09 stsp
5658 ef20f542 2022-06-26 thomas static const struct got_error *
5659 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5660 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5661 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5662 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5663 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5664 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5665 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5666 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5667 afa376bf 2019-05-09 stsp struct got_repository *repo)
5668 c4296144 2019-05-09 stsp {
5669 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5670 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5671 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5672 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5673 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5674 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5675 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5676 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5677 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5678 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5679 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5680 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5681 c4296144 2019-05-09 stsp
5682 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5683 c4296144 2019-05-09 stsp
5684 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5685 675c7539 2019-05-09 stsp
5686 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5687 588edf97 2019-05-10 stsp if (err)
5688 588edf97 2019-05-10 stsp goto done;
5689 de18fc63 2019-05-09 stsp
5690 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5691 588edf97 2019-05-10 stsp if (err)
5692 588edf97 2019-05-10 stsp goto done;
5693 588edf97 2019-05-10 stsp
5694 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5695 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5696 33ad4cbe 2019-05-12 jcs if (err)
5697 33ad4cbe 2019-05-12 jcs goto done;
5698 33ad4cbe 2019-05-12 jcs }
5699 c4296144 2019-05-09 stsp
5700 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5701 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5702 33ad4cbe 2019-05-12 jcs goto done;
5703 33ad4cbe 2019-05-12 jcs }
5704 33ad4cbe 2019-05-12 jcs
5705 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5706 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5707 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5708 cf066bf8 2019-05-09 stsp char *ondisk_path;
5709 cf066bf8 2019-05-09 stsp
5710 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5711 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5712 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5713 5f8a88c6 2019-08-03 stsp continue;
5714 5f8a88c6 2019-08-03 stsp
5715 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5716 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5717 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5718 cf066bf8 2019-05-09 stsp continue;
5719 cf066bf8 2019-05-09 stsp
5720 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5721 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5722 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5723 cf066bf8 2019-05-09 stsp goto done;
5724 cf066bf8 2019-05-09 stsp }
5725 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5726 2e1fa222 2020-07-23 stsp free(ondisk_path);
5727 2e1fa222 2020-07-23 stsp if (err)
5728 cf066bf8 2019-05-09 stsp goto done;
5729 cf066bf8 2019-05-09 stsp }
5730 036813ee 2019-05-09 stsp
5731 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5732 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5733 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5734 036813ee 2019-05-09 stsp if (err)
5735 036813ee 2019-05-09 stsp goto done;
5736 036813ee 2019-05-09 stsp
5737 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5738 de18fc63 2019-05-09 stsp if (err)
5739 de18fc63 2019-05-09 stsp goto done;
5740 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5741 10604dce 2021-09-24 thomas nparents++;
5742 10604dce 2021-09-24 thomas if (parent_id2) {
5743 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5744 10604dce 2021-09-24 thomas if (err)
5745 10604dce 2021-09-24 thomas goto done;
5746 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5747 10604dce 2021-09-24 thomas nparents++;
5748 10604dce 2021-09-24 thomas }
5749 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5750 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5751 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5752 33ad4cbe 2019-05-12 jcs free(logmsg);
5753 9d40349a 2019-05-09 stsp if (err)
5754 9d40349a 2019-05-09 stsp goto done;
5755 9d40349a 2019-05-09 stsp
5756 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5757 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5758 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5759 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5760 09f5bd90 2019-05-09 stsp goto done;
5761 09f5bd90 2019-05-09 stsp }
5762 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5763 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5764 09f5bd90 2019-05-09 stsp if (err)
5765 09f5bd90 2019-05-09 stsp goto done;
5766 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5767 ebf99748 2019-05-09 stsp if (err)
5768 ebf99748 2019-05-09 stsp goto done;
5769 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5770 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5771 09f5bd90 2019-05-09 stsp goto done;
5772 09f5bd90 2019-05-09 stsp }
5773 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5774 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5775 f2c16586 2019-05-09 stsp if (err)
5776 f2c16586 2019-05-09 stsp goto done;
5777 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5778 f2c16586 2019-05-09 stsp if (err)
5779 f2c16586 2019-05-09 stsp goto done;
5780 f2c16586 2019-05-09 stsp
5781 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5782 09f5bd90 2019-05-09 stsp if (err)
5783 09f5bd90 2019-05-09 stsp goto done;
5784 09f5bd90 2019-05-09 stsp
5785 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5786 ebf99748 2019-05-09 stsp if (err)
5787 ebf99748 2019-05-09 stsp goto done;
5788 c4296144 2019-05-09 stsp done:
5789 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5790 588edf97 2019-05-10 stsp if (head_tree)
5791 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5792 588edf97 2019-05-10 stsp if (head_commit)
5793 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5794 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5795 2f17228e 2019-05-12 stsp if (head_ref2) {
5796 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5797 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5798 2f17228e 2019-05-12 stsp err = unlockerr;
5799 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5800 39cd0ff6 2019-07-12 stsp }
5801 39cd0ff6 2019-07-12 stsp return err;
5802 39cd0ff6 2019-07-12 stsp }
5803 5c1e53bc 2019-07-28 stsp
5804 5c1e53bc 2019-07-28 stsp static const struct got_error *
5805 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5806 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5807 5c1e53bc 2019-07-28 stsp {
5808 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5809 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5810 39cd0ff6 2019-07-12 stsp
5811 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5812 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5813 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5814 5c1e53bc 2019-07-28 stsp
5815 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5816 5c1e53bc 2019-07-28 stsp ct_path++;
5817 5c1e53bc 2019-07-28 stsp
5818 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5819 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5820 5c1e53bc 2019-07-28 stsp break;
5821 5c1e53bc 2019-07-28 stsp }
5822 5c1e53bc 2019-07-28 stsp
5823 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5824 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5825 5c1e53bc 2019-07-28 stsp
5826 5c1e53bc 2019-07-28 stsp return NULL;
5827 5c1e53bc 2019-07-28 stsp }
5828 5c1e53bc 2019-07-28 stsp
5829 f0b75401 2019-08-03 stsp static const struct got_error *
5830 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5831 f0b75401 2019-08-03 stsp {
5832 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5833 f0b75401 2019-08-03 stsp
5834 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5835 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5836 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5837 f0b75401 2019-08-03 stsp }
5838 f0b75401 2019-08-03 stsp
5839 f0b75401 2019-08-03 stsp return NULL;
5840 f0b75401 2019-08-03 stsp }
5841 f0b75401 2019-08-03 stsp
5842 5f8a88c6 2019-08-03 stsp static const struct got_error *
5843 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5844 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5845 5f8a88c6 2019-08-03 stsp {
5846 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5847 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5848 5f8a88c6 2019-08-03 stsp
5849 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5850 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5851 5f8a88c6 2019-08-03 stsp continue;
5852 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5853 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5854 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5855 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5856 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5857 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5858 5f8a88c6 2019-08-03 stsp }
5859 5f8a88c6 2019-08-03 stsp
5860 5f8a88c6 2019-08-03 stsp return NULL;
5861 5f8a88c6 2019-08-03 stsp }
5862 5f8a88c6 2019-08-03 stsp
5863 39cd0ff6 2019-07-12 stsp const struct got_error *
5864 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5865 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5866 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5867 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5868 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5869 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5870 39cd0ff6 2019-07-12 stsp {
5871 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5872 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5873 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5874 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5875 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5876 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5877 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5878 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5879 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5880 39cd0ff6 2019-07-12 stsp
5881 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5882 39cd0ff6 2019-07-12 stsp
5883 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5884 39cd0ff6 2019-07-12 stsp
5885 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5886 39cd0ff6 2019-07-12 stsp if (err)
5887 39cd0ff6 2019-07-12 stsp goto done;
5888 39cd0ff6 2019-07-12 stsp
5889 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5890 39cd0ff6 2019-07-12 stsp if (err)
5891 39cd0ff6 2019-07-12 stsp goto done;
5892 39cd0ff6 2019-07-12 stsp
5893 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5894 39cd0ff6 2019-07-12 stsp if (err)
5895 39cd0ff6 2019-07-12 stsp goto done;
5896 39cd0ff6 2019-07-12 stsp
5897 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5898 39cd0ff6 2019-07-12 stsp if (err)
5899 39cd0ff6 2019-07-12 stsp goto done;
5900 39cd0ff6 2019-07-12 stsp
5901 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5902 5f8a88c6 2019-08-03 stsp &have_staged_files);
5903 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5904 5f8a88c6 2019-08-03 stsp goto done;
5905 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5906 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5907 5f8a88c6 2019-08-03 stsp if (err)
5908 5f8a88c6 2019-08-03 stsp goto done;
5909 5f8a88c6 2019-08-03 stsp }
5910 5f8a88c6 2019-08-03 stsp
5911 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5912 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5913 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5914 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5915 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5916 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5917 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5918 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5919 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5920 5c1e53bc 2019-07-28 stsp if (err)
5921 5c1e53bc 2019-07-28 stsp goto done;
5922 5c1e53bc 2019-07-28 stsp }
5923 39cd0ff6 2019-07-12 stsp
5924 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5925 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5926 39cd0ff6 2019-07-12 stsp goto done;
5927 5c1e53bc 2019-07-28 stsp }
5928 5c1e53bc 2019-07-28 stsp
5929 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5930 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5931 5c1e53bc 2019-07-28 stsp if (err)
5932 5c1e53bc 2019-07-28 stsp goto done;
5933 39cd0ff6 2019-07-12 stsp }
5934 f0b75401 2019-08-03 stsp
5935 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5936 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5937 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5938 f0b75401 2019-08-03 stsp
5939 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5940 f0b75401 2019-08-03 stsp ct_path++;
5941 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5942 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5943 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5944 b50cabdf 2019-07-12 stsp if (err)
5945 b50cabdf 2019-07-12 stsp goto done;
5946 f0b75401 2019-08-03 stsp
5947 b50cabdf 2019-07-12 stsp }
5948 b50cabdf 2019-07-12 stsp
5949 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5950 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5951 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5952 39cd0ff6 2019-07-12 stsp if (err)
5953 39cd0ff6 2019-07-12 stsp goto done;
5954 39cd0ff6 2019-07-12 stsp
5955 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5956 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5957 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5958 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5959 39cd0ff6 2019-07-12 stsp err = sync_err;
5960 39cd0ff6 2019-07-12 stsp done:
5961 39cd0ff6 2019-07-12 stsp if (fileindex)
5962 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5963 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5964 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5965 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5966 39cd0ff6 2019-07-12 stsp err = unlockerr;
5967 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5968 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5969 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5970 39cd0ff6 2019-07-12 stsp }
5971 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5972 c4296144 2019-05-09 stsp return err;
5973 8656d6c4 2019-05-20 stsp }
5974 8656d6c4 2019-05-20 stsp
5975 8656d6c4 2019-05-20 stsp const char *
5976 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5977 8656d6c4 2019-05-20 stsp {
5978 8656d6c4 2019-05-20 stsp return ct->path;
5979 8656d6c4 2019-05-20 stsp }
5980 8656d6c4 2019-05-20 stsp
5981 8656d6c4 2019-05-20 stsp unsigned int
5982 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5983 8656d6c4 2019-05-20 stsp {
5984 8656d6c4 2019-05-20 stsp return ct->status;
5985 818c7501 2019-07-11 stsp }
5986 818c7501 2019-07-11 stsp
5987 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5988 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5989 818c7501 2019-07-11 stsp struct got_repository *repo;
5990 818c7501 2019-07-11 stsp };
5991 818c7501 2019-07-11 stsp
5992 818c7501 2019-07-11 stsp static const struct got_error *
5993 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5994 818c7501 2019-07-11 stsp {
5995 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5996 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5997 818c7501 2019-07-11 stsp unsigned char status;
5998 818c7501 2019-07-11 stsp struct stat sb;
5999 818c7501 2019-07-11 stsp char *ondisk_path;
6000 818c7501 2019-07-11 stsp
6001 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6002 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6003 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6004 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6005 818c7501 2019-07-11 stsp
6006 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6007 818c7501 2019-07-11 stsp == -1)
6008 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6009 818c7501 2019-07-11 stsp
6010 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6011 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6012 818c7501 2019-07-11 stsp free(ondisk_path);
6013 818c7501 2019-07-11 stsp if (err)
6014 818c7501 2019-07-11 stsp return err;
6015 818c7501 2019-07-11 stsp
6016 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6017 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6018 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6019 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6020 818c7501 2019-07-11 stsp
6021 818c7501 2019-07-11 stsp return NULL;
6022 818c7501 2019-07-11 stsp }
6023 818c7501 2019-07-11 stsp
6024 818c7501 2019-07-11 stsp const struct got_error *
6025 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6026 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6027 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6028 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6029 818c7501 2019-07-11 stsp {
6030 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6031 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6032 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6033 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6034 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6035 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6036 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6037 818c7501 2019-07-11 stsp
6038 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6039 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6040 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6041 818c7501 2019-07-11 stsp
6042 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6043 818c7501 2019-07-11 stsp if (err)
6044 818c7501 2019-07-11 stsp return err;
6045 818c7501 2019-07-11 stsp
6046 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6047 818c7501 2019-07-11 stsp if (err)
6048 818c7501 2019-07-11 stsp goto done;
6049 818c7501 2019-07-11 stsp
6050 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6051 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6052 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6053 818c7501 2019-07-11 stsp &ok_arg);
6054 818c7501 2019-07-11 stsp if (err)
6055 818c7501 2019-07-11 stsp goto done;
6056 818c7501 2019-07-11 stsp
6057 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6058 818c7501 2019-07-11 stsp if (err)
6059 818c7501 2019-07-11 stsp goto done;
6060 818c7501 2019-07-11 stsp
6061 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6062 818c7501 2019-07-11 stsp if (err)
6063 818c7501 2019-07-11 stsp goto done;
6064 818c7501 2019-07-11 stsp
6065 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6066 818c7501 2019-07-11 stsp if (err)
6067 818c7501 2019-07-11 stsp goto done;
6068 818c7501 2019-07-11 stsp
6069 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6070 818c7501 2019-07-11 stsp 0);
6071 e51d7b55 2020-01-04 stsp if (err)
6072 e51d7b55 2020-01-04 stsp goto done;
6073 e51d7b55 2020-01-04 stsp
6074 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6075 818c7501 2019-07-11 stsp if (err)
6076 818c7501 2019-07-11 stsp goto done;
6077 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6078 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6079 e51d7b55 2020-01-04 stsp goto done;
6080 e51d7b55 2020-01-04 stsp }
6081 818c7501 2019-07-11 stsp
6082 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6083 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6084 818c7501 2019-07-11 stsp if (err)
6085 818c7501 2019-07-11 stsp goto done;
6086 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6087 818c7501 2019-07-11 stsp if (err)
6088 818c7501 2019-07-11 stsp goto done;
6089 818c7501 2019-07-11 stsp
6090 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6091 818c7501 2019-07-11 stsp
6092 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6093 818c7501 2019-07-11 stsp if (err)
6094 818c7501 2019-07-11 stsp goto done;
6095 818c7501 2019-07-11 stsp
6096 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6097 818c7501 2019-07-11 stsp if (err)
6098 818c7501 2019-07-11 stsp goto done;
6099 818c7501 2019-07-11 stsp
6100 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6101 818c7501 2019-07-11 stsp worktree->base_commit_id);
6102 818c7501 2019-07-11 stsp if (err)
6103 818c7501 2019-07-11 stsp goto done;
6104 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6105 818c7501 2019-07-11 stsp if (err)
6106 818c7501 2019-07-11 stsp goto done;
6107 818c7501 2019-07-11 stsp
6108 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6109 818c7501 2019-07-11 stsp if (err)
6110 818c7501 2019-07-11 stsp goto done;
6111 818c7501 2019-07-11 stsp done:
6112 818c7501 2019-07-11 stsp free(fileindex_path);
6113 818c7501 2019-07-11 stsp free(tmp_branch_name);
6114 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6115 818c7501 2019-07-11 stsp free(branch_ref_name);
6116 818c7501 2019-07-11 stsp if (branch_ref)
6117 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6118 818c7501 2019-07-11 stsp if (wt_branch)
6119 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6120 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6121 818c7501 2019-07-11 stsp if (err) {
6122 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6123 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6124 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6125 818c7501 2019-07-11 stsp }
6126 818c7501 2019-07-11 stsp if (*tmp_branch) {
6127 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6128 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6129 818c7501 2019-07-11 stsp }
6130 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6131 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6132 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6133 3e3a69f1 2019-07-25 stsp }
6134 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6135 818c7501 2019-07-11 stsp }
6136 818c7501 2019-07-11 stsp return err;
6137 818c7501 2019-07-11 stsp }
6138 818c7501 2019-07-11 stsp
6139 818c7501 2019-07-11 stsp const struct got_error *
6140 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6141 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6142 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6143 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6144 818c7501 2019-07-11 stsp {
6145 818c7501 2019-07-11 stsp const struct got_error *err;
6146 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6147 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6148 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6149 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6150 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6151 818c7501 2019-07-11 stsp
6152 818c7501 2019-07-11 stsp *commit_id = NULL;
6153 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6154 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6155 3e3a69f1 2019-07-25 stsp *branch = NULL;
6156 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6157 3e3a69f1 2019-07-25 stsp
6158 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6159 3e3a69f1 2019-07-25 stsp if (err)
6160 3e3a69f1 2019-07-25 stsp return err;
6161 818c7501 2019-07-11 stsp
6162 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6163 3e3a69f1 2019-07-25 stsp if (err)
6164 f032f1f7 2019-08-04 stsp goto done;
6165 f032f1f7 2019-08-04 stsp
6166 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6167 f032f1f7 2019-08-04 stsp &have_staged_files);
6168 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6169 f032f1f7 2019-08-04 stsp goto done;
6170 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6171 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6172 3e3a69f1 2019-07-25 stsp goto done;
6173 f032f1f7 2019-08-04 stsp }
6174 3e3a69f1 2019-07-25 stsp
6175 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6176 818c7501 2019-07-11 stsp if (err)
6177 f032f1f7 2019-08-04 stsp goto done;
6178 818c7501 2019-07-11 stsp
6179 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6180 818c7501 2019-07-11 stsp if (err)
6181 818c7501 2019-07-11 stsp goto done;
6182 818c7501 2019-07-11 stsp
6183 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6184 818c7501 2019-07-11 stsp if (err)
6185 818c7501 2019-07-11 stsp goto done;
6186 818c7501 2019-07-11 stsp
6187 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6188 818c7501 2019-07-11 stsp if (err)
6189 818c7501 2019-07-11 stsp goto done;
6190 818c7501 2019-07-11 stsp
6191 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6192 818c7501 2019-07-11 stsp if (err)
6193 818c7501 2019-07-11 stsp goto done;
6194 818c7501 2019-07-11 stsp
6195 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6196 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6197 818c7501 2019-07-11 stsp if (err)
6198 818c7501 2019-07-11 stsp goto done;
6199 818c7501 2019-07-11 stsp
6200 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6201 818c7501 2019-07-11 stsp if (err)
6202 818c7501 2019-07-11 stsp goto done;
6203 818c7501 2019-07-11 stsp
6204 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6205 818c7501 2019-07-11 stsp if (err)
6206 818c7501 2019-07-11 stsp goto done;
6207 818c7501 2019-07-11 stsp
6208 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6209 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6210 818c7501 2019-07-11 stsp if (err)
6211 818c7501 2019-07-11 stsp goto done;
6212 818c7501 2019-07-11 stsp
6213 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6214 818c7501 2019-07-11 stsp if (err)
6215 818c7501 2019-07-11 stsp goto done;
6216 818c7501 2019-07-11 stsp done:
6217 818c7501 2019-07-11 stsp free(commit_ref_name);
6218 818c7501 2019-07-11 stsp free(branch_ref_name);
6219 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6220 818c7501 2019-07-11 stsp if (commit_ref)
6221 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6222 818c7501 2019-07-11 stsp if (branch_ref)
6223 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6224 818c7501 2019-07-11 stsp if (err) {
6225 818c7501 2019-07-11 stsp free(*commit_id);
6226 818c7501 2019-07-11 stsp *commit_id = NULL;
6227 818c7501 2019-07-11 stsp if (*tmp_branch) {
6228 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6229 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6230 818c7501 2019-07-11 stsp }
6231 818c7501 2019-07-11 stsp if (*new_base_branch) {
6232 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6233 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6234 818c7501 2019-07-11 stsp }
6235 818c7501 2019-07-11 stsp if (*branch) {
6236 818c7501 2019-07-11 stsp got_ref_close(*branch);
6237 818c7501 2019-07-11 stsp *branch = NULL;
6238 818c7501 2019-07-11 stsp }
6239 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6240 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6241 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6242 3e3a69f1 2019-07-25 stsp }
6243 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6244 818c7501 2019-07-11 stsp }
6245 818c7501 2019-07-11 stsp return err;
6246 c4296144 2019-05-09 stsp }
6247 818c7501 2019-07-11 stsp
6248 818c7501 2019-07-11 stsp const struct got_error *
6249 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6250 818c7501 2019-07-11 stsp {
6251 818c7501 2019-07-11 stsp const struct got_error *err;
6252 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6253 818c7501 2019-07-11 stsp
6254 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6255 818c7501 2019-07-11 stsp if (err)
6256 818c7501 2019-07-11 stsp return err;
6257 818c7501 2019-07-11 stsp
6258 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6259 818c7501 2019-07-11 stsp free(tmp_branch_name);
6260 818c7501 2019-07-11 stsp return NULL;
6261 818c7501 2019-07-11 stsp }
6262 818c7501 2019-07-11 stsp
6263 818c7501 2019-07-11 stsp static const struct got_error *
6264 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6265 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6266 818c7501 2019-07-11 stsp {
6267 0ebf8283 2019-07-24 stsp *logmsg = arg;
6268 818c7501 2019-07-11 stsp return NULL;
6269 818c7501 2019-07-11 stsp }
6270 818c7501 2019-07-11 stsp
6271 818c7501 2019-07-11 stsp static const struct got_error *
6272 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6273 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6274 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6275 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6276 818c7501 2019-07-11 stsp {
6277 818c7501 2019-07-11 stsp return NULL;
6278 01757395 2019-07-12 stsp }
6279 01757395 2019-07-12 stsp
6280 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6281 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6282 01757395 2019-07-12 stsp void *progress_arg;
6283 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6284 01757395 2019-07-12 stsp };
6285 01757395 2019-07-12 stsp
6286 01757395 2019-07-12 stsp static const struct got_error *
6287 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6288 01757395 2019-07-12 stsp {
6289 01757395 2019-07-12 stsp const struct got_error *err;
6290 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6291 01757395 2019-07-12 stsp char *p;
6292 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6293 01757395 2019-07-12 stsp
6294 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6295 01757395 2019-07-12 stsp if (err)
6296 01757395 2019-07-12 stsp return err;
6297 01757395 2019-07-12 stsp
6298 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6299 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6300 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6301 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6302 01757395 2019-07-12 stsp return NULL;
6303 01757395 2019-07-12 stsp
6304 01757395 2019-07-12 stsp p = strdup(path);
6305 01757395 2019-07-12 stsp if (p == NULL)
6306 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6307 01757395 2019-07-12 stsp
6308 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6309 01757395 2019-07-12 stsp if (err || new == NULL)
6310 01757395 2019-07-12 stsp free(p);
6311 01757395 2019-07-12 stsp return err;
6312 01757395 2019-07-12 stsp }
6313 01757395 2019-07-12 stsp
6314 01757395 2019-07-12 stsp void
6315 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6316 01757395 2019-07-12 stsp {
6317 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6318 01757395 2019-07-12 stsp
6319 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6320 01757395 2019-07-12 stsp free((char *)pe->path);
6321 01757395 2019-07-12 stsp
6322 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6323 818c7501 2019-07-11 stsp }
6324 818c7501 2019-07-11 stsp
6325 0ebf8283 2019-07-24 stsp static const struct got_error *
6326 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6327 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6328 818c7501 2019-07-11 stsp {
6329 818c7501 2019-07-11 stsp const struct got_error *err;
6330 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6331 818c7501 2019-07-11 stsp
6332 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6333 818c7501 2019-07-11 stsp if (err) {
6334 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6335 818c7501 2019-07-11 stsp goto done;
6336 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6337 818c7501 2019-07-11 stsp if (err)
6338 818c7501 2019-07-11 stsp goto done;
6339 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6340 818c7501 2019-07-11 stsp if (err)
6341 818c7501 2019-07-11 stsp goto done;
6342 de05890f 2020-03-05 stsp } else if (is_rebase) {
6343 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6344 818c7501 2019-07-11 stsp int cmp;
6345 818c7501 2019-07-11 stsp
6346 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6347 818c7501 2019-07-11 stsp if (err)
6348 818c7501 2019-07-11 stsp goto done;
6349 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6350 818c7501 2019-07-11 stsp free(stored_id);
6351 818c7501 2019-07-11 stsp if (cmp != 0) {
6352 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6353 818c7501 2019-07-11 stsp goto done;
6354 818c7501 2019-07-11 stsp }
6355 818c7501 2019-07-11 stsp }
6356 0ebf8283 2019-07-24 stsp done:
6357 0ebf8283 2019-07-24 stsp if (commit_ref)
6358 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6359 0ebf8283 2019-07-24 stsp return err;
6360 0ebf8283 2019-07-24 stsp }
6361 0ebf8283 2019-07-24 stsp
6362 0ebf8283 2019-07-24 stsp static const struct got_error *
6363 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6364 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6365 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6366 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6367 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6368 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6369 0ebf8283 2019-07-24 stsp {
6370 0ebf8283 2019-07-24 stsp const struct got_error *err;
6371 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6372 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6373 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6374 818c7501 2019-07-11 stsp
6375 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6376 0ebf8283 2019-07-24 stsp
6377 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6378 0ebf8283 2019-07-24 stsp if (err)
6379 0ebf8283 2019-07-24 stsp return err;
6380 0ebf8283 2019-07-24 stsp
6381 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6382 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6383 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6384 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6385 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6386 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6387 818c7501 2019-07-11 stsp if (commit_ref)
6388 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6389 818c7501 2019-07-11 stsp return err;
6390 818c7501 2019-07-11 stsp }
6391 818c7501 2019-07-11 stsp
6392 818c7501 2019-07-11 stsp const struct got_error *
6393 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6394 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6395 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6396 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6397 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6398 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6399 818c7501 2019-07-11 stsp {
6400 0ebf8283 2019-07-24 stsp const struct got_error *err;
6401 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6404 0ebf8283 2019-07-24 stsp if (err)
6405 0ebf8283 2019-07-24 stsp return err;
6406 0ebf8283 2019-07-24 stsp
6407 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6408 0ebf8283 2019-07-24 stsp if (err)
6409 0ebf8283 2019-07-24 stsp goto done;
6410 0ebf8283 2019-07-24 stsp
6411 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6412 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6413 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6414 0ebf8283 2019-07-24 stsp done:
6415 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6416 0ebf8283 2019-07-24 stsp return err;
6417 0ebf8283 2019-07-24 stsp }
6418 0ebf8283 2019-07-24 stsp
6419 0ebf8283 2019-07-24 stsp const struct got_error *
6420 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6421 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6422 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6423 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6424 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6425 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6426 0ebf8283 2019-07-24 stsp {
6427 0ebf8283 2019-07-24 stsp const struct got_error *err;
6428 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6429 0ebf8283 2019-07-24 stsp
6430 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6431 0ebf8283 2019-07-24 stsp if (err)
6432 0ebf8283 2019-07-24 stsp return err;
6433 0ebf8283 2019-07-24 stsp
6434 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6435 0ebf8283 2019-07-24 stsp if (err)
6436 0ebf8283 2019-07-24 stsp goto done;
6437 0ebf8283 2019-07-24 stsp
6438 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6439 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6440 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6441 0ebf8283 2019-07-24 stsp done:
6442 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6443 0ebf8283 2019-07-24 stsp return err;
6444 0ebf8283 2019-07-24 stsp }
6445 0ebf8283 2019-07-24 stsp
6446 0ebf8283 2019-07-24 stsp static const struct got_error *
6447 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6448 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6449 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6450 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6451 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6452 0ebf8283 2019-07-24 stsp {
6453 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6454 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6455 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6456 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6457 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6458 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6459 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6460 818c7501 2019-07-11 stsp
6461 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6462 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6463 a0e95631 2019-07-12 stsp
6464 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6465 818c7501 2019-07-11 stsp
6466 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6467 a0e95631 2019-07-12 stsp if (err)
6468 3e3a69f1 2019-07-25 stsp return err;
6469 a0e95631 2019-07-12 stsp
6470 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6471 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6472 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6473 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6474 01757395 2019-07-12 stsp /*
6475 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6476 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6477 6c13b005 2021-09-02 stsp *
6478 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6479 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6480 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6481 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6482 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6483 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6484 01757395 2019-07-12 stsp */
6485 01757395 2019-07-12 stsp if (merged_paths) {
6486 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6487 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6488 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6489 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6490 f2a9dc41 2019-12-13 tracey 0);
6491 01757395 2019-07-12 stsp if (err)
6492 01757395 2019-07-12 stsp goto done;
6493 01757395 2019-07-12 stsp }
6494 01757395 2019-07-12 stsp } else {
6495 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6496 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6497 01757395 2019-07-12 stsp if (err)
6498 01757395 2019-07-12 stsp goto done;
6499 01757395 2019-07-12 stsp }
6500 a0e95631 2019-07-12 stsp
6501 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6502 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6503 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6504 a0e95631 2019-07-12 stsp if (err)
6505 a0e95631 2019-07-12 stsp goto done;
6506 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6507 a0e95631 2019-07-12 stsp goto done;
6508 ff0d2220 2019-07-11 stsp }
6509 818c7501 2019-07-11 stsp
6510 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6511 edd02c5e 2019-07-11 stsp if (err)
6512 edd02c5e 2019-07-11 stsp goto done;
6513 edd02c5e 2019-07-11 stsp
6514 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6515 818c7501 2019-07-11 stsp if (err)
6516 818c7501 2019-07-11 stsp goto done;
6517 818c7501 2019-07-11 stsp
6518 5943eee2 2019-08-13 stsp if (new_logmsg) {
6519 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6520 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6521 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6522 5943eee2 2019-08-13 stsp goto done;
6523 5943eee2 2019-08-13 stsp }
6524 5943eee2 2019-08-13 stsp } else {
6525 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6526 5943eee2 2019-08-13 stsp if (err)
6527 5943eee2 2019-08-13 stsp goto done;
6528 5943eee2 2019-08-13 stsp }
6529 0ebf8283 2019-07-24 stsp
6530 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6531 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6532 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6533 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6534 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6535 a0e95631 2019-07-12 stsp if (err)
6536 a0e95631 2019-07-12 stsp goto done;
6537 a0e95631 2019-07-12 stsp
6538 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6539 a0e95631 2019-07-12 stsp if (err)
6540 a0e95631 2019-07-12 stsp goto done;
6541 a0e95631 2019-07-12 stsp
6542 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6543 a0e95631 2019-07-12 stsp if (err)
6544 a0e95631 2019-07-12 stsp goto done;
6545 a0e95631 2019-07-12 stsp
6546 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6547 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6548 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6549 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6550 a0e95631 2019-07-12 stsp err = sync_err;
6551 818c7501 2019-07-11 stsp done:
6552 a0e95631 2019-07-12 stsp free(fileindex_path);
6553 a0e95631 2019-07-12 stsp free(head_commit_id);
6554 a0e95631 2019-07-12 stsp if (head_ref)
6555 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6556 818c7501 2019-07-11 stsp if (err) {
6557 818c7501 2019-07-11 stsp free(*new_commit_id);
6558 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6559 818c7501 2019-07-11 stsp }
6560 818c7501 2019-07-11 stsp return err;
6561 818c7501 2019-07-11 stsp }
6562 818c7501 2019-07-11 stsp
6563 818c7501 2019-07-11 stsp const struct got_error *
6564 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6565 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6566 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6567 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6568 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6569 0ebf8283 2019-07-24 stsp {
6570 0ebf8283 2019-07-24 stsp const struct got_error *err;
6571 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6572 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6573 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6576 0ebf8283 2019-07-24 stsp if (err)
6577 0ebf8283 2019-07-24 stsp return err;
6578 0ebf8283 2019-07-24 stsp
6579 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6580 0ebf8283 2019-07-24 stsp if (err)
6581 0ebf8283 2019-07-24 stsp goto done;
6582 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6583 0ebf8283 2019-07-24 stsp if (err)
6584 0ebf8283 2019-07-24 stsp goto done;
6585 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6586 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6587 0ebf8283 2019-07-24 stsp goto done;
6588 0ebf8283 2019-07-24 stsp }
6589 0ebf8283 2019-07-24 stsp
6590 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6591 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6592 0ebf8283 2019-07-24 stsp done:
6593 0ebf8283 2019-07-24 stsp if (commit_ref)
6594 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6595 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6596 0ebf8283 2019-07-24 stsp free(commit_id);
6597 0ebf8283 2019-07-24 stsp return err;
6598 0ebf8283 2019-07-24 stsp }
6599 0ebf8283 2019-07-24 stsp
6600 0ebf8283 2019-07-24 stsp const struct got_error *
6601 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6602 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6603 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6604 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6605 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6606 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6607 0ebf8283 2019-07-24 stsp {
6608 0ebf8283 2019-07-24 stsp const struct got_error *err;
6609 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6610 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6611 0ebf8283 2019-07-24 stsp
6612 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6613 0ebf8283 2019-07-24 stsp if (err)
6614 0ebf8283 2019-07-24 stsp return err;
6615 0ebf8283 2019-07-24 stsp
6616 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6617 0ebf8283 2019-07-24 stsp if (err)
6618 0ebf8283 2019-07-24 stsp goto done;
6619 0ebf8283 2019-07-24 stsp
6620 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6621 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6622 0ebf8283 2019-07-24 stsp done:
6623 0ebf8283 2019-07-24 stsp if (commit_ref)
6624 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6625 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6626 0ebf8283 2019-07-24 stsp return err;
6627 0ebf8283 2019-07-24 stsp }
6628 0ebf8283 2019-07-24 stsp
6629 0ebf8283 2019-07-24 stsp const struct got_error *
6630 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6631 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6632 818c7501 2019-07-11 stsp {
6633 3e3a69f1 2019-07-25 stsp if (fileindex)
6634 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6635 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6636 69844fba 2019-07-11 stsp }
6637 69844fba 2019-07-11 stsp
6638 69844fba 2019-07-11 stsp static const struct got_error *
6639 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6640 69844fba 2019-07-11 stsp {
6641 69844fba 2019-07-11 stsp const struct got_error *err;
6642 69844fba 2019-07-11 stsp struct got_reference *ref;
6643 69844fba 2019-07-11 stsp
6644 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6645 69844fba 2019-07-11 stsp if (err) {
6646 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6647 69844fba 2019-07-11 stsp return NULL;
6648 69844fba 2019-07-11 stsp return err;
6649 69844fba 2019-07-11 stsp }
6650 69844fba 2019-07-11 stsp
6651 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6652 69844fba 2019-07-11 stsp got_ref_close(ref);
6653 69844fba 2019-07-11 stsp return err;
6654 818c7501 2019-07-11 stsp }
6655 818c7501 2019-07-11 stsp
6656 69844fba 2019-07-11 stsp static const struct got_error *
6657 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6658 69844fba 2019-07-11 stsp {
6659 69844fba 2019-07-11 stsp const struct got_error *err;
6660 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6661 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6662 69844fba 2019-07-11 stsp
6663 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6664 69844fba 2019-07-11 stsp if (err)
6665 69844fba 2019-07-11 stsp goto done;
6666 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6667 69844fba 2019-07-11 stsp if (err)
6668 69844fba 2019-07-11 stsp goto done;
6669 69844fba 2019-07-11 stsp
6670 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6671 69844fba 2019-07-11 stsp if (err)
6672 69844fba 2019-07-11 stsp goto done;
6673 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6674 69844fba 2019-07-11 stsp if (err)
6675 69844fba 2019-07-11 stsp goto done;
6676 69844fba 2019-07-11 stsp
6677 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6678 69844fba 2019-07-11 stsp if (err)
6679 69844fba 2019-07-11 stsp goto done;
6680 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6681 69844fba 2019-07-11 stsp if (err)
6682 69844fba 2019-07-11 stsp goto done;
6683 69844fba 2019-07-11 stsp
6684 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6685 69844fba 2019-07-11 stsp if (err)
6686 69844fba 2019-07-11 stsp goto done;
6687 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6688 69844fba 2019-07-11 stsp if (err)
6689 69844fba 2019-07-11 stsp goto done;
6690 69844fba 2019-07-11 stsp
6691 69844fba 2019-07-11 stsp done:
6692 69844fba 2019-07-11 stsp free(tmp_branch_name);
6693 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6694 69844fba 2019-07-11 stsp free(branch_ref_name);
6695 69844fba 2019-07-11 stsp free(commit_ref_name);
6696 e600f124 2021-03-21 stsp return err;
6697 e600f124 2021-03-21 stsp }
6698 e600f124 2021-03-21 stsp
6699 ef20f542 2022-06-26 thomas static const struct got_error *
6700 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6701 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6702 e600f124 2021-03-21 stsp {
6703 e600f124 2021-03-21 stsp const struct got_error *err;
6704 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6705 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6706 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6707 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6708 e600f124 2021-03-21 stsp char *refname = NULL;
6709 e600f124 2021-03-21 stsp
6710 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6711 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6712 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6713 e600f124 2021-03-21 stsp branch_name += 11;
6714 e600f124 2021-03-21 stsp
6715 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6716 e600f124 2021-03-21 stsp if (err)
6717 e600f124 2021-03-21 stsp return err;
6718 e600f124 2021-03-21 stsp
6719 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6720 e600f124 2021-03-21 stsp new_id_str) == -1) {
6721 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6722 e600f124 2021-03-21 stsp goto done;
6723 e600f124 2021-03-21 stsp }
6724 e600f124 2021-03-21 stsp
6725 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6726 e600f124 2021-03-21 stsp if (err)
6727 e600f124 2021-03-21 stsp goto done;
6728 e600f124 2021-03-21 stsp
6729 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6730 e600f124 2021-03-21 stsp if (err)
6731 e600f124 2021-03-21 stsp goto done;
6732 e600f124 2021-03-21 stsp
6733 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6734 e600f124 2021-03-21 stsp done:
6735 e600f124 2021-03-21 stsp free(new_id_str);
6736 e600f124 2021-03-21 stsp free(refname);
6737 e600f124 2021-03-21 stsp free(old_commit_id);
6738 e600f124 2021-03-21 stsp if (ref)
6739 e600f124 2021-03-21 stsp got_ref_close(ref);
6740 69844fba 2019-07-11 stsp return err;
6741 69844fba 2019-07-11 stsp }
6742 69844fba 2019-07-11 stsp
6743 818c7501 2019-07-11 stsp const struct got_error *
6744 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6745 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6746 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6747 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6748 818c7501 2019-07-11 stsp {
6749 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6750 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6751 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6752 818c7501 2019-07-11 stsp
6753 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6754 818c7501 2019-07-11 stsp if (err)
6755 818c7501 2019-07-11 stsp return err;
6756 e600f124 2021-03-21 stsp
6757 e600f124 2021-03-21 stsp if (create_backup) {
6758 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6759 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6760 e600f124 2021-03-21 stsp if (err)
6761 e600f124 2021-03-21 stsp goto done;
6762 e600f124 2021-03-21 stsp }
6763 818c7501 2019-07-11 stsp
6764 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6765 818c7501 2019-07-11 stsp if (err)
6766 818c7501 2019-07-11 stsp goto done;
6767 818c7501 2019-07-11 stsp
6768 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6769 818c7501 2019-07-11 stsp if (err)
6770 818c7501 2019-07-11 stsp goto done;
6771 818c7501 2019-07-11 stsp
6772 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6773 818c7501 2019-07-11 stsp if (err)
6774 818c7501 2019-07-11 stsp goto done;
6775 818c7501 2019-07-11 stsp
6776 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6777 a615e0e7 2020-12-16 stsp if (err)
6778 a615e0e7 2020-12-16 stsp goto done;
6779 a615e0e7 2020-12-16 stsp
6780 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6781 a615e0e7 2020-12-16 stsp if (err)
6782 a615e0e7 2020-12-16 stsp goto done;
6783 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6784 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6785 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6786 a615e0e7 2020-12-16 stsp err = sync_err;
6787 818c7501 2019-07-11 stsp done:
6788 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6789 a615e0e7 2020-12-16 stsp free(fileindex_path);
6790 818c7501 2019-07-11 stsp free(new_head_commit_id);
6791 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6792 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6793 818c7501 2019-07-11 stsp err = unlockerr;
6794 818c7501 2019-07-11 stsp return err;
6795 818c7501 2019-07-11 stsp }
6796 818c7501 2019-07-11 stsp
6797 818c7501 2019-07-11 stsp const struct got_error *
6798 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6799 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6800 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6801 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6802 818c7501 2019-07-11 stsp {
6803 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6804 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6805 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6806 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6807 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6808 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6809 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6810 818c7501 2019-07-11 stsp
6811 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6812 818c7501 2019-07-11 stsp if (err)
6813 818c7501 2019-07-11 stsp return err;
6814 818c7501 2019-07-11 stsp
6815 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
6816 945f9229 2022-04-16 thomas worktree->base_commit_id);
6817 945f9229 2022-04-16 thomas if (err)
6818 945f9229 2022-04-16 thomas goto done;
6819 945f9229 2022-04-16 thomas
6820 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6821 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6822 818c7501 2019-07-11 stsp if (err)
6823 818c7501 2019-07-11 stsp goto done;
6824 818c7501 2019-07-11 stsp
6825 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6826 818c7501 2019-07-11 stsp if (err)
6827 818c7501 2019-07-11 stsp goto done;
6828 818c7501 2019-07-11 stsp
6829 818c7501 2019-07-11 stsp /*
6830 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6831 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6832 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6833 818c7501 2019-07-11 stsp */
6834 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6835 818c7501 2019-07-11 stsp if (err)
6836 818c7501 2019-07-11 stsp goto done;
6837 818c7501 2019-07-11 stsp
6838 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6839 818c7501 2019-07-11 stsp if (err)
6840 818c7501 2019-07-11 stsp goto done;
6841 818c7501 2019-07-11 stsp
6842 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
6843 945f9229 2022-04-16 thomas worktree->path_prefix);
6844 ca355955 2019-07-12 stsp if (err)
6845 ca355955 2019-07-12 stsp goto done;
6846 ca355955 2019-07-12 stsp
6847 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6848 ca355955 2019-07-12 stsp if (err)
6849 ca355955 2019-07-12 stsp goto done;
6850 ca355955 2019-07-12 stsp
6851 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6852 818c7501 2019-07-11 stsp if (err)
6853 818c7501 2019-07-11 stsp goto done;
6854 818c7501 2019-07-11 stsp
6855 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6856 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6857 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6858 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6859 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6860 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6861 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6862 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6863 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6864 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6865 55bd499d 2019-07-12 stsp if (err)
6866 1f1abb7e 2019-08-08 stsp goto sync;
6867 55bd499d 2019-07-12 stsp
6868 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6869 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6870 ca355955 2019-07-12 stsp sync:
6871 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6872 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6873 ca355955 2019-07-12 stsp err = sync_err;
6874 818c7501 2019-07-11 stsp done:
6875 818c7501 2019-07-11 stsp got_ref_close(resolved);
6876 a3a2faf2 2019-07-12 stsp free(tree_id);
6877 818c7501 2019-07-11 stsp free(commit_id);
6878 945f9229 2022-04-16 thomas if (commit)
6879 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6880 0ebf8283 2019-07-24 stsp if (fileindex)
6881 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6882 0ebf8283 2019-07-24 stsp free(fileindex_path);
6883 0ebf8283 2019-07-24 stsp
6884 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6885 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6886 0ebf8283 2019-07-24 stsp err = unlockerr;
6887 0ebf8283 2019-07-24 stsp return err;
6888 0ebf8283 2019-07-24 stsp }
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp const struct got_error *
6891 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6892 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6893 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6894 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6895 0ebf8283 2019-07-24 stsp {
6896 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6897 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6898 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6899 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6900 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6901 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6902 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6903 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6906 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6907 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6908 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6909 0ebf8283 2019-07-24 stsp
6910 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6911 0ebf8283 2019-07-24 stsp if (err)
6912 0ebf8283 2019-07-24 stsp return err;
6913 0ebf8283 2019-07-24 stsp
6914 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6915 0ebf8283 2019-07-24 stsp if (err)
6916 0ebf8283 2019-07-24 stsp goto done;
6917 0ebf8283 2019-07-24 stsp
6918 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6919 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6920 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6921 0ebf8283 2019-07-24 stsp &ok_arg);
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp goto done;
6924 0ebf8283 2019-07-24 stsp
6925 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6926 0ebf8283 2019-07-24 stsp if (err)
6927 0ebf8283 2019-07-24 stsp goto done;
6928 0ebf8283 2019-07-24 stsp
6929 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp
6933 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6934 0ebf8283 2019-07-24 stsp worktree);
6935 0ebf8283 2019-07-24 stsp if (err)
6936 0ebf8283 2019-07-24 stsp goto done;
6937 0ebf8283 2019-07-24 stsp
6938 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6939 0ebf8283 2019-07-24 stsp 0);
6940 0ebf8283 2019-07-24 stsp if (err)
6941 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp
6943 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6944 0ebf8283 2019-07-24 stsp if (err)
6945 0ebf8283 2019-07-24 stsp goto done;
6946 0ebf8283 2019-07-24 stsp
6947 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6948 0ebf8283 2019-07-24 stsp if (err)
6949 0ebf8283 2019-07-24 stsp goto done;
6950 0ebf8283 2019-07-24 stsp
6951 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6952 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6953 0ebf8283 2019-07-24 stsp if (err)
6954 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6956 0ebf8283 2019-07-24 stsp if (err)
6957 0ebf8283 2019-07-24 stsp goto done;
6958 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6959 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6960 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6961 0ebf8283 2019-07-24 stsp goto done;
6962 0ebf8283 2019-07-24 stsp }
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6965 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6966 0ebf8283 2019-07-24 stsp if (err)
6967 0ebf8283 2019-07-24 stsp goto done;
6968 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6969 0ebf8283 2019-07-24 stsp if (err)
6970 0ebf8283 2019-07-24 stsp goto done;
6971 0ebf8283 2019-07-24 stsp
6972 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6973 0ebf8283 2019-07-24 stsp if (err)
6974 0ebf8283 2019-07-24 stsp goto done;
6975 0ebf8283 2019-07-24 stsp done:
6976 0ebf8283 2019-07-24 stsp free(fileindex_path);
6977 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6978 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6979 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6980 0ebf8283 2019-07-24 stsp if (wt_branch)
6981 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6982 0ebf8283 2019-07-24 stsp if (err) {
6983 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6984 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6985 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6986 0ebf8283 2019-07-24 stsp }
6987 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6988 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6989 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6990 0ebf8283 2019-07-24 stsp }
6991 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6992 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6993 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6994 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6995 3e3a69f1 2019-07-25 stsp }
6996 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6997 0ebf8283 2019-07-24 stsp }
6998 0ebf8283 2019-07-24 stsp return err;
6999 0ebf8283 2019-07-24 stsp }
7000 0ebf8283 2019-07-24 stsp
7001 0ebf8283 2019-07-24 stsp const struct got_error *
7002 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7003 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7004 0ebf8283 2019-07-24 stsp {
7005 3e3a69f1 2019-07-25 stsp if (fileindex)
7006 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7007 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7008 0ebf8283 2019-07-24 stsp }
7009 0ebf8283 2019-07-24 stsp
7010 0ebf8283 2019-07-24 stsp const struct got_error *
7011 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7012 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7013 0ebf8283 2019-07-24 stsp {
7014 0ebf8283 2019-07-24 stsp const struct got_error *err;
7015 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7016 0ebf8283 2019-07-24 stsp
7017 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7018 0ebf8283 2019-07-24 stsp if (err)
7019 0ebf8283 2019-07-24 stsp return err;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7022 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7023 0ebf8283 2019-07-24 stsp return NULL;
7024 0ebf8283 2019-07-24 stsp }
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp const struct got_error *
7027 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7028 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7029 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7030 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7031 0ebf8283 2019-07-24 stsp {
7032 0ebf8283 2019-07-24 stsp const struct got_error *err;
7033 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7034 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7035 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7036 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7037 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7038 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7039 0ebf8283 2019-07-24 stsp
7040 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7041 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7042 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7043 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7044 0ebf8283 2019-07-24 stsp
7045 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7046 3e3a69f1 2019-07-25 stsp if (err)
7047 3e3a69f1 2019-07-25 stsp return err;
7048 3e3a69f1 2019-07-25 stsp
7049 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7050 3e3a69f1 2019-07-25 stsp if (err)
7051 f032f1f7 2019-08-04 stsp goto done;
7052 f032f1f7 2019-08-04 stsp
7053 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7054 f032f1f7 2019-08-04 stsp &have_staged_files);
7055 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7056 f032f1f7 2019-08-04 stsp goto done;
7057 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7058 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7059 3e3a69f1 2019-07-25 stsp goto done;
7060 f032f1f7 2019-08-04 stsp }
7061 3e3a69f1 2019-07-25 stsp
7062 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7063 0ebf8283 2019-07-24 stsp if (err)
7064 f032f1f7 2019-08-04 stsp goto done;
7065 0ebf8283 2019-07-24 stsp
7066 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7067 0ebf8283 2019-07-24 stsp if (err)
7068 0ebf8283 2019-07-24 stsp goto done;
7069 0ebf8283 2019-07-24 stsp
7070 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7071 0ebf8283 2019-07-24 stsp if (err)
7072 0ebf8283 2019-07-24 stsp goto done;
7073 0ebf8283 2019-07-24 stsp
7074 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7075 0ebf8283 2019-07-24 stsp worktree);
7076 0ebf8283 2019-07-24 stsp if (err)
7077 0ebf8283 2019-07-24 stsp goto done;
7078 0ebf8283 2019-07-24 stsp
7079 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7080 0ebf8283 2019-07-24 stsp if (err)
7081 0ebf8283 2019-07-24 stsp goto done;
7082 0ebf8283 2019-07-24 stsp
7083 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7084 0ebf8283 2019-07-24 stsp if (err)
7085 0ebf8283 2019-07-24 stsp goto done;
7086 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7087 0ebf8283 2019-07-24 stsp if (err)
7088 0ebf8283 2019-07-24 stsp goto done;
7089 0ebf8283 2019-07-24 stsp
7090 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7091 0ebf8283 2019-07-24 stsp if (err)
7092 0ebf8283 2019-07-24 stsp goto done;
7093 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7094 0ebf8283 2019-07-24 stsp if (err)
7095 0ebf8283 2019-07-24 stsp goto done;
7096 0ebf8283 2019-07-24 stsp
7097 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7098 0ebf8283 2019-07-24 stsp if (err)
7099 0ebf8283 2019-07-24 stsp goto done;
7100 0ebf8283 2019-07-24 stsp done:
7101 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7102 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7103 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7104 0ebf8283 2019-07-24 stsp if (commit_ref)
7105 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7106 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7107 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7108 0ebf8283 2019-07-24 stsp if (err) {
7109 0ebf8283 2019-07-24 stsp free(*commit_id);
7110 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7111 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7112 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7113 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7114 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7115 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7116 0ebf8283 2019-07-24 stsp }
7117 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7118 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7119 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7120 3e3a69f1 2019-07-25 stsp }
7121 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7122 0ebf8283 2019-07-24 stsp }
7123 0ebf8283 2019-07-24 stsp return err;
7124 0ebf8283 2019-07-24 stsp }
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp static const struct got_error *
7127 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7128 0ebf8283 2019-07-24 stsp {
7129 0ebf8283 2019-07-24 stsp const struct got_error *err;
7130 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7131 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7132 0ebf8283 2019-07-24 stsp
7133 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7134 0ebf8283 2019-07-24 stsp if (err)
7135 0ebf8283 2019-07-24 stsp goto done;
7136 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7137 0ebf8283 2019-07-24 stsp if (err)
7138 0ebf8283 2019-07-24 stsp goto done;
7139 0ebf8283 2019-07-24 stsp
7140 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7141 0ebf8283 2019-07-24 stsp worktree);
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp goto done;
7144 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7145 0ebf8283 2019-07-24 stsp if (err)
7146 0ebf8283 2019-07-24 stsp goto done;
7147 0ebf8283 2019-07-24 stsp
7148 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7149 0ebf8283 2019-07-24 stsp if (err)
7150 0ebf8283 2019-07-24 stsp goto done;
7151 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7152 0ebf8283 2019-07-24 stsp if (err)
7153 0ebf8283 2019-07-24 stsp goto done;
7154 0ebf8283 2019-07-24 stsp
7155 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7156 0ebf8283 2019-07-24 stsp if (err)
7157 0ebf8283 2019-07-24 stsp goto done;
7158 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7159 0ebf8283 2019-07-24 stsp if (err)
7160 0ebf8283 2019-07-24 stsp goto done;
7161 0ebf8283 2019-07-24 stsp done:
7162 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7163 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7164 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7165 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7166 0ebf8283 2019-07-24 stsp return err;
7167 0ebf8283 2019-07-24 stsp }
7168 0ebf8283 2019-07-24 stsp
7169 0ebf8283 2019-07-24 stsp const struct got_error *
7170 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7171 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7172 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7173 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7174 0ebf8283 2019-07-24 stsp {
7175 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7176 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7177 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7178 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7179 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7180 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7181 0ebf8283 2019-07-24 stsp
7182 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7183 0ebf8283 2019-07-24 stsp if (err)
7184 0ebf8283 2019-07-24 stsp return err;
7185 0ebf8283 2019-07-24 stsp
7186 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7187 945f9229 2022-04-16 thomas worktree->base_commit_id);
7188 945f9229 2022-04-16 thomas if (err)
7189 945f9229 2022-04-16 thomas goto done;
7190 945f9229 2022-04-16 thomas
7191 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7192 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7193 0ebf8283 2019-07-24 stsp if (err)
7194 0ebf8283 2019-07-24 stsp goto done;
7195 0ebf8283 2019-07-24 stsp
7196 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7197 0ebf8283 2019-07-24 stsp if (err)
7198 0ebf8283 2019-07-24 stsp goto done;
7199 0ebf8283 2019-07-24 stsp
7200 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7201 0ebf8283 2019-07-24 stsp if (err)
7202 0ebf8283 2019-07-24 stsp goto done;
7203 0ebf8283 2019-07-24 stsp
7204 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7205 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7206 0ebf8283 2019-07-24 stsp if (err)
7207 0ebf8283 2019-07-24 stsp goto done;
7208 0ebf8283 2019-07-24 stsp
7209 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7210 0ebf8283 2019-07-24 stsp if (err)
7211 0ebf8283 2019-07-24 stsp goto done;
7212 0ebf8283 2019-07-24 stsp
7213 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7214 0ebf8283 2019-07-24 stsp if (err)
7215 0ebf8283 2019-07-24 stsp goto done;
7216 0ebf8283 2019-07-24 stsp
7217 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7218 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7219 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7220 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7221 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7222 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7223 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7224 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7225 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7226 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7227 0ebf8283 2019-07-24 stsp if (err)
7228 1f1abb7e 2019-08-08 stsp goto sync;
7229 0ebf8283 2019-07-24 stsp
7230 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7231 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7232 0ebf8283 2019-07-24 stsp sync:
7233 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7234 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7235 0ebf8283 2019-07-24 stsp err = sync_err;
7236 0ebf8283 2019-07-24 stsp done:
7237 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7238 0ebf8283 2019-07-24 stsp free(tree_id);
7239 818c7501 2019-07-11 stsp free(fileindex_path);
7240 818c7501 2019-07-11 stsp
7241 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7242 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7243 818c7501 2019-07-11 stsp err = unlockerr;
7244 818c7501 2019-07-11 stsp return err;
7245 818c7501 2019-07-11 stsp }
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp const struct got_error *
7248 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7249 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7250 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7251 0ebf8283 2019-07-24 stsp {
7252 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7253 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7254 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7255 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7256 0ebf8283 2019-07-24 stsp
7257 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7258 0ebf8283 2019-07-24 stsp if (err)
7259 0ebf8283 2019-07-24 stsp return err;
7260 0ebf8283 2019-07-24 stsp
7261 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7262 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7263 e600f124 2021-03-21 stsp if (err)
7264 e600f124 2021-03-21 stsp goto done;
7265 e600f124 2021-03-21 stsp
7266 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7267 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7268 0ebf8283 2019-07-24 stsp if (err)
7269 0ebf8283 2019-07-24 stsp goto done;
7270 0ebf8283 2019-07-24 stsp
7271 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7272 0ebf8283 2019-07-24 stsp if (err)
7273 0ebf8283 2019-07-24 stsp goto done;
7274 0ebf8283 2019-07-24 stsp
7275 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7276 0ebf8283 2019-07-24 stsp if (err)
7277 0ebf8283 2019-07-24 stsp goto done;
7278 0ebf8283 2019-07-24 stsp
7279 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7280 0ebf8283 2019-07-24 stsp if (err)
7281 0ebf8283 2019-07-24 stsp goto done;
7282 0ebf8283 2019-07-24 stsp
7283 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7284 a615e0e7 2020-12-16 stsp if (err)
7285 a615e0e7 2020-12-16 stsp goto done;
7286 a615e0e7 2020-12-16 stsp
7287 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7288 a615e0e7 2020-12-16 stsp if (err)
7289 a615e0e7 2020-12-16 stsp goto done;
7290 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7291 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7292 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7293 a615e0e7 2020-12-16 stsp err = sync_err;
7294 0ebf8283 2019-07-24 stsp done:
7295 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7296 a615e0e7 2020-12-16 stsp free(fileindex_path);
7297 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7298 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7299 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7300 0ebf8283 2019-07-24 stsp err = unlockerr;
7301 0ebf8283 2019-07-24 stsp return err;
7302 0ebf8283 2019-07-24 stsp }
7303 0ebf8283 2019-07-24 stsp
7304 0ebf8283 2019-07-24 stsp const struct got_error *
7305 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7306 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7307 0ebf8283 2019-07-24 stsp {
7308 0ebf8283 2019-07-24 stsp const struct got_error *err;
7309 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7310 0ebf8283 2019-07-24 stsp
7311 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7312 0ebf8283 2019-07-24 stsp if (err)
7313 0ebf8283 2019-07-24 stsp return err;
7314 0ebf8283 2019-07-24 stsp
7315 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7316 0ebf8283 2019-07-24 stsp if (err)
7317 0ebf8283 2019-07-24 stsp goto done;
7318 0ebf8283 2019-07-24 stsp
7319 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7320 0ebf8283 2019-07-24 stsp done:
7321 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7322 2822a352 2019-10-15 stsp return err;
7323 2822a352 2019-10-15 stsp }
7324 2822a352 2019-10-15 stsp
7325 2822a352 2019-10-15 stsp const struct got_error *
7326 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7327 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7328 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7329 2822a352 2019-10-15 stsp struct got_repository *repo)
7330 2822a352 2019-10-15 stsp {
7331 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7332 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7333 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7334 2822a352 2019-10-15 stsp
7335 2822a352 2019-10-15 stsp *fileindex = NULL;
7336 2822a352 2019-10-15 stsp *branch_ref = NULL;
7337 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7338 2822a352 2019-10-15 stsp
7339 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7340 2822a352 2019-10-15 stsp if (err)
7341 2822a352 2019-10-15 stsp return err;
7342 2822a352 2019-10-15 stsp
7343 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7344 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7345 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7346 2822a352 2019-10-15 stsp "update -b or different branch name required");
7347 2822a352 2019-10-15 stsp goto done;
7348 2822a352 2019-10-15 stsp }
7349 2822a352 2019-10-15 stsp
7350 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7351 2822a352 2019-10-15 stsp if (err)
7352 2822a352 2019-10-15 stsp goto done;
7353 2822a352 2019-10-15 stsp
7354 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7355 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7356 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7357 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7358 2822a352 2019-10-15 stsp &ok_arg);
7359 2822a352 2019-10-15 stsp if (err)
7360 2822a352 2019-10-15 stsp goto done;
7361 2822a352 2019-10-15 stsp
7362 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7363 2822a352 2019-10-15 stsp if (err)
7364 2822a352 2019-10-15 stsp goto done;
7365 2822a352 2019-10-15 stsp
7366 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7367 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7368 2822a352 2019-10-15 stsp done:
7369 2822a352 2019-10-15 stsp if (err) {
7370 2822a352 2019-10-15 stsp if (*branch_ref) {
7371 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7372 2822a352 2019-10-15 stsp *branch_ref = NULL;
7373 2822a352 2019-10-15 stsp }
7374 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7375 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7376 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7377 2822a352 2019-10-15 stsp }
7378 2822a352 2019-10-15 stsp if (*fileindex) {
7379 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7380 2822a352 2019-10-15 stsp *fileindex = NULL;
7381 2822a352 2019-10-15 stsp }
7382 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7383 2822a352 2019-10-15 stsp }
7384 0cb83759 2019-08-03 stsp return err;
7385 0cb83759 2019-08-03 stsp }
7386 0cb83759 2019-08-03 stsp
7387 2822a352 2019-10-15 stsp const struct got_error *
7388 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7389 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7390 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7391 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7392 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7393 2822a352 2019-10-15 stsp {
7394 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7395 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7396 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7397 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7398 2822a352 2019-10-15 stsp
7399 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7400 2822a352 2019-10-15 stsp if (err)
7401 2822a352 2019-10-15 stsp goto done;
7402 2822a352 2019-10-15 stsp
7403 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7404 2822a352 2019-10-15 stsp if (err)
7405 2822a352 2019-10-15 stsp goto done;
7406 2822a352 2019-10-15 stsp
7407 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7408 945f9229 2022-04-16 thomas if (err)
7409 945f9229 2022-04-16 thomas goto done;
7410 945f9229 2022-04-16 thomas
7411 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7412 2822a352 2019-10-15 stsp worktree->path_prefix);
7413 2822a352 2019-10-15 stsp if (err)
7414 2822a352 2019-10-15 stsp goto done;
7415 2822a352 2019-10-15 stsp
7416 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7417 2822a352 2019-10-15 stsp if (err)
7418 2822a352 2019-10-15 stsp goto done;
7419 2822a352 2019-10-15 stsp
7420 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7421 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7422 2822a352 2019-10-15 stsp if (err)
7423 2822a352 2019-10-15 stsp goto sync;
7424 2822a352 2019-10-15 stsp
7425 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7426 2822a352 2019-10-15 stsp if (err)
7427 2822a352 2019-10-15 stsp goto sync;
7428 2822a352 2019-10-15 stsp
7429 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7430 6e210706 2021-01-22 stsp if (err)
7431 6e210706 2021-01-22 stsp goto sync;
7432 6e210706 2021-01-22 stsp
7433 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7434 2822a352 2019-10-15 stsp sync:
7435 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7436 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7437 2822a352 2019-10-15 stsp err = sync_err;
7438 2822a352 2019-10-15 stsp
7439 2822a352 2019-10-15 stsp done:
7440 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7441 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7442 2822a352 2019-10-15 stsp err = unlockerr;
7443 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7444 2822a352 2019-10-15 stsp
7445 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7446 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7447 2822a352 2019-10-15 stsp err = unlockerr;
7448 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7449 2822a352 2019-10-15 stsp
7450 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7451 2822a352 2019-10-15 stsp free(fileindex_path);
7452 2822a352 2019-10-15 stsp free(tree_id);
7453 945f9229 2022-04-16 thomas if (commit)
7454 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7455 2822a352 2019-10-15 stsp
7456 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7457 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7458 2822a352 2019-10-15 stsp err = unlockerr;
7459 2822a352 2019-10-15 stsp return err;
7460 2822a352 2019-10-15 stsp }
7461 2822a352 2019-10-15 stsp
7462 2822a352 2019-10-15 stsp const struct got_error *
7463 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7464 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7465 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7466 2822a352 2019-10-15 stsp {
7467 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7468 8b692cd0 2019-10-21 stsp
7469 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7470 8b692cd0 2019-10-21 stsp
7471 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7472 8b692cd0 2019-10-21 stsp
7473 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7474 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7475 8b692cd0 2019-10-21 stsp err = unlockerr;
7476 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7477 8b692cd0 2019-10-21 stsp
7478 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7479 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7480 8b692cd0 2019-10-21 stsp err = unlockerr;
7481 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7482 10604dce 2021-09-24 thomas
7483 10604dce 2021-09-24 thomas return err;
7484 10604dce 2021-09-24 thomas }
7485 10604dce 2021-09-24 thomas
7486 10604dce 2021-09-24 thomas const struct got_error *
7487 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7488 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7489 10604dce 2021-09-24 thomas {
7490 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7491 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7492 10604dce 2021-09-24 thomas
7493 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7494 10604dce 2021-09-24 thomas if (err)
7495 10604dce 2021-09-24 thomas goto done;
7496 8b692cd0 2019-10-21 stsp
7497 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7498 10604dce 2021-09-24 thomas
7499 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7500 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7501 10604dce 2021-09-24 thomas err = sync_err;
7502 10604dce 2021-09-24 thomas done:
7503 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7504 10604dce 2021-09-24 thomas free(fileindex_path);
7505 8b692cd0 2019-10-21 stsp return err;
7506 2822a352 2019-10-15 stsp }
7507 2822a352 2019-10-15 stsp
7508 10604dce 2021-09-24 thomas static const struct got_error *
7509 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7510 10604dce 2021-09-24 thomas {
7511 10604dce 2021-09-24 thomas const struct got_error *err;
7512 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7513 10604dce 2021-09-24 thomas
7514 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7515 10604dce 2021-09-24 thomas if (err)
7516 10604dce 2021-09-24 thomas goto done;
7517 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7518 10604dce 2021-09-24 thomas if (err)
7519 10604dce 2021-09-24 thomas goto done;
7520 10604dce 2021-09-24 thomas
7521 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7522 10604dce 2021-09-24 thomas if (err)
7523 10604dce 2021-09-24 thomas goto done;
7524 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7525 10604dce 2021-09-24 thomas if (err)
7526 10604dce 2021-09-24 thomas goto done;
7527 10604dce 2021-09-24 thomas
7528 10604dce 2021-09-24 thomas done:
7529 10604dce 2021-09-24 thomas free(branch_refname);
7530 10604dce 2021-09-24 thomas free(commit_refname);
7531 10604dce 2021-09-24 thomas return err;
7532 10604dce 2021-09-24 thomas }
7533 10604dce 2021-09-24 thomas
7534 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7535 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7536 10604dce 2021-09-24 thomas const char *branch_name;
7537 10604dce 2021-09-24 thomas };
7538 10604dce 2021-09-24 thomas
7539 10604dce 2021-09-24 thomas static const struct got_error *
7540 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7541 10604dce 2021-09-24 thomas void *arg)
7542 10604dce 2021-09-24 thomas {
7543 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7544 10604dce 2021-09-24 thomas
7545 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7546 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7547 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7548 10604dce 2021-09-24 thomas
7549 10604dce 2021-09-24 thomas return NULL;
7550 10604dce 2021-09-24 thomas }
7551 10604dce 2021-09-24 thomas
7552 10604dce 2021-09-24 thomas
7553 10604dce 2021-09-24 thomas const struct got_error *
7554 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7555 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7556 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7557 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7558 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7559 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7560 10604dce 2021-09-24 thomas {
7561 10604dce 2021-09-24 thomas const struct got_error *err;
7562 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7563 10604dce 2021-09-24 thomas
7564 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7565 10604dce 2021-09-24 thomas if (err)
7566 10604dce 2021-09-24 thomas goto done;
7567 10604dce 2021-09-24 thomas
7568 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7569 10604dce 2021-09-24 thomas worktree);
7570 10604dce 2021-09-24 thomas if (err)
7571 10604dce 2021-09-24 thomas goto done;
7572 10604dce 2021-09-24 thomas
7573 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7574 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7575 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7576 10604dce 2021-09-24 thomas done:
7577 10604dce 2021-09-24 thomas free(fileindex_path);
7578 10604dce 2021-09-24 thomas return err;
7579 10604dce 2021-09-24 thomas }
7580 10604dce 2021-09-24 thomas
7581 10604dce 2021-09-24 thomas const struct got_error *
7582 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7583 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7584 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7585 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7586 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7587 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7588 ae1e948a 2021-09-28 thomas
7589 10604dce 2021-09-24 thomas {
7590 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7591 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7592 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7593 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7594 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7595 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7596 10604dce 2021-09-24 thomas int have_staged_files = 0;
7597 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7598 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7599 10604dce 2021-09-24 thomas
7600 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7601 10604dce 2021-09-24 thomas
7602 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7603 10604dce 2021-09-24 thomas
7604 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7605 10604dce 2021-09-24 thomas if (err)
7606 10604dce 2021-09-24 thomas goto done;
7607 10604dce 2021-09-24 thomas
7608 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7609 10604dce 2021-09-24 thomas if (err)
7610 10604dce 2021-09-24 thomas goto done;
7611 10604dce 2021-09-24 thomas
7612 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7613 10604dce 2021-09-24 thomas if (err)
7614 10604dce 2021-09-24 thomas goto done;
7615 10604dce 2021-09-24 thomas
7616 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7617 10604dce 2021-09-24 thomas &have_staged_files);
7618 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7619 10604dce 2021-09-24 thomas goto done;
7620 10604dce 2021-09-24 thomas if (have_staged_files) {
7621 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7622 10604dce 2021-09-24 thomas goto done;
7623 10604dce 2021-09-24 thomas }
7624 10604dce 2021-09-24 thomas
7625 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7626 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7627 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7628 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7629 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7630 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7631 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7632 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7633 10604dce 2021-09-24 thomas if (err)
7634 10604dce 2021-09-24 thomas goto done;
7635 10604dce 2021-09-24 thomas
7636 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7637 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7638 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7639 10604dce 2021-09-24 thomas goto done;
7640 10604dce 2021-09-24 thomas }
7641 10604dce 2021-09-24 thomas
7642 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7643 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7644 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7645 10604dce 2021-09-24 thomas
7646 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7647 10604dce 2021-09-24 thomas ct_path++;
7648 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7649 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7650 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7651 10604dce 2021-09-24 thomas if (err)
7652 10604dce 2021-09-24 thomas goto done;
7653 10604dce 2021-09-24 thomas
7654 10604dce 2021-09-24 thomas }
7655 10604dce 2021-09-24 thomas
7656 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7657 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7658 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7659 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7660 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7661 10604dce 2021-09-24 thomas if (err)
7662 10604dce 2021-09-24 thomas goto done;
7663 10604dce 2021-09-24 thomas
7664 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7665 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7666 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7667 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7668 10604dce 2021-09-24 thomas err = sync_err;
7669 10604dce 2021-09-24 thomas done:
7670 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7671 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7672 10604dce 2021-09-24 thomas free_commitable(ct);
7673 10604dce 2021-09-24 thomas }
7674 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7675 10604dce 2021-09-24 thomas free(fileindex_path);
7676 10604dce 2021-09-24 thomas return err;
7677 10604dce 2021-09-24 thomas }
7678 10604dce 2021-09-24 thomas
7679 10604dce 2021-09-24 thomas const struct got_error *
7680 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7681 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7682 10604dce 2021-09-24 thomas {
7683 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7684 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7685 10604dce 2021-09-24 thomas
7686 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7687 10604dce 2021-09-24 thomas if (err)
7688 10604dce 2021-09-24 thomas goto done;
7689 10604dce 2021-09-24 thomas
7690 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7691 10604dce 2021-09-24 thomas if (err)
7692 10604dce 2021-09-24 thomas goto done;
7693 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7694 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7695 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7696 10604dce 2021-09-24 thomas err = sync_err;
7697 10604dce 2021-09-24 thomas done:
7698 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7699 10604dce 2021-09-24 thomas free(fileindex_path);
7700 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7701 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7702 10604dce 2021-09-24 thomas err = unlockerr;
7703 10604dce 2021-09-24 thomas return err;
7704 10604dce 2021-09-24 thomas }
7705 10604dce 2021-09-24 thomas
7706 10604dce 2021-09-24 thomas const struct got_error *
7707 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7708 10604dce 2021-09-24 thomas struct got_repository *repo)
7709 10604dce 2021-09-24 thomas {
7710 10604dce 2021-09-24 thomas const struct got_error *err;
7711 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7712 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7713 10604dce 2021-09-24 thomas
7714 10604dce 2021-09-24 thomas *in_progress = 0;
7715 10604dce 2021-09-24 thomas
7716 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7717 10604dce 2021-09-24 thomas if (err)
7718 10604dce 2021-09-24 thomas return err;
7719 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7720 dfe86d1f 2021-09-24 thomas free(branch_refname);
7721 10604dce 2021-09-24 thomas if (err) {
7722 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7723 10604dce 2021-09-24 thomas return err;
7724 10604dce 2021-09-24 thomas } else
7725 10604dce 2021-09-24 thomas *in_progress = 1;
7726 10604dce 2021-09-24 thomas
7727 10604dce 2021-09-24 thomas return NULL;
7728 10604dce 2021-09-24 thomas }
7729 10604dce 2021-09-24 thomas
7730 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7731 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7732 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7733 10604dce 2021-09-24 thomas {
7734 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7735 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7736 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7737 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7738 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7739 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7740 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7741 10604dce 2021-09-24 thomas
7742 10604dce 2021-09-24 thomas *fileindex = NULL;
7743 10604dce 2021-09-24 thomas
7744 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7745 10604dce 2021-09-24 thomas if (err)
7746 10604dce 2021-09-24 thomas return err;
7747 10604dce 2021-09-24 thomas
7748 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7749 10604dce 2021-09-24 thomas if (err)
7750 10604dce 2021-09-24 thomas goto done;
7751 10604dce 2021-09-24 thomas
7752 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7753 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7754 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7755 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7756 10604dce 2021-09-24 thomas &ok_arg);
7757 10604dce 2021-09-24 thomas if (err)
7758 10604dce 2021-09-24 thomas goto done;
7759 10604dce 2021-09-24 thomas
7760 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7761 10604dce 2021-09-24 thomas if (err)
7762 10604dce 2021-09-24 thomas return err;
7763 10604dce 2021-09-24 thomas
7764 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7765 10604dce 2021-09-24 thomas if (err)
7766 10604dce 2021-09-24 thomas return err;
7767 10604dce 2021-09-24 thomas
7768 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7769 10604dce 2021-09-24 thomas 0);
7770 10604dce 2021-09-24 thomas if (err)
7771 10604dce 2021-09-24 thomas goto done;
7772 10604dce 2021-09-24 thomas
7773 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7774 10604dce 2021-09-24 thomas if (err)
7775 10604dce 2021-09-24 thomas goto done;
7776 10604dce 2021-09-24 thomas
7777 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7778 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7779 10604dce 2021-09-24 thomas goto done;
7780 10604dce 2021-09-24 thomas }
7781 10604dce 2021-09-24 thomas
7782 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7783 10604dce 2021-09-24 thomas if (err)
7784 10604dce 2021-09-24 thomas goto done;
7785 10604dce 2021-09-24 thomas
7786 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7787 10604dce 2021-09-24 thomas if (err)
7788 10604dce 2021-09-24 thomas goto done;
7789 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7790 10604dce 2021-09-24 thomas if (err)
7791 10604dce 2021-09-24 thomas goto done;
7792 10604dce 2021-09-24 thomas
7793 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7794 10604dce 2021-09-24 thomas if (err)
7795 10604dce 2021-09-24 thomas goto done;
7796 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7797 10604dce 2021-09-24 thomas if (err)
7798 10604dce 2021-09-24 thomas goto done;
7799 10604dce 2021-09-24 thomas
7800 10604dce 2021-09-24 thomas done:
7801 10604dce 2021-09-24 thomas free(branch_refname);
7802 10604dce 2021-09-24 thomas free(commit_refname);
7803 10604dce 2021-09-24 thomas free(fileindex_path);
7804 10604dce 2021-09-24 thomas if (branch_ref)
7805 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7806 10604dce 2021-09-24 thomas if (commit_ref)
7807 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7808 10604dce 2021-09-24 thomas if (wt_branch)
7809 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7810 10604dce 2021-09-24 thomas free(wt_branch_tip);
7811 10604dce 2021-09-24 thomas if (err) {
7812 10604dce 2021-09-24 thomas if (*fileindex) {
7813 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7814 10604dce 2021-09-24 thomas *fileindex = NULL;
7815 10604dce 2021-09-24 thomas }
7816 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7817 10604dce 2021-09-24 thomas }
7818 10604dce 2021-09-24 thomas return err;
7819 10604dce 2021-09-24 thomas }
7820 10604dce 2021-09-24 thomas
7821 10604dce 2021-09-24 thomas const struct got_error *
7822 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7823 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7824 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7825 10604dce 2021-09-24 thomas {
7826 10604dce 2021-09-24 thomas const struct got_error *err;
7827 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7828 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7829 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7830 10604dce 2021-09-24 thomas int have_staged_files = 0;
7831 10604dce 2021-09-24 thomas
7832 10604dce 2021-09-24 thomas *branch_name = NULL;
7833 10604dce 2021-09-24 thomas *branch_tip = NULL;
7834 10604dce 2021-09-24 thomas *fileindex = NULL;
7835 10604dce 2021-09-24 thomas
7836 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7837 10604dce 2021-09-24 thomas if (err)
7838 10604dce 2021-09-24 thomas return err;
7839 10604dce 2021-09-24 thomas
7840 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7841 10604dce 2021-09-24 thomas if (err)
7842 10604dce 2021-09-24 thomas goto done;
7843 10604dce 2021-09-24 thomas
7844 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7845 10604dce 2021-09-24 thomas &have_staged_files);
7846 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7847 10604dce 2021-09-24 thomas goto done;
7848 10604dce 2021-09-24 thomas if (have_staged_files) {
7849 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7850 10604dce 2021-09-24 thomas goto done;
7851 10604dce 2021-09-24 thomas }
7852 10604dce 2021-09-24 thomas
7853 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7854 10604dce 2021-09-24 thomas if (err)
7855 10604dce 2021-09-24 thomas goto done;
7856 10604dce 2021-09-24 thomas
7857 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7858 10604dce 2021-09-24 thomas if (err)
7859 10604dce 2021-09-24 thomas goto done;
7860 10604dce 2021-09-24 thomas
7861 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7862 10604dce 2021-09-24 thomas if (err)
7863 10604dce 2021-09-24 thomas goto done;
7864 10604dce 2021-09-24 thomas
7865 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7866 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7867 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7868 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7869 10604dce 2021-09-24 thomas goto done;
7870 10604dce 2021-09-24 thomas }
7871 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7872 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7873 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7874 10604dce 2021-09-24 thomas goto done;
7875 10604dce 2021-09-24 thomas }
7876 10604dce 2021-09-24 thomas
7877 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7878 10604dce 2021-09-24 thomas if (err)
7879 10604dce 2021-09-24 thomas goto done;
7880 10604dce 2021-09-24 thomas
7881 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7882 10604dce 2021-09-24 thomas if (err)
7883 10604dce 2021-09-24 thomas goto done;
7884 10604dce 2021-09-24 thomas done:
7885 10604dce 2021-09-24 thomas free(commit_refname);
7886 10604dce 2021-09-24 thomas free(branch_refname);
7887 10604dce 2021-09-24 thomas free(fileindex_path);
7888 10604dce 2021-09-24 thomas if (commit_ref)
7889 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7890 10604dce 2021-09-24 thomas if (branch_ref)
7891 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7892 10604dce 2021-09-24 thomas if (err) {
7893 10604dce 2021-09-24 thomas if (*branch_name) {
7894 10604dce 2021-09-24 thomas free(*branch_name);
7895 10604dce 2021-09-24 thomas *branch_name = NULL;
7896 10604dce 2021-09-24 thomas }
7897 10604dce 2021-09-24 thomas free(*branch_tip);
7898 10604dce 2021-09-24 thomas *branch_tip = NULL;
7899 10604dce 2021-09-24 thomas if (*fileindex) {
7900 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7901 10604dce 2021-09-24 thomas *fileindex = NULL;
7902 10604dce 2021-09-24 thomas }
7903 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7904 10604dce 2021-09-24 thomas }
7905 10604dce 2021-09-24 thomas return err;
7906 10604dce 2021-09-24 thomas }
7907 10604dce 2021-09-24 thomas
7908 10604dce 2021-09-24 thomas const struct got_error *
7909 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7910 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7911 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7912 10604dce 2021-09-24 thomas {
7913 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7914 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7915 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7916 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7917 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7918 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7919 10604dce 2021-09-24 thomas
7920 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7921 945f9229 2022-04-16 thomas worktree->base_commit_id);
7922 945f9229 2022-04-16 thomas if (err)
7923 945f9229 2022-04-16 thomas goto done;
7924 945f9229 2022-04-16 thomas
7925 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7926 945f9229 2022-04-16 thomas worktree->path_prefix);
7927 10604dce 2021-09-24 thomas if (err)
7928 10604dce 2021-09-24 thomas goto done;
7929 10604dce 2021-09-24 thomas
7930 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
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 = get_fileindex_path(&fileindex_path, worktree);
7935 10604dce 2021-09-24 thomas if (err)
7936 10604dce 2021-09-24 thomas goto done;
7937 10604dce 2021-09-24 thomas
7938 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7939 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7940 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7941 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7942 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7943 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7944 10604dce 2021-09-24 thomas rfa.repo = repo;
7945 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7946 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7947 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7948 10604dce 2021-09-24 thomas if (err)
7949 10604dce 2021-09-24 thomas goto sync;
7950 10604dce 2021-09-24 thomas
7951 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7952 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7953 10604dce 2021-09-24 thomas sync:
7954 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7955 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7956 10604dce 2021-09-24 thomas err = sync_err;
7957 10604dce 2021-09-24 thomas done:
7958 10604dce 2021-09-24 thomas free(tree_id);
7959 10604dce 2021-09-24 thomas free(commit_id);
7960 945f9229 2022-04-16 thomas if (commit)
7961 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7962 10604dce 2021-09-24 thomas if (fileindex)
7963 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7964 10604dce 2021-09-24 thomas free(fileindex_path);
7965 10604dce 2021-09-24 thomas
7966 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7967 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7968 10604dce 2021-09-24 thomas err = unlockerr;
7969 10604dce 2021-09-24 thomas return err;
7970 10604dce 2021-09-24 thomas }
7971 10604dce 2021-09-24 thomas
7972 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7973 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7974 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7975 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7976 2db2652d 2019-08-07 stsp struct got_repository *repo;
7977 2db2652d 2019-08-07 stsp int have_changes;
7978 2db2652d 2019-08-07 stsp };
7979 2db2652d 2019-08-07 stsp
7980 ef20f542 2022-06-26 thomas static const struct got_error *
7981 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7982 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7983 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7984 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7985 735ef5ac 2019-08-03 stsp {
7986 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7987 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7988 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7989 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7990 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7991 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7992 8b13ce36 2019-08-08 stsp
7993 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7994 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7995 8b13ce36 2019-08-08 stsp return NULL;
7996 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7997 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7998 735ef5ac 2019-08-03 stsp
7999 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8000 735ef5ac 2019-08-03 stsp if (ie == NULL)
8001 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8002 735ef5ac 2019-08-03 stsp
8003 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8004 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8005 735ef5ac 2019-08-03 stsp relpath) == -1)
8006 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8007 735ef5ac 2019-08-03 stsp
8008 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8009 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8010 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8011 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8012 735ef5ac 2019-08-03 stsp }
8013 735ef5ac 2019-08-03 stsp
8014 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8015 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8016 3aa5969e 2019-08-06 stsp goto done;
8017 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8018 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8019 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8020 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8021 735ef5ac 2019-08-03 stsp goto done;
8022 3aa5969e 2019-08-06 stsp }
8023 735ef5ac 2019-08-03 stsp
8024 2db2652d 2019-08-07 stsp a->have_changes = 1;
8025 2db2652d 2019-08-07 stsp
8026 735ef5ac 2019-08-03 stsp p = in_repo_path;
8027 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8028 735ef5ac 2019-08-03 stsp p++;
8029 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8030 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8031 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8032 735ef5ac 2019-08-03 stsp done:
8033 735ef5ac 2019-08-03 stsp free(in_repo_path);
8034 dc424a06 2019-08-07 stsp return err;
8035 dc424a06 2019-08-07 stsp }
8036 dc424a06 2019-08-07 stsp
8037 2db2652d 2019-08-07 stsp struct stage_path_arg {
8038 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8039 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8040 2db2652d 2019-08-07 stsp struct got_repository *repo;
8041 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8042 2db2652d 2019-08-07 stsp void *status_arg;
8043 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8044 2db2652d 2019-08-07 stsp void *patch_arg;
8045 7b5dc508 2019-10-28 stsp int staged_something;
8046 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8047 2db2652d 2019-08-07 stsp };
8048 2db2652d 2019-08-07 stsp
8049 2db2652d 2019-08-07 stsp static const struct got_error *
8050 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8051 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8052 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8053 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8054 0cb83759 2019-08-03 stsp {
8055 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8056 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8057 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8058 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8059 0cb83759 2019-08-03 stsp uint32_t stage;
8060 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8061 0aeb8099 2020-07-23 stsp struct stat sb;
8062 8b13ce36 2019-08-08 stsp
8063 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8064 8b13ce36 2019-08-08 stsp return NULL;
8065 0cb83759 2019-08-03 stsp
8066 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8067 d3e7c587 2019-08-03 stsp if (ie == NULL)
8068 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8069 0cb83759 2019-08-03 stsp
8070 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8071 2db2652d 2019-08-07 stsp relpath)== -1)
8072 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8073 0cb83759 2019-08-03 stsp
8074 0cb83759 2019-08-03 stsp switch (status) {
8075 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8076 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8077 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8078 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8079 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8080 0aeb8099 2020-07-23 stsp break;
8081 0aeb8099 2020-07-23 stsp }
8082 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8083 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8084 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8085 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8086 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8087 dc424a06 2019-08-07 stsp if (err)
8088 dc424a06 2019-08-07 stsp break;
8089 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8090 dc424a06 2019-08-07 stsp break;
8091 dc424a06 2019-08-07 stsp } else {
8092 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8093 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8094 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8095 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8096 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8097 dc424a06 2019-08-07 stsp break;
8098 dc424a06 2019-08-07 stsp }
8099 dc424a06 2019-08-07 stsp }
8100 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8101 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8102 0cb83759 2019-08-03 stsp if (err)
8103 dc424a06 2019-08-07 stsp break;
8104 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8105 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8106 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8107 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8108 0cb83759 2019-08-03 stsp else
8109 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8110 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8111 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8112 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8113 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8114 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8115 35213c7c 2020-07-23 stsp ssize_t target_len;
8116 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8117 35213c7c 2020-07-23 stsp sizeof(target_path));
8118 35213c7c 2020-07-23 stsp if (target_len == -1) {
8119 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8120 35213c7c 2020-07-23 stsp ondisk_path);
8121 35213c7c 2020-07-23 stsp break;
8122 35213c7c 2020-07-23 stsp }
8123 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8124 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8125 35213c7c 2020-07-23 stsp a->worktree->root_path);
8126 35213c7c 2020-07-23 stsp if (err)
8127 35213c7c 2020-07-23 stsp break;
8128 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8129 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8130 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8131 35213c7c 2020-07-23 stsp break;
8132 35213c7c 2020-07-23 stsp }
8133 35213c7c 2020-07-23 stsp }
8134 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8135 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8136 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8137 35213c7c 2020-07-23 stsp else
8138 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8139 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8140 0aeb8099 2020-07-23 stsp } else {
8141 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8142 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8143 0aeb8099 2020-07-23 stsp }
8144 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8145 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8146 dc424a06 2019-08-07 stsp break;
8147 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8148 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8149 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8150 f289c82c 2022-06-13 thomas if (err)
8151 f289c82c 2022-06-13 thomas break;
8152 f289c82c 2022-06-13 thomas /*
8153 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8154 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8155 f289c82c 2022-06-13 thomas */
8156 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8157 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8158 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8159 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8160 f289c82c 2022-06-13 thomas }
8161 0cb83759 2019-08-03 stsp break;
8162 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8163 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8164 d3e7c587 2019-08-03 stsp break;
8165 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8166 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8167 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8168 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8169 dc424a06 2019-08-07 stsp if (err)
8170 dc424a06 2019-08-07 stsp break;
8171 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8172 88f33a19 2019-08-08 stsp break;
8173 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8174 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8175 dc424a06 2019-08-07 stsp break;
8176 88f33a19 2019-08-08 stsp }
8177 dc424a06 2019-08-07 stsp }
8178 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8179 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8180 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8181 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8182 dc424a06 2019-08-07 stsp break;
8183 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8184 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8185 12463d8b 2019-12-13 stsp de_name);
8186 0cb83759 2019-08-03 stsp break;
8187 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8188 d3e7c587 2019-08-03 stsp break;
8189 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8190 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8191 ebf48fd5 2019-08-03 stsp break;
8192 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8193 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8194 2a06fe5f 2019-08-24 stsp break;
8195 0cb83759 2019-08-03 stsp default:
8196 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8197 537ac44b 2019-08-03 stsp break;
8198 0cb83759 2019-08-03 stsp }
8199 dc424a06 2019-08-07 stsp
8200 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8201 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8202 dc424a06 2019-08-07 stsp free(path_content);
8203 2db2652d 2019-08-07 stsp free(ondisk_path);
8204 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8205 0ebf8283 2019-07-24 stsp return err;
8206 0ebf8283 2019-07-24 stsp }
8207 0cb83759 2019-08-03 stsp
8208 0cb83759 2019-08-03 stsp const struct got_error *
8209 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8210 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8211 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8212 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8213 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8214 0cb83759 2019-08-03 stsp {
8215 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8216 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8217 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8218 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8219 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8220 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8221 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8222 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8223 0cb83759 2019-08-03 stsp
8224 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8225 0cb83759 2019-08-03 stsp if (err)
8226 0cb83759 2019-08-03 stsp return err;
8227 0cb83759 2019-08-03 stsp
8228 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8229 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8230 735ef5ac 2019-08-03 stsp if (err)
8231 735ef5ac 2019-08-03 stsp goto done;
8232 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8233 735ef5ac 2019-08-03 stsp if (err)
8234 735ef5ac 2019-08-03 stsp goto done;
8235 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8236 0cb83759 2019-08-03 stsp if (err)
8237 0cb83759 2019-08-03 stsp goto done;
8238 0cb83759 2019-08-03 stsp
8239 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8240 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8241 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8242 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8243 2db2652d 2019-08-07 stsp oka.repo = repo;
8244 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8245 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8246 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8247 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8248 735ef5ac 2019-08-03 stsp if (err)
8249 735ef5ac 2019-08-03 stsp goto done;
8250 735ef5ac 2019-08-03 stsp }
8251 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8252 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8253 2db2652d 2019-08-07 stsp goto done;
8254 2db2652d 2019-08-07 stsp }
8255 735ef5ac 2019-08-03 stsp
8256 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8257 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8258 2db2652d 2019-08-07 stsp spa.repo = repo;
8259 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8260 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8261 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8262 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8263 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8264 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8265 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8266 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8267 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8268 42005733 2019-08-03 stsp if (err)
8269 2db2652d 2019-08-07 stsp goto done;
8270 7b5dc508 2019-10-28 stsp }
8271 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8272 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8273 7b5dc508 2019-10-28 stsp goto done;
8274 0cb83759 2019-08-03 stsp }
8275 0cb83759 2019-08-03 stsp
8276 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8277 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8278 0cb83759 2019-08-03 stsp err = sync_err;
8279 0cb83759 2019-08-03 stsp done:
8280 735ef5ac 2019-08-03 stsp if (head_ref)
8281 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8282 735ef5ac 2019-08-03 stsp free(head_commit_id);
8283 0cb83759 2019-08-03 stsp free(fileindex_path);
8284 0cb83759 2019-08-03 stsp if (fileindex)
8285 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8286 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8287 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8288 0cb83759 2019-08-03 stsp err = unlockerr;
8289 0cb83759 2019-08-03 stsp return err;
8290 0cb83759 2019-08-03 stsp }
8291 ad493afc 2019-08-03 stsp
8292 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8293 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8294 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8295 ad493afc 2019-08-03 stsp struct got_repository *repo;
8296 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8297 ad493afc 2019-08-03 stsp void *progress_arg;
8298 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8299 2e1f37b0 2019-08-08 stsp void *patch_arg;
8300 ad493afc 2019-08-03 stsp };
8301 2e1f37b0 2019-08-08 stsp
8302 2e1f37b0 2019-08-08 stsp static const struct got_error *
8303 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8304 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8305 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8306 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8307 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8308 2e1f37b0 2019-08-08 stsp {
8309 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8310 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8311 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8312 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8313 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8314 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8315 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8316 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8317 2e1f37b0 2019-08-08 stsp
8318 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8319 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8320 2e1f37b0 2019-08-08 stsp
8321 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8322 2e1f37b0 2019-08-08 stsp if (err)
8323 2e1f37b0 2019-08-08 stsp return err;
8324 f4ae6ddb 2022-07-01 thomas
8325 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8326 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8327 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8328 f4ae6ddb 2022-07-01 thomas goto done;
8329 f4ae6ddb 2022-07-01 thomas }
8330 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8331 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8332 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8333 f4ae6ddb 2022-07-01 thomas goto done;
8334 f4ae6ddb 2022-07-01 thomas }
8335 f4ae6ddb 2022-07-01 thomas
8336 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8337 2e1f37b0 2019-08-08 stsp if (err)
8338 2e1f37b0 2019-08-08 stsp goto done;
8339 2e1f37b0 2019-08-08 stsp
8340 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8341 2e1f37b0 2019-08-08 stsp if (err)
8342 2e1f37b0 2019-08-08 stsp goto done;
8343 2e1f37b0 2019-08-08 stsp
8344 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8345 2e1f37b0 2019-08-08 stsp if (err)
8346 2e1f37b0 2019-08-08 stsp goto done;
8347 2e1f37b0 2019-08-08 stsp
8348 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8349 f4ae6ddb 2022-07-01 thomas fd2);
8350 2e1f37b0 2019-08-08 stsp if (err)
8351 2e1f37b0 2019-08-08 stsp goto done;
8352 2e1f37b0 2019-08-08 stsp
8353 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8354 2e1f37b0 2019-08-08 stsp if (err)
8355 2e1f37b0 2019-08-08 stsp goto done;
8356 ad493afc 2019-08-03 stsp
8357 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8358 2e1f37b0 2019-08-08 stsp if (err)
8359 2e1f37b0 2019-08-08 stsp goto done;
8360 2e1f37b0 2019-08-08 stsp
8361 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8362 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8363 2e1f37b0 2019-08-08 stsp if (err)
8364 2e1f37b0 2019-08-08 stsp goto done;
8365 2e1f37b0 2019-08-08 stsp
8366 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8367 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8368 2e1f37b0 2019-08-08 stsp if (err)
8369 2e1f37b0 2019-08-08 stsp goto done;
8370 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8371 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8372 2e1f37b0 2019-08-08 stsp if (err)
8373 2e1f37b0 2019-08-08 stsp goto done;
8374 2e1f37b0 2019-08-08 stsp
8375 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8376 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8377 2e1f37b0 2019-08-08 stsp goto done;
8378 2e1f37b0 2019-08-08 stsp }
8379 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8380 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8381 2e1f37b0 2019-08-08 stsp goto done;
8382 2e1f37b0 2019-08-08 stsp }
8383 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8384 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8385 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8386 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8387 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8388 fe621944 2020-11-10 stsp nchanges++;
8389 fe621944 2020-11-10 stsp }
8390 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8391 2e1f37b0 2019-08-08 stsp int choice;
8392 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8393 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8394 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8395 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8396 2e1f37b0 2019-08-08 stsp if (err)
8397 2e1f37b0 2019-08-08 stsp goto done;
8398 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8399 2e1f37b0 2019-08-08 stsp have_content = 1;
8400 19e4b907 2019-08-08 stsp else
8401 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8402 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8403 2e1f37b0 2019-08-08 stsp break;
8404 2e1f37b0 2019-08-08 stsp }
8405 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8406 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8407 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8408 2e1f37b0 2019-08-08 stsp done:
8409 2e1f37b0 2019-08-08 stsp free(label1);
8410 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8411 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8412 2e1f37b0 2019-08-08 stsp if (blob)
8413 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8414 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8415 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8416 2e1f37b0 2019-08-08 stsp if (staged_blob)
8417 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8418 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8419 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8420 fe621944 2020-11-10 stsp err = free_err;
8421 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8422 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8423 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8424 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8425 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8426 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8427 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8428 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8429 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8430 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8431 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8432 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8433 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8434 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8435 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8436 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8437 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8438 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8439 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8440 2e1f37b0 2019-08-08 stsp }
8441 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8442 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8443 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8444 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8445 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8446 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8447 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8448 2e1f37b0 2019-08-08 stsp }
8449 2e1f37b0 2019-08-08 stsp free(path1);
8450 2e1f37b0 2019-08-08 stsp free(path2);
8451 fda8017d 2020-07-23 stsp return err;
8452 fda8017d 2020-07-23 stsp }
8453 fda8017d 2020-07-23 stsp
8454 fda8017d 2020-07-23 stsp static const struct got_error *
8455 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8456 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8457 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8458 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8459 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8460 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8461 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8462 fda8017d 2020-07-23 stsp {
8463 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8464 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8465 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8466 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8467 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8468 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8469 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8470 fda8017d 2020-07-23 stsp struct stat sb;
8471 fda8017d 2020-07-23 stsp
8472 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8473 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8474 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8475 fda8017d 2020-07-23 stsp if (err)
8476 fda8017d 2020-07-23 stsp return err;
8477 fda8017d 2020-07-23 stsp
8478 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8479 fda8017d 2020-07-23 stsp return NULL;
8480 fda8017d 2020-07-23 stsp
8481 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8482 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8483 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8484 fda8017d 2020-07-23 stsp if (err)
8485 fda8017d 2020-07-23 stsp goto done;
8486 fda8017d 2020-07-23 stsp }
8487 fda8017d 2020-07-23 stsp
8488 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8489 fda8017d 2020-07-23 stsp if (f == NULL) {
8490 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8491 fda8017d 2020-07-23 stsp path_unstaged_content);
8492 fda8017d 2020-07-23 stsp goto done;
8493 fda8017d 2020-07-23 stsp }
8494 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8495 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8496 fda8017d 2020-07-23 stsp goto done;
8497 fda8017d 2020-07-23 stsp }
8498 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8499 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8500 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8501 fda8017d 2020-07-23 stsp size_t r;
8502 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8503 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8504 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8505 fda8017d 2020-07-23 stsp goto done;
8506 fda8017d 2020-07-23 stsp }
8507 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8508 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8509 fda8017d 2020-07-23 stsp goto done;
8510 fda8017d 2020-07-23 stsp }
8511 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8512 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8513 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8514 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8515 fda8017d 2020-07-23 stsp progress_arg);
8516 fda8017d 2020-07-23 stsp } else {
8517 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8518 67a66647 2021-05-31 stsp
8519 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8520 67a66647 2021-05-31 stsp if (err)
8521 67a66647 2021-05-31 stsp return err;
8522 67a66647 2021-05-31 stsp
8523 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8524 67a66647 2021-05-31 stsp parent) == -1) {
8525 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8526 67a66647 2021-05-31 stsp base_path = NULL;
8527 67a66647 2021-05-31 stsp goto done;
8528 67a66647 2021-05-31 stsp }
8529 67a66647 2021-05-31 stsp
8530 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8531 67a66647 2021-05-31 stsp if (err)
8532 67a66647 2021-05-31 stsp goto done;
8533 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8534 67a66647 2021-05-31 stsp blob_base);
8535 67a66647 2021-05-31 stsp if (err)
8536 67a66647 2021-05-31 stsp goto done;
8537 67a66647 2021-05-31 stsp
8538 eec2f5a9 2021-06-03 stsp /*
8539 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8540 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8541 eec2f5a9 2021-06-03 stsp */
8542 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8543 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8544 eec2f5a9 2021-06-03 stsp ondisk_path);
8545 eec2f5a9 2021-06-03 stsp if (err)
8546 eec2f5a9 2021-06-03 stsp goto done;
8547 eec2f5a9 2021-06-03 stsp } else {
8548 eec2f5a9 2021-06-03 stsp int fd;
8549 06340621 2021-12-31 thomas fd = open(ondisk_path,
8550 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8551 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8552 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8553 eec2f5a9 2021-06-03 stsp goto done;
8554 eec2f5a9 2021-06-03 stsp }
8555 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8556 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8557 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8558 eec2f5a9 2021-06-03 stsp close(fd);
8559 eec2f5a9 2021-06-03 stsp goto done;
8560 eec2f5a9 2021-06-03 stsp }
8561 eec2f5a9 2021-06-03 stsp }
8562 eec2f5a9 2021-06-03 stsp
8563 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8564 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8565 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8566 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8567 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8568 fda8017d 2020-07-23 stsp }
8569 fda8017d 2020-07-23 stsp if (err)
8570 fda8017d 2020-07-23 stsp goto done;
8571 fda8017d 2020-07-23 stsp
8572 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8573 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8574 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8575 2ac8aa02 2020-11-09 stsp } else {
8576 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8577 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8578 2ac8aa02 2020-11-09 stsp }
8579 fda8017d 2020-07-23 stsp done:
8580 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8581 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8582 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8583 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8584 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8585 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8586 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8587 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8588 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8589 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8590 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8591 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8592 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8593 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8594 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8595 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8596 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8597 67a66647 2021-05-31 stsp free(blob_base_path);
8598 67a66647 2021-05-31 stsp free(parent);
8599 67a66647 2021-05-31 stsp free(base_path);
8600 2e1f37b0 2019-08-08 stsp return err;
8601 2e1f37b0 2019-08-08 stsp }
8602 2e1f37b0 2019-08-08 stsp
8603 ad493afc 2019-08-03 stsp static const struct got_error *
8604 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8605 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8606 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8607 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8608 ad493afc 2019-08-03 stsp {
8609 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8610 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8611 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8612 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8613 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8614 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8615 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8616 9bc94a15 2019-08-03 stsp struct stat sb;
8617 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8618 ad493afc 2019-08-03 stsp
8619 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8620 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8621 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8622 2e1f37b0 2019-08-08 stsp return NULL;
8623 2e1f37b0 2019-08-08 stsp
8624 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8625 ad493afc 2019-08-03 stsp if (ie == NULL)
8626 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8627 9bc94a15 2019-08-03 stsp
8628 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8629 9bc94a15 2019-08-03 stsp == -1)
8630 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8631 ad493afc 2019-08-03 stsp
8632 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8633 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8634 f69721c3 2019-10-21 stsp if (err)
8635 f69721c3 2019-10-21 stsp goto done;
8636 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8637 f69721c3 2019-10-21 stsp id_str) == -1) {
8638 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8639 f4ae6ddb 2022-07-01 thomas goto done;
8640 f4ae6ddb 2022-07-01 thomas }
8641 f4ae6ddb 2022-07-01 thomas
8642 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8643 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8644 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8645 f4ae6ddb 2022-07-01 thomas goto done;
8646 f4ae6ddb 2022-07-01 thomas }
8647 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8648 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8649 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8650 f69721c3 2019-10-21 stsp goto done;
8651 f69721c3 2019-10-21 stsp }
8652 f69721c3 2019-10-21 stsp
8653 ad493afc 2019-08-03 stsp switch (staged_status) {
8654 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8655 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8656 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8657 ad493afc 2019-08-03 stsp if (err)
8658 ad493afc 2019-08-03 stsp break;
8659 ad493afc 2019-08-03 stsp /* fall through */
8660 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8661 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8662 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8663 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8664 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8665 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8666 2e1f37b0 2019-08-08 stsp if (err)
8667 2e1f37b0 2019-08-08 stsp break;
8668 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8669 2e1f37b0 2019-08-08 stsp break;
8670 2e1f37b0 2019-08-08 stsp } else {
8671 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8672 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8673 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8674 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8675 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8676 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8677 2e1f37b0 2019-08-08 stsp }
8678 2e1f37b0 2019-08-08 stsp }
8679 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8680 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
8681 ad493afc 2019-08-03 stsp if (err)
8682 ad493afc 2019-08-03 stsp break;
8683 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8684 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8685 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8686 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8687 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8688 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8689 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8690 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8691 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8692 ea7786be 2020-07-23 stsp break;
8693 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8694 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8695 36bf999c 2020-07-23 stsp char *staged_target;
8696 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8697 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8698 36bf999c 2020-07-23 stsp if (err)
8699 36bf999c 2020-07-23 stsp goto done;
8700 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8701 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8702 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8703 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8704 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8705 36bf999c 2020-07-23 stsp free(staged_target);
8706 dfe9fba0 2020-07-23 stsp } else {
8707 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8708 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8709 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8710 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8711 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8712 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8713 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8714 dfe9fba0 2020-07-23 stsp }
8715 ea7786be 2020-07-23 stsp break;
8716 ea7786be 2020-07-23 stsp default:
8717 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8718 ea7786be 2020-07-23 stsp break;
8719 ea7786be 2020-07-23 stsp }
8720 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8721 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8722 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8723 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8724 2ac8aa02 2020-11-09 stsp }
8725 ad493afc 2019-08-03 stsp break;
8726 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8727 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8728 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8729 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8730 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8731 2e1f37b0 2019-08-08 stsp if (err)
8732 2e1f37b0 2019-08-08 stsp break;
8733 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8734 2e1f37b0 2019-08-08 stsp break;
8735 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8736 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8737 2e1f37b0 2019-08-08 stsp break;
8738 2e1f37b0 2019-08-08 stsp }
8739 2e1f37b0 2019-08-08 stsp }
8740 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8741 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8742 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8743 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8744 9bc94a15 2019-08-03 stsp if (err)
8745 9bc94a15 2019-08-03 stsp break;
8746 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8747 ad493afc 2019-08-03 stsp break;
8748 ad493afc 2019-08-03 stsp }
8749 f69721c3 2019-10-21 stsp done:
8750 ad493afc 2019-08-03 stsp free(ondisk_path);
8751 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8752 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8753 ad493afc 2019-08-03 stsp if (blob_base)
8754 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8755 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8756 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8757 ad493afc 2019-08-03 stsp if (blob_staged)
8758 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8759 f69721c3 2019-10-21 stsp free(id_str);
8760 f69721c3 2019-10-21 stsp free(label_orig);
8761 ad493afc 2019-08-03 stsp return err;
8762 ad493afc 2019-08-03 stsp }
8763 ad493afc 2019-08-03 stsp
8764 ad493afc 2019-08-03 stsp const struct got_error *
8765 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8766 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8767 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8768 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8769 ad493afc 2019-08-03 stsp struct got_repository *repo)
8770 ad493afc 2019-08-03 stsp {
8771 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8772 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8773 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8774 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8775 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8776 ad493afc 2019-08-03 stsp
8777 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8778 ad493afc 2019-08-03 stsp if (err)
8779 ad493afc 2019-08-03 stsp return err;
8780 ad493afc 2019-08-03 stsp
8781 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8782 ad493afc 2019-08-03 stsp if (err)
8783 ad493afc 2019-08-03 stsp goto done;
8784 ad493afc 2019-08-03 stsp
8785 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8786 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8787 ad493afc 2019-08-03 stsp upa.repo = repo;
8788 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8789 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8790 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8791 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8792 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8793 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8794 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8795 ad493afc 2019-08-03 stsp if (err)
8796 ad493afc 2019-08-03 stsp goto done;
8797 ad493afc 2019-08-03 stsp }
8798 ad493afc 2019-08-03 stsp
8799 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8800 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8801 ad493afc 2019-08-03 stsp err = sync_err;
8802 ad493afc 2019-08-03 stsp done:
8803 ad493afc 2019-08-03 stsp free(fileindex_path);
8804 ad493afc 2019-08-03 stsp if (fileindex)
8805 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8806 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8807 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8808 ad493afc 2019-08-03 stsp err = unlockerr;
8809 ad493afc 2019-08-03 stsp return err;
8810 ad493afc 2019-08-03 stsp }
8811 b2118c49 2020-07-28 stsp
8812 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8813 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8814 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8815 b2118c49 2020-07-28 stsp void *info_arg;
8816 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8817 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8818 b2118c49 2020-07-28 stsp void *cancel_arg;
8819 b2118c49 2020-07-28 stsp };
8820 b2118c49 2020-07-28 stsp
8821 b2118c49 2020-07-28 stsp static const struct got_error *
8822 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8823 b2118c49 2020-07-28 stsp {
8824 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8825 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8826 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8827 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8828 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8829 b2118c49 2020-07-28 stsp int stage;
8830 b2118c49 2020-07-28 stsp
8831 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8832 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8833 b2118c49 2020-07-28 stsp
8834 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8835 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8836 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8837 b2118c49 2020-07-28 stsp break;
8838 b2118c49 2020-07-28 stsp }
8839 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8840 b2118c49 2020-07-28 stsp return NULL;
8841 b2118c49 2020-07-28 stsp
8842 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8843 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8844 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8845 b2118c49 2020-07-28 stsp }
8846 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8847 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8848 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8849 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8850 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8851 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8852 b2118c49 2020-07-28 stsp }
8853 b2118c49 2020-07-28 stsp
8854 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8855 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8856 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8857 b2118c49 2020-07-28 stsp }
8858 b2118c49 2020-07-28 stsp
8859 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8860 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8861 b2118c49 2020-07-28 stsp }
8862 b2118c49 2020-07-28 stsp
8863 b2118c49 2020-07-28 stsp const struct got_error *
8864 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8865 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8866 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8867 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8868 b2118c49 2020-07-28 stsp
8869 b2118c49 2020-07-28 stsp {
8870 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8871 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8872 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8873 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8874 b2118c49 2020-07-28 stsp
8875 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8876 b2118c49 2020-07-28 stsp if (err)
8877 b2118c49 2020-07-28 stsp return err;
8878 b2118c49 2020-07-28 stsp
8879 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8880 b2118c49 2020-07-28 stsp if (err)
8881 b2118c49 2020-07-28 stsp goto done;
8882 b2118c49 2020-07-28 stsp
8883 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8884 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8885 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8886 b2118c49 2020-07-28 stsp arg.paths = paths;
8887 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8888 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8889 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8890 b2118c49 2020-07-28 stsp &arg);
8891 b2118c49 2020-07-28 stsp done:
8892 b2118c49 2020-07-28 stsp free(fileindex_path);
8893 b2118c49 2020-07-28 stsp if (fileindex)
8894 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8895 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8896 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8897 b2118c49 2020-07-28 stsp err = unlockerr;
8898 b2118c49 2020-07-28 stsp return err;
8899 b2118c49 2020-07-28 stsp }
8900 814624e7 2022-03-22 thomas
8901 814624e7 2022-03-22 thomas static const struct got_error *
8902 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
8903 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
8904 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
8905 814624e7 2022-03-22 thomas {
8906 814624e7 2022-03-22 thomas const struct got_error *err;
8907 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
8908 814624e7 2022-03-22 thomas struct stat sb;
8909 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
8910 814624e7 2022-03-22 thomas
8911 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
8912 814624e7 2022-03-22 thomas if (err)
8913 814624e7 2022-03-22 thomas return err;
8914 814624e7 2022-03-22 thomas
8915 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8916 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
8917 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
8918 814624e7 2022-03-22 thomas
8919 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8920 814624e7 2022-03-22 thomas if (ie) {
8921 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
8922 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8923 12de5570 2022-05-01 thomas repo);
8924 814624e7 2022-03-22 thomas if (err)
8925 814624e7 2022-03-22 thomas goto done;
8926 814624e7 2022-03-22 thomas } else {
8927 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
8928 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
8929 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
8930 814624e7 2022-03-22 thomas if (errno != ENOENT) {
8931 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
8932 814624e7 2022-03-22 thomas ondisk_path);
8933 814624e7 2022-03-22 thomas goto done;
8934 814624e7 2022-03-22 thomas }
8935 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
8936 814624e7 2022-03-22 thomas }
8937 814624e7 2022-03-22 thomas }
8938 814624e7 2022-03-22 thomas
8939 814624e7 2022-03-22 thomas done:
8940 814624e7 2022-03-22 thomas free(ondisk_path);
8941 814624e7 2022-03-22 thomas return err;
8942 814624e7 2022-03-22 thomas }
8943 814624e7 2022-03-22 thomas
8944 814624e7 2022-03-22 thomas static const struct got_error *
8945 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
8946 814624e7 2022-03-22 thomas unsigned char staged_status)
8947 814624e7 2022-03-22 thomas {
8948 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8949 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8950 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8951 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8952 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
8953 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
8954 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8955 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8956 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8957 814624e7 2022-03-22 thomas return NULL;
8958 814624e7 2022-03-22 thomas }
8959 814624e7 2022-03-22 thomas
8960 814624e7 2022-03-22 thomas static const struct got_error *
8961 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
8962 814624e7 2022-03-22 thomas {
8963 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
8964 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8965 814624e7 2022-03-22 thomas return NULL;
8966 814624e7 2022-03-22 thomas }
8967 814624e7 2022-03-22 thomas
8968 814624e7 2022-03-22 thomas static const struct got_error *
8969 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
8970 814624e7 2022-03-22 thomas unsigned char staged_status)
8971 814624e7 2022-03-22 thomas {
8972 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8973 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8974 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8975 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8976 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
8977 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8978 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8979 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8980 814624e7 2022-03-22 thomas return NULL;
8981 814624e7 2022-03-22 thomas }
8982 814624e7 2022-03-22 thomas
8983 814624e7 2022-03-22 thomas const struct got_error *
8984 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
8985 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
8986 814624e7 2022-03-22 thomas {
8987 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
8988 814624e7 2022-03-22 thomas }
8989 814624e7 2022-03-22 thomas
8990 814624e7 2022-03-22 thomas const struct got_error *
8991 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
8992 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
8993 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
8994 814624e7 2022-03-22 thomas {
8995 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
8996 814624e7 2022-03-22 thomas int file_renamed = 0;
8997 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
8998 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
8999 814624e7 2022-03-22 thomas
9000 814624e7 2022-03-22 thomas *oldpath = NULL;
9001 814624e7 2022-03-22 thomas *newpath = NULL;
9002 814624e7 2022-03-22 thomas
9003 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9004 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9005 814624e7 2022-03-22 thomas if (err)
9006 814624e7 2022-03-22 thomas goto done;
9007 814624e7 2022-03-22 thomas
9008 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9009 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9010 814624e7 2022-03-22 thomas if (err)
9011 814624e7 2022-03-22 thomas goto done;
9012 814624e7 2022-03-22 thomas
9013 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9014 814624e7 2022-03-22 thomas file_renamed = 1;
9015 814624e7 2022-03-22 thomas
9016 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9017 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9018 814624e7 2022-03-22 thomas else if (file_renamed) {
9019 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9020 814624e7 2022-03-22 thomas if (err == NULL)
9021 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9022 814624e7 2022-03-22 thomas } else if (old == NULL)
9023 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9024 814624e7 2022-03-22 thomas else
9025 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9026 814624e7 2022-03-22 thomas
9027 814624e7 2022-03-22 thomas done:
9028 814624e7 2022-03-22 thomas if (err) {
9029 814624e7 2022-03-22 thomas free(*oldpath);
9030 814624e7 2022-03-22 thomas *oldpath = NULL;
9031 814624e7 2022-03-22 thomas free(*newpath);
9032 814624e7 2022-03-22 thomas *newpath = NULL;
9033 814624e7 2022-03-22 thomas }
9034 814624e7 2022-03-22 thomas return err;
9035 814624e7 2022-03-22 thomas }
9036 814624e7 2022-03-22 thomas
9037 814624e7 2022-03-22 thomas const struct got_error *
9038 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9039 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9040 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9041 814624e7 2022-03-22 thomas {
9042 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9043 5e22b737 2022-05-31 thomas
9044 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9045 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9046 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9047 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9048 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9049 5e22b737 2022-05-31 thomas saa.repo = repo;
9050 5e22b737 2022-05-31 thomas
9051 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9052 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9053 814624e7 2022-03-22 thomas }
9054 5e22b737 2022-05-31 thomas
9055 5e22b737 2022-05-31 thomas const struct got_error *
9056 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9057 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9058 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9059 5e22b737 2022-05-31 thomas {
9060 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9061 5e22b737 2022-05-31 thomas
9062 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9063 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9064 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9065 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9066 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9067 5e22b737 2022-05-31 thomas sda.repo = repo;
9068 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9069 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9070 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9071 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9072 5e22b737 2022-05-31 thomas
9073 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9074 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9075 5e22b737 2022-05-31 thomas }
9076 5e22b737 2022-05-31 thomas
9077 5e22b737 2022-05-31 thomas const struct got_error *
9078 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9079 36832a8e 2022-05-31 thomas const char *fileindex_path)
9080 5e22b737 2022-05-31 thomas {
9081 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9082 5e22b737 2022-05-31 thomas
9083 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9084 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9085 5e22b737 2022-05-31 thomas
9086 5e22b737 2022-05-31 thomas return err;
9087 5e22b737 2022-05-31 thomas }