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 1338848f 2019-12-13 stsp int fd = -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 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1707 6353ad76 2019-02-08 stsp if (err)
1708 1338848f 2019-12-13 stsp goto done;
1709 6353ad76 2019-02-08 stsp
1710 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1711 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1712 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1713 a919d5c4 2020-07-23 stsp goto done;
1714 a919d5c4 2020-07-23 stsp }
1715 a919d5c4 2020-07-23 stsp
1716 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1717 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1718 ab0d4361 2019-12-13 stsp if (fd == -1) {
1719 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1720 ab0d4361 2019-12-13 stsp goto done;
1721 ab0d4361 2019-12-13 stsp }
1722 3d35a492 2019-12-13 stsp }
1723 3d35a492 2019-12-13 stsp
1724 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1725 6353ad76 2019-02-08 stsp if (f == NULL) {
1726 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1727 6353ad76 2019-02-08 stsp goto done;
1728 6353ad76 2019-02-08 stsp }
1729 1338848f 2019-12-13 stsp fd = -1;
1730 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1731 656b1f76 2019-05-11 jcs for (;;) {
1732 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1734 6353ad76 2019-02-08 stsp if (err)
1735 7154f6ce 2019-03-27 stsp goto done;
1736 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1737 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1739 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1740 7154f6ce 2019-03-27 stsp goto done;
1741 80c5c120 2019-02-19 stsp }
1742 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1743 6353ad76 2019-02-08 stsp if (flen != 0)
1744 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1745 6353ad76 2019-02-08 stsp break;
1746 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1747 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1748 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1749 6353ad76 2019-02-08 stsp break;
1750 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1751 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1752 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1754 6353ad76 2019-02-08 stsp break;
1755 6353ad76 2019-02-08 stsp }
1756 6353ad76 2019-02-08 stsp } else {
1757 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1758 6353ad76 2019-02-08 stsp break;
1759 6353ad76 2019-02-08 stsp }
1760 6353ad76 2019-02-08 stsp hdrlen = 0;
1761 6353ad76 2019-02-08 stsp }
1762 7154f6ce 2019-03-27 stsp
1763 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1764 7154f6ce 2019-03-27 stsp rewind(f);
1765 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1766 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1767 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1768 6353ad76 2019-02-08 stsp done:
1769 6353ad76 2019-02-08 stsp if (blob)
1770 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1771 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1772 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1773 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1774 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1775 9d31a1d8 2018-03-11 stsp return err;
1776 e2b1e152 2019-07-13 stsp }
1777 e2b1e152 2019-07-13 stsp
1778 e2b1e152 2019-07-13 stsp /*
1779 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1780 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1781 e2b1e152 2019-07-13 stsp */
1782 e2b1e152 2019-07-13 stsp static const struct got_error *
1783 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1785 e2b1e152 2019-07-13 stsp {
1786 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1788 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1789 e2b1e152 2019-07-13 stsp
1790 e2b1e152 2019-07-13 stsp return NULL;
1791 9d31a1d8 2018-03-11 stsp }
1792 9d31a1d8 2018-03-11 stsp
1793 9d31a1d8 2018-03-11 stsp static const struct got_error *
1794 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1795 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1797 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1798 0584f854 2019-04-06 stsp void *progress_arg)
1799 9d31a1d8 2018-03-11 stsp {
1800 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1801 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1802 6353ad76 2019-02-08 stsp char *ondisk_path;
1803 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1804 b8f41171 2019-02-10 stsp struct stat sb;
1805 9d31a1d8 2018-03-11 stsp
1806 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1808 6353ad76 2019-02-08 stsp
1809 abb4604f 2019-07-27 stsp if (ie) {
1810 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1812 a76c42e6 2019-08-03 stsp goto done;
1813 a76c42e6 2019-08-03 stsp }
1814 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1815 7f91a133 2019-12-13 stsp repo);
1816 abb4604f 2019-07-27 stsp if (err)
1817 abb4604f 2019-07-27 stsp goto done;
1818 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1820 c90c8ce3 2020-07-23 stsp } else {
1821 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1822 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1823 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1824 e6f4ba31 2021-09-24 thomas ondisk_path);
1825 e6f4ba31 2021-09-24 thomas goto done;
1826 e6f4ba31 2021-09-24 thomas }
1827 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1829 e6f4ba31 2021-09-24 thomas } else {
1830 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1832 e6f4ba31 2021-09-24 thomas else
1833 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1834 e6f4ba31 2021-09-24 thomas }
1835 c90c8ce3 2020-07-23 stsp }
1836 6353ad76 2019-02-08 stsp
1837 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1838 2c41dce7 2021-06-27 stsp if (ie)
1839 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1840 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1841 b8f41171 2019-02-10 stsp goto done;
1842 b8f41171 2019-02-10 stsp }
1843 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1844 a769b60b 2021-06-27 stsp if (ie)
1845 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1846 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1847 5036ab18 2020-04-18 stsp path);
1848 5036ab18 2020-04-18 stsp goto done;
1849 5036ab18 2020-04-18 stsp }
1850 b8f41171 2019-02-10 stsp
1851 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1853 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1854 c6e8a826 2021-04-05 stsp /*
1855 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1856 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1857 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1858 c6e8a826 2021-04-05 stsp * updating contents of this file.
1859 c6e8a826 2021-04-05 stsp */
1860 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1861 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1863 c6e8a826 2021-04-05 stsp /* Same commit. */
1864 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1865 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1866 e2b1e152 2019-07-13 stsp if (err)
1867 e2b1e152 2019-07-13 stsp goto done;
1868 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 b8f41171 2019-02-10 stsp path);
1870 6353ad76 2019-02-08 stsp goto done;
1871 a378724f 2019-02-10 stsp }
1872 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1873 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1874 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1875 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1876 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1877 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1878 0f58026f 2021-04-05 stsp if (err)
1879 0f58026f 2021-04-05 stsp goto done;
1880 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1881 0f58026f 2021-04-05 stsp path);
1882 b8f41171 2019-02-10 stsp goto done;
1883 e2b1e152 2019-07-13 stsp }
1884 9d31a1d8 2018-03-11 stsp }
1885 9d31a1d8 2018-03-11 stsp
1886 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 8da9e5f4 2019-01-12 stsp if (err)
1888 6353ad76 2019-02-08 stsp goto done;
1889 512f0d0e 2019-01-02 stsp
1890 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 234035bc 2019-06-01 stsp int update_timestamps;
1892 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1893 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1894 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1895 234035bc 2019-06-01 stsp struct got_object_id id2;
1896 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 234035bc 2019-06-01 stsp if (err)
1899 f69721c3 2019-10-21 stsp goto done;
1900 f69721c3 2019-10-21 stsp }
1901 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1902 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1905 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1906 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1907 f69721c3 2019-10-21 stsp goto done;
1908 f69721c3 2019-10-21 stsp }
1909 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1910 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1912 234035bc 2019-06-01 stsp goto done;
1913 f69721c3 2019-10-21 stsp }
1914 234035bc 2019-06-01 stsp }
1915 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 36bf999c 2020-07-23 stsp char *link_target;
1917 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1918 36bf999c 2020-07-23 stsp if (err)
1919 36bf999c 2020-07-23 stsp goto done;
1920 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1922 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1923 36bf999c 2020-07-23 stsp free(link_target);
1924 993e2a1b 2020-07-23 stsp } else {
1925 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1926 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1927 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1928 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1929 993e2a1b 2020-07-23 stsp }
1930 f69721c3 2019-10-21 stsp free(label_orig);
1931 234035bc 2019-06-01 stsp if (blob2)
1932 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1933 909d120e 2019-09-22 stsp if (err)
1934 909d120e 2019-09-22 stsp goto done;
1935 234035bc 2019-06-01 stsp /*
1936 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1937 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1938 234035bc 2019-06-01 stsp * unmodified files again.
1939 234035bc 2019-06-01 stsp */
1940 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1942 234035bc 2019-06-01 stsp update_timestamps);
1943 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1947 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1948 1ee397ad 2019-07-12 stsp if (err)
1949 1ee397ad 2019-07-12 stsp goto done;
1950 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1952 13d9040b 2019-03-27 stsp if (err)
1953 13d9040b 2019-03-27 stsp goto done;
1954 13d9040b 2019-03-27 stsp } else {
1955 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1956 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1957 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1958 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1959 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1960 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1961 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
1962 2e1fa222 2020-07-23 stsp } else {
1963 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1964 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1965 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1966 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1967 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1968 2e1fa222 2020-07-23 stsp }
1969 13d9040b 2019-03-27 stsp if (err)
1970 13d9040b 2019-03-27 stsp goto done;
1971 65b05cec 2020-07-23 stsp
1972 054041d0 2020-06-24 stsp if (ie) {
1973 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1974 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1975 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1976 054041d0 2020-06-24 stsp } else {
1977 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1978 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
1979 054041d0 2020-06-24 stsp &blob->id);
1980 054041d0 2020-06-24 stsp }
1981 13d9040b 2019-03-27 stsp if (err)
1982 13d9040b 2019-03-27 stsp goto done;
1983 65b05cec 2020-07-23 stsp
1984 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1985 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1986 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1987 2e1fa222 2020-07-23 stsp }
1988 13d9040b 2019-03-27 stsp }
1989 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1990 6353ad76 2019-02-08 stsp done:
1991 6353ad76 2019-02-08 stsp free(ondisk_path);
1992 8da9e5f4 2019-01-12 stsp return err;
1993 90285c3b 2019-01-08 stsp }
1994 90285c3b 2019-01-08 stsp
1995 90285c3b 2019-01-08 stsp static const struct got_error *
1996 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1997 90285c3b 2019-01-08 stsp {
1998 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1999 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2000 90285c3b 2019-01-08 stsp
2001 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2003 90285c3b 2019-01-08 stsp
2004 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2005 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2006 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2007 c97665ae 2019-01-13 stsp } else {
2008 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2009 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2010 1c4cdd89 2021-06-20 stsp if (err)
2011 1c4cdd89 2021-06-20 stsp goto done;
2012 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2013 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2014 fddefe3b 2020-10-20 stsp free(ondisk_path);
2015 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2016 1c4cdd89 2021-06-20 stsp parent = NULL;
2017 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2018 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2019 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2020 fddefe3b 2020-10-20 stsp ondisk_path);
2021 90285c3b 2019-01-08 stsp break;
2022 90285c3b 2019-01-08 stsp }
2023 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2024 1c4cdd89 2021-06-20 stsp if (err)
2025 1c4cdd89 2021-06-20 stsp break;
2026 1c4cdd89 2021-06-20 stsp }
2027 90285c3b 2019-01-08 stsp }
2028 1c4cdd89 2021-06-20 stsp done:
2029 90285c3b 2019-01-08 stsp free(ondisk_path);
2030 1c4cdd89 2021-06-20 stsp free(parent);
2031 90285c3b 2019-01-08 stsp return err;
2032 512f0d0e 2019-01-02 stsp }
2033 512f0d0e 2019-01-02 stsp
2034 708d8e67 2019-03-27 stsp static const struct got_error *
2035 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2037 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2038 708d8e67 2019-03-27 stsp {
2039 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2040 708d8e67 2019-03-27 stsp unsigned char status;
2041 708d8e67 2019-03-27 stsp struct stat sb;
2042 708d8e67 2019-03-27 stsp char *ondisk_path;
2043 708d8e67 2019-03-27 stsp
2044 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2046 a76c42e6 2019-08-03 stsp
2047 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2048 708d8e67 2019-03-27 stsp == -1)
2049 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2050 708d8e67 2019-03-27 stsp
2051 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2052 708d8e67 2019-03-27 stsp if (err)
2053 5a58a424 2020-06-23 stsp goto done;
2054 708d8e67 2019-03-27 stsp
2055 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2057 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2059 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2060 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2061 993e2a1b 2020-07-23 stsp goto done;
2062 993e2a1b 2020-07-23 stsp }
2063 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2064 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2066 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2067 993e2a1b 2020-07-23 stsp if (err)
2068 993e2a1b 2020-07-23 stsp goto done;
2069 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2070 993e2a1b 2020-07-23 stsp ie->path);
2071 993e2a1b 2020-07-23 stsp goto done;
2072 993e2a1b 2020-07-23 stsp }
2073 993e2a1b 2020-07-23 stsp
2074 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2076 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2077 1ee397ad 2019-07-12 stsp if (err)
2078 5a58a424 2020-06-23 stsp goto done;
2079 fc6346c4 2019-03-27 stsp /*
2080 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2081 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2082 fc6346c4 2019-03-27 stsp */
2083 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2085 fc6346c4 2019-03-27 stsp } else {
2086 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2087 1ee397ad 2019-07-12 stsp if (err)
2088 5a58a424 2020-06-23 stsp goto done;
2089 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2090 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2091 fc6346c4 2019-03-27 stsp if (err)
2092 5a58a424 2020-06-23 stsp goto done;
2093 fc6346c4 2019-03-27 stsp }
2094 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2095 708d8e67 2019-03-27 stsp }
2096 5a58a424 2020-06-23 stsp done:
2097 5a58a424 2020-06-23 stsp free(ondisk_path);
2098 fc6346c4 2019-03-27 stsp return err;
2099 708d8e67 2019-03-27 stsp }
2100 708d8e67 2019-03-27 stsp
2101 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2102 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2103 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2104 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2105 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2106 8da9e5f4 2019-01-12 stsp void *progress_arg;
2107 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2108 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2109 8da9e5f4 2019-01-12 stsp };
2110 8da9e5f4 2019-01-12 stsp
2111 512f0d0e 2019-01-02 stsp static const struct got_error *
2112 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2114 512f0d0e 2019-01-02 stsp {
2115 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2116 0584f854 2019-04-06 stsp
2117 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2119 512f0d0e 2019-01-02 stsp
2120 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2121 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2122 8da9e5f4 2019-01-12 stsp }
2123 512f0d0e 2019-01-02 stsp
2124 8da9e5f4 2019-01-12 stsp static const struct got_error *
2125 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2126 8da9e5f4 2019-01-12 stsp {
2127 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2128 7a9df742 2019-01-08 stsp
2129 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2131 0584f854 2019-04-06 stsp
2132 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2133 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2134 9d31a1d8 2018-03-11 stsp }
2135 9d31a1d8 2018-03-11 stsp
2136 9d31a1d8 2018-03-11 stsp static const struct got_error *
2137 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2138 9d31a1d8 2018-03-11 stsp {
2139 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2140 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2141 8da9e5f4 2019-01-12 stsp char *path;
2142 9d31a1d8 2018-03-11 stsp
2143 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2145 0584f854 2019-04-06 stsp
2146 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2147 63c5ca5d 2019-08-24 stsp return NULL;
2148 63c5ca5d 2019-08-24 stsp
2149 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2150 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2151 8da9e5f4 2019-01-12 stsp == -1)
2152 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2153 9d31a1d8 2018-03-11 stsp
2154 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2155 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2156 8da9e5f4 2019-01-12 stsp else
2157 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2159 9d31a1d8 2018-03-11 stsp
2160 8da9e5f4 2019-01-12 stsp free(path);
2161 0cd1c46a 2019-03-11 stsp return err;
2162 0cd1c46a 2019-03-11 stsp }
2163 0cd1c46a 2019-03-11 stsp
2164 b2118c49 2020-07-28 stsp const struct got_error *
2165 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2166 b2118c49 2020-07-28 stsp {
2167 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2168 b2118c49 2020-07-28 stsp
2169 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2171 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2172 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2173 b2118c49 2020-07-28 stsp }
2174 b2118c49 2020-07-28 stsp
2175 b2118c49 2020-07-28 stsp return NULL;
2176 b2118c49 2020-07-28 stsp }
2177 b2118c49 2020-07-28 stsp
2178 818c7501 2019-07-11 stsp static const struct got_error *
2179 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2180 0cd1c46a 2019-03-11 stsp {
2181 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2182 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2183 0cd1c46a 2019-03-11 stsp
2184 517bab73 2019-03-11 stsp *refname = NULL;
2185 517bab73 2019-03-11 stsp
2186 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2187 b2118c49 2020-07-28 stsp if (err)
2188 b2118c49 2020-07-28 stsp return err;
2189 0cd1c46a 2019-03-11 stsp
2190 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2192 0647c563 2019-03-11 stsp *refname = NULL;
2193 0cd1c46a 2019-03-11 stsp }
2194 517bab73 2019-03-11 stsp free(uuidstr);
2195 517bab73 2019-03-11 stsp return err;
2196 517bab73 2019-03-11 stsp }
2197 517bab73 2019-03-11 stsp
2198 818c7501 2019-07-11 stsp const struct got_error *
2199 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2200 818c7501 2019-07-11 stsp {
2201 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2202 818c7501 2019-07-11 stsp }
2203 818c7501 2019-07-11 stsp
2204 818c7501 2019-07-11 stsp static const struct got_error *
2205 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2206 818c7501 2019-07-11 stsp {
2207 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2208 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2209 818c7501 2019-07-11 stsp }
2210 818c7501 2019-07-11 stsp
2211 818c7501 2019-07-11 stsp static const struct got_error *
2212 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2213 818c7501 2019-07-11 stsp {
2214 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2215 818c7501 2019-07-11 stsp }
2216 818c7501 2019-07-11 stsp
2217 818c7501 2019-07-11 stsp static const struct got_error *
2218 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2219 818c7501 2019-07-11 stsp {
2220 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2221 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2222 818c7501 2019-07-11 stsp }
2223 818c7501 2019-07-11 stsp
2224 818c7501 2019-07-11 stsp static const struct got_error *
2225 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2226 818c7501 2019-07-11 stsp {
2227 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2228 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2229 0ebf8283 2019-07-24 stsp }
2230 0ebf8283 2019-07-24 stsp
2231 0ebf8283 2019-07-24 stsp static const struct got_error *
2232 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2233 0ebf8283 2019-07-24 stsp {
2234 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2235 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2236 0ebf8283 2019-07-24 stsp }
2237 0ebf8283 2019-07-24 stsp
2238 0ebf8283 2019-07-24 stsp static const struct got_error *
2239 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2240 0ebf8283 2019-07-24 stsp {
2241 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2242 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2243 0ebf8283 2019-07-24 stsp }
2244 0ebf8283 2019-07-24 stsp
2245 0ebf8283 2019-07-24 stsp static const struct got_error *
2246 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2247 0ebf8283 2019-07-24 stsp {
2248 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2249 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2250 818c7501 2019-07-11 stsp }
2251 818c7501 2019-07-11 stsp
2252 0ebf8283 2019-07-24 stsp static const struct got_error *
2253 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2254 0ebf8283 2019-07-24 stsp {
2255 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2256 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2257 0ebf8283 2019-07-24 stsp }
2258 818c7501 2019-07-11 stsp
2259 0ebf8283 2019-07-24 stsp const struct got_error *
2260 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2261 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2262 0ebf8283 2019-07-24 stsp {
2263 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2265 0ebf8283 2019-07-24 stsp *path = NULL;
2266 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2267 0ebf8283 2019-07-24 stsp }
2268 0ebf8283 2019-07-24 stsp return NULL;
2269 10604dce 2021-09-24 thomas }
2270 10604dce 2021-09-24 thomas
2271 10604dce 2021-09-24 thomas static const struct got_error *
2272 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2273 10604dce 2021-09-24 thomas {
2274 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2275 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2276 0ebf8283 2019-07-24 stsp }
2277 0ebf8283 2019-07-24 stsp
2278 10604dce 2021-09-24 thomas static const struct got_error *
2279 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2280 10604dce 2021-09-24 thomas {
2281 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2282 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2283 10604dce 2021-09-24 thomas }
2284 10604dce 2021-09-24 thomas
2285 517bab73 2019-03-11 stsp /*
2286 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2287 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2288 517bab73 2019-03-11 stsp */
2289 517bab73 2019-03-11 stsp static const struct got_error *
2290 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2291 517bab73 2019-03-11 stsp {
2292 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2293 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2294 517bab73 2019-03-11 stsp char *refname;
2295 517bab73 2019-03-11 stsp
2296 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2297 517bab73 2019-03-11 stsp if (err)
2298 517bab73 2019-03-11 stsp return err;
2299 0cd1c46a 2019-03-11 stsp
2300 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2301 0cd1c46a 2019-03-11 stsp if (err)
2302 0cd1c46a 2019-03-11 stsp goto done;
2303 0cd1c46a 2019-03-11 stsp
2304 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2305 0cd1c46a 2019-03-11 stsp done:
2306 0cd1c46a 2019-03-11 stsp free(refname);
2307 0cd1c46a 2019-03-11 stsp if (ref)
2308 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2309 3e3a69f1 2019-07-25 stsp return err;
2310 3e3a69f1 2019-07-25 stsp }
2311 3e3a69f1 2019-07-25 stsp
2312 3e3a69f1 2019-07-25 stsp static const struct got_error *
2313 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2314 3e3a69f1 2019-07-25 stsp {
2315 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2316 3e3a69f1 2019-07-25 stsp
2317 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2320 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2321 3e3a69f1 2019-07-25 stsp }
2322 9d31a1d8 2018-03-11 stsp return err;
2323 9d31a1d8 2018-03-11 stsp }
2324 9d31a1d8 2018-03-11 stsp
2325 3e3a69f1 2019-07-25 stsp
2326 ebf99748 2019-05-09 stsp static const struct got_error *
2327 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2329 ebf99748 2019-05-09 stsp {
2330 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2331 ebf99748 2019-05-09 stsp FILE *index = NULL;
2332 0cd1c46a 2019-03-11 stsp
2333 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2334 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2335 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2336 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2337 ebf99748 2019-05-09 stsp
2338 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2339 3e3a69f1 2019-07-25 stsp if (err)
2340 ebf99748 2019-05-09 stsp goto done;
2341 ebf99748 2019-05-09 stsp
2342 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2343 ebf99748 2019-05-09 stsp if (index == NULL) {
2344 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2345 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2346 ebf99748 2019-05-09 stsp } else {
2347 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2348 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2349 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2350 ebf99748 2019-05-09 stsp }
2351 ebf99748 2019-05-09 stsp done:
2352 ebf99748 2019-05-09 stsp if (err) {
2353 ebf99748 2019-05-09 stsp free(*fileindex_path);
2354 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2355 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2356 ebf99748 2019-05-09 stsp *fileindex = NULL;
2357 ebf99748 2019-05-09 stsp }
2358 ebf99748 2019-05-09 stsp return err;
2359 ebf99748 2019-05-09 stsp }
2360 c932eeeb 2019-05-22 stsp
2361 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2362 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2363 c932eeeb 2019-05-22 stsp const char *path;
2364 c932eeeb 2019-05-22 stsp size_t path_len;
2365 c932eeeb 2019-05-22 stsp const char *entry_name;
2366 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2367 a484d721 2019-06-10 stsp void *progress_arg;
2368 c932eeeb 2019-05-22 stsp };
2369 ebf99748 2019-05-09 stsp
2370 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2371 c932eeeb 2019-05-22 stsp static const struct got_error *
2372 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2373 c932eeeb 2019-05-22 stsp {
2374 1ee397ad 2019-07-12 stsp const struct got_error *err;
2375 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2376 c932eeeb 2019-05-22 stsp
2377 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2378 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2379 c932eeeb 2019-05-22 stsp return NULL;
2380 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2381 102ff934 2019-06-11 stsp return NULL;
2382 102ff934 2019-06-11 stsp
2383 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2384 a769b60b 2021-06-27 stsp return NULL;
2385 a769b60b 2021-06-27 stsp
2386 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2388 c932eeeb 2019-05-22 stsp return NULL;
2389 c932eeeb 2019-05-22 stsp
2390 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2391 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2392 edd02c5e 2019-07-11 stsp ie->path);
2393 1ee397ad 2019-07-12 stsp if (err)
2394 1ee397ad 2019-07-12 stsp return err;
2395 1ee397ad 2019-07-12 stsp }
2396 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2397 c932eeeb 2019-05-22 stsp return NULL;
2398 a615e0e7 2020-12-16 stsp }
2399 a615e0e7 2020-12-16 stsp
2400 a615e0e7 2020-12-16 stsp static const struct got_error *
2401 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2403 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2404 a615e0e7 2020-12-16 stsp {
2405 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2406 a615e0e7 2020-12-16 stsp
2407 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2408 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2409 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2410 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2411 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2412 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2413 a615e0e7 2020-12-16 stsp
2414 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2415 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2416 9c6338c4 2019-06-02 stsp }
2417 9c6338c4 2019-06-02 stsp
2418 9c6338c4 2019-06-02 stsp static const struct got_error *
2419 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2420 9c6338c4 2019-06-02 stsp {
2421 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2422 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2423 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2424 867630bb 2020-01-17 stsp struct timespec timeout;
2425 9c6338c4 2019-06-02 stsp
2426 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2427 9c6338c4 2019-06-02 stsp fileindex_path);
2428 9c6338c4 2019-06-02 stsp if (err)
2429 9c6338c4 2019-06-02 stsp goto done;
2430 9c6338c4 2019-06-02 stsp
2431 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2432 9c6338c4 2019-06-02 stsp if (err)
2433 9c6338c4 2019-06-02 stsp goto done;
2434 9c6338c4 2019-06-02 stsp
2435 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2437 9c6338c4 2019-06-02 stsp fileindex_path);
2438 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2439 9c6338c4 2019-06-02 stsp }
2440 867630bb 2020-01-17 stsp
2441 867630bb 2020-01-17 stsp /*
2442 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2443 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2444 867630bb 2020-01-17 stsp * was recorded in the file index.
2445 867630bb 2020-01-17 stsp */
2446 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2447 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2448 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2449 9c6338c4 2019-06-02 stsp done:
2450 9c6338c4 2019-06-02 stsp if (new_index)
2451 9c6338c4 2019-06-02 stsp fclose(new_index);
2452 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2453 9c6338c4 2019-06-02 stsp return err;
2454 c932eeeb 2019-05-22 stsp }
2455 6ced4176 2019-07-12 stsp
2456 6ced4176 2019-07-12 stsp static const struct got_error *
2457 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2459 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2460 945f9229 2022-04-16 thomas struct got_repository *repo)
2461 6ced4176 2019-07-12 stsp {
2462 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2463 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2464 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2465 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2466 6ced4176 2019-07-12 stsp
2467 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2468 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2469 6ced4176 2019-07-12 stsp *tree_id = NULL;
2470 6ced4176 2019-07-12 stsp
2471 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2472 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2473 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2474 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2475 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2476 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2477 6ced4176 2019-07-12 stsp goto done;
2478 6ced4176 2019-07-12 stsp }
2479 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2480 945f9229 2022-04-16 thomas worktree->path_prefix);
2481 6ced4176 2019-07-12 stsp if (err)
2482 6ced4176 2019-07-12 stsp goto done;
2483 6ced4176 2019-07-12 stsp return NULL;
2484 6ced4176 2019-07-12 stsp }
2485 6ced4176 2019-07-12 stsp
2486 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2487 6ced4176 2019-07-12 stsp
2488 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2491 6ced4176 2019-07-12 stsp goto done;
2492 6ced4176 2019-07-12 stsp }
2493 6ced4176 2019-07-12 stsp
2494 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2495 6ced4176 2019-07-12 stsp if (err)
2496 6ced4176 2019-07-12 stsp goto done;
2497 6ced4176 2019-07-12 stsp
2498 6ced4176 2019-07-12 stsp free(in_repo_path);
2499 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2500 6ced4176 2019-07-12 stsp
2501 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2502 6ced4176 2019-07-12 stsp if (err)
2503 6ced4176 2019-07-12 stsp goto done;
2504 c932eeeb 2019-05-22 stsp
2505 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 6ced4176 2019-07-12 stsp /* Check out a single file. */
2507 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2508 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2509 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2510 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2511 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2512 6ced4176 2019-07-12 stsp goto done;
2513 6ced4176 2019-07-12 stsp }
2514 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2515 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2516 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2517 6ced4176 2019-07-12 stsp goto done;
2518 6ced4176 2019-07-12 stsp }
2519 6ced4176 2019-07-12 stsp } else {
2520 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2521 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2522 6ced4176 2019-07-12 stsp if (err)
2523 6ced4176 2019-07-12 stsp return err;
2524 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2525 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2526 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2527 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2528 6ced4176 2019-07-12 stsp goto done;
2529 6ced4176 2019-07-12 stsp }
2530 6ced4176 2019-07-12 stsp }
2531 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2532 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2533 6ced4176 2019-07-12 stsp } else {
2534 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2535 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2536 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2537 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2538 6ced4176 2019-07-12 stsp goto done;
2539 6ced4176 2019-07-12 stsp }
2540 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2541 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2542 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2543 6ced4176 2019-07-12 stsp goto done;
2544 6ced4176 2019-07-12 stsp }
2545 6ced4176 2019-07-12 stsp }
2546 6ced4176 2019-07-12 stsp done:
2547 6ced4176 2019-07-12 stsp free(id);
2548 6ced4176 2019-07-12 stsp free(in_repo_path);
2549 6ced4176 2019-07-12 stsp if (err) {
2550 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2551 6ced4176 2019-07-12 stsp free(*tree_relpath);
2552 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2553 6ced4176 2019-07-12 stsp free(*tree_id);
2554 6ced4176 2019-07-12 stsp *tree_id = NULL;
2555 6ced4176 2019-07-12 stsp }
2556 07ed472d 2019-07-12 stsp return err;
2557 07ed472d 2019-07-12 stsp }
2558 07ed472d 2019-07-12 stsp
2559 07ed472d 2019-07-12 stsp static const struct got_error *
2560 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2564 07ed472d 2019-07-12 stsp {
2565 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2566 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2567 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2568 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2569 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2570 07ed472d 2019-07-12 stsp
2571 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2572 7f47418f 2019-12-20 stsp if (err) {
2573 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2574 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2575 7f47418f 2019-12-20 stsp goto done;
2576 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2577 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2578 7f47418f 2019-12-20 stsp if (err)
2579 7f47418f 2019-12-20 stsp return err;
2580 7f47418f 2019-12-20 stsp }
2581 07ed472d 2019-07-12 stsp
2582 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2583 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2584 07ed472d 2019-07-12 stsp if (err)
2585 07ed472d 2019-07-12 stsp goto done;
2586 07ed472d 2019-07-12 stsp
2587 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2588 07ed472d 2019-07-12 stsp if (err)
2589 07ed472d 2019-07-12 stsp goto done;
2590 07ed472d 2019-07-12 stsp
2591 07ed472d 2019-07-12 stsp if (entry_name &&
2592 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2594 07ed472d 2019-07-12 stsp goto done;
2595 07ed472d 2019-07-12 stsp }
2596 07ed472d 2019-07-12 stsp
2597 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2598 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2599 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2600 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2601 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2602 07ed472d 2019-07-12 stsp arg.repo = repo;
2603 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2604 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2605 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2606 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2607 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2609 07ed472d 2019-07-12 stsp done:
2610 07ed472d 2019-07-12 stsp if (tree)
2611 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2612 07ed472d 2019-07-12 stsp if (commit)
2613 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2614 6ced4176 2019-07-12 stsp return err;
2615 6ced4176 2019-07-12 stsp }
2616 6ced4176 2019-07-12 stsp
2617 9d31a1d8 2018-03-11 stsp const struct got_error *
2618 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2619 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2620 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2622 9d31a1d8 2018-03-11 stsp {
2623 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2625 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2626 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2627 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2628 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2629 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2630 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2631 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2632 f2ea84fa 2019-07-27 stsp int entry_type;
2633 f2ea84fa 2019-07-27 stsp char *relpath;
2634 f2ea84fa 2019-07-27 stsp char *entry_name;
2635 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2636 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2637 9d31a1d8 2018-03-11 stsp
2638 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2639 f2ea84fa 2019-07-27 stsp
2640 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2641 9d31a1d8 2018-03-11 stsp if (err)
2642 9d31a1d8 2018-03-11 stsp return err;
2643 945f9229 2022-04-16 thomas
2644 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2645 945f9229 2022-04-16 thomas worktree->base_commit_id);
2646 945f9229 2022-04-16 thomas if (err)
2647 945f9229 2022-04-16 thomas goto done;
2648 93a30277 2018-12-24 stsp
2649 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2650 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2651 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2652 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2653 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2654 f2ea84fa 2019-07-27 stsp goto done;
2655 f2ea84fa 2019-07-27 stsp }
2656 6ced4176 2019-07-12 stsp
2657 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2658 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2659 945f9229 2022-04-16 thomas worktree, repo);
2660 f2ea84fa 2019-07-27 stsp if (err) {
2661 f2ea84fa 2019-07-27 stsp free(tpd);
2662 6ced4176 2019-07-12 stsp goto done;
2663 6ced4176 2019-07-12 stsp }
2664 f2ea84fa 2019-07-27 stsp
2665 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2666 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2667 f2ea84fa 2019-07-27 stsp if (err) {
2668 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2669 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2670 f2ea84fa 2019-07-27 stsp free(tpd);
2671 f2ea84fa 2019-07-27 stsp goto done;
2672 f2ea84fa 2019-07-27 stsp }
2673 f2ea84fa 2019-07-27 stsp } else
2674 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2675 f2ea84fa 2019-07-27 stsp
2676 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 6ced4176 2019-07-12 stsp }
2678 6ced4176 2019-07-12 stsp
2679 51514078 2018-12-25 stsp /*
2680 51514078 2018-12-25 stsp * Read the file index.
2681 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2682 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2683 51514078 2018-12-25 stsp */
2684 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2685 8da9e5f4 2019-01-12 stsp if (err)
2686 c4cdcb68 2019-04-03 stsp goto done;
2687 c4cdcb68 2019-04-03 stsp
2688 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2689 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2690 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2691 f2ea84fa 2019-07-27 stsp
2692 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2693 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2694 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 f2ea84fa 2019-07-27 stsp if (err)
2696 f2ea84fa 2019-07-27 stsp break;
2697 f2ea84fa 2019-07-27 stsp
2698 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2699 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2700 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2701 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2702 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2703 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2704 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2705 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2706 f2ea84fa 2019-07-27 stsp if (err)
2707 f2ea84fa 2019-07-27 stsp break;
2708 f2ea84fa 2019-07-27 stsp
2709 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2710 711d9cd9 2019-07-12 stsp }
2711 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2712 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2713 af12c6b9 2019-06-04 stsp err = sync_err;
2714 9d31a1d8 2018-03-11 stsp done:
2715 9c6338c4 2019-06-02 stsp free(fileindex_path);
2716 edfa7d7f 2018-09-11 stsp if (tree)
2717 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2718 9d31a1d8 2018-03-11 stsp if (commit)
2719 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2720 5ade8233 2019-07-12 stsp if (fileindex)
2721 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2722 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2723 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2724 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2725 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2726 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2727 f2ea84fa 2019-07-27 stsp free(tpd);
2728 f2ea84fa 2019-07-27 stsp }
2729 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2730 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2731 9d31a1d8 2018-03-11 stsp err = unlockerr;
2732 f8d1f275 2019-02-04 stsp return err;
2733 f8d1f275 2019-02-04 stsp }
2734 f8d1f275 2019-02-04 stsp
2735 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2736 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2737 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2738 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2739 234035bc 2019-06-01 stsp void *progress_arg;
2740 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2741 234035bc 2019-06-01 stsp void *cancel_arg;
2742 f69721c3 2019-10-21 stsp const char *label_orig;
2743 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2744 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2745 234035bc 2019-06-01 stsp };
2746 234035bc 2019-06-01 stsp
2747 234035bc 2019-06-01 stsp static const struct got_error *
2748 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2749 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2750 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2751 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2752 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2753 234035bc 2019-06-01 stsp {
2754 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2755 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2756 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2757 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2758 234035bc 2019-06-01 stsp struct stat sb;
2759 234035bc 2019-06-01 stsp unsigned char status;
2760 234035bc 2019-06-01 stsp int local_changes_subsumed;
2761 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2762 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2763 234035bc 2019-06-01 stsp
2764 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2765 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2766 d6c87207 2019-08-02 stsp strlen(path2));
2767 1ee397ad 2019-07-12 stsp if (ie == NULL)
2768 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2769 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2770 234035bc 2019-06-01 stsp
2771 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2772 234035bc 2019-06-01 stsp path2) == -1)
2773 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2774 234035bc 2019-06-01 stsp
2775 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2776 7f91a133 2019-12-13 stsp repo);
2777 234035bc 2019-06-01 stsp if (err)
2778 234035bc 2019-06-01 stsp goto done;
2779 234035bc 2019-06-01 stsp
2780 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2781 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2782 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2783 234035bc 2019-06-01 stsp goto done;
2784 234035bc 2019-06-01 stsp }
2785 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2786 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2787 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2788 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2789 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2790 234035bc 2019-06-01 stsp goto done;
2791 234035bc 2019-06-01 stsp }
2792 234035bc 2019-06-01 stsp
2793 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2794 36bf999c 2020-07-23 stsp char *link_target2;
2795 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2796 36bf999c 2020-07-23 stsp if (err)
2797 36bf999c 2020-07-23 stsp goto done;
2798 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2799 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2800 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2801 36bf999c 2020-07-23 stsp free(link_target2);
2802 af57b12a 2020-07-23 stsp } else {
2803 1af628f4 2021-06-03 stsp int fd;
2804 1af628f4 2021-06-03 stsp
2805 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2806 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2807 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2808 1af628f4 2021-06-03 stsp goto done;
2809 1af628f4 2021-06-03 stsp }
2810 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2811 1af628f4 2021-06-03 stsp f_orig, blob1);
2812 1af628f4 2021-06-03 stsp if (err)
2813 1af628f4 2021-06-03 stsp goto done;
2814 1af628f4 2021-06-03 stsp
2815 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2816 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2817 1af628f4 2021-06-03 stsp goto done;
2818 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2819 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2820 1af628f4 2021-06-03 stsp if (err)
2821 1af628f4 2021-06-03 stsp goto done;
2822 1af628f4 2021-06-03 stsp
2823 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2824 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2825 1af628f4 2021-06-03 stsp if (fd == -1) {
2826 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2827 1af628f4 2021-06-03 stsp ondisk_path);
2828 1af628f4 2021-06-03 stsp goto done;
2829 1af628f4 2021-06-03 stsp }
2830 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2831 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2832 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2833 1af628f4 2021-06-03 stsp ondisk_path);
2834 1af628f4 2021-06-03 stsp close(fd);
2835 1af628f4 2021-06-03 stsp goto done;
2836 1af628f4 2021-06-03 stsp }
2837 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2838 1af628f4 2021-06-03 stsp if (err)
2839 1af628f4 2021-06-03 stsp goto done;
2840 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2841 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2842 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2843 1af628f4 2021-06-03 stsp goto done;
2844 1af628f4 2021-06-03 stsp }
2845 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2846 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2847 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2848 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2849 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2850 af57b12a 2020-07-23 stsp }
2851 234035bc 2019-06-01 stsp } else if (blob1) {
2852 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2853 d6c87207 2019-08-02 stsp strlen(path1));
2854 1ee397ad 2019-07-12 stsp if (ie == NULL)
2855 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2856 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2857 2b92fad7 2019-06-02 stsp
2858 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2859 2b92fad7 2019-06-02 stsp path1) == -1)
2860 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2861 2b92fad7 2019-06-02 stsp
2862 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2863 7f91a133 2019-12-13 stsp repo);
2864 2b92fad7 2019-06-02 stsp if (err)
2865 2b92fad7 2019-06-02 stsp goto done;
2866 2b92fad7 2019-06-02 stsp
2867 2b92fad7 2019-06-02 stsp switch (status) {
2868 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2869 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2870 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2871 1ee397ad 2019-07-12 stsp if (err)
2872 1ee397ad 2019-07-12 stsp goto done;
2873 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2874 2b92fad7 2019-06-02 stsp if (err)
2875 2b92fad7 2019-06-02 stsp goto done;
2876 2b92fad7 2019-06-02 stsp if (ie)
2877 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2878 2b92fad7 2019-06-02 stsp break;
2879 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2880 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2881 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2882 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2883 1ee397ad 2019-07-12 stsp if (err)
2884 1ee397ad 2019-07-12 stsp goto done;
2885 2b92fad7 2019-06-02 stsp if (ie)
2886 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2887 2b92fad7 2019-06-02 stsp break;
2888 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2889 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2890 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2891 e8f02263 2022-01-23 thomas off_t blob1_size;
2892 0a22ca1a 2020-09-23 stsp /*
2893 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2894 0a22ca1a 2020-09-23 stsp * exists in the repository.
2895 0a22ca1a 2020-09-23 stsp */
2896 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2897 e8f02263 2022-01-23 thomas &blob1_size, path1);
2898 0a22ca1a 2020-09-23 stsp if (err)
2899 0a22ca1a 2020-09-23 stsp goto done;
2900 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2901 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2902 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2903 0a22ca1a 2020-09-23 stsp if (err)
2904 0a22ca1a 2020-09-23 stsp goto done;
2905 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2906 0a22ca1a 2020-09-23 stsp path1);
2907 0a22ca1a 2020-09-23 stsp if (err)
2908 0a22ca1a 2020-09-23 stsp goto done;
2909 0a22ca1a 2020-09-23 stsp if (ie)
2910 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2911 0a22ca1a 2020-09-23 stsp ie);
2912 0a22ca1a 2020-09-23 stsp } else {
2913 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2914 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2915 0a22ca1a 2020-09-23 stsp }
2916 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2917 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2918 0a22ca1a 2020-09-23 stsp free(id);
2919 0a22ca1a 2020-09-23 stsp if (err)
2920 0a22ca1a 2020-09-23 stsp goto done;
2921 0a22ca1a 2020-09-23 stsp break;
2922 0a22ca1a 2020-09-23 stsp }
2923 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2924 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2925 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2926 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2927 1ee397ad 2019-07-12 stsp if (err)
2928 1ee397ad 2019-07-12 stsp goto done;
2929 2b92fad7 2019-06-02 stsp break;
2930 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2931 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2932 1ee397ad 2019-07-12 stsp if (err)
2933 1ee397ad 2019-07-12 stsp goto done;
2934 2b92fad7 2019-06-02 stsp break;
2935 2b92fad7 2019-06-02 stsp default:
2936 2b92fad7 2019-06-02 stsp break;
2937 2b92fad7 2019-06-02 stsp }
2938 234035bc 2019-06-01 stsp } else if (blob2) {
2939 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2940 234035bc 2019-06-01 stsp path2) == -1)
2941 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2942 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2943 d6c87207 2019-08-02 stsp strlen(path2));
2944 234035bc 2019-06-01 stsp if (ie) {
2945 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2946 7f91a133 2019-12-13 stsp -1, NULL, repo);
2947 234035bc 2019-06-01 stsp if (err)
2948 234035bc 2019-06-01 stsp goto done;
2949 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2950 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2951 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2952 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2953 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2954 1ee397ad 2019-07-12 stsp status, path2);
2955 234035bc 2019-06-01 stsp goto done;
2956 234035bc 2019-06-01 stsp }
2957 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2958 36bf999c 2020-07-23 stsp char *link_target2;
2959 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2960 36bf999c 2020-07-23 stsp blob2);
2961 36bf999c 2020-07-23 stsp if (err)
2962 36bf999c 2020-07-23 stsp goto done;
2963 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2964 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2965 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2966 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2967 36bf999c 2020-07-23 stsp free(link_target2);
2968 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2969 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2970 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2971 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2972 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2973 526a746f 2020-07-23 stsp a->progress_arg);
2974 dfe9fba0 2020-07-23 stsp } else {
2975 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2976 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2977 526a746f 2020-07-23 stsp }
2978 dfe9fba0 2020-07-23 stsp if (err)
2979 dfe9fba0 2020-07-23 stsp goto done;
2980 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2981 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2982 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2983 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2984 234035bc 2019-06-01 stsp if (err)
2985 234035bc 2019-06-01 stsp goto done;
2986 234035bc 2019-06-01 stsp }
2987 234035bc 2019-06-01 stsp } else {
2988 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2989 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2990 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2991 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2992 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2993 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
2994 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
2995 2e1fa222 2020-07-23 stsp } else {
2996 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2997 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2998 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2999 2e1fa222 2020-07-23 stsp }
3000 234035bc 2019-06-01 stsp if (err)
3001 234035bc 2019-06-01 stsp goto done;
3002 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3003 234035bc 2019-06-01 stsp if (err)
3004 234035bc 2019-06-01 stsp goto done;
3005 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3006 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3007 3969253a 2020-03-07 stsp if (err) {
3008 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3009 3969253a 2020-03-07 stsp goto done;
3010 3969253a 2020-03-07 stsp }
3011 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3012 2b92fad7 2019-06-02 stsp if (err) {
3013 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3014 2b92fad7 2019-06-02 stsp goto done;
3015 2b92fad7 2019-06-02 stsp }
3016 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3017 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3018 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3019 2e1fa222 2020-07-23 stsp }
3020 234035bc 2019-06-01 stsp }
3021 234035bc 2019-06-01 stsp }
3022 234035bc 2019-06-01 stsp done:
3023 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3024 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3025 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3026 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3027 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3028 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3029 1af628f4 2021-06-03 stsp free(id_str);
3030 54d5be07 2021-06-03 stsp free(label_deriv2);
3031 234035bc 2019-06-01 stsp free(ondisk_path);
3032 234035bc 2019-06-01 stsp return err;
3033 234035bc 2019-06-01 stsp }
3034 234035bc 2019-06-01 stsp
3035 69de9dd4 2021-09-03 stsp static const struct got_error *
3036 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3037 69de9dd4 2021-09-03 stsp {
3038 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3039 69de9dd4 2021-09-03 stsp
3040 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3041 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3042 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3043 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3044 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3045 69de9dd4 2021-09-03 stsp
3046 69de9dd4 2021-09-03 stsp return NULL;
3047 69de9dd4 2021-09-03 stsp }
3048 69de9dd4 2021-09-03 stsp
3049 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3050 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3051 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3052 234035bc 2019-06-01 stsp struct got_repository *repo;
3053 234035bc 2019-06-01 stsp };
3054 234035bc 2019-06-01 stsp
3055 234035bc 2019-06-01 stsp static const struct got_error *
3056 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3057 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3058 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3059 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3060 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3061 234035bc 2019-06-01 stsp {
3062 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3063 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3064 234035bc 2019-06-01 stsp unsigned char status;
3065 234035bc 2019-06-01 stsp struct stat sb;
3066 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3067 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3068 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3069 234035bc 2019-06-01 stsp char *ondisk_path;
3070 234035bc 2019-06-01 stsp
3071 69de9dd4 2021-09-03 stsp if (id == NULL)
3072 69de9dd4 2021-09-03 stsp return NULL;
3073 234035bc 2019-06-01 stsp
3074 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3075 69de9dd4 2021-09-03 stsp if (ie == NULL)
3076 69de9dd4 2021-09-03 stsp return NULL;
3077 69de9dd4 2021-09-03 stsp
3078 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3079 234035bc 2019-06-01 stsp == -1)
3080 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3081 234035bc 2019-06-01 stsp
3082 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3083 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3084 5546d466 2021-09-02 stsp free(ondisk_path);
3085 234035bc 2019-06-01 stsp if (err)
3086 234035bc 2019-06-01 stsp return err;
3087 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3088 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3089 234035bc 2019-06-01 stsp
3090 234035bc 2019-06-01 stsp return NULL;
3091 234035bc 2019-06-01 stsp }
3092 234035bc 2019-06-01 stsp
3093 818c7501 2019-07-11 stsp static const struct got_error *
3094 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3095 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3096 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3097 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3098 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3099 234035bc 2019-06-01 stsp {
3100 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3101 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3102 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3103 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3104 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3105 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3106 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3107 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3108 234035bc 2019-06-01 stsp
3109 03415a1a 2019-06-02 stsp if (commit_id1) {
3110 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3111 945f9229 2022-04-16 thomas if (err)
3112 945f9229 2022-04-16 thomas goto done;
3113 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3114 03415a1a 2019-06-02 stsp worktree->path_prefix);
3115 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3116 03415a1a 2019-06-02 stsp goto done;
3117 69d57f3d 2020-07-31 stsp }
3118 69d57f3d 2020-07-31 stsp if (tree_id1) {
3119 69d57f3d 2020-07-31 stsp char *id_str;
3120 03415a1a 2019-06-02 stsp
3121 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3122 03415a1a 2019-06-02 stsp if (err)
3123 03415a1a 2019-06-02 stsp goto done;
3124 f69721c3 2019-10-21 stsp
3125 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3126 f69721c3 2019-10-21 stsp if (err)
3127 f69721c3 2019-10-21 stsp goto done;
3128 f69721c3 2019-10-21 stsp
3129 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3130 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3131 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3132 f69721c3 2019-10-21 stsp free(id_str);
3133 f69721c3 2019-10-21 stsp goto done;
3134 f69721c3 2019-10-21 stsp }
3135 f69721c3 2019-10-21 stsp free(id_str);
3136 a0f32f33 2022-06-13 thomas
3137 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3138 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3139 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3140 a0f32f33 2022-06-13 thomas goto done;
3141 a0f32f33 2022-06-13 thomas }
3142 03415a1a 2019-06-02 stsp }
3143 234035bc 2019-06-01 stsp
3144 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3145 945f9229 2022-04-16 thomas if (err)
3146 945f9229 2022-04-16 thomas goto done;
3147 945f9229 2022-04-16 thomas
3148 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3149 234035bc 2019-06-01 stsp worktree->path_prefix);
3150 234035bc 2019-06-01 stsp if (err)
3151 234035bc 2019-06-01 stsp goto done;
3152 234035bc 2019-06-01 stsp
3153 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3154 234035bc 2019-06-01 stsp if (err)
3155 234035bc 2019-06-01 stsp goto done;
3156 234035bc 2019-06-01 stsp
3157 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3158 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3159 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3160 a0f32f33 2022-06-13 thomas goto done;
3161 a0f32f33 2022-06-13 thomas }
3162 a0f32f33 2022-06-13 thomas
3163 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3164 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3165 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3166 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3167 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3168 69de9dd4 2021-09-03 stsp if (err)
3169 69de9dd4 2021-09-03 stsp goto done;
3170 69de9dd4 2021-09-03 stsp
3171 234035bc 2019-06-01 stsp arg.worktree = worktree;
3172 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3173 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3174 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3175 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3176 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3177 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3178 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3179 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3180 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3181 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3182 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3183 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3184 af12c6b9 2019-06-04 stsp err = sync_err;
3185 234035bc 2019-06-01 stsp done:
3186 945f9229 2022-04-16 thomas if (commit1)
3187 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3188 945f9229 2022-04-16 thomas if (commit2)
3189 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3190 234035bc 2019-06-01 stsp if (tree1)
3191 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3192 234035bc 2019-06-01 stsp if (tree2)
3193 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3194 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3195 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3196 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3197 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3198 f69721c3 2019-10-21 stsp free(label_orig);
3199 818c7501 2019-07-11 stsp return err;
3200 818c7501 2019-07-11 stsp }
3201 234035bc 2019-06-01 stsp
3202 818c7501 2019-07-11 stsp const struct got_error *
3203 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3204 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3205 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3206 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3207 818c7501 2019-07-11 stsp {
3208 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3209 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3210 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3211 818c7501 2019-07-11 stsp
3212 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3213 818c7501 2019-07-11 stsp if (err)
3214 818c7501 2019-07-11 stsp return err;
3215 818c7501 2019-07-11 stsp
3216 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3217 818c7501 2019-07-11 stsp if (err)
3218 818c7501 2019-07-11 stsp goto done;
3219 818c7501 2019-07-11 stsp
3220 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3221 69de9dd4 2021-09-03 stsp worktree);
3222 818c7501 2019-07-11 stsp if (err)
3223 818c7501 2019-07-11 stsp goto done;
3224 818c7501 2019-07-11 stsp
3225 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3226 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3227 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3228 818c7501 2019-07-11 stsp done:
3229 818c7501 2019-07-11 stsp if (fileindex)
3230 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3231 818c7501 2019-07-11 stsp free(fileindex_path);
3232 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3233 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3234 234035bc 2019-06-01 stsp err = unlockerr;
3235 234035bc 2019-06-01 stsp return err;
3236 234035bc 2019-06-01 stsp }
3237 234035bc 2019-06-01 stsp
3238 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3239 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3240 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3241 927df6b7 2019-02-10 stsp const char *status_path;
3242 927df6b7 2019-02-10 stsp size_t status_path_len;
3243 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3244 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3245 f8d1f275 2019-02-04 stsp void *status_arg;
3246 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3247 f8d1f275 2019-02-04 stsp void *cancel_arg;
3248 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3249 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3250 f2a9dc41 2019-12-13 tracey int report_unchanged;
3251 3143d852 2020-06-25 stsp int no_ignores;
3252 f8d1f275 2019-02-04 stsp };
3253 88d0e355 2019-08-03 stsp
3254 f8d1f275 2019-02-04 stsp static const struct got_error *
3255 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3256 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3257 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3258 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3259 927df6b7 2019-02-10 stsp {
3260 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3261 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3262 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3263 927df6b7 2019-02-10 stsp struct stat sb;
3264 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3265 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3266 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3267 927df6b7 2019-02-10 stsp
3268 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3269 98eaaa12 2019-08-03 stsp if (err)
3270 98eaaa12 2019-08-03 stsp return err;
3271 98eaaa12 2019-08-03 stsp
3272 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3273 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3274 98eaaa12 2019-08-03 stsp return NULL;
3275 98eaaa12 2019-08-03 stsp
3276 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3277 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3278 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3279 98eaaa12 2019-08-03 stsp }
3280 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3281 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3282 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3283 927df6b7 2019-02-10 stsp }
3284 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3285 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3286 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3287 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3288 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3289 98eaaa12 2019-08-03 stsp }
3290 98eaaa12 2019-08-03 stsp
3291 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3292 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3293 927df6b7 2019-02-10 stsp }
3294 927df6b7 2019-02-10 stsp
3295 927df6b7 2019-02-10 stsp static const struct got_error *
3296 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3297 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3298 f8d1f275 2019-02-04 stsp {
3299 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3300 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3301 f8d1f275 2019-02-04 stsp char *abspath;
3302 f8d1f275 2019-02-04 stsp
3303 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3304 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3305 0584f854 2019-04-06 stsp
3306 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3307 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3308 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3309 927df6b7 2019-02-10 stsp return NULL;
3310 927df6b7 2019-02-10 stsp
3311 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3312 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3313 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3314 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3315 f8d1f275 2019-02-04 stsp } else {
3316 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3317 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3318 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3319 f8d1f275 2019-02-04 stsp }
3320 f8d1f275 2019-02-04 stsp
3321 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3322 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3323 f8d1f275 2019-02-04 stsp free(abspath);
3324 9d31a1d8 2018-03-11 stsp return err;
3325 9d31a1d8 2018-03-11 stsp }
3326 f8d1f275 2019-02-04 stsp
3327 f8d1f275 2019-02-04 stsp static const struct got_error *
3328 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3329 f8d1f275 2019-02-04 stsp {
3330 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3331 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3332 2ec1f75b 2019-03-26 stsp unsigned char status;
3333 927df6b7 2019-02-10 stsp
3334 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3335 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3336 0584f854 2019-04-06 stsp
3337 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3338 927df6b7 2019-02-10 stsp return NULL;
3339 927df6b7 2019-02-10 stsp
3340 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3341 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3342 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3343 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3344 2ec1f75b 2019-03-26 stsp else
3345 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3346 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3347 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3348 6841da00 2019-08-08 stsp }
3349 6841da00 2019-08-08 stsp
3350 ef20f542 2022-06-26 thomas static void
3351 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3352 6841da00 2019-08-08 stsp {
3353 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3354 6841da00 2019-08-08 stsp
3355 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3356 6841da00 2019-08-08 stsp free((char *)pe->path);
3357 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3358 6841da00 2019-08-08 stsp }
3359 6841da00 2019-08-08 stsp
3360 ef20f542 2022-06-26 thomas static void
3361 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3362 6841da00 2019-08-08 stsp {
3363 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3364 6841da00 2019-08-08 stsp
3365 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3366 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3367 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3368 6841da00 2019-08-08 stsp free((char *)pe->path);
3369 6841da00 2019-08-08 stsp }
3370 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3371 6841da00 2019-08-08 stsp }
3372 6841da00 2019-08-08 stsp
3373 6841da00 2019-08-08 stsp static const struct got_error *
3374 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3375 6841da00 2019-08-08 stsp {
3376 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3377 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3378 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3379 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3380 6841da00 2019-08-08 stsp size_t linesize = 0;
3381 6841da00 2019-08-08 stsp ssize_t linelen;
3382 6841da00 2019-08-08 stsp
3383 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3384 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3385 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3386 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3387 6841da00 2019-08-08 stsp
3388 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3389 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3390 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3391 bd8de430 2019-10-04 stsp
3392 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3393 bd8de430 2019-10-04 stsp if (line[0] == '#')
3394 bd8de430 2019-10-04 stsp continue;
3395 bd8de430 2019-10-04 stsp
3396 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3397 bd8de430 2019-10-04 stsp if (line[0] == '!')
3398 bd8de430 2019-10-04 stsp continue;
3399 bd8de430 2019-10-04 stsp
3400 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3401 6841da00 2019-08-08 stsp line) == -1) {
3402 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3403 6841da00 2019-08-08 stsp goto done;
3404 6841da00 2019-08-08 stsp }
3405 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3406 6841da00 2019-08-08 stsp if (err)
3407 6841da00 2019-08-08 stsp goto done;
3408 6841da00 2019-08-08 stsp }
3409 6841da00 2019-08-08 stsp if (ferror(f)) {
3410 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3411 6841da00 2019-08-08 stsp goto done;
3412 6841da00 2019-08-08 stsp }
3413 6841da00 2019-08-08 stsp
3414 6841da00 2019-08-08 stsp dirpath = strdup(path);
3415 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3416 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3417 6841da00 2019-08-08 stsp goto done;
3418 6841da00 2019-08-08 stsp }
3419 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3420 6841da00 2019-08-08 stsp done:
3421 6841da00 2019-08-08 stsp free(line);
3422 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3423 6841da00 2019-08-08 stsp free(dirpath);
3424 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3425 6841da00 2019-08-08 stsp }
3426 6841da00 2019-08-08 stsp return err;
3427 6841da00 2019-08-08 stsp }
3428 6841da00 2019-08-08 stsp
3429 ef20f542 2022-06-26 thomas static int
3430 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3431 6841da00 2019-08-08 stsp {
3432 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3433 bd8de430 2019-10-04 stsp
3434 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3435 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3436 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3437 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3438 bd8de430 2019-10-04 stsp
3439 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3440 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3441 6841da00 2019-08-08 stsp
3442 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3443 bd8de430 2019-10-04 stsp continue;
3444 bd8de430 2019-10-04 stsp pattern += 3;
3445 bd8de430 2019-10-04 stsp p = path;
3446 bd8de430 2019-10-04 stsp while (*p) {
3447 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3448 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3449 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3450 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3451 bd8de430 2019-10-04 stsp p++;
3452 bd8de430 2019-10-04 stsp while (*p == '/')
3453 bd8de430 2019-10-04 stsp p++;
3454 bd8de430 2019-10-04 stsp continue;
3455 bd8de430 2019-10-04 stsp }
3456 bd8de430 2019-10-04 stsp return 1;
3457 bd8de430 2019-10-04 stsp }
3458 bd8de430 2019-10-04 stsp }
3459 bd8de430 2019-10-04 stsp }
3460 bd8de430 2019-10-04 stsp
3461 6841da00 2019-08-08 stsp /*
3462 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3463 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3464 6841da00 2019-08-08 stsp * ignores backwards.
3465 6841da00 2019-08-08 stsp */
3466 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3467 6841da00 2019-08-08 stsp while (pe) {
3468 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3469 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3470 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3471 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3472 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3473 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3474 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3475 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3476 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3477 6841da00 2019-08-08 stsp continue;
3478 6841da00 2019-08-08 stsp return 1;
3479 6841da00 2019-08-08 stsp }
3480 6841da00 2019-08-08 stsp }
3481 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3482 6841da00 2019-08-08 stsp }
3483 6841da00 2019-08-08 stsp
3484 6841da00 2019-08-08 stsp return 0;
3485 6841da00 2019-08-08 stsp }
3486 6841da00 2019-08-08 stsp
3487 6841da00 2019-08-08 stsp static const struct got_error *
3488 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3489 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3490 6841da00 2019-08-08 stsp {
3491 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3492 6841da00 2019-08-08 stsp char *ignorespath;
3493 886cec17 2019-12-15 stsp int fd = -1;
3494 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3495 6841da00 2019-08-08 stsp
3496 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3497 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3498 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3499 6841da00 2019-08-08 stsp
3500 886cec17 2019-12-15 stsp if (dirfd != -1) {
3501 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3502 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3503 886cec17 2019-12-15 stsp if (fd == -1) {
3504 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3505 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3506 886cec17 2019-12-15 stsp ignorespath);
3507 886cec17 2019-12-15 stsp } else {
3508 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3509 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3510 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3511 886cec17 2019-12-15 stsp ignorespath);
3512 886cec17 2019-12-15 stsp else {
3513 886cec17 2019-12-15 stsp fd = -1;
3514 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3515 886cec17 2019-12-15 stsp }
3516 886cec17 2019-12-15 stsp }
3517 886cec17 2019-12-15 stsp } else {
3518 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3519 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3520 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3521 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3522 886cec17 2019-12-15 stsp ignorespath);
3523 886cec17 2019-12-15 stsp } else
3524 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3525 886cec17 2019-12-15 stsp }
3526 6841da00 2019-08-08 stsp
3527 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3528 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3529 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3530 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3531 6841da00 2019-08-08 stsp free(ignorespath);
3532 6841da00 2019-08-08 stsp return err;
3533 f8d1f275 2019-02-04 stsp }
3534 f8d1f275 2019-02-04 stsp
3535 f8d1f275 2019-02-04 stsp static const struct got_error *
3536 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3537 6092c299 2021-10-04 thomas int dirfd)
3538 f8d1f275 2019-02-04 stsp {
3539 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3540 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3541 f8d1f275 2019-02-04 stsp char *path = NULL;
3542 6092c299 2021-10-04 thomas
3543 6092c299 2021-10-04 thomas if (ignore != NULL)
3544 6092c299 2021-10-04 thomas *ignore = 0;
3545 f8d1f275 2019-02-04 stsp
3546 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3547 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3548 0584f854 2019-04-06 stsp
3549 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3550 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3551 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3552 f8d1f275 2019-02-04 stsp } else {
3553 f8d1f275 2019-02-04 stsp path = de->d_name;
3554 f8d1f275 2019-02-04 stsp }
3555 f8d1f275 2019-02-04 stsp
3556 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3557 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3558 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3559 6092c299 2021-10-04 thomas *ignore = 1;
3560 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3561 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3562 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3563 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3564 f8d1f275 2019-02-04 stsp if (parent_path[0])
3565 f8d1f275 2019-02-04 stsp free(path);
3566 b72f483a 2019-02-05 stsp return err;
3567 f8d1f275 2019-02-04 stsp }
3568 f8d1f275 2019-02-04 stsp
3569 347d1d3e 2019-07-12 stsp static const struct got_error *
3570 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3571 3143d852 2020-06-25 stsp {
3572 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3573 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3574 3143d852 2020-06-25 stsp
3575 3143d852 2020-06-25 stsp if (a->no_ignores)
3576 3143d852 2020-06-25 stsp return NULL;
3577 3143d852 2020-06-25 stsp
3578 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3579 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3580 3143d852 2020-06-25 stsp if (err)
3581 3143d852 2020-06-25 stsp return err;
3582 3143d852 2020-06-25 stsp
3583 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3584 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3585 3143d852 2020-06-25 stsp
3586 3143d852 2020-06-25 stsp return err;
3587 3143d852 2020-06-25 stsp }
3588 3143d852 2020-06-25 stsp
3589 3143d852 2020-06-25 stsp static const struct got_error *
3590 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3591 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3592 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3593 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3594 abb4604f 2019-07-27 stsp {
3595 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3596 abb4604f 2019-07-27 stsp struct stat sb;
3597 ff56836b 2021-07-08 stsp
3598 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3599 abb4604f 2019-07-27 stsp if (ie)
3600 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3601 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3602 abb4604f 2019-07-27 stsp
3603 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3604 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3605 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3606 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3607 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3608 abb4604f 2019-07-27 stsp }
3609 abb4604f 2019-07-27 stsp
3610 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3611 fe1d8685 2022-02-12 thomas return NULL;
3612 fe1d8685 2022-02-12 thomas
3613 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3614 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3615 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3616 abb4604f 2019-07-27 stsp
3617 abb4604f 2019-07-27 stsp return NULL;
3618 3143d852 2020-06-25 stsp }
3619 3143d852 2020-06-25 stsp
3620 3143d852 2020-06-25 stsp static const struct got_error *
3621 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3622 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3623 3143d852 2020-06-25 stsp {
3624 3143d852 2020-06-25 stsp const struct got_error *err;
3625 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3626 3143d852 2020-06-25 stsp
3627 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3628 3143d852 2020-06-25 stsp ".cvsignore");
3629 3143d852 2020-06-25 stsp if (err)
3630 3143d852 2020-06-25 stsp return err;
3631 3143d852 2020-06-25 stsp
3632 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3633 3143d852 2020-06-25 stsp ".gitignore");
3634 3143d852 2020-06-25 stsp if (err)
3635 3143d852 2020-06-25 stsp return err;
3636 3143d852 2020-06-25 stsp
3637 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3638 3143d852 2020-06-25 stsp if (err) {
3639 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3640 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3641 3143d852 2020-06-25 stsp return err;
3642 3143d852 2020-06-25 stsp }
3643 3143d852 2020-06-25 stsp for (;;) {
3644 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3645 3143d852 2020-06-25 stsp ".cvsignore");
3646 3143d852 2020-06-25 stsp if (err)
3647 3143d852 2020-06-25 stsp break;
3648 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3649 3143d852 2020-06-25 stsp ".gitignore");
3650 3143d852 2020-06-25 stsp if (err)
3651 3143d852 2020-06-25 stsp break;
3652 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3653 3143d852 2020-06-25 stsp if (err) {
3654 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3655 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3656 3143d852 2020-06-25 stsp break;
3657 3143d852 2020-06-25 stsp }
3658 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3659 ff56836b 2021-07-08 stsp break;
3660 b737c85a 2020-06-26 stsp free(parent_path);
3661 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3662 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3663 3143d852 2020-06-25 stsp }
3664 3143d852 2020-06-25 stsp
3665 b737c85a 2020-06-26 stsp free(parent_path);
3666 b737c85a 2020-06-26 stsp free(next_parent_path);
3667 3143d852 2020-06-25 stsp return err;
3668 abb4604f 2019-07-27 stsp }
3669 abb4604f 2019-07-27 stsp
3670 abb4604f 2019-07-27 stsp static const struct got_error *
3671 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3672 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3673 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3674 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3675 f2a9dc41 2019-12-13 tracey int report_unchanged)
3676 f8d1f275 2019-02-04 stsp {
3677 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3678 6fc93f37 2019-12-13 stsp int fd = -1;
3679 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3680 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3681 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3682 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3683 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3684 3143d852 2020-06-25 stsp
3685 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3686 f8d1f275 2019-02-04 stsp
3687 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3688 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3689 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3690 8dc303cc 2019-07-27 stsp
3691 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3692 a78810f8 2022-03-13 thomas if (ie) {
3693 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3694 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3695 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3696 a78810f8 2022-03-13 thomas goto done;
3697 a78810f8 2022-03-13 thomas }
3698 a78810f8 2022-03-13 thomas
3699 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3700 6fc93f37 2019-12-13 stsp if (fd == -1) {
3701 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3702 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3703 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3704 ff56836b 2021-07-08 stsp else {
3705 ff56836b 2021-07-08 stsp if (!no_ignores) {
3706 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3707 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3708 ff56836b 2021-07-08 stsp if (err)
3709 ff56836b 2021-07-08 stsp goto done;
3710 ff56836b 2021-07-08 stsp }
3711 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3712 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3713 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3714 ff56836b 2021-07-08 stsp }
3715 abb4604f 2019-07-27 stsp } else {
3716 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3717 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3718 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3719 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3720 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3721 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3722 abb4604f 2019-07-27 stsp arg.status_path = path;
3723 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3724 abb4604f 2019-07-27 stsp arg.repo = repo;
3725 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3726 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3727 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3728 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3729 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3730 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3731 022fae89 2019-12-06 tracey if (!no_ignores) {
3732 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3733 3143d852 2020-06-25 stsp worktree->root_path, path);
3734 3143d852 2020-06-25 stsp if (err)
3735 3143d852 2020-06-25 stsp goto done;
3736 022fae89 2019-12-06 tracey }
3737 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3738 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3739 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3740 927df6b7 2019-02-10 stsp }
3741 3143d852 2020-06-25 stsp done:
3742 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3743 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3744 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3745 927df6b7 2019-02-10 stsp free(ondisk_path);
3746 347d1d3e 2019-07-12 stsp return err;
3747 347d1d3e 2019-07-12 stsp }
3748 347d1d3e 2019-07-12 stsp
3749 347d1d3e 2019-07-12 stsp const struct got_error *
3750 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3751 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3752 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3753 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3754 347d1d3e 2019-07-12 stsp {
3755 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3756 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3757 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3758 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3759 347d1d3e 2019-07-12 stsp
3760 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3761 347d1d3e 2019-07-12 stsp if (err)
3762 347d1d3e 2019-07-12 stsp return err;
3763 347d1d3e 2019-07-12 stsp
3764 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3765 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3766 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3767 f6343036 2021-06-22 stsp no_ignores, 0);
3768 72ea6654 2019-07-27 stsp if (err)
3769 72ea6654 2019-07-27 stsp break;
3770 72ea6654 2019-07-27 stsp }
3771 f8d1f275 2019-02-04 stsp free(fileindex_path);
3772 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3773 f8d1f275 2019-02-04 stsp return err;
3774 f8d1f275 2019-02-04 stsp }
3775 6c7ab921 2019-03-18 stsp
3776 6c7ab921 2019-03-18 stsp const struct got_error *
3777 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3778 6c7ab921 2019-03-18 stsp const char *arg)
3779 6c7ab921 2019-03-18 stsp {
3780 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3781 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3782 6c7ab921 2019-03-18 stsp size_t len;
3783 00bb5ea0 2020-07-23 stsp struct stat sb;
3784 01740607 2020-11-04 stsp char *abspath = NULL;
3785 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3786 6c7ab921 2019-03-18 stsp
3787 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3788 6c7ab921 2019-03-18 stsp
3789 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3790 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3791 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3792 00bb5ea0 2020-07-23 stsp
3793 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3794 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3795 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3796 00bb5ea0 2020-07-23 stsp goto done;
3797 00bb5ea0 2020-07-23 stsp }
3798 727173c3 2020-11-06 stsp sb.st_mode = 0;
3799 00bb5ea0 2020-07-23 stsp }
3800 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3801 00bb5ea0 2020-07-23 stsp /*
3802 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3803 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3804 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3805 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3806 00bb5ea0 2020-07-23 stsp */
3807 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3808 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3809 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3810 00bb5ea0 2020-07-23 stsp goto done;
3811 00bb5ea0 2020-07-23 stsp }
3812 00bb5ea0 2020-07-23 stsp
3813 00bb5ea0 2020-07-23 stsp }
3814 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3815 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3816 00bb5ea0 2020-07-23 stsp if (err)
3817 d0710d08 2019-07-22 stsp goto done;
3818 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3819 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3820 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3821 00bb5ea0 2020-07-23 stsp goto done;
3822 00bb5ea0 2020-07-23 stsp }
3823 00bb5ea0 2020-07-23 stsp } else {
3824 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3825 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3826 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3827 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3828 00bb5ea0 2020-07-23 stsp goto done;
3829 00bb5ea0 2020-07-23 stsp }
3830 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3831 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3832 01740607 2020-11-04 stsp goto done;
3833 01740607 2020-11-04 stsp }
3834 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3835 01740607 2020-11-04 stsp sizeof(canonpath));
3836 01740607 2020-11-04 stsp if (err)
3837 00bb5ea0 2020-07-23 stsp goto done;
3838 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3839 01740607 2020-11-04 stsp if (resolved == NULL) {
3840 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3841 01740607 2020-11-04 stsp goto done;
3842 00bb5ea0 2020-07-23 stsp }
3843 d0710d08 2019-07-22 stsp }
3844 d0710d08 2019-07-22 stsp }
3845 6c7ab921 2019-03-18 stsp
3846 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3847 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3848 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3849 6c7ab921 2019-03-18 stsp goto done;
3850 6c7ab921 2019-03-18 stsp }
3851 6c7ab921 2019-03-18 stsp
3852 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3853 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3854 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3855 f6d88e1a 2019-05-29 stsp if (err)
3856 f6d88e1a 2019-05-29 stsp goto done;
3857 f6d88e1a 2019-05-29 stsp } else {
3858 f6d88e1a 2019-05-29 stsp path = strdup("");
3859 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3860 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3861 f6d88e1a 2019-05-29 stsp goto done;
3862 f6d88e1a 2019-05-29 stsp }
3863 6c7ab921 2019-03-18 stsp }
3864 6c7ab921 2019-03-18 stsp
3865 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3866 6c7ab921 2019-03-18 stsp len = strlen(path);
3867 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3868 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3869 6c7ab921 2019-03-18 stsp len--;
3870 6c7ab921 2019-03-18 stsp }
3871 6c7ab921 2019-03-18 stsp done:
3872 01740607 2020-11-04 stsp free(abspath);
3873 6c7ab921 2019-03-18 stsp free(resolved);
3874 d0710d08 2019-07-22 stsp free(cwd);
3875 6c7ab921 2019-03-18 stsp if (err == NULL)
3876 6c7ab921 2019-03-18 stsp *wt_path = path;
3877 6c7ab921 2019-03-18 stsp else
3878 6c7ab921 2019-03-18 stsp free(path);
3879 d00136be 2019-03-26 stsp return err;
3880 d00136be 2019-03-26 stsp }
3881 d00136be 2019-03-26 stsp
3882 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3883 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3884 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3885 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3886 4e68cba3 2019-11-23 stsp void *progress_arg;
3887 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3888 4e68cba3 2019-11-23 stsp };
3889 4e68cba3 2019-11-23 stsp
3890 4e68cba3 2019-11-23 stsp static const struct got_error *
3891 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3892 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3893 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3894 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3895 07f5b47a 2019-06-02 stsp {
3896 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3897 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3898 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3899 6d022e97 2019-08-04 stsp struct stat sb;
3900 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3901 07f5b47a 2019-06-02 stsp
3902 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3903 dbb83fbd 2019-12-12 stsp relpath) == -1)
3904 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3905 dbb83fbd 2019-12-12 stsp
3906 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3907 6d022e97 2019-08-04 stsp if (ie) {
3908 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3909 12463d8b 2019-12-13 stsp de_name, a->repo);
3910 6d022e97 2019-08-04 stsp if (err)
3911 dbb83fbd 2019-12-12 stsp goto done;
3912 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3913 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3914 dbb83fbd 2019-12-12 stsp goto done;
3915 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3916 dbb83fbd 2019-12-12 stsp if (err)
3917 dbb83fbd 2019-12-12 stsp goto done;
3918 6d022e97 2019-08-04 stsp }
3919 07f5b47a 2019-06-02 stsp
3920 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3921 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3922 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3923 84bf00a6 2022-02-12 thomas else
3924 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3925 dbb83fbd 2019-12-12 stsp goto done;
3926 4e68cba3 2019-11-23 stsp }
3927 07f5b47a 2019-06-02 stsp
3928 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3929 4e68cba3 2019-11-23 stsp if (err)
3930 4e68cba3 2019-11-23 stsp goto done;
3931 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3932 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3933 3969253a 2020-03-07 stsp if (err) {
3934 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3935 3969253a 2020-03-07 stsp goto done;
3936 3969253a 2020-03-07 stsp }
3937 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3938 07f5b47a 2019-06-02 stsp if (err) {
3939 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3940 4e68cba3 2019-11-23 stsp goto done;
3941 07f5b47a 2019-06-02 stsp }
3942 4e68cba3 2019-11-23 stsp done:
3943 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3944 dbb83fbd 2019-12-12 stsp if (err)
3945 dbb83fbd 2019-12-12 stsp return err;
3946 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3947 dbb83fbd 2019-12-12 stsp return NULL;
3948 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3949 07f5b47a 2019-06-02 stsp }
3950 07f5b47a 2019-06-02 stsp
3951 d00136be 2019-03-26 stsp const struct got_error *
3952 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3953 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3954 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3955 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3956 d00136be 2019-03-26 stsp {
3957 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3958 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3959 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3960 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3961 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3962 d00136be 2019-03-26 stsp
3963 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3964 d00136be 2019-03-26 stsp if (err)
3965 d00136be 2019-03-26 stsp return err;
3966 d00136be 2019-03-26 stsp
3967 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3968 d00136be 2019-03-26 stsp if (err)
3969 d00136be 2019-03-26 stsp goto done;
3970 d00136be 2019-03-26 stsp
3971 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3972 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3973 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3974 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3975 4e68cba3 2019-11-23 stsp saa.repo = repo;
3976 4e68cba3 2019-11-23 stsp
3977 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3978 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3979 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3980 1dd54920 2019-05-11 stsp if (err)
3981 af12c6b9 2019-06-04 stsp break;
3982 1dd54920 2019-05-11 stsp }
3983 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3984 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3985 af12c6b9 2019-06-04 stsp err = sync_err;
3986 d00136be 2019-03-26 stsp done:
3987 fb399478 2019-07-12 stsp free(fileindex_path);
3988 d00136be 2019-03-26 stsp if (fileindex)
3989 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3990 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3991 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3992 d00136be 2019-03-26 stsp err = unlockerr;
3993 6c7ab921 2019-03-18 stsp return err;
3994 6c7ab921 2019-03-18 stsp }
3995 17ed4618 2019-06-02 stsp
3996 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3997 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3998 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3999 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4000 f2a9dc41 2019-12-13 tracey void *progress_arg;
4001 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4002 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4003 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4004 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4005 766841c2 2020-08-13 stsp const char *status_codes;
4006 f2a9dc41 2019-12-13 tracey };
4007 f2a9dc41 2019-12-13 tracey
4008 f2a9dc41 2019-12-13 tracey static const struct got_error *
4009 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4010 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4011 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4012 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4013 17ed4618 2019-06-02 stsp {
4014 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4015 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4016 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4017 17ed4618 2019-06-02 stsp struct stat sb;
4018 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4019 d64cc78a 2022-01-25 thomas
4020 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4021 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4022 d64cc78a 2022-01-25 thomas return NULL;
4023 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4024 d64cc78a 2022-01-25 thomas }
4025 17ed4618 2019-06-02 stsp
4026 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4027 17ed4618 2019-06-02 stsp if (ie == NULL)
4028 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4029 2ec1f75b 2019-03-26 stsp
4030 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4031 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4032 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4033 9acbc4fa 2019-08-03 stsp return NULL;
4034 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4035 9acbc4fa 2019-08-03 stsp }
4036 9acbc4fa 2019-08-03 stsp
4037 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4038 f2a9dc41 2019-12-13 tracey relpath) == -1)
4039 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4040 f2a9dc41 2019-12-13 tracey
4041 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4042 12463d8b 2019-12-13 stsp a->repo);
4043 17ed4618 2019-06-02 stsp if (err)
4044 f2a9dc41 2019-12-13 tracey goto done;
4045 17ed4618 2019-06-02 stsp
4046 766841c2 2020-08-13 stsp if (a->status_codes) {
4047 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4048 766841c2 2020-08-13 stsp int i;
4049 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4050 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4051 766841c2 2020-08-13 stsp break;
4052 766841c2 2020-08-13 stsp }
4053 766841c2 2020-08-13 stsp if (i == ncodes) {
4054 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4055 766841c2 2020-08-13 stsp free(ondisk_path);
4056 766841c2 2020-08-13 stsp return NULL;
4057 766841c2 2020-08-13 stsp }
4058 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4059 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4060 766841c2 2020-08-13 stsp static char msg[64];
4061 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4062 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4063 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4064 766841c2 2020-08-13 stsp goto done;
4065 766841c2 2020-08-13 stsp }
4066 766841c2 2020-08-13 stsp }
4067 766841c2 2020-08-13 stsp
4068 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4069 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4070 f2a9dc41 2019-12-13 tracey goto done;
4071 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4072 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4073 f2a9dc41 2019-12-13 tracey goto done;
4074 f2a9dc41 2019-12-13 tracey }
4075 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4076 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4077 d64cc78a 2022-01-25 thomas goto done;
4078 d64cc78a 2022-01-25 thomas }
4079 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4080 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4081 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4082 f2a9dc41 2019-12-13 tracey goto done;
4083 f2a9dc41 2019-12-13 tracey }
4084 17ed4618 2019-06-02 stsp }
4085 17ed4618 2019-06-02 stsp
4086 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4087 20a2ad1c 2020-10-20 stsp size_t root_len;
4088 20a2ad1c 2020-10-20 stsp
4089 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4090 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4091 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4092 12463d8b 2019-12-13 stsp ondisk_path);
4093 12463d8b 2019-12-13 stsp goto done;
4094 12463d8b 2019-12-13 stsp }
4095 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4096 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4097 12463d8b 2019-12-13 stsp goto done;
4098 15341bfd 2020-03-05 tracey }
4099 15341bfd 2020-03-05 tracey
4100 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4101 20a2ad1c 2020-10-20 stsp do {
4102 20a2ad1c 2020-10-20 stsp char *parent;
4103 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4104 20a2ad1c 2020-10-20 stsp if (err)
4105 2513f20a 2020-10-20 stsp goto done;
4106 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4107 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4108 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4109 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4110 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4111 20a2ad1c 2020-10-20 stsp ondisk_path);
4112 15341bfd 2020-03-05 tracey break;
4113 15341bfd 2020-03-05 tracey }
4114 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4115 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4116 f2a9dc41 2019-12-13 tracey }
4117 17ed4618 2019-06-02 stsp
4118 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4119 f2a9dc41 2019-12-13 tracey done:
4120 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4121 f2a9dc41 2019-12-13 tracey if (err)
4122 f2a9dc41 2019-12-13 tracey return err;
4123 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4124 f2a9dc41 2019-12-13 tracey return NULL;
4125 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4126 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4127 17ed4618 2019-06-02 stsp }
4128 17ed4618 2019-06-02 stsp
4129 2ec1f75b 2019-03-26 stsp const struct got_error *
4130 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4131 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4132 766841c2 2020-08-13 stsp const char *status_codes,
4133 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4134 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4135 2ec1f75b 2019-03-26 stsp {
4136 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4137 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4138 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4139 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4140 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4141 2ec1f75b 2019-03-26 stsp
4142 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4143 2ec1f75b 2019-03-26 stsp if (err)
4144 2ec1f75b 2019-03-26 stsp return err;
4145 2ec1f75b 2019-03-26 stsp
4146 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4147 2ec1f75b 2019-03-26 stsp if (err)
4148 2ec1f75b 2019-03-26 stsp goto done;
4149 f2a9dc41 2019-12-13 tracey
4150 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4151 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4152 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4153 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4154 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4155 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4156 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4157 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4158 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4159 2ec1f75b 2019-03-26 stsp
4160 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4161 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4162 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4163 17ed4618 2019-06-02 stsp if (err)
4164 af12c6b9 2019-06-04 stsp break;
4165 2ec1f75b 2019-03-26 stsp }
4166 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4167 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4168 af12c6b9 2019-06-04 stsp err = sync_err;
4169 2ec1f75b 2019-03-26 stsp done:
4170 fb399478 2019-07-12 stsp free(fileindex_path);
4171 2ec1f75b 2019-03-26 stsp if (fileindex)
4172 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4173 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4174 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4175 2ec1f75b 2019-03-26 stsp err = unlockerr;
4176 33aa809d 2019-08-08 stsp return err;
4177 33aa809d 2019-08-08 stsp }
4178 33aa809d 2019-08-08 stsp
4179 33aa809d 2019-08-08 stsp static const struct got_error *
4180 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4181 33aa809d 2019-08-08 stsp {
4182 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4183 33aa809d 2019-08-08 stsp char *line = NULL;
4184 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4185 33aa809d 2019-08-08 stsp ssize_t linelen;
4186 33aa809d 2019-08-08 stsp
4187 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4188 33aa809d 2019-08-08 stsp if (linelen == -1) {
4189 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4190 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4191 33aa809d 2019-08-08 stsp goto done;
4192 33aa809d 2019-08-08 stsp }
4193 33aa809d 2019-08-08 stsp return NULL;
4194 33aa809d 2019-08-08 stsp }
4195 33aa809d 2019-08-08 stsp if (outfile) {
4196 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4197 33aa809d 2019-08-08 stsp if (n != linelen) {
4198 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4199 33aa809d 2019-08-08 stsp goto done;
4200 33aa809d 2019-08-08 stsp }
4201 33aa809d 2019-08-08 stsp }
4202 33aa809d 2019-08-08 stsp if (rejectfile) {
4203 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4204 33aa809d 2019-08-08 stsp if (n != linelen)
4205 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4206 33aa809d 2019-08-08 stsp }
4207 33aa809d 2019-08-08 stsp done:
4208 33aa809d 2019-08-08 stsp free(line);
4209 2ec1f75b 2019-03-26 stsp return err;
4210 2ec1f75b 2019-03-26 stsp }
4211 1f1abb7e 2019-08-08 stsp
4212 33aa809d 2019-08-08 stsp static const struct got_error *
4213 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4214 33aa809d 2019-08-08 stsp {
4215 33aa809d 2019-08-08 stsp char *line = NULL;
4216 33aa809d 2019-08-08 stsp size_t linesize = 0;
4217 33aa809d 2019-08-08 stsp ssize_t linelen;
4218 33aa809d 2019-08-08 stsp
4219 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4220 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4221 500467ff 2019-09-25 hiltjo if (ferror(f))
4222 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4223 500467ff 2019-09-25 hiltjo return NULL;
4224 500467ff 2019-09-25 hiltjo }
4225 33aa809d 2019-08-08 stsp free(line);
4226 33aa809d 2019-08-08 stsp return NULL;
4227 33aa809d 2019-08-08 stsp }
4228 33aa809d 2019-08-08 stsp
4229 33aa809d 2019-08-08 stsp static const struct got_error *
4230 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4231 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4232 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4233 abc59930 2021-09-05 naddy {
4234 33aa809d 2019-08-08 stsp const struct got_error *err;
4235 33aa809d 2019-08-08 stsp
4236 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4237 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4238 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4239 33aa809d 2019-08-08 stsp if (err)
4240 33aa809d 2019-08-08 stsp return err;
4241 33aa809d 2019-08-08 stsp (*line_cur1)++;
4242 33aa809d 2019-08-08 stsp }
4243 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4244 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4245 33aa809d 2019-08-08 stsp if (rejectfile)
4246 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4247 33aa809d 2019-08-08 stsp else
4248 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4249 33aa809d 2019-08-08 stsp if (err)
4250 33aa809d 2019-08-08 stsp return err;
4251 33aa809d 2019-08-08 stsp (*line_cur2)++;
4252 33aa809d 2019-08-08 stsp }
4253 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4254 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4255 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4256 33aa809d 2019-08-08 stsp if (err)
4257 33aa809d 2019-08-08 stsp return err;
4258 33aa809d 2019-08-08 stsp (*line_cur2)++;
4259 33aa809d 2019-08-08 stsp }
4260 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4261 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4262 33aa809d 2019-08-08 stsp if (rejectfile)
4263 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4264 33aa809d 2019-08-08 stsp else
4265 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4266 33aa809d 2019-08-08 stsp if (err)
4267 33aa809d 2019-08-08 stsp return err;
4268 33aa809d 2019-08-08 stsp (*line_cur1)++;
4269 33aa809d 2019-08-08 stsp }
4270 f1e81a05 2019-08-10 stsp
4271 f1e81a05 2019-08-10 stsp return NULL;
4272 f1e81a05 2019-08-10 stsp }
4273 f1e81a05 2019-08-10 stsp
4274 f1e81a05 2019-08-10 stsp static const struct got_error *
4275 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4276 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4277 f1e81a05 2019-08-10 stsp {
4278 f1e81a05 2019-08-10 stsp const struct got_error *err;
4279 f1e81a05 2019-08-10 stsp
4280 f1e81a05 2019-08-10 stsp if (outfile) {
4281 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4282 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4283 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4284 f1e81a05 2019-08-10 stsp if (err)
4285 f1e81a05 2019-08-10 stsp return err;
4286 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4287 f1e81a05 2019-08-10 stsp }
4288 f1e81a05 2019-08-10 stsp }
4289 f1e81a05 2019-08-10 stsp if (rejectfile) {
4290 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4291 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4292 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4293 f1e81a05 2019-08-10 stsp if (err)
4294 f1e81a05 2019-08-10 stsp return err;
4295 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4296 f1e81a05 2019-08-10 stsp }
4297 33aa809d 2019-08-08 stsp }
4298 33aa809d 2019-08-08 stsp
4299 33aa809d 2019-08-08 stsp return NULL;
4300 33aa809d 2019-08-08 stsp }
4301 33aa809d 2019-08-08 stsp
4302 33aa809d 2019-08-08 stsp static const struct got_error *
4303 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4304 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4305 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4306 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4307 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4308 33aa809d 2019-08-08 stsp {
4309 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4310 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4311 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4312 33aa809d 2019-08-08 stsp FILE *hunkfile;
4313 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4314 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4315 fe621944 2020-11-10 stsp int rc;
4316 33aa809d 2019-08-08 stsp
4317 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4318 33aa809d 2019-08-08 stsp
4319 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4320 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4321 fe621944 2020-11-10 stsp start_old = cc.left.start;
4322 fe621944 2020-11-10 stsp end_old = cc.left.end;
4323 fe621944 2020-11-10 stsp start_new = cc.right.start;
4324 fe621944 2020-11-10 stsp end_new = cc.right.end;
4325 33aa809d 2019-08-08 stsp
4326 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4327 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4328 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4329 33aa809d 2019-08-08 stsp
4330 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4331 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4332 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4333 33aa809d 2019-08-08 stsp
4334 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4335 fe621944 2020-11-10 stsp if (diff_state == NULL)
4336 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4337 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4338 fe621944 2020-11-10 stsp
4339 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4340 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4341 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4342 33aa809d 2019-08-08 stsp goto done;
4343 33aa809d 2019-08-08 stsp }
4344 fe621944 2020-11-10 stsp
4345 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4346 fe621944 2020-11-10 stsp diff_result, &cc);
4347 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4348 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4349 33aa809d 2019-08-08 stsp goto done;
4350 33aa809d 2019-08-08 stsp }
4351 fe621944 2020-11-10 stsp
4352 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4353 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4354 33aa809d 2019-08-08 stsp goto done;
4355 33aa809d 2019-08-08 stsp }
4356 33aa809d 2019-08-08 stsp
4357 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4358 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4359 33aa809d 2019-08-08 stsp if (err)
4360 33aa809d 2019-08-08 stsp goto done;
4361 33aa809d 2019-08-08 stsp
4362 33aa809d 2019-08-08 stsp switch (*choice) {
4363 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4364 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4365 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4366 33aa809d 2019-08-08 stsp break;
4367 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4368 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4369 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4370 33aa809d 2019-08-08 stsp break;
4371 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4372 33aa809d 2019-08-08 stsp break;
4373 33aa809d 2019-08-08 stsp default:
4374 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4375 33aa809d 2019-08-08 stsp break;
4376 33aa809d 2019-08-08 stsp }
4377 33aa809d 2019-08-08 stsp done:
4378 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4379 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4380 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4381 33aa809d 2019-08-08 stsp return err;
4382 33aa809d 2019-08-08 stsp }
4383 33aa809d 2019-08-08 stsp
4384 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4385 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4386 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4387 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4388 1f1abb7e 2019-08-08 stsp void *progress_arg;
4389 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4390 33aa809d 2019-08-08 stsp void *patch_arg;
4391 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4392 10604dce 2021-09-24 thomas int unlink_added_files;
4393 1f1abb7e 2019-08-08 stsp };
4394 a129376b 2019-03-28 stsp
4395 e20a8b6f 2019-06-04 stsp static const struct got_error *
4396 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4397 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4398 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4399 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4400 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4401 33aa809d 2019-08-08 stsp {
4402 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4403 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4404 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4405 1ebedb77 2019-10-19 stsp int fd2 = -1;
4406 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4407 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4408 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4409 fe621944 2020-11-10 stsp struct stat sb2;
4410 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4411 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4412 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4413 33aa809d 2019-08-08 stsp
4414 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4415 33aa809d 2019-08-08 stsp
4416 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4417 33aa809d 2019-08-08 stsp if (err)
4418 33aa809d 2019-08-08 stsp return err;
4419 33aa809d 2019-08-08 stsp
4420 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4421 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4422 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4423 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4424 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4425 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4426 fa3cef63 2020-07-23 stsp goto done;
4427 fa3cef63 2020-07-23 stsp }
4428 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4429 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4430 48b4f239 2021-12-31 thomas if (link_len == -1) {
4431 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4432 48b4f239 2021-12-31 thomas path2);
4433 48b4f239 2021-12-31 thomas }
4434 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4435 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4436 12463d8b 2019-12-13 stsp }
4437 12463d8b 2019-12-13 stsp } else {
4438 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4439 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4440 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4441 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4442 fa3cef63 2020-07-23 stsp goto done;
4443 fa3cef63 2020-07-23 stsp }
4444 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4445 fa3cef63 2020-07-23 stsp sizeof(link_target));
4446 fa3cef63 2020-07-23 stsp if (link_len == -1)
4447 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4448 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4449 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4450 12463d8b 2019-12-13 stsp }
4451 1ebedb77 2019-10-19 stsp }
4452 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4453 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4454 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4455 fa3cef63 2020-07-23 stsp goto done;
4456 fa3cef63 2020-07-23 stsp }
4457 1ebedb77 2019-10-19 stsp
4458 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4459 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4460 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4461 fa3cef63 2020-07-23 stsp goto done;
4462 fa3cef63 2020-07-23 stsp }
4463 fa3cef63 2020-07-23 stsp fd2 = -1;
4464 fa3cef63 2020-07-23 stsp } else {
4465 fa3cef63 2020-07-23 stsp size_t n;
4466 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4467 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4468 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4469 fa3cef63 2020-07-23 stsp goto done;
4470 fa3cef63 2020-07-23 stsp }
4471 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4472 fa3cef63 2020-07-23 stsp if (n != link_len) {
4473 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4474 fa3cef63 2020-07-23 stsp goto done;
4475 fa3cef63 2020-07-23 stsp }
4476 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4477 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4478 fa3cef63 2020-07-23 stsp goto done;
4479 fa3cef63 2020-07-23 stsp }
4480 fa3cef63 2020-07-23 stsp rewind(f2);
4481 33aa809d 2019-08-08 stsp }
4482 33aa809d 2019-08-08 stsp
4483 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4484 33aa809d 2019-08-08 stsp if (err)
4485 33aa809d 2019-08-08 stsp goto done;
4486 33aa809d 2019-08-08 stsp
4487 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4488 33aa809d 2019-08-08 stsp if (err)
4489 33aa809d 2019-08-08 stsp goto done;
4490 33aa809d 2019-08-08 stsp
4491 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4492 33aa809d 2019-08-08 stsp if (err)
4493 33aa809d 2019-08-08 stsp goto done;
4494 33aa809d 2019-08-08 stsp
4495 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4496 fe621944 2020-11-10 stsp NULL);
4497 33aa809d 2019-08-08 stsp if (err)
4498 33aa809d 2019-08-08 stsp goto done;
4499 33aa809d 2019-08-08 stsp
4500 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4501 33aa809d 2019-08-08 stsp if (err)
4502 33aa809d 2019-08-08 stsp goto done;
4503 33aa809d 2019-08-08 stsp
4504 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4505 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4506 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4507 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4508 fe621944 2020-11-10 stsp
4509 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4510 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4511 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4512 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4513 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4514 fe621944 2020-11-10 stsp nchanges++;
4515 fe621944 2020-11-10 stsp }
4516 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4517 33aa809d 2019-08-08 stsp int choice;
4518 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4519 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4520 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4521 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4522 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4523 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4524 33aa809d 2019-08-08 stsp if (err)
4525 33aa809d 2019-08-08 stsp goto done;
4526 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4527 33aa809d 2019-08-08 stsp have_content = 1;
4528 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4529 33aa809d 2019-08-08 stsp break;
4530 33aa809d 2019-08-08 stsp }
4531 1ebedb77 2019-10-19 stsp if (have_content) {
4532 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4533 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4534 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4535 1ebedb77 2019-10-19 stsp if (err)
4536 1ebedb77 2019-10-19 stsp goto done;
4537 1ebedb77 2019-10-19 stsp
4538 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4539 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4540 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4541 fa3cef63 2020-07-23 stsp goto done;
4542 fa3cef63 2020-07-23 stsp }
4543 1ebedb77 2019-10-19 stsp }
4544 1ebedb77 2019-10-19 stsp }
4545 33aa809d 2019-08-08 stsp done:
4546 33aa809d 2019-08-08 stsp free(id_str);
4547 33aa809d 2019-08-08 stsp if (blob)
4548 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4549 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4550 fe621944 2020-11-10 stsp if (err == NULL)
4551 fe621944 2020-11-10 stsp err = free_err;
4552 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4553 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4554 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4555 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4556 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4557 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4558 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4559 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4560 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4561 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4562 33aa809d 2019-08-08 stsp if (err || !have_content) {
4563 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4564 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4565 33aa809d 2019-08-08 stsp free(*path_outfile);
4566 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4567 33aa809d 2019-08-08 stsp }
4568 33aa809d 2019-08-08 stsp free(path1);
4569 33aa809d 2019-08-08 stsp return err;
4570 33aa809d 2019-08-08 stsp }
4571 33aa809d 2019-08-08 stsp
4572 33aa809d 2019-08-08 stsp static const struct got_error *
4573 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4574 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4575 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4576 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4577 a129376b 2019-03-28 stsp {
4578 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4579 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4580 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4581 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4582 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4583 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4584 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4585 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4586 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4587 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4588 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4589 a129376b 2019-03-28 stsp
4590 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4591 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4592 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4593 d3bcc3d1 2019-08-08 stsp return NULL;
4594 3d69ad8d 2019-08-17 semarie
4595 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4596 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4597 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4598 d3bcc3d1 2019-08-08 stsp
4599 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4600 65084dad 2019-08-08 stsp if (ie == NULL)
4601 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4602 a129376b 2019-03-28 stsp
4603 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4604 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4605 a129376b 2019-03-28 stsp if (err) {
4606 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4607 a129376b 2019-03-28 stsp goto done;
4608 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4609 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4610 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4611 e20a8b6f 2019-06-04 stsp goto done;
4612 e20a8b6f 2019-06-04 stsp }
4613 a129376b 2019-03-28 stsp }
4614 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4615 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4616 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4617 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4618 a129376b 2019-03-28 stsp goto done;
4619 a129376b 2019-03-28 stsp }
4620 a129376b 2019-03-28 stsp } else {
4621 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4622 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4623 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4624 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4625 a129376b 2019-03-28 stsp goto done;
4626 a129376b 2019-03-28 stsp }
4627 a129376b 2019-03-28 stsp } else {
4628 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4629 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4630 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4631 a129376b 2019-03-28 stsp goto done;
4632 a129376b 2019-03-28 stsp }
4633 a129376b 2019-03-28 stsp }
4634 a129376b 2019-03-28 stsp }
4635 a129376b 2019-03-28 stsp
4636 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4637 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4638 945f9229 2022-04-16 thomas if (err)
4639 945f9229 2022-04-16 thomas goto done;
4640 945f9229 2022-04-16 thomas
4641 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4642 a9fa2909 2019-07-27 stsp if (err) {
4643 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4644 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4645 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4646 a9fa2909 2019-07-27 stsp goto done;
4647 a9fa2909 2019-07-27 stsp } else {
4648 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4649 a9fa2909 2019-07-27 stsp if (err)
4650 a9fa2909 2019-07-27 stsp goto done;
4651 a9fa2909 2019-07-27 stsp
4652 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4653 1233e6b6 2020-10-19 stsp if (err)
4654 a9fa2909 2019-07-27 stsp goto done;
4655 a9fa2909 2019-07-27 stsp
4656 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4657 1233e6b6 2020-10-19 stsp free(te_name);
4658 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4659 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4660 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4661 a9fa2909 2019-07-27 stsp goto done;
4662 a9fa2909 2019-07-27 stsp }
4663 a129376b 2019-03-28 stsp }
4664 a129376b 2019-03-28 stsp
4665 a129376b 2019-03-28 stsp switch (status) {
4666 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4667 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4668 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4669 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4670 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4671 33aa809d 2019-08-08 stsp if (err)
4672 33aa809d 2019-08-08 stsp goto done;
4673 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4674 33aa809d 2019-08-08 stsp break;
4675 33aa809d 2019-08-08 stsp }
4676 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4677 1f1abb7e 2019-08-08 stsp ie->path);
4678 1ee397ad 2019-07-12 stsp if (err)
4679 1ee397ad 2019-07-12 stsp goto done;
4680 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4681 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4682 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4683 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4684 10604dce 2021-09-24 thomas relpath) == -1) {
4685 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4686 10604dce 2021-09-24 thomas goto done;
4687 10604dce 2021-09-24 thomas }
4688 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4689 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4690 10604dce 2021-09-24 thomas ondisk_path);
4691 10604dce 2021-09-24 thomas break;
4692 10604dce 2021-09-24 thomas }
4693 10604dce 2021-09-24 thomas }
4694 a129376b 2019-03-28 stsp break;
4695 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4696 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4697 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4698 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4699 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4700 33aa809d 2019-08-08 stsp if (err)
4701 33aa809d 2019-08-08 stsp goto done;
4702 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4703 33aa809d 2019-08-08 stsp break;
4704 33aa809d 2019-08-08 stsp }
4705 33aa809d 2019-08-08 stsp /* fall through */
4706 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4707 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4708 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4709 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4710 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4711 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4712 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4713 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4714 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4715 24278f30 2019-08-03 stsp } else
4716 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4717 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4718 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4719 a129376b 2019-03-28 stsp if (err)
4720 65084dad 2019-08-08 stsp goto done;
4721 65084dad 2019-08-08 stsp
4722 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4723 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4724 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4725 a129376b 2019-03-28 stsp goto done;
4726 65084dad 2019-08-08 stsp }
4727 65084dad 2019-08-08 stsp
4728 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4729 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4730 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4731 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4732 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4733 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4734 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4735 33aa809d 2019-08-08 stsp break;
4736 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4737 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4738 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4739 369fd7e5 2020-07-23 stsp path_content);
4740 369fd7e5 2020-07-23 stsp break;
4741 369fd7e5 2020-07-23 stsp }
4742 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4743 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4744 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4745 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4746 369fd7e5 2020-07-23 stsp } else {
4747 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4748 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4749 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4750 369fd7e5 2020-07-23 stsp goto done;
4751 369fd7e5 2020-07-23 stsp }
4752 33aa809d 2019-08-08 stsp }
4753 33aa809d 2019-08-08 stsp } else {
4754 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4755 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4756 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4757 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4758 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4759 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4760 2e1fa222 2020-07-23 stsp } else {
4761 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4762 2e1fa222 2020-07-23 stsp ie->path,
4763 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4764 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4765 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4766 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4767 2e1fa222 2020-07-23 stsp }
4768 a129376b 2019-03-28 stsp if (err)
4769 a129376b 2019-03-28 stsp goto done;
4770 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4771 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4772 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4773 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4774 437adc9d 2020-12-10 yzhong blob->id.sha1,
4775 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4776 2e1fa222 2020-07-23 stsp if (err)
4777 2e1fa222 2020-07-23 stsp goto done;
4778 2e1fa222 2020-07-23 stsp }
4779 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4780 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4781 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4782 33aa809d 2019-08-08 stsp }
4783 a129376b 2019-03-28 stsp }
4784 a129376b 2019-03-28 stsp break;
4785 e20a8b6f 2019-06-04 stsp }
4786 a129376b 2019-03-28 stsp default:
4787 1f1abb7e 2019-08-08 stsp break;
4788 a129376b 2019-03-28 stsp }
4789 a129376b 2019-03-28 stsp done:
4790 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4791 33aa809d 2019-08-08 stsp free(path_content);
4792 e20a8b6f 2019-06-04 stsp free(parent_path);
4793 a129376b 2019-03-28 stsp free(tree_path);
4794 a129376b 2019-03-28 stsp if (blob)
4795 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4796 a129376b 2019-03-28 stsp if (tree)
4797 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4798 a129376b 2019-03-28 stsp free(tree_id);
4799 945f9229 2022-04-16 thomas if (base_commit)
4800 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4801 e20a8b6f 2019-06-04 stsp return err;
4802 e20a8b6f 2019-06-04 stsp }
4803 e20a8b6f 2019-06-04 stsp
4804 e20a8b6f 2019-06-04 stsp const struct got_error *
4805 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4806 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4807 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4808 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4809 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4810 e20a8b6f 2019-06-04 stsp {
4811 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4812 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4813 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4814 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4815 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4816 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4817 e20a8b6f 2019-06-04 stsp
4818 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4819 e20a8b6f 2019-06-04 stsp if (err)
4820 e20a8b6f 2019-06-04 stsp return err;
4821 e20a8b6f 2019-06-04 stsp
4822 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4823 e20a8b6f 2019-06-04 stsp if (err)
4824 e20a8b6f 2019-06-04 stsp goto done;
4825 e20a8b6f 2019-06-04 stsp
4826 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4827 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4828 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4829 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4830 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4831 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4832 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4833 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4834 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4835 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4836 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4837 e20a8b6f 2019-06-04 stsp if (err)
4838 af12c6b9 2019-06-04 stsp break;
4839 e20a8b6f 2019-06-04 stsp }
4840 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4841 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4842 e20a8b6f 2019-06-04 stsp err = sync_err;
4843 af12c6b9 2019-06-04 stsp done:
4844 fb399478 2019-07-12 stsp free(fileindex_path);
4845 a129376b 2019-03-28 stsp if (fileindex)
4846 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4847 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4848 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4849 a129376b 2019-03-28 stsp err = unlockerr;
4850 c4296144 2019-05-09 stsp return err;
4851 c4296144 2019-05-09 stsp }
4852 c4296144 2019-05-09 stsp
4853 cf066bf8 2019-05-09 stsp static void
4854 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4855 cf066bf8 2019-05-09 stsp {
4856 24519714 2019-05-09 stsp free(ct->path);
4857 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4858 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4859 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4860 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4861 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4862 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4863 cf066bf8 2019-05-09 stsp free(ct);
4864 cf066bf8 2019-05-09 stsp }
4865 24519714 2019-05-09 stsp
4866 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4867 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4868 24519714 2019-05-09 stsp struct got_repository *repo;
4869 24519714 2019-05-09 stsp struct got_worktree *worktree;
4870 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4871 5f8a88c6 2019-08-03 stsp int have_staged_files;
4872 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4873 24519714 2019-05-09 stsp };
4874 cf066bf8 2019-05-09 stsp
4875 c4296144 2019-05-09 stsp static const struct got_error *
4876 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4877 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4878 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4879 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4880 c4296144 2019-05-09 stsp {
4881 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4882 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4883 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4884 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4885 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4886 768aea60 2019-05-09 stsp struct stat sb;
4887 c4296144 2019-05-09 stsp
4888 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4889 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4890 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4891 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4892 5f8a88c6 2019-08-03 stsp return NULL;
4893 5f8a88c6 2019-08-03 stsp } else {
4894 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4895 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4896 c4296144 2019-05-09 stsp
4897 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4898 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4899 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4900 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4901 5f8a88c6 2019-08-03 stsp return NULL;
4902 5f8a88c6 2019-08-03 stsp }
4903 0b5cc0d6 2019-05-09 stsp
4904 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4905 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4906 036813ee 2019-05-09 stsp goto done;
4907 036813ee 2019-05-09 stsp }
4908 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4909 036813ee 2019-05-09 stsp parent_path = strdup("");
4910 69960a46 2019-05-09 stsp if (parent_path == NULL)
4911 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4912 69960a46 2019-05-09 stsp } else {
4913 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4914 69960a46 2019-05-09 stsp if (err)
4915 69960a46 2019-05-09 stsp return err;
4916 69960a46 2019-05-09 stsp }
4917 c4296144 2019-05-09 stsp
4918 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4919 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4920 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4921 c4296144 2019-05-09 stsp goto done;
4922 768aea60 2019-05-09 stsp }
4923 768aea60 2019-05-09 stsp
4924 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4925 768aea60 2019-05-09 stsp relpath) == -1) {
4926 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4927 768aea60 2019-05-09 stsp goto done;
4928 768aea60 2019-05-09 stsp }
4929 0aeb8099 2020-07-23 stsp
4930 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4931 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4932 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4933 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4934 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4935 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4936 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4937 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4938 0aeb8099 2020-07-23 stsp break;
4939 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4940 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4941 0aeb8099 2020-07-23 stsp break;
4942 0aeb8099 2020-07-23 stsp default:
4943 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4944 0aeb8099 2020-07-23 stsp goto done;
4945 0aeb8099 2020-07-23 stsp }
4946 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4947 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4948 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4949 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4950 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4951 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4952 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4953 12463d8b 2019-12-13 stsp ct->ondisk_path);
4954 12463d8b 2019-12-13 stsp goto done;
4955 12463d8b 2019-12-13 stsp }
4956 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4957 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4958 768aea60 2019-05-09 stsp goto done;
4959 768aea60 2019-05-09 stsp }
4960 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4961 c4296144 2019-05-09 stsp }
4962 c4296144 2019-05-09 stsp
4963 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4964 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4965 44d03001 2019-05-09 stsp relpath) == -1) {
4966 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4967 44d03001 2019-05-09 stsp goto done;
4968 44d03001 2019-05-09 stsp }
4969 44d03001 2019-05-09 stsp
4970 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4971 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4972 35213c7c 2020-07-23 stsp int is_bad_symlink;
4973 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4974 35213c7c 2020-07-23 stsp ssize_t target_len;
4975 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4976 35213c7c 2020-07-23 stsp sizeof(target_path));
4977 35213c7c 2020-07-23 stsp if (target_len == -1) {
4978 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4979 35213c7c 2020-07-23 stsp ct->ondisk_path);
4980 35213c7c 2020-07-23 stsp goto done;
4981 35213c7c 2020-07-23 stsp }
4982 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4983 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4984 35213c7c 2020-07-23 stsp if (err)
4985 35213c7c 2020-07-23 stsp goto done;
4986 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4987 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4988 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4989 35213c7c 2020-07-23 stsp goto done;
4990 35213c7c 2020-07-23 stsp }
4991 35213c7c 2020-07-23 stsp }
4992 35213c7c 2020-07-23 stsp
4993 35213c7c 2020-07-23 stsp
4994 cf066bf8 2019-05-09 stsp ct->status = status;
4995 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4996 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4997 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4998 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4999 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5000 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5001 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5002 b416585c 2019-05-13 stsp goto done;
5003 b416585c 2019-05-13 stsp }
5004 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5005 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5006 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5007 f0b75401 2019-08-03 stsp goto done;
5008 f0b75401 2019-08-03 stsp }
5009 f0b75401 2019-08-03 stsp }
5010 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5011 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5012 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5013 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5014 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5015 036813ee 2019-05-09 stsp goto done;
5016 036813ee 2019-05-09 stsp }
5017 ca2503ea 2019-05-09 stsp }
5018 24519714 2019-05-09 stsp ct->path = strdup(path);
5019 24519714 2019-05-09 stsp if (ct->path == NULL) {
5020 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5021 24519714 2019-05-09 stsp goto done;
5022 24519714 2019-05-09 stsp }
5023 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5024 c4296144 2019-05-09 stsp done:
5025 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5026 ed175427 2019-05-09 stsp free_commitable(ct);
5027 24519714 2019-05-09 stsp free(parent_path);
5028 036813ee 2019-05-09 stsp free(path);
5029 a129376b 2019-03-28 stsp return err;
5030 a129376b 2019-03-28 stsp }
5031 c4296144 2019-05-09 stsp
5032 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5033 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5034 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5035 036813ee 2019-05-09 stsp struct got_repository *);
5036 ed175427 2019-05-09 stsp
5037 ed175427 2019-05-09 stsp static const struct got_error *
5038 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5039 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5040 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5041 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5042 afa376bf 2019-05-09 stsp struct got_repository *repo)
5043 ed175427 2019-05-09 stsp {
5044 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5045 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5046 036813ee 2019-05-09 stsp char *subpath;
5047 ed175427 2019-05-09 stsp
5048 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5049 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5050 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5051 ed175427 2019-05-09 stsp
5052 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5053 ed175427 2019-05-09 stsp if (err)
5054 ed175427 2019-05-09 stsp return err;
5055 ed175427 2019-05-09 stsp
5056 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5057 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5058 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5059 036813ee 2019-05-09 stsp free(subpath);
5060 036813ee 2019-05-09 stsp return err;
5061 036813ee 2019-05-09 stsp }
5062 ed175427 2019-05-09 stsp
5063 036813ee 2019-05-09 stsp static const struct got_error *
5064 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5065 036813ee 2019-05-09 stsp {
5066 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5067 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5068 ed175427 2019-05-09 stsp
5069 036813ee 2019-05-09 stsp *match = 0;
5070 ed175427 2019-05-09 stsp
5071 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5072 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5073 0f63689d 2019-05-10 stsp return NULL;
5074 036813ee 2019-05-09 stsp }
5075 0b5cc0d6 2019-05-09 stsp
5076 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5077 0f63689d 2019-05-10 stsp if (err)
5078 0f63689d 2019-05-10 stsp return err;
5079 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5080 036813ee 2019-05-09 stsp free(ct_parent_path);
5081 036813ee 2019-05-09 stsp return err;
5082 036813ee 2019-05-09 stsp }
5083 0b5cc0d6 2019-05-09 stsp
5084 768aea60 2019-05-09 stsp static mode_t
5085 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5086 768aea60 2019-05-09 stsp {
5087 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5088 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5089 3d9a4ec4 2020-07-23 stsp
5090 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5091 768aea60 2019-05-09 stsp }
5092 768aea60 2019-05-09 stsp
5093 036813ee 2019-05-09 stsp static const struct got_error *
5094 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5095 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5096 036813ee 2019-05-09 stsp {
5097 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5098 ca2503ea 2019-05-09 stsp
5099 036813ee 2019-05-09 stsp *new_te = NULL;
5100 0b5cc0d6 2019-05-09 stsp
5101 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5102 036813ee 2019-05-09 stsp if (err)
5103 036813ee 2019-05-09 stsp goto done;
5104 0b5cc0d6 2019-05-09 stsp
5105 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5106 036813ee 2019-05-09 stsp
5107 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5108 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5109 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5110 5f8a88c6 2019-08-03 stsp else
5111 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5112 036813ee 2019-05-09 stsp done:
5113 036813ee 2019-05-09 stsp if (err && *new_te) {
5114 56e0773d 2019-11-28 stsp free(*new_te);
5115 036813ee 2019-05-09 stsp *new_te = NULL;
5116 036813ee 2019-05-09 stsp }
5117 036813ee 2019-05-09 stsp return err;
5118 036813ee 2019-05-09 stsp }
5119 036813ee 2019-05-09 stsp
5120 036813ee 2019-05-09 stsp static const struct got_error *
5121 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5122 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5123 036813ee 2019-05-09 stsp {
5124 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5125 102b254e 2020-10-19 stsp char *ct_name = NULL;
5126 036813ee 2019-05-09 stsp
5127 036813ee 2019-05-09 stsp *new_te = NULL;
5128 036813ee 2019-05-09 stsp
5129 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5130 036813ee 2019-05-09 stsp if (*new_te == NULL)
5131 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5132 036813ee 2019-05-09 stsp
5133 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5134 102b254e 2020-10-19 stsp if (err)
5135 036813ee 2019-05-09 stsp goto done;
5136 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5137 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5138 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5139 036813ee 2019-05-09 stsp goto done;
5140 036813ee 2019-05-09 stsp }
5141 036813ee 2019-05-09 stsp
5142 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5143 036813ee 2019-05-09 stsp
5144 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5145 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5146 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5147 5f8a88c6 2019-08-03 stsp else
5148 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5149 036813ee 2019-05-09 stsp done:
5150 102b254e 2020-10-19 stsp free(ct_name);
5151 036813ee 2019-05-09 stsp if (err && *new_te) {
5152 56e0773d 2019-11-28 stsp free(*new_te);
5153 036813ee 2019-05-09 stsp *new_te = NULL;
5154 036813ee 2019-05-09 stsp }
5155 036813ee 2019-05-09 stsp return err;
5156 036813ee 2019-05-09 stsp }
5157 036813ee 2019-05-09 stsp
5158 036813ee 2019-05-09 stsp static const struct got_error *
5159 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5160 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5161 036813ee 2019-05-09 stsp {
5162 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5163 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5164 036813ee 2019-05-09 stsp
5165 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5166 036813ee 2019-05-09 stsp if (err)
5167 036813ee 2019-05-09 stsp return err;
5168 036813ee 2019-05-09 stsp if (new_pe == NULL)
5169 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5170 036813ee 2019-05-09 stsp return NULL;
5171 afa376bf 2019-05-09 stsp }
5172 afa376bf 2019-05-09 stsp
5173 afa376bf 2019-05-09 stsp static const struct got_error *
5174 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5175 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5176 afa376bf 2019-05-09 stsp {
5177 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5178 5f8a88c6 2019-08-03 stsp unsigned char status;
5179 ae1e948a 2021-09-28 thomas
5180 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5181 ae1e948a 2021-09-28 thomas return NULL;
5182 5f8a88c6 2019-08-03 stsp
5183 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5184 afa376bf 2019-05-09 stsp ct_path++;
5185 5f8a88c6 2019-08-03 stsp
5186 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5187 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5188 5f8a88c6 2019-08-03 stsp else
5189 5f8a88c6 2019-08-03 stsp status = ct->status;
5190 5f8a88c6 2019-08-03 stsp
5191 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5192 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5193 036813ee 2019-05-09 stsp }
5194 036813ee 2019-05-09 stsp
5195 036813ee 2019-05-09 stsp static const struct got_error *
5196 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5197 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5198 44d03001 2019-05-09 stsp {
5199 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5200 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5201 44d03001 2019-05-09 stsp char *te_path;
5202 44d03001 2019-05-09 stsp
5203 44d03001 2019-05-09 stsp *modified = 0;
5204 44d03001 2019-05-09 stsp
5205 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5206 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5207 44d03001 2019-05-09 stsp te->name) == -1)
5208 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5209 44d03001 2019-05-09 stsp
5210 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5211 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5212 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5213 44d03001 2019-05-09 stsp strlen(te_path));
5214 62d463ca 2020-10-20 naddy if (*modified)
5215 44d03001 2019-05-09 stsp break;
5216 44d03001 2019-05-09 stsp }
5217 44d03001 2019-05-09 stsp
5218 44d03001 2019-05-09 stsp free(te_path);
5219 44d03001 2019-05-09 stsp return err;
5220 44d03001 2019-05-09 stsp }
5221 44d03001 2019-05-09 stsp
5222 44d03001 2019-05-09 stsp static const struct got_error *
5223 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5224 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5225 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5226 036813ee 2019-05-09 stsp {
5227 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5228 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5229 036813ee 2019-05-09 stsp
5230 036813ee 2019-05-09 stsp *ctp = NULL;
5231 036813ee 2019-05-09 stsp
5232 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5233 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5234 036813ee 2019-05-09 stsp char *ct_name = NULL;
5235 036813ee 2019-05-09 stsp int path_matches;
5236 036813ee 2019-05-09 stsp
5237 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5238 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5239 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5240 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5241 5f8a88c6 2019-08-03 stsp continue;
5242 5f8a88c6 2019-08-03 stsp } else {
5243 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5244 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5245 5f8a88c6 2019-08-03 stsp continue;
5246 5f8a88c6 2019-08-03 stsp }
5247 036813ee 2019-05-09 stsp
5248 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5249 036813ee 2019-05-09 stsp continue;
5250 036813ee 2019-05-09 stsp
5251 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5252 62d463ca 2020-10-20 naddy if (err)
5253 036813ee 2019-05-09 stsp return err;
5254 036813ee 2019-05-09 stsp if (!path_matches)
5255 036813ee 2019-05-09 stsp continue;
5256 036813ee 2019-05-09 stsp
5257 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5258 d34b633e 2020-10-19 stsp if (err)
5259 d34b633e 2020-10-19 stsp return err;
5260 d34b633e 2020-10-19 stsp
5261 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5262 d34b633e 2020-10-19 stsp free(ct_name);
5263 036813ee 2019-05-09 stsp continue;
5264 d34b633e 2020-10-19 stsp }
5265 d34b633e 2020-10-19 stsp free(ct_name);
5266 036813ee 2019-05-09 stsp
5267 036813ee 2019-05-09 stsp *ctp = ct;
5268 036813ee 2019-05-09 stsp break;
5269 036813ee 2019-05-09 stsp }
5270 2b496619 2019-07-10 stsp
5271 2b496619 2019-07-10 stsp return err;
5272 2b496619 2019-07-10 stsp }
5273 2b496619 2019-07-10 stsp
5274 2b496619 2019-07-10 stsp static const struct got_error *
5275 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5276 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5277 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5278 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5279 2b496619 2019-07-10 stsp struct got_repository *repo)
5280 2b496619 2019-07-10 stsp {
5281 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5282 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5283 2b496619 2019-07-10 stsp char *subtree_path;
5284 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5285 ba580f68 2020-03-22 stsp int nentries;
5286 2b496619 2019-07-10 stsp
5287 2b496619 2019-07-10 stsp *new_tep = NULL;
5288 2b496619 2019-07-10 stsp
5289 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5290 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5291 2b496619 2019-07-10 stsp child_path) == -1)
5292 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5293 036813ee 2019-05-09 stsp
5294 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5295 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5296 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5297 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5298 56e0773d 2019-11-28 stsp
5299 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5300 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5301 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5302 2b496619 2019-07-10 stsp goto done;
5303 2b496619 2019-07-10 stsp }
5304 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5305 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5306 2b496619 2019-07-10 stsp if (err) {
5307 56e0773d 2019-11-28 stsp free(new_te);
5308 2b496619 2019-07-10 stsp goto done;
5309 2b496619 2019-07-10 stsp }
5310 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5311 2b496619 2019-07-10 stsp done:
5312 56e0773d 2019-11-28 stsp free(id);
5313 2b496619 2019-07-10 stsp free(subtree_path);
5314 2b496619 2019-07-10 stsp if (err == NULL)
5315 2b496619 2019-07-10 stsp *new_tep = new_te;
5316 036813ee 2019-05-09 stsp return err;
5317 036813ee 2019-05-09 stsp }
5318 036813ee 2019-05-09 stsp
5319 036813ee 2019-05-09 stsp static const struct got_error *
5320 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5321 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5322 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5323 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5324 036813ee 2019-05-09 stsp struct got_repository *repo)
5325 036813ee 2019-05-09 stsp {
5326 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5327 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5328 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5329 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5330 036813ee 2019-05-09 stsp
5331 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5332 ba580f68 2020-03-22 stsp *nentries = 0;
5333 036813ee 2019-05-09 stsp
5334 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5335 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5336 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5337 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5338 036813ee 2019-05-09 stsp
5339 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5340 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5341 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5342 036813ee 2019-05-09 stsp continue;
5343 036813ee 2019-05-09 stsp
5344 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5345 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5346 036813ee 2019-05-09 stsp continue;
5347 036813ee 2019-05-09 stsp
5348 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5349 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5350 036813ee 2019-05-09 stsp if (err)
5351 036813ee 2019-05-09 stsp goto done;
5352 036813ee 2019-05-09 stsp
5353 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5354 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5355 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5356 036813ee 2019-05-09 stsp if (err)
5357 036813ee 2019-05-09 stsp goto done;
5358 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5359 afa376bf 2019-05-09 stsp if (err)
5360 afa376bf 2019-05-09 stsp goto done;
5361 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5362 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5363 2b496619 2019-07-10 stsp if (err)
5364 2f51b5b3 2019-05-09 stsp goto done;
5365 ba580f68 2020-03-22 stsp (*nentries)++;
5366 2b496619 2019-07-10 stsp } else {
5367 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5368 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5369 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5370 2b496619 2019-07-10 stsp == NULL) {
5371 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5372 2b496619 2019-07-10 stsp child_path, path_base_tree,
5373 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5374 2b496619 2019-07-10 stsp repo);
5375 2b496619 2019-07-10 stsp if (err)
5376 2b496619 2019-07-10 stsp goto done;
5377 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5378 2b496619 2019-07-10 stsp if (err)
5379 2b496619 2019-07-10 stsp goto done;
5380 ba580f68 2020-03-22 stsp (*nentries)++;
5381 9ba0479c 2019-05-10 stsp }
5382 ed175427 2019-05-09 stsp }
5383 2f51b5b3 2019-05-09 stsp }
5384 2f51b5b3 2019-05-09 stsp
5385 2f51b5b3 2019-05-09 stsp if (base_tree) {
5386 56e0773d 2019-11-28 stsp int i, nbase_entries;
5387 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5388 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5389 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5390 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5391 63c5ca5d 2019-08-24 stsp
5392 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5393 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5394 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5395 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5396 63c5ca5d 2019-08-24 stsp if (err)
5397 63c5ca5d 2019-08-24 stsp goto done;
5398 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5399 63c5ca5d 2019-08-24 stsp if (err)
5400 63c5ca5d 2019-08-24 stsp goto done;
5401 ba580f68 2020-03-22 stsp (*nentries)++;
5402 63c5ca5d 2019-08-24 stsp continue;
5403 63c5ca5d 2019-08-24 stsp }
5404 2f51b5b3 2019-05-09 stsp
5405 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5406 44d03001 2019-05-09 stsp int modified;
5407 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5408 036813ee 2019-05-09 stsp if (err)
5409 036813ee 2019-05-09 stsp goto done;
5410 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5411 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5412 2f51b5b3 2019-05-09 stsp if (err)
5413 2f51b5b3 2019-05-09 stsp goto done;
5414 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5415 44d03001 2019-05-09 stsp if (modified) {
5416 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5417 ba580f68 2020-03-22 stsp int nsubentries;
5418 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5419 ba580f68 2020-03-22 stsp &nsubentries, te,
5420 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5421 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5422 44d03001 2019-05-09 stsp if (err)
5423 44d03001 2019-05-09 stsp goto done;
5424 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5425 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5426 ba580f68 2020-03-22 stsp free(new_id);
5427 ba580f68 2020-03-22 stsp continue;
5428 ba580f68 2020-03-22 stsp }
5429 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5430 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5431 56e0773d 2019-11-28 stsp free(new_id);
5432 44d03001 2019-05-09 stsp }
5433 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5434 036813ee 2019-05-09 stsp if (err)
5435 036813ee 2019-05-09 stsp goto done;
5436 ba580f68 2020-03-22 stsp (*nentries)++;
5437 2f51b5b3 2019-05-09 stsp continue;
5438 036813ee 2019-05-09 stsp }
5439 2f51b5b3 2019-05-09 stsp
5440 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5441 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5442 f66c734c 2019-09-22 stsp if (err)
5443 f66c734c 2019-09-22 stsp goto done;
5444 2f51b5b3 2019-05-09 stsp if (ct) {
5445 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5446 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5447 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5448 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5449 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5450 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5451 2f51b5b3 2019-05-09 stsp if (err)
5452 2f51b5b3 2019-05-09 stsp goto done;
5453 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5454 2f51b5b3 2019-05-09 stsp if (err)
5455 2f51b5b3 2019-05-09 stsp goto done;
5456 ba580f68 2020-03-22 stsp (*nentries)++;
5457 2f51b5b3 2019-05-09 stsp }
5458 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5459 b416585c 2019-05-13 stsp status_arg);
5460 afa376bf 2019-05-09 stsp if (err)
5461 afa376bf 2019-05-09 stsp goto done;
5462 2f51b5b3 2019-05-09 stsp } else {
5463 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5464 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5465 2f51b5b3 2019-05-09 stsp if (err)
5466 2f51b5b3 2019-05-09 stsp goto done;
5467 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5468 2f51b5b3 2019-05-09 stsp if (err)
5469 2f51b5b3 2019-05-09 stsp goto done;
5470 ba580f68 2020-03-22 stsp (*nentries)++;
5471 2f51b5b3 2019-05-09 stsp }
5472 ca2503ea 2019-05-09 stsp }
5473 ed175427 2019-05-09 stsp }
5474 0b5cc0d6 2019-05-09 stsp
5475 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5476 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5477 ed175427 2019-05-09 stsp done:
5478 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5479 2e1fa222 2020-07-23 stsp return err;
5480 2e1fa222 2020-07-23 stsp }
5481 2e1fa222 2020-07-23 stsp
5482 2e1fa222 2020-07-23 stsp static const struct got_error *
5483 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5484 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5485 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5486 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5487 ebf99748 2019-05-09 stsp {
5488 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5489 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5490 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5491 ebf99748 2019-05-09 stsp
5492 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5493 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5494 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5495 ebf99748 2019-05-09 stsp
5496 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5497 437adc9d 2020-12-10 yzhong
5498 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5499 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5500 437adc9d 2020-12-10 yzhong if (err)
5501 437adc9d 2020-12-10 yzhong goto done;
5502 437adc9d 2020-12-10 yzhong
5503 ebf99748 2019-05-09 stsp if (ie) {
5504 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5505 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5506 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5507 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5508 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5509 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5510 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5511 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5512 437adc9d 2020-12-10 yzhong
5513 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5514 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5515 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5516 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5517 72fd46fa 2019-09-06 stsp !have_staged_files);
5518 ebf99748 2019-05-09 stsp } else
5519 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5520 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5521 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5522 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5523 72fd46fa 2019-09-06 stsp !have_staged_files);
5524 ebf99748 2019-05-09 stsp } else {
5525 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5526 ebf99748 2019-05-09 stsp if (err)
5527 437adc9d 2020-12-10 yzhong goto done;
5528 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5529 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5530 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5531 3969253a 2020-03-07 stsp if (err) {
5532 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5533 437adc9d 2020-12-10 yzhong goto done;
5534 3969253a 2020-03-07 stsp }
5535 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5536 3969253a 2020-03-07 stsp if (err) {
5537 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5538 437adc9d 2020-12-10 yzhong goto done;
5539 3969253a 2020-03-07 stsp }
5540 ebf99748 2019-05-09 stsp }
5541 437adc9d 2020-12-10 yzhong free(relpath);
5542 437adc9d 2020-12-10 yzhong relpath = NULL;
5543 ebf99748 2019-05-09 stsp }
5544 437adc9d 2020-12-10 yzhong done:
5545 437adc9d 2020-12-10 yzhong free(relpath);
5546 d56d26ce 2019-05-10 stsp return err;
5547 d56d26ce 2019-05-10 stsp }
5548 735ef5ac 2019-08-03 stsp
5549 d56d26ce 2019-05-10 stsp
5550 d56d26ce 2019-05-10 stsp static const struct got_error *
5551 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5552 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5553 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5554 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5555 735ef5ac 2019-08-03 stsp int ood_errcode)
5556 d56d26ce 2019-05-10 stsp {
5557 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5558 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5559 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5560 1a36436d 2019-06-10 stsp
5561 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5562 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5563 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5564 1a36436d 2019-06-10 stsp return NULL;
5565 9bead371 2019-07-28 stsp /*
5566 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5567 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5568 9bead371 2019-07-28 stsp */
5569 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5570 945f9229 2022-04-16 thomas if (err)
5571 945f9229 2022-04-16 thomas goto done;
5572 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5573 9bead371 2019-07-28 stsp if (err) {
5574 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5575 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5576 1a36436d 2019-06-10 stsp goto done;
5577 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5578 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5579 1a36436d 2019-06-10 stsp } else {
5580 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5581 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5582 945f9229 2022-04-16 thomas if (err)
5583 945f9229 2022-04-16 thomas goto done;
5584 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5585 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5586 1a36436d 2019-06-10 stsp goto done;
5587 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5588 1a36436d 2019-06-10 stsp }
5589 1a36436d 2019-06-10 stsp done:
5590 1a36436d 2019-06-10 stsp free(id);
5591 945f9229 2022-04-16 thomas if (commit)
5592 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5593 1a36436d 2019-06-10 stsp return err;
5594 ebf99748 2019-05-09 stsp }
5595 ebf99748 2019-05-09 stsp
5596 ef20f542 2022-06-26 thomas static const struct got_error *
5597 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5598 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5599 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5600 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5601 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5602 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5603 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5604 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5605 afa376bf 2019-05-09 stsp struct got_repository *repo)
5606 c4296144 2019-05-09 stsp {
5607 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5608 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5609 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5610 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5611 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5612 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5613 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5614 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5615 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5616 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5617 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5618 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5619 c4296144 2019-05-09 stsp
5620 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5621 c4296144 2019-05-09 stsp
5622 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5623 675c7539 2019-05-09 stsp
5624 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5625 588edf97 2019-05-10 stsp if (err)
5626 588edf97 2019-05-10 stsp goto done;
5627 de18fc63 2019-05-09 stsp
5628 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5629 588edf97 2019-05-10 stsp if (err)
5630 588edf97 2019-05-10 stsp goto done;
5631 588edf97 2019-05-10 stsp
5632 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5633 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5634 33ad4cbe 2019-05-12 jcs if (err)
5635 33ad4cbe 2019-05-12 jcs goto done;
5636 33ad4cbe 2019-05-12 jcs }
5637 c4296144 2019-05-09 stsp
5638 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5639 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5640 33ad4cbe 2019-05-12 jcs goto done;
5641 33ad4cbe 2019-05-12 jcs }
5642 33ad4cbe 2019-05-12 jcs
5643 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5644 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5645 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5646 cf066bf8 2019-05-09 stsp char *ondisk_path;
5647 cf066bf8 2019-05-09 stsp
5648 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5649 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5650 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5651 5f8a88c6 2019-08-03 stsp continue;
5652 5f8a88c6 2019-08-03 stsp
5653 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5654 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5655 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5656 cf066bf8 2019-05-09 stsp continue;
5657 cf066bf8 2019-05-09 stsp
5658 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5659 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5660 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5661 cf066bf8 2019-05-09 stsp goto done;
5662 cf066bf8 2019-05-09 stsp }
5663 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5664 2e1fa222 2020-07-23 stsp free(ondisk_path);
5665 2e1fa222 2020-07-23 stsp if (err)
5666 cf066bf8 2019-05-09 stsp goto done;
5667 cf066bf8 2019-05-09 stsp }
5668 036813ee 2019-05-09 stsp
5669 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5670 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5671 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5672 036813ee 2019-05-09 stsp if (err)
5673 036813ee 2019-05-09 stsp goto done;
5674 036813ee 2019-05-09 stsp
5675 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5676 de18fc63 2019-05-09 stsp if (err)
5677 de18fc63 2019-05-09 stsp goto done;
5678 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5679 10604dce 2021-09-24 thomas nparents++;
5680 10604dce 2021-09-24 thomas if (parent_id2) {
5681 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5682 10604dce 2021-09-24 thomas if (err)
5683 10604dce 2021-09-24 thomas goto done;
5684 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5685 10604dce 2021-09-24 thomas nparents++;
5686 10604dce 2021-09-24 thomas }
5687 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5688 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5689 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5690 33ad4cbe 2019-05-12 jcs free(logmsg);
5691 9d40349a 2019-05-09 stsp if (err)
5692 9d40349a 2019-05-09 stsp goto done;
5693 9d40349a 2019-05-09 stsp
5694 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5695 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5696 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5697 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5698 09f5bd90 2019-05-09 stsp goto done;
5699 09f5bd90 2019-05-09 stsp }
5700 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5701 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5702 09f5bd90 2019-05-09 stsp if (err)
5703 09f5bd90 2019-05-09 stsp goto done;
5704 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5705 ebf99748 2019-05-09 stsp if (err)
5706 ebf99748 2019-05-09 stsp goto done;
5707 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5708 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5709 09f5bd90 2019-05-09 stsp goto done;
5710 09f5bd90 2019-05-09 stsp }
5711 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5712 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5713 f2c16586 2019-05-09 stsp if (err)
5714 f2c16586 2019-05-09 stsp goto done;
5715 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5716 f2c16586 2019-05-09 stsp if (err)
5717 f2c16586 2019-05-09 stsp goto done;
5718 f2c16586 2019-05-09 stsp
5719 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5720 09f5bd90 2019-05-09 stsp if (err)
5721 09f5bd90 2019-05-09 stsp goto done;
5722 09f5bd90 2019-05-09 stsp
5723 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5724 ebf99748 2019-05-09 stsp if (err)
5725 ebf99748 2019-05-09 stsp goto done;
5726 c4296144 2019-05-09 stsp done:
5727 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5728 588edf97 2019-05-10 stsp if (head_tree)
5729 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5730 588edf97 2019-05-10 stsp if (head_commit)
5731 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5732 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5733 2f17228e 2019-05-12 stsp if (head_ref2) {
5734 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5735 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5736 2f17228e 2019-05-12 stsp err = unlockerr;
5737 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5738 39cd0ff6 2019-07-12 stsp }
5739 39cd0ff6 2019-07-12 stsp return err;
5740 39cd0ff6 2019-07-12 stsp }
5741 5c1e53bc 2019-07-28 stsp
5742 5c1e53bc 2019-07-28 stsp static const struct got_error *
5743 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5744 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5745 5c1e53bc 2019-07-28 stsp {
5746 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5747 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5748 39cd0ff6 2019-07-12 stsp
5749 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5750 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5751 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5752 5c1e53bc 2019-07-28 stsp
5753 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5754 5c1e53bc 2019-07-28 stsp ct_path++;
5755 5c1e53bc 2019-07-28 stsp
5756 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5757 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5758 5c1e53bc 2019-07-28 stsp break;
5759 5c1e53bc 2019-07-28 stsp }
5760 5c1e53bc 2019-07-28 stsp
5761 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5762 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5763 5c1e53bc 2019-07-28 stsp
5764 5c1e53bc 2019-07-28 stsp return NULL;
5765 5c1e53bc 2019-07-28 stsp }
5766 5c1e53bc 2019-07-28 stsp
5767 f0b75401 2019-08-03 stsp static const struct got_error *
5768 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5769 f0b75401 2019-08-03 stsp {
5770 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5771 f0b75401 2019-08-03 stsp
5772 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5773 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5774 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5775 f0b75401 2019-08-03 stsp }
5776 f0b75401 2019-08-03 stsp
5777 f0b75401 2019-08-03 stsp return NULL;
5778 f0b75401 2019-08-03 stsp }
5779 f0b75401 2019-08-03 stsp
5780 5f8a88c6 2019-08-03 stsp static const struct got_error *
5781 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5782 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5783 5f8a88c6 2019-08-03 stsp {
5784 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5785 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5786 5f8a88c6 2019-08-03 stsp
5787 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5788 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5789 5f8a88c6 2019-08-03 stsp continue;
5790 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5791 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5792 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5793 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5794 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5795 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5796 5f8a88c6 2019-08-03 stsp }
5797 5f8a88c6 2019-08-03 stsp
5798 5f8a88c6 2019-08-03 stsp return NULL;
5799 5f8a88c6 2019-08-03 stsp }
5800 5f8a88c6 2019-08-03 stsp
5801 39cd0ff6 2019-07-12 stsp const struct got_error *
5802 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5803 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5804 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5805 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5806 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5807 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5808 39cd0ff6 2019-07-12 stsp {
5809 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5810 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5811 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5812 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5813 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5814 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5815 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5816 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5817 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5818 39cd0ff6 2019-07-12 stsp
5819 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5820 39cd0ff6 2019-07-12 stsp
5821 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5822 39cd0ff6 2019-07-12 stsp
5823 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5824 39cd0ff6 2019-07-12 stsp if (err)
5825 39cd0ff6 2019-07-12 stsp goto done;
5826 39cd0ff6 2019-07-12 stsp
5827 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5828 39cd0ff6 2019-07-12 stsp if (err)
5829 39cd0ff6 2019-07-12 stsp goto done;
5830 39cd0ff6 2019-07-12 stsp
5831 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5832 39cd0ff6 2019-07-12 stsp if (err)
5833 39cd0ff6 2019-07-12 stsp goto done;
5834 39cd0ff6 2019-07-12 stsp
5835 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5836 39cd0ff6 2019-07-12 stsp if (err)
5837 39cd0ff6 2019-07-12 stsp goto done;
5838 39cd0ff6 2019-07-12 stsp
5839 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5840 5f8a88c6 2019-08-03 stsp &have_staged_files);
5841 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5842 5f8a88c6 2019-08-03 stsp goto done;
5843 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5844 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5845 5f8a88c6 2019-08-03 stsp if (err)
5846 5f8a88c6 2019-08-03 stsp goto done;
5847 5f8a88c6 2019-08-03 stsp }
5848 5f8a88c6 2019-08-03 stsp
5849 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5850 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5851 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5852 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5853 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5854 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5855 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5856 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5857 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5858 5c1e53bc 2019-07-28 stsp if (err)
5859 5c1e53bc 2019-07-28 stsp goto done;
5860 5c1e53bc 2019-07-28 stsp }
5861 39cd0ff6 2019-07-12 stsp
5862 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5863 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5864 39cd0ff6 2019-07-12 stsp goto done;
5865 5c1e53bc 2019-07-28 stsp }
5866 5c1e53bc 2019-07-28 stsp
5867 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5868 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5869 5c1e53bc 2019-07-28 stsp if (err)
5870 5c1e53bc 2019-07-28 stsp goto done;
5871 39cd0ff6 2019-07-12 stsp }
5872 f0b75401 2019-08-03 stsp
5873 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5874 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5875 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5876 f0b75401 2019-08-03 stsp
5877 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5878 f0b75401 2019-08-03 stsp ct_path++;
5879 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5880 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5881 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5882 b50cabdf 2019-07-12 stsp if (err)
5883 b50cabdf 2019-07-12 stsp goto done;
5884 f0b75401 2019-08-03 stsp
5885 b50cabdf 2019-07-12 stsp }
5886 b50cabdf 2019-07-12 stsp
5887 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5888 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5889 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5890 39cd0ff6 2019-07-12 stsp if (err)
5891 39cd0ff6 2019-07-12 stsp goto done;
5892 39cd0ff6 2019-07-12 stsp
5893 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5894 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5895 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5896 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5897 39cd0ff6 2019-07-12 stsp err = sync_err;
5898 39cd0ff6 2019-07-12 stsp done:
5899 39cd0ff6 2019-07-12 stsp if (fileindex)
5900 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5901 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5902 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5903 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5904 39cd0ff6 2019-07-12 stsp err = unlockerr;
5905 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5906 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5907 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5908 39cd0ff6 2019-07-12 stsp }
5909 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5910 c4296144 2019-05-09 stsp return err;
5911 8656d6c4 2019-05-20 stsp }
5912 8656d6c4 2019-05-20 stsp
5913 8656d6c4 2019-05-20 stsp const char *
5914 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5915 8656d6c4 2019-05-20 stsp {
5916 8656d6c4 2019-05-20 stsp return ct->path;
5917 8656d6c4 2019-05-20 stsp }
5918 8656d6c4 2019-05-20 stsp
5919 8656d6c4 2019-05-20 stsp unsigned int
5920 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5921 8656d6c4 2019-05-20 stsp {
5922 8656d6c4 2019-05-20 stsp return ct->status;
5923 818c7501 2019-07-11 stsp }
5924 818c7501 2019-07-11 stsp
5925 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5926 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5927 818c7501 2019-07-11 stsp struct got_repository *repo;
5928 818c7501 2019-07-11 stsp };
5929 818c7501 2019-07-11 stsp
5930 818c7501 2019-07-11 stsp static const struct got_error *
5931 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5932 818c7501 2019-07-11 stsp {
5933 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5934 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5935 818c7501 2019-07-11 stsp unsigned char status;
5936 818c7501 2019-07-11 stsp struct stat sb;
5937 818c7501 2019-07-11 stsp char *ondisk_path;
5938 818c7501 2019-07-11 stsp
5939 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5940 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5941 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5942 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5943 818c7501 2019-07-11 stsp
5944 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5945 818c7501 2019-07-11 stsp == -1)
5946 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5947 818c7501 2019-07-11 stsp
5948 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5949 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5950 818c7501 2019-07-11 stsp free(ondisk_path);
5951 818c7501 2019-07-11 stsp if (err)
5952 818c7501 2019-07-11 stsp return err;
5953 818c7501 2019-07-11 stsp
5954 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5955 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5956 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5957 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5958 818c7501 2019-07-11 stsp
5959 818c7501 2019-07-11 stsp return NULL;
5960 818c7501 2019-07-11 stsp }
5961 818c7501 2019-07-11 stsp
5962 818c7501 2019-07-11 stsp const struct got_error *
5963 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5964 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5965 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5966 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5967 818c7501 2019-07-11 stsp {
5968 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5969 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5970 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5971 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5972 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5973 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5974 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5975 818c7501 2019-07-11 stsp
5976 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5977 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5978 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5979 818c7501 2019-07-11 stsp
5980 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5981 818c7501 2019-07-11 stsp if (err)
5982 818c7501 2019-07-11 stsp return err;
5983 818c7501 2019-07-11 stsp
5984 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5985 818c7501 2019-07-11 stsp if (err)
5986 818c7501 2019-07-11 stsp goto done;
5987 818c7501 2019-07-11 stsp
5988 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5989 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5990 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5991 818c7501 2019-07-11 stsp &ok_arg);
5992 818c7501 2019-07-11 stsp if (err)
5993 818c7501 2019-07-11 stsp goto done;
5994 818c7501 2019-07-11 stsp
5995 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5996 818c7501 2019-07-11 stsp if (err)
5997 818c7501 2019-07-11 stsp goto done;
5998 818c7501 2019-07-11 stsp
5999 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6000 818c7501 2019-07-11 stsp if (err)
6001 818c7501 2019-07-11 stsp goto done;
6002 818c7501 2019-07-11 stsp
6003 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6004 818c7501 2019-07-11 stsp if (err)
6005 818c7501 2019-07-11 stsp goto done;
6006 818c7501 2019-07-11 stsp
6007 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6008 818c7501 2019-07-11 stsp 0);
6009 e51d7b55 2020-01-04 stsp if (err)
6010 e51d7b55 2020-01-04 stsp goto done;
6011 e51d7b55 2020-01-04 stsp
6012 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6013 818c7501 2019-07-11 stsp if (err)
6014 818c7501 2019-07-11 stsp goto done;
6015 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6016 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6017 e51d7b55 2020-01-04 stsp goto done;
6018 e51d7b55 2020-01-04 stsp }
6019 818c7501 2019-07-11 stsp
6020 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6021 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6022 818c7501 2019-07-11 stsp if (err)
6023 818c7501 2019-07-11 stsp goto done;
6024 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6025 818c7501 2019-07-11 stsp if (err)
6026 818c7501 2019-07-11 stsp goto done;
6027 818c7501 2019-07-11 stsp
6028 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6029 818c7501 2019-07-11 stsp
6030 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6031 818c7501 2019-07-11 stsp if (err)
6032 818c7501 2019-07-11 stsp goto done;
6033 818c7501 2019-07-11 stsp
6034 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6035 818c7501 2019-07-11 stsp if (err)
6036 818c7501 2019-07-11 stsp goto done;
6037 818c7501 2019-07-11 stsp
6038 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6039 818c7501 2019-07-11 stsp worktree->base_commit_id);
6040 818c7501 2019-07-11 stsp if (err)
6041 818c7501 2019-07-11 stsp goto done;
6042 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6043 818c7501 2019-07-11 stsp if (err)
6044 818c7501 2019-07-11 stsp goto done;
6045 818c7501 2019-07-11 stsp
6046 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6047 818c7501 2019-07-11 stsp if (err)
6048 818c7501 2019-07-11 stsp goto done;
6049 818c7501 2019-07-11 stsp done:
6050 818c7501 2019-07-11 stsp free(fileindex_path);
6051 818c7501 2019-07-11 stsp free(tmp_branch_name);
6052 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6053 818c7501 2019-07-11 stsp free(branch_ref_name);
6054 818c7501 2019-07-11 stsp if (branch_ref)
6055 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6056 818c7501 2019-07-11 stsp if (wt_branch)
6057 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6058 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6059 818c7501 2019-07-11 stsp if (err) {
6060 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6061 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6062 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6063 818c7501 2019-07-11 stsp }
6064 818c7501 2019-07-11 stsp if (*tmp_branch) {
6065 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6066 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6067 818c7501 2019-07-11 stsp }
6068 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6069 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6070 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6071 3e3a69f1 2019-07-25 stsp }
6072 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6073 818c7501 2019-07-11 stsp }
6074 818c7501 2019-07-11 stsp return err;
6075 818c7501 2019-07-11 stsp }
6076 818c7501 2019-07-11 stsp
6077 818c7501 2019-07-11 stsp const struct got_error *
6078 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6079 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6080 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6081 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6082 818c7501 2019-07-11 stsp {
6083 818c7501 2019-07-11 stsp const struct got_error *err;
6084 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6085 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6086 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6087 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6088 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6089 818c7501 2019-07-11 stsp
6090 818c7501 2019-07-11 stsp *commit_id = NULL;
6091 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6092 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6093 3e3a69f1 2019-07-25 stsp *branch = NULL;
6094 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6095 3e3a69f1 2019-07-25 stsp
6096 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6097 3e3a69f1 2019-07-25 stsp if (err)
6098 3e3a69f1 2019-07-25 stsp return err;
6099 818c7501 2019-07-11 stsp
6100 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6101 3e3a69f1 2019-07-25 stsp if (err)
6102 f032f1f7 2019-08-04 stsp goto done;
6103 f032f1f7 2019-08-04 stsp
6104 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6105 f032f1f7 2019-08-04 stsp &have_staged_files);
6106 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6107 f032f1f7 2019-08-04 stsp goto done;
6108 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6109 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6110 3e3a69f1 2019-07-25 stsp goto done;
6111 f032f1f7 2019-08-04 stsp }
6112 3e3a69f1 2019-07-25 stsp
6113 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6114 818c7501 2019-07-11 stsp if (err)
6115 f032f1f7 2019-08-04 stsp goto done;
6116 818c7501 2019-07-11 stsp
6117 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6118 818c7501 2019-07-11 stsp if (err)
6119 818c7501 2019-07-11 stsp goto done;
6120 818c7501 2019-07-11 stsp
6121 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6122 818c7501 2019-07-11 stsp if (err)
6123 818c7501 2019-07-11 stsp goto done;
6124 818c7501 2019-07-11 stsp
6125 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6126 818c7501 2019-07-11 stsp if (err)
6127 818c7501 2019-07-11 stsp goto done;
6128 818c7501 2019-07-11 stsp
6129 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6130 818c7501 2019-07-11 stsp if (err)
6131 818c7501 2019-07-11 stsp goto done;
6132 818c7501 2019-07-11 stsp
6133 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6134 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6135 818c7501 2019-07-11 stsp if (err)
6136 818c7501 2019-07-11 stsp goto done;
6137 818c7501 2019-07-11 stsp
6138 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6139 818c7501 2019-07-11 stsp if (err)
6140 818c7501 2019-07-11 stsp goto done;
6141 818c7501 2019-07-11 stsp
6142 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6143 818c7501 2019-07-11 stsp if (err)
6144 818c7501 2019-07-11 stsp goto done;
6145 818c7501 2019-07-11 stsp
6146 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6147 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6148 818c7501 2019-07-11 stsp if (err)
6149 818c7501 2019-07-11 stsp goto done;
6150 818c7501 2019-07-11 stsp
6151 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6152 818c7501 2019-07-11 stsp if (err)
6153 818c7501 2019-07-11 stsp goto done;
6154 818c7501 2019-07-11 stsp done:
6155 818c7501 2019-07-11 stsp free(commit_ref_name);
6156 818c7501 2019-07-11 stsp free(branch_ref_name);
6157 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6158 818c7501 2019-07-11 stsp if (commit_ref)
6159 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6160 818c7501 2019-07-11 stsp if (branch_ref)
6161 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6162 818c7501 2019-07-11 stsp if (err) {
6163 818c7501 2019-07-11 stsp free(*commit_id);
6164 818c7501 2019-07-11 stsp *commit_id = NULL;
6165 818c7501 2019-07-11 stsp if (*tmp_branch) {
6166 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6167 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6168 818c7501 2019-07-11 stsp }
6169 818c7501 2019-07-11 stsp if (*new_base_branch) {
6170 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6171 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6172 818c7501 2019-07-11 stsp }
6173 818c7501 2019-07-11 stsp if (*branch) {
6174 818c7501 2019-07-11 stsp got_ref_close(*branch);
6175 818c7501 2019-07-11 stsp *branch = NULL;
6176 818c7501 2019-07-11 stsp }
6177 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6178 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6179 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6180 3e3a69f1 2019-07-25 stsp }
6181 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6182 818c7501 2019-07-11 stsp }
6183 818c7501 2019-07-11 stsp return err;
6184 c4296144 2019-05-09 stsp }
6185 818c7501 2019-07-11 stsp
6186 818c7501 2019-07-11 stsp const struct got_error *
6187 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6188 818c7501 2019-07-11 stsp {
6189 818c7501 2019-07-11 stsp const struct got_error *err;
6190 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6191 818c7501 2019-07-11 stsp
6192 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6193 818c7501 2019-07-11 stsp if (err)
6194 818c7501 2019-07-11 stsp return err;
6195 818c7501 2019-07-11 stsp
6196 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6197 818c7501 2019-07-11 stsp free(tmp_branch_name);
6198 818c7501 2019-07-11 stsp return NULL;
6199 818c7501 2019-07-11 stsp }
6200 818c7501 2019-07-11 stsp
6201 818c7501 2019-07-11 stsp static const struct got_error *
6202 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6203 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6204 818c7501 2019-07-11 stsp {
6205 0ebf8283 2019-07-24 stsp *logmsg = arg;
6206 818c7501 2019-07-11 stsp return NULL;
6207 818c7501 2019-07-11 stsp }
6208 818c7501 2019-07-11 stsp
6209 818c7501 2019-07-11 stsp static const struct got_error *
6210 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6211 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6212 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6213 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6214 818c7501 2019-07-11 stsp {
6215 818c7501 2019-07-11 stsp return NULL;
6216 01757395 2019-07-12 stsp }
6217 01757395 2019-07-12 stsp
6218 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6219 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6220 01757395 2019-07-12 stsp void *progress_arg;
6221 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6222 01757395 2019-07-12 stsp };
6223 01757395 2019-07-12 stsp
6224 01757395 2019-07-12 stsp static const struct got_error *
6225 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6226 01757395 2019-07-12 stsp {
6227 01757395 2019-07-12 stsp const struct got_error *err;
6228 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6229 01757395 2019-07-12 stsp char *p;
6230 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6231 01757395 2019-07-12 stsp
6232 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6233 01757395 2019-07-12 stsp if (err)
6234 01757395 2019-07-12 stsp return err;
6235 01757395 2019-07-12 stsp
6236 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6237 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6238 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6239 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6240 01757395 2019-07-12 stsp return NULL;
6241 01757395 2019-07-12 stsp
6242 01757395 2019-07-12 stsp p = strdup(path);
6243 01757395 2019-07-12 stsp if (p == NULL)
6244 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6245 01757395 2019-07-12 stsp
6246 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6247 01757395 2019-07-12 stsp if (err || new == NULL)
6248 01757395 2019-07-12 stsp free(p);
6249 01757395 2019-07-12 stsp return err;
6250 01757395 2019-07-12 stsp }
6251 01757395 2019-07-12 stsp
6252 01757395 2019-07-12 stsp void
6253 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6254 01757395 2019-07-12 stsp {
6255 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6256 01757395 2019-07-12 stsp
6257 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6258 01757395 2019-07-12 stsp free((char *)pe->path);
6259 01757395 2019-07-12 stsp
6260 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6261 818c7501 2019-07-11 stsp }
6262 818c7501 2019-07-11 stsp
6263 0ebf8283 2019-07-24 stsp static const struct got_error *
6264 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6265 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6266 818c7501 2019-07-11 stsp {
6267 818c7501 2019-07-11 stsp const struct got_error *err;
6268 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6269 818c7501 2019-07-11 stsp
6270 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6271 818c7501 2019-07-11 stsp if (err) {
6272 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6273 818c7501 2019-07-11 stsp goto done;
6274 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6275 818c7501 2019-07-11 stsp if (err)
6276 818c7501 2019-07-11 stsp goto done;
6277 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6278 818c7501 2019-07-11 stsp if (err)
6279 818c7501 2019-07-11 stsp goto done;
6280 de05890f 2020-03-05 stsp } else if (is_rebase) {
6281 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6282 818c7501 2019-07-11 stsp int cmp;
6283 818c7501 2019-07-11 stsp
6284 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6285 818c7501 2019-07-11 stsp if (err)
6286 818c7501 2019-07-11 stsp goto done;
6287 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6288 818c7501 2019-07-11 stsp free(stored_id);
6289 818c7501 2019-07-11 stsp if (cmp != 0) {
6290 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6291 818c7501 2019-07-11 stsp goto done;
6292 818c7501 2019-07-11 stsp }
6293 818c7501 2019-07-11 stsp }
6294 0ebf8283 2019-07-24 stsp done:
6295 0ebf8283 2019-07-24 stsp if (commit_ref)
6296 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6297 0ebf8283 2019-07-24 stsp return err;
6298 0ebf8283 2019-07-24 stsp }
6299 0ebf8283 2019-07-24 stsp
6300 0ebf8283 2019-07-24 stsp static const struct got_error *
6301 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6302 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6303 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6304 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6305 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6306 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6307 0ebf8283 2019-07-24 stsp {
6308 0ebf8283 2019-07-24 stsp const struct got_error *err;
6309 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6310 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6311 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6312 818c7501 2019-07-11 stsp
6313 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6314 0ebf8283 2019-07-24 stsp
6315 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6316 0ebf8283 2019-07-24 stsp if (err)
6317 0ebf8283 2019-07-24 stsp return err;
6318 0ebf8283 2019-07-24 stsp
6319 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6320 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6321 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6322 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6323 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6324 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6325 818c7501 2019-07-11 stsp if (commit_ref)
6326 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6327 818c7501 2019-07-11 stsp return err;
6328 818c7501 2019-07-11 stsp }
6329 818c7501 2019-07-11 stsp
6330 818c7501 2019-07-11 stsp const struct got_error *
6331 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6332 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6333 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6334 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6335 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6336 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6337 818c7501 2019-07-11 stsp {
6338 0ebf8283 2019-07-24 stsp const struct got_error *err;
6339 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6340 0ebf8283 2019-07-24 stsp
6341 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6342 0ebf8283 2019-07-24 stsp if (err)
6343 0ebf8283 2019-07-24 stsp return err;
6344 0ebf8283 2019-07-24 stsp
6345 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6346 0ebf8283 2019-07-24 stsp if (err)
6347 0ebf8283 2019-07-24 stsp goto done;
6348 0ebf8283 2019-07-24 stsp
6349 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6350 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6351 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6352 0ebf8283 2019-07-24 stsp done:
6353 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6354 0ebf8283 2019-07-24 stsp return err;
6355 0ebf8283 2019-07-24 stsp }
6356 0ebf8283 2019-07-24 stsp
6357 0ebf8283 2019-07-24 stsp const struct got_error *
6358 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6359 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6360 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6361 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6362 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6363 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6364 0ebf8283 2019-07-24 stsp {
6365 0ebf8283 2019-07-24 stsp const struct got_error *err;
6366 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6367 0ebf8283 2019-07-24 stsp
6368 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6369 0ebf8283 2019-07-24 stsp if (err)
6370 0ebf8283 2019-07-24 stsp return err;
6371 0ebf8283 2019-07-24 stsp
6372 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6373 0ebf8283 2019-07-24 stsp if (err)
6374 0ebf8283 2019-07-24 stsp goto done;
6375 0ebf8283 2019-07-24 stsp
6376 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6377 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6378 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6379 0ebf8283 2019-07-24 stsp done:
6380 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6381 0ebf8283 2019-07-24 stsp return err;
6382 0ebf8283 2019-07-24 stsp }
6383 0ebf8283 2019-07-24 stsp
6384 0ebf8283 2019-07-24 stsp static const struct got_error *
6385 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6386 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6387 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6388 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6389 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6390 0ebf8283 2019-07-24 stsp {
6391 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6392 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6393 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6394 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6395 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6396 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6397 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6398 818c7501 2019-07-11 stsp
6399 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6400 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6401 a0e95631 2019-07-12 stsp
6402 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6403 818c7501 2019-07-11 stsp
6404 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6405 a0e95631 2019-07-12 stsp if (err)
6406 3e3a69f1 2019-07-25 stsp return err;
6407 a0e95631 2019-07-12 stsp
6408 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6409 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6410 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6411 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6412 01757395 2019-07-12 stsp /*
6413 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6414 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6415 6c13b005 2021-09-02 stsp *
6416 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6417 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6418 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6419 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6420 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6421 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6422 01757395 2019-07-12 stsp */
6423 01757395 2019-07-12 stsp if (merged_paths) {
6424 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6425 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6426 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6427 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6428 f2a9dc41 2019-12-13 tracey 0);
6429 01757395 2019-07-12 stsp if (err)
6430 01757395 2019-07-12 stsp goto done;
6431 01757395 2019-07-12 stsp }
6432 01757395 2019-07-12 stsp } else {
6433 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6434 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6435 01757395 2019-07-12 stsp if (err)
6436 01757395 2019-07-12 stsp goto done;
6437 01757395 2019-07-12 stsp }
6438 a0e95631 2019-07-12 stsp
6439 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6440 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6441 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6442 a0e95631 2019-07-12 stsp if (err)
6443 a0e95631 2019-07-12 stsp goto done;
6444 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6445 a0e95631 2019-07-12 stsp goto done;
6446 ff0d2220 2019-07-11 stsp }
6447 818c7501 2019-07-11 stsp
6448 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6449 edd02c5e 2019-07-11 stsp if (err)
6450 edd02c5e 2019-07-11 stsp goto done;
6451 edd02c5e 2019-07-11 stsp
6452 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6453 818c7501 2019-07-11 stsp if (err)
6454 818c7501 2019-07-11 stsp goto done;
6455 818c7501 2019-07-11 stsp
6456 5943eee2 2019-08-13 stsp if (new_logmsg) {
6457 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6458 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6459 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6460 5943eee2 2019-08-13 stsp goto done;
6461 5943eee2 2019-08-13 stsp }
6462 5943eee2 2019-08-13 stsp } else {
6463 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6464 5943eee2 2019-08-13 stsp if (err)
6465 5943eee2 2019-08-13 stsp goto done;
6466 5943eee2 2019-08-13 stsp }
6467 0ebf8283 2019-07-24 stsp
6468 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6469 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6470 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6471 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6472 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6473 a0e95631 2019-07-12 stsp if (err)
6474 a0e95631 2019-07-12 stsp goto done;
6475 a0e95631 2019-07-12 stsp
6476 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6477 a0e95631 2019-07-12 stsp if (err)
6478 a0e95631 2019-07-12 stsp goto done;
6479 a0e95631 2019-07-12 stsp
6480 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6481 a0e95631 2019-07-12 stsp if (err)
6482 a0e95631 2019-07-12 stsp goto done;
6483 a0e95631 2019-07-12 stsp
6484 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6485 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6486 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6487 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6488 a0e95631 2019-07-12 stsp err = sync_err;
6489 818c7501 2019-07-11 stsp done:
6490 a0e95631 2019-07-12 stsp free(fileindex_path);
6491 a0e95631 2019-07-12 stsp free(head_commit_id);
6492 a0e95631 2019-07-12 stsp if (head_ref)
6493 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6494 818c7501 2019-07-11 stsp if (err) {
6495 818c7501 2019-07-11 stsp free(*new_commit_id);
6496 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6497 818c7501 2019-07-11 stsp }
6498 818c7501 2019-07-11 stsp return err;
6499 818c7501 2019-07-11 stsp }
6500 818c7501 2019-07-11 stsp
6501 818c7501 2019-07-11 stsp const struct got_error *
6502 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6503 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6504 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6505 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6506 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6507 0ebf8283 2019-07-24 stsp {
6508 0ebf8283 2019-07-24 stsp const struct got_error *err;
6509 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6510 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6511 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6512 0ebf8283 2019-07-24 stsp
6513 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6514 0ebf8283 2019-07-24 stsp if (err)
6515 0ebf8283 2019-07-24 stsp return err;
6516 0ebf8283 2019-07-24 stsp
6517 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6518 0ebf8283 2019-07-24 stsp if (err)
6519 0ebf8283 2019-07-24 stsp goto done;
6520 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6521 0ebf8283 2019-07-24 stsp if (err)
6522 0ebf8283 2019-07-24 stsp goto done;
6523 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6524 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6525 0ebf8283 2019-07-24 stsp goto done;
6526 0ebf8283 2019-07-24 stsp }
6527 0ebf8283 2019-07-24 stsp
6528 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6529 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6530 0ebf8283 2019-07-24 stsp done:
6531 0ebf8283 2019-07-24 stsp if (commit_ref)
6532 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6533 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6534 0ebf8283 2019-07-24 stsp free(commit_id);
6535 0ebf8283 2019-07-24 stsp return err;
6536 0ebf8283 2019-07-24 stsp }
6537 0ebf8283 2019-07-24 stsp
6538 0ebf8283 2019-07-24 stsp const struct got_error *
6539 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6540 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6541 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6542 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6543 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6544 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6545 0ebf8283 2019-07-24 stsp {
6546 0ebf8283 2019-07-24 stsp const struct got_error *err;
6547 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6548 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6549 0ebf8283 2019-07-24 stsp
6550 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6551 0ebf8283 2019-07-24 stsp if (err)
6552 0ebf8283 2019-07-24 stsp return err;
6553 0ebf8283 2019-07-24 stsp
6554 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6555 0ebf8283 2019-07-24 stsp if (err)
6556 0ebf8283 2019-07-24 stsp goto done;
6557 0ebf8283 2019-07-24 stsp
6558 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6559 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6560 0ebf8283 2019-07-24 stsp done:
6561 0ebf8283 2019-07-24 stsp if (commit_ref)
6562 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6563 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6564 0ebf8283 2019-07-24 stsp return err;
6565 0ebf8283 2019-07-24 stsp }
6566 0ebf8283 2019-07-24 stsp
6567 0ebf8283 2019-07-24 stsp const struct got_error *
6568 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6569 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6570 818c7501 2019-07-11 stsp {
6571 3e3a69f1 2019-07-25 stsp if (fileindex)
6572 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6573 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6574 69844fba 2019-07-11 stsp }
6575 69844fba 2019-07-11 stsp
6576 69844fba 2019-07-11 stsp static const struct got_error *
6577 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6578 69844fba 2019-07-11 stsp {
6579 69844fba 2019-07-11 stsp const struct got_error *err;
6580 69844fba 2019-07-11 stsp struct got_reference *ref;
6581 69844fba 2019-07-11 stsp
6582 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6583 69844fba 2019-07-11 stsp if (err) {
6584 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6585 69844fba 2019-07-11 stsp return NULL;
6586 69844fba 2019-07-11 stsp return err;
6587 69844fba 2019-07-11 stsp }
6588 69844fba 2019-07-11 stsp
6589 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6590 69844fba 2019-07-11 stsp got_ref_close(ref);
6591 69844fba 2019-07-11 stsp return err;
6592 818c7501 2019-07-11 stsp }
6593 818c7501 2019-07-11 stsp
6594 69844fba 2019-07-11 stsp static const struct got_error *
6595 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6596 69844fba 2019-07-11 stsp {
6597 69844fba 2019-07-11 stsp const struct got_error *err;
6598 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6599 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6600 69844fba 2019-07-11 stsp
6601 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6602 69844fba 2019-07-11 stsp if (err)
6603 69844fba 2019-07-11 stsp goto done;
6604 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6605 69844fba 2019-07-11 stsp if (err)
6606 69844fba 2019-07-11 stsp goto done;
6607 69844fba 2019-07-11 stsp
6608 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6609 69844fba 2019-07-11 stsp if (err)
6610 69844fba 2019-07-11 stsp goto done;
6611 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6612 69844fba 2019-07-11 stsp if (err)
6613 69844fba 2019-07-11 stsp goto done;
6614 69844fba 2019-07-11 stsp
6615 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6616 69844fba 2019-07-11 stsp if (err)
6617 69844fba 2019-07-11 stsp goto done;
6618 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6619 69844fba 2019-07-11 stsp if (err)
6620 69844fba 2019-07-11 stsp goto done;
6621 69844fba 2019-07-11 stsp
6622 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6623 69844fba 2019-07-11 stsp if (err)
6624 69844fba 2019-07-11 stsp goto done;
6625 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6626 69844fba 2019-07-11 stsp if (err)
6627 69844fba 2019-07-11 stsp goto done;
6628 69844fba 2019-07-11 stsp
6629 69844fba 2019-07-11 stsp done:
6630 69844fba 2019-07-11 stsp free(tmp_branch_name);
6631 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6632 69844fba 2019-07-11 stsp free(branch_ref_name);
6633 69844fba 2019-07-11 stsp free(commit_ref_name);
6634 e600f124 2021-03-21 stsp return err;
6635 e600f124 2021-03-21 stsp }
6636 e600f124 2021-03-21 stsp
6637 ef20f542 2022-06-26 thomas static const struct got_error *
6638 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6639 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6640 e600f124 2021-03-21 stsp {
6641 e600f124 2021-03-21 stsp const struct got_error *err;
6642 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6643 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6644 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6645 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6646 e600f124 2021-03-21 stsp char *refname = NULL;
6647 e600f124 2021-03-21 stsp
6648 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6649 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6650 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6651 e600f124 2021-03-21 stsp branch_name += 11;
6652 e600f124 2021-03-21 stsp
6653 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6654 e600f124 2021-03-21 stsp if (err)
6655 e600f124 2021-03-21 stsp return err;
6656 e600f124 2021-03-21 stsp
6657 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6658 e600f124 2021-03-21 stsp new_id_str) == -1) {
6659 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6660 e600f124 2021-03-21 stsp goto done;
6661 e600f124 2021-03-21 stsp }
6662 e600f124 2021-03-21 stsp
6663 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6664 e600f124 2021-03-21 stsp if (err)
6665 e600f124 2021-03-21 stsp goto done;
6666 e600f124 2021-03-21 stsp
6667 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6668 e600f124 2021-03-21 stsp if (err)
6669 e600f124 2021-03-21 stsp goto done;
6670 e600f124 2021-03-21 stsp
6671 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6672 e600f124 2021-03-21 stsp done:
6673 e600f124 2021-03-21 stsp free(new_id_str);
6674 e600f124 2021-03-21 stsp free(refname);
6675 e600f124 2021-03-21 stsp free(old_commit_id);
6676 e600f124 2021-03-21 stsp if (ref)
6677 e600f124 2021-03-21 stsp got_ref_close(ref);
6678 69844fba 2019-07-11 stsp return err;
6679 69844fba 2019-07-11 stsp }
6680 69844fba 2019-07-11 stsp
6681 818c7501 2019-07-11 stsp const struct got_error *
6682 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6683 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6684 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6685 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6686 818c7501 2019-07-11 stsp {
6687 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6688 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6689 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6690 818c7501 2019-07-11 stsp
6691 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6692 818c7501 2019-07-11 stsp if (err)
6693 818c7501 2019-07-11 stsp return err;
6694 e600f124 2021-03-21 stsp
6695 e600f124 2021-03-21 stsp if (create_backup) {
6696 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6697 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6698 e600f124 2021-03-21 stsp if (err)
6699 e600f124 2021-03-21 stsp goto done;
6700 e600f124 2021-03-21 stsp }
6701 818c7501 2019-07-11 stsp
6702 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6703 818c7501 2019-07-11 stsp if (err)
6704 818c7501 2019-07-11 stsp goto done;
6705 818c7501 2019-07-11 stsp
6706 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6707 818c7501 2019-07-11 stsp if (err)
6708 818c7501 2019-07-11 stsp goto done;
6709 818c7501 2019-07-11 stsp
6710 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6711 818c7501 2019-07-11 stsp if (err)
6712 818c7501 2019-07-11 stsp goto done;
6713 818c7501 2019-07-11 stsp
6714 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6715 a615e0e7 2020-12-16 stsp if (err)
6716 a615e0e7 2020-12-16 stsp goto done;
6717 a615e0e7 2020-12-16 stsp
6718 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6719 a615e0e7 2020-12-16 stsp if (err)
6720 a615e0e7 2020-12-16 stsp goto done;
6721 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6722 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6723 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6724 a615e0e7 2020-12-16 stsp err = sync_err;
6725 818c7501 2019-07-11 stsp done:
6726 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6727 a615e0e7 2020-12-16 stsp free(fileindex_path);
6728 818c7501 2019-07-11 stsp free(new_head_commit_id);
6729 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6730 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6731 818c7501 2019-07-11 stsp err = unlockerr;
6732 818c7501 2019-07-11 stsp return err;
6733 818c7501 2019-07-11 stsp }
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp const struct got_error *
6736 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6737 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6738 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6739 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6740 818c7501 2019-07-11 stsp {
6741 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6742 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6743 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6744 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6745 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6746 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6747 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6748 818c7501 2019-07-11 stsp
6749 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6750 818c7501 2019-07-11 stsp if (err)
6751 818c7501 2019-07-11 stsp return err;
6752 818c7501 2019-07-11 stsp
6753 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
6754 945f9229 2022-04-16 thomas worktree->base_commit_id);
6755 945f9229 2022-04-16 thomas if (err)
6756 945f9229 2022-04-16 thomas goto done;
6757 945f9229 2022-04-16 thomas
6758 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6759 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6760 818c7501 2019-07-11 stsp if (err)
6761 818c7501 2019-07-11 stsp goto done;
6762 818c7501 2019-07-11 stsp
6763 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6764 818c7501 2019-07-11 stsp if (err)
6765 818c7501 2019-07-11 stsp goto done;
6766 818c7501 2019-07-11 stsp
6767 818c7501 2019-07-11 stsp /*
6768 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6769 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6770 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6771 818c7501 2019-07-11 stsp */
6772 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6773 818c7501 2019-07-11 stsp if (err)
6774 818c7501 2019-07-11 stsp goto done;
6775 818c7501 2019-07-11 stsp
6776 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6777 818c7501 2019-07-11 stsp if (err)
6778 818c7501 2019-07-11 stsp goto done;
6779 818c7501 2019-07-11 stsp
6780 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
6781 945f9229 2022-04-16 thomas worktree->path_prefix);
6782 ca355955 2019-07-12 stsp if (err)
6783 ca355955 2019-07-12 stsp goto done;
6784 ca355955 2019-07-12 stsp
6785 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6786 ca355955 2019-07-12 stsp if (err)
6787 ca355955 2019-07-12 stsp goto done;
6788 ca355955 2019-07-12 stsp
6789 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6790 818c7501 2019-07-11 stsp if (err)
6791 818c7501 2019-07-11 stsp goto done;
6792 818c7501 2019-07-11 stsp
6793 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6794 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6795 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6796 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6797 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6798 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6799 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6800 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6801 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6802 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6803 55bd499d 2019-07-12 stsp if (err)
6804 1f1abb7e 2019-08-08 stsp goto sync;
6805 55bd499d 2019-07-12 stsp
6806 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6807 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6808 ca355955 2019-07-12 stsp sync:
6809 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6810 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6811 ca355955 2019-07-12 stsp err = sync_err;
6812 818c7501 2019-07-11 stsp done:
6813 818c7501 2019-07-11 stsp got_ref_close(resolved);
6814 a3a2faf2 2019-07-12 stsp free(tree_id);
6815 818c7501 2019-07-11 stsp free(commit_id);
6816 945f9229 2022-04-16 thomas if (commit)
6817 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6818 0ebf8283 2019-07-24 stsp if (fileindex)
6819 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6820 0ebf8283 2019-07-24 stsp free(fileindex_path);
6821 0ebf8283 2019-07-24 stsp
6822 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6823 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6824 0ebf8283 2019-07-24 stsp err = unlockerr;
6825 0ebf8283 2019-07-24 stsp return err;
6826 0ebf8283 2019-07-24 stsp }
6827 0ebf8283 2019-07-24 stsp
6828 0ebf8283 2019-07-24 stsp const struct got_error *
6829 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6830 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6831 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6832 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6833 0ebf8283 2019-07-24 stsp {
6834 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6835 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6836 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6837 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6838 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6839 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6840 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6841 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6842 0ebf8283 2019-07-24 stsp
6843 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6844 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6845 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6846 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6847 0ebf8283 2019-07-24 stsp
6848 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6849 0ebf8283 2019-07-24 stsp if (err)
6850 0ebf8283 2019-07-24 stsp return err;
6851 0ebf8283 2019-07-24 stsp
6852 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6853 0ebf8283 2019-07-24 stsp if (err)
6854 0ebf8283 2019-07-24 stsp goto done;
6855 0ebf8283 2019-07-24 stsp
6856 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6857 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6858 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6859 0ebf8283 2019-07-24 stsp &ok_arg);
6860 0ebf8283 2019-07-24 stsp if (err)
6861 0ebf8283 2019-07-24 stsp goto done;
6862 0ebf8283 2019-07-24 stsp
6863 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6864 0ebf8283 2019-07-24 stsp if (err)
6865 0ebf8283 2019-07-24 stsp goto done;
6866 0ebf8283 2019-07-24 stsp
6867 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6868 0ebf8283 2019-07-24 stsp if (err)
6869 0ebf8283 2019-07-24 stsp goto done;
6870 0ebf8283 2019-07-24 stsp
6871 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6872 0ebf8283 2019-07-24 stsp worktree);
6873 0ebf8283 2019-07-24 stsp if (err)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp
6876 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6877 0ebf8283 2019-07-24 stsp 0);
6878 0ebf8283 2019-07-24 stsp if (err)
6879 0ebf8283 2019-07-24 stsp goto done;
6880 0ebf8283 2019-07-24 stsp
6881 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6882 0ebf8283 2019-07-24 stsp if (err)
6883 0ebf8283 2019-07-24 stsp goto done;
6884 0ebf8283 2019-07-24 stsp
6885 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6886 0ebf8283 2019-07-24 stsp if (err)
6887 0ebf8283 2019-07-24 stsp goto done;
6888 0ebf8283 2019-07-24 stsp
6889 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6890 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6891 0ebf8283 2019-07-24 stsp if (err)
6892 0ebf8283 2019-07-24 stsp goto done;
6893 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6894 0ebf8283 2019-07-24 stsp if (err)
6895 0ebf8283 2019-07-24 stsp goto done;
6896 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6897 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6898 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6899 0ebf8283 2019-07-24 stsp goto done;
6900 0ebf8283 2019-07-24 stsp }
6901 0ebf8283 2019-07-24 stsp
6902 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6903 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6904 0ebf8283 2019-07-24 stsp if (err)
6905 0ebf8283 2019-07-24 stsp goto done;
6906 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6907 0ebf8283 2019-07-24 stsp if (err)
6908 0ebf8283 2019-07-24 stsp goto done;
6909 0ebf8283 2019-07-24 stsp
6910 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6911 0ebf8283 2019-07-24 stsp if (err)
6912 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp done:
6914 0ebf8283 2019-07-24 stsp free(fileindex_path);
6915 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6916 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6917 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6918 0ebf8283 2019-07-24 stsp if (wt_branch)
6919 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6920 0ebf8283 2019-07-24 stsp if (err) {
6921 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6922 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6923 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6924 0ebf8283 2019-07-24 stsp }
6925 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6926 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6927 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6928 0ebf8283 2019-07-24 stsp }
6929 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6930 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6931 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6932 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6933 3e3a69f1 2019-07-25 stsp }
6934 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6935 0ebf8283 2019-07-24 stsp }
6936 0ebf8283 2019-07-24 stsp return err;
6937 0ebf8283 2019-07-24 stsp }
6938 0ebf8283 2019-07-24 stsp
6939 0ebf8283 2019-07-24 stsp const struct got_error *
6940 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6941 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6942 0ebf8283 2019-07-24 stsp {
6943 3e3a69f1 2019-07-25 stsp if (fileindex)
6944 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6945 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6946 0ebf8283 2019-07-24 stsp }
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp const struct got_error *
6949 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6950 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6951 0ebf8283 2019-07-24 stsp {
6952 0ebf8283 2019-07-24 stsp const struct got_error *err;
6953 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6954 0ebf8283 2019-07-24 stsp
6955 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6956 0ebf8283 2019-07-24 stsp if (err)
6957 0ebf8283 2019-07-24 stsp return err;
6958 0ebf8283 2019-07-24 stsp
6959 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6960 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6961 0ebf8283 2019-07-24 stsp return NULL;
6962 0ebf8283 2019-07-24 stsp }
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp const struct got_error *
6965 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6966 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6967 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6968 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6969 0ebf8283 2019-07-24 stsp {
6970 0ebf8283 2019-07-24 stsp const struct got_error *err;
6971 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6972 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6973 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6974 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6975 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6976 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6977 0ebf8283 2019-07-24 stsp
6978 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6979 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6980 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6981 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6982 0ebf8283 2019-07-24 stsp
6983 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6984 3e3a69f1 2019-07-25 stsp if (err)
6985 3e3a69f1 2019-07-25 stsp return err;
6986 3e3a69f1 2019-07-25 stsp
6987 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6988 3e3a69f1 2019-07-25 stsp if (err)
6989 f032f1f7 2019-08-04 stsp goto done;
6990 f032f1f7 2019-08-04 stsp
6991 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6992 f032f1f7 2019-08-04 stsp &have_staged_files);
6993 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6994 f032f1f7 2019-08-04 stsp goto done;
6995 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6996 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6997 3e3a69f1 2019-07-25 stsp goto done;
6998 f032f1f7 2019-08-04 stsp }
6999 3e3a69f1 2019-07-25 stsp
7000 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7001 0ebf8283 2019-07-24 stsp if (err)
7002 f032f1f7 2019-08-04 stsp goto done;
7003 0ebf8283 2019-07-24 stsp
7004 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7005 0ebf8283 2019-07-24 stsp if (err)
7006 0ebf8283 2019-07-24 stsp goto done;
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7009 0ebf8283 2019-07-24 stsp if (err)
7010 0ebf8283 2019-07-24 stsp goto done;
7011 0ebf8283 2019-07-24 stsp
7012 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7013 0ebf8283 2019-07-24 stsp worktree);
7014 0ebf8283 2019-07-24 stsp if (err)
7015 0ebf8283 2019-07-24 stsp goto done;
7016 0ebf8283 2019-07-24 stsp
7017 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7018 0ebf8283 2019-07-24 stsp if (err)
7019 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7022 0ebf8283 2019-07-24 stsp if (err)
7023 0ebf8283 2019-07-24 stsp goto done;
7024 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7025 0ebf8283 2019-07-24 stsp if (err)
7026 0ebf8283 2019-07-24 stsp goto done;
7027 0ebf8283 2019-07-24 stsp
7028 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7029 0ebf8283 2019-07-24 stsp if (err)
7030 0ebf8283 2019-07-24 stsp goto done;
7031 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7032 0ebf8283 2019-07-24 stsp if (err)
7033 0ebf8283 2019-07-24 stsp goto done;
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7036 0ebf8283 2019-07-24 stsp if (err)
7037 0ebf8283 2019-07-24 stsp goto done;
7038 0ebf8283 2019-07-24 stsp done:
7039 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7040 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7041 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7042 0ebf8283 2019-07-24 stsp if (commit_ref)
7043 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7044 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7045 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7046 0ebf8283 2019-07-24 stsp if (err) {
7047 0ebf8283 2019-07-24 stsp free(*commit_id);
7048 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7049 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7050 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7051 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7052 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7053 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7054 0ebf8283 2019-07-24 stsp }
7055 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7056 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7057 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7058 3e3a69f1 2019-07-25 stsp }
7059 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7060 0ebf8283 2019-07-24 stsp }
7061 0ebf8283 2019-07-24 stsp return err;
7062 0ebf8283 2019-07-24 stsp }
7063 0ebf8283 2019-07-24 stsp
7064 0ebf8283 2019-07-24 stsp static const struct got_error *
7065 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7066 0ebf8283 2019-07-24 stsp {
7067 0ebf8283 2019-07-24 stsp const struct got_error *err;
7068 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7069 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7070 0ebf8283 2019-07-24 stsp
7071 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7072 0ebf8283 2019-07-24 stsp if (err)
7073 0ebf8283 2019-07-24 stsp goto done;
7074 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7075 0ebf8283 2019-07-24 stsp if (err)
7076 0ebf8283 2019-07-24 stsp goto done;
7077 0ebf8283 2019-07-24 stsp
7078 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7079 0ebf8283 2019-07-24 stsp worktree);
7080 0ebf8283 2019-07-24 stsp if (err)
7081 0ebf8283 2019-07-24 stsp goto done;
7082 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7083 0ebf8283 2019-07-24 stsp if (err)
7084 0ebf8283 2019-07-24 stsp goto done;
7085 0ebf8283 2019-07-24 stsp
7086 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7087 0ebf8283 2019-07-24 stsp if (err)
7088 0ebf8283 2019-07-24 stsp goto done;
7089 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7090 0ebf8283 2019-07-24 stsp if (err)
7091 0ebf8283 2019-07-24 stsp goto done;
7092 0ebf8283 2019-07-24 stsp
7093 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7094 0ebf8283 2019-07-24 stsp if (err)
7095 0ebf8283 2019-07-24 stsp goto done;
7096 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7097 0ebf8283 2019-07-24 stsp if (err)
7098 0ebf8283 2019-07-24 stsp goto done;
7099 0ebf8283 2019-07-24 stsp done:
7100 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7101 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7102 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7103 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7104 0ebf8283 2019-07-24 stsp return err;
7105 0ebf8283 2019-07-24 stsp }
7106 0ebf8283 2019-07-24 stsp
7107 0ebf8283 2019-07-24 stsp const struct got_error *
7108 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7109 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7110 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7111 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7112 0ebf8283 2019-07-24 stsp {
7113 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7114 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7115 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7116 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7117 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7118 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7119 0ebf8283 2019-07-24 stsp
7120 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7121 0ebf8283 2019-07-24 stsp if (err)
7122 0ebf8283 2019-07-24 stsp return err;
7123 0ebf8283 2019-07-24 stsp
7124 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7125 945f9229 2022-04-16 thomas worktree->base_commit_id);
7126 945f9229 2022-04-16 thomas if (err)
7127 945f9229 2022-04-16 thomas goto done;
7128 945f9229 2022-04-16 thomas
7129 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7130 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7131 0ebf8283 2019-07-24 stsp if (err)
7132 0ebf8283 2019-07-24 stsp goto done;
7133 0ebf8283 2019-07-24 stsp
7134 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7135 0ebf8283 2019-07-24 stsp if (err)
7136 0ebf8283 2019-07-24 stsp goto done;
7137 0ebf8283 2019-07-24 stsp
7138 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7139 0ebf8283 2019-07-24 stsp if (err)
7140 0ebf8283 2019-07-24 stsp goto done;
7141 0ebf8283 2019-07-24 stsp
7142 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7143 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7144 0ebf8283 2019-07-24 stsp if (err)
7145 0ebf8283 2019-07-24 stsp goto done;
7146 0ebf8283 2019-07-24 stsp
7147 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7148 0ebf8283 2019-07-24 stsp if (err)
7149 0ebf8283 2019-07-24 stsp goto done;
7150 0ebf8283 2019-07-24 stsp
7151 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7152 0ebf8283 2019-07-24 stsp if (err)
7153 0ebf8283 2019-07-24 stsp goto done;
7154 0ebf8283 2019-07-24 stsp
7155 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7156 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7157 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7158 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7159 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7160 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7161 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7162 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7163 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7164 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7165 0ebf8283 2019-07-24 stsp if (err)
7166 1f1abb7e 2019-08-08 stsp goto sync;
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7169 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7170 0ebf8283 2019-07-24 stsp sync:
7171 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7172 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7173 0ebf8283 2019-07-24 stsp err = sync_err;
7174 0ebf8283 2019-07-24 stsp done:
7175 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7176 0ebf8283 2019-07-24 stsp free(tree_id);
7177 818c7501 2019-07-11 stsp free(fileindex_path);
7178 818c7501 2019-07-11 stsp
7179 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7180 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7181 818c7501 2019-07-11 stsp err = unlockerr;
7182 818c7501 2019-07-11 stsp return err;
7183 818c7501 2019-07-11 stsp }
7184 0ebf8283 2019-07-24 stsp
7185 0ebf8283 2019-07-24 stsp const struct got_error *
7186 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7187 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7188 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7189 0ebf8283 2019-07-24 stsp {
7190 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7191 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7192 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7193 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7194 0ebf8283 2019-07-24 stsp
7195 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7196 0ebf8283 2019-07-24 stsp if (err)
7197 0ebf8283 2019-07-24 stsp return err;
7198 0ebf8283 2019-07-24 stsp
7199 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7200 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7201 e600f124 2021-03-21 stsp if (err)
7202 e600f124 2021-03-21 stsp goto done;
7203 e600f124 2021-03-21 stsp
7204 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7205 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
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 = got_ref_change_ref(resolved, new_head_commit_id);
7210 0ebf8283 2019-07-24 stsp if (err)
7211 0ebf8283 2019-07-24 stsp goto done;
7212 0ebf8283 2019-07-24 stsp
7213 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7214 0ebf8283 2019-07-24 stsp if (err)
7215 0ebf8283 2019-07-24 stsp goto done;
7216 0ebf8283 2019-07-24 stsp
7217 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7218 0ebf8283 2019-07-24 stsp if (err)
7219 0ebf8283 2019-07-24 stsp goto done;
7220 0ebf8283 2019-07-24 stsp
7221 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7222 a615e0e7 2020-12-16 stsp if (err)
7223 a615e0e7 2020-12-16 stsp goto done;
7224 a615e0e7 2020-12-16 stsp
7225 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7226 a615e0e7 2020-12-16 stsp if (err)
7227 a615e0e7 2020-12-16 stsp goto done;
7228 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7229 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7230 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7231 a615e0e7 2020-12-16 stsp err = sync_err;
7232 0ebf8283 2019-07-24 stsp done:
7233 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7234 a615e0e7 2020-12-16 stsp free(fileindex_path);
7235 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7236 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7237 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7238 0ebf8283 2019-07-24 stsp err = unlockerr;
7239 0ebf8283 2019-07-24 stsp return err;
7240 0ebf8283 2019-07-24 stsp }
7241 0ebf8283 2019-07-24 stsp
7242 0ebf8283 2019-07-24 stsp const struct got_error *
7243 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7244 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7245 0ebf8283 2019-07-24 stsp {
7246 0ebf8283 2019-07-24 stsp const struct got_error *err;
7247 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7248 0ebf8283 2019-07-24 stsp
7249 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7250 0ebf8283 2019-07-24 stsp if (err)
7251 0ebf8283 2019-07-24 stsp return err;
7252 0ebf8283 2019-07-24 stsp
7253 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7254 0ebf8283 2019-07-24 stsp if (err)
7255 0ebf8283 2019-07-24 stsp goto done;
7256 0ebf8283 2019-07-24 stsp
7257 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7258 0ebf8283 2019-07-24 stsp done:
7259 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7260 2822a352 2019-10-15 stsp return err;
7261 2822a352 2019-10-15 stsp }
7262 2822a352 2019-10-15 stsp
7263 2822a352 2019-10-15 stsp const struct got_error *
7264 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7265 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7266 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7267 2822a352 2019-10-15 stsp struct got_repository *repo)
7268 2822a352 2019-10-15 stsp {
7269 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7270 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7271 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7272 2822a352 2019-10-15 stsp
7273 2822a352 2019-10-15 stsp *fileindex = NULL;
7274 2822a352 2019-10-15 stsp *branch_ref = NULL;
7275 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7276 2822a352 2019-10-15 stsp
7277 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7278 2822a352 2019-10-15 stsp if (err)
7279 2822a352 2019-10-15 stsp return err;
7280 2822a352 2019-10-15 stsp
7281 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7282 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7283 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7284 2822a352 2019-10-15 stsp "update -b or different branch name required");
7285 2822a352 2019-10-15 stsp goto done;
7286 2822a352 2019-10-15 stsp }
7287 2822a352 2019-10-15 stsp
7288 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7289 2822a352 2019-10-15 stsp if (err)
7290 2822a352 2019-10-15 stsp goto done;
7291 2822a352 2019-10-15 stsp
7292 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7293 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7294 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7295 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7296 2822a352 2019-10-15 stsp &ok_arg);
7297 2822a352 2019-10-15 stsp if (err)
7298 2822a352 2019-10-15 stsp goto done;
7299 2822a352 2019-10-15 stsp
7300 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7301 2822a352 2019-10-15 stsp if (err)
7302 2822a352 2019-10-15 stsp goto done;
7303 2822a352 2019-10-15 stsp
7304 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7305 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7306 2822a352 2019-10-15 stsp done:
7307 2822a352 2019-10-15 stsp if (err) {
7308 2822a352 2019-10-15 stsp if (*branch_ref) {
7309 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7310 2822a352 2019-10-15 stsp *branch_ref = NULL;
7311 2822a352 2019-10-15 stsp }
7312 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7313 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7314 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7315 2822a352 2019-10-15 stsp }
7316 2822a352 2019-10-15 stsp if (*fileindex) {
7317 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7318 2822a352 2019-10-15 stsp *fileindex = NULL;
7319 2822a352 2019-10-15 stsp }
7320 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7321 2822a352 2019-10-15 stsp }
7322 0cb83759 2019-08-03 stsp return err;
7323 0cb83759 2019-08-03 stsp }
7324 0cb83759 2019-08-03 stsp
7325 2822a352 2019-10-15 stsp const struct got_error *
7326 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7327 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7328 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7329 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7330 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7331 2822a352 2019-10-15 stsp {
7332 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7333 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7334 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7335 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7336 2822a352 2019-10-15 stsp
7337 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7338 2822a352 2019-10-15 stsp if (err)
7339 2822a352 2019-10-15 stsp goto done;
7340 2822a352 2019-10-15 stsp
7341 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7342 2822a352 2019-10-15 stsp if (err)
7343 2822a352 2019-10-15 stsp goto done;
7344 2822a352 2019-10-15 stsp
7345 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7346 945f9229 2022-04-16 thomas if (err)
7347 945f9229 2022-04-16 thomas goto done;
7348 945f9229 2022-04-16 thomas
7349 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7350 2822a352 2019-10-15 stsp worktree->path_prefix);
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 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7355 2822a352 2019-10-15 stsp if (err)
7356 2822a352 2019-10-15 stsp goto done;
7357 2822a352 2019-10-15 stsp
7358 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7359 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7360 2822a352 2019-10-15 stsp if (err)
7361 2822a352 2019-10-15 stsp goto sync;
7362 2822a352 2019-10-15 stsp
7363 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7364 2822a352 2019-10-15 stsp if (err)
7365 2822a352 2019-10-15 stsp goto sync;
7366 2822a352 2019-10-15 stsp
7367 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7368 6e210706 2021-01-22 stsp if (err)
7369 6e210706 2021-01-22 stsp goto sync;
7370 6e210706 2021-01-22 stsp
7371 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7372 2822a352 2019-10-15 stsp sync:
7373 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7374 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7375 2822a352 2019-10-15 stsp err = sync_err;
7376 2822a352 2019-10-15 stsp
7377 2822a352 2019-10-15 stsp done:
7378 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7379 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7380 2822a352 2019-10-15 stsp err = unlockerr;
7381 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7382 2822a352 2019-10-15 stsp
7383 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7384 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7385 2822a352 2019-10-15 stsp err = unlockerr;
7386 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7387 2822a352 2019-10-15 stsp
7388 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7389 2822a352 2019-10-15 stsp free(fileindex_path);
7390 2822a352 2019-10-15 stsp free(tree_id);
7391 945f9229 2022-04-16 thomas if (commit)
7392 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7393 2822a352 2019-10-15 stsp
7394 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7395 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7396 2822a352 2019-10-15 stsp err = unlockerr;
7397 2822a352 2019-10-15 stsp return err;
7398 2822a352 2019-10-15 stsp }
7399 2822a352 2019-10-15 stsp
7400 2822a352 2019-10-15 stsp const struct got_error *
7401 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7402 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7403 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7404 2822a352 2019-10-15 stsp {
7405 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7406 8b692cd0 2019-10-21 stsp
7407 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7408 8b692cd0 2019-10-21 stsp
7409 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7410 8b692cd0 2019-10-21 stsp
7411 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7412 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7413 8b692cd0 2019-10-21 stsp err = unlockerr;
7414 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7415 8b692cd0 2019-10-21 stsp
7416 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7417 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7418 8b692cd0 2019-10-21 stsp err = unlockerr;
7419 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7420 10604dce 2021-09-24 thomas
7421 10604dce 2021-09-24 thomas return err;
7422 10604dce 2021-09-24 thomas }
7423 10604dce 2021-09-24 thomas
7424 10604dce 2021-09-24 thomas const struct got_error *
7425 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7426 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7427 10604dce 2021-09-24 thomas {
7428 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7429 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7430 10604dce 2021-09-24 thomas
7431 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7432 10604dce 2021-09-24 thomas if (err)
7433 10604dce 2021-09-24 thomas goto done;
7434 8b692cd0 2019-10-21 stsp
7435 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7436 10604dce 2021-09-24 thomas
7437 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7438 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7439 10604dce 2021-09-24 thomas err = sync_err;
7440 10604dce 2021-09-24 thomas done:
7441 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7442 10604dce 2021-09-24 thomas free(fileindex_path);
7443 8b692cd0 2019-10-21 stsp return err;
7444 2822a352 2019-10-15 stsp }
7445 2822a352 2019-10-15 stsp
7446 10604dce 2021-09-24 thomas static const struct got_error *
7447 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7448 10604dce 2021-09-24 thomas {
7449 10604dce 2021-09-24 thomas const struct got_error *err;
7450 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7451 10604dce 2021-09-24 thomas
7452 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7453 10604dce 2021-09-24 thomas if (err)
7454 10604dce 2021-09-24 thomas goto done;
7455 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7456 10604dce 2021-09-24 thomas if (err)
7457 10604dce 2021-09-24 thomas goto done;
7458 10604dce 2021-09-24 thomas
7459 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7460 10604dce 2021-09-24 thomas if (err)
7461 10604dce 2021-09-24 thomas goto done;
7462 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7463 10604dce 2021-09-24 thomas if (err)
7464 10604dce 2021-09-24 thomas goto done;
7465 10604dce 2021-09-24 thomas
7466 10604dce 2021-09-24 thomas done:
7467 10604dce 2021-09-24 thomas free(branch_refname);
7468 10604dce 2021-09-24 thomas free(commit_refname);
7469 10604dce 2021-09-24 thomas return err;
7470 10604dce 2021-09-24 thomas }
7471 10604dce 2021-09-24 thomas
7472 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7473 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7474 10604dce 2021-09-24 thomas const char *branch_name;
7475 10604dce 2021-09-24 thomas };
7476 10604dce 2021-09-24 thomas
7477 10604dce 2021-09-24 thomas static const struct got_error *
7478 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7479 10604dce 2021-09-24 thomas void *arg)
7480 10604dce 2021-09-24 thomas {
7481 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7482 10604dce 2021-09-24 thomas
7483 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7484 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7485 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7486 10604dce 2021-09-24 thomas
7487 10604dce 2021-09-24 thomas return NULL;
7488 10604dce 2021-09-24 thomas }
7489 10604dce 2021-09-24 thomas
7490 10604dce 2021-09-24 thomas
7491 10604dce 2021-09-24 thomas const struct got_error *
7492 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7493 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7494 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7495 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7496 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7497 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7498 10604dce 2021-09-24 thomas {
7499 10604dce 2021-09-24 thomas const struct got_error *err;
7500 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7501 10604dce 2021-09-24 thomas
7502 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7503 10604dce 2021-09-24 thomas if (err)
7504 10604dce 2021-09-24 thomas goto done;
7505 10604dce 2021-09-24 thomas
7506 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7507 10604dce 2021-09-24 thomas worktree);
7508 10604dce 2021-09-24 thomas if (err)
7509 10604dce 2021-09-24 thomas goto done;
7510 10604dce 2021-09-24 thomas
7511 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7512 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7513 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7514 10604dce 2021-09-24 thomas done:
7515 10604dce 2021-09-24 thomas free(fileindex_path);
7516 10604dce 2021-09-24 thomas return err;
7517 10604dce 2021-09-24 thomas }
7518 10604dce 2021-09-24 thomas
7519 10604dce 2021-09-24 thomas const struct got_error *
7520 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7521 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7522 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7523 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7524 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7525 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7526 ae1e948a 2021-09-28 thomas
7527 10604dce 2021-09-24 thomas {
7528 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7529 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7530 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7531 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7532 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7533 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7534 10604dce 2021-09-24 thomas int have_staged_files = 0;
7535 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7536 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7537 10604dce 2021-09-24 thomas
7538 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7539 10604dce 2021-09-24 thomas
7540 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7541 10604dce 2021-09-24 thomas
7542 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7543 10604dce 2021-09-24 thomas if (err)
7544 10604dce 2021-09-24 thomas goto done;
7545 10604dce 2021-09-24 thomas
7546 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7547 10604dce 2021-09-24 thomas if (err)
7548 10604dce 2021-09-24 thomas goto done;
7549 10604dce 2021-09-24 thomas
7550 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7551 10604dce 2021-09-24 thomas if (err)
7552 10604dce 2021-09-24 thomas goto done;
7553 10604dce 2021-09-24 thomas
7554 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7555 10604dce 2021-09-24 thomas &have_staged_files);
7556 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7557 10604dce 2021-09-24 thomas goto done;
7558 10604dce 2021-09-24 thomas if (have_staged_files) {
7559 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7560 10604dce 2021-09-24 thomas goto done;
7561 10604dce 2021-09-24 thomas }
7562 10604dce 2021-09-24 thomas
7563 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7564 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7565 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7566 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7567 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7568 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7569 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7570 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7571 10604dce 2021-09-24 thomas if (err)
7572 10604dce 2021-09-24 thomas goto done;
7573 10604dce 2021-09-24 thomas
7574 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7575 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7576 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7577 10604dce 2021-09-24 thomas goto done;
7578 10604dce 2021-09-24 thomas }
7579 10604dce 2021-09-24 thomas
7580 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7581 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7582 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7583 10604dce 2021-09-24 thomas
7584 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7585 10604dce 2021-09-24 thomas ct_path++;
7586 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7587 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7588 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7589 10604dce 2021-09-24 thomas if (err)
7590 10604dce 2021-09-24 thomas goto done;
7591 10604dce 2021-09-24 thomas
7592 10604dce 2021-09-24 thomas }
7593 10604dce 2021-09-24 thomas
7594 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7595 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7596 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7597 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7598 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7599 10604dce 2021-09-24 thomas if (err)
7600 10604dce 2021-09-24 thomas goto done;
7601 10604dce 2021-09-24 thomas
7602 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7603 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7604 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7605 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7606 10604dce 2021-09-24 thomas err = sync_err;
7607 10604dce 2021-09-24 thomas done:
7608 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7609 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7610 10604dce 2021-09-24 thomas free_commitable(ct);
7611 10604dce 2021-09-24 thomas }
7612 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7613 10604dce 2021-09-24 thomas free(fileindex_path);
7614 10604dce 2021-09-24 thomas return err;
7615 10604dce 2021-09-24 thomas }
7616 10604dce 2021-09-24 thomas
7617 10604dce 2021-09-24 thomas const struct got_error *
7618 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7619 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7620 10604dce 2021-09-24 thomas {
7621 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7622 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7623 10604dce 2021-09-24 thomas
7624 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7625 10604dce 2021-09-24 thomas if (err)
7626 10604dce 2021-09-24 thomas goto done;
7627 10604dce 2021-09-24 thomas
7628 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7629 10604dce 2021-09-24 thomas if (err)
7630 10604dce 2021-09-24 thomas goto done;
7631 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7632 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7633 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7634 10604dce 2021-09-24 thomas err = sync_err;
7635 10604dce 2021-09-24 thomas done:
7636 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7637 10604dce 2021-09-24 thomas free(fileindex_path);
7638 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7639 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7640 10604dce 2021-09-24 thomas err = unlockerr;
7641 10604dce 2021-09-24 thomas return err;
7642 10604dce 2021-09-24 thomas }
7643 10604dce 2021-09-24 thomas
7644 10604dce 2021-09-24 thomas const struct got_error *
7645 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7646 10604dce 2021-09-24 thomas struct got_repository *repo)
7647 10604dce 2021-09-24 thomas {
7648 10604dce 2021-09-24 thomas const struct got_error *err;
7649 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7650 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7651 10604dce 2021-09-24 thomas
7652 10604dce 2021-09-24 thomas *in_progress = 0;
7653 10604dce 2021-09-24 thomas
7654 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7655 10604dce 2021-09-24 thomas if (err)
7656 10604dce 2021-09-24 thomas return err;
7657 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7658 dfe86d1f 2021-09-24 thomas free(branch_refname);
7659 10604dce 2021-09-24 thomas if (err) {
7660 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7661 10604dce 2021-09-24 thomas return err;
7662 10604dce 2021-09-24 thomas } else
7663 10604dce 2021-09-24 thomas *in_progress = 1;
7664 10604dce 2021-09-24 thomas
7665 10604dce 2021-09-24 thomas return NULL;
7666 10604dce 2021-09-24 thomas }
7667 10604dce 2021-09-24 thomas
7668 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7669 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7670 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7671 10604dce 2021-09-24 thomas {
7672 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7673 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7674 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7675 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7676 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7677 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7678 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7679 10604dce 2021-09-24 thomas
7680 10604dce 2021-09-24 thomas *fileindex = NULL;
7681 10604dce 2021-09-24 thomas
7682 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7683 10604dce 2021-09-24 thomas if (err)
7684 10604dce 2021-09-24 thomas return err;
7685 10604dce 2021-09-24 thomas
7686 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
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 /* Preconditions are the same as for rebase. */
7691 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7692 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7693 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7694 10604dce 2021-09-24 thomas &ok_arg);
7695 10604dce 2021-09-24 thomas if (err)
7696 10604dce 2021-09-24 thomas goto done;
7697 10604dce 2021-09-24 thomas
7698 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7699 10604dce 2021-09-24 thomas if (err)
7700 10604dce 2021-09-24 thomas return err;
7701 10604dce 2021-09-24 thomas
7702 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7703 10604dce 2021-09-24 thomas if (err)
7704 10604dce 2021-09-24 thomas return err;
7705 10604dce 2021-09-24 thomas
7706 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7707 10604dce 2021-09-24 thomas 0);
7708 10604dce 2021-09-24 thomas if (err)
7709 10604dce 2021-09-24 thomas goto done;
7710 10604dce 2021-09-24 thomas
7711 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7712 10604dce 2021-09-24 thomas if (err)
7713 10604dce 2021-09-24 thomas goto done;
7714 10604dce 2021-09-24 thomas
7715 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7716 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7717 10604dce 2021-09-24 thomas goto done;
7718 10604dce 2021-09-24 thomas }
7719 10604dce 2021-09-24 thomas
7720 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7721 10604dce 2021-09-24 thomas if (err)
7722 10604dce 2021-09-24 thomas goto done;
7723 10604dce 2021-09-24 thomas
7724 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7725 10604dce 2021-09-24 thomas if (err)
7726 10604dce 2021-09-24 thomas goto done;
7727 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7728 10604dce 2021-09-24 thomas if (err)
7729 10604dce 2021-09-24 thomas goto done;
7730 10604dce 2021-09-24 thomas
7731 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7732 10604dce 2021-09-24 thomas if (err)
7733 10604dce 2021-09-24 thomas goto done;
7734 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7735 10604dce 2021-09-24 thomas if (err)
7736 10604dce 2021-09-24 thomas goto done;
7737 10604dce 2021-09-24 thomas
7738 10604dce 2021-09-24 thomas done:
7739 10604dce 2021-09-24 thomas free(branch_refname);
7740 10604dce 2021-09-24 thomas free(commit_refname);
7741 10604dce 2021-09-24 thomas free(fileindex_path);
7742 10604dce 2021-09-24 thomas if (branch_ref)
7743 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7744 10604dce 2021-09-24 thomas if (commit_ref)
7745 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7746 10604dce 2021-09-24 thomas if (wt_branch)
7747 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7748 10604dce 2021-09-24 thomas free(wt_branch_tip);
7749 10604dce 2021-09-24 thomas if (err) {
7750 10604dce 2021-09-24 thomas if (*fileindex) {
7751 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7752 10604dce 2021-09-24 thomas *fileindex = NULL;
7753 10604dce 2021-09-24 thomas }
7754 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7755 10604dce 2021-09-24 thomas }
7756 10604dce 2021-09-24 thomas return err;
7757 10604dce 2021-09-24 thomas }
7758 10604dce 2021-09-24 thomas
7759 10604dce 2021-09-24 thomas const struct got_error *
7760 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7761 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7762 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7763 10604dce 2021-09-24 thomas {
7764 10604dce 2021-09-24 thomas const struct got_error *err;
7765 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7766 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7767 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7768 10604dce 2021-09-24 thomas int have_staged_files = 0;
7769 10604dce 2021-09-24 thomas
7770 10604dce 2021-09-24 thomas *branch_name = NULL;
7771 10604dce 2021-09-24 thomas *branch_tip = NULL;
7772 10604dce 2021-09-24 thomas *fileindex = NULL;
7773 10604dce 2021-09-24 thomas
7774 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7775 10604dce 2021-09-24 thomas if (err)
7776 10604dce 2021-09-24 thomas return err;
7777 10604dce 2021-09-24 thomas
7778 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7779 10604dce 2021-09-24 thomas if (err)
7780 10604dce 2021-09-24 thomas goto done;
7781 10604dce 2021-09-24 thomas
7782 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7783 10604dce 2021-09-24 thomas &have_staged_files);
7784 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7785 10604dce 2021-09-24 thomas goto done;
7786 10604dce 2021-09-24 thomas if (have_staged_files) {
7787 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7788 10604dce 2021-09-24 thomas goto done;
7789 10604dce 2021-09-24 thomas }
7790 10604dce 2021-09-24 thomas
7791 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7792 10604dce 2021-09-24 thomas if (err)
7793 10604dce 2021-09-24 thomas goto done;
7794 10604dce 2021-09-24 thomas
7795 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7796 10604dce 2021-09-24 thomas if (err)
7797 10604dce 2021-09-24 thomas goto done;
7798 10604dce 2021-09-24 thomas
7799 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7800 10604dce 2021-09-24 thomas if (err)
7801 10604dce 2021-09-24 thomas goto done;
7802 10604dce 2021-09-24 thomas
7803 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7804 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7805 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7806 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7807 10604dce 2021-09-24 thomas goto done;
7808 10604dce 2021-09-24 thomas }
7809 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7810 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7811 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7812 10604dce 2021-09-24 thomas goto done;
7813 10604dce 2021-09-24 thomas }
7814 10604dce 2021-09-24 thomas
7815 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7816 10604dce 2021-09-24 thomas if (err)
7817 10604dce 2021-09-24 thomas goto done;
7818 10604dce 2021-09-24 thomas
7819 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7820 10604dce 2021-09-24 thomas if (err)
7821 10604dce 2021-09-24 thomas goto done;
7822 10604dce 2021-09-24 thomas done:
7823 10604dce 2021-09-24 thomas free(commit_refname);
7824 10604dce 2021-09-24 thomas free(branch_refname);
7825 10604dce 2021-09-24 thomas free(fileindex_path);
7826 10604dce 2021-09-24 thomas if (commit_ref)
7827 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7828 10604dce 2021-09-24 thomas if (branch_ref)
7829 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7830 10604dce 2021-09-24 thomas if (err) {
7831 10604dce 2021-09-24 thomas if (*branch_name) {
7832 10604dce 2021-09-24 thomas free(*branch_name);
7833 10604dce 2021-09-24 thomas *branch_name = NULL;
7834 10604dce 2021-09-24 thomas }
7835 10604dce 2021-09-24 thomas free(*branch_tip);
7836 10604dce 2021-09-24 thomas *branch_tip = NULL;
7837 10604dce 2021-09-24 thomas if (*fileindex) {
7838 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7839 10604dce 2021-09-24 thomas *fileindex = NULL;
7840 10604dce 2021-09-24 thomas }
7841 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7842 10604dce 2021-09-24 thomas }
7843 10604dce 2021-09-24 thomas return err;
7844 10604dce 2021-09-24 thomas }
7845 10604dce 2021-09-24 thomas
7846 10604dce 2021-09-24 thomas const struct got_error *
7847 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7848 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7849 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7850 10604dce 2021-09-24 thomas {
7851 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7852 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7853 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7854 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7855 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7856 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7857 10604dce 2021-09-24 thomas
7858 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7859 945f9229 2022-04-16 thomas worktree->base_commit_id);
7860 945f9229 2022-04-16 thomas if (err)
7861 945f9229 2022-04-16 thomas goto done;
7862 945f9229 2022-04-16 thomas
7863 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7864 945f9229 2022-04-16 thomas worktree->path_prefix);
7865 10604dce 2021-09-24 thomas if (err)
7866 10604dce 2021-09-24 thomas goto done;
7867 10604dce 2021-09-24 thomas
7868 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7869 10604dce 2021-09-24 thomas if (err)
7870 10604dce 2021-09-24 thomas goto done;
7871 10604dce 2021-09-24 thomas
7872 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7873 10604dce 2021-09-24 thomas if (err)
7874 10604dce 2021-09-24 thomas goto done;
7875 10604dce 2021-09-24 thomas
7876 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7877 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7878 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7879 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7880 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7881 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7882 10604dce 2021-09-24 thomas rfa.repo = repo;
7883 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7884 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7885 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7886 10604dce 2021-09-24 thomas if (err)
7887 10604dce 2021-09-24 thomas goto sync;
7888 10604dce 2021-09-24 thomas
7889 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7890 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7891 10604dce 2021-09-24 thomas sync:
7892 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7893 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7894 10604dce 2021-09-24 thomas err = sync_err;
7895 10604dce 2021-09-24 thomas done:
7896 10604dce 2021-09-24 thomas free(tree_id);
7897 10604dce 2021-09-24 thomas free(commit_id);
7898 945f9229 2022-04-16 thomas if (commit)
7899 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7900 10604dce 2021-09-24 thomas if (fileindex)
7901 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7902 10604dce 2021-09-24 thomas free(fileindex_path);
7903 10604dce 2021-09-24 thomas
7904 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7905 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7906 10604dce 2021-09-24 thomas err = unlockerr;
7907 10604dce 2021-09-24 thomas return err;
7908 10604dce 2021-09-24 thomas }
7909 10604dce 2021-09-24 thomas
7910 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7911 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7912 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7913 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7914 2db2652d 2019-08-07 stsp struct got_repository *repo;
7915 2db2652d 2019-08-07 stsp int have_changes;
7916 2db2652d 2019-08-07 stsp };
7917 2db2652d 2019-08-07 stsp
7918 ef20f542 2022-06-26 thomas static const struct got_error *
7919 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7920 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7921 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7922 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7923 735ef5ac 2019-08-03 stsp {
7924 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7925 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7926 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7927 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7928 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7929 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7930 8b13ce36 2019-08-08 stsp
7931 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7932 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7933 8b13ce36 2019-08-08 stsp return NULL;
7934 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7935 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7936 735ef5ac 2019-08-03 stsp
7937 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7938 735ef5ac 2019-08-03 stsp if (ie == NULL)
7939 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7940 735ef5ac 2019-08-03 stsp
7941 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7942 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7943 735ef5ac 2019-08-03 stsp relpath) == -1)
7944 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7945 735ef5ac 2019-08-03 stsp
7946 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7947 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7948 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7949 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7950 735ef5ac 2019-08-03 stsp }
7951 735ef5ac 2019-08-03 stsp
7952 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7953 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7954 3aa5969e 2019-08-06 stsp goto done;
7955 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7956 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7957 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7958 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7959 735ef5ac 2019-08-03 stsp goto done;
7960 3aa5969e 2019-08-06 stsp }
7961 735ef5ac 2019-08-03 stsp
7962 2db2652d 2019-08-07 stsp a->have_changes = 1;
7963 2db2652d 2019-08-07 stsp
7964 735ef5ac 2019-08-03 stsp p = in_repo_path;
7965 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7966 735ef5ac 2019-08-03 stsp p++;
7967 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7968 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7969 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7970 735ef5ac 2019-08-03 stsp done:
7971 735ef5ac 2019-08-03 stsp free(in_repo_path);
7972 dc424a06 2019-08-07 stsp return err;
7973 dc424a06 2019-08-07 stsp }
7974 dc424a06 2019-08-07 stsp
7975 2db2652d 2019-08-07 stsp struct stage_path_arg {
7976 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7977 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7978 2db2652d 2019-08-07 stsp struct got_repository *repo;
7979 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7980 2db2652d 2019-08-07 stsp void *status_arg;
7981 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7982 2db2652d 2019-08-07 stsp void *patch_arg;
7983 7b5dc508 2019-10-28 stsp int staged_something;
7984 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7985 2db2652d 2019-08-07 stsp };
7986 2db2652d 2019-08-07 stsp
7987 2db2652d 2019-08-07 stsp static const struct got_error *
7988 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7989 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7990 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7991 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7992 0cb83759 2019-08-03 stsp {
7993 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7994 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7995 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7996 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7997 0cb83759 2019-08-03 stsp uint32_t stage;
7998 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7999 0aeb8099 2020-07-23 stsp struct stat sb;
8000 8b13ce36 2019-08-08 stsp
8001 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8002 8b13ce36 2019-08-08 stsp return NULL;
8003 0cb83759 2019-08-03 stsp
8004 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8005 d3e7c587 2019-08-03 stsp if (ie == NULL)
8006 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8007 0cb83759 2019-08-03 stsp
8008 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8009 2db2652d 2019-08-07 stsp relpath)== -1)
8010 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8011 0cb83759 2019-08-03 stsp
8012 0cb83759 2019-08-03 stsp switch (status) {
8013 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8014 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8015 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8016 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8017 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8018 0aeb8099 2020-07-23 stsp break;
8019 0aeb8099 2020-07-23 stsp }
8020 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8021 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8022 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8023 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8024 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8025 dc424a06 2019-08-07 stsp if (err)
8026 dc424a06 2019-08-07 stsp break;
8027 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8028 dc424a06 2019-08-07 stsp break;
8029 dc424a06 2019-08-07 stsp } else {
8030 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8031 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8032 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8033 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8034 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8035 dc424a06 2019-08-07 stsp break;
8036 dc424a06 2019-08-07 stsp }
8037 dc424a06 2019-08-07 stsp }
8038 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8039 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8040 0cb83759 2019-08-03 stsp if (err)
8041 dc424a06 2019-08-07 stsp break;
8042 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8043 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8044 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8045 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8046 0cb83759 2019-08-03 stsp else
8047 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8048 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8049 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8050 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8051 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8052 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8053 35213c7c 2020-07-23 stsp ssize_t target_len;
8054 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8055 35213c7c 2020-07-23 stsp sizeof(target_path));
8056 35213c7c 2020-07-23 stsp if (target_len == -1) {
8057 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8058 35213c7c 2020-07-23 stsp ondisk_path);
8059 35213c7c 2020-07-23 stsp break;
8060 35213c7c 2020-07-23 stsp }
8061 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8062 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8063 35213c7c 2020-07-23 stsp a->worktree->root_path);
8064 35213c7c 2020-07-23 stsp if (err)
8065 35213c7c 2020-07-23 stsp break;
8066 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8067 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8068 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8069 35213c7c 2020-07-23 stsp break;
8070 35213c7c 2020-07-23 stsp }
8071 35213c7c 2020-07-23 stsp }
8072 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8073 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8074 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8075 35213c7c 2020-07-23 stsp else
8076 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8077 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8078 0aeb8099 2020-07-23 stsp } else {
8079 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8080 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8081 0aeb8099 2020-07-23 stsp }
8082 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8083 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8084 dc424a06 2019-08-07 stsp break;
8085 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8086 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8087 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8088 f289c82c 2022-06-13 thomas if (err)
8089 f289c82c 2022-06-13 thomas break;
8090 f289c82c 2022-06-13 thomas /*
8091 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8092 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8093 f289c82c 2022-06-13 thomas */
8094 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8095 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8096 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8097 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8098 f289c82c 2022-06-13 thomas }
8099 0cb83759 2019-08-03 stsp break;
8100 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8101 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8102 d3e7c587 2019-08-03 stsp break;
8103 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8104 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8105 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8106 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8107 dc424a06 2019-08-07 stsp if (err)
8108 dc424a06 2019-08-07 stsp break;
8109 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8110 88f33a19 2019-08-08 stsp break;
8111 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8112 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8113 dc424a06 2019-08-07 stsp break;
8114 88f33a19 2019-08-08 stsp }
8115 dc424a06 2019-08-07 stsp }
8116 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8117 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8118 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8119 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8120 dc424a06 2019-08-07 stsp break;
8121 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8122 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8123 12463d8b 2019-12-13 stsp de_name);
8124 0cb83759 2019-08-03 stsp break;
8125 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8126 d3e7c587 2019-08-03 stsp break;
8127 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8128 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8129 ebf48fd5 2019-08-03 stsp break;
8130 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8131 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8132 2a06fe5f 2019-08-24 stsp break;
8133 0cb83759 2019-08-03 stsp default:
8134 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8135 537ac44b 2019-08-03 stsp break;
8136 0cb83759 2019-08-03 stsp }
8137 dc424a06 2019-08-07 stsp
8138 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8139 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8140 dc424a06 2019-08-07 stsp free(path_content);
8141 2db2652d 2019-08-07 stsp free(ondisk_path);
8142 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8143 0ebf8283 2019-07-24 stsp return err;
8144 0ebf8283 2019-07-24 stsp }
8145 0cb83759 2019-08-03 stsp
8146 0cb83759 2019-08-03 stsp const struct got_error *
8147 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8148 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8149 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8150 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8151 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8152 0cb83759 2019-08-03 stsp {
8153 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8154 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8155 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8156 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8157 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8158 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8159 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8160 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8161 0cb83759 2019-08-03 stsp
8162 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8163 0cb83759 2019-08-03 stsp if (err)
8164 0cb83759 2019-08-03 stsp return err;
8165 0cb83759 2019-08-03 stsp
8166 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8167 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8168 735ef5ac 2019-08-03 stsp if (err)
8169 735ef5ac 2019-08-03 stsp goto done;
8170 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8171 735ef5ac 2019-08-03 stsp if (err)
8172 735ef5ac 2019-08-03 stsp goto done;
8173 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8174 0cb83759 2019-08-03 stsp if (err)
8175 0cb83759 2019-08-03 stsp goto done;
8176 0cb83759 2019-08-03 stsp
8177 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8178 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8179 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8180 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8181 2db2652d 2019-08-07 stsp oka.repo = repo;
8182 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8183 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8184 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8185 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8186 735ef5ac 2019-08-03 stsp if (err)
8187 735ef5ac 2019-08-03 stsp goto done;
8188 735ef5ac 2019-08-03 stsp }
8189 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8190 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8191 2db2652d 2019-08-07 stsp goto done;
8192 2db2652d 2019-08-07 stsp }
8193 735ef5ac 2019-08-03 stsp
8194 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8195 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8196 2db2652d 2019-08-07 stsp spa.repo = repo;
8197 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8198 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8199 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8200 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8201 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8202 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8203 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8204 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8205 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8206 42005733 2019-08-03 stsp if (err)
8207 2db2652d 2019-08-07 stsp goto done;
8208 7b5dc508 2019-10-28 stsp }
8209 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8210 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8211 7b5dc508 2019-10-28 stsp goto done;
8212 0cb83759 2019-08-03 stsp }
8213 0cb83759 2019-08-03 stsp
8214 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8215 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8216 0cb83759 2019-08-03 stsp err = sync_err;
8217 0cb83759 2019-08-03 stsp done:
8218 735ef5ac 2019-08-03 stsp if (head_ref)
8219 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8220 735ef5ac 2019-08-03 stsp free(head_commit_id);
8221 0cb83759 2019-08-03 stsp free(fileindex_path);
8222 0cb83759 2019-08-03 stsp if (fileindex)
8223 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8224 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8225 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8226 0cb83759 2019-08-03 stsp err = unlockerr;
8227 0cb83759 2019-08-03 stsp return err;
8228 0cb83759 2019-08-03 stsp }
8229 ad493afc 2019-08-03 stsp
8230 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8231 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8232 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8233 ad493afc 2019-08-03 stsp struct got_repository *repo;
8234 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8235 ad493afc 2019-08-03 stsp void *progress_arg;
8236 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8237 2e1f37b0 2019-08-08 stsp void *patch_arg;
8238 ad493afc 2019-08-03 stsp };
8239 2e1f37b0 2019-08-08 stsp
8240 2e1f37b0 2019-08-08 stsp static const struct got_error *
8241 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8242 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8243 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8244 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8245 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8246 2e1f37b0 2019-08-08 stsp {
8247 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8248 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8249 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8250 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8251 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8252 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8253 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8254 2e1f37b0 2019-08-08 stsp
8255 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8256 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8257 2e1f37b0 2019-08-08 stsp
8258 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8259 2e1f37b0 2019-08-08 stsp if (err)
8260 2e1f37b0 2019-08-08 stsp return err;
8261 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8262 2e1f37b0 2019-08-08 stsp if (err)
8263 2e1f37b0 2019-08-08 stsp goto done;
8264 2e1f37b0 2019-08-08 stsp
8265 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8266 2e1f37b0 2019-08-08 stsp if (err)
8267 2e1f37b0 2019-08-08 stsp goto done;
8268 2e1f37b0 2019-08-08 stsp
8269 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8270 2e1f37b0 2019-08-08 stsp if (err)
8271 2e1f37b0 2019-08-08 stsp goto done;
8272 2e1f37b0 2019-08-08 stsp
8273 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8274 2e1f37b0 2019-08-08 stsp if (err)
8275 2e1f37b0 2019-08-08 stsp goto done;
8276 2e1f37b0 2019-08-08 stsp
8277 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8278 2e1f37b0 2019-08-08 stsp if (err)
8279 2e1f37b0 2019-08-08 stsp goto done;
8280 ad493afc 2019-08-03 stsp
8281 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8282 2e1f37b0 2019-08-08 stsp if (err)
8283 2e1f37b0 2019-08-08 stsp goto done;
8284 2e1f37b0 2019-08-08 stsp
8285 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8286 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8287 2e1f37b0 2019-08-08 stsp if (err)
8288 2e1f37b0 2019-08-08 stsp goto done;
8289 2e1f37b0 2019-08-08 stsp
8290 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8291 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8292 2e1f37b0 2019-08-08 stsp if (err)
8293 2e1f37b0 2019-08-08 stsp goto done;
8294 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8295 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8296 2e1f37b0 2019-08-08 stsp if (err)
8297 2e1f37b0 2019-08-08 stsp goto done;
8298 2e1f37b0 2019-08-08 stsp
8299 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8300 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8301 2e1f37b0 2019-08-08 stsp goto done;
8302 2e1f37b0 2019-08-08 stsp }
8303 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8304 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8305 2e1f37b0 2019-08-08 stsp goto done;
8306 2e1f37b0 2019-08-08 stsp }
8307 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8308 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8309 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8310 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8311 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8312 fe621944 2020-11-10 stsp nchanges++;
8313 fe621944 2020-11-10 stsp }
8314 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8315 2e1f37b0 2019-08-08 stsp int choice;
8316 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8317 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8318 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8319 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8320 2e1f37b0 2019-08-08 stsp if (err)
8321 2e1f37b0 2019-08-08 stsp goto done;
8322 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8323 2e1f37b0 2019-08-08 stsp have_content = 1;
8324 19e4b907 2019-08-08 stsp else
8325 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8326 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8327 2e1f37b0 2019-08-08 stsp break;
8328 2e1f37b0 2019-08-08 stsp }
8329 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8330 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8331 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8332 2e1f37b0 2019-08-08 stsp done:
8333 2e1f37b0 2019-08-08 stsp free(label1);
8334 2e1f37b0 2019-08-08 stsp if (blob)
8335 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8336 2e1f37b0 2019-08-08 stsp if (staged_blob)
8337 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8338 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8339 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8340 fe621944 2020-11-10 stsp err = free_err;
8341 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8342 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8343 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8344 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8345 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8346 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8347 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8348 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8349 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8350 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8351 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8352 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8353 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8354 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8355 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8356 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8357 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8358 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8359 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8360 2e1f37b0 2019-08-08 stsp }
8361 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8362 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8363 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8364 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8365 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8366 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8367 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8368 2e1f37b0 2019-08-08 stsp }
8369 2e1f37b0 2019-08-08 stsp free(path1);
8370 2e1f37b0 2019-08-08 stsp free(path2);
8371 fda8017d 2020-07-23 stsp return err;
8372 fda8017d 2020-07-23 stsp }
8373 fda8017d 2020-07-23 stsp
8374 fda8017d 2020-07-23 stsp static const struct got_error *
8375 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8376 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8377 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8378 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8379 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8380 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8381 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8382 fda8017d 2020-07-23 stsp {
8383 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8384 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8385 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8386 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8387 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8388 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8389 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8390 fda8017d 2020-07-23 stsp struct stat sb;
8391 fda8017d 2020-07-23 stsp
8392 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8393 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8394 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8395 fda8017d 2020-07-23 stsp if (err)
8396 fda8017d 2020-07-23 stsp return err;
8397 fda8017d 2020-07-23 stsp
8398 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8399 fda8017d 2020-07-23 stsp return NULL;
8400 fda8017d 2020-07-23 stsp
8401 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8402 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8403 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8404 fda8017d 2020-07-23 stsp if (err)
8405 fda8017d 2020-07-23 stsp goto done;
8406 fda8017d 2020-07-23 stsp }
8407 fda8017d 2020-07-23 stsp
8408 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8409 fda8017d 2020-07-23 stsp if (f == NULL) {
8410 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8411 fda8017d 2020-07-23 stsp path_unstaged_content);
8412 fda8017d 2020-07-23 stsp goto done;
8413 fda8017d 2020-07-23 stsp }
8414 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8415 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8416 fda8017d 2020-07-23 stsp goto done;
8417 fda8017d 2020-07-23 stsp }
8418 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8419 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8420 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8421 fda8017d 2020-07-23 stsp size_t r;
8422 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8423 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8424 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8425 fda8017d 2020-07-23 stsp goto done;
8426 fda8017d 2020-07-23 stsp }
8427 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8428 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8429 fda8017d 2020-07-23 stsp goto done;
8430 fda8017d 2020-07-23 stsp }
8431 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8432 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8433 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8434 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8435 fda8017d 2020-07-23 stsp progress_arg);
8436 fda8017d 2020-07-23 stsp } else {
8437 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8438 67a66647 2021-05-31 stsp
8439 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8440 67a66647 2021-05-31 stsp if (err)
8441 67a66647 2021-05-31 stsp return err;
8442 67a66647 2021-05-31 stsp
8443 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8444 67a66647 2021-05-31 stsp parent) == -1) {
8445 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8446 67a66647 2021-05-31 stsp base_path = NULL;
8447 67a66647 2021-05-31 stsp goto done;
8448 67a66647 2021-05-31 stsp }
8449 67a66647 2021-05-31 stsp
8450 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8451 67a66647 2021-05-31 stsp if (err)
8452 67a66647 2021-05-31 stsp goto done;
8453 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8454 67a66647 2021-05-31 stsp blob_base);
8455 67a66647 2021-05-31 stsp if (err)
8456 67a66647 2021-05-31 stsp goto done;
8457 67a66647 2021-05-31 stsp
8458 eec2f5a9 2021-06-03 stsp /*
8459 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8460 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8461 eec2f5a9 2021-06-03 stsp */
8462 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8463 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8464 eec2f5a9 2021-06-03 stsp ondisk_path);
8465 eec2f5a9 2021-06-03 stsp if (err)
8466 eec2f5a9 2021-06-03 stsp goto done;
8467 eec2f5a9 2021-06-03 stsp } else {
8468 eec2f5a9 2021-06-03 stsp int fd;
8469 06340621 2021-12-31 thomas fd = open(ondisk_path,
8470 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8471 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8472 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8473 eec2f5a9 2021-06-03 stsp goto done;
8474 eec2f5a9 2021-06-03 stsp }
8475 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8476 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8477 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8478 eec2f5a9 2021-06-03 stsp close(fd);
8479 eec2f5a9 2021-06-03 stsp goto done;
8480 eec2f5a9 2021-06-03 stsp }
8481 eec2f5a9 2021-06-03 stsp }
8482 eec2f5a9 2021-06-03 stsp
8483 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8484 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8485 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8486 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8487 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8488 fda8017d 2020-07-23 stsp }
8489 fda8017d 2020-07-23 stsp if (err)
8490 fda8017d 2020-07-23 stsp goto done;
8491 fda8017d 2020-07-23 stsp
8492 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8493 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8494 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8495 2ac8aa02 2020-11-09 stsp } else {
8496 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8497 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8498 2ac8aa02 2020-11-09 stsp }
8499 fda8017d 2020-07-23 stsp done:
8500 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8501 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8502 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8503 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8504 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8505 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8506 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8507 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8508 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8509 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8510 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8511 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8512 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8513 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8514 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8515 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8516 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8517 67a66647 2021-05-31 stsp free(blob_base_path);
8518 67a66647 2021-05-31 stsp free(parent);
8519 67a66647 2021-05-31 stsp free(base_path);
8520 2e1f37b0 2019-08-08 stsp return err;
8521 2e1f37b0 2019-08-08 stsp }
8522 2e1f37b0 2019-08-08 stsp
8523 ad493afc 2019-08-03 stsp static const struct got_error *
8524 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8525 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8526 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8527 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8528 ad493afc 2019-08-03 stsp {
8529 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8530 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8531 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8532 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8533 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8534 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8535 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8536 9bc94a15 2019-08-03 stsp struct stat sb;
8537 ad493afc 2019-08-03 stsp
8538 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8539 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8540 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8541 2e1f37b0 2019-08-08 stsp return NULL;
8542 2e1f37b0 2019-08-08 stsp
8543 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8544 ad493afc 2019-08-03 stsp if (ie == NULL)
8545 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8546 9bc94a15 2019-08-03 stsp
8547 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8548 9bc94a15 2019-08-03 stsp == -1)
8549 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8550 ad493afc 2019-08-03 stsp
8551 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8552 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8553 f69721c3 2019-10-21 stsp if (err)
8554 f69721c3 2019-10-21 stsp goto done;
8555 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8556 f69721c3 2019-10-21 stsp id_str) == -1) {
8557 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8558 f69721c3 2019-10-21 stsp goto done;
8559 f69721c3 2019-10-21 stsp }
8560 f69721c3 2019-10-21 stsp
8561 ad493afc 2019-08-03 stsp switch (staged_status) {
8562 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8563 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8564 ad493afc 2019-08-03 stsp blob_id, 8192);
8565 ad493afc 2019-08-03 stsp if (err)
8566 ad493afc 2019-08-03 stsp break;
8567 ad493afc 2019-08-03 stsp /* fall through */
8568 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8569 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8570 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8571 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8572 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8573 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8574 2e1f37b0 2019-08-08 stsp if (err)
8575 2e1f37b0 2019-08-08 stsp break;
8576 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8577 2e1f37b0 2019-08-08 stsp break;
8578 2e1f37b0 2019-08-08 stsp } else {
8579 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8580 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8581 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8582 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8583 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8584 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8585 2e1f37b0 2019-08-08 stsp }
8586 2e1f37b0 2019-08-08 stsp }
8587 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8588 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8589 ad493afc 2019-08-03 stsp if (err)
8590 ad493afc 2019-08-03 stsp break;
8591 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8592 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8593 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8594 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8595 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8596 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8597 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8598 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8599 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8600 ea7786be 2020-07-23 stsp break;
8601 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8602 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8603 36bf999c 2020-07-23 stsp char *staged_target;
8604 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8605 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8606 36bf999c 2020-07-23 stsp if (err)
8607 36bf999c 2020-07-23 stsp goto done;
8608 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8609 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8610 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8611 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8612 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8613 36bf999c 2020-07-23 stsp free(staged_target);
8614 dfe9fba0 2020-07-23 stsp } else {
8615 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8616 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8617 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8618 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8619 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8620 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8621 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8622 dfe9fba0 2020-07-23 stsp }
8623 ea7786be 2020-07-23 stsp break;
8624 ea7786be 2020-07-23 stsp default:
8625 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8626 ea7786be 2020-07-23 stsp break;
8627 ea7786be 2020-07-23 stsp }
8628 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8629 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8630 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8631 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8632 2ac8aa02 2020-11-09 stsp }
8633 ad493afc 2019-08-03 stsp break;
8634 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8635 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8636 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8637 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8638 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8639 2e1f37b0 2019-08-08 stsp if (err)
8640 2e1f37b0 2019-08-08 stsp break;
8641 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8642 2e1f37b0 2019-08-08 stsp break;
8643 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8644 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8645 2e1f37b0 2019-08-08 stsp break;
8646 2e1f37b0 2019-08-08 stsp }
8647 2e1f37b0 2019-08-08 stsp }
8648 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8649 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8650 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8651 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8652 9bc94a15 2019-08-03 stsp if (err)
8653 9bc94a15 2019-08-03 stsp break;
8654 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8655 ad493afc 2019-08-03 stsp break;
8656 ad493afc 2019-08-03 stsp }
8657 f69721c3 2019-10-21 stsp done:
8658 ad493afc 2019-08-03 stsp free(ondisk_path);
8659 ad493afc 2019-08-03 stsp if (blob_base)
8660 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8661 ad493afc 2019-08-03 stsp if (blob_staged)
8662 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8663 f69721c3 2019-10-21 stsp free(id_str);
8664 f69721c3 2019-10-21 stsp free(label_orig);
8665 ad493afc 2019-08-03 stsp return err;
8666 ad493afc 2019-08-03 stsp }
8667 ad493afc 2019-08-03 stsp
8668 ad493afc 2019-08-03 stsp const struct got_error *
8669 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8670 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8671 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8672 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8673 ad493afc 2019-08-03 stsp struct got_repository *repo)
8674 ad493afc 2019-08-03 stsp {
8675 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8676 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8677 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8678 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8679 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8680 ad493afc 2019-08-03 stsp
8681 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8682 ad493afc 2019-08-03 stsp if (err)
8683 ad493afc 2019-08-03 stsp return err;
8684 ad493afc 2019-08-03 stsp
8685 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8686 ad493afc 2019-08-03 stsp if (err)
8687 ad493afc 2019-08-03 stsp goto done;
8688 ad493afc 2019-08-03 stsp
8689 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8690 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8691 ad493afc 2019-08-03 stsp upa.repo = repo;
8692 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8693 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8694 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8695 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8696 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8697 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8698 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8699 ad493afc 2019-08-03 stsp if (err)
8700 ad493afc 2019-08-03 stsp goto done;
8701 ad493afc 2019-08-03 stsp }
8702 ad493afc 2019-08-03 stsp
8703 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8704 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8705 ad493afc 2019-08-03 stsp err = sync_err;
8706 ad493afc 2019-08-03 stsp done:
8707 ad493afc 2019-08-03 stsp free(fileindex_path);
8708 ad493afc 2019-08-03 stsp if (fileindex)
8709 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8710 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8711 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8712 ad493afc 2019-08-03 stsp err = unlockerr;
8713 ad493afc 2019-08-03 stsp return err;
8714 ad493afc 2019-08-03 stsp }
8715 b2118c49 2020-07-28 stsp
8716 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8717 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8718 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8719 b2118c49 2020-07-28 stsp void *info_arg;
8720 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8721 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8722 b2118c49 2020-07-28 stsp void *cancel_arg;
8723 b2118c49 2020-07-28 stsp };
8724 b2118c49 2020-07-28 stsp
8725 b2118c49 2020-07-28 stsp static const struct got_error *
8726 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8727 b2118c49 2020-07-28 stsp {
8728 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8729 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8730 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8731 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8732 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8733 b2118c49 2020-07-28 stsp int stage;
8734 b2118c49 2020-07-28 stsp
8735 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8736 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8737 b2118c49 2020-07-28 stsp
8738 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8739 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8740 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8741 b2118c49 2020-07-28 stsp break;
8742 b2118c49 2020-07-28 stsp }
8743 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8744 b2118c49 2020-07-28 stsp return NULL;
8745 b2118c49 2020-07-28 stsp
8746 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8747 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8748 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8749 b2118c49 2020-07-28 stsp }
8750 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8751 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8752 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8753 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8754 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8755 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8756 b2118c49 2020-07-28 stsp }
8757 b2118c49 2020-07-28 stsp
8758 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8759 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8760 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8761 b2118c49 2020-07-28 stsp }
8762 b2118c49 2020-07-28 stsp
8763 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8764 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8765 b2118c49 2020-07-28 stsp }
8766 b2118c49 2020-07-28 stsp
8767 b2118c49 2020-07-28 stsp const struct got_error *
8768 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8769 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8770 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8771 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8772 b2118c49 2020-07-28 stsp
8773 b2118c49 2020-07-28 stsp {
8774 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8775 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8776 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8777 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8778 b2118c49 2020-07-28 stsp
8779 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8780 b2118c49 2020-07-28 stsp if (err)
8781 b2118c49 2020-07-28 stsp return err;
8782 b2118c49 2020-07-28 stsp
8783 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8784 b2118c49 2020-07-28 stsp if (err)
8785 b2118c49 2020-07-28 stsp goto done;
8786 b2118c49 2020-07-28 stsp
8787 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8788 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8789 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8790 b2118c49 2020-07-28 stsp arg.paths = paths;
8791 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8792 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8793 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8794 b2118c49 2020-07-28 stsp &arg);
8795 b2118c49 2020-07-28 stsp done:
8796 b2118c49 2020-07-28 stsp free(fileindex_path);
8797 b2118c49 2020-07-28 stsp if (fileindex)
8798 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8799 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8800 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8801 b2118c49 2020-07-28 stsp err = unlockerr;
8802 b2118c49 2020-07-28 stsp return err;
8803 b2118c49 2020-07-28 stsp }
8804 814624e7 2022-03-22 thomas
8805 814624e7 2022-03-22 thomas static const struct got_error *
8806 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
8807 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
8808 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
8809 814624e7 2022-03-22 thomas {
8810 814624e7 2022-03-22 thomas const struct got_error *err;
8811 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
8812 814624e7 2022-03-22 thomas struct stat sb;
8813 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
8814 814624e7 2022-03-22 thomas
8815 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
8816 814624e7 2022-03-22 thomas if (err)
8817 814624e7 2022-03-22 thomas return err;
8818 814624e7 2022-03-22 thomas
8819 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8820 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
8821 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
8822 814624e7 2022-03-22 thomas
8823 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8824 814624e7 2022-03-22 thomas if (ie) {
8825 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
8826 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8827 12de5570 2022-05-01 thomas repo);
8828 814624e7 2022-03-22 thomas if (err)
8829 814624e7 2022-03-22 thomas goto done;
8830 814624e7 2022-03-22 thomas } else {
8831 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
8832 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
8833 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
8834 814624e7 2022-03-22 thomas if (errno != ENOENT) {
8835 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
8836 814624e7 2022-03-22 thomas ondisk_path);
8837 814624e7 2022-03-22 thomas goto done;
8838 814624e7 2022-03-22 thomas }
8839 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
8840 814624e7 2022-03-22 thomas }
8841 814624e7 2022-03-22 thomas }
8842 814624e7 2022-03-22 thomas
8843 814624e7 2022-03-22 thomas done:
8844 814624e7 2022-03-22 thomas free(ondisk_path);
8845 814624e7 2022-03-22 thomas return err;
8846 814624e7 2022-03-22 thomas }
8847 814624e7 2022-03-22 thomas
8848 814624e7 2022-03-22 thomas static const struct got_error *
8849 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
8850 814624e7 2022-03-22 thomas unsigned char staged_status)
8851 814624e7 2022-03-22 thomas {
8852 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8853 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8854 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8855 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8856 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
8857 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
8858 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8859 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8860 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8861 814624e7 2022-03-22 thomas return NULL;
8862 814624e7 2022-03-22 thomas }
8863 814624e7 2022-03-22 thomas
8864 814624e7 2022-03-22 thomas static const struct got_error *
8865 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
8866 814624e7 2022-03-22 thomas {
8867 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
8868 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8869 814624e7 2022-03-22 thomas return NULL;
8870 814624e7 2022-03-22 thomas }
8871 814624e7 2022-03-22 thomas
8872 814624e7 2022-03-22 thomas static const struct got_error *
8873 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
8874 814624e7 2022-03-22 thomas unsigned char staged_status)
8875 814624e7 2022-03-22 thomas {
8876 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8877 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8878 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8879 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8880 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
8881 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8882 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8883 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8884 814624e7 2022-03-22 thomas return NULL;
8885 814624e7 2022-03-22 thomas }
8886 814624e7 2022-03-22 thomas
8887 814624e7 2022-03-22 thomas const struct got_error *
8888 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
8889 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
8890 814624e7 2022-03-22 thomas {
8891 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
8892 814624e7 2022-03-22 thomas }
8893 814624e7 2022-03-22 thomas
8894 814624e7 2022-03-22 thomas const struct got_error *
8895 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
8896 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
8897 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
8898 814624e7 2022-03-22 thomas {
8899 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
8900 814624e7 2022-03-22 thomas int file_renamed = 0;
8901 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
8902 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
8903 814624e7 2022-03-22 thomas
8904 814624e7 2022-03-22 thomas *oldpath = NULL;
8905 814624e7 2022-03-22 thomas *newpath = NULL;
8906 814624e7 2022-03-22 thomas
8907 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
8908 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
8909 814624e7 2022-03-22 thomas if (err)
8910 814624e7 2022-03-22 thomas goto done;
8911 814624e7 2022-03-22 thomas
8912 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
8913 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
8914 814624e7 2022-03-22 thomas if (err)
8915 814624e7 2022-03-22 thomas goto done;
8916 814624e7 2022-03-22 thomas
8917 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
8918 814624e7 2022-03-22 thomas file_renamed = 1;
8919 814624e7 2022-03-22 thomas
8920 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
8921 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
8922 814624e7 2022-03-22 thomas else if (file_renamed) {
8923 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
8924 814624e7 2022-03-22 thomas if (err == NULL)
8925 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
8926 814624e7 2022-03-22 thomas } else if (old == NULL)
8927 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
8928 814624e7 2022-03-22 thomas else
8929 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
8930 814624e7 2022-03-22 thomas
8931 814624e7 2022-03-22 thomas done:
8932 814624e7 2022-03-22 thomas if (err) {
8933 814624e7 2022-03-22 thomas free(*oldpath);
8934 814624e7 2022-03-22 thomas *oldpath = NULL;
8935 814624e7 2022-03-22 thomas free(*newpath);
8936 814624e7 2022-03-22 thomas *newpath = NULL;
8937 814624e7 2022-03-22 thomas }
8938 814624e7 2022-03-22 thomas return err;
8939 814624e7 2022-03-22 thomas }
8940 814624e7 2022-03-22 thomas
8941 814624e7 2022-03-22 thomas const struct got_error *
8942 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
8943 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8944 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8945 814624e7 2022-03-22 thomas {
8946 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
8947 5e22b737 2022-05-31 thomas
8948 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
8949 5e22b737 2022-05-31 thomas saa.worktree = worktree;
8950 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
8951 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
8952 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
8953 5e22b737 2022-05-31 thomas saa.repo = repo;
8954 5e22b737 2022-05-31 thomas
8955 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
8956 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
8957 814624e7 2022-03-22 thomas }
8958 5e22b737 2022-05-31 thomas
8959 5e22b737 2022-05-31 thomas const struct got_error *
8960 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
8961 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8962 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
8963 5e22b737 2022-05-31 thomas {
8964 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
8965 5e22b737 2022-05-31 thomas
8966 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
8967 5e22b737 2022-05-31 thomas sda.worktree = worktree;
8968 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
8969 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
8970 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
8971 5e22b737 2022-05-31 thomas sda.repo = repo;
8972 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
8973 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
8974 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
8975 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
8976 5e22b737 2022-05-31 thomas
8977 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
8978 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
8979 5e22b737 2022-05-31 thomas }
8980 5e22b737 2022-05-31 thomas
8981 5e22b737 2022-05-31 thomas const struct got_error *
8982 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
8983 36832a8e 2022-05-31 thomas const char *fileindex_path)
8984 5e22b737 2022-05-31 thomas {
8985 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
8986 5e22b737 2022-05-31 thomas
8987 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
8988 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
8989 5e22b737 2022-05-31 thomas
8990 5e22b737 2022-05-31 thomas return err;
8991 5e22b737 2022-05-31 thomas }