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 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 0e039681 2021-11-15 stsp }
478 0e039681 2021-11-15 stsp
479 0e039681 2021-11-15 stsp static const struct got_error *
480 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 0e039681 2021-11-15 stsp {
482 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
483 0e039681 2021-11-15 stsp size_t flen;
484 0e039681 2021-11-15 stsp ssize_t outlen;
485 0e039681 2021-11-15 stsp
486 0e039681 2021-11-15 stsp *outsize = 0;
487 0e039681 2021-11-15 stsp
488 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
489 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
490 0e039681 2021-11-15 stsp
491 0e039681 2021-11-15 stsp for (;;) {
492 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
493 0e039681 2021-11-15 stsp if (flen == 0) {
494 0e039681 2021-11-15 stsp if (ferror(f))
495 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
496 0e039681 2021-11-15 stsp if (feof(f))
497 0e039681 2021-11-15 stsp break;
498 0e039681 2021-11-15 stsp }
499 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
500 0e039681 2021-11-15 stsp if (outlen == -1)
501 0e039681 2021-11-15 stsp return got_error_from_errno("write");
502 0e039681 2021-11-15 stsp if (outlen != flen)
503 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
504 0e039681 2021-11-15 stsp *outsize += outlen;
505 0e039681 2021-11-15 stsp }
506 0e039681 2021-11-15 stsp
507 0e039681 2021-11-15 stsp return NULL;
508 0e039681 2021-11-15 stsp }
509 0e039681 2021-11-15 stsp
510 0e039681 2021-11-15 stsp static const struct got_error *
511 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
512 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 0e039681 2021-11-15 stsp const char *ondisk_path)
515 0e039681 2021-11-15 stsp {
516 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
517 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
518 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
522 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp
524 0e039681 2021-11-15 stsp *overlapcnt = 0;
525 0e039681 2021-11-15 stsp
526 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 0e039681 2021-11-15 stsp if (err)
528 0e039681 2021-11-15 stsp return err;
529 0e039681 2021-11-15 stsp
530 0e039681 2021-11-15 stsp if (same_content)
531 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 0e039681 2021-11-15 stsp
533 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
534 0e039681 2021-11-15 stsp if (err)
535 0e039681 2021-11-15 stsp return err;
536 0e039681 2021-11-15 stsp changed_deriv = !same_content;
537 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
538 0e039681 2021-11-15 stsp if (err)
539 0e039681 2021-11-15 stsp return err;
540 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
541 0e039681 2021-11-15 stsp
542 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
543 0e039681 2021-11-15 stsp *overlapcnt = 1;
544 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
546 0e039681 2021-11-15 stsp goto done;
547 0e039681 2021-11-15 stsp }
548 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
550 0e039681 2021-11-15 stsp goto done;
551 0e039681 2021-11-15 stsp }
552 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
554 0e039681 2021-11-15 stsp goto done;
555 0e039681 2021-11-15 stsp }
556 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 0e039681 2021-11-15 stsp base_path_orig);
558 0e039681 2021-11-15 stsp if (err)
559 0e039681 2021-11-15 stsp goto done;
560 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 0e039681 2021-11-15 stsp base_path_deriv);
562 0e039681 2021-11-15 stsp if (err)
563 0e039681 2021-11-15 stsp goto done;
564 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 0e039681 2021-11-15 stsp base_path_deriv2);
566 0e039681 2021-11-15 stsp if (err)
567 0e039681 2021-11-15 stsp goto done;
568 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 0e039681 2021-11-15 stsp if (err)
570 0e039681 2021-11-15 stsp goto done;
571 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 0e039681 2021-11-15 stsp if (err)
573 0e039681 2021-11-15 stsp goto done;
574 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 0e039681 2021-11-15 stsp if (err)
576 0e039681 2021-11-15 stsp goto done;
577 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
578 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
579 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
580 0e039681 2021-11-15 stsp goto done;
581 0e039681 2021-11-15 stsp }
582 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 0e039681 2021-11-15 stsp label_deriv ? " " : "",
585 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
586 0e039681 2021-11-15 stsp path_deriv) < 0) {
587 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
588 0e039681 2021-11-15 stsp goto done;
589 0e039681 2021-11-15 stsp }
590 0e039681 2021-11-15 stsp if (size_orig > 0) {
591 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
593 0e039681 2021-11-15 stsp label_orig ? " " : "",
594 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
595 0e039681 2021-11-15 stsp path_orig) < 0) {
596 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
597 0e039681 2021-11-15 stsp goto done;
598 0e039681 2021-11-15 stsp }
599 0e039681 2021-11-15 stsp }
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
602 0e039681 2021-11-15 stsp path_deriv2,
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
604 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
605 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
606 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
607 0e039681 2021-11-15 stsp goto done;
608 0e039681 2021-11-15 stsp }
609 0e039681 2021-11-15 stsp } else if (changed_deriv)
610 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 0e039681 2021-11-15 stsp else if (changed_deriv2)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 0e039681 2021-11-15 stsp done:
614 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 0e039681 2021-11-15 stsp err == NULL)
616 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
617 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
619 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
621 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
623 0e039681 2021-11-15 stsp free(path_orig);
624 0e039681 2021-11-15 stsp free(path_deriv);
625 0e039681 2021-11-15 stsp free(path_deriv2);
626 0e039681 2021-11-15 stsp free(base_path_orig);
627 0e039681 2021-11-15 stsp free(base_path_deriv);
628 0e039681 2021-11-15 stsp free(base_path_deriv2);
629 0e039681 2021-11-15 stsp return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 0e039681 2021-11-15 stsp if (err) {
672 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
673 0e039681 2021-11-15 stsp goto done;
674 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 0e039681 2021-11-15 stsp ondisk_path);
677 0e039681 2021-11-15 stsp if (err)
678 0e039681 2021-11-15 stsp goto done;
679 0e039681 2021-11-15 stsp }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 67a66647 2021-05-31 stsp if (err)
995 67a66647 2021-05-31 stsp goto done;
996 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 67a66647 2021-05-31 stsp blob_orig);
998 67a66647 2021-05-31 stsp if (err)
999 67a66647 2021-05-31 stsp goto done;
1000 67a66647 2021-05-31 stsp free(base_path);
1001 db590691 2021-06-02 stsp } else {
1002 db590691 2021-06-02 stsp /*
1003 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1004 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1005 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1006 db590691 2021-06-02 stsp */
1007 db590691 2021-06-02 stsp f_orig = got_opentemp();
1008 db590691 2021-06-02 stsp if (f_orig == NULL) {
1009 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1010 db590691 2021-06-02 stsp goto done;
1011 db590691 2021-06-02 stsp }
1012 67a66647 2021-05-31 stsp }
1013 67a66647 2021-05-31 stsp
1014 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1016 14c901f1 2019-08-08 stsp base_path = NULL;
1017 14c901f1 2019-08-08 stsp goto done;
1018 14c901f1 2019-08-08 stsp }
1019 14c901f1 2019-08-08 stsp
1020 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 14c901f1 2019-08-08 stsp if (err)
1022 14c901f1 2019-08-08 stsp goto done;
1023 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 14c901f1 2019-08-08 stsp blob_deriv);
1025 14c901f1 2019-08-08 stsp if (err)
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp
1028 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 14c901f1 2019-08-08 stsp if (err)
1030 14c901f1 2019-08-08 stsp goto done;
1031 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1032 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1034 14c901f1 2019-08-08 stsp goto done;
1035 eec2f5a9 2021-06-03 stsp }
1036 eec2f5a9 2021-06-03 stsp
1037 5e91dae4 2022-08-30 stsp /*
1038 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1039 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1040 eec2f5a9 2021-06-03 stsp */
1041 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1042 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 eec2f5a9 2021-06-03 stsp if (err)
1044 eec2f5a9 2021-06-03 stsp goto done;
1045 eec2f5a9 2021-06-03 stsp } else {
1046 eec2f5a9 2021-06-03 stsp int fd;
1047 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1049 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1050 eec2f5a9 2021-06-03 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp }
1052 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1053 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1054 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1055 eec2f5a9 2021-06-03 stsp close(fd);
1056 eec2f5a9 2021-06-03 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp }
1058 14c901f1 2019-08-08 stsp }
1059 14c901f1 2019-08-08 stsp
1060 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 14c901f1 2019-08-08 stsp done:
1064 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1066 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1068 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1070 14c901f1 2019-08-08 stsp free(base_path);
1071 67a66647 2021-05-31 stsp if (blob_orig_path) {
1072 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1073 67a66647 2021-05-31 stsp free(blob_orig_path);
1074 67a66647 2021-05-31 stsp }
1075 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1076 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1077 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp }
1079 6353ad76 2019-02-08 stsp free(id_str);
1080 818c7501 2019-07-11 stsp free(label_deriv);
1081 ed6b5030 2020-10-20 stsp free(parent);
1082 4a1ddfc2 2019-01-12 stsp return err;
1083 4a1ddfc2 2019-01-12 stsp }
1084 4a1ddfc2 2019-01-12 stsp
1085 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1086 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1089 13d9040b 2019-03-27 stsp {
1090 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1091 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1092 13d9040b 2019-03-27 stsp
1093 65b05cec 2020-07-23 stsp *new_iep = NULL;
1094 65b05cec 2020-07-23 stsp
1095 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1096 054041d0 2020-06-24 stsp if (err)
1097 054041d0 2020-06-24 stsp return err;
1098 054041d0 2020-06-24 stsp
1099 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1101 054041d0 2020-06-24 stsp if (err)
1102 054041d0 2020-06-24 stsp goto done;
1103 054041d0 2020-06-24 stsp
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1105 054041d0 2020-06-24 stsp done:
1106 054041d0 2020-06-24 stsp if (err)
1107 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1108 65b05cec 2020-07-23 stsp else
1109 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1110 13d9040b 2019-03-27 stsp return err;
1111 1ebedb77 2019-10-19 stsp }
1112 1ebedb77 2019-10-19 stsp
1113 1ebedb77 2019-10-19 stsp static mode_t
1114 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1115 1ebedb77 2019-10-19 stsp {
1116 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1117 1ebedb77 2019-10-19 stsp
1118 1ebedb77 2019-10-19 stsp if (executable) {
1119 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1124 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1125 1ebedb77 2019-10-19 stsp }
1126 1ebedb77 2019-10-19 stsp
1127 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 13d9040b 2019-03-27 stsp }
1129 13d9040b 2019-03-27 stsp
1130 8ba819a3 2020-07-23 stsp /* forward declaration */
1131 13d9040b 2019-03-27 stsp static const struct got_error *
1132 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1134 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1135 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1136 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1137 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 8ba819a3 2020-07-23 stsp
1139 5a1fbc73 2020-07-23 stsp /*
1140 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1141 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1142 5a1fbc73 2020-07-23 stsp */
1143 8ba819a3 2020-07-23 stsp static const struct got_error *
1144 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1146 5a1fbc73 2020-07-23 stsp {
1147 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1148 5a1fbc73 2020-07-23 stsp ssize_t elen;
1149 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1150 5a1fbc73 2020-07-23 stsp int fd;
1151 5a1fbc73 2020-07-23 stsp
1152 c6e8a826 2021-04-05 stsp *did_something = 0;
1153 c6e8a826 2021-04-05 stsp
1154 5a1fbc73 2020-07-23 stsp /*
1155 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1156 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1157 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1158 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1159 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1160 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1161 5a1fbc73 2020-07-23 stsp */
1162 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1164 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1165 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1166 5a1fbc73 2020-07-23 stsp
1167 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1168 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 5a1fbc73 2020-07-23 stsp if (elen == -1)
1170 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1171 5a1fbc73 2020-07-23 stsp
1172 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1173 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1174 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1175 5a1fbc73 2020-07-23 stsp }
1176 5a1fbc73 2020-07-23 stsp
1177 c6e8a826 2021-04-05 stsp *did_something = 1;
1178 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1179 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1180 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp return err;
1182 3c1ec782 2020-07-23 stsp }
1183 3c1ec782 2020-07-23 stsp
1184 3c1ec782 2020-07-23 stsp static const struct got_error *
1185 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1187 3c1ec782 2020-07-23 stsp {
1188 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1189 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1190 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1191 3c1ec782 2020-07-23 stsp
1192 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1193 3c1ec782 2020-07-23 stsp
1194 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1195 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1196 3c1ec782 2020-07-23 stsp return NULL;
1197 3c1ec782 2020-07-23 stsp }
1198 3c1ec782 2020-07-23 stsp
1199 3c1ec782 2020-07-23 stsp /*
1200 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1201 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1202 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1203 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1204 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1205 3c1ec782 2020-07-23 stsp */
1206 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1207 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1208 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1209 ce031e9e 2020-10-20 stsp if (err)
1210 ce031e9e 2020-10-20 stsp return err;
1211 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 ce031e9e 2020-10-20 stsp free(parent);
1213 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1214 ce031e9e 2020-10-20 stsp }
1215 ce031e9e 2020-10-20 stsp free(parent);
1216 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 3c1ec782 2020-07-23 stsp free(abspath);
1219 3c1ec782 2020-07-23 stsp return err;
1220 3c1ec782 2020-07-23 stsp }
1221 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 3c1ec782 2020-07-23 stsp free(abspath);
1223 3c1ec782 2020-07-23 stsp if (err)
1224 3c1ec782 2020-07-23 stsp return err;
1225 3c1ec782 2020-07-23 stsp } else {
1226 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 3c1ec782 2020-07-23 stsp if (err)
1228 3c1ec782 2020-07-23 stsp return err;
1229 3c1ec782 2020-07-23 stsp }
1230 3c1ec782 2020-07-23 stsp
1231 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1232 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1234 3c1ec782 2020-07-23 stsp return NULL;
1235 3c1ec782 2020-07-23 stsp }
1236 3c1ec782 2020-07-23 stsp
1237 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1238 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1240 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1241 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1243 3c1ec782 2020-07-23 stsp
1244 3c1ec782 2020-07-23 stsp free(path_got);
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 5a1fbc73 2020-07-23 stsp }
1247 5a1fbc73 2020-07-23 stsp
1248 5a1fbc73 2020-07-23 stsp static const struct got_error *
1249 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1252 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1253 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1254 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1255 9d31a1d8 2018-03-11 stsp {
1256 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1257 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1258 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1259 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1260 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 8ba819a3 2020-07-23 stsp
1262 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1263 2e1fa222 2020-07-23 stsp
1264 3c29341b 2022-07-21 florian /*
1265 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1266 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1267 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1268 8ba819a3 2020-07-23 stsp * in the blob object.
1269 8ba819a3 2020-07-23 stsp */
1270 8ba819a3 2020-07-23 stsp do {
1271 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1272 538b6881 2022-07-21 florian if (err)
1273 538b6881 2022-07-21 florian return err;
1274 538b6881 2022-07-21 florian
1275 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1276 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1277 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1278 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1279 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1280 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1281 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1282 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1283 3b9f0f87 2020-07-23 stsp progress_arg);
1284 8ba819a3 2020-07-23 stsp }
1285 8ba819a3 2020-07-23 stsp if (len > 0) {
1286 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1287 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1288 8ba819a3 2020-07-23 stsp len - hdrlen);
1289 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1290 8ba819a3 2020-07-23 stsp hdrlen = 0;
1291 8ba819a3 2020-07-23 stsp }
1292 8ba819a3 2020-07-23 stsp } while (len != 0);
1293 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1294 8ba819a3 2020-07-23 stsp
1295 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1296 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1297 3c1ec782 2020-07-23 stsp if (err)
1298 3c1ec782 2020-07-23 stsp return err;
1299 8ba819a3 2020-07-23 stsp
1300 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1301 906c123b 2020-07-23 stsp /* install as a regular file */
1302 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1303 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1304 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1305 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1306 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1307 3c29341b 2022-07-21 florian return err;
1308 906c123b 2020-07-23 stsp }
1309 906c123b 2020-07-23 stsp
1310 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1311 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1312 c6e8a826 2021-04-05 stsp int symlink_replaced;
1313 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1314 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1315 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1316 3c29341b 2022-07-21 florian return err;
1317 c90c8ce3 2020-07-23 stsp }
1318 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1319 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1320 5a1fbc73 2020-07-23 stsp if (err)
1321 3c29341b 2022-07-21 florian return err;
1322 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1323 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1324 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1325 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1326 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1327 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1328 c6e8a826 2021-04-05 stsp } else {
1329 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1330 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1331 c6e8a826 2021-04-05 stsp }
1332 f35fa46a 2020-07-23 stsp }
1333 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1334 f35fa46a 2020-07-23 stsp }
1335 f35fa46a 2020-07-23 stsp
1336 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1337 f4994adc 2020-10-20 stsp char *parent;
1338 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1339 f4994adc 2020-10-20 stsp if (err)
1340 3c29341b 2022-07-21 florian return err;
1341 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1342 f4994adc 2020-10-20 stsp free(parent);
1343 f35fa46a 2020-07-23 stsp if (err)
1344 3c29341b 2022-07-21 florian return err;
1345 f35fa46a 2020-07-23 stsp /*
1346 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1347 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1348 f35fa46a 2020-07-23 stsp */
1349 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1350 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1351 3c29341b 2022-07-21 florian return err;
1352 f35fa46a 2020-07-23 stsp }
1353 f35fa46a 2020-07-23 stsp }
1354 f35fa46a 2020-07-23 stsp
1355 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1356 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1357 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1358 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1359 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1360 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1361 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1362 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1363 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1364 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1365 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1366 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1367 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1368 8ba819a3 2020-07-23 stsp } else {
1369 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1370 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1371 8ba819a3 2020-07-23 stsp }
1372 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1373 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1374 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1375 8ba819a3 2020-07-23 stsp return err;
1376 8ba819a3 2020-07-23 stsp }
1377 8ba819a3 2020-07-23 stsp
1378 8ba819a3 2020-07-23 stsp static const struct got_error *
1379 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1381 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1382 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1383 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1384 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1385 8ba819a3 2020-07-23 stsp {
1386 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1387 507dc3bb 2018-12-29 stsp int fd = -1;
1388 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1389 507dc3bb 2018-12-29 stsp int update = 0;
1390 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1391 8ba819a3 2020-07-23 stsp
1392 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 8bd0cdad 2021-12-31 stsp O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1395 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1396 f5375317 2020-10-20 stsp char *parent;
1397 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1398 f5375317 2020-10-20 stsp if (err)
1399 f5375317 2020-10-20 stsp return err;
1400 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1401 f5375317 2020-10-20 stsp free(parent);
1402 21908da4 2019-01-13 stsp if (err)
1403 21908da4 2019-01-13 stsp return err;
1404 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1405 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1407 21908da4 2019-01-13 stsp if (fd == -1)
1408 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1409 230a42bd 2019-05-11 jcs ondisk_path);
1410 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1411 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1412 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1413 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1414 3b9f0f87 2020-07-23 stsp goto done;
1415 3b9f0f87 2020-07-23 stsp }
1416 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1419 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1420 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1421 507dc3bb 2018-12-29 stsp goto done;
1422 d70b8e30 2018-12-27 stsp } else {
1423 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1424 507dc3bb 2018-12-29 stsp ondisk_path);
1425 507dc3bb 2018-12-29 stsp if (err)
1426 507dc3bb 2018-12-29 stsp goto done;
1427 507dc3bb 2018-12-29 stsp update = 1;
1428 9d31a1d8 2018-03-11 stsp }
1429 507dc3bb 2018-12-29 stsp } else
1430 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1431 9d31a1d8 2018-03-11 stsp }
1432 9d31a1d8 2018-03-11 stsp
1433 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1435 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1436 3818e3c4 2020-11-01 naddy goto done;
1437 3818e3c4 2020-11-01 naddy }
1438 3818e3c4 2020-11-01 naddy
1439 bd6aa359 2020-07-23 stsp if (progress_cb) {
1440 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1441 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 bd6aa359 2020-07-23 stsp path);
1443 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1444 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 bd6aa359 2020-07-23 stsp path);
1446 bd6aa359 2020-07-23 stsp else
1447 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 bd6aa359 2020-07-23 stsp if (err)
1450 bd6aa359 2020-07-23 stsp goto done;
1451 bd6aa359 2020-07-23 stsp }
1452 d7b62c98 2018-12-27 stsp
1453 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1454 9d31a1d8 2018-03-11 stsp do {
1455 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1457 9d31a1d8 2018-03-11 stsp if (err)
1458 9d31a1d8 2018-03-11 stsp break;
1459 9d31a1d8 2018-03-11 stsp if (len > 0) {
1460 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1461 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1463 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1464 b87c6f83 2018-12-24 stsp goto done;
1465 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1466 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1467 b87c6f83 2018-12-24 stsp goto done;
1468 9d31a1d8 2018-03-11 stsp }
1469 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1470 9d31a1d8 2018-03-11 stsp }
1471 9d31a1d8 2018-03-11 stsp } while (len != 0);
1472 9d31a1d8 2018-03-11 stsp
1473 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1474 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1475 816dc654 2019-02-16 stsp goto done;
1476 816dc654 2019-02-16 stsp }
1477 9d31a1d8 2018-03-11 stsp
1478 507dc3bb 2018-12-29 stsp if (update) {
1479 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1481 f6d8c0ac 2020-11-09 stsp goto done;
1482 f6d8c0ac 2020-11-09 stsp }
1483 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1484 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1485 230a42bd 2019-05-11 jcs ondisk_path);
1486 68ed9ba5 2019-02-10 stsp goto done;
1487 68ed9ba5 2019-02-10 stsp }
1488 63df146d 2020-11-09 stsp free(tmppath);
1489 63df146d 2020-11-09 stsp tmppath = NULL;
1490 507dc3bb 2018-12-29 stsp }
1491 507dc3bb 2018-12-29 stsp
1492 9d31a1d8 2018-03-11 stsp done:
1493 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1494 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1495 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1497 507dc3bb 2018-12-29 stsp free(tmppath);
1498 6353ad76 2019-02-08 stsp return err;
1499 6353ad76 2019-02-08 stsp }
1500 6353ad76 2019-02-08 stsp
1501 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 6353ad76 2019-02-08 stsp static const struct got_error *
1503 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1504 7154f6ce 2019-03-27 stsp {
1505 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1506 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1509 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1510 7154f6ce 2019-03-27 stsp };
1511 7154f6ce 2019-03-27 stsp int i = 0;
1512 9bdd68dd 2021-01-02 naddy char *line = NULL;
1513 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1514 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1515 7154f6ce 2019-03-27 stsp
1516 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1517 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1518 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1519 7154f6ce 2019-03-27 stsp if (feof(f))
1520 7154f6ce 2019-03-27 stsp break;
1521 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1522 7154f6ce 2019-03-27 stsp break;
1523 7154f6ce 2019-03-27 stsp }
1524 7154f6ce 2019-03-27 stsp
1525 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 19332e6d 2019-05-13 stsp == 0)
1528 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1529 7154f6ce 2019-03-27 stsp else
1530 7154f6ce 2019-03-27 stsp i++;
1531 7154f6ce 2019-03-27 stsp }
1532 7154f6ce 2019-03-27 stsp }
1533 9bdd68dd 2021-01-02 naddy free(line);
1534 7154f6ce 2019-03-27 stsp
1535 7154f6ce 2019-03-27 stsp return err;
1536 e2b1e152 2019-07-13 stsp }
1537 e2b1e152 2019-07-13 stsp
1538 e2b1e152 2019-07-13 stsp static int
1539 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1540 1ebedb77 2019-10-19 stsp {
1541 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 1ebedb77 2019-10-19 stsp }
1544 1ebedb77 2019-10-19 stsp
1545 1ebedb77 2019-10-19 stsp static int
1546 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1547 e2b1e152 2019-07-13 stsp {
1548 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1553 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1554 c363b2c1 2019-08-03 stsp }
1555 c363b2c1 2019-08-03 stsp
1556 c363b2c1 2019-08-03 stsp static unsigned char
1557 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1558 c363b2c1 2019-08-03 stsp {
1559 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1564 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1566 c363b2c1 2019-08-03 stsp default:
1567 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1568 a919d5c4 2020-07-23 stsp }
1569 a919d5c4 2020-07-23 stsp }
1570 a919d5c4 2020-07-23 stsp
1571 a919d5c4 2020-07-23 stsp static const struct got_error *
1572 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1573 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1574 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1575 a919d5c4 2020-07-23 stsp {
1576 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1577 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1578 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1579 a919d5c4 2020-07-23 stsp ssize_t elen;
1580 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1581 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1583 a919d5c4 2020-07-23 stsp
1584 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1585 a919d5c4 2020-07-23 stsp
1586 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1587 a919d5c4 2020-07-23 stsp do {
1588 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1589 a919d5c4 2020-07-23 stsp if (err)
1590 a919d5c4 2020-07-23 stsp return err;
1591 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1592 a919d5c4 2020-07-23 stsp /*
1593 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1594 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1595 a919d5c4 2020-07-23 stsp */
1596 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1597 a919d5c4 2020-07-23 stsp }
1598 a919d5c4 2020-07-23 stsp if (len > 0) {
1599 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1600 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1601 a919d5c4 2020-07-23 stsp len - hdrlen);
1602 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1603 a919d5c4 2020-07-23 stsp hdrlen = 0;
1604 a919d5c4 2020-07-23 stsp }
1605 a919d5c4 2020-07-23 stsp } while (len != 0);
1606 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1607 a919d5c4 2020-07-23 stsp
1608 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1609 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 a919d5c4 2020-07-23 stsp if (elen == -1)
1611 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1612 a919d5c4 2020-07-23 stsp } else {
1613 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1614 a919d5c4 2020-07-23 stsp if (elen == -1)
1615 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1616 c363b2c1 2019-08-03 stsp }
1617 a919d5c4 2020-07-23 stsp
1618 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1620 a919d5c4 2020-07-23 stsp
1621 a919d5c4 2020-07-23 stsp return NULL;
1622 7154f6ce 2019-03-27 stsp }
1623 7154f6ce 2019-03-27 stsp
1624 7154f6ce 2019-03-27 stsp static const struct got_error *
1625 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1626 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1627 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1628 6353ad76 2019-02-08 stsp {
1629 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1630 6353ad76 2019-02-08 stsp struct got_object_id id;
1631 6353ad76 2019-02-08 stsp size_t hdrlen;
1632 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1633 6353ad76 2019-02-08 stsp FILE *f = NULL;
1634 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1635 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1636 6353ad76 2019-02-08 stsp size_t flen, blen;
1637 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1638 6353ad76 2019-02-08 stsp
1639 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1640 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1641 6353ad76 2019-02-08 stsp
1642 7f91a133 2019-12-13 stsp /*
1643 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1644 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1645 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1646 7f91a133 2019-12-13 stsp */
1647 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1648 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1650 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1652 882ef1b9 2019-12-13 stsp else
1653 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1654 882ef1b9 2019-12-13 stsp goto done;
1655 882ef1b9 2019-12-13 stsp }
1656 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1657 3d35a492 2019-12-13 stsp goto done;
1658 3d35a492 2019-12-13 stsp }
1659 7f91a133 2019-12-13 stsp } else {
1660 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1662 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1663 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1664 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1666 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1667 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1669 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1671 3d35a492 2019-12-13 stsp else
1672 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1673 3d35a492 2019-12-13 stsp goto done;
1674 3d35a492 2019-12-13 stsp }
1675 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1676 1338848f 2019-12-13 stsp goto done;
1677 a378724f 2019-02-10 stsp }
1678 a378724f 2019-02-10 stsp }
1679 6353ad76 2019-02-08 stsp
1680 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1682 1338848f 2019-12-13 stsp goto done;
1683 3f148bc6 2019-07-27 stsp }
1684 efdd40df 2019-07-27 stsp
1685 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1687 1338848f 2019-12-13 stsp goto done;
1688 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1689 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1690 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1691 1338848f 2019-12-13 stsp goto done;
1692 d00136be 2019-03-26 stsp }
1693 b8f41171 2019-02-10 stsp
1694 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1695 f179e66d 2020-07-23 stsp goto done;
1696 f179e66d 2020-07-23 stsp
1697 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1698 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 1338848f 2019-12-13 stsp goto done;
1701 f179e66d 2020-07-23 stsp }
1702 6353ad76 2019-02-08 stsp
1703 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1704 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 c363b2c1 2019-08-03 stsp else
1707 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1708 c363b2c1 2019-08-03 stsp
1709 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1710 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1711 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1712 eb81bc23 2022-06-28 tracey goto done;
1713 eb81bc23 2022-06-28 tracey }
1714 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1715 6353ad76 2019-02-08 stsp if (err)
1716 1338848f 2019-12-13 stsp goto done;
1717 6353ad76 2019-02-08 stsp
1718 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1719 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1720 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1721 a919d5c4 2020-07-23 stsp goto done;
1722 a919d5c4 2020-07-23 stsp }
1723 a919d5c4 2020-07-23 stsp
1724 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1725 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1726 ab0d4361 2019-12-13 stsp if (fd == -1) {
1727 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1728 ab0d4361 2019-12-13 stsp goto done;
1729 ab0d4361 2019-12-13 stsp }
1730 3d35a492 2019-12-13 stsp }
1731 3d35a492 2019-12-13 stsp
1732 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1733 6353ad76 2019-02-08 stsp if (f == NULL) {
1734 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1735 6353ad76 2019-02-08 stsp goto done;
1736 6353ad76 2019-02-08 stsp }
1737 1338848f 2019-12-13 stsp fd = -1;
1738 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1739 656b1f76 2019-05-11 jcs for (;;) {
1740 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1741 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1742 6353ad76 2019-02-08 stsp if (err)
1743 7154f6ce 2019-03-27 stsp goto done;
1744 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1745 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1746 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1747 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1748 7154f6ce 2019-03-27 stsp goto done;
1749 80c5c120 2019-02-19 stsp }
1750 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1751 6353ad76 2019-02-08 stsp if (flen != 0)
1752 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1753 6353ad76 2019-02-08 stsp break;
1754 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1755 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1756 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1757 6353ad76 2019-02-08 stsp break;
1758 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1759 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1760 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1761 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1762 6353ad76 2019-02-08 stsp break;
1763 6353ad76 2019-02-08 stsp }
1764 6353ad76 2019-02-08 stsp } else {
1765 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1766 6353ad76 2019-02-08 stsp break;
1767 6353ad76 2019-02-08 stsp }
1768 6353ad76 2019-02-08 stsp hdrlen = 0;
1769 6353ad76 2019-02-08 stsp }
1770 7154f6ce 2019-03-27 stsp
1771 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1772 7154f6ce 2019-03-27 stsp rewind(f);
1773 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1774 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1775 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1776 6353ad76 2019-02-08 stsp done:
1777 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1778 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1779 6353ad76 2019-02-08 stsp if (blob)
1780 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1781 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1782 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1783 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1784 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1785 9d31a1d8 2018-03-11 stsp return err;
1786 e2b1e152 2019-07-13 stsp }
1787 e2b1e152 2019-07-13 stsp
1788 e2b1e152 2019-07-13 stsp /*
1789 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1790 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1791 e2b1e152 2019-07-13 stsp */
1792 e2b1e152 2019-07-13 stsp static const struct got_error *
1793 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1794 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1795 e2b1e152 2019-07-13 stsp {
1796 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1797 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1798 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1799 e2b1e152 2019-07-13 stsp
1800 e2b1e152 2019-07-13 stsp return NULL;
1801 9d31a1d8 2018-03-11 stsp }
1802 9d31a1d8 2018-03-11 stsp
1803 9d31a1d8 2018-03-11 stsp static const struct got_error *
1804 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1805 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1806 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1807 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1808 0584f854 2019-04-06 stsp void *progress_arg)
1809 9d31a1d8 2018-03-11 stsp {
1810 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1811 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1812 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1813 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1814 b8f41171 2019-02-10 stsp struct stat sb;
1815 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1816 9d31a1d8 2018-03-11 stsp
1817 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1818 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1819 6353ad76 2019-02-08 stsp
1820 abb4604f 2019-07-27 stsp if (ie) {
1821 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1822 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1823 a76c42e6 2019-08-03 stsp goto done;
1824 a76c42e6 2019-08-03 stsp }
1825 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1826 7f91a133 2019-12-13 stsp repo);
1827 abb4604f 2019-07-27 stsp if (err)
1828 abb4604f 2019-07-27 stsp goto done;
1829 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1830 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1831 c90c8ce3 2020-07-23 stsp } else {
1832 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1833 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1834 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1835 f6764181 2021-09-24 stsp ondisk_path);
1836 f6764181 2021-09-24 stsp goto done;
1837 f6764181 2021-09-24 stsp }
1838 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1839 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1840 f6764181 2021-09-24 stsp } else {
1841 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1842 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1843 f6764181 2021-09-24 stsp else
1844 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1845 f6764181 2021-09-24 stsp }
1846 c90c8ce3 2020-07-23 stsp }
1847 6353ad76 2019-02-08 stsp
1848 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1849 2c41dce7 2021-06-27 stsp if (ie)
1850 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1851 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1852 b8f41171 2019-02-10 stsp goto done;
1853 b8f41171 2019-02-10 stsp }
1854 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1855 a769b60b 2021-06-27 stsp if (ie)
1856 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1857 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1858 5036ab18 2020-04-18 stsp path);
1859 5036ab18 2020-04-18 stsp goto done;
1860 5036ab18 2020-04-18 stsp }
1861 b8f41171 2019-02-10 stsp
1862 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1863 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1864 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1865 c6e8a826 2021-04-05 stsp /*
1866 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1867 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1868 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1869 c6e8a826 2021-04-05 stsp * updating contents of this file.
1870 c6e8a826 2021-04-05 stsp */
1871 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1872 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1874 c6e8a826 2021-04-05 stsp /* Same commit. */
1875 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1876 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1877 e2b1e152 2019-07-13 stsp if (err)
1878 e2b1e152 2019-07-13 stsp goto done;
1879 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1880 b8f41171 2019-02-10 stsp path);
1881 6353ad76 2019-02-08 stsp goto done;
1882 a378724f 2019-02-10 stsp }
1883 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1884 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1885 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1886 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1887 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1888 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1889 0f58026f 2021-04-05 stsp if (err)
1890 0f58026f 2021-04-05 stsp goto done;
1891 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1892 0f58026f 2021-04-05 stsp path);
1893 b8f41171 2019-02-10 stsp goto done;
1894 e2b1e152 2019-07-13 stsp }
1895 9d31a1d8 2018-03-11 stsp }
1896 9d31a1d8 2018-03-11 stsp
1897 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1898 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1899 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1900 eb81bc23 2022-06-28 tracey goto done;
1901 eb81bc23 2022-06-28 tracey }
1902 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1903 8da9e5f4 2019-01-12 stsp if (err)
1904 6353ad76 2019-02-08 stsp goto done;
1905 512f0d0e 2019-01-02 stsp
1906 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1907 234035bc 2019-06-01 stsp int update_timestamps;
1908 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1909 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1910 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1911 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1912 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1913 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1914 eb81bc23 2022-06-28 tracey goto done;
1915 eb81bc23 2022-06-28 tracey }
1916 234035bc 2019-06-01 stsp struct got_object_id id2;
1917 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1918 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1919 eb81bc23 2022-06-28 tracey fd2);
1920 234035bc 2019-06-01 stsp if (err)
1921 f69721c3 2019-10-21 stsp goto done;
1922 f69721c3 2019-10-21 stsp }
1923 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1924 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1925 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1926 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1927 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1928 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1929 f69721c3 2019-10-21 stsp goto done;
1930 f69721c3 2019-10-21 stsp }
1931 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1932 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1933 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1934 234035bc 2019-06-01 stsp goto done;
1935 f69721c3 2019-10-21 stsp }
1936 234035bc 2019-06-01 stsp }
1937 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1938 36bf999c 2020-07-23 stsp char *link_target;
1939 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1940 36bf999c 2020-07-23 stsp if (err)
1941 36bf999c 2020-07-23 stsp goto done;
1942 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1943 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1944 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1945 36bf999c 2020-07-23 stsp free(link_target);
1946 993e2a1b 2020-07-23 stsp } else {
1947 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1948 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1949 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1950 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1951 993e2a1b 2020-07-23 stsp }
1952 f69721c3 2019-10-21 stsp free(label_orig);
1953 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1954 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1955 eb81bc23 2022-06-28 tracey goto done;
1956 eb81bc23 2022-06-28 tracey }
1957 234035bc 2019-06-01 stsp if (blob2)
1958 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1959 909d120e 2019-09-22 stsp if (err)
1960 909d120e 2019-09-22 stsp goto done;
1961 234035bc 2019-06-01 stsp /*
1962 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1963 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1964 234035bc 2019-06-01 stsp * unmodified files again.
1965 234035bc 2019-06-01 stsp */
1966 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1967 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1968 234035bc 2019-06-01 stsp update_timestamps);
1969 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1970 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1971 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1972 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1973 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1974 1ee397ad 2019-07-12 stsp if (err)
1975 1ee397ad 2019-07-12 stsp goto done;
1976 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1977 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1978 13d9040b 2019-03-27 stsp if (err)
1979 13d9040b 2019-03-27 stsp goto done;
1980 13d9040b 2019-03-27 stsp } else {
1981 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1982 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1983 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1984 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1985 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1986 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
1987 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
1988 2e1fa222 2020-07-23 stsp } else {
1989 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1990 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1991 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1992 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1993 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1994 2e1fa222 2020-07-23 stsp }
1995 13d9040b 2019-03-27 stsp if (err)
1996 13d9040b 2019-03-27 stsp goto done;
1997 65b05cec 2020-07-23 stsp
1998 054041d0 2020-06-24 stsp if (ie) {
1999 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2000 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2001 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2002 054041d0 2020-06-24 stsp } else {
2003 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2004 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2005 054041d0 2020-06-24 stsp &blob->id);
2006 054041d0 2020-06-24 stsp }
2007 13d9040b 2019-03-27 stsp if (err)
2008 13d9040b 2019-03-27 stsp goto done;
2009 65b05cec 2020-07-23 stsp
2010 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2011 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2012 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2013 2e1fa222 2020-07-23 stsp }
2014 13d9040b 2019-03-27 stsp }
2015 eb81bc23 2022-06-28 tracey
2016 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2017 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2018 eb81bc23 2022-06-28 tracey goto done;
2019 eb81bc23 2022-06-28 tracey }
2020 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2021 6353ad76 2019-02-08 stsp done:
2022 6353ad76 2019-02-08 stsp free(ondisk_path);
2023 8da9e5f4 2019-01-12 stsp return err;
2024 90285c3b 2019-01-08 stsp }
2025 90285c3b 2019-01-08 stsp
2026 90285c3b 2019-01-08 stsp static const struct got_error *
2027 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2028 90285c3b 2019-01-08 stsp {
2029 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2030 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2031 90285c3b 2019-01-08 stsp
2032 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2033 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2034 90285c3b 2019-01-08 stsp
2035 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2036 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2037 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2038 c97665ae 2019-01-13 stsp } else {
2039 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2040 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2041 1c4cdd89 2021-06-20 stsp if (err)
2042 1c4cdd89 2021-06-20 stsp goto done;
2043 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2044 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2045 fddefe3b 2020-10-20 stsp free(ondisk_path);
2046 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2047 1c4cdd89 2021-06-20 stsp parent = NULL;
2048 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2049 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2050 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2051 fddefe3b 2020-10-20 stsp ondisk_path);
2052 90285c3b 2019-01-08 stsp break;
2053 90285c3b 2019-01-08 stsp }
2054 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2055 1c4cdd89 2021-06-20 stsp if (err)
2056 1c4cdd89 2021-06-20 stsp break;
2057 1c4cdd89 2021-06-20 stsp }
2058 90285c3b 2019-01-08 stsp }
2059 1c4cdd89 2021-06-20 stsp done:
2060 90285c3b 2019-01-08 stsp free(ondisk_path);
2061 1c4cdd89 2021-06-20 stsp free(parent);
2062 90285c3b 2019-01-08 stsp return err;
2063 512f0d0e 2019-01-02 stsp }
2064 512f0d0e 2019-01-02 stsp
2065 708d8e67 2019-03-27 stsp static const struct got_error *
2066 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2068 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2069 708d8e67 2019-03-27 stsp {
2070 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2071 708d8e67 2019-03-27 stsp unsigned char status;
2072 708d8e67 2019-03-27 stsp struct stat sb;
2073 708d8e67 2019-03-27 stsp char *ondisk_path;
2074 708d8e67 2019-03-27 stsp
2075 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2077 a76c42e6 2019-08-03 stsp
2078 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2079 708d8e67 2019-03-27 stsp == -1)
2080 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2081 708d8e67 2019-03-27 stsp
2082 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2083 708d8e67 2019-03-27 stsp if (err)
2084 5a58a424 2020-06-23 stsp goto done;
2085 708d8e67 2019-03-27 stsp
2086 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2088 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2090 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2091 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2092 993e2a1b 2020-07-23 stsp goto done;
2093 993e2a1b 2020-07-23 stsp }
2094 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2095 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2097 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2098 993e2a1b 2020-07-23 stsp if (err)
2099 993e2a1b 2020-07-23 stsp goto done;
2100 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2101 993e2a1b 2020-07-23 stsp ie->path);
2102 993e2a1b 2020-07-23 stsp goto done;
2103 993e2a1b 2020-07-23 stsp }
2104 993e2a1b 2020-07-23 stsp
2105 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2107 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2108 1ee397ad 2019-07-12 stsp if (err)
2109 5a58a424 2020-06-23 stsp goto done;
2110 fc6346c4 2019-03-27 stsp /*
2111 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2112 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2113 fc6346c4 2019-03-27 stsp */
2114 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2115 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2116 fc6346c4 2019-03-27 stsp } else {
2117 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2118 1ee397ad 2019-07-12 stsp if (err)
2119 5a58a424 2020-06-23 stsp goto done;
2120 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2121 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2122 fc6346c4 2019-03-27 stsp if (err)
2123 5a58a424 2020-06-23 stsp goto done;
2124 fc6346c4 2019-03-27 stsp }
2125 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2126 708d8e67 2019-03-27 stsp }
2127 5a58a424 2020-06-23 stsp done:
2128 5a58a424 2020-06-23 stsp free(ondisk_path);
2129 fc6346c4 2019-03-27 stsp return err;
2130 708d8e67 2019-03-27 stsp }
2131 708d8e67 2019-03-27 stsp
2132 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2133 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2134 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2135 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2136 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2137 8da9e5f4 2019-01-12 stsp void *progress_arg;
2138 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2139 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2140 8da9e5f4 2019-01-12 stsp };
2141 8da9e5f4 2019-01-12 stsp
2142 512f0d0e 2019-01-02 stsp static const struct got_error *
2143 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2145 512f0d0e 2019-01-02 stsp {
2146 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2147 0584f854 2019-04-06 stsp
2148 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2150 512f0d0e 2019-01-02 stsp
2151 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2152 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2153 8da9e5f4 2019-01-12 stsp }
2154 512f0d0e 2019-01-02 stsp
2155 8da9e5f4 2019-01-12 stsp static const struct got_error *
2156 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2157 8da9e5f4 2019-01-12 stsp {
2158 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2159 7a9df742 2019-01-08 stsp
2160 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2162 0584f854 2019-04-06 stsp
2163 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2164 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2165 9d31a1d8 2018-03-11 stsp }
2166 9d31a1d8 2018-03-11 stsp
2167 9d31a1d8 2018-03-11 stsp static const struct got_error *
2168 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2169 9d31a1d8 2018-03-11 stsp {
2170 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2171 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2172 8da9e5f4 2019-01-12 stsp char *path;
2173 9d31a1d8 2018-03-11 stsp
2174 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2176 0584f854 2019-04-06 stsp
2177 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2178 63c5ca5d 2019-08-24 stsp return NULL;
2179 63c5ca5d 2019-08-24 stsp
2180 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2181 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2182 8da9e5f4 2019-01-12 stsp == -1)
2183 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2184 9d31a1d8 2018-03-11 stsp
2185 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2186 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2187 8da9e5f4 2019-01-12 stsp else
2188 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2190 9d31a1d8 2018-03-11 stsp
2191 8da9e5f4 2019-01-12 stsp free(path);
2192 0cd1c46a 2019-03-11 stsp return err;
2193 0cd1c46a 2019-03-11 stsp }
2194 0cd1c46a 2019-03-11 stsp
2195 b2118c49 2020-07-28 stsp const struct got_error *
2196 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2197 b2118c49 2020-07-28 stsp {
2198 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2199 b2118c49 2020-07-28 stsp
2200 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2202 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2203 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2204 b2118c49 2020-07-28 stsp }
2205 b2118c49 2020-07-28 stsp
2206 b2118c49 2020-07-28 stsp return NULL;
2207 b2118c49 2020-07-28 stsp }
2208 b2118c49 2020-07-28 stsp
2209 818c7501 2019-07-11 stsp static const struct got_error *
2210 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2211 0cd1c46a 2019-03-11 stsp {
2212 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2213 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2214 0cd1c46a 2019-03-11 stsp
2215 517bab73 2019-03-11 stsp *refname = NULL;
2216 517bab73 2019-03-11 stsp
2217 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2218 b2118c49 2020-07-28 stsp if (err)
2219 b2118c49 2020-07-28 stsp return err;
2220 0cd1c46a 2019-03-11 stsp
2221 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2223 0647c563 2019-03-11 stsp *refname = NULL;
2224 0cd1c46a 2019-03-11 stsp }
2225 517bab73 2019-03-11 stsp free(uuidstr);
2226 517bab73 2019-03-11 stsp return err;
2227 517bab73 2019-03-11 stsp }
2228 517bab73 2019-03-11 stsp
2229 818c7501 2019-07-11 stsp const struct got_error *
2230 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2231 818c7501 2019-07-11 stsp {
2232 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2233 818c7501 2019-07-11 stsp }
2234 818c7501 2019-07-11 stsp
2235 818c7501 2019-07-11 stsp static const struct got_error *
2236 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 818c7501 2019-07-11 stsp {
2238 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2239 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2240 818c7501 2019-07-11 stsp }
2241 818c7501 2019-07-11 stsp
2242 818c7501 2019-07-11 stsp static const struct got_error *
2243 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2244 818c7501 2019-07-11 stsp {
2245 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2246 818c7501 2019-07-11 stsp }
2247 818c7501 2019-07-11 stsp
2248 818c7501 2019-07-11 stsp static const struct got_error *
2249 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2250 818c7501 2019-07-11 stsp {
2251 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2252 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2253 818c7501 2019-07-11 stsp }
2254 818c7501 2019-07-11 stsp
2255 818c7501 2019-07-11 stsp static const struct got_error *
2256 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 818c7501 2019-07-11 stsp {
2258 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2259 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2260 0ebf8283 2019-07-24 stsp }
2261 0ebf8283 2019-07-24 stsp
2262 0ebf8283 2019-07-24 stsp static const struct got_error *
2263 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2264 0ebf8283 2019-07-24 stsp {
2265 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2266 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2267 0ebf8283 2019-07-24 stsp }
2268 0ebf8283 2019-07-24 stsp
2269 0ebf8283 2019-07-24 stsp static const struct got_error *
2270 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2271 0ebf8283 2019-07-24 stsp {
2272 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2273 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2274 0ebf8283 2019-07-24 stsp }
2275 0ebf8283 2019-07-24 stsp
2276 0ebf8283 2019-07-24 stsp static const struct got_error *
2277 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2278 0ebf8283 2019-07-24 stsp {
2279 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2280 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2281 818c7501 2019-07-11 stsp }
2282 818c7501 2019-07-11 stsp
2283 0ebf8283 2019-07-24 stsp static const struct got_error *
2284 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 0ebf8283 2019-07-24 stsp {
2286 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2287 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2288 0ebf8283 2019-07-24 stsp }
2289 818c7501 2019-07-11 stsp
2290 0ebf8283 2019-07-24 stsp const struct got_error *
2291 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2292 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2293 0ebf8283 2019-07-24 stsp {
2294 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2296 0ebf8283 2019-07-24 stsp *path = NULL;
2297 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2298 0ebf8283 2019-07-24 stsp }
2299 0ebf8283 2019-07-24 stsp return NULL;
2300 f259c4c1 2021-09-24 stsp }
2301 f259c4c1 2021-09-24 stsp
2302 f259c4c1 2021-09-24 stsp static const struct got_error *
2303 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2304 f259c4c1 2021-09-24 stsp {
2305 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2306 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2307 0ebf8283 2019-07-24 stsp }
2308 0ebf8283 2019-07-24 stsp
2309 f259c4c1 2021-09-24 stsp static const struct got_error *
2310 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2311 f259c4c1 2021-09-24 stsp {
2312 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2313 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2314 f259c4c1 2021-09-24 stsp }
2315 f259c4c1 2021-09-24 stsp
2316 517bab73 2019-03-11 stsp /*
2317 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2318 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2319 517bab73 2019-03-11 stsp */
2320 517bab73 2019-03-11 stsp static const struct got_error *
2321 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2322 517bab73 2019-03-11 stsp {
2323 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2324 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2325 517bab73 2019-03-11 stsp char *refname;
2326 517bab73 2019-03-11 stsp
2327 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2328 517bab73 2019-03-11 stsp if (err)
2329 517bab73 2019-03-11 stsp return err;
2330 0cd1c46a 2019-03-11 stsp
2331 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2332 0cd1c46a 2019-03-11 stsp if (err)
2333 0cd1c46a 2019-03-11 stsp goto done;
2334 0cd1c46a 2019-03-11 stsp
2335 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2336 0cd1c46a 2019-03-11 stsp done:
2337 0cd1c46a 2019-03-11 stsp free(refname);
2338 0cd1c46a 2019-03-11 stsp if (ref)
2339 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2340 3e3a69f1 2019-07-25 stsp return err;
2341 3e3a69f1 2019-07-25 stsp }
2342 3e3a69f1 2019-07-25 stsp
2343 3e3a69f1 2019-07-25 stsp static const struct got_error *
2344 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2345 3e3a69f1 2019-07-25 stsp {
2346 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2347 3e3a69f1 2019-07-25 stsp
2348 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2349 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2350 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2351 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2352 3e3a69f1 2019-07-25 stsp }
2353 9d31a1d8 2018-03-11 stsp return err;
2354 9d31a1d8 2018-03-11 stsp }
2355 9d31a1d8 2018-03-11 stsp
2356 3e3a69f1 2019-07-25 stsp
2357 ebf99748 2019-05-09 stsp static const struct got_error *
2358 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2359 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2360 ebf99748 2019-05-09 stsp {
2361 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2362 ebf99748 2019-05-09 stsp FILE *index = NULL;
2363 0cd1c46a 2019-03-11 stsp
2364 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2365 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2366 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2367 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2368 ebf99748 2019-05-09 stsp
2369 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2370 3e3a69f1 2019-07-25 stsp if (err)
2371 ebf99748 2019-05-09 stsp goto done;
2372 ebf99748 2019-05-09 stsp
2373 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2374 ebf99748 2019-05-09 stsp if (index == NULL) {
2375 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2376 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2377 ebf99748 2019-05-09 stsp } else {
2378 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2379 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2380 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2381 ebf99748 2019-05-09 stsp }
2382 ebf99748 2019-05-09 stsp done:
2383 ebf99748 2019-05-09 stsp if (err) {
2384 ebf99748 2019-05-09 stsp free(*fileindex_path);
2385 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2386 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2387 ebf99748 2019-05-09 stsp *fileindex = NULL;
2388 ebf99748 2019-05-09 stsp }
2389 ebf99748 2019-05-09 stsp return err;
2390 ebf99748 2019-05-09 stsp }
2391 c932eeeb 2019-05-22 stsp
2392 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2393 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2394 c932eeeb 2019-05-22 stsp const char *path;
2395 c932eeeb 2019-05-22 stsp size_t path_len;
2396 c932eeeb 2019-05-22 stsp const char *entry_name;
2397 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2398 a484d721 2019-06-10 stsp void *progress_arg;
2399 c932eeeb 2019-05-22 stsp };
2400 ebf99748 2019-05-09 stsp
2401 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2402 c932eeeb 2019-05-22 stsp static const struct got_error *
2403 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2404 c932eeeb 2019-05-22 stsp {
2405 1ee397ad 2019-07-12 stsp const struct got_error *err;
2406 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2407 c932eeeb 2019-05-22 stsp
2408 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2409 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2410 c932eeeb 2019-05-22 stsp return NULL;
2411 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2412 102ff934 2019-06-11 stsp return NULL;
2413 102ff934 2019-06-11 stsp
2414 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2415 a769b60b 2021-06-27 stsp return NULL;
2416 a769b60b 2021-06-27 stsp
2417 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2418 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2419 c932eeeb 2019-05-22 stsp return NULL;
2420 c932eeeb 2019-05-22 stsp
2421 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2422 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2423 edd02c5e 2019-07-11 stsp ie->path);
2424 1ee397ad 2019-07-12 stsp if (err)
2425 1ee397ad 2019-07-12 stsp return err;
2426 1ee397ad 2019-07-12 stsp }
2427 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2428 c932eeeb 2019-05-22 stsp return NULL;
2429 a615e0e7 2020-12-16 stsp }
2430 a615e0e7 2020-12-16 stsp
2431 a615e0e7 2020-12-16 stsp static const struct got_error *
2432 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2433 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2434 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2435 a615e0e7 2020-12-16 stsp {
2436 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2437 a615e0e7 2020-12-16 stsp
2438 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2439 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2440 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2441 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2442 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2443 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2444 a615e0e7 2020-12-16 stsp
2445 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2446 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2447 9c6338c4 2019-06-02 stsp }
2448 9c6338c4 2019-06-02 stsp
2449 9c6338c4 2019-06-02 stsp static const struct got_error *
2450 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2451 9c6338c4 2019-06-02 stsp {
2452 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2453 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2454 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2455 867630bb 2020-01-17 stsp struct timespec timeout;
2456 9c6338c4 2019-06-02 stsp
2457 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2458 9c6338c4 2019-06-02 stsp fileindex_path);
2459 9c6338c4 2019-06-02 stsp if (err)
2460 9c6338c4 2019-06-02 stsp goto done;
2461 9c6338c4 2019-06-02 stsp
2462 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2463 9c6338c4 2019-06-02 stsp if (err)
2464 9c6338c4 2019-06-02 stsp goto done;
2465 9c6338c4 2019-06-02 stsp
2466 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2467 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2468 9c6338c4 2019-06-02 stsp fileindex_path);
2469 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2470 9c6338c4 2019-06-02 stsp }
2471 867630bb 2020-01-17 stsp
2472 867630bb 2020-01-17 stsp /*
2473 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2474 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2475 867630bb 2020-01-17 stsp * was recorded in the file index.
2476 867630bb 2020-01-17 stsp */
2477 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2478 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2479 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2480 9c6338c4 2019-06-02 stsp done:
2481 9c6338c4 2019-06-02 stsp if (new_index)
2482 9c6338c4 2019-06-02 stsp fclose(new_index);
2483 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2484 9c6338c4 2019-06-02 stsp return err;
2485 c932eeeb 2019-05-22 stsp }
2486 6ced4176 2019-07-12 stsp
2487 6ced4176 2019-07-12 stsp static const struct got_error *
2488 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2489 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2490 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2491 a44927cc 2022-04-07 stsp struct got_repository *repo)
2492 6ced4176 2019-07-12 stsp {
2493 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2494 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2495 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2496 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2497 6ced4176 2019-07-12 stsp
2498 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2499 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2500 6ced4176 2019-07-12 stsp *tree_id = NULL;
2501 6ced4176 2019-07-12 stsp
2502 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2503 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2504 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2505 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2506 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2507 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2508 6ced4176 2019-07-12 stsp goto done;
2509 6ced4176 2019-07-12 stsp }
2510 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2511 a44927cc 2022-04-07 stsp worktree->path_prefix);
2512 6ced4176 2019-07-12 stsp if (err)
2513 6ced4176 2019-07-12 stsp goto done;
2514 6ced4176 2019-07-12 stsp return NULL;
2515 6ced4176 2019-07-12 stsp }
2516 6ced4176 2019-07-12 stsp
2517 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2518 6ced4176 2019-07-12 stsp
2519 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2520 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2521 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2522 6ced4176 2019-07-12 stsp goto done;
2523 6ced4176 2019-07-12 stsp }
2524 6ced4176 2019-07-12 stsp
2525 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2526 6ced4176 2019-07-12 stsp if (err)
2527 6ced4176 2019-07-12 stsp goto done;
2528 6ced4176 2019-07-12 stsp
2529 6ced4176 2019-07-12 stsp free(in_repo_path);
2530 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2531 6ced4176 2019-07-12 stsp
2532 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2533 6ced4176 2019-07-12 stsp if (err)
2534 6ced4176 2019-07-12 stsp goto done;
2535 c932eeeb 2019-05-22 stsp
2536 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2537 6ced4176 2019-07-12 stsp /* Check out a single file. */
2538 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2539 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2540 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2541 6ced4176 2019-07-12 stsp if (in_repo_path == 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 *tree_relpath = strdup("");
2546 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2547 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2548 6ced4176 2019-07-12 stsp goto done;
2549 6ced4176 2019-07-12 stsp }
2550 6ced4176 2019-07-12 stsp } else {
2551 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2552 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2553 6ced4176 2019-07-12 stsp if (err)
2554 6ced4176 2019-07-12 stsp return err;
2555 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2556 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2557 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2558 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2559 6ced4176 2019-07-12 stsp goto done;
2560 6ced4176 2019-07-12 stsp }
2561 6ced4176 2019-07-12 stsp }
2562 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2563 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2564 6ced4176 2019-07-12 stsp } else {
2565 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2566 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2567 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2568 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2569 6ced4176 2019-07-12 stsp goto done;
2570 6ced4176 2019-07-12 stsp }
2571 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2572 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2573 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2574 6ced4176 2019-07-12 stsp goto done;
2575 6ced4176 2019-07-12 stsp }
2576 6ced4176 2019-07-12 stsp }
2577 6ced4176 2019-07-12 stsp done:
2578 6ced4176 2019-07-12 stsp free(id);
2579 6ced4176 2019-07-12 stsp free(in_repo_path);
2580 6ced4176 2019-07-12 stsp if (err) {
2581 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2582 6ced4176 2019-07-12 stsp free(*tree_relpath);
2583 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2584 6ced4176 2019-07-12 stsp free(*tree_id);
2585 6ced4176 2019-07-12 stsp *tree_id = NULL;
2586 6ced4176 2019-07-12 stsp }
2587 07ed472d 2019-07-12 stsp return err;
2588 07ed472d 2019-07-12 stsp }
2589 07ed472d 2019-07-12 stsp
2590 07ed472d 2019-07-12 stsp static const struct got_error *
2591 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2592 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2593 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2594 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2595 07ed472d 2019-07-12 stsp {
2596 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2597 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2598 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2599 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2600 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2601 07ed472d 2019-07-12 stsp
2602 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2603 7f47418f 2019-12-20 stsp if (err) {
2604 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2605 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2606 7f47418f 2019-12-20 stsp goto done;
2607 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2608 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2609 7f47418f 2019-12-20 stsp if (err)
2610 7f47418f 2019-12-20 stsp return err;
2611 7f47418f 2019-12-20 stsp }
2612 07ed472d 2019-07-12 stsp
2613 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2614 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2615 07ed472d 2019-07-12 stsp if (err)
2616 07ed472d 2019-07-12 stsp goto done;
2617 07ed472d 2019-07-12 stsp
2618 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2619 07ed472d 2019-07-12 stsp if (err)
2620 07ed472d 2019-07-12 stsp goto done;
2621 07ed472d 2019-07-12 stsp
2622 07ed472d 2019-07-12 stsp if (entry_name &&
2623 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2624 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2625 07ed472d 2019-07-12 stsp goto done;
2626 07ed472d 2019-07-12 stsp }
2627 07ed472d 2019-07-12 stsp
2628 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2629 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2630 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2631 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2632 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2633 07ed472d 2019-07-12 stsp arg.repo = repo;
2634 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2635 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2636 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2637 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2638 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2639 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2640 07ed472d 2019-07-12 stsp done:
2641 07ed472d 2019-07-12 stsp if (tree)
2642 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2643 07ed472d 2019-07-12 stsp if (commit)
2644 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2645 6ced4176 2019-07-12 stsp return err;
2646 6ced4176 2019-07-12 stsp }
2647 6ced4176 2019-07-12 stsp
2648 9d31a1d8 2018-03-11 stsp const struct got_error *
2649 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2650 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2651 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2652 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2653 9d31a1d8 2018-03-11 stsp {
2654 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2655 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2656 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2657 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2658 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2659 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2660 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2661 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2662 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2663 f2ea84fa 2019-07-27 stsp int entry_type;
2664 f2ea84fa 2019-07-27 stsp char *relpath;
2665 f2ea84fa 2019-07-27 stsp char *entry_name;
2666 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2667 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2668 9d31a1d8 2018-03-11 stsp
2669 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2670 f2ea84fa 2019-07-27 stsp
2671 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2672 9d31a1d8 2018-03-11 stsp if (err)
2673 9d31a1d8 2018-03-11 stsp return err;
2674 a44927cc 2022-04-07 stsp
2675 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2676 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2677 a44927cc 2022-04-07 stsp if (err)
2678 a44927cc 2022-04-07 stsp goto done;
2679 93a30277 2018-12-24 stsp
2680 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2681 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2682 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2683 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2684 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2685 f2ea84fa 2019-07-27 stsp goto done;
2686 f2ea84fa 2019-07-27 stsp }
2687 6ced4176 2019-07-12 stsp
2688 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2689 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2690 a44927cc 2022-04-07 stsp worktree, repo);
2691 f2ea84fa 2019-07-27 stsp if (err) {
2692 f2ea84fa 2019-07-27 stsp free(tpd);
2693 6ced4176 2019-07-12 stsp goto done;
2694 6ced4176 2019-07-12 stsp }
2695 f2ea84fa 2019-07-27 stsp
2696 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2697 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2698 f2ea84fa 2019-07-27 stsp if (err) {
2699 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2700 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2701 f2ea84fa 2019-07-27 stsp free(tpd);
2702 f2ea84fa 2019-07-27 stsp goto done;
2703 f2ea84fa 2019-07-27 stsp }
2704 f2ea84fa 2019-07-27 stsp } else
2705 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2706 f2ea84fa 2019-07-27 stsp
2707 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2708 6ced4176 2019-07-12 stsp }
2709 6ced4176 2019-07-12 stsp
2710 51514078 2018-12-25 stsp /*
2711 51514078 2018-12-25 stsp * Read the file index.
2712 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2713 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2714 51514078 2018-12-25 stsp */
2715 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 8da9e5f4 2019-01-12 stsp if (err)
2717 c4cdcb68 2019-04-03 stsp goto done;
2718 c4cdcb68 2019-04-03 stsp
2719 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2720 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2721 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2722 f2ea84fa 2019-07-27 stsp
2723 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2724 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2725 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2726 f2ea84fa 2019-07-27 stsp if (err)
2727 f2ea84fa 2019-07-27 stsp break;
2728 f2ea84fa 2019-07-27 stsp
2729 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2730 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2731 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2732 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2733 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2734 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2735 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2736 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2737 f2ea84fa 2019-07-27 stsp if (err)
2738 f2ea84fa 2019-07-27 stsp break;
2739 f2ea84fa 2019-07-27 stsp
2740 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2741 711d9cd9 2019-07-12 stsp }
2742 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2743 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2744 af12c6b9 2019-06-04 stsp err = sync_err;
2745 9d31a1d8 2018-03-11 stsp done:
2746 9c6338c4 2019-06-02 stsp free(fileindex_path);
2747 edfa7d7f 2018-09-11 stsp if (tree)
2748 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2749 9d31a1d8 2018-03-11 stsp if (commit)
2750 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2751 5ade8233 2019-07-12 stsp if (fileindex)
2752 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2753 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2754 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2755 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2756 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2757 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2758 f2ea84fa 2019-07-27 stsp free(tpd);
2759 f2ea84fa 2019-07-27 stsp }
2760 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2761 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2762 9d31a1d8 2018-03-11 stsp err = unlockerr;
2763 f8d1f275 2019-02-04 stsp return err;
2764 f8d1f275 2019-02-04 stsp }
2765 f8d1f275 2019-02-04 stsp
2766 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2767 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2768 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2769 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2770 234035bc 2019-06-01 stsp void *progress_arg;
2771 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2772 234035bc 2019-06-01 stsp void *cancel_arg;
2773 f69721c3 2019-10-21 stsp const char *label_orig;
2774 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2775 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2776 234035bc 2019-06-01 stsp };
2777 234035bc 2019-06-01 stsp
2778 234035bc 2019-06-01 stsp static const struct got_error *
2779 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2780 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2781 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2782 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2783 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2784 234035bc 2019-06-01 stsp {
2785 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2786 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2787 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2788 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2789 234035bc 2019-06-01 stsp struct stat sb;
2790 234035bc 2019-06-01 stsp unsigned char status;
2791 234035bc 2019-06-01 stsp int local_changes_subsumed;
2792 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2793 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2794 234035bc 2019-06-01 stsp
2795 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2796 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2797 d6c87207 2019-08-02 stsp strlen(path2));
2798 1ee397ad 2019-07-12 stsp if (ie == NULL)
2799 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2800 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2801 234035bc 2019-06-01 stsp
2802 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 234035bc 2019-06-01 stsp path2) == -1)
2804 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2805 234035bc 2019-06-01 stsp
2806 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2807 7f91a133 2019-12-13 stsp repo);
2808 234035bc 2019-06-01 stsp if (err)
2809 234035bc 2019-06-01 stsp goto done;
2810 234035bc 2019-06-01 stsp
2811 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2812 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2813 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2814 234035bc 2019-06-01 stsp goto done;
2815 234035bc 2019-06-01 stsp }
2816 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2817 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2818 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2819 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2820 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2821 234035bc 2019-06-01 stsp goto done;
2822 234035bc 2019-06-01 stsp }
2823 234035bc 2019-06-01 stsp
2824 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2825 36bf999c 2020-07-23 stsp char *link_target2;
2826 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2827 36bf999c 2020-07-23 stsp if (err)
2828 36bf999c 2020-07-23 stsp goto done;
2829 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2831 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2832 36bf999c 2020-07-23 stsp free(link_target2);
2833 af57b12a 2020-07-23 stsp } else {
2834 1af628f4 2021-06-03 stsp int fd;
2835 1af628f4 2021-06-03 stsp
2836 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2837 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2838 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2839 1af628f4 2021-06-03 stsp goto done;
2840 1af628f4 2021-06-03 stsp }
2841 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2842 1af628f4 2021-06-03 stsp f_orig, blob1);
2843 1af628f4 2021-06-03 stsp if (err)
2844 1af628f4 2021-06-03 stsp goto done;
2845 1af628f4 2021-06-03 stsp
2846 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2847 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2848 1af628f4 2021-06-03 stsp goto done;
2849 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2850 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2851 1af628f4 2021-06-03 stsp if (err)
2852 1af628f4 2021-06-03 stsp goto done;
2853 1af628f4 2021-06-03 stsp
2854 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2855 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2856 1af628f4 2021-06-03 stsp if (fd == -1) {
2857 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2858 1af628f4 2021-06-03 stsp ondisk_path);
2859 1af628f4 2021-06-03 stsp goto done;
2860 1af628f4 2021-06-03 stsp }
2861 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2862 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2863 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2864 1af628f4 2021-06-03 stsp ondisk_path);
2865 1af628f4 2021-06-03 stsp close(fd);
2866 1af628f4 2021-06-03 stsp goto done;
2867 1af628f4 2021-06-03 stsp }
2868 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2869 1af628f4 2021-06-03 stsp if (err)
2870 1af628f4 2021-06-03 stsp goto done;
2871 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2872 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2873 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2874 1af628f4 2021-06-03 stsp goto done;
2875 1af628f4 2021-06-03 stsp }
2876 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2877 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2878 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2879 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2880 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2881 af57b12a 2020-07-23 stsp }
2882 234035bc 2019-06-01 stsp } else if (blob1) {
2883 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2884 d6c87207 2019-08-02 stsp strlen(path1));
2885 1ee397ad 2019-07-12 stsp if (ie == NULL)
2886 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2887 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2888 2b92fad7 2019-06-02 stsp
2889 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 2b92fad7 2019-06-02 stsp path1) == -1)
2891 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2892 2b92fad7 2019-06-02 stsp
2893 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2894 7f91a133 2019-12-13 stsp repo);
2895 2b92fad7 2019-06-02 stsp if (err)
2896 2b92fad7 2019-06-02 stsp goto done;
2897 2b92fad7 2019-06-02 stsp
2898 2b92fad7 2019-06-02 stsp switch (status) {
2899 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2900 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2901 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2902 1ee397ad 2019-07-12 stsp if (err)
2903 1ee397ad 2019-07-12 stsp goto done;
2904 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2905 2b92fad7 2019-06-02 stsp if (err)
2906 2b92fad7 2019-06-02 stsp goto done;
2907 2b92fad7 2019-06-02 stsp if (ie)
2908 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2909 2b92fad7 2019-06-02 stsp break;
2910 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2911 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2912 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2913 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2914 1ee397ad 2019-07-12 stsp if (err)
2915 1ee397ad 2019-07-12 stsp goto done;
2916 2b92fad7 2019-06-02 stsp if (ie)
2917 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2918 2b92fad7 2019-06-02 stsp break;
2919 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2920 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2921 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2922 72840534 2022-01-19 stsp off_t blob1_size;
2923 0a22ca1a 2020-09-23 stsp /*
2924 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2925 0a22ca1a 2020-09-23 stsp * exists in the repository.
2926 0a22ca1a 2020-09-23 stsp */
2927 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2928 72840534 2022-01-19 stsp &blob1_size, path1);
2929 0a22ca1a 2020-09-23 stsp if (err)
2930 0a22ca1a 2020-09-23 stsp goto done;
2931 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2932 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2933 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2934 0a22ca1a 2020-09-23 stsp if (err)
2935 0a22ca1a 2020-09-23 stsp goto done;
2936 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2937 0a22ca1a 2020-09-23 stsp path1);
2938 0a22ca1a 2020-09-23 stsp if (err)
2939 0a22ca1a 2020-09-23 stsp goto done;
2940 0a22ca1a 2020-09-23 stsp if (ie)
2941 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2942 0a22ca1a 2020-09-23 stsp ie);
2943 0a22ca1a 2020-09-23 stsp } else {
2944 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2945 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2946 0a22ca1a 2020-09-23 stsp }
2947 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2948 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2949 0a22ca1a 2020-09-23 stsp free(id);
2950 0a22ca1a 2020-09-23 stsp if (err)
2951 0a22ca1a 2020-09-23 stsp goto done;
2952 0a22ca1a 2020-09-23 stsp break;
2953 0a22ca1a 2020-09-23 stsp }
2954 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2955 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2956 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2957 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2958 1ee397ad 2019-07-12 stsp if (err)
2959 1ee397ad 2019-07-12 stsp goto done;
2960 2b92fad7 2019-06-02 stsp break;
2961 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2962 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2963 1ee397ad 2019-07-12 stsp if (err)
2964 1ee397ad 2019-07-12 stsp goto done;
2965 2b92fad7 2019-06-02 stsp break;
2966 2b92fad7 2019-06-02 stsp default:
2967 2b92fad7 2019-06-02 stsp break;
2968 2b92fad7 2019-06-02 stsp }
2969 234035bc 2019-06-01 stsp } else if (blob2) {
2970 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2971 234035bc 2019-06-01 stsp path2) == -1)
2972 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2973 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2974 d6c87207 2019-08-02 stsp strlen(path2));
2975 234035bc 2019-06-01 stsp if (ie) {
2976 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2977 7f91a133 2019-12-13 stsp -1, NULL, repo);
2978 234035bc 2019-06-01 stsp if (err)
2979 234035bc 2019-06-01 stsp goto done;
2980 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2981 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2982 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2983 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2984 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2985 1ee397ad 2019-07-12 stsp status, path2);
2986 234035bc 2019-06-01 stsp goto done;
2987 234035bc 2019-06-01 stsp }
2988 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2989 36bf999c 2020-07-23 stsp char *link_target2;
2990 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2991 36bf999c 2020-07-23 stsp blob2);
2992 36bf999c 2020-07-23 stsp if (err)
2993 36bf999c 2020-07-23 stsp goto done;
2994 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2995 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2996 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2997 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2998 36bf999c 2020-07-23 stsp free(link_target2);
2999 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3000 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3001 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3002 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3003 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3004 526a746f 2020-07-23 stsp a->progress_arg);
3005 dfe9fba0 2020-07-23 stsp } else {
3006 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3007 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3008 526a746f 2020-07-23 stsp }
3009 dfe9fba0 2020-07-23 stsp if (err)
3010 dfe9fba0 2020-07-23 stsp goto done;
3011 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3012 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3013 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3014 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3015 234035bc 2019-06-01 stsp if (err)
3016 234035bc 2019-06-01 stsp goto done;
3017 234035bc 2019-06-01 stsp }
3018 234035bc 2019-06-01 stsp } else {
3019 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3020 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3021 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3022 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3023 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3024 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3025 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3026 2e1fa222 2020-07-23 stsp } else {
3027 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3028 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3029 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3030 2e1fa222 2020-07-23 stsp }
3031 234035bc 2019-06-01 stsp if (err)
3032 234035bc 2019-06-01 stsp goto done;
3033 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3034 234035bc 2019-06-01 stsp if (err)
3035 234035bc 2019-06-01 stsp goto done;
3036 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3037 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3038 3969253a 2020-03-07 stsp if (err) {
3039 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3040 3969253a 2020-03-07 stsp goto done;
3041 3969253a 2020-03-07 stsp }
3042 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3043 2b92fad7 2019-06-02 stsp if (err) {
3044 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3045 2b92fad7 2019-06-02 stsp goto done;
3046 2b92fad7 2019-06-02 stsp }
3047 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3048 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3049 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3050 2e1fa222 2020-07-23 stsp }
3051 234035bc 2019-06-01 stsp }
3052 234035bc 2019-06-01 stsp }
3053 234035bc 2019-06-01 stsp done:
3054 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3055 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3056 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3058 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3059 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3060 1af628f4 2021-06-03 stsp free(id_str);
3061 54d5be07 2021-06-03 stsp free(label_deriv2);
3062 234035bc 2019-06-01 stsp free(ondisk_path);
3063 234035bc 2019-06-01 stsp return err;
3064 234035bc 2019-06-01 stsp }
3065 234035bc 2019-06-01 stsp
3066 69de9dd4 2021-09-03 stsp static const struct got_error *
3067 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3068 69de9dd4 2021-09-03 stsp {
3069 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3070 69de9dd4 2021-09-03 stsp
3071 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3072 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3073 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3074 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3075 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3076 69de9dd4 2021-09-03 stsp
3077 69de9dd4 2021-09-03 stsp return NULL;
3078 69de9dd4 2021-09-03 stsp }
3079 69de9dd4 2021-09-03 stsp
3080 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3081 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3082 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3083 234035bc 2019-06-01 stsp struct got_repository *repo;
3084 234035bc 2019-06-01 stsp };
3085 234035bc 2019-06-01 stsp
3086 234035bc 2019-06-01 stsp static const struct got_error *
3087 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3088 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3089 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3090 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3091 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3092 234035bc 2019-06-01 stsp {
3093 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3094 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3095 234035bc 2019-06-01 stsp unsigned char status;
3096 234035bc 2019-06-01 stsp struct stat sb;
3097 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3098 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3099 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3100 234035bc 2019-06-01 stsp char *ondisk_path;
3101 234035bc 2019-06-01 stsp
3102 69de9dd4 2021-09-03 stsp if (id == NULL)
3103 69de9dd4 2021-09-03 stsp return NULL;
3104 234035bc 2019-06-01 stsp
3105 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3106 69de9dd4 2021-09-03 stsp if (ie == NULL)
3107 69de9dd4 2021-09-03 stsp return NULL;
3108 69de9dd4 2021-09-03 stsp
3109 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3110 234035bc 2019-06-01 stsp == -1)
3111 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3112 234035bc 2019-06-01 stsp
3113 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3114 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3115 5546d466 2021-09-02 stsp free(ondisk_path);
3116 234035bc 2019-06-01 stsp if (err)
3117 234035bc 2019-06-01 stsp return err;
3118 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3119 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3120 234035bc 2019-06-01 stsp
3121 234035bc 2019-06-01 stsp return NULL;
3122 234035bc 2019-06-01 stsp }
3123 234035bc 2019-06-01 stsp
3124 818c7501 2019-07-11 stsp static const struct got_error *
3125 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3126 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3127 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3128 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3129 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3130 234035bc 2019-06-01 stsp {
3131 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3132 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3133 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3134 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3135 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3136 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3137 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3138 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3139 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3140 234035bc 2019-06-01 stsp
3141 03415a1a 2019-06-02 stsp if (commit_id1) {
3142 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3143 a44927cc 2022-04-07 stsp if (err)
3144 a44927cc 2022-04-07 stsp goto done;
3145 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3146 03415a1a 2019-06-02 stsp worktree->path_prefix);
3147 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3148 03415a1a 2019-06-02 stsp goto done;
3149 69d57f3d 2020-07-31 stsp }
3150 69d57f3d 2020-07-31 stsp if (tree_id1) {
3151 69d57f3d 2020-07-31 stsp char *id_str;
3152 03415a1a 2019-06-02 stsp
3153 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3154 03415a1a 2019-06-02 stsp if (err)
3155 03415a1a 2019-06-02 stsp goto done;
3156 f69721c3 2019-10-21 stsp
3157 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3158 f69721c3 2019-10-21 stsp if (err)
3159 f69721c3 2019-10-21 stsp goto done;
3160 f69721c3 2019-10-21 stsp
3161 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3162 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3163 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3164 f69721c3 2019-10-21 stsp free(id_str);
3165 f69721c3 2019-10-21 stsp goto done;
3166 f69721c3 2019-10-21 stsp }
3167 f69721c3 2019-10-21 stsp free(id_str);
3168 b72706c3 2022-06-01 stsp
3169 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3170 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3171 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3172 b72706c3 2022-06-01 stsp goto done;
3173 b72706c3 2022-06-01 stsp }
3174 03415a1a 2019-06-02 stsp }
3175 234035bc 2019-06-01 stsp
3176 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3177 a44927cc 2022-04-07 stsp if (err)
3178 a44927cc 2022-04-07 stsp goto done;
3179 a44927cc 2022-04-07 stsp
3180 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3181 234035bc 2019-06-01 stsp worktree->path_prefix);
3182 234035bc 2019-06-01 stsp if (err)
3183 234035bc 2019-06-01 stsp goto done;
3184 234035bc 2019-06-01 stsp
3185 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3186 234035bc 2019-06-01 stsp if (err)
3187 234035bc 2019-06-01 stsp goto done;
3188 234035bc 2019-06-01 stsp
3189 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3190 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3191 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3192 f9d37699 2022-06-28 stsp goto done;
3193 f9d37699 2022-06-28 stsp }
3194 f9d37699 2022-06-28 stsp
3195 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3196 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3197 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3198 b72706c3 2022-06-01 stsp goto done;
3199 b72706c3 2022-06-01 stsp }
3200 b72706c3 2022-06-01 stsp
3201 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3202 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3203 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3204 f9d37699 2022-06-28 stsp goto done;
3205 f9d37699 2022-06-28 stsp }
3206 f9d37699 2022-06-28 stsp
3207 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3208 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3209 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3210 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3211 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3212 69de9dd4 2021-09-03 stsp if (err)
3213 69de9dd4 2021-09-03 stsp goto done;
3214 69de9dd4 2021-09-03 stsp
3215 234035bc 2019-06-01 stsp arg.worktree = worktree;
3216 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3217 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3218 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3219 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3220 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3221 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3222 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3223 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3224 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3226 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3227 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3228 af12c6b9 2019-06-04 stsp err = sync_err;
3229 234035bc 2019-06-01 stsp done:
3230 a44927cc 2022-04-07 stsp if (commit1)
3231 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3232 a44927cc 2022-04-07 stsp if (commit2)
3233 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3234 234035bc 2019-06-01 stsp if (tree1)
3235 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3236 234035bc 2019-06-01 stsp if (tree2)
3237 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3238 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3239 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3240 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3241 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3242 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3243 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3244 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3245 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3246 f69721c3 2019-10-21 stsp free(label_orig);
3247 818c7501 2019-07-11 stsp return err;
3248 818c7501 2019-07-11 stsp }
3249 234035bc 2019-06-01 stsp
3250 818c7501 2019-07-11 stsp const struct got_error *
3251 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3252 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3253 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3254 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3255 818c7501 2019-07-11 stsp {
3256 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3257 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3258 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3259 818c7501 2019-07-11 stsp
3260 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3261 818c7501 2019-07-11 stsp if (err)
3262 818c7501 2019-07-11 stsp return err;
3263 818c7501 2019-07-11 stsp
3264 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3265 818c7501 2019-07-11 stsp if (err)
3266 818c7501 2019-07-11 stsp goto done;
3267 818c7501 2019-07-11 stsp
3268 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3269 69de9dd4 2021-09-03 stsp worktree);
3270 818c7501 2019-07-11 stsp if (err)
3271 818c7501 2019-07-11 stsp goto done;
3272 818c7501 2019-07-11 stsp
3273 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3274 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3275 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3276 818c7501 2019-07-11 stsp done:
3277 818c7501 2019-07-11 stsp if (fileindex)
3278 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3279 818c7501 2019-07-11 stsp free(fileindex_path);
3280 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3281 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3282 234035bc 2019-06-01 stsp err = unlockerr;
3283 234035bc 2019-06-01 stsp return err;
3284 234035bc 2019-06-01 stsp }
3285 234035bc 2019-06-01 stsp
3286 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3287 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3288 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3289 927df6b7 2019-02-10 stsp const char *status_path;
3290 927df6b7 2019-02-10 stsp size_t status_path_len;
3291 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3292 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3293 f8d1f275 2019-02-04 stsp void *status_arg;
3294 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3295 f8d1f275 2019-02-04 stsp void *cancel_arg;
3296 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3297 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3298 f2a9dc41 2019-12-13 tracey int report_unchanged;
3299 3143d852 2020-06-25 stsp int no_ignores;
3300 f8d1f275 2019-02-04 stsp };
3301 88d0e355 2019-08-03 stsp
3302 f8d1f275 2019-02-04 stsp static const struct got_error *
3303 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3304 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3305 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3306 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3307 927df6b7 2019-02-10 stsp {
3308 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3309 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3310 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3311 927df6b7 2019-02-10 stsp struct stat sb;
3312 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3313 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3314 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3315 927df6b7 2019-02-10 stsp
3316 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3317 98eaaa12 2019-08-03 stsp if (err)
3318 98eaaa12 2019-08-03 stsp return err;
3319 98eaaa12 2019-08-03 stsp
3320 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3321 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3322 98eaaa12 2019-08-03 stsp return NULL;
3323 98eaaa12 2019-08-03 stsp
3324 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3325 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3326 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3327 98eaaa12 2019-08-03 stsp }
3328 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3329 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3330 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3331 927df6b7 2019-02-10 stsp }
3332 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3333 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3334 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3335 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3336 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3337 98eaaa12 2019-08-03 stsp }
3338 98eaaa12 2019-08-03 stsp
3339 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3340 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3341 927df6b7 2019-02-10 stsp }
3342 927df6b7 2019-02-10 stsp
3343 927df6b7 2019-02-10 stsp static const struct got_error *
3344 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3345 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3346 f8d1f275 2019-02-04 stsp {
3347 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3348 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3349 f8d1f275 2019-02-04 stsp char *abspath;
3350 f8d1f275 2019-02-04 stsp
3351 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3352 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3353 0584f854 2019-04-06 stsp
3354 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3355 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3356 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3357 927df6b7 2019-02-10 stsp return NULL;
3358 927df6b7 2019-02-10 stsp
3359 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3360 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3361 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3362 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3363 f8d1f275 2019-02-04 stsp } else {
3364 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3365 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3366 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3367 f8d1f275 2019-02-04 stsp }
3368 f8d1f275 2019-02-04 stsp
3369 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3370 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3371 f8d1f275 2019-02-04 stsp free(abspath);
3372 9d31a1d8 2018-03-11 stsp return err;
3373 9d31a1d8 2018-03-11 stsp }
3374 f8d1f275 2019-02-04 stsp
3375 f8d1f275 2019-02-04 stsp static const struct got_error *
3376 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3377 f8d1f275 2019-02-04 stsp {
3378 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3379 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3380 2ec1f75b 2019-03-26 stsp unsigned char status;
3381 927df6b7 2019-02-10 stsp
3382 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3383 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3384 0584f854 2019-04-06 stsp
3385 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3386 927df6b7 2019-02-10 stsp return NULL;
3387 927df6b7 2019-02-10 stsp
3388 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3389 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3390 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3391 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3392 2ec1f75b 2019-03-26 stsp else
3393 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3394 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3395 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3396 6841da00 2019-08-08 stsp }
3397 6841da00 2019-08-08 stsp
3398 336075a4 2022-06-25 op static void
3399 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3400 6841da00 2019-08-08 stsp {
3401 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3402 6841da00 2019-08-08 stsp
3403 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3404 6841da00 2019-08-08 stsp free((char *)pe->path);
3405 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3406 6841da00 2019-08-08 stsp }
3407 6841da00 2019-08-08 stsp
3408 336075a4 2022-06-25 op static void
3409 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3410 6841da00 2019-08-08 stsp {
3411 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3412 6841da00 2019-08-08 stsp
3413 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3414 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3415 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3416 6841da00 2019-08-08 stsp free((char *)pe->path);
3417 6841da00 2019-08-08 stsp }
3418 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3419 6841da00 2019-08-08 stsp }
3420 6841da00 2019-08-08 stsp
3421 6841da00 2019-08-08 stsp static const struct got_error *
3422 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3423 6841da00 2019-08-08 stsp {
3424 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3425 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3426 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3427 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3428 6841da00 2019-08-08 stsp size_t linesize = 0;
3429 6841da00 2019-08-08 stsp ssize_t linelen;
3430 6841da00 2019-08-08 stsp
3431 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3432 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3433 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3434 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3435 6841da00 2019-08-08 stsp
3436 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3437 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3438 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3439 bd8de430 2019-10-04 stsp
3440 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3441 bd8de430 2019-10-04 stsp if (line[0] == '#')
3442 bd8de430 2019-10-04 stsp continue;
3443 bd8de430 2019-10-04 stsp
3444 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3445 bd8de430 2019-10-04 stsp if (line[0] == '!')
3446 bd8de430 2019-10-04 stsp continue;
3447 bd8de430 2019-10-04 stsp
3448 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3449 6841da00 2019-08-08 stsp line) == -1) {
3450 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3451 6841da00 2019-08-08 stsp goto done;
3452 6841da00 2019-08-08 stsp }
3453 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3454 6841da00 2019-08-08 stsp if (err)
3455 6841da00 2019-08-08 stsp goto done;
3456 6841da00 2019-08-08 stsp }
3457 6841da00 2019-08-08 stsp if (ferror(f)) {
3458 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3459 6841da00 2019-08-08 stsp goto done;
3460 6841da00 2019-08-08 stsp }
3461 6841da00 2019-08-08 stsp
3462 6841da00 2019-08-08 stsp dirpath = strdup(path);
3463 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3464 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3465 6841da00 2019-08-08 stsp goto done;
3466 6841da00 2019-08-08 stsp }
3467 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3468 6841da00 2019-08-08 stsp done:
3469 6841da00 2019-08-08 stsp free(line);
3470 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3471 6841da00 2019-08-08 stsp free(dirpath);
3472 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3473 6841da00 2019-08-08 stsp }
3474 6841da00 2019-08-08 stsp return err;
3475 6841da00 2019-08-08 stsp }
3476 6841da00 2019-08-08 stsp
3477 336075a4 2022-06-25 op static int
3478 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3479 6841da00 2019-08-08 stsp {
3480 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3481 bd8de430 2019-10-04 stsp
3482 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3483 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3484 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3485 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3486 bd8de430 2019-10-04 stsp
3487 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3488 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3489 6841da00 2019-08-08 stsp
3490 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3491 bd8de430 2019-10-04 stsp continue;
3492 bd8de430 2019-10-04 stsp pattern += 3;
3493 bd8de430 2019-10-04 stsp p = path;
3494 bd8de430 2019-10-04 stsp while (*p) {
3495 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3496 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3497 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3498 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3499 bd8de430 2019-10-04 stsp p++;
3500 bd8de430 2019-10-04 stsp while (*p == '/')
3501 bd8de430 2019-10-04 stsp p++;
3502 bd8de430 2019-10-04 stsp continue;
3503 bd8de430 2019-10-04 stsp }
3504 bd8de430 2019-10-04 stsp return 1;
3505 bd8de430 2019-10-04 stsp }
3506 bd8de430 2019-10-04 stsp }
3507 bd8de430 2019-10-04 stsp }
3508 bd8de430 2019-10-04 stsp
3509 6841da00 2019-08-08 stsp /*
3510 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3511 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3512 6841da00 2019-08-08 stsp * ignores backwards.
3513 6841da00 2019-08-08 stsp */
3514 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3515 6841da00 2019-08-08 stsp while (pe) {
3516 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3517 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3518 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3519 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3520 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3521 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3522 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3523 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3524 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3525 6841da00 2019-08-08 stsp continue;
3526 6841da00 2019-08-08 stsp return 1;
3527 6841da00 2019-08-08 stsp }
3528 6841da00 2019-08-08 stsp }
3529 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3530 6841da00 2019-08-08 stsp }
3531 6841da00 2019-08-08 stsp
3532 6841da00 2019-08-08 stsp return 0;
3533 6841da00 2019-08-08 stsp }
3534 6841da00 2019-08-08 stsp
3535 6841da00 2019-08-08 stsp static const struct got_error *
3536 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3537 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3538 6841da00 2019-08-08 stsp {
3539 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3540 6841da00 2019-08-08 stsp char *ignorespath;
3541 886cec17 2019-12-15 stsp int fd = -1;
3542 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3543 6841da00 2019-08-08 stsp
3544 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3545 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3546 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3547 6841da00 2019-08-08 stsp
3548 886cec17 2019-12-15 stsp if (dirfd != -1) {
3549 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3550 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3551 886cec17 2019-12-15 stsp if (fd == -1) {
3552 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3553 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3554 886cec17 2019-12-15 stsp ignorespath);
3555 886cec17 2019-12-15 stsp } else {
3556 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3557 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3558 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3559 886cec17 2019-12-15 stsp ignorespath);
3560 886cec17 2019-12-15 stsp else {
3561 886cec17 2019-12-15 stsp fd = -1;
3562 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3563 886cec17 2019-12-15 stsp }
3564 886cec17 2019-12-15 stsp }
3565 886cec17 2019-12-15 stsp } else {
3566 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3567 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3568 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3569 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3570 886cec17 2019-12-15 stsp ignorespath);
3571 886cec17 2019-12-15 stsp } else
3572 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3573 886cec17 2019-12-15 stsp }
3574 6841da00 2019-08-08 stsp
3575 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3576 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3577 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3578 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3579 6841da00 2019-08-08 stsp free(ignorespath);
3580 6841da00 2019-08-08 stsp return err;
3581 f8d1f275 2019-02-04 stsp }
3582 f8d1f275 2019-02-04 stsp
3583 f8d1f275 2019-02-04 stsp static const struct got_error *
3584 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3585 62da3196 2021-10-01 stsp int dirfd)
3586 f8d1f275 2019-02-04 stsp {
3587 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3588 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3589 f8d1f275 2019-02-04 stsp char *path = NULL;
3590 62da3196 2021-10-01 stsp
3591 62da3196 2021-10-01 stsp if (ignore != NULL)
3592 62da3196 2021-10-01 stsp *ignore = 0;
3593 f8d1f275 2019-02-04 stsp
3594 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3596 0584f854 2019-04-06 stsp
3597 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3598 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3600 f8d1f275 2019-02-04 stsp } else {
3601 f8d1f275 2019-02-04 stsp path = de->d_name;
3602 f8d1f275 2019-02-04 stsp }
3603 f8d1f275 2019-02-04 stsp
3604 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3605 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3606 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3607 62da3196 2021-10-01 stsp *ignore = 1;
3608 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3609 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3610 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3611 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3612 f8d1f275 2019-02-04 stsp if (parent_path[0])
3613 f8d1f275 2019-02-04 stsp free(path);
3614 b72f483a 2019-02-05 stsp return err;
3615 f8d1f275 2019-02-04 stsp }
3616 f8d1f275 2019-02-04 stsp
3617 347d1d3e 2019-07-12 stsp static const struct got_error *
3618 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3619 3143d852 2020-06-25 stsp {
3620 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3621 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3622 3143d852 2020-06-25 stsp
3623 3143d852 2020-06-25 stsp if (a->no_ignores)
3624 3143d852 2020-06-25 stsp return NULL;
3625 3143d852 2020-06-25 stsp
3626 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3627 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3628 3143d852 2020-06-25 stsp if (err)
3629 3143d852 2020-06-25 stsp return err;
3630 3143d852 2020-06-25 stsp
3631 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3632 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3633 3143d852 2020-06-25 stsp
3634 3143d852 2020-06-25 stsp return err;
3635 3143d852 2020-06-25 stsp }
3636 3143d852 2020-06-25 stsp
3637 3143d852 2020-06-25 stsp static const struct got_error *
3638 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3639 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3640 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3641 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3642 abb4604f 2019-07-27 stsp {
3643 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3644 abb4604f 2019-07-27 stsp struct stat sb;
3645 ff56836b 2021-07-08 stsp
3646 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3647 abb4604f 2019-07-27 stsp if (ie)
3648 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3649 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3650 abb4604f 2019-07-27 stsp
3651 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3652 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3653 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3654 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3656 abb4604f 2019-07-27 stsp }
3657 abb4604f 2019-07-27 stsp
3658 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3659 916237f3 2022-02-11 stsp return NULL;
3660 916237f3 2022-02-11 stsp
3661 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3662 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3663 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3664 abb4604f 2019-07-27 stsp
3665 abb4604f 2019-07-27 stsp return NULL;
3666 3143d852 2020-06-25 stsp }
3667 3143d852 2020-06-25 stsp
3668 3143d852 2020-06-25 stsp static const struct got_error *
3669 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3670 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3671 3143d852 2020-06-25 stsp {
3672 3143d852 2020-06-25 stsp const struct got_error *err;
3673 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3674 3143d852 2020-06-25 stsp
3675 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3676 3143d852 2020-06-25 stsp ".cvsignore");
3677 3143d852 2020-06-25 stsp if (err)
3678 3143d852 2020-06-25 stsp return err;
3679 3143d852 2020-06-25 stsp
3680 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3681 3143d852 2020-06-25 stsp ".gitignore");
3682 3143d852 2020-06-25 stsp if (err)
3683 3143d852 2020-06-25 stsp return err;
3684 3143d852 2020-06-25 stsp
3685 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3686 3143d852 2020-06-25 stsp if (err) {
3687 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3688 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3689 3143d852 2020-06-25 stsp return err;
3690 3143d852 2020-06-25 stsp }
3691 3143d852 2020-06-25 stsp for (;;) {
3692 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3693 3143d852 2020-06-25 stsp ".cvsignore");
3694 3143d852 2020-06-25 stsp if (err)
3695 3143d852 2020-06-25 stsp break;
3696 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3697 3143d852 2020-06-25 stsp ".gitignore");
3698 3143d852 2020-06-25 stsp if (err)
3699 3143d852 2020-06-25 stsp break;
3700 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3701 3143d852 2020-06-25 stsp if (err) {
3702 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3703 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3704 3143d852 2020-06-25 stsp break;
3705 3143d852 2020-06-25 stsp }
3706 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3707 ff56836b 2021-07-08 stsp break;
3708 b737c85a 2020-06-26 stsp free(parent_path);
3709 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3710 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3711 3143d852 2020-06-25 stsp }
3712 3143d852 2020-06-25 stsp
3713 b737c85a 2020-06-26 stsp free(parent_path);
3714 b737c85a 2020-06-26 stsp free(next_parent_path);
3715 3143d852 2020-06-25 stsp return err;
3716 abb4604f 2019-07-27 stsp }
3717 abb4604f 2019-07-27 stsp
3718 abb4604f 2019-07-27 stsp static const struct got_error *
3719 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3720 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3721 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3722 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3723 f2a9dc41 2019-12-13 tracey int report_unchanged)
3724 f8d1f275 2019-02-04 stsp {
3725 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3726 6fc93f37 2019-12-13 stsp int fd = -1;
3727 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3728 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3729 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3730 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3731 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3732 3143d852 2020-06-25 stsp
3733 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3734 f8d1f275 2019-02-04 stsp
3735 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3736 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3737 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3738 8dc303cc 2019-07-27 stsp
3739 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3740 a84c0d30 2022-03-12 stsp if (ie) {
3741 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3742 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3743 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3744 a84c0d30 2022-03-12 stsp goto done;
3745 a84c0d30 2022-03-12 stsp }
3746 a84c0d30 2022-03-12 stsp
3747 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3748 6fc93f37 2019-12-13 stsp if (fd == -1) {
3749 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3750 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3751 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3752 ff56836b 2021-07-08 stsp else {
3753 ff56836b 2021-07-08 stsp if (!no_ignores) {
3754 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3755 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3756 ff56836b 2021-07-08 stsp if (err)
3757 ff56836b 2021-07-08 stsp goto done;
3758 ff56836b 2021-07-08 stsp }
3759 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3760 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3761 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3762 ff56836b 2021-07-08 stsp }
3763 abb4604f 2019-07-27 stsp } else {
3764 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3765 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3766 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3767 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3768 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3769 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3770 abb4604f 2019-07-27 stsp arg.status_path = path;
3771 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3772 abb4604f 2019-07-27 stsp arg.repo = repo;
3773 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3774 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3775 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3776 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3777 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3778 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3779 022fae89 2019-12-06 tracey if (!no_ignores) {
3780 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3781 3143d852 2020-06-25 stsp worktree->root_path, path);
3782 3143d852 2020-06-25 stsp if (err)
3783 3143d852 2020-06-25 stsp goto done;
3784 022fae89 2019-12-06 tracey }
3785 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3786 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3787 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3788 927df6b7 2019-02-10 stsp }
3789 3143d852 2020-06-25 stsp done:
3790 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3791 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3792 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3793 927df6b7 2019-02-10 stsp free(ondisk_path);
3794 347d1d3e 2019-07-12 stsp return err;
3795 347d1d3e 2019-07-12 stsp }
3796 347d1d3e 2019-07-12 stsp
3797 347d1d3e 2019-07-12 stsp const struct got_error *
3798 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3799 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3800 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3801 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3802 347d1d3e 2019-07-12 stsp {
3803 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3804 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3805 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3806 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3807 347d1d3e 2019-07-12 stsp
3808 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3809 347d1d3e 2019-07-12 stsp if (err)
3810 347d1d3e 2019-07-12 stsp return err;
3811 347d1d3e 2019-07-12 stsp
3812 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3813 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3814 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3815 f6343036 2021-06-22 stsp no_ignores, 0);
3816 72ea6654 2019-07-27 stsp if (err)
3817 72ea6654 2019-07-27 stsp break;
3818 72ea6654 2019-07-27 stsp }
3819 f8d1f275 2019-02-04 stsp free(fileindex_path);
3820 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3821 f8d1f275 2019-02-04 stsp return err;
3822 f8d1f275 2019-02-04 stsp }
3823 6c7ab921 2019-03-18 stsp
3824 6c7ab921 2019-03-18 stsp const struct got_error *
3825 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3826 6c7ab921 2019-03-18 stsp const char *arg)
3827 6c7ab921 2019-03-18 stsp {
3828 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3829 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3830 6c7ab921 2019-03-18 stsp size_t len;
3831 00bb5ea0 2020-07-23 stsp struct stat sb;
3832 01740607 2020-11-04 stsp char *abspath = NULL;
3833 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3834 6c7ab921 2019-03-18 stsp
3835 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3836 6c7ab921 2019-03-18 stsp
3837 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3838 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3839 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3840 00bb5ea0 2020-07-23 stsp
3841 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3842 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3843 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3844 00bb5ea0 2020-07-23 stsp goto done;
3845 00bb5ea0 2020-07-23 stsp }
3846 727173c3 2020-11-06 stsp sb.st_mode = 0;
3847 00bb5ea0 2020-07-23 stsp }
3848 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3849 00bb5ea0 2020-07-23 stsp /*
3850 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3851 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3852 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3853 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3854 00bb5ea0 2020-07-23 stsp */
3855 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3856 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3857 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3858 00bb5ea0 2020-07-23 stsp goto done;
3859 00bb5ea0 2020-07-23 stsp }
3860 00bb5ea0 2020-07-23 stsp
3861 00bb5ea0 2020-07-23 stsp }
3862 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3863 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3864 00bb5ea0 2020-07-23 stsp if (err)
3865 d0710d08 2019-07-22 stsp goto done;
3866 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3867 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3868 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3869 00bb5ea0 2020-07-23 stsp goto done;
3870 00bb5ea0 2020-07-23 stsp }
3871 00bb5ea0 2020-07-23 stsp } else {
3872 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3873 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3874 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3875 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3876 00bb5ea0 2020-07-23 stsp goto done;
3877 00bb5ea0 2020-07-23 stsp }
3878 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3880 01740607 2020-11-04 stsp goto done;
3881 01740607 2020-11-04 stsp }
3882 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3883 01740607 2020-11-04 stsp sizeof(canonpath));
3884 01740607 2020-11-04 stsp if (err)
3885 00bb5ea0 2020-07-23 stsp goto done;
3886 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3887 01740607 2020-11-04 stsp if (resolved == NULL) {
3888 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3889 01740607 2020-11-04 stsp goto done;
3890 00bb5ea0 2020-07-23 stsp }
3891 d0710d08 2019-07-22 stsp }
3892 d0710d08 2019-07-22 stsp }
3893 6c7ab921 2019-03-18 stsp
3894 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3895 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3896 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3897 6c7ab921 2019-03-18 stsp goto done;
3898 6c7ab921 2019-03-18 stsp }
3899 6c7ab921 2019-03-18 stsp
3900 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3901 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3902 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3903 f6d88e1a 2019-05-29 stsp if (err)
3904 f6d88e1a 2019-05-29 stsp goto done;
3905 f6d88e1a 2019-05-29 stsp } else {
3906 f6d88e1a 2019-05-29 stsp path = strdup("");
3907 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3908 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3909 f6d88e1a 2019-05-29 stsp goto done;
3910 f6d88e1a 2019-05-29 stsp }
3911 6c7ab921 2019-03-18 stsp }
3912 6c7ab921 2019-03-18 stsp
3913 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3914 6c7ab921 2019-03-18 stsp len = strlen(path);
3915 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3916 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3917 6c7ab921 2019-03-18 stsp len--;
3918 6c7ab921 2019-03-18 stsp }
3919 6c7ab921 2019-03-18 stsp done:
3920 01740607 2020-11-04 stsp free(abspath);
3921 6c7ab921 2019-03-18 stsp free(resolved);
3922 d0710d08 2019-07-22 stsp free(cwd);
3923 6c7ab921 2019-03-18 stsp if (err == NULL)
3924 6c7ab921 2019-03-18 stsp *wt_path = path;
3925 6c7ab921 2019-03-18 stsp else
3926 6c7ab921 2019-03-18 stsp free(path);
3927 d00136be 2019-03-26 stsp return err;
3928 d00136be 2019-03-26 stsp }
3929 d00136be 2019-03-26 stsp
3930 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3931 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3932 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3933 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3934 4e68cba3 2019-11-23 stsp void *progress_arg;
3935 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3936 4e68cba3 2019-11-23 stsp };
3937 4e68cba3 2019-11-23 stsp
3938 4e68cba3 2019-11-23 stsp static const struct got_error *
3939 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3940 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3941 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3942 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3943 07f5b47a 2019-06-02 stsp {
3944 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3945 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3946 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3947 6d022e97 2019-08-04 stsp struct stat sb;
3948 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3949 07f5b47a 2019-06-02 stsp
3950 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3951 dbb83fbd 2019-12-12 stsp relpath) == -1)
3952 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3953 dbb83fbd 2019-12-12 stsp
3954 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3955 6d022e97 2019-08-04 stsp if (ie) {
3956 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3957 12463d8b 2019-12-13 stsp de_name, a->repo);
3958 6d022e97 2019-08-04 stsp if (err)
3959 dbb83fbd 2019-12-12 stsp goto done;
3960 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3961 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3962 dbb83fbd 2019-12-12 stsp goto done;
3963 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3964 dbb83fbd 2019-12-12 stsp if (err)
3965 dbb83fbd 2019-12-12 stsp goto done;
3966 6d022e97 2019-08-04 stsp }
3967 07f5b47a 2019-06-02 stsp
3968 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3969 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3970 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3971 9b4603c0 2022-01-31 stsp else
3972 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3973 dbb83fbd 2019-12-12 stsp goto done;
3974 4e68cba3 2019-11-23 stsp }
3975 07f5b47a 2019-06-02 stsp
3976 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3977 4e68cba3 2019-11-23 stsp if (err)
3978 4e68cba3 2019-11-23 stsp goto done;
3979 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3980 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3981 3969253a 2020-03-07 stsp if (err) {
3982 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3983 3969253a 2020-03-07 stsp goto done;
3984 3969253a 2020-03-07 stsp }
3985 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3986 07f5b47a 2019-06-02 stsp if (err) {
3987 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3988 4e68cba3 2019-11-23 stsp goto done;
3989 07f5b47a 2019-06-02 stsp }
3990 4e68cba3 2019-11-23 stsp done:
3991 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3992 dbb83fbd 2019-12-12 stsp if (err)
3993 dbb83fbd 2019-12-12 stsp return err;
3994 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3995 dbb83fbd 2019-12-12 stsp return NULL;
3996 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3997 07f5b47a 2019-06-02 stsp }
3998 07f5b47a 2019-06-02 stsp
3999 d00136be 2019-03-26 stsp const struct got_error *
4000 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4001 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4002 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4004 d00136be 2019-03-26 stsp {
4005 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4006 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4007 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4008 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4009 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4010 d00136be 2019-03-26 stsp
4011 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4012 d00136be 2019-03-26 stsp if (err)
4013 d00136be 2019-03-26 stsp return err;
4014 d00136be 2019-03-26 stsp
4015 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4016 d00136be 2019-03-26 stsp if (err)
4017 d00136be 2019-03-26 stsp goto done;
4018 d00136be 2019-03-26 stsp
4019 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4020 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4021 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4022 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4023 4e68cba3 2019-11-23 stsp saa.repo = repo;
4024 4e68cba3 2019-11-23 stsp
4025 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4026 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4027 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4028 1dd54920 2019-05-11 stsp if (err)
4029 af12c6b9 2019-06-04 stsp break;
4030 1dd54920 2019-05-11 stsp }
4031 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4032 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4033 af12c6b9 2019-06-04 stsp err = sync_err;
4034 d00136be 2019-03-26 stsp done:
4035 fb399478 2019-07-12 stsp free(fileindex_path);
4036 d00136be 2019-03-26 stsp if (fileindex)
4037 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4038 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4039 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4040 d00136be 2019-03-26 stsp err = unlockerr;
4041 6c7ab921 2019-03-18 stsp return err;
4042 6c7ab921 2019-03-18 stsp }
4043 17ed4618 2019-06-02 stsp
4044 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4045 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4046 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4047 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4048 f2a9dc41 2019-12-13 tracey void *progress_arg;
4049 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4050 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4051 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4052 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4053 766841c2 2020-08-13 stsp const char *status_codes;
4054 f2a9dc41 2019-12-13 tracey };
4055 f2a9dc41 2019-12-13 tracey
4056 f2a9dc41 2019-12-13 tracey static const struct got_error *
4057 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4058 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4059 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4061 17ed4618 2019-06-02 stsp {
4062 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4063 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4064 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4065 17ed4618 2019-06-02 stsp struct stat sb;
4066 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4067 4e12cd97 2022-01-25 stsp
4068 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4069 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4070 4e12cd97 2022-01-25 stsp return NULL;
4071 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4072 4e12cd97 2022-01-25 stsp }
4073 17ed4618 2019-06-02 stsp
4074 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4075 17ed4618 2019-06-02 stsp if (ie == NULL)
4076 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4077 2ec1f75b 2019-03-26 stsp
4078 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4079 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4080 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4081 9acbc4fa 2019-08-03 stsp return NULL;
4082 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4083 9acbc4fa 2019-08-03 stsp }
4084 9acbc4fa 2019-08-03 stsp
4085 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4086 f2a9dc41 2019-12-13 tracey relpath) == -1)
4087 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4088 f2a9dc41 2019-12-13 tracey
4089 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4090 12463d8b 2019-12-13 stsp a->repo);
4091 17ed4618 2019-06-02 stsp if (err)
4092 f2a9dc41 2019-12-13 tracey goto done;
4093 17ed4618 2019-06-02 stsp
4094 766841c2 2020-08-13 stsp if (a->status_codes) {
4095 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4096 766841c2 2020-08-13 stsp int i;
4097 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4098 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4099 766841c2 2020-08-13 stsp break;
4100 766841c2 2020-08-13 stsp }
4101 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4102 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4103 766841c2 2020-08-13 stsp free(ondisk_path);
4104 766841c2 2020-08-13 stsp return NULL;
4105 766841c2 2020-08-13 stsp }
4106 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4107 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4108 766841c2 2020-08-13 stsp static char msg[64];
4109 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4110 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4111 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4112 766841c2 2020-08-13 stsp goto done;
4113 766841c2 2020-08-13 stsp }
4114 766841c2 2020-08-13 stsp }
4115 766841c2 2020-08-13 stsp
4116 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4117 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4118 f2a9dc41 2019-12-13 tracey goto done;
4119 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4120 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4121 f2a9dc41 2019-12-13 tracey goto done;
4122 f2a9dc41 2019-12-13 tracey }
4123 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4124 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4125 4e12cd97 2022-01-25 stsp goto done;
4126 4e12cd97 2022-01-25 stsp }
4127 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4128 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4129 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4130 f2a9dc41 2019-12-13 tracey goto done;
4131 f2a9dc41 2019-12-13 tracey }
4132 17ed4618 2019-06-02 stsp }
4133 17ed4618 2019-06-02 stsp
4134 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4135 20a2ad1c 2020-10-20 stsp size_t root_len;
4136 20a2ad1c 2020-10-20 stsp
4137 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4138 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4139 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4140 12463d8b 2019-12-13 stsp ondisk_path);
4141 12463d8b 2019-12-13 stsp goto done;
4142 12463d8b 2019-12-13 stsp }
4143 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4144 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4145 12463d8b 2019-12-13 stsp goto done;
4146 15341bfd 2020-03-05 tracey }
4147 15341bfd 2020-03-05 tracey
4148 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4149 20a2ad1c 2020-10-20 stsp do {
4150 20a2ad1c 2020-10-20 stsp char *parent;
4151 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4152 20a2ad1c 2020-10-20 stsp if (err)
4153 2513f20a 2020-10-20 stsp goto done;
4154 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4155 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4156 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4157 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4158 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4159 20a2ad1c 2020-10-20 stsp ondisk_path);
4160 15341bfd 2020-03-05 tracey break;
4161 15341bfd 2020-03-05 tracey }
4162 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4163 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4164 f2a9dc41 2019-12-13 tracey }
4165 17ed4618 2019-06-02 stsp
4166 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4167 f2a9dc41 2019-12-13 tracey done:
4168 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4169 f2a9dc41 2019-12-13 tracey if (err)
4170 f2a9dc41 2019-12-13 tracey return err;
4171 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4172 f2a9dc41 2019-12-13 tracey return NULL;
4173 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4174 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4175 17ed4618 2019-06-02 stsp }
4176 17ed4618 2019-06-02 stsp
4177 2ec1f75b 2019-03-26 stsp const struct got_error *
4178 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4179 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4180 766841c2 2020-08-13 stsp const char *status_codes,
4181 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4182 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4183 2ec1f75b 2019-03-26 stsp {
4184 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4185 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4186 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4187 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4188 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4189 2ec1f75b 2019-03-26 stsp
4190 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4191 2ec1f75b 2019-03-26 stsp if (err)
4192 2ec1f75b 2019-03-26 stsp return err;
4193 2ec1f75b 2019-03-26 stsp
4194 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4195 2ec1f75b 2019-03-26 stsp if (err)
4196 2ec1f75b 2019-03-26 stsp goto done;
4197 f2a9dc41 2019-12-13 tracey
4198 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4199 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4200 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4201 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4202 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4203 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4204 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4205 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4206 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4207 2ec1f75b 2019-03-26 stsp
4208 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4209 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4210 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4211 17ed4618 2019-06-02 stsp if (err)
4212 af12c6b9 2019-06-04 stsp break;
4213 2ec1f75b 2019-03-26 stsp }
4214 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4215 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4216 af12c6b9 2019-06-04 stsp err = sync_err;
4217 2ec1f75b 2019-03-26 stsp done:
4218 fb399478 2019-07-12 stsp free(fileindex_path);
4219 2ec1f75b 2019-03-26 stsp if (fileindex)
4220 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4221 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4222 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4223 2ec1f75b 2019-03-26 stsp err = unlockerr;
4224 33aa809d 2019-08-08 stsp return err;
4225 33aa809d 2019-08-08 stsp }
4226 33aa809d 2019-08-08 stsp
4227 33aa809d 2019-08-08 stsp static const struct got_error *
4228 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4229 33aa809d 2019-08-08 stsp {
4230 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4231 33aa809d 2019-08-08 stsp char *line = NULL;
4232 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4233 33aa809d 2019-08-08 stsp ssize_t linelen;
4234 33aa809d 2019-08-08 stsp
4235 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4236 33aa809d 2019-08-08 stsp if (linelen == -1) {
4237 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4238 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4239 33aa809d 2019-08-08 stsp goto done;
4240 33aa809d 2019-08-08 stsp }
4241 33aa809d 2019-08-08 stsp return NULL;
4242 33aa809d 2019-08-08 stsp }
4243 33aa809d 2019-08-08 stsp if (outfile) {
4244 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4245 33aa809d 2019-08-08 stsp if (n != linelen) {
4246 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4247 33aa809d 2019-08-08 stsp goto done;
4248 33aa809d 2019-08-08 stsp }
4249 33aa809d 2019-08-08 stsp }
4250 33aa809d 2019-08-08 stsp if (rejectfile) {
4251 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4252 33aa809d 2019-08-08 stsp if (n != linelen)
4253 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4254 33aa809d 2019-08-08 stsp }
4255 33aa809d 2019-08-08 stsp done:
4256 33aa809d 2019-08-08 stsp free(line);
4257 2ec1f75b 2019-03-26 stsp return err;
4258 2ec1f75b 2019-03-26 stsp }
4259 1f1abb7e 2019-08-08 stsp
4260 33aa809d 2019-08-08 stsp static const struct got_error *
4261 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4262 33aa809d 2019-08-08 stsp {
4263 33aa809d 2019-08-08 stsp char *line = NULL;
4264 33aa809d 2019-08-08 stsp size_t linesize = 0;
4265 33aa809d 2019-08-08 stsp ssize_t linelen;
4266 33aa809d 2019-08-08 stsp
4267 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4268 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4269 500467ff 2019-09-25 hiltjo if (ferror(f))
4270 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4271 500467ff 2019-09-25 hiltjo return NULL;
4272 500467ff 2019-09-25 hiltjo }
4273 33aa809d 2019-08-08 stsp free(line);
4274 33aa809d 2019-08-08 stsp return NULL;
4275 33aa809d 2019-08-08 stsp }
4276 33aa809d 2019-08-08 stsp
4277 33aa809d 2019-08-08 stsp static const struct got_error *
4278 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4280 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4281 abc59930 2021-09-05 naddy {
4282 33aa809d 2019-08-08 stsp const struct got_error *err;
4283 33aa809d 2019-08-08 stsp
4284 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4285 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4286 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4287 33aa809d 2019-08-08 stsp if (err)
4288 33aa809d 2019-08-08 stsp return err;
4289 33aa809d 2019-08-08 stsp (*line_cur1)++;
4290 33aa809d 2019-08-08 stsp }
4291 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4292 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4293 33aa809d 2019-08-08 stsp if (rejectfile)
4294 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4295 33aa809d 2019-08-08 stsp else
4296 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4297 33aa809d 2019-08-08 stsp if (err)
4298 33aa809d 2019-08-08 stsp return err;
4299 33aa809d 2019-08-08 stsp (*line_cur2)++;
4300 33aa809d 2019-08-08 stsp }
4301 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4302 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4303 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4304 33aa809d 2019-08-08 stsp if (err)
4305 33aa809d 2019-08-08 stsp return err;
4306 33aa809d 2019-08-08 stsp (*line_cur2)++;
4307 33aa809d 2019-08-08 stsp }
4308 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4309 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4310 33aa809d 2019-08-08 stsp if (rejectfile)
4311 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4312 33aa809d 2019-08-08 stsp else
4313 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4314 33aa809d 2019-08-08 stsp if (err)
4315 33aa809d 2019-08-08 stsp return err;
4316 33aa809d 2019-08-08 stsp (*line_cur1)++;
4317 33aa809d 2019-08-08 stsp }
4318 f1e81a05 2019-08-10 stsp
4319 f1e81a05 2019-08-10 stsp return NULL;
4320 f1e81a05 2019-08-10 stsp }
4321 f1e81a05 2019-08-10 stsp
4322 f1e81a05 2019-08-10 stsp static const struct got_error *
4323 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4324 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4325 f1e81a05 2019-08-10 stsp {
4326 f1e81a05 2019-08-10 stsp const struct got_error *err;
4327 f1e81a05 2019-08-10 stsp
4328 f1e81a05 2019-08-10 stsp if (outfile) {
4329 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4330 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4331 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4332 f1e81a05 2019-08-10 stsp if (err)
4333 f1e81a05 2019-08-10 stsp return err;
4334 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4335 f1e81a05 2019-08-10 stsp }
4336 f1e81a05 2019-08-10 stsp }
4337 f1e81a05 2019-08-10 stsp if (rejectfile) {
4338 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4339 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4340 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4341 f1e81a05 2019-08-10 stsp if (err)
4342 f1e81a05 2019-08-10 stsp return err;
4343 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4344 f1e81a05 2019-08-10 stsp }
4345 33aa809d 2019-08-08 stsp }
4346 33aa809d 2019-08-08 stsp
4347 33aa809d 2019-08-08 stsp return NULL;
4348 33aa809d 2019-08-08 stsp }
4349 33aa809d 2019-08-08 stsp
4350 33aa809d 2019-08-08 stsp static const struct got_error *
4351 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4352 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4353 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4354 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4355 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4356 33aa809d 2019-08-08 stsp {
4357 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4358 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4359 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4360 33aa809d 2019-08-08 stsp FILE *hunkfile;
4361 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4362 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4363 fe621944 2020-11-10 stsp int rc;
4364 33aa809d 2019-08-08 stsp
4365 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4366 33aa809d 2019-08-08 stsp
4367 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4368 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4369 fe621944 2020-11-10 stsp start_old = cc.left.start;
4370 fe621944 2020-11-10 stsp end_old = cc.left.end;
4371 fe621944 2020-11-10 stsp start_new = cc.right.start;
4372 fe621944 2020-11-10 stsp end_new = cc.right.end;
4373 33aa809d 2019-08-08 stsp
4374 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4375 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4376 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4377 33aa809d 2019-08-08 stsp
4378 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4379 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4380 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4381 33aa809d 2019-08-08 stsp
4382 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4383 fe621944 2020-11-10 stsp if (diff_state == NULL)
4384 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4385 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4386 fe621944 2020-11-10 stsp
4387 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4388 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4389 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4390 33aa809d 2019-08-08 stsp goto done;
4391 33aa809d 2019-08-08 stsp }
4392 fe621944 2020-11-10 stsp
4393 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4394 fe621944 2020-11-10 stsp diff_result, &cc);
4395 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4396 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4397 33aa809d 2019-08-08 stsp goto done;
4398 33aa809d 2019-08-08 stsp }
4399 fe621944 2020-11-10 stsp
4400 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4401 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4402 33aa809d 2019-08-08 stsp goto done;
4403 33aa809d 2019-08-08 stsp }
4404 33aa809d 2019-08-08 stsp
4405 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4406 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4407 33aa809d 2019-08-08 stsp if (err)
4408 33aa809d 2019-08-08 stsp goto done;
4409 33aa809d 2019-08-08 stsp
4410 33aa809d 2019-08-08 stsp switch (*choice) {
4411 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4412 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4413 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4414 33aa809d 2019-08-08 stsp break;
4415 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4416 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4417 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4418 33aa809d 2019-08-08 stsp break;
4419 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4420 33aa809d 2019-08-08 stsp break;
4421 33aa809d 2019-08-08 stsp default:
4422 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4423 33aa809d 2019-08-08 stsp break;
4424 33aa809d 2019-08-08 stsp }
4425 33aa809d 2019-08-08 stsp done:
4426 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4427 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4428 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4429 33aa809d 2019-08-08 stsp return err;
4430 33aa809d 2019-08-08 stsp }
4431 33aa809d 2019-08-08 stsp
4432 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4433 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4434 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4435 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4436 1f1abb7e 2019-08-08 stsp void *progress_arg;
4437 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4438 33aa809d 2019-08-08 stsp void *patch_arg;
4439 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4440 f259c4c1 2021-09-24 stsp int unlink_added_files;
4441 1f1abb7e 2019-08-08 stsp };
4442 a129376b 2019-03-28 stsp
4443 e20a8b6f 2019-06-04 stsp static const struct got_error *
4444 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4445 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4446 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4447 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4448 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4449 33aa809d 2019-08-08 stsp {
4450 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4451 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4452 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4453 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4454 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4455 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4456 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4457 fe621944 2020-11-10 stsp struct stat sb2;
4458 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4459 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4460 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4461 33aa809d 2019-08-08 stsp
4462 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4463 33aa809d 2019-08-08 stsp
4464 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4465 33aa809d 2019-08-08 stsp if (err)
4466 33aa809d 2019-08-08 stsp return err;
4467 33aa809d 2019-08-08 stsp
4468 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4469 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4470 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4471 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4472 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4473 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4474 fa3cef63 2020-07-23 stsp goto done;
4475 fa3cef63 2020-07-23 stsp }
4476 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4477 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4478 c0df5966 2021-12-31 stsp if (link_len == -1) {
4479 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4480 c0df5966 2021-12-31 stsp path2);
4481 c0df5966 2021-12-31 stsp }
4482 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4483 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4484 12463d8b 2019-12-13 stsp }
4485 12463d8b 2019-12-13 stsp } else {
4486 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4487 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4488 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4489 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4490 fa3cef63 2020-07-23 stsp goto done;
4491 fa3cef63 2020-07-23 stsp }
4492 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4493 fa3cef63 2020-07-23 stsp sizeof(link_target));
4494 fa3cef63 2020-07-23 stsp if (link_len == -1)
4495 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4496 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4497 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4498 12463d8b 2019-12-13 stsp }
4499 1ebedb77 2019-10-19 stsp }
4500 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4501 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4502 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4503 fa3cef63 2020-07-23 stsp goto done;
4504 fa3cef63 2020-07-23 stsp }
4505 1ebedb77 2019-10-19 stsp
4506 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4507 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4508 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4509 fa3cef63 2020-07-23 stsp goto done;
4510 fa3cef63 2020-07-23 stsp }
4511 fa3cef63 2020-07-23 stsp fd2 = -1;
4512 fa3cef63 2020-07-23 stsp } else {
4513 fa3cef63 2020-07-23 stsp size_t n;
4514 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4515 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4516 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4517 fa3cef63 2020-07-23 stsp goto done;
4518 fa3cef63 2020-07-23 stsp }
4519 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4520 fa3cef63 2020-07-23 stsp if (n != link_len) {
4521 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4522 fa3cef63 2020-07-23 stsp goto done;
4523 fa3cef63 2020-07-23 stsp }
4524 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4525 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4526 fa3cef63 2020-07-23 stsp goto done;
4527 fa3cef63 2020-07-23 stsp }
4528 fa3cef63 2020-07-23 stsp rewind(f2);
4529 33aa809d 2019-08-08 stsp }
4530 33aa809d 2019-08-08 stsp
4531 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4532 eb81bc23 2022-06-28 tracey if (fd == -1) {
4533 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4534 eb81bc23 2022-06-28 tracey goto done;
4535 eb81bc23 2022-06-28 tracey }
4536 eb81bc23 2022-06-28 tracey
4537 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4538 33aa809d 2019-08-08 stsp if (err)
4539 33aa809d 2019-08-08 stsp goto done;
4540 33aa809d 2019-08-08 stsp
4541 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4542 33aa809d 2019-08-08 stsp if (err)
4543 33aa809d 2019-08-08 stsp goto done;
4544 33aa809d 2019-08-08 stsp
4545 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4546 33aa809d 2019-08-08 stsp if (err)
4547 33aa809d 2019-08-08 stsp goto done;
4548 33aa809d 2019-08-08 stsp
4549 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4550 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4551 33aa809d 2019-08-08 stsp if (err)
4552 33aa809d 2019-08-08 stsp goto done;
4553 33aa809d 2019-08-08 stsp
4554 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4555 33aa809d 2019-08-08 stsp if (err)
4556 33aa809d 2019-08-08 stsp goto done;
4557 33aa809d 2019-08-08 stsp
4558 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4559 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4560 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4561 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4562 fe621944 2020-11-10 stsp
4563 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4564 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4565 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4566 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4567 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4568 fe621944 2020-11-10 stsp nchanges++;
4569 fe621944 2020-11-10 stsp }
4570 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4571 33aa809d 2019-08-08 stsp int choice;
4572 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4573 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4574 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4575 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4576 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4577 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4578 33aa809d 2019-08-08 stsp if (err)
4579 33aa809d 2019-08-08 stsp goto done;
4580 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4581 33aa809d 2019-08-08 stsp have_content = 1;
4582 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4583 33aa809d 2019-08-08 stsp break;
4584 33aa809d 2019-08-08 stsp }
4585 1ebedb77 2019-10-19 stsp if (have_content) {
4586 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4587 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4588 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4589 1ebedb77 2019-10-19 stsp if (err)
4590 1ebedb77 2019-10-19 stsp goto done;
4591 1ebedb77 2019-10-19 stsp
4592 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4593 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4594 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4595 fa3cef63 2020-07-23 stsp goto done;
4596 fa3cef63 2020-07-23 stsp }
4597 1ebedb77 2019-10-19 stsp }
4598 1ebedb77 2019-10-19 stsp }
4599 33aa809d 2019-08-08 stsp done:
4600 33aa809d 2019-08-08 stsp free(id_str);
4601 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4602 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4603 33aa809d 2019-08-08 stsp if (blob)
4604 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4605 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4606 fe621944 2020-11-10 stsp if (err == NULL)
4607 fe621944 2020-11-10 stsp err = free_err;
4608 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4609 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4610 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4611 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4612 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4614 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4615 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4616 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4617 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4618 33aa809d 2019-08-08 stsp if (err || !have_content) {
4619 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4621 33aa809d 2019-08-08 stsp free(*path_outfile);
4622 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4623 33aa809d 2019-08-08 stsp }
4624 33aa809d 2019-08-08 stsp free(path1);
4625 33aa809d 2019-08-08 stsp return err;
4626 33aa809d 2019-08-08 stsp }
4627 33aa809d 2019-08-08 stsp
4628 33aa809d 2019-08-08 stsp static const struct got_error *
4629 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4631 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4633 a129376b 2019-03-28 stsp {
4634 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4635 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4636 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4637 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4638 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4639 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4640 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4641 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4642 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4643 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4644 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4645 eb81bc23 2022-06-28 tracey int fd = -1;
4646 a129376b 2019-03-28 stsp
4647 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4648 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4649 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4650 d3bcc3d1 2019-08-08 stsp return NULL;
4651 3d69ad8d 2019-08-17 semarie
4652 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4653 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4654 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4655 d3bcc3d1 2019-08-08 stsp
4656 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4657 65084dad 2019-08-08 stsp if (ie == NULL)
4658 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4659 a129376b 2019-03-28 stsp
4660 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4661 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4662 a129376b 2019-03-28 stsp if (err) {
4663 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4664 a129376b 2019-03-28 stsp goto done;
4665 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4666 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4667 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4668 e20a8b6f 2019-06-04 stsp goto done;
4669 e20a8b6f 2019-06-04 stsp }
4670 a129376b 2019-03-28 stsp }
4671 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4673 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4674 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4675 a129376b 2019-03-28 stsp goto done;
4676 a129376b 2019-03-28 stsp }
4677 a129376b 2019-03-28 stsp } else {
4678 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4679 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4680 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4681 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4682 a129376b 2019-03-28 stsp goto done;
4683 a129376b 2019-03-28 stsp }
4684 a129376b 2019-03-28 stsp } else {
4685 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4686 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4687 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4688 a129376b 2019-03-28 stsp goto done;
4689 a129376b 2019-03-28 stsp }
4690 a129376b 2019-03-28 stsp }
4691 a129376b 2019-03-28 stsp }
4692 a129376b 2019-03-28 stsp
4693 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4694 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4695 a44927cc 2022-04-07 stsp if (err)
4696 a44927cc 2022-04-07 stsp goto done;
4697 a44927cc 2022-04-07 stsp
4698 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4699 a9fa2909 2019-07-27 stsp if (err) {
4700 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4701 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4702 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4703 a9fa2909 2019-07-27 stsp goto done;
4704 a9fa2909 2019-07-27 stsp } else {
4705 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4706 a9fa2909 2019-07-27 stsp if (err)
4707 a9fa2909 2019-07-27 stsp goto done;
4708 a9fa2909 2019-07-27 stsp
4709 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4710 1233e6b6 2020-10-19 stsp if (err)
4711 a9fa2909 2019-07-27 stsp goto done;
4712 a9fa2909 2019-07-27 stsp
4713 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4714 1233e6b6 2020-10-19 stsp free(te_name);
4715 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4716 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4717 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4718 a9fa2909 2019-07-27 stsp goto done;
4719 a9fa2909 2019-07-27 stsp }
4720 a129376b 2019-03-28 stsp }
4721 a129376b 2019-03-28 stsp
4722 a129376b 2019-03-28 stsp switch (status) {
4723 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4724 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4725 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4726 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4727 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4728 33aa809d 2019-08-08 stsp if (err)
4729 33aa809d 2019-08-08 stsp goto done;
4730 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4731 33aa809d 2019-08-08 stsp break;
4732 33aa809d 2019-08-08 stsp }
4733 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4734 1f1abb7e 2019-08-08 stsp ie->path);
4735 1ee397ad 2019-07-12 stsp if (err)
4736 1ee397ad 2019-07-12 stsp goto done;
4737 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4738 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4739 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4740 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4741 f259c4c1 2021-09-24 stsp relpath) == -1) {
4742 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4743 f259c4c1 2021-09-24 stsp goto done;
4744 f259c4c1 2021-09-24 stsp }
4745 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4746 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4747 f259c4c1 2021-09-24 stsp ondisk_path);
4748 f259c4c1 2021-09-24 stsp break;
4749 f259c4c1 2021-09-24 stsp }
4750 f259c4c1 2021-09-24 stsp }
4751 a129376b 2019-03-28 stsp break;
4752 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4753 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4754 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4755 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4756 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4757 33aa809d 2019-08-08 stsp if (err)
4758 33aa809d 2019-08-08 stsp goto done;
4759 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4760 33aa809d 2019-08-08 stsp break;
4761 33aa809d 2019-08-08 stsp }
4762 33aa809d 2019-08-08 stsp /* fall through */
4763 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4764 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4765 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4766 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4767 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4768 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4769 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4770 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4771 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4772 24278f30 2019-08-03 stsp } else
4773 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4774 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4775 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4776 eb81bc23 2022-06-28 tracey if (fd == -1) {
4777 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4778 eb81bc23 2022-06-28 tracey goto done;
4779 eb81bc23 2022-06-28 tracey }
4780 eb81bc23 2022-06-28 tracey
4781 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4782 a129376b 2019-03-28 stsp if (err)
4783 65084dad 2019-08-08 stsp goto done;
4784 65084dad 2019-08-08 stsp
4785 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4786 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4787 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4788 a129376b 2019-03-28 stsp goto done;
4789 65084dad 2019-08-08 stsp }
4790 65084dad 2019-08-08 stsp
4791 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4792 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4793 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4794 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4795 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4796 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4797 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4798 33aa809d 2019-08-08 stsp break;
4799 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4800 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4801 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4802 369fd7e5 2020-07-23 stsp path_content);
4803 369fd7e5 2020-07-23 stsp break;
4804 369fd7e5 2020-07-23 stsp }
4805 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4806 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4807 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4808 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4809 369fd7e5 2020-07-23 stsp } else {
4810 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4811 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4812 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4813 369fd7e5 2020-07-23 stsp goto done;
4814 369fd7e5 2020-07-23 stsp }
4815 33aa809d 2019-08-08 stsp }
4816 33aa809d 2019-08-08 stsp } else {
4817 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4818 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4819 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4820 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4821 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4822 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4823 2e1fa222 2020-07-23 stsp } else {
4824 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4825 2e1fa222 2020-07-23 stsp ie->path,
4826 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4827 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4828 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4829 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4830 2e1fa222 2020-07-23 stsp }
4831 a129376b 2019-03-28 stsp if (err)
4832 a129376b 2019-03-28 stsp goto done;
4833 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4834 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4835 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4836 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4837 437adc9d 2020-12-10 yzhong blob->id.sha1,
4838 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4839 2e1fa222 2020-07-23 stsp if (err)
4840 2e1fa222 2020-07-23 stsp goto done;
4841 2e1fa222 2020-07-23 stsp }
4842 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4843 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4844 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4845 33aa809d 2019-08-08 stsp }
4846 a129376b 2019-03-28 stsp }
4847 a129376b 2019-03-28 stsp break;
4848 e20a8b6f 2019-06-04 stsp }
4849 a129376b 2019-03-28 stsp default:
4850 1f1abb7e 2019-08-08 stsp break;
4851 a129376b 2019-03-28 stsp }
4852 a129376b 2019-03-28 stsp done:
4853 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4854 33aa809d 2019-08-08 stsp free(path_content);
4855 e20a8b6f 2019-06-04 stsp free(parent_path);
4856 a129376b 2019-03-28 stsp free(tree_path);
4857 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4858 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4859 a129376b 2019-03-28 stsp if (blob)
4860 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4861 a129376b 2019-03-28 stsp if (tree)
4862 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4863 a129376b 2019-03-28 stsp free(tree_id);
4864 a44927cc 2022-04-07 stsp if (base_commit)
4865 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4866 e20a8b6f 2019-06-04 stsp return err;
4867 e20a8b6f 2019-06-04 stsp }
4868 e20a8b6f 2019-06-04 stsp
4869 e20a8b6f 2019-06-04 stsp const struct got_error *
4870 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4871 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4872 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4873 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4874 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4875 e20a8b6f 2019-06-04 stsp {
4876 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4877 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4878 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4879 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4880 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4881 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4882 e20a8b6f 2019-06-04 stsp
4883 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4884 e20a8b6f 2019-06-04 stsp if (err)
4885 e20a8b6f 2019-06-04 stsp return err;
4886 e20a8b6f 2019-06-04 stsp
4887 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4888 e20a8b6f 2019-06-04 stsp if (err)
4889 e20a8b6f 2019-06-04 stsp goto done;
4890 e20a8b6f 2019-06-04 stsp
4891 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4892 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4893 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4894 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4895 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4896 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4897 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4898 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4899 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4900 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4901 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4902 e20a8b6f 2019-06-04 stsp if (err)
4903 af12c6b9 2019-06-04 stsp break;
4904 e20a8b6f 2019-06-04 stsp }
4905 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4906 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4907 e20a8b6f 2019-06-04 stsp err = sync_err;
4908 af12c6b9 2019-06-04 stsp done:
4909 fb399478 2019-07-12 stsp free(fileindex_path);
4910 a129376b 2019-03-28 stsp if (fileindex)
4911 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4912 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4913 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4914 a129376b 2019-03-28 stsp err = unlockerr;
4915 c4296144 2019-05-09 stsp return err;
4916 c4296144 2019-05-09 stsp }
4917 c4296144 2019-05-09 stsp
4918 cf066bf8 2019-05-09 stsp static void
4919 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4920 cf066bf8 2019-05-09 stsp {
4921 24519714 2019-05-09 stsp free(ct->path);
4922 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4923 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4924 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4925 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4926 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4927 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4928 cf066bf8 2019-05-09 stsp free(ct);
4929 cf066bf8 2019-05-09 stsp }
4930 24519714 2019-05-09 stsp
4931 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4932 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4933 24519714 2019-05-09 stsp struct got_repository *repo;
4934 24519714 2019-05-09 stsp struct got_worktree *worktree;
4935 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4936 5f8a88c6 2019-08-03 stsp int have_staged_files;
4937 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4938 24519714 2019-05-09 stsp };
4939 cf066bf8 2019-05-09 stsp
4940 c4296144 2019-05-09 stsp static const struct got_error *
4941 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4942 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4943 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4944 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4945 c4296144 2019-05-09 stsp {
4946 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4947 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4948 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4949 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4950 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4951 768aea60 2019-05-09 stsp struct stat sb;
4952 c4296144 2019-05-09 stsp
4953 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4954 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4955 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4956 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4957 5f8a88c6 2019-08-03 stsp return NULL;
4958 5f8a88c6 2019-08-03 stsp } else {
4959 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4960 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4961 c4296144 2019-05-09 stsp
4962 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4963 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4964 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4965 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4966 5f8a88c6 2019-08-03 stsp return NULL;
4967 5f8a88c6 2019-08-03 stsp }
4968 0b5cc0d6 2019-05-09 stsp
4969 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4970 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4971 036813ee 2019-05-09 stsp goto done;
4972 036813ee 2019-05-09 stsp }
4973 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4974 036813ee 2019-05-09 stsp parent_path = strdup("");
4975 69960a46 2019-05-09 stsp if (parent_path == NULL)
4976 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4977 69960a46 2019-05-09 stsp } else {
4978 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4979 69960a46 2019-05-09 stsp if (err)
4980 69960a46 2019-05-09 stsp return err;
4981 69960a46 2019-05-09 stsp }
4982 c4296144 2019-05-09 stsp
4983 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4984 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4985 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4986 c4296144 2019-05-09 stsp goto done;
4987 768aea60 2019-05-09 stsp }
4988 768aea60 2019-05-09 stsp
4989 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4990 768aea60 2019-05-09 stsp relpath) == -1) {
4991 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4992 768aea60 2019-05-09 stsp goto done;
4993 768aea60 2019-05-09 stsp }
4994 0aeb8099 2020-07-23 stsp
4995 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4996 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4997 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4998 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4999 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5000 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5001 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5002 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5003 0aeb8099 2020-07-23 stsp break;
5004 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5005 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5006 0aeb8099 2020-07-23 stsp break;
5007 0aeb8099 2020-07-23 stsp default:
5008 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5009 0aeb8099 2020-07-23 stsp goto done;
5010 0aeb8099 2020-07-23 stsp }
5011 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5012 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5013 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5014 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5015 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5016 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5017 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5018 12463d8b 2019-12-13 stsp ct->ondisk_path);
5019 12463d8b 2019-12-13 stsp goto done;
5020 12463d8b 2019-12-13 stsp }
5021 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5022 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5023 768aea60 2019-05-09 stsp goto done;
5024 768aea60 2019-05-09 stsp }
5025 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5026 c4296144 2019-05-09 stsp }
5027 c4296144 2019-05-09 stsp
5028 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5029 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5030 44d03001 2019-05-09 stsp relpath) == -1) {
5031 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5032 44d03001 2019-05-09 stsp goto done;
5033 44d03001 2019-05-09 stsp }
5034 44d03001 2019-05-09 stsp
5035 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5036 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5037 35213c7c 2020-07-23 stsp int is_bad_symlink;
5038 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5039 35213c7c 2020-07-23 stsp ssize_t target_len;
5040 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5041 35213c7c 2020-07-23 stsp sizeof(target_path));
5042 35213c7c 2020-07-23 stsp if (target_len == -1) {
5043 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5044 35213c7c 2020-07-23 stsp ct->ondisk_path);
5045 35213c7c 2020-07-23 stsp goto done;
5046 35213c7c 2020-07-23 stsp }
5047 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5048 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5049 35213c7c 2020-07-23 stsp if (err)
5050 35213c7c 2020-07-23 stsp goto done;
5051 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5052 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5053 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5054 35213c7c 2020-07-23 stsp goto done;
5055 35213c7c 2020-07-23 stsp }
5056 35213c7c 2020-07-23 stsp }
5057 35213c7c 2020-07-23 stsp
5058 35213c7c 2020-07-23 stsp
5059 cf066bf8 2019-05-09 stsp ct->status = status;
5060 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5061 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5062 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5063 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5064 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5065 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5066 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5067 b416585c 2019-05-13 stsp goto done;
5068 b416585c 2019-05-13 stsp }
5069 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5070 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5071 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5072 f0b75401 2019-08-03 stsp goto done;
5073 f0b75401 2019-08-03 stsp }
5074 f0b75401 2019-08-03 stsp }
5075 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5076 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5077 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5078 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5079 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5080 036813ee 2019-05-09 stsp goto done;
5081 036813ee 2019-05-09 stsp }
5082 ca2503ea 2019-05-09 stsp }
5083 24519714 2019-05-09 stsp ct->path = strdup(path);
5084 24519714 2019-05-09 stsp if (ct->path == NULL) {
5085 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5086 24519714 2019-05-09 stsp goto done;
5087 24519714 2019-05-09 stsp }
5088 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5089 c4296144 2019-05-09 stsp done:
5090 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5091 ed175427 2019-05-09 stsp free_commitable(ct);
5092 24519714 2019-05-09 stsp free(parent_path);
5093 036813ee 2019-05-09 stsp free(path);
5094 a129376b 2019-03-28 stsp return err;
5095 a129376b 2019-03-28 stsp }
5096 c4296144 2019-05-09 stsp
5097 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5098 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5099 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5100 036813ee 2019-05-09 stsp struct got_repository *);
5101 ed175427 2019-05-09 stsp
5102 ed175427 2019-05-09 stsp static const struct got_error *
5103 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5104 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5105 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5106 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5107 afa376bf 2019-05-09 stsp struct got_repository *repo)
5108 ed175427 2019-05-09 stsp {
5109 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5110 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5111 036813ee 2019-05-09 stsp char *subpath;
5112 ed175427 2019-05-09 stsp
5113 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5114 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5115 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5116 ed175427 2019-05-09 stsp
5117 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5118 ed175427 2019-05-09 stsp if (err)
5119 ed175427 2019-05-09 stsp return err;
5120 ed175427 2019-05-09 stsp
5121 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5122 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5123 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5124 036813ee 2019-05-09 stsp free(subpath);
5125 036813ee 2019-05-09 stsp return err;
5126 036813ee 2019-05-09 stsp }
5127 ed175427 2019-05-09 stsp
5128 036813ee 2019-05-09 stsp static const struct got_error *
5129 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5130 036813ee 2019-05-09 stsp {
5131 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5132 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5133 ed175427 2019-05-09 stsp
5134 036813ee 2019-05-09 stsp *match = 0;
5135 ed175427 2019-05-09 stsp
5136 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5137 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5138 0f63689d 2019-05-10 stsp return NULL;
5139 036813ee 2019-05-09 stsp }
5140 0b5cc0d6 2019-05-09 stsp
5141 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5142 0f63689d 2019-05-10 stsp if (err)
5143 0f63689d 2019-05-10 stsp return err;
5144 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5145 036813ee 2019-05-09 stsp free(ct_parent_path);
5146 036813ee 2019-05-09 stsp return err;
5147 036813ee 2019-05-09 stsp }
5148 0b5cc0d6 2019-05-09 stsp
5149 768aea60 2019-05-09 stsp static mode_t
5150 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5151 768aea60 2019-05-09 stsp {
5152 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5153 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5154 3d9a4ec4 2020-07-23 stsp
5155 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5156 768aea60 2019-05-09 stsp }
5157 768aea60 2019-05-09 stsp
5158 036813ee 2019-05-09 stsp static const struct got_error *
5159 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5160 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5161 036813ee 2019-05-09 stsp {
5162 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5163 ca2503ea 2019-05-09 stsp
5164 036813ee 2019-05-09 stsp *new_te = NULL;
5165 0b5cc0d6 2019-05-09 stsp
5166 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5167 036813ee 2019-05-09 stsp if (err)
5168 036813ee 2019-05-09 stsp goto done;
5169 0b5cc0d6 2019-05-09 stsp
5170 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5171 036813ee 2019-05-09 stsp
5172 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5173 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5174 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5175 5f8a88c6 2019-08-03 stsp else
5176 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5177 036813ee 2019-05-09 stsp done:
5178 036813ee 2019-05-09 stsp if (err && *new_te) {
5179 56e0773d 2019-11-28 stsp free(*new_te);
5180 036813ee 2019-05-09 stsp *new_te = NULL;
5181 036813ee 2019-05-09 stsp }
5182 036813ee 2019-05-09 stsp return err;
5183 036813ee 2019-05-09 stsp }
5184 036813ee 2019-05-09 stsp
5185 036813ee 2019-05-09 stsp static const struct got_error *
5186 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5187 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5188 036813ee 2019-05-09 stsp {
5189 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5190 102b254e 2020-10-19 stsp char *ct_name = NULL;
5191 036813ee 2019-05-09 stsp
5192 036813ee 2019-05-09 stsp *new_te = NULL;
5193 036813ee 2019-05-09 stsp
5194 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5195 036813ee 2019-05-09 stsp if (*new_te == NULL)
5196 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5197 036813ee 2019-05-09 stsp
5198 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5199 102b254e 2020-10-19 stsp if (err)
5200 036813ee 2019-05-09 stsp goto done;
5201 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5202 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5203 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5204 036813ee 2019-05-09 stsp goto done;
5205 036813ee 2019-05-09 stsp }
5206 036813ee 2019-05-09 stsp
5207 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5208 036813ee 2019-05-09 stsp
5209 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5210 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5211 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5212 5f8a88c6 2019-08-03 stsp else
5213 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5214 036813ee 2019-05-09 stsp done:
5215 102b254e 2020-10-19 stsp free(ct_name);
5216 036813ee 2019-05-09 stsp if (err && *new_te) {
5217 56e0773d 2019-11-28 stsp free(*new_te);
5218 036813ee 2019-05-09 stsp *new_te = NULL;
5219 036813ee 2019-05-09 stsp }
5220 036813ee 2019-05-09 stsp return err;
5221 036813ee 2019-05-09 stsp }
5222 036813ee 2019-05-09 stsp
5223 036813ee 2019-05-09 stsp static const struct got_error *
5224 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5225 036813ee 2019-05-09 stsp struct got_pathlist_head *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 *new_pe;
5229 036813ee 2019-05-09 stsp
5230 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5231 036813ee 2019-05-09 stsp if (err)
5232 036813ee 2019-05-09 stsp return err;
5233 036813ee 2019-05-09 stsp if (new_pe == NULL)
5234 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5235 036813ee 2019-05-09 stsp return NULL;
5236 afa376bf 2019-05-09 stsp }
5237 afa376bf 2019-05-09 stsp
5238 afa376bf 2019-05-09 stsp static const struct got_error *
5239 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5240 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5241 afa376bf 2019-05-09 stsp {
5242 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5243 5f8a88c6 2019-08-03 stsp unsigned char status;
5244 0ff8d236 2021-09-28 stsp
5245 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5246 0ff8d236 2021-09-28 stsp return NULL;
5247 5f8a88c6 2019-08-03 stsp
5248 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5249 afa376bf 2019-05-09 stsp ct_path++;
5250 5f8a88c6 2019-08-03 stsp
5251 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5252 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5253 5f8a88c6 2019-08-03 stsp else
5254 5f8a88c6 2019-08-03 stsp status = ct->status;
5255 5f8a88c6 2019-08-03 stsp
5256 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5257 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5258 036813ee 2019-05-09 stsp }
5259 036813ee 2019-05-09 stsp
5260 036813ee 2019-05-09 stsp static const struct got_error *
5261 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5262 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5263 44d03001 2019-05-09 stsp {
5264 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5265 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5266 44d03001 2019-05-09 stsp char *te_path;
5267 44d03001 2019-05-09 stsp
5268 44d03001 2019-05-09 stsp *modified = 0;
5269 44d03001 2019-05-09 stsp
5270 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5271 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5272 44d03001 2019-05-09 stsp te->name) == -1)
5273 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5274 44d03001 2019-05-09 stsp
5275 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5276 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5277 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5278 44d03001 2019-05-09 stsp strlen(te_path));
5279 62d463ca 2020-10-20 naddy if (*modified)
5280 44d03001 2019-05-09 stsp break;
5281 44d03001 2019-05-09 stsp }
5282 44d03001 2019-05-09 stsp
5283 44d03001 2019-05-09 stsp free(te_path);
5284 44d03001 2019-05-09 stsp return err;
5285 44d03001 2019-05-09 stsp }
5286 44d03001 2019-05-09 stsp
5287 44d03001 2019-05-09 stsp static const struct got_error *
5288 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5289 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5290 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5291 036813ee 2019-05-09 stsp {
5292 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5293 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5294 036813ee 2019-05-09 stsp
5295 036813ee 2019-05-09 stsp *ctp = NULL;
5296 036813ee 2019-05-09 stsp
5297 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5299 036813ee 2019-05-09 stsp char *ct_name = NULL;
5300 036813ee 2019-05-09 stsp int path_matches;
5301 036813ee 2019-05-09 stsp
5302 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5303 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5304 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5305 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5306 5f8a88c6 2019-08-03 stsp continue;
5307 5f8a88c6 2019-08-03 stsp } else {
5308 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5309 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5310 5f8a88c6 2019-08-03 stsp continue;
5311 5f8a88c6 2019-08-03 stsp }
5312 036813ee 2019-05-09 stsp
5313 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5314 036813ee 2019-05-09 stsp continue;
5315 036813ee 2019-05-09 stsp
5316 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5317 62d463ca 2020-10-20 naddy if (err)
5318 036813ee 2019-05-09 stsp return err;
5319 036813ee 2019-05-09 stsp if (!path_matches)
5320 036813ee 2019-05-09 stsp continue;
5321 036813ee 2019-05-09 stsp
5322 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5323 d34b633e 2020-10-19 stsp if (err)
5324 d34b633e 2020-10-19 stsp return err;
5325 d34b633e 2020-10-19 stsp
5326 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5327 d34b633e 2020-10-19 stsp free(ct_name);
5328 036813ee 2019-05-09 stsp continue;
5329 d34b633e 2020-10-19 stsp }
5330 d34b633e 2020-10-19 stsp free(ct_name);
5331 036813ee 2019-05-09 stsp
5332 036813ee 2019-05-09 stsp *ctp = ct;
5333 036813ee 2019-05-09 stsp break;
5334 036813ee 2019-05-09 stsp }
5335 2b496619 2019-07-10 stsp
5336 2b496619 2019-07-10 stsp return err;
5337 2b496619 2019-07-10 stsp }
5338 2b496619 2019-07-10 stsp
5339 2b496619 2019-07-10 stsp static const struct got_error *
5340 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5341 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5342 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5343 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5344 2b496619 2019-07-10 stsp struct got_repository *repo)
5345 2b496619 2019-07-10 stsp {
5346 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5347 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5348 2b496619 2019-07-10 stsp char *subtree_path;
5349 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5350 ba580f68 2020-03-22 stsp int nentries;
5351 2b496619 2019-07-10 stsp
5352 2b496619 2019-07-10 stsp *new_tep = NULL;
5353 2b496619 2019-07-10 stsp
5354 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5355 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5356 2b496619 2019-07-10 stsp child_path) == -1)
5357 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5358 036813ee 2019-05-09 stsp
5359 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5360 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5361 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5362 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5363 56e0773d 2019-11-28 stsp
5364 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5365 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5366 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5367 2b496619 2019-07-10 stsp goto done;
5368 2b496619 2019-07-10 stsp }
5369 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5370 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5371 2b496619 2019-07-10 stsp if (err) {
5372 56e0773d 2019-11-28 stsp free(new_te);
5373 2b496619 2019-07-10 stsp goto done;
5374 2b496619 2019-07-10 stsp }
5375 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5376 2b496619 2019-07-10 stsp done:
5377 56e0773d 2019-11-28 stsp free(id);
5378 2b496619 2019-07-10 stsp free(subtree_path);
5379 2b496619 2019-07-10 stsp if (err == NULL)
5380 2b496619 2019-07-10 stsp *new_tep = new_te;
5381 036813ee 2019-05-09 stsp return err;
5382 036813ee 2019-05-09 stsp }
5383 036813ee 2019-05-09 stsp
5384 036813ee 2019-05-09 stsp static const struct got_error *
5385 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5386 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5387 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5388 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5389 036813ee 2019-05-09 stsp struct got_repository *repo)
5390 036813ee 2019-05-09 stsp {
5391 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5392 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5393 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5394 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5395 036813ee 2019-05-09 stsp
5396 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5397 ba580f68 2020-03-22 stsp *nentries = 0;
5398 036813ee 2019-05-09 stsp
5399 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5400 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5401 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5402 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5403 036813ee 2019-05-09 stsp
5404 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5405 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5406 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5407 036813ee 2019-05-09 stsp continue;
5408 036813ee 2019-05-09 stsp
5409 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5410 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5411 036813ee 2019-05-09 stsp continue;
5412 036813ee 2019-05-09 stsp
5413 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5414 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5415 036813ee 2019-05-09 stsp if (err)
5416 036813ee 2019-05-09 stsp goto done;
5417 036813ee 2019-05-09 stsp
5418 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5419 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5420 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5421 036813ee 2019-05-09 stsp if (err)
5422 036813ee 2019-05-09 stsp goto done;
5423 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5424 afa376bf 2019-05-09 stsp if (err)
5425 afa376bf 2019-05-09 stsp goto done;
5426 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5427 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5428 2b496619 2019-07-10 stsp if (err)
5429 2f51b5b3 2019-05-09 stsp goto done;
5430 ba580f68 2020-03-22 stsp (*nentries)++;
5431 2b496619 2019-07-10 stsp } else {
5432 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5433 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5434 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5435 2b496619 2019-07-10 stsp == NULL) {
5436 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5437 2b496619 2019-07-10 stsp child_path, path_base_tree,
5438 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5439 2b496619 2019-07-10 stsp repo);
5440 2b496619 2019-07-10 stsp if (err)
5441 2b496619 2019-07-10 stsp goto done;
5442 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5443 2b496619 2019-07-10 stsp if (err)
5444 2b496619 2019-07-10 stsp goto done;
5445 ba580f68 2020-03-22 stsp (*nentries)++;
5446 9ba0479c 2019-05-10 stsp }
5447 ed175427 2019-05-09 stsp }
5448 2f51b5b3 2019-05-09 stsp }
5449 2f51b5b3 2019-05-09 stsp
5450 2f51b5b3 2019-05-09 stsp if (base_tree) {
5451 56e0773d 2019-11-28 stsp int i, nbase_entries;
5452 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5453 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5454 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5455 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5456 63c5ca5d 2019-08-24 stsp
5457 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5458 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5459 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5460 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5461 63c5ca5d 2019-08-24 stsp if (err)
5462 63c5ca5d 2019-08-24 stsp goto done;
5463 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5464 63c5ca5d 2019-08-24 stsp if (err)
5465 63c5ca5d 2019-08-24 stsp goto done;
5466 ba580f68 2020-03-22 stsp (*nentries)++;
5467 63c5ca5d 2019-08-24 stsp continue;
5468 63c5ca5d 2019-08-24 stsp }
5469 2f51b5b3 2019-05-09 stsp
5470 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5471 44d03001 2019-05-09 stsp int modified;
5472 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5473 036813ee 2019-05-09 stsp if (err)
5474 036813ee 2019-05-09 stsp goto done;
5475 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5476 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5477 2f51b5b3 2019-05-09 stsp if (err)
5478 2f51b5b3 2019-05-09 stsp goto done;
5479 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5480 44d03001 2019-05-09 stsp if (modified) {
5481 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5482 ba580f68 2020-03-22 stsp int nsubentries;
5483 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5484 ba580f68 2020-03-22 stsp &nsubentries, te,
5485 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5486 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5487 44d03001 2019-05-09 stsp if (err)
5488 44d03001 2019-05-09 stsp goto done;
5489 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5490 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5491 ba580f68 2020-03-22 stsp free(new_id);
5492 ba580f68 2020-03-22 stsp continue;
5493 ba580f68 2020-03-22 stsp }
5494 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5495 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5496 56e0773d 2019-11-28 stsp free(new_id);
5497 44d03001 2019-05-09 stsp }
5498 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5499 036813ee 2019-05-09 stsp if (err)
5500 036813ee 2019-05-09 stsp goto done;
5501 ba580f68 2020-03-22 stsp (*nentries)++;
5502 2f51b5b3 2019-05-09 stsp continue;
5503 036813ee 2019-05-09 stsp }
5504 2f51b5b3 2019-05-09 stsp
5505 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5506 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5507 f66c734c 2019-09-22 stsp if (err)
5508 f66c734c 2019-09-22 stsp goto done;
5509 2f51b5b3 2019-05-09 stsp if (ct) {
5510 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5511 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5512 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5513 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5514 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5515 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5516 2f51b5b3 2019-05-09 stsp if (err)
5517 2f51b5b3 2019-05-09 stsp goto done;
5518 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5519 2f51b5b3 2019-05-09 stsp if (err)
5520 2f51b5b3 2019-05-09 stsp goto done;
5521 ba580f68 2020-03-22 stsp (*nentries)++;
5522 2f51b5b3 2019-05-09 stsp }
5523 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5524 b416585c 2019-05-13 stsp status_arg);
5525 afa376bf 2019-05-09 stsp if (err)
5526 afa376bf 2019-05-09 stsp goto done;
5527 2f51b5b3 2019-05-09 stsp } else {
5528 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5529 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5530 2f51b5b3 2019-05-09 stsp if (err)
5531 2f51b5b3 2019-05-09 stsp goto done;
5532 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5533 2f51b5b3 2019-05-09 stsp if (err)
5534 2f51b5b3 2019-05-09 stsp goto done;
5535 ba580f68 2020-03-22 stsp (*nentries)++;
5536 2f51b5b3 2019-05-09 stsp }
5537 ca2503ea 2019-05-09 stsp }
5538 ed175427 2019-05-09 stsp }
5539 0b5cc0d6 2019-05-09 stsp
5540 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5541 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5542 ed175427 2019-05-09 stsp done:
5543 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5544 2e1fa222 2020-07-23 stsp return err;
5545 2e1fa222 2020-07-23 stsp }
5546 2e1fa222 2020-07-23 stsp
5547 2e1fa222 2020-07-23 stsp static const struct got_error *
5548 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5549 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5550 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5551 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5552 ebf99748 2019-05-09 stsp {
5553 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5554 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5555 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5556 ebf99748 2019-05-09 stsp
5557 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5558 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5559 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5560 ebf99748 2019-05-09 stsp
5561 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5562 437adc9d 2020-12-10 yzhong
5563 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5564 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5565 437adc9d 2020-12-10 yzhong if (err)
5566 437adc9d 2020-12-10 yzhong goto done;
5567 437adc9d 2020-12-10 yzhong
5568 ebf99748 2019-05-09 stsp if (ie) {
5569 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5570 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5571 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5572 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5573 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5574 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5575 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5576 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5577 437adc9d 2020-12-10 yzhong
5578 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5579 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5580 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5581 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5582 72fd46fa 2019-09-06 stsp !have_staged_files);
5583 ebf99748 2019-05-09 stsp } else
5584 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5585 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5586 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5587 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5588 72fd46fa 2019-09-06 stsp !have_staged_files);
5589 ebf99748 2019-05-09 stsp } else {
5590 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5591 ebf99748 2019-05-09 stsp if (err)
5592 437adc9d 2020-12-10 yzhong goto done;
5593 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5594 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5595 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5596 3969253a 2020-03-07 stsp if (err) {
5597 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5598 437adc9d 2020-12-10 yzhong goto done;
5599 3969253a 2020-03-07 stsp }
5600 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5601 3969253a 2020-03-07 stsp if (err) {
5602 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5603 437adc9d 2020-12-10 yzhong goto done;
5604 3969253a 2020-03-07 stsp }
5605 ebf99748 2019-05-09 stsp }
5606 437adc9d 2020-12-10 yzhong free(relpath);
5607 437adc9d 2020-12-10 yzhong relpath = NULL;
5608 ebf99748 2019-05-09 stsp }
5609 437adc9d 2020-12-10 yzhong done:
5610 437adc9d 2020-12-10 yzhong free(relpath);
5611 d56d26ce 2019-05-10 stsp return err;
5612 d56d26ce 2019-05-10 stsp }
5613 735ef5ac 2019-08-03 stsp
5614 d56d26ce 2019-05-10 stsp
5615 d56d26ce 2019-05-10 stsp static const struct got_error *
5616 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5617 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5618 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5619 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5620 735ef5ac 2019-08-03 stsp int ood_errcode)
5621 d56d26ce 2019-05-10 stsp {
5622 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5623 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5624 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5625 1a36436d 2019-06-10 stsp
5626 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5627 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5628 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5629 1a36436d 2019-06-10 stsp return NULL;
5630 9bead371 2019-07-28 stsp /*
5631 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5632 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5633 9bead371 2019-07-28 stsp */
5634 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5635 a44927cc 2022-04-07 stsp if (err)
5636 a44927cc 2022-04-07 stsp goto done;
5637 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5638 9bead371 2019-07-28 stsp if (err) {
5639 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5640 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5641 1a36436d 2019-06-10 stsp goto done;
5642 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5643 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5644 1a36436d 2019-06-10 stsp } else {
5645 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5646 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5647 a44927cc 2022-04-07 stsp if (err)
5648 a44927cc 2022-04-07 stsp goto done;
5649 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5650 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5651 1a36436d 2019-06-10 stsp goto done;
5652 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5653 1a36436d 2019-06-10 stsp }
5654 1a36436d 2019-06-10 stsp done:
5655 1a36436d 2019-06-10 stsp free(id);
5656 a44927cc 2022-04-07 stsp if (commit)
5657 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5658 1a36436d 2019-06-10 stsp return err;
5659 ebf99748 2019-05-09 stsp }
5660 ebf99748 2019-05-09 stsp
5661 336075a4 2022-06-25 op static const struct got_error *
5662 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5663 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5664 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5665 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5666 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5667 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5668 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5669 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5670 afa376bf 2019-05-09 stsp struct got_repository *repo)
5671 c4296144 2019-05-09 stsp {
5672 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5673 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5674 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5675 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5676 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5677 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5678 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5679 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5680 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5681 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5682 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5683 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5684 f8399b8f 2022-07-22 stsp time_t timestamp;
5685 c4296144 2019-05-09 stsp
5686 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5687 c4296144 2019-05-09 stsp
5688 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5689 675c7539 2019-05-09 stsp
5690 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5691 588edf97 2019-05-10 stsp if (err)
5692 588edf97 2019-05-10 stsp goto done;
5693 de18fc63 2019-05-09 stsp
5694 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5695 588edf97 2019-05-10 stsp if (err)
5696 588edf97 2019-05-10 stsp goto done;
5697 588edf97 2019-05-10 stsp
5698 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5699 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5700 33ad4cbe 2019-05-12 jcs if (err)
5701 33ad4cbe 2019-05-12 jcs goto done;
5702 33ad4cbe 2019-05-12 jcs }
5703 c4296144 2019-05-09 stsp
5704 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5705 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5706 33ad4cbe 2019-05-12 jcs goto done;
5707 33ad4cbe 2019-05-12 jcs }
5708 33ad4cbe 2019-05-12 jcs
5709 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5710 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5711 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5712 cf066bf8 2019-05-09 stsp char *ondisk_path;
5713 cf066bf8 2019-05-09 stsp
5714 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5715 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5716 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5717 5f8a88c6 2019-08-03 stsp continue;
5718 5f8a88c6 2019-08-03 stsp
5719 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5720 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5721 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5722 cf066bf8 2019-05-09 stsp continue;
5723 cf066bf8 2019-05-09 stsp
5724 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5725 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5726 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5727 cf066bf8 2019-05-09 stsp goto done;
5728 cf066bf8 2019-05-09 stsp }
5729 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5730 2e1fa222 2020-07-23 stsp free(ondisk_path);
5731 2e1fa222 2020-07-23 stsp if (err)
5732 cf066bf8 2019-05-09 stsp goto done;
5733 cf066bf8 2019-05-09 stsp }
5734 036813ee 2019-05-09 stsp
5735 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5736 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5737 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5738 036813ee 2019-05-09 stsp if (err)
5739 036813ee 2019-05-09 stsp goto done;
5740 036813ee 2019-05-09 stsp
5741 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5742 de18fc63 2019-05-09 stsp if (err)
5743 de18fc63 2019-05-09 stsp goto done;
5744 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5745 f259c4c1 2021-09-24 stsp nparents++;
5746 f259c4c1 2021-09-24 stsp if (parent_id2) {
5747 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5748 f259c4c1 2021-09-24 stsp if (err)
5749 f259c4c1 2021-09-24 stsp goto done;
5750 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5751 f259c4c1 2021-09-24 stsp nparents++;
5752 f259c4c1 2021-09-24 stsp }
5753 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
5754 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5755 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
5756 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5757 33ad4cbe 2019-05-12 jcs free(logmsg);
5758 9d40349a 2019-05-09 stsp if (err)
5759 9d40349a 2019-05-09 stsp goto done;
5760 9d40349a 2019-05-09 stsp
5761 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5762 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5763 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5764 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5765 09f5bd90 2019-05-09 stsp goto done;
5766 09f5bd90 2019-05-09 stsp }
5767 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5768 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5769 09f5bd90 2019-05-09 stsp if (err)
5770 09f5bd90 2019-05-09 stsp goto done;
5771 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5772 ebf99748 2019-05-09 stsp if (err)
5773 ebf99748 2019-05-09 stsp goto done;
5774 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5775 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5776 09f5bd90 2019-05-09 stsp goto done;
5777 09f5bd90 2019-05-09 stsp }
5778 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5779 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5780 f2c16586 2019-05-09 stsp if (err)
5781 f2c16586 2019-05-09 stsp goto done;
5782 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5783 f2c16586 2019-05-09 stsp if (err)
5784 f2c16586 2019-05-09 stsp goto done;
5785 f2c16586 2019-05-09 stsp
5786 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5787 09f5bd90 2019-05-09 stsp if (err)
5788 09f5bd90 2019-05-09 stsp goto done;
5789 09f5bd90 2019-05-09 stsp
5790 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5791 ebf99748 2019-05-09 stsp if (err)
5792 ebf99748 2019-05-09 stsp goto done;
5793 c4296144 2019-05-09 stsp done:
5794 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5795 588edf97 2019-05-10 stsp if (head_tree)
5796 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5797 588edf97 2019-05-10 stsp if (head_commit)
5798 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5799 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5800 2f17228e 2019-05-12 stsp if (head_ref2) {
5801 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5802 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5803 2f17228e 2019-05-12 stsp err = unlockerr;
5804 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5805 39cd0ff6 2019-07-12 stsp }
5806 39cd0ff6 2019-07-12 stsp return err;
5807 39cd0ff6 2019-07-12 stsp }
5808 5c1e53bc 2019-07-28 stsp
5809 5c1e53bc 2019-07-28 stsp static const struct got_error *
5810 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5811 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5812 5c1e53bc 2019-07-28 stsp {
5813 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5814 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5815 39cd0ff6 2019-07-12 stsp
5816 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5817 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5818 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5819 5c1e53bc 2019-07-28 stsp
5820 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5821 5c1e53bc 2019-07-28 stsp ct_path++;
5822 5c1e53bc 2019-07-28 stsp
5823 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5824 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5825 5c1e53bc 2019-07-28 stsp break;
5826 5c1e53bc 2019-07-28 stsp }
5827 5c1e53bc 2019-07-28 stsp
5828 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5829 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5830 5c1e53bc 2019-07-28 stsp
5831 5c1e53bc 2019-07-28 stsp return NULL;
5832 5c1e53bc 2019-07-28 stsp }
5833 5c1e53bc 2019-07-28 stsp
5834 f0b75401 2019-08-03 stsp static const struct got_error *
5835 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5836 f0b75401 2019-08-03 stsp {
5837 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5838 f0b75401 2019-08-03 stsp
5839 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5840 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5841 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5842 f0b75401 2019-08-03 stsp }
5843 f0b75401 2019-08-03 stsp
5844 f0b75401 2019-08-03 stsp return NULL;
5845 f0b75401 2019-08-03 stsp }
5846 f0b75401 2019-08-03 stsp
5847 5f8a88c6 2019-08-03 stsp static const struct got_error *
5848 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5849 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5850 5f8a88c6 2019-08-03 stsp {
5851 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5852 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5853 5f8a88c6 2019-08-03 stsp
5854 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5855 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5856 5f8a88c6 2019-08-03 stsp continue;
5857 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5858 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5859 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5860 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5861 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5862 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5863 5f8a88c6 2019-08-03 stsp }
5864 5f8a88c6 2019-08-03 stsp
5865 5f8a88c6 2019-08-03 stsp return NULL;
5866 5f8a88c6 2019-08-03 stsp }
5867 5f8a88c6 2019-08-03 stsp
5868 39cd0ff6 2019-07-12 stsp const struct got_error *
5869 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5870 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5871 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5872 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5873 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5874 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5875 39cd0ff6 2019-07-12 stsp {
5876 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5877 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5878 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5879 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5880 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5881 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5882 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5883 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5884 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5885 39cd0ff6 2019-07-12 stsp
5886 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5887 39cd0ff6 2019-07-12 stsp
5888 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5889 39cd0ff6 2019-07-12 stsp
5890 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5891 39cd0ff6 2019-07-12 stsp if (err)
5892 39cd0ff6 2019-07-12 stsp goto done;
5893 39cd0ff6 2019-07-12 stsp
5894 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5895 39cd0ff6 2019-07-12 stsp if (err)
5896 39cd0ff6 2019-07-12 stsp goto done;
5897 39cd0ff6 2019-07-12 stsp
5898 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5899 39cd0ff6 2019-07-12 stsp if (err)
5900 39cd0ff6 2019-07-12 stsp goto done;
5901 39cd0ff6 2019-07-12 stsp
5902 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5903 39cd0ff6 2019-07-12 stsp if (err)
5904 39cd0ff6 2019-07-12 stsp goto done;
5905 39cd0ff6 2019-07-12 stsp
5906 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5907 5f8a88c6 2019-08-03 stsp &have_staged_files);
5908 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5909 5f8a88c6 2019-08-03 stsp goto done;
5910 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5911 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5912 5f8a88c6 2019-08-03 stsp if (err)
5913 5f8a88c6 2019-08-03 stsp goto done;
5914 5f8a88c6 2019-08-03 stsp }
5915 5f8a88c6 2019-08-03 stsp
5916 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5917 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5918 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5919 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5920 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5921 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5922 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5923 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5924 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5925 5c1e53bc 2019-07-28 stsp if (err)
5926 5c1e53bc 2019-07-28 stsp goto done;
5927 5c1e53bc 2019-07-28 stsp }
5928 39cd0ff6 2019-07-12 stsp
5929 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5930 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5931 39cd0ff6 2019-07-12 stsp goto done;
5932 5c1e53bc 2019-07-28 stsp }
5933 5c1e53bc 2019-07-28 stsp
5934 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5935 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5936 5c1e53bc 2019-07-28 stsp if (err)
5937 5c1e53bc 2019-07-28 stsp goto done;
5938 39cd0ff6 2019-07-12 stsp }
5939 f0b75401 2019-08-03 stsp
5940 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5941 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5942 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5943 f0b75401 2019-08-03 stsp
5944 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5945 f0b75401 2019-08-03 stsp ct_path++;
5946 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5947 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5948 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5949 b50cabdf 2019-07-12 stsp if (err)
5950 b50cabdf 2019-07-12 stsp goto done;
5951 f0b75401 2019-08-03 stsp
5952 b50cabdf 2019-07-12 stsp }
5953 b50cabdf 2019-07-12 stsp
5954 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5955 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5956 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5957 39cd0ff6 2019-07-12 stsp if (err)
5958 39cd0ff6 2019-07-12 stsp goto done;
5959 39cd0ff6 2019-07-12 stsp
5960 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5961 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5962 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5963 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5964 39cd0ff6 2019-07-12 stsp err = sync_err;
5965 39cd0ff6 2019-07-12 stsp done:
5966 39cd0ff6 2019-07-12 stsp if (fileindex)
5967 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5968 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5969 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5970 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5971 39cd0ff6 2019-07-12 stsp err = unlockerr;
5972 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5973 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5974 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5975 39cd0ff6 2019-07-12 stsp }
5976 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5977 c4296144 2019-05-09 stsp return err;
5978 8656d6c4 2019-05-20 stsp }
5979 8656d6c4 2019-05-20 stsp
5980 8656d6c4 2019-05-20 stsp const char *
5981 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5982 8656d6c4 2019-05-20 stsp {
5983 8656d6c4 2019-05-20 stsp return ct->path;
5984 8656d6c4 2019-05-20 stsp }
5985 8656d6c4 2019-05-20 stsp
5986 8656d6c4 2019-05-20 stsp unsigned int
5987 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5988 8656d6c4 2019-05-20 stsp {
5989 8656d6c4 2019-05-20 stsp return ct->status;
5990 818c7501 2019-07-11 stsp }
5991 818c7501 2019-07-11 stsp
5992 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5993 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5994 818c7501 2019-07-11 stsp struct got_repository *repo;
5995 818c7501 2019-07-11 stsp };
5996 818c7501 2019-07-11 stsp
5997 818c7501 2019-07-11 stsp static const struct got_error *
5998 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5999 818c7501 2019-07-11 stsp {
6000 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6001 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6002 818c7501 2019-07-11 stsp unsigned char status;
6003 818c7501 2019-07-11 stsp struct stat sb;
6004 818c7501 2019-07-11 stsp char *ondisk_path;
6005 818c7501 2019-07-11 stsp
6006 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6007 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6008 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6009 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6010 818c7501 2019-07-11 stsp
6011 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6012 818c7501 2019-07-11 stsp == -1)
6013 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6014 818c7501 2019-07-11 stsp
6015 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6016 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6017 818c7501 2019-07-11 stsp free(ondisk_path);
6018 818c7501 2019-07-11 stsp if (err)
6019 818c7501 2019-07-11 stsp return err;
6020 818c7501 2019-07-11 stsp
6021 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6022 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6023 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6024 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6025 818c7501 2019-07-11 stsp
6026 818c7501 2019-07-11 stsp return NULL;
6027 818c7501 2019-07-11 stsp }
6028 818c7501 2019-07-11 stsp
6029 818c7501 2019-07-11 stsp const struct got_error *
6030 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6031 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6032 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6033 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6034 818c7501 2019-07-11 stsp {
6035 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6036 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6037 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6038 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6039 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6040 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6041 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6042 818c7501 2019-07-11 stsp
6043 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6044 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6045 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6046 818c7501 2019-07-11 stsp
6047 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6048 818c7501 2019-07-11 stsp if (err)
6049 818c7501 2019-07-11 stsp return err;
6050 818c7501 2019-07-11 stsp
6051 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6052 818c7501 2019-07-11 stsp if (err)
6053 818c7501 2019-07-11 stsp goto done;
6054 818c7501 2019-07-11 stsp
6055 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6056 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6057 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6058 818c7501 2019-07-11 stsp &ok_arg);
6059 818c7501 2019-07-11 stsp if (err)
6060 818c7501 2019-07-11 stsp goto done;
6061 818c7501 2019-07-11 stsp
6062 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6063 818c7501 2019-07-11 stsp if (err)
6064 818c7501 2019-07-11 stsp goto done;
6065 818c7501 2019-07-11 stsp
6066 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6067 818c7501 2019-07-11 stsp if (err)
6068 818c7501 2019-07-11 stsp goto done;
6069 818c7501 2019-07-11 stsp
6070 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6071 818c7501 2019-07-11 stsp if (err)
6072 818c7501 2019-07-11 stsp goto done;
6073 818c7501 2019-07-11 stsp
6074 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6075 818c7501 2019-07-11 stsp 0);
6076 e51d7b55 2020-01-04 stsp if (err)
6077 e51d7b55 2020-01-04 stsp goto done;
6078 e51d7b55 2020-01-04 stsp
6079 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6080 818c7501 2019-07-11 stsp if (err)
6081 818c7501 2019-07-11 stsp goto done;
6082 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6083 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6084 e51d7b55 2020-01-04 stsp goto done;
6085 e51d7b55 2020-01-04 stsp }
6086 818c7501 2019-07-11 stsp
6087 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6088 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6089 818c7501 2019-07-11 stsp if (err)
6090 818c7501 2019-07-11 stsp goto done;
6091 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6092 818c7501 2019-07-11 stsp if (err)
6093 818c7501 2019-07-11 stsp goto done;
6094 818c7501 2019-07-11 stsp
6095 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6096 818c7501 2019-07-11 stsp
6097 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6098 818c7501 2019-07-11 stsp if (err)
6099 818c7501 2019-07-11 stsp goto done;
6100 818c7501 2019-07-11 stsp
6101 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6102 818c7501 2019-07-11 stsp if (err)
6103 818c7501 2019-07-11 stsp goto done;
6104 818c7501 2019-07-11 stsp
6105 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6106 818c7501 2019-07-11 stsp worktree->base_commit_id);
6107 818c7501 2019-07-11 stsp if (err)
6108 818c7501 2019-07-11 stsp goto done;
6109 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6110 818c7501 2019-07-11 stsp if (err)
6111 818c7501 2019-07-11 stsp goto done;
6112 818c7501 2019-07-11 stsp
6113 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6114 818c7501 2019-07-11 stsp if (err)
6115 818c7501 2019-07-11 stsp goto done;
6116 818c7501 2019-07-11 stsp done:
6117 818c7501 2019-07-11 stsp free(fileindex_path);
6118 818c7501 2019-07-11 stsp free(tmp_branch_name);
6119 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6120 818c7501 2019-07-11 stsp free(branch_ref_name);
6121 818c7501 2019-07-11 stsp if (branch_ref)
6122 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6123 818c7501 2019-07-11 stsp if (wt_branch)
6124 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6125 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6126 818c7501 2019-07-11 stsp if (err) {
6127 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6128 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6129 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6130 818c7501 2019-07-11 stsp }
6131 818c7501 2019-07-11 stsp if (*tmp_branch) {
6132 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6133 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6134 818c7501 2019-07-11 stsp }
6135 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6136 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6137 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6138 3e3a69f1 2019-07-25 stsp }
6139 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6140 818c7501 2019-07-11 stsp }
6141 818c7501 2019-07-11 stsp return err;
6142 818c7501 2019-07-11 stsp }
6143 818c7501 2019-07-11 stsp
6144 818c7501 2019-07-11 stsp const struct got_error *
6145 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6146 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6147 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6148 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6149 818c7501 2019-07-11 stsp {
6150 818c7501 2019-07-11 stsp const struct got_error *err;
6151 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6152 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6153 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6154 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6155 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6156 818c7501 2019-07-11 stsp
6157 818c7501 2019-07-11 stsp *commit_id = NULL;
6158 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6159 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6160 3e3a69f1 2019-07-25 stsp *branch = NULL;
6161 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6162 3e3a69f1 2019-07-25 stsp
6163 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6164 3e3a69f1 2019-07-25 stsp if (err)
6165 3e3a69f1 2019-07-25 stsp return err;
6166 818c7501 2019-07-11 stsp
6167 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6168 3e3a69f1 2019-07-25 stsp if (err)
6169 f032f1f7 2019-08-04 stsp goto done;
6170 f032f1f7 2019-08-04 stsp
6171 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6172 f032f1f7 2019-08-04 stsp &have_staged_files);
6173 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6174 f032f1f7 2019-08-04 stsp goto done;
6175 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6176 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6177 3e3a69f1 2019-07-25 stsp goto done;
6178 f032f1f7 2019-08-04 stsp }
6179 3e3a69f1 2019-07-25 stsp
6180 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6181 818c7501 2019-07-11 stsp if (err)
6182 f032f1f7 2019-08-04 stsp goto done;
6183 818c7501 2019-07-11 stsp
6184 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6185 818c7501 2019-07-11 stsp if (err)
6186 818c7501 2019-07-11 stsp goto done;
6187 818c7501 2019-07-11 stsp
6188 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6189 818c7501 2019-07-11 stsp if (err)
6190 818c7501 2019-07-11 stsp goto done;
6191 818c7501 2019-07-11 stsp
6192 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6193 818c7501 2019-07-11 stsp if (err)
6194 818c7501 2019-07-11 stsp goto done;
6195 818c7501 2019-07-11 stsp
6196 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6197 818c7501 2019-07-11 stsp if (err)
6198 818c7501 2019-07-11 stsp goto done;
6199 818c7501 2019-07-11 stsp
6200 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6201 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6202 818c7501 2019-07-11 stsp if (err)
6203 818c7501 2019-07-11 stsp goto done;
6204 818c7501 2019-07-11 stsp
6205 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6206 818c7501 2019-07-11 stsp if (err)
6207 818c7501 2019-07-11 stsp goto done;
6208 818c7501 2019-07-11 stsp
6209 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6210 818c7501 2019-07-11 stsp if (err)
6211 818c7501 2019-07-11 stsp goto done;
6212 818c7501 2019-07-11 stsp
6213 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6214 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6215 818c7501 2019-07-11 stsp if (err)
6216 818c7501 2019-07-11 stsp goto done;
6217 818c7501 2019-07-11 stsp
6218 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6219 818c7501 2019-07-11 stsp if (err)
6220 818c7501 2019-07-11 stsp goto done;
6221 818c7501 2019-07-11 stsp done:
6222 818c7501 2019-07-11 stsp free(commit_ref_name);
6223 818c7501 2019-07-11 stsp free(branch_ref_name);
6224 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6225 818c7501 2019-07-11 stsp if (commit_ref)
6226 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6227 818c7501 2019-07-11 stsp if (branch_ref)
6228 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6229 818c7501 2019-07-11 stsp if (err) {
6230 818c7501 2019-07-11 stsp free(*commit_id);
6231 818c7501 2019-07-11 stsp *commit_id = NULL;
6232 818c7501 2019-07-11 stsp if (*tmp_branch) {
6233 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6234 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6235 818c7501 2019-07-11 stsp }
6236 818c7501 2019-07-11 stsp if (*new_base_branch) {
6237 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6238 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6239 818c7501 2019-07-11 stsp }
6240 818c7501 2019-07-11 stsp if (*branch) {
6241 818c7501 2019-07-11 stsp got_ref_close(*branch);
6242 818c7501 2019-07-11 stsp *branch = NULL;
6243 818c7501 2019-07-11 stsp }
6244 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6245 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6246 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6247 3e3a69f1 2019-07-25 stsp }
6248 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6249 818c7501 2019-07-11 stsp }
6250 818c7501 2019-07-11 stsp return err;
6251 c4296144 2019-05-09 stsp }
6252 818c7501 2019-07-11 stsp
6253 818c7501 2019-07-11 stsp const struct got_error *
6254 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6255 818c7501 2019-07-11 stsp {
6256 818c7501 2019-07-11 stsp const struct got_error *err;
6257 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6258 818c7501 2019-07-11 stsp
6259 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6260 818c7501 2019-07-11 stsp if (err)
6261 818c7501 2019-07-11 stsp return err;
6262 818c7501 2019-07-11 stsp
6263 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6264 818c7501 2019-07-11 stsp free(tmp_branch_name);
6265 818c7501 2019-07-11 stsp return NULL;
6266 818c7501 2019-07-11 stsp }
6267 818c7501 2019-07-11 stsp
6268 818c7501 2019-07-11 stsp static const struct got_error *
6269 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6270 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6271 818c7501 2019-07-11 stsp {
6272 0ebf8283 2019-07-24 stsp *logmsg = arg;
6273 818c7501 2019-07-11 stsp return NULL;
6274 818c7501 2019-07-11 stsp }
6275 818c7501 2019-07-11 stsp
6276 818c7501 2019-07-11 stsp static const struct got_error *
6277 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6278 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6279 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6280 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6281 818c7501 2019-07-11 stsp {
6282 818c7501 2019-07-11 stsp return NULL;
6283 01757395 2019-07-12 stsp }
6284 01757395 2019-07-12 stsp
6285 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6286 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6287 01757395 2019-07-12 stsp void *progress_arg;
6288 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6289 01757395 2019-07-12 stsp };
6290 01757395 2019-07-12 stsp
6291 01757395 2019-07-12 stsp static const struct got_error *
6292 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6293 01757395 2019-07-12 stsp {
6294 01757395 2019-07-12 stsp const struct got_error *err;
6295 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6296 01757395 2019-07-12 stsp char *p;
6297 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6298 01757395 2019-07-12 stsp
6299 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6300 01757395 2019-07-12 stsp if (err)
6301 01757395 2019-07-12 stsp return err;
6302 01757395 2019-07-12 stsp
6303 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6304 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6305 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6306 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6307 01757395 2019-07-12 stsp return NULL;
6308 01757395 2019-07-12 stsp
6309 01757395 2019-07-12 stsp p = strdup(path);
6310 01757395 2019-07-12 stsp if (p == NULL)
6311 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6312 01757395 2019-07-12 stsp
6313 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6314 01757395 2019-07-12 stsp if (err || new == NULL)
6315 01757395 2019-07-12 stsp free(p);
6316 01757395 2019-07-12 stsp return err;
6317 01757395 2019-07-12 stsp }
6318 01757395 2019-07-12 stsp
6319 01757395 2019-07-12 stsp void
6320 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6321 01757395 2019-07-12 stsp {
6322 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6323 01757395 2019-07-12 stsp
6324 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6325 01757395 2019-07-12 stsp free((char *)pe->path);
6326 01757395 2019-07-12 stsp
6327 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6328 818c7501 2019-07-11 stsp }
6329 818c7501 2019-07-11 stsp
6330 0ebf8283 2019-07-24 stsp static const struct got_error *
6331 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6332 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6333 818c7501 2019-07-11 stsp {
6334 818c7501 2019-07-11 stsp const struct got_error *err;
6335 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6336 818c7501 2019-07-11 stsp
6337 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6338 818c7501 2019-07-11 stsp if (err) {
6339 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6340 818c7501 2019-07-11 stsp goto done;
6341 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6342 818c7501 2019-07-11 stsp if (err)
6343 818c7501 2019-07-11 stsp goto done;
6344 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6345 818c7501 2019-07-11 stsp if (err)
6346 818c7501 2019-07-11 stsp goto done;
6347 de05890f 2020-03-05 stsp } else if (is_rebase) {
6348 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6349 818c7501 2019-07-11 stsp int cmp;
6350 818c7501 2019-07-11 stsp
6351 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6352 818c7501 2019-07-11 stsp if (err)
6353 818c7501 2019-07-11 stsp goto done;
6354 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6355 818c7501 2019-07-11 stsp free(stored_id);
6356 818c7501 2019-07-11 stsp if (cmp != 0) {
6357 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6358 818c7501 2019-07-11 stsp goto done;
6359 818c7501 2019-07-11 stsp }
6360 818c7501 2019-07-11 stsp }
6361 0ebf8283 2019-07-24 stsp done:
6362 0ebf8283 2019-07-24 stsp if (commit_ref)
6363 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6364 0ebf8283 2019-07-24 stsp return err;
6365 0ebf8283 2019-07-24 stsp }
6366 0ebf8283 2019-07-24 stsp
6367 0ebf8283 2019-07-24 stsp static const struct got_error *
6368 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6369 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6370 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6371 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6372 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6373 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6374 0ebf8283 2019-07-24 stsp {
6375 0ebf8283 2019-07-24 stsp const struct got_error *err;
6376 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6377 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6378 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6379 818c7501 2019-07-11 stsp
6380 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6381 0ebf8283 2019-07-24 stsp
6382 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6383 0ebf8283 2019-07-24 stsp if (err)
6384 0ebf8283 2019-07-24 stsp return err;
6385 0ebf8283 2019-07-24 stsp
6386 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6387 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6388 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6389 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6390 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6391 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6392 818c7501 2019-07-11 stsp if (commit_ref)
6393 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6394 818c7501 2019-07-11 stsp return err;
6395 818c7501 2019-07-11 stsp }
6396 818c7501 2019-07-11 stsp
6397 818c7501 2019-07-11 stsp const struct got_error *
6398 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6399 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6400 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6401 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6402 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6403 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6404 818c7501 2019-07-11 stsp {
6405 0ebf8283 2019-07-24 stsp const struct got_error *err;
6406 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6407 0ebf8283 2019-07-24 stsp
6408 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6409 0ebf8283 2019-07-24 stsp if (err)
6410 0ebf8283 2019-07-24 stsp return err;
6411 0ebf8283 2019-07-24 stsp
6412 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6413 0ebf8283 2019-07-24 stsp if (err)
6414 0ebf8283 2019-07-24 stsp goto done;
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6417 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6418 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6419 0ebf8283 2019-07-24 stsp done:
6420 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6421 0ebf8283 2019-07-24 stsp return err;
6422 0ebf8283 2019-07-24 stsp }
6423 0ebf8283 2019-07-24 stsp
6424 0ebf8283 2019-07-24 stsp const struct got_error *
6425 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6426 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6427 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6428 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6429 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6430 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6431 0ebf8283 2019-07-24 stsp {
6432 0ebf8283 2019-07-24 stsp const struct got_error *err;
6433 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6434 0ebf8283 2019-07-24 stsp
6435 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6436 0ebf8283 2019-07-24 stsp if (err)
6437 0ebf8283 2019-07-24 stsp return err;
6438 0ebf8283 2019-07-24 stsp
6439 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6440 0ebf8283 2019-07-24 stsp if (err)
6441 0ebf8283 2019-07-24 stsp goto done;
6442 0ebf8283 2019-07-24 stsp
6443 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6444 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6445 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6446 0ebf8283 2019-07-24 stsp done:
6447 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6448 0ebf8283 2019-07-24 stsp return err;
6449 0ebf8283 2019-07-24 stsp }
6450 0ebf8283 2019-07-24 stsp
6451 0ebf8283 2019-07-24 stsp static const struct got_error *
6452 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6453 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6454 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6455 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6456 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6457 598eac43 2022-07-22 stsp struct got_repository *repo)
6458 0ebf8283 2019-07-24 stsp {
6459 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6460 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6461 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6462 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6463 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6464 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6465 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6466 818c7501 2019-07-11 stsp
6467 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6468 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6469 a0e95631 2019-07-12 stsp
6470 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6471 818c7501 2019-07-11 stsp
6472 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6473 a0e95631 2019-07-12 stsp if (err)
6474 3e3a69f1 2019-07-25 stsp return err;
6475 a0e95631 2019-07-12 stsp
6476 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6477 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6478 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6479 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6480 01757395 2019-07-12 stsp /*
6481 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6482 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6483 6c13b005 2021-09-02 stsp *
6484 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6485 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6486 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6487 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6488 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6489 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6490 01757395 2019-07-12 stsp */
6491 01757395 2019-07-12 stsp if (merged_paths) {
6492 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6493 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6494 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6495 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6496 f2a9dc41 2019-12-13 tracey 0);
6497 01757395 2019-07-12 stsp if (err)
6498 01757395 2019-07-12 stsp goto done;
6499 01757395 2019-07-12 stsp }
6500 01757395 2019-07-12 stsp } else {
6501 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6502 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6503 01757395 2019-07-12 stsp if (err)
6504 01757395 2019-07-12 stsp goto done;
6505 01757395 2019-07-12 stsp }
6506 a0e95631 2019-07-12 stsp
6507 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6508 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6509 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6510 a0e95631 2019-07-12 stsp if (err)
6511 a0e95631 2019-07-12 stsp goto done;
6512 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6513 a0e95631 2019-07-12 stsp goto done;
6514 ff0d2220 2019-07-11 stsp }
6515 818c7501 2019-07-11 stsp
6516 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6517 edd02c5e 2019-07-11 stsp if (err)
6518 edd02c5e 2019-07-11 stsp goto done;
6519 edd02c5e 2019-07-11 stsp
6520 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6521 818c7501 2019-07-11 stsp if (err)
6522 818c7501 2019-07-11 stsp goto done;
6523 818c7501 2019-07-11 stsp
6524 5943eee2 2019-08-13 stsp if (new_logmsg) {
6525 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6526 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6527 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6528 5943eee2 2019-08-13 stsp goto done;
6529 5943eee2 2019-08-13 stsp }
6530 5943eee2 2019-08-13 stsp } else {
6531 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6532 5943eee2 2019-08-13 stsp if (err)
6533 5943eee2 2019-08-13 stsp goto done;
6534 5943eee2 2019-08-13 stsp }
6535 0ebf8283 2019-07-24 stsp
6536 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6537 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6538 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6539 50e7a649 2022-07-22 stsp committer ? committer :
6540 50e7a649 2022-07-22 stsp got_object_commit_get_committer(orig_commit),
6541 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6542 a0e95631 2019-07-12 stsp if (err)
6543 a0e95631 2019-07-12 stsp goto done;
6544 a0e95631 2019-07-12 stsp
6545 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6546 a0e95631 2019-07-12 stsp if (err)
6547 a0e95631 2019-07-12 stsp goto done;
6548 a0e95631 2019-07-12 stsp
6549 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6550 a0e95631 2019-07-12 stsp if (err)
6551 a0e95631 2019-07-12 stsp goto done;
6552 a0e95631 2019-07-12 stsp
6553 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6554 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6555 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6556 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6557 a0e95631 2019-07-12 stsp err = sync_err;
6558 818c7501 2019-07-11 stsp done:
6559 a0e95631 2019-07-12 stsp free(fileindex_path);
6560 a0e95631 2019-07-12 stsp free(head_commit_id);
6561 a0e95631 2019-07-12 stsp if (head_ref)
6562 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6563 818c7501 2019-07-11 stsp if (err) {
6564 818c7501 2019-07-11 stsp free(*new_commit_id);
6565 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6566 818c7501 2019-07-11 stsp }
6567 818c7501 2019-07-11 stsp return err;
6568 818c7501 2019-07-11 stsp }
6569 818c7501 2019-07-11 stsp
6570 818c7501 2019-07-11 stsp const struct got_error *
6571 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6572 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6573 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6574 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6575 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6576 0ebf8283 2019-07-24 stsp {
6577 0ebf8283 2019-07-24 stsp const struct got_error *err;
6578 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6579 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6580 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6583 0ebf8283 2019-07-24 stsp if (err)
6584 0ebf8283 2019-07-24 stsp return err;
6585 0ebf8283 2019-07-24 stsp
6586 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6587 0ebf8283 2019-07-24 stsp if (err)
6588 0ebf8283 2019-07-24 stsp goto done;
6589 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6590 0ebf8283 2019-07-24 stsp if (err)
6591 0ebf8283 2019-07-24 stsp goto done;
6592 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6593 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6594 0ebf8283 2019-07-24 stsp goto done;
6595 0ebf8283 2019-07-24 stsp }
6596 0ebf8283 2019-07-24 stsp
6597 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6598 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6599 598eac43 2022-07-22 stsp NULL, repo);
6600 0ebf8283 2019-07-24 stsp done:
6601 0ebf8283 2019-07-24 stsp if (commit_ref)
6602 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6603 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6604 0ebf8283 2019-07-24 stsp free(commit_id);
6605 0ebf8283 2019-07-24 stsp return err;
6606 0ebf8283 2019-07-24 stsp }
6607 0ebf8283 2019-07-24 stsp
6608 0ebf8283 2019-07-24 stsp const struct got_error *
6609 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6610 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6611 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6612 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6613 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6614 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6615 0ebf8283 2019-07-24 stsp {
6616 0ebf8283 2019-07-24 stsp const struct got_error *err;
6617 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6618 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6619 0ebf8283 2019-07-24 stsp
6620 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6621 0ebf8283 2019-07-24 stsp if (err)
6622 0ebf8283 2019-07-24 stsp return err;
6623 0ebf8283 2019-07-24 stsp
6624 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6625 0ebf8283 2019-07-24 stsp if (err)
6626 0ebf8283 2019-07-24 stsp goto done;
6627 0ebf8283 2019-07-24 stsp
6628 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6629 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6630 598eac43 2022-07-22 stsp new_logmsg, repo);
6631 0ebf8283 2019-07-24 stsp done:
6632 0ebf8283 2019-07-24 stsp if (commit_ref)
6633 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6634 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6635 0ebf8283 2019-07-24 stsp return err;
6636 0ebf8283 2019-07-24 stsp }
6637 0ebf8283 2019-07-24 stsp
6638 0ebf8283 2019-07-24 stsp const struct got_error *
6639 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6640 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6641 818c7501 2019-07-11 stsp {
6642 3e3a69f1 2019-07-25 stsp if (fileindex)
6643 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6644 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6645 69844fba 2019-07-11 stsp }
6646 69844fba 2019-07-11 stsp
6647 69844fba 2019-07-11 stsp static const struct got_error *
6648 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6649 69844fba 2019-07-11 stsp {
6650 69844fba 2019-07-11 stsp const struct got_error *err;
6651 69844fba 2019-07-11 stsp struct got_reference *ref;
6652 69844fba 2019-07-11 stsp
6653 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6654 69844fba 2019-07-11 stsp if (err) {
6655 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6656 69844fba 2019-07-11 stsp return NULL;
6657 69844fba 2019-07-11 stsp return err;
6658 69844fba 2019-07-11 stsp }
6659 69844fba 2019-07-11 stsp
6660 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6661 69844fba 2019-07-11 stsp got_ref_close(ref);
6662 69844fba 2019-07-11 stsp return err;
6663 818c7501 2019-07-11 stsp }
6664 818c7501 2019-07-11 stsp
6665 69844fba 2019-07-11 stsp static const struct got_error *
6666 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6667 69844fba 2019-07-11 stsp {
6668 69844fba 2019-07-11 stsp const struct got_error *err;
6669 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6670 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6671 69844fba 2019-07-11 stsp
6672 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6673 69844fba 2019-07-11 stsp if (err)
6674 69844fba 2019-07-11 stsp goto done;
6675 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6676 69844fba 2019-07-11 stsp if (err)
6677 69844fba 2019-07-11 stsp goto done;
6678 69844fba 2019-07-11 stsp
6679 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6680 69844fba 2019-07-11 stsp if (err)
6681 69844fba 2019-07-11 stsp goto done;
6682 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6683 69844fba 2019-07-11 stsp if (err)
6684 69844fba 2019-07-11 stsp goto done;
6685 69844fba 2019-07-11 stsp
6686 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6687 69844fba 2019-07-11 stsp if (err)
6688 69844fba 2019-07-11 stsp goto done;
6689 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6690 69844fba 2019-07-11 stsp if (err)
6691 69844fba 2019-07-11 stsp goto done;
6692 69844fba 2019-07-11 stsp
6693 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6694 69844fba 2019-07-11 stsp if (err)
6695 69844fba 2019-07-11 stsp goto done;
6696 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6697 69844fba 2019-07-11 stsp if (err)
6698 69844fba 2019-07-11 stsp goto done;
6699 69844fba 2019-07-11 stsp
6700 69844fba 2019-07-11 stsp done:
6701 69844fba 2019-07-11 stsp free(tmp_branch_name);
6702 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6703 69844fba 2019-07-11 stsp free(branch_ref_name);
6704 69844fba 2019-07-11 stsp free(commit_ref_name);
6705 e600f124 2021-03-21 stsp return err;
6706 e600f124 2021-03-21 stsp }
6707 e600f124 2021-03-21 stsp
6708 336075a4 2022-06-25 op static const struct got_error *
6709 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6710 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6711 e600f124 2021-03-21 stsp {
6712 e600f124 2021-03-21 stsp const struct got_error *err;
6713 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6714 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6715 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6716 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6717 e600f124 2021-03-21 stsp char *refname = NULL;
6718 e600f124 2021-03-21 stsp
6719 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6720 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6721 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6722 e600f124 2021-03-21 stsp branch_name += 11;
6723 e600f124 2021-03-21 stsp
6724 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6725 e600f124 2021-03-21 stsp if (err)
6726 e600f124 2021-03-21 stsp return err;
6727 e600f124 2021-03-21 stsp
6728 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6729 e600f124 2021-03-21 stsp new_id_str) == -1) {
6730 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6731 e600f124 2021-03-21 stsp goto done;
6732 e600f124 2021-03-21 stsp }
6733 e600f124 2021-03-21 stsp
6734 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6735 e600f124 2021-03-21 stsp if (err)
6736 e600f124 2021-03-21 stsp goto done;
6737 e600f124 2021-03-21 stsp
6738 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6739 e600f124 2021-03-21 stsp if (err)
6740 e600f124 2021-03-21 stsp goto done;
6741 e600f124 2021-03-21 stsp
6742 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6743 e600f124 2021-03-21 stsp done:
6744 e600f124 2021-03-21 stsp free(new_id_str);
6745 e600f124 2021-03-21 stsp free(refname);
6746 e600f124 2021-03-21 stsp free(old_commit_id);
6747 e600f124 2021-03-21 stsp if (ref)
6748 e600f124 2021-03-21 stsp got_ref_close(ref);
6749 69844fba 2019-07-11 stsp return err;
6750 69844fba 2019-07-11 stsp }
6751 69844fba 2019-07-11 stsp
6752 818c7501 2019-07-11 stsp const struct got_error *
6753 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6754 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6755 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6756 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6757 818c7501 2019-07-11 stsp {
6758 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6759 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6760 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6761 818c7501 2019-07-11 stsp
6762 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6763 818c7501 2019-07-11 stsp if (err)
6764 818c7501 2019-07-11 stsp return err;
6765 e600f124 2021-03-21 stsp
6766 e600f124 2021-03-21 stsp if (create_backup) {
6767 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6768 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6769 e600f124 2021-03-21 stsp if (err)
6770 e600f124 2021-03-21 stsp goto done;
6771 e600f124 2021-03-21 stsp }
6772 818c7501 2019-07-11 stsp
6773 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6774 818c7501 2019-07-11 stsp if (err)
6775 818c7501 2019-07-11 stsp goto done;
6776 818c7501 2019-07-11 stsp
6777 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6778 818c7501 2019-07-11 stsp if (err)
6779 818c7501 2019-07-11 stsp goto done;
6780 818c7501 2019-07-11 stsp
6781 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6782 818c7501 2019-07-11 stsp if (err)
6783 818c7501 2019-07-11 stsp goto done;
6784 818c7501 2019-07-11 stsp
6785 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6786 a615e0e7 2020-12-16 stsp if (err)
6787 a615e0e7 2020-12-16 stsp goto done;
6788 a615e0e7 2020-12-16 stsp
6789 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6790 a615e0e7 2020-12-16 stsp if (err)
6791 a615e0e7 2020-12-16 stsp goto done;
6792 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6793 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6794 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6795 a615e0e7 2020-12-16 stsp err = sync_err;
6796 818c7501 2019-07-11 stsp done:
6797 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6798 a615e0e7 2020-12-16 stsp free(fileindex_path);
6799 818c7501 2019-07-11 stsp free(new_head_commit_id);
6800 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6801 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6802 818c7501 2019-07-11 stsp err = unlockerr;
6803 818c7501 2019-07-11 stsp return err;
6804 818c7501 2019-07-11 stsp }
6805 818c7501 2019-07-11 stsp
6806 818c7501 2019-07-11 stsp const struct got_error *
6807 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6808 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6809 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6810 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6811 818c7501 2019-07-11 stsp {
6812 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6813 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6814 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6815 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6816 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6817 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6818 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6819 818c7501 2019-07-11 stsp
6820 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6821 818c7501 2019-07-11 stsp if (err)
6822 818c7501 2019-07-11 stsp return err;
6823 818c7501 2019-07-11 stsp
6824 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
6825 a44927cc 2022-04-07 stsp worktree->base_commit_id);
6826 a44927cc 2022-04-07 stsp if (err)
6827 a44927cc 2022-04-07 stsp goto done;
6828 a44927cc 2022-04-07 stsp
6829 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6830 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6831 818c7501 2019-07-11 stsp if (err)
6832 818c7501 2019-07-11 stsp goto done;
6833 818c7501 2019-07-11 stsp
6834 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6835 818c7501 2019-07-11 stsp if (err)
6836 818c7501 2019-07-11 stsp goto done;
6837 818c7501 2019-07-11 stsp
6838 818c7501 2019-07-11 stsp /*
6839 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6840 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6841 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6842 818c7501 2019-07-11 stsp */
6843 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6844 818c7501 2019-07-11 stsp if (err)
6845 818c7501 2019-07-11 stsp goto done;
6846 818c7501 2019-07-11 stsp
6847 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6848 818c7501 2019-07-11 stsp if (err)
6849 818c7501 2019-07-11 stsp goto done;
6850 818c7501 2019-07-11 stsp
6851 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
6852 a44927cc 2022-04-07 stsp worktree->path_prefix);
6853 ca355955 2019-07-12 stsp if (err)
6854 ca355955 2019-07-12 stsp goto done;
6855 ca355955 2019-07-12 stsp
6856 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6857 ca355955 2019-07-12 stsp if (err)
6858 ca355955 2019-07-12 stsp goto done;
6859 ca355955 2019-07-12 stsp
6860 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6861 818c7501 2019-07-11 stsp if (err)
6862 818c7501 2019-07-11 stsp goto done;
6863 818c7501 2019-07-11 stsp
6864 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6865 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6866 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6867 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6868 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6869 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6870 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6871 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6872 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6873 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6874 55bd499d 2019-07-12 stsp if (err)
6875 1f1abb7e 2019-08-08 stsp goto sync;
6876 55bd499d 2019-07-12 stsp
6877 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6878 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6879 ca355955 2019-07-12 stsp sync:
6880 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6881 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6882 ca355955 2019-07-12 stsp err = sync_err;
6883 818c7501 2019-07-11 stsp done:
6884 818c7501 2019-07-11 stsp got_ref_close(resolved);
6885 a3a2faf2 2019-07-12 stsp free(tree_id);
6886 818c7501 2019-07-11 stsp free(commit_id);
6887 a44927cc 2022-04-07 stsp if (commit)
6888 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6889 0ebf8283 2019-07-24 stsp if (fileindex)
6890 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6891 0ebf8283 2019-07-24 stsp free(fileindex_path);
6892 0ebf8283 2019-07-24 stsp
6893 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6894 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6895 0ebf8283 2019-07-24 stsp err = unlockerr;
6896 0ebf8283 2019-07-24 stsp return err;
6897 0ebf8283 2019-07-24 stsp }
6898 0ebf8283 2019-07-24 stsp
6899 0ebf8283 2019-07-24 stsp const struct got_error *
6900 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6901 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6902 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6903 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6904 0ebf8283 2019-07-24 stsp {
6905 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6906 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6907 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6908 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6909 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6910 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6911 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6912 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6915 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6916 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6917 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6920 0ebf8283 2019-07-24 stsp if (err)
6921 0ebf8283 2019-07-24 stsp return err;
6922 0ebf8283 2019-07-24 stsp
6923 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6924 0ebf8283 2019-07-24 stsp if (err)
6925 0ebf8283 2019-07-24 stsp goto done;
6926 0ebf8283 2019-07-24 stsp
6927 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6928 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6929 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6930 0ebf8283 2019-07-24 stsp &ok_arg);
6931 0ebf8283 2019-07-24 stsp if (err)
6932 0ebf8283 2019-07-24 stsp goto done;
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6935 0ebf8283 2019-07-24 stsp if (err)
6936 0ebf8283 2019-07-24 stsp goto done;
6937 0ebf8283 2019-07-24 stsp
6938 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6939 0ebf8283 2019-07-24 stsp if (err)
6940 0ebf8283 2019-07-24 stsp goto done;
6941 0ebf8283 2019-07-24 stsp
6942 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6943 0ebf8283 2019-07-24 stsp worktree);
6944 0ebf8283 2019-07-24 stsp if (err)
6945 0ebf8283 2019-07-24 stsp goto done;
6946 0ebf8283 2019-07-24 stsp
6947 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6948 0ebf8283 2019-07-24 stsp 0);
6949 0ebf8283 2019-07-24 stsp if (err)
6950 0ebf8283 2019-07-24 stsp goto done;
6951 0ebf8283 2019-07-24 stsp
6952 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6953 0ebf8283 2019-07-24 stsp if (err)
6954 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp
6956 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6957 0ebf8283 2019-07-24 stsp if (err)
6958 0ebf8283 2019-07-24 stsp goto done;
6959 0ebf8283 2019-07-24 stsp
6960 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6961 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6962 0ebf8283 2019-07-24 stsp if (err)
6963 0ebf8283 2019-07-24 stsp goto done;
6964 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6965 0ebf8283 2019-07-24 stsp if (err)
6966 0ebf8283 2019-07-24 stsp goto done;
6967 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6968 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6969 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6970 0ebf8283 2019-07-24 stsp goto done;
6971 0ebf8283 2019-07-24 stsp }
6972 0ebf8283 2019-07-24 stsp
6973 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6974 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6975 0ebf8283 2019-07-24 stsp if (err)
6976 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6978 0ebf8283 2019-07-24 stsp if (err)
6979 0ebf8283 2019-07-24 stsp goto done;
6980 0ebf8283 2019-07-24 stsp
6981 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6982 0ebf8283 2019-07-24 stsp if (err)
6983 0ebf8283 2019-07-24 stsp goto done;
6984 0ebf8283 2019-07-24 stsp done:
6985 0ebf8283 2019-07-24 stsp free(fileindex_path);
6986 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6987 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6988 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6989 0ebf8283 2019-07-24 stsp if (wt_branch)
6990 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6991 0ebf8283 2019-07-24 stsp if (err) {
6992 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6993 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6994 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6995 0ebf8283 2019-07-24 stsp }
6996 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6997 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6998 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6999 0ebf8283 2019-07-24 stsp }
7000 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7001 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7002 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7003 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7004 3e3a69f1 2019-07-25 stsp }
7005 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7006 0ebf8283 2019-07-24 stsp }
7007 0ebf8283 2019-07-24 stsp return err;
7008 0ebf8283 2019-07-24 stsp }
7009 0ebf8283 2019-07-24 stsp
7010 0ebf8283 2019-07-24 stsp const struct got_error *
7011 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7012 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7013 0ebf8283 2019-07-24 stsp {
7014 3e3a69f1 2019-07-25 stsp if (fileindex)
7015 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7016 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7017 0ebf8283 2019-07-24 stsp }
7018 0ebf8283 2019-07-24 stsp
7019 0ebf8283 2019-07-24 stsp const struct got_error *
7020 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7021 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7022 0ebf8283 2019-07-24 stsp {
7023 0ebf8283 2019-07-24 stsp const struct got_error *err;
7024 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7027 0ebf8283 2019-07-24 stsp if (err)
7028 0ebf8283 2019-07-24 stsp return err;
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7031 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7032 0ebf8283 2019-07-24 stsp return NULL;
7033 0ebf8283 2019-07-24 stsp }
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp const struct got_error *
7036 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7037 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7038 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7039 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7040 0ebf8283 2019-07-24 stsp {
7041 0ebf8283 2019-07-24 stsp const struct got_error *err;
7042 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7043 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7044 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7045 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7046 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7047 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7048 0ebf8283 2019-07-24 stsp
7049 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7050 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7051 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7052 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7053 0ebf8283 2019-07-24 stsp
7054 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7055 3e3a69f1 2019-07-25 stsp if (err)
7056 3e3a69f1 2019-07-25 stsp return err;
7057 3e3a69f1 2019-07-25 stsp
7058 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7059 3e3a69f1 2019-07-25 stsp if (err)
7060 f032f1f7 2019-08-04 stsp goto done;
7061 f032f1f7 2019-08-04 stsp
7062 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7063 f032f1f7 2019-08-04 stsp &have_staged_files);
7064 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7065 f032f1f7 2019-08-04 stsp goto done;
7066 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7067 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7068 3e3a69f1 2019-07-25 stsp goto done;
7069 f032f1f7 2019-08-04 stsp }
7070 3e3a69f1 2019-07-25 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 f032f1f7 2019-08-04 stsp goto done;
7074 0ebf8283 2019-07-24 stsp
7075 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7076 0ebf8283 2019-07-24 stsp if (err)
7077 0ebf8283 2019-07-24 stsp goto done;
7078 0ebf8283 2019-07-24 stsp
7079 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7080 0ebf8283 2019-07-24 stsp if (err)
7081 0ebf8283 2019-07-24 stsp goto done;
7082 0ebf8283 2019-07-24 stsp
7083 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7084 0ebf8283 2019-07-24 stsp worktree);
7085 0ebf8283 2019-07-24 stsp if (err)
7086 0ebf8283 2019-07-24 stsp goto done;
7087 0ebf8283 2019-07-24 stsp
7088 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7089 0ebf8283 2019-07-24 stsp if (err)
7090 0ebf8283 2019-07-24 stsp goto done;
7091 0ebf8283 2019-07-24 stsp
7092 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7093 0ebf8283 2019-07-24 stsp if (err)
7094 0ebf8283 2019-07-24 stsp goto done;
7095 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7096 0ebf8283 2019-07-24 stsp if (err)
7097 0ebf8283 2019-07-24 stsp goto done;
7098 0ebf8283 2019-07-24 stsp
7099 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7100 0ebf8283 2019-07-24 stsp if (err)
7101 0ebf8283 2019-07-24 stsp goto done;
7102 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7103 0ebf8283 2019-07-24 stsp if (err)
7104 0ebf8283 2019-07-24 stsp goto done;
7105 0ebf8283 2019-07-24 stsp
7106 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7107 0ebf8283 2019-07-24 stsp if (err)
7108 0ebf8283 2019-07-24 stsp goto done;
7109 0ebf8283 2019-07-24 stsp done:
7110 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7111 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7112 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7113 0ebf8283 2019-07-24 stsp if (commit_ref)
7114 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7115 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7116 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7117 0ebf8283 2019-07-24 stsp if (err) {
7118 0ebf8283 2019-07-24 stsp free(*commit_id);
7119 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7120 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7121 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7122 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7123 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7124 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7125 0ebf8283 2019-07-24 stsp }
7126 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7127 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7128 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7129 3e3a69f1 2019-07-25 stsp }
7130 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7131 0ebf8283 2019-07-24 stsp }
7132 0ebf8283 2019-07-24 stsp return err;
7133 0ebf8283 2019-07-24 stsp }
7134 0ebf8283 2019-07-24 stsp
7135 0ebf8283 2019-07-24 stsp static const struct got_error *
7136 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7137 0ebf8283 2019-07-24 stsp {
7138 0ebf8283 2019-07-24 stsp const struct got_error *err;
7139 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7140 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7141 0ebf8283 2019-07-24 stsp
7142 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7143 0ebf8283 2019-07-24 stsp if (err)
7144 0ebf8283 2019-07-24 stsp goto done;
7145 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7146 0ebf8283 2019-07-24 stsp if (err)
7147 0ebf8283 2019-07-24 stsp goto done;
7148 0ebf8283 2019-07-24 stsp
7149 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7150 0ebf8283 2019-07-24 stsp worktree);
7151 0ebf8283 2019-07-24 stsp if (err)
7152 0ebf8283 2019-07-24 stsp goto done;
7153 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7154 0ebf8283 2019-07-24 stsp if (err)
7155 0ebf8283 2019-07-24 stsp goto done;
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7158 0ebf8283 2019-07-24 stsp if (err)
7159 0ebf8283 2019-07-24 stsp goto done;
7160 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp
7164 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7165 0ebf8283 2019-07-24 stsp if (err)
7166 0ebf8283 2019-07-24 stsp goto done;
7167 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7168 0ebf8283 2019-07-24 stsp if (err)
7169 0ebf8283 2019-07-24 stsp goto done;
7170 0ebf8283 2019-07-24 stsp done:
7171 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7172 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7173 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7174 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7175 0ebf8283 2019-07-24 stsp return err;
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp
7178 0ebf8283 2019-07-24 stsp const struct got_error *
7179 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7180 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7181 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7182 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7183 0ebf8283 2019-07-24 stsp {
7184 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7185 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7186 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7187 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7188 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7189 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7190 0ebf8283 2019-07-24 stsp
7191 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7192 0ebf8283 2019-07-24 stsp if (err)
7193 0ebf8283 2019-07-24 stsp return err;
7194 0ebf8283 2019-07-24 stsp
7195 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7196 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7197 a44927cc 2022-04-07 stsp if (err)
7198 a44927cc 2022-04-07 stsp goto done;
7199 a44927cc 2022-04-07 stsp
7200 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7201 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7202 0ebf8283 2019-07-24 stsp if (err)
7203 0ebf8283 2019-07-24 stsp goto done;
7204 0ebf8283 2019-07-24 stsp
7205 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
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_worktree_set_base_commit_id(worktree, repo, base_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 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7214 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7215 0ebf8283 2019-07-24 stsp if (err)
7216 0ebf8283 2019-07-24 stsp goto done;
7217 0ebf8283 2019-07-24 stsp
7218 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7219 0ebf8283 2019-07-24 stsp if (err)
7220 0ebf8283 2019-07-24 stsp goto done;
7221 0ebf8283 2019-07-24 stsp
7222 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7223 0ebf8283 2019-07-24 stsp if (err)
7224 0ebf8283 2019-07-24 stsp goto done;
7225 0ebf8283 2019-07-24 stsp
7226 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7227 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7228 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7229 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7230 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7231 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7232 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7233 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7234 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7235 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7236 0ebf8283 2019-07-24 stsp if (err)
7237 1f1abb7e 2019-08-08 stsp goto sync;
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7240 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7241 0ebf8283 2019-07-24 stsp sync:
7242 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7243 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7244 0ebf8283 2019-07-24 stsp err = sync_err;
7245 0ebf8283 2019-07-24 stsp done:
7246 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7247 0ebf8283 2019-07-24 stsp free(tree_id);
7248 818c7501 2019-07-11 stsp free(fileindex_path);
7249 818c7501 2019-07-11 stsp
7250 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7251 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7252 818c7501 2019-07-11 stsp err = unlockerr;
7253 818c7501 2019-07-11 stsp return err;
7254 818c7501 2019-07-11 stsp }
7255 0ebf8283 2019-07-24 stsp
7256 0ebf8283 2019-07-24 stsp const struct got_error *
7257 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7258 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7259 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7260 0ebf8283 2019-07-24 stsp {
7261 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7262 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7263 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7264 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7265 0ebf8283 2019-07-24 stsp
7266 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7267 0ebf8283 2019-07-24 stsp if (err)
7268 0ebf8283 2019-07-24 stsp return err;
7269 0ebf8283 2019-07-24 stsp
7270 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7271 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7272 e600f124 2021-03-21 stsp if (err)
7273 e600f124 2021-03-21 stsp goto done;
7274 e600f124 2021-03-21 stsp
7275 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7276 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7277 0ebf8283 2019-07-24 stsp if (err)
7278 0ebf8283 2019-07-24 stsp goto done;
7279 0ebf8283 2019-07-24 stsp
7280 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7281 0ebf8283 2019-07-24 stsp if (err)
7282 0ebf8283 2019-07-24 stsp goto done;
7283 0ebf8283 2019-07-24 stsp
7284 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7285 0ebf8283 2019-07-24 stsp if (err)
7286 0ebf8283 2019-07-24 stsp goto done;
7287 0ebf8283 2019-07-24 stsp
7288 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7289 0ebf8283 2019-07-24 stsp if (err)
7290 0ebf8283 2019-07-24 stsp goto done;
7291 0ebf8283 2019-07-24 stsp
7292 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7293 a615e0e7 2020-12-16 stsp if (err)
7294 a615e0e7 2020-12-16 stsp goto done;
7295 a615e0e7 2020-12-16 stsp
7296 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7297 a615e0e7 2020-12-16 stsp if (err)
7298 a615e0e7 2020-12-16 stsp goto done;
7299 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7300 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7301 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7302 a615e0e7 2020-12-16 stsp err = sync_err;
7303 0ebf8283 2019-07-24 stsp done:
7304 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7305 a615e0e7 2020-12-16 stsp free(fileindex_path);
7306 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7307 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7308 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7309 0ebf8283 2019-07-24 stsp err = unlockerr;
7310 0ebf8283 2019-07-24 stsp return err;
7311 0ebf8283 2019-07-24 stsp }
7312 0ebf8283 2019-07-24 stsp
7313 0ebf8283 2019-07-24 stsp const struct got_error *
7314 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7315 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7316 0ebf8283 2019-07-24 stsp {
7317 0ebf8283 2019-07-24 stsp const struct got_error *err;
7318 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7319 0ebf8283 2019-07-24 stsp
7320 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7321 0ebf8283 2019-07-24 stsp if (err)
7322 0ebf8283 2019-07-24 stsp return err;
7323 0ebf8283 2019-07-24 stsp
7324 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7325 0ebf8283 2019-07-24 stsp if (err)
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7329 0ebf8283 2019-07-24 stsp done:
7330 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7331 2822a352 2019-10-15 stsp return err;
7332 2822a352 2019-10-15 stsp }
7333 2822a352 2019-10-15 stsp
7334 2822a352 2019-10-15 stsp const struct got_error *
7335 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7336 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7337 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7338 2822a352 2019-10-15 stsp struct got_repository *repo)
7339 2822a352 2019-10-15 stsp {
7340 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7341 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7342 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7343 2822a352 2019-10-15 stsp
7344 2822a352 2019-10-15 stsp *fileindex = NULL;
7345 2822a352 2019-10-15 stsp *branch_ref = NULL;
7346 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7347 2822a352 2019-10-15 stsp
7348 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7349 2822a352 2019-10-15 stsp if (err)
7350 2822a352 2019-10-15 stsp return err;
7351 2822a352 2019-10-15 stsp
7352 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7353 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7354 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7355 2822a352 2019-10-15 stsp "update -b or different branch name required");
7356 2822a352 2019-10-15 stsp goto done;
7357 2822a352 2019-10-15 stsp }
7358 2822a352 2019-10-15 stsp
7359 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7360 2822a352 2019-10-15 stsp if (err)
7361 2822a352 2019-10-15 stsp goto done;
7362 2822a352 2019-10-15 stsp
7363 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7364 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7365 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7366 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7367 2822a352 2019-10-15 stsp &ok_arg);
7368 2822a352 2019-10-15 stsp if (err)
7369 2822a352 2019-10-15 stsp goto done;
7370 2822a352 2019-10-15 stsp
7371 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7372 2822a352 2019-10-15 stsp if (err)
7373 2822a352 2019-10-15 stsp goto done;
7374 2822a352 2019-10-15 stsp
7375 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7376 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7377 2822a352 2019-10-15 stsp done:
7378 2822a352 2019-10-15 stsp if (err) {
7379 2822a352 2019-10-15 stsp if (*branch_ref) {
7380 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7381 2822a352 2019-10-15 stsp *branch_ref = NULL;
7382 2822a352 2019-10-15 stsp }
7383 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7384 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7385 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7386 2822a352 2019-10-15 stsp }
7387 2822a352 2019-10-15 stsp if (*fileindex) {
7388 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7389 2822a352 2019-10-15 stsp *fileindex = NULL;
7390 2822a352 2019-10-15 stsp }
7391 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7392 2822a352 2019-10-15 stsp }
7393 0cb83759 2019-08-03 stsp return err;
7394 0cb83759 2019-08-03 stsp }
7395 0cb83759 2019-08-03 stsp
7396 2822a352 2019-10-15 stsp const struct got_error *
7397 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7398 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7399 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7400 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7401 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7402 2822a352 2019-10-15 stsp {
7403 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7404 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7405 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7406 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7407 2822a352 2019-10-15 stsp
7408 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7409 2822a352 2019-10-15 stsp if (err)
7410 2822a352 2019-10-15 stsp goto done;
7411 2822a352 2019-10-15 stsp
7412 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7413 2822a352 2019-10-15 stsp if (err)
7414 2822a352 2019-10-15 stsp goto done;
7415 2822a352 2019-10-15 stsp
7416 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7417 a44927cc 2022-04-07 stsp if (err)
7418 a44927cc 2022-04-07 stsp goto done;
7419 a44927cc 2022-04-07 stsp
7420 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7421 2822a352 2019-10-15 stsp worktree->path_prefix);
7422 2822a352 2019-10-15 stsp if (err)
7423 2822a352 2019-10-15 stsp goto done;
7424 2822a352 2019-10-15 stsp
7425 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7426 2822a352 2019-10-15 stsp if (err)
7427 2822a352 2019-10-15 stsp goto done;
7428 2822a352 2019-10-15 stsp
7429 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7430 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7431 2822a352 2019-10-15 stsp if (err)
7432 2822a352 2019-10-15 stsp goto sync;
7433 2822a352 2019-10-15 stsp
7434 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7435 2822a352 2019-10-15 stsp if (err)
7436 2822a352 2019-10-15 stsp goto sync;
7437 2822a352 2019-10-15 stsp
7438 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7439 6e210706 2021-01-22 stsp if (err)
7440 6e210706 2021-01-22 stsp goto sync;
7441 6e210706 2021-01-22 stsp
7442 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7443 2822a352 2019-10-15 stsp sync:
7444 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7445 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7446 2822a352 2019-10-15 stsp err = sync_err;
7447 2822a352 2019-10-15 stsp
7448 2822a352 2019-10-15 stsp done:
7449 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7450 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7451 2822a352 2019-10-15 stsp err = unlockerr;
7452 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7453 2822a352 2019-10-15 stsp
7454 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7455 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7456 2822a352 2019-10-15 stsp err = unlockerr;
7457 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7458 2822a352 2019-10-15 stsp
7459 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7460 2822a352 2019-10-15 stsp free(fileindex_path);
7461 2822a352 2019-10-15 stsp free(tree_id);
7462 a44927cc 2022-04-07 stsp if (commit)
7463 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7464 2822a352 2019-10-15 stsp
7465 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7466 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7467 2822a352 2019-10-15 stsp err = unlockerr;
7468 2822a352 2019-10-15 stsp return err;
7469 2822a352 2019-10-15 stsp }
7470 2822a352 2019-10-15 stsp
7471 2822a352 2019-10-15 stsp const struct got_error *
7472 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7473 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7474 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7475 2822a352 2019-10-15 stsp {
7476 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7477 8b692cd0 2019-10-21 stsp
7478 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7479 8b692cd0 2019-10-21 stsp
7480 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7481 8b692cd0 2019-10-21 stsp
7482 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7483 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7484 8b692cd0 2019-10-21 stsp err = unlockerr;
7485 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7486 8b692cd0 2019-10-21 stsp
7487 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7488 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7489 8b692cd0 2019-10-21 stsp err = unlockerr;
7490 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7491 f259c4c1 2021-09-24 stsp
7492 f259c4c1 2021-09-24 stsp return err;
7493 f259c4c1 2021-09-24 stsp }
7494 f259c4c1 2021-09-24 stsp
7495 f259c4c1 2021-09-24 stsp const struct got_error *
7496 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7497 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7498 f259c4c1 2021-09-24 stsp {
7499 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7500 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7501 f259c4c1 2021-09-24 stsp
7502 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7503 f259c4c1 2021-09-24 stsp if (err)
7504 f259c4c1 2021-09-24 stsp goto done;
7505 8b692cd0 2019-10-21 stsp
7506 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7507 f259c4c1 2021-09-24 stsp
7508 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7509 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7510 f259c4c1 2021-09-24 stsp err = sync_err;
7511 f259c4c1 2021-09-24 stsp done:
7512 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7513 f259c4c1 2021-09-24 stsp free(fileindex_path);
7514 8b692cd0 2019-10-21 stsp return err;
7515 2822a352 2019-10-15 stsp }
7516 2822a352 2019-10-15 stsp
7517 f259c4c1 2021-09-24 stsp static const struct got_error *
7518 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7519 f259c4c1 2021-09-24 stsp {
7520 f259c4c1 2021-09-24 stsp const struct got_error *err;
7521 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7522 f259c4c1 2021-09-24 stsp
7523 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7524 f259c4c1 2021-09-24 stsp if (err)
7525 f259c4c1 2021-09-24 stsp goto done;
7526 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7527 f259c4c1 2021-09-24 stsp if (err)
7528 f259c4c1 2021-09-24 stsp goto done;
7529 f259c4c1 2021-09-24 stsp
7530 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7531 f259c4c1 2021-09-24 stsp if (err)
7532 f259c4c1 2021-09-24 stsp goto done;
7533 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7534 f259c4c1 2021-09-24 stsp if (err)
7535 f259c4c1 2021-09-24 stsp goto done;
7536 f259c4c1 2021-09-24 stsp
7537 f259c4c1 2021-09-24 stsp done:
7538 f259c4c1 2021-09-24 stsp free(branch_refname);
7539 f259c4c1 2021-09-24 stsp free(commit_refname);
7540 f259c4c1 2021-09-24 stsp return err;
7541 f259c4c1 2021-09-24 stsp }
7542 f259c4c1 2021-09-24 stsp
7543 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7544 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7545 f259c4c1 2021-09-24 stsp const char *branch_name;
7546 f259c4c1 2021-09-24 stsp };
7547 f259c4c1 2021-09-24 stsp
7548 f259c4c1 2021-09-24 stsp static const struct got_error *
7549 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7550 f259c4c1 2021-09-24 stsp void *arg)
7551 f259c4c1 2021-09-24 stsp {
7552 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7553 f259c4c1 2021-09-24 stsp
7554 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7555 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7556 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7557 f259c4c1 2021-09-24 stsp
7558 f259c4c1 2021-09-24 stsp return NULL;
7559 f259c4c1 2021-09-24 stsp }
7560 f259c4c1 2021-09-24 stsp
7561 f259c4c1 2021-09-24 stsp
7562 f259c4c1 2021-09-24 stsp const struct got_error *
7563 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7564 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7565 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7566 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7567 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7568 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7569 f259c4c1 2021-09-24 stsp {
7570 f259c4c1 2021-09-24 stsp const struct got_error *err;
7571 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7572 f259c4c1 2021-09-24 stsp
7573 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7574 f259c4c1 2021-09-24 stsp if (err)
7575 f259c4c1 2021-09-24 stsp goto done;
7576 f259c4c1 2021-09-24 stsp
7577 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7578 f259c4c1 2021-09-24 stsp worktree);
7579 f259c4c1 2021-09-24 stsp if (err)
7580 f259c4c1 2021-09-24 stsp goto done;
7581 f259c4c1 2021-09-24 stsp
7582 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7583 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7584 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7585 f259c4c1 2021-09-24 stsp done:
7586 f259c4c1 2021-09-24 stsp free(fileindex_path);
7587 f259c4c1 2021-09-24 stsp return err;
7588 f259c4c1 2021-09-24 stsp }
7589 f259c4c1 2021-09-24 stsp
7590 f259c4c1 2021-09-24 stsp const struct got_error *
7591 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7592 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7593 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7594 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7595 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7596 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7597 0ff8d236 2021-09-28 stsp
7598 f259c4c1 2021-09-24 stsp {
7599 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7600 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7601 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7602 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7603 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7604 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7605 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7606 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7607 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7608 f259c4c1 2021-09-24 stsp
7609 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7610 f259c4c1 2021-09-24 stsp
7611 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7612 f259c4c1 2021-09-24 stsp
7613 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7614 f259c4c1 2021-09-24 stsp if (err)
7615 f259c4c1 2021-09-24 stsp goto done;
7616 f259c4c1 2021-09-24 stsp
7617 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7618 f259c4c1 2021-09-24 stsp if (err)
7619 f259c4c1 2021-09-24 stsp goto done;
7620 f259c4c1 2021-09-24 stsp
7621 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7622 f259c4c1 2021-09-24 stsp if (err)
7623 f259c4c1 2021-09-24 stsp goto done;
7624 f259c4c1 2021-09-24 stsp
7625 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7626 f259c4c1 2021-09-24 stsp &have_staged_files);
7627 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7628 f259c4c1 2021-09-24 stsp goto done;
7629 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7630 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7631 f259c4c1 2021-09-24 stsp goto done;
7632 f259c4c1 2021-09-24 stsp }
7633 f259c4c1 2021-09-24 stsp
7634 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7635 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7636 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7637 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7638 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7639 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7640 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7641 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7642 f259c4c1 2021-09-24 stsp if (err)
7643 f259c4c1 2021-09-24 stsp goto done;
7644 f259c4c1 2021-09-24 stsp
7645 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7646 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7647 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7648 f259c4c1 2021-09-24 stsp goto done;
7649 f259c4c1 2021-09-24 stsp }
7650 f259c4c1 2021-09-24 stsp
7651 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7652 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7653 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7654 f259c4c1 2021-09-24 stsp
7655 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7656 f259c4c1 2021-09-24 stsp ct_path++;
7657 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7658 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7659 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7660 f259c4c1 2021-09-24 stsp if (err)
7661 f259c4c1 2021-09-24 stsp goto done;
7662 f259c4c1 2021-09-24 stsp
7663 f259c4c1 2021-09-24 stsp }
7664 f259c4c1 2021-09-24 stsp
7665 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7666 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7667 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7668 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7669 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7670 f259c4c1 2021-09-24 stsp if (err)
7671 f259c4c1 2021-09-24 stsp goto done;
7672 f259c4c1 2021-09-24 stsp
7673 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7674 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7675 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7676 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7677 f259c4c1 2021-09-24 stsp err = sync_err;
7678 f259c4c1 2021-09-24 stsp done:
7679 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7680 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7681 f259c4c1 2021-09-24 stsp free_commitable(ct);
7682 f259c4c1 2021-09-24 stsp }
7683 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7684 f259c4c1 2021-09-24 stsp free(fileindex_path);
7685 f259c4c1 2021-09-24 stsp return err;
7686 f259c4c1 2021-09-24 stsp }
7687 f259c4c1 2021-09-24 stsp
7688 f259c4c1 2021-09-24 stsp const struct got_error *
7689 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7690 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7691 f259c4c1 2021-09-24 stsp {
7692 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7693 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7694 f259c4c1 2021-09-24 stsp
7695 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7696 f259c4c1 2021-09-24 stsp if (err)
7697 f259c4c1 2021-09-24 stsp goto done;
7698 f259c4c1 2021-09-24 stsp
7699 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7700 f259c4c1 2021-09-24 stsp if (err)
7701 f259c4c1 2021-09-24 stsp goto done;
7702 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7703 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7704 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7705 f259c4c1 2021-09-24 stsp err = sync_err;
7706 f259c4c1 2021-09-24 stsp done:
7707 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7708 f259c4c1 2021-09-24 stsp free(fileindex_path);
7709 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7710 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7711 f259c4c1 2021-09-24 stsp err = unlockerr;
7712 f259c4c1 2021-09-24 stsp return err;
7713 f259c4c1 2021-09-24 stsp }
7714 f259c4c1 2021-09-24 stsp
7715 f259c4c1 2021-09-24 stsp const struct got_error *
7716 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7717 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7718 f259c4c1 2021-09-24 stsp {
7719 f259c4c1 2021-09-24 stsp const struct got_error *err;
7720 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7721 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7722 f259c4c1 2021-09-24 stsp
7723 f259c4c1 2021-09-24 stsp *in_progress = 0;
7724 f259c4c1 2021-09-24 stsp
7725 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7726 f259c4c1 2021-09-24 stsp if (err)
7727 f259c4c1 2021-09-24 stsp return err;
7728 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7729 793fcac3 2021-09-24 stsp free(branch_refname);
7730 f259c4c1 2021-09-24 stsp if (err) {
7731 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7732 f259c4c1 2021-09-24 stsp return err;
7733 f259c4c1 2021-09-24 stsp } else
7734 f259c4c1 2021-09-24 stsp *in_progress = 1;
7735 f259c4c1 2021-09-24 stsp
7736 f259c4c1 2021-09-24 stsp return NULL;
7737 f259c4c1 2021-09-24 stsp }
7738 f259c4c1 2021-09-24 stsp
7739 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7740 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7741 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7742 f259c4c1 2021-09-24 stsp {
7743 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7744 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7745 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7746 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7747 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7748 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7749 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7750 f259c4c1 2021-09-24 stsp
7751 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7752 f259c4c1 2021-09-24 stsp
7753 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7754 f259c4c1 2021-09-24 stsp if (err)
7755 f259c4c1 2021-09-24 stsp return err;
7756 f259c4c1 2021-09-24 stsp
7757 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7758 f259c4c1 2021-09-24 stsp if (err)
7759 f259c4c1 2021-09-24 stsp goto done;
7760 f259c4c1 2021-09-24 stsp
7761 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7762 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7763 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7764 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7765 f259c4c1 2021-09-24 stsp &ok_arg);
7766 f259c4c1 2021-09-24 stsp if (err)
7767 f259c4c1 2021-09-24 stsp goto done;
7768 f259c4c1 2021-09-24 stsp
7769 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7770 f259c4c1 2021-09-24 stsp if (err)
7771 f259c4c1 2021-09-24 stsp return err;
7772 f259c4c1 2021-09-24 stsp
7773 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7774 f259c4c1 2021-09-24 stsp if (err)
7775 f259c4c1 2021-09-24 stsp return err;
7776 f259c4c1 2021-09-24 stsp
7777 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7778 f259c4c1 2021-09-24 stsp 0);
7779 f259c4c1 2021-09-24 stsp if (err)
7780 f259c4c1 2021-09-24 stsp goto done;
7781 f259c4c1 2021-09-24 stsp
7782 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7783 f259c4c1 2021-09-24 stsp if (err)
7784 f259c4c1 2021-09-24 stsp goto done;
7785 f259c4c1 2021-09-24 stsp
7786 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7787 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7788 f259c4c1 2021-09-24 stsp goto done;
7789 f259c4c1 2021-09-24 stsp }
7790 f259c4c1 2021-09-24 stsp
7791 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7792 f259c4c1 2021-09-24 stsp if (err)
7793 f259c4c1 2021-09-24 stsp goto done;
7794 f259c4c1 2021-09-24 stsp
7795 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7796 f259c4c1 2021-09-24 stsp if (err)
7797 f259c4c1 2021-09-24 stsp goto done;
7798 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7799 f259c4c1 2021-09-24 stsp if (err)
7800 f259c4c1 2021-09-24 stsp goto done;
7801 f259c4c1 2021-09-24 stsp
7802 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7803 f259c4c1 2021-09-24 stsp if (err)
7804 f259c4c1 2021-09-24 stsp goto done;
7805 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7806 f259c4c1 2021-09-24 stsp if (err)
7807 f259c4c1 2021-09-24 stsp goto done;
7808 f259c4c1 2021-09-24 stsp
7809 f259c4c1 2021-09-24 stsp done:
7810 f259c4c1 2021-09-24 stsp free(branch_refname);
7811 f259c4c1 2021-09-24 stsp free(commit_refname);
7812 f259c4c1 2021-09-24 stsp free(fileindex_path);
7813 f259c4c1 2021-09-24 stsp if (branch_ref)
7814 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7815 f259c4c1 2021-09-24 stsp if (commit_ref)
7816 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7817 f259c4c1 2021-09-24 stsp if (wt_branch)
7818 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7819 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7820 f259c4c1 2021-09-24 stsp if (err) {
7821 f259c4c1 2021-09-24 stsp if (*fileindex) {
7822 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7823 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7824 f259c4c1 2021-09-24 stsp }
7825 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7826 f259c4c1 2021-09-24 stsp }
7827 f259c4c1 2021-09-24 stsp return err;
7828 f259c4c1 2021-09-24 stsp }
7829 f259c4c1 2021-09-24 stsp
7830 f259c4c1 2021-09-24 stsp const struct got_error *
7831 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7832 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7833 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7834 f259c4c1 2021-09-24 stsp {
7835 f259c4c1 2021-09-24 stsp const struct got_error *err;
7836 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7837 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7838 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7839 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7840 f259c4c1 2021-09-24 stsp
7841 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7842 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7843 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7844 f259c4c1 2021-09-24 stsp
7845 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7846 f259c4c1 2021-09-24 stsp if (err)
7847 f259c4c1 2021-09-24 stsp return err;
7848 f259c4c1 2021-09-24 stsp
7849 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7850 f259c4c1 2021-09-24 stsp if (err)
7851 f259c4c1 2021-09-24 stsp goto done;
7852 f259c4c1 2021-09-24 stsp
7853 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7854 f259c4c1 2021-09-24 stsp &have_staged_files);
7855 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7856 f259c4c1 2021-09-24 stsp goto done;
7857 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7858 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7859 f259c4c1 2021-09-24 stsp goto done;
7860 f259c4c1 2021-09-24 stsp }
7861 f259c4c1 2021-09-24 stsp
7862 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7863 f259c4c1 2021-09-24 stsp if (err)
7864 f259c4c1 2021-09-24 stsp goto done;
7865 f259c4c1 2021-09-24 stsp
7866 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7867 f259c4c1 2021-09-24 stsp if (err)
7868 f259c4c1 2021-09-24 stsp goto done;
7869 f259c4c1 2021-09-24 stsp
7870 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7871 f259c4c1 2021-09-24 stsp if (err)
7872 f259c4c1 2021-09-24 stsp goto done;
7873 f259c4c1 2021-09-24 stsp
7874 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7875 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7876 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7877 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7878 f259c4c1 2021-09-24 stsp goto done;
7879 f259c4c1 2021-09-24 stsp }
7880 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7881 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7882 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7883 f259c4c1 2021-09-24 stsp goto done;
7884 f259c4c1 2021-09-24 stsp }
7885 f259c4c1 2021-09-24 stsp
7886 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7887 f259c4c1 2021-09-24 stsp if (err)
7888 f259c4c1 2021-09-24 stsp goto done;
7889 f259c4c1 2021-09-24 stsp
7890 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7891 f259c4c1 2021-09-24 stsp if (err)
7892 f259c4c1 2021-09-24 stsp goto done;
7893 f259c4c1 2021-09-24 stsp done:
7894 f259c4c1 2021-09-24 stsp free(commit_refname);
7895 f259c4c1 2021-09-24 stsp free(branch_refname);
7896 f259c4c1 2021-09-24 stsp free(fileindex_path);
7897 f259c4c1 2021-09-24 stsp if (commit_ref)
7898 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7899 f259c4c1 2021-09-24 stsp if (branch_ref)
7900 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7901 f259c4c1 2021-09-24 stsp if (err) {
7902 f259c4c1 2021-09-24 stsp if (*branch_name) {
7903 f259c4c1 2021-09-24 stsp free(*branch_name);
7904 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7905 f259c4c1 2021-09-24 stsp }
7906 f259c4c1 2021-09-24 stsp free(*branch_tip);
7907 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7908 f259c4c1 2021-09-24 stsp if (*fileindex) {
7909 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7910 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7911 f259c4c1 2021-09-24 stsp }
7912 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7913 f259c4c1 2021-09-24 stsp }
7914 f259c4c1 2021-09-24 stsp return err;
7915 f259c4c1 2021-09-24 stsp }
7916 f259c4c1 2021-09-24 stsp
7917 f259c4c1 2021-09-24 stsp const struct got_error *
7918 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7919 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7920 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7921 f259c4c1 2021-09-24 stsp {
7922 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7923 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7924 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7925 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7926 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7927 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7928 f259c4c1 2021-09-24 stsp
7929 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7930 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7931 a44927cc 2022-04-07 stsp if (err)
7932 a44927cc 2022-04-07 stsp goto done;
7933 a44927cc 2022-04-07 stsp
7934 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7935 a44927cc 2022-04-07 stsp worktree->path_prefix);
7936 f259c4c1 2021-09-24 stsp if (err)
7937 f259c4c1 2021-09-24 stsp goto done;
7938 f259c4c1 2021-09-24 stsp
7939 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7940 f259c4c1 2021-09-24 stsp if (err)
7941 f259c4c1 2021-09-24 stsp goto done;
7942 f259c4c1 2021-09-24 stsp
7943 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7944 f259c4c1 2021-09-24 stsp if (err)
7945 f259c4c1 2021-09-24 stsp goto done;
7946 f259c4c1 2021-09-24 stsp
7947 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7948 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7949 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7950 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7951 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7952 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7953 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7954 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7955 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7956 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7957 f259c4c1 2021-09-24 stsp if (err)
7958 f259c4c1 2021-09-24 stsp goto sync;
7959 f259c4c1 2021-09-24 stsp
7960 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7961 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7962 f259c4c1 2021-09-24 stsp sync:
7963 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7964 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7965 f259c4c1 2021-09-24 stsp err = sync_err;
7966 f259c4c1 2021-09-24 stsp done:
7967 f259c4c1 2021-09-24 stsp free(tree_id);
7968 f259c4c1 2021-09-24 stsp free(commit_id);
7969 a44927cc 2022-04-07 stsp if (commit)
7970 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7971 f259c4c1 2021-09-24 stsp if (fileindex)
7972 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7973 f259c4c1 2021-09-24 stsp free(fileindex_path);
7974 f259c4c1 2021-09-24 stsp
7975 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7976 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7977 f259c4c1 2021-09-24 stsp err = unlockerr;
7978 f259c4c1 2021-09-24 stsp return err;
7979 f259c4c1 2021-09-24 stsp }
7980 f259c4c1 2021-09-24 stsp
7981 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7982 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7983 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7984 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7985 2db2652d 2019-08-07 stsp struct got_repository *repo;
7986 2db2652d 2019-08-07 stsp int have_changes;
7987 2db2652d 2019-08-07 stsp };
7988 2db2652d 2019-08-07 stsp
7989 336075a4 2022-06-25 op static const struct got_error *
7990 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7991 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7992 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7993 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7994 735ef5ac 2019-08-03 stsp {
7995 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7996 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7997 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7998 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7999 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8000 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8001 8b13ce36 2019-08-08 stsp
8002 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8003 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8004 8b13ce36 2019-08-08 stsp return NULL;
8005 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8006 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8007 735ef5ac 2019-08-03 stsp
8008 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8009 735ef5ac 2019-08-03 stsp if (ie == NULL)
8010 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8011 735ef5ac 2019-08-03 stsp
8012 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8013 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8014 735ef5ac 2019-08-03 stsp relpath) == -1)
8015 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8016 735ef5ac 2019-08-03 stsp
8017 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8018 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8019 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8020 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8021 735ef5ac 2019-08-03 stsp }
8022 735ef5ac 2019-08-03 stsp
8023 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8024 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8025 3aa5969e 2019-08-06 stsp goto done;
8026 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8027 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8028 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8029 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8030 735ef5ac 2019-08-03 stsp goto done;
8031 3aa5969e 2019-08-06 stsp }
8032 735ef5ac 2019-08-03 stsp
8033 2db2652d 2019-08-07 stsp a->have_changes = 1;
8034 2db2652d 2019-08-07 stsp
8035 735ef5ac 2019-08-03 stsp p = in_repo_path;
8036 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8037 735ef5ac 2019-08-03 stsp p++;
8038 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8039 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8040 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8041 735ef5ac 2019-08-03 stsp done:
8042 735ef5ac 2019-08-03 stsp free(in_repo_path);
8043 dc424a06 2019-08-07 stsp return err;
8044 dc424a06 2019-08-07 stsp }
8045 dc424a06 2019-08-07 stsp
8046 2db2652d 2019-08-07 stsp struct stage_path_arg {
8047 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8048 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8049 2db2652d 2019-08-07 stsp struct got_repository *repo;
8050 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8051 2db2652d 2019-08-07 stsp void *status_arg;
8052 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8053 2db2652d 2019-08-07 stsp void *patch_arg;
8054 7b5dc508 2019-10-28 stsp int staged_something;
8055 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8056 2db2652d 2019-08-07 stsp };
8057 2db2652d 2019-08-07 stsp
8058 2db2652d 2019-08-07 stsp static const struct got_error *
8059 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8060 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8061 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8062 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8063 0cb83759 2019-08-03 stsp {
8064 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8065 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8066 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8067 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8068 0cb83759 2019-08-03 stsp uint32_t stage;
8069 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8070 0aeb8099 2020-07-23 stsp struct stat sb;
8071 8b13ce36 2019-08-08 stsp
8072 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8073 8b13ce36 2019-08-08 stsp return NULL;
8074 0cb83759 2019-08-03 stsp
8075 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8076 d3e7c587 2019-08-03 stsp if (ie == NULL)
8077 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8078 0cb83759 2019-08-03 stsp
8079 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8080 2db2652d 2019-08-07 stsp relpath)== -1)
8081 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8082 0cb83759 2019-08-03 stsp
8083 0cb83759 2019-08-03 stsp switch (status) {
8084 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8085 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8086 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8087 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8088 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8089 0aeb8099 2020-07-23 stsp break;
8090 0aeb8099 2020-07-23 stsp }
8091 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8092 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8093 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8094 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8095 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8096 dc424a06 2019-08-07 stsp if (err)
8097 dc424a06 2019-08-07 stsp break;
8098 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8099 dc424a06 2019-08-07 stsp break;
8100 dc424a06 2019-08-07 stsp } else {
8101 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8102 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8103 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8104 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8105 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8106 dc424a06 2019-08-07 stsp break;
8107 dc424a06 2019-08-07 stsp }
8108 dc424a06 2019-08-07 stsp }
8109 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8110 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8111 0cb83759 2019-08-03 stsp if (err)
8112 dc424a06 2019-08-07 stsp break;
8113 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8114 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8115 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8116 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8117 0cb83759 2019-08-03 stsp else
8118 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8119 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8120 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8121 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8122 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8123 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8124 35213c7c 2020-07-23 stsp ssize_t target_len;
8125 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8126 35213c7c 2020-07-23 stsp sizeof(target_path));
8127 35213c7c 2020-07-23 stsp if (target_len == -1) {
8128 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8129 35213c7c 2020-07-23 stsp ondisk_path);
8130 35213c7c 2020-07-23 stsp break;
8131 35213c7c 2020-07-23 stsp }
8132 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8133 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8134 35213c7c 2020-07-23 stsp a->worktree->root_path);
8135 35213c7c 2020-07-23 stsp if (err)
8136 35213c7c 2020-07-23 stsp break;
8137 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8138 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8139 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8140 35213c7c 2020-07-23 stsp break;
8141 35213c7c 2020-07-23 stsp }
8142 35213c7c 2020-07-23 stsp }
8143 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8144 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8145 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8146 35213c7c 2020-07-23 stsp else
8147 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8148 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8149 0aeb8099 2020-07-23 stsp } else {
8150 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8151 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8152 0aeb8099 2020-07-23 stsp }
8153 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8154 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8155 dc424a06 2019-08-07 stsp break;
8156 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8157 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8158 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8159 9fdde394 2022-06-04 op if (err)
8160 9fdde394 2022-06-04 op break;
8161 9fdde394 2022-06-04 op /*
8162 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8163 9fdde394 2022-06-04 op * implicitly unstage the file.
8164 9fdde394 2022-06-04 op */
8165 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8166 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8167 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8168 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8169 9fdde394 2022-06-04 op }
8170 0cb83759 2019-08-03 stsp break;
8171 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8172 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8173 d3e7c587 2019-08-03 stsp break;
8174 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8175 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8176 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8177 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8178 dc424a06 2019-08-07 stsp if (err)
8179 dc424a06 2019-08-07 stsp break;
8180 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8181 88f33a19 2019-08-08 stsp break;
8182 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8183 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8184 dc424a06 2019-08-07 stsp break;
8185 88f33a19 2019-08-08 stsp }
8186 dc424a06 2019-08-07 stsp }
8187 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8188 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8189 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8190 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8191 dc424a06 2019-08-07 stsp break;
8192 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8193 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8194 12463d8b 2019-12-13 stsp de_name);
8195 0cb83759 2019-08-03 stsp break;
8196 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8197 d3e7c587 2019-08-03 stsp break;
8198 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8199 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8200 ebf48fd5 2019-08-03 stsp break;
8201 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8202 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8203 2a06fe5f 2019-08-24 stsp break;
8204 0cb83759 2019-08-03 stsp default:
8205 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8206 537ac44b 2019-08-03 stsp break;
8207 0cb83759 2019-08-03 stsp }
8208 dc424a06 2019-08-07 stsp
8209 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8210 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8211 dc424a06 2019-08-07 stsp free(path_content);
8212 2db2652d 2019-08-07 stsp free(ondisk_path);
8213 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8214 0ebf8283 2019-07-24 stsp return err;
8215 0ebf8283 2019-07-24 stsp }
8216 0cb83759 2019-08-03 stsp
8217 0cb83759 2019-08-03 stsp const struct got_error *
8218 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8219 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8220 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8221 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8222 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8223 0cb83759 2019-08-03 stsp {
8224 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8225 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8226 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8227 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8228 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8229 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8230 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8231 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8232 0cb83759 2019-08-03 stsp
8233 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8234 0cb83759 2019-08-03 stsp if (err)
8235 0cb83759 2019-08-03 stsp return err;
8236 0cb83759 2019-08-03 stsp
8237 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8238 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8239 735ef5ac 2019-08-03 stsp if (err)
8240 735ef5ac 2019-08-03 stsp goto done;
8241 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8242 735ef5ac 2019-08-03 stsp if (err)
8243 735ef5ac 2019-08-03 stsp goto done;
8244 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8245 0cb83759 2019-08-03 stsp if (err)
8246 0cb83759 2019-08-03 stsp goto done;
8247 0cb83759 2019-08-03 stsp
8248 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8249 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8250 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8251 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8252 2db2652d 2019-08-07 stsp oka.repo = repo;
8253 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8254 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8255 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8256 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8257 735ef5ac 2019-08-03 stsp if (err)
8258 735ef5ac 2019-08-03 stsp goto done;
8259 735ef5ac 2019-08-03 stsp }
8260 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8261 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8262 2db2652d 2019-08-07 stsp goto done;
8263 2db2652d 2019-08-07 stsp }
8264 735ef5ac 2019-08-03 stsp
8265 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8266 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8267 2db2652d 2019-08-07 stsp spa.repo = repo;
8268 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8269 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8270 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8271 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8272 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8273 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8274 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8275 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8276 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8277 42005733 2019-08-03 stsp if (err)
8278 2db2652d 2019-08-07 stsp goto done;
8279 7b5dc508 2019-10-28 stsp }
8280 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8281 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8282 7b5dc508 2019-10-28 stsp goto done;
8283 0cb83759 2019-08-03 stsp }
8284 0cb83759 2019-08-03 stsp
8285 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8286 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8287 0cb83759 2019-08-03 stsp err = sync_err;
8288 0cb83759 2019-08-03 stsp done:
8289 735ef5ac 2019-08-03 stsp if (head_ref)
8290 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8291 735ef5ac 2019-08-03 stsp free(head_commit_id);
8292 0cb83759 2019-08-03 stsp free(fileindex_path);
8293 0cb83759 2019-08-03 stsp if (fileindex)
8294 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8295 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8296 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8297 0cb83759 2019-08-03 stsp err = unlockerr;
8298 0cb83759 2019-08-03 stsp return err;
8299 0cb83759 2019-08-03 stsp }
8300 ad493afc 2019-08-03 stsp
8301 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8302 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8303 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8304 ad493afc 2019-08-03 stsp struct got_repository *repo;
8305 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8306 ad493afc 2019-08-03 stsp void *progress_arg;
8307 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8308 2e1f37b0 2019-08-08 stsp void *patch_arg;
8309 ad493afc 2019-08-03 stsp };
8310 2e1f37b0 2019-08-08 stsp
8311 2e1f37b0 2019-08-08 stsp static const struct got_error *
8312 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8313 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8314 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8315 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8316 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8317 2e1f37b0 2019-08-08 stsp {
8318 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8319 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8320 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8321 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8322 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8323 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8324 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8325 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8326 2e1f37b0 2019-08-08 stsp
8327 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8328 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8329 2e1f37b0 2019-08-08 stsp
8330 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8331 2e1f37b0 2019-08-08 stsp if (err)
8332 2e1f37b0 2019-08-08 stsp return err;
8333 eb81bc23 2022-06-28 tracey
8334 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8335 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8336 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8337 eb81bc23 2022-06-28 tracey goto done;
8338 eb81bc23 2022-06-28 tracey }
8339 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8340 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8341 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8342 eb81bc23 2022-06-28 tracey goto done;
8343 eb81bc23 2022-06-28 tracey }
8344 eb81bc23 2022-06-28 tracey
8345 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8346 2e1f37b0 2019-08-08 stsp if (err)
8347 2e1f37b0 2019-08-08 stsp goto done;
8348 2e1f37b0 2019-08-08 stsp
8349 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8350 2e1f37b0 2019-08-08 stsp if (err)
8351 2e1f37b0 2019-08-08 stsp goto done;
8352 2e1f37b0 2019-08-08 stsp
8353 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8354 2e1f37b0 2019-08-08 stsp if (err)
8355 2e1f37b0 2019-08-08 stsp goto done;
8356 2e1f37b0 2019-08-08 stsp
8357 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8358 eb81bc23 2022-06-28 tracey fd2);
8359 2e1f37b0 2019-08-08 stsp if (err)
8360 2e1f37b0 2019-08-08 stsp goto done;
8361 2e1f37b0 2019-08-08 stsp
8362 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8363 2e1f37b0 2019-08-08 stsp if (err)
8364 2e1f37b0 2019-08-08 stsp goto done;
8365 ad493afc 2019-08-03 stsp
8366 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8367 2e1f37b0 2019-08-08 stsp if (err)
8368 2e1f37b0 2019-08-08 stsp goto done;
8369 2e1f37b0 2019-08-08 stsp
8370 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8371 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8372 2e1f37b0 2019-08-08 stsp if (err)
8373 2e1f37b0 2019-08-08 stsp goto done;
8374 2e1f37b0 2019-08-08 stsp
8375 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8376 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8377 2e1f37b0 2019-08-08 stsp if (err)
8378 2e1f37b0 2019-08-08 stsp goto done;
8379 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8380 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8381 2e1f37b0 2019-08-08 stsp if (err)
8382 2e1f37b0 2019-08-08 stsp goto done;
8383 2e1f37b0 2019-08-08 stsp
8384 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8385 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8386 2e1f37b0 2019-08-08 stsp goto done;
8387 2e1f37b0 2019-08-08 stsp }
8388 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8389 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8390 2e1f37b0 2019-08-08 stsp goto done;
8391 2e1f37b0 2019-08-08 stsp }
8392 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8393 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8394 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8395 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8396 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8397 fe621944 2020-11-10 stsp nchanges++;
8398 fe621944 2020-11-10 stsp }
8399 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8400 2e1f37b0 2019-08-08 stsp int choice;
8401 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8402 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8403 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8404 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8405 2e1f37b0 2019-08-08 stsp if (err)
8406 2e1f37b0 2019-08-08 stsp goto done;
8407 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8408 2e1f37b0 2019-08-08 stsp have_content = 1;
8409 19e4b907 2019-08-08 stsp else
8410 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8411 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8412 2e1f37b0 2019-08-08 stsp break;
8413 2e1f37b0 2019-08-08 stsp }
8414 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8415 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8416 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8417 2e1f37b0 2019-08-08 stsp done:
8418 2e1f37b0 2019-08-08 stsp free(label1);
8419 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8420 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8421 2e1f37b0 2019-08-08 stsp if (blob)
8422 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8423 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8424 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8425 2e1f37b0 2019-08-08 stsp if (staged_blob)
8426 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8427 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8428 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8429 fe621944 2020-11-10 stsp err = free_err;
8430 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8431 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8432 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8433 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8434 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8435 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8436 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8437 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8438 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8439 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8440 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8441 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8442 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8443 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8444 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8445 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8446 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8447 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8448 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8449 2e1f37b0 2019-08-08 stsp }
8450 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8451 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8452 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8453 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8454 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8455 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8456 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8457 2e1f37b0 2019-08-08 stsp }
8458 2e1f37b0 2019-08-08 stsp free(path1);
8459 2e1f37b0 2019-08-08 stsp free(path2);
8460 fda8017d 2020-07-23 stsp return err;
8461 fda8017d 2020-07-23 stsp }
8462 fda8017d 2020-07-23 stsp
8463 fda8017d 2020-07-23 stsp static const struct got_error *
8464 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8465 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8466 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8467 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8468 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8469 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8470 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8471 fda8017d 2020-07-23 stsp {
8472 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8473 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8474 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8475 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8476 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8477 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8478 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8479 fda8017d 2020-07-23 stsp struct stat sb;
8480 fda8017d 2020-07-23 stsp
8481 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8482 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8483 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8484 fda8017d 2020-07-23 stsp if (err)
8485 fda8017d 2020-07-23 stsp return err;
8486 fda8017d 2020-07-23 stsp
8487 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8488 fda8017d 2020-07-23 stsp return NULL;
8489 fda8017d 2020-07-23 stsp
8490 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8491 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8492 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8493 fda8017d 2020-07-23 stsp if (err)
8494 fda8017d 2020-07-23 stsp goto done;
8495 fda8017d 2020-07-23 stsp }
8496 fda8017d 2020-07-23 stsp
8497 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8498 fda8017d 2020-07-23 stsp if (f == NULL) {
8499 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8500 fda8017d 2020-07-23 stsp path_unstaged_content);
8501 fda8017d 2020-07-23 stsp goto done;
8502 fda8017d 2020-07-23 stsp }
8503 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8504 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8505 fda8017d 2020-07-23 stsp goto done;
8506 fda8017d 2020-07-23 stsp }
8507 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8508 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8509 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8510 fda8017d 2020-07-23 stsp size_t r;
8511 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8512 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8513 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8514 fda8017d 2020-07-23 stsp goto done;
8515 fda8017d 2020-07-23 stsp }
8516 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8517 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8518 fda8017d 2020-07-23 stsp goto done;
8519 fda8017d 2020-07-23 stsp }
8520 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8521 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8522 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8523 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8524 fda8017d 2020-07-23 stsp progress_arg);
8525 fda8017d 2020-07-23 stsp } else {
8526 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8527 67a66647 2021-05-31 stsp
8528 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8529 67a66647 2021-05-31 stsp if (err)
8530 67a66647 2021-05-31 stsp return err;
8531 67a66647 2021-05-31 stsp
8532 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8533 67a66647 2021-05-31 stsp parent) == -1) {
8534 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8535 67a66647 2021-05-31 stsp base_path = NULL;
8536 67a66647 2021-05-31 stsp goto done;
8537 67a66647 2021-05-31 stsp }
8538 67a66647 2021-05-31 stsp
8539 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8540 67a66647 2021-05-31 stsp if (err)
8541 67a66647 2021-05-31 stsp goto done;
8542 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8543 67a66647 2021-05-31 stsp blob_base);
8544 67a66647 2021-05-31 stsp if (err)
8545 67a66647 2021-05-31 stsp goto done;
8546 67a66647 2021-05-31 stsp
8547 5e91dae4 2022-08-30 stsp /*
8548 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8549 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8550 eec2f5a9 2021-06-03 stsp */
8551 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8552 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8553 eec2f5a9 2021-06-03 stsp ondisk_path);
8554 eec2f5a9 2021-06-03 stsp if (err)
8555 eec2f5a9 2021-06-03 stsp goto done;
8556 eec2f5a9 2021-06-03 stsp } else {
8557 eec2f5a9 2021-06-03 stsp int fd;
8558 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8559 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8560 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8561 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8562 eec2f5a9 2021-06-03 stsp goto done;
8563 eec2f5a9 2021-06-03 stsp }
8564 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8565 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8566 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8567 eec2f5a9 2021-06-03 stsp close(fd);
8568 eec2f5a9 2021-06-03 stsp goto done;
8569 eec2f5a9 2021-06-03 stsp }
8570 eec2f5a9 2021-06-03 stsp }
8571 eec2f5a9 2021-06-03 stsp
8572 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8573 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8574 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8575 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8576 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8577 fda8017d 2020-07-23 stsp }
8578 fda8017d 2020-07-23 stsp if (err)
8579 fda8017d 2020-07-23 stsp goto done;
8580 fda8017d 2020-07-23 stsp
8581 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8582 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8583 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8584 2ac8aa02 2020-11-09 stsp } else {
8585 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8586 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8587 2ac8aa02 2020-11-09 stsp }
8588 fda8017d 2020-07-23 stsp done:
8589 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8590 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8591 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8592 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8593 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8594 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8595 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8596 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8597 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8598 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8599 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8600 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8601 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8602 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8603 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8604 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8605 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8606 67a66647 2021-05-31 stsp free(blob_base_path);
8607 67a66647 2021-05-31 stsp free(parent);
8608 67a66647 2021-05-31 stsp free(base_path);
8609 2e1f37b0 2019-08-08 stsp return err;
8610 2e1f37b0 2019-08-08 stsp }
8611 2e1f37b0 2019-08-08 stsp
8612 ad493afc 2019-08-03 stsp static const struct got_error *
8613 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8614 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8615 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8616 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8617 ad493afc 2019-08-03 stsp {
8618 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8619 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8620 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8621 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8622 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8623 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8624 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8625 9bc94a15 2019-08-03 stsp struct stat sb;
8626 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8627 ad493afc 2019-08-03 stsp
8628 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8629 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8630 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8631 2e1f37b0 2019-08-08 stsp return NULL;
8632 2e1f37b0 2019-08-08 stsp
8633 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8634 ad493afc 2019-08-03 stsp if (ie == NULL)
8635 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8636 9bc94a15 2019-08-03 stsp
8637 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8638 9bc94a15 2019-08-03 stsp == -1)
8639 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8640 ad493afc 2019-08-03 stsp
8641 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8642 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8643 f69721c3 2019-10-21 stsp if (err)
8644 f69721c3 2019-10-21 stsp goto done;
8645 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8646 f69721c3 2019-10-21 stsp id_str) == -1) {
8647 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8648 eb81bc23 2022-06-28 tracey goto done;
8649 eb81bc23 2022-06-28 tracey }
8650 eb81bc23 2022-06-28 tracey
8651 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8652 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8653 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8654 eb81bc23 2022-06-28 tracey goto done;
8655 eb81bc23 2022-06-28 tracey }
8656 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8657 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8658 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8659 f69721c3 2019-10-21 stsp goto done;
8660 f69721c3 2019-10-21 stsp }
8661 f69721c3 2019-10-21 stsp
8662 ad493afc 2019-08-03 stsp switch (staged_status) {
8663 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8664 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8665 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8666 ad493afc 2019-08-03 stsp if (err)
8667 ad493afc 2019-08-03 stsp break;
8668 ad493afc 2019-08-03 stsp /* fall through */
8669 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8670 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8671 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8672 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8673 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8674 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8675 2e1f37b0 2019-08-08 stsp if (err)
8676 2e1f37b0 2019-08-08 stsp break;
8677 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8678 2e1f37b0 2019-08-08 stsp break;
8679 2e1f37b0 2019-08-08 stsp } else {
8680 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8681 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8682 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8683 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8684 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8685 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8686 2e1f37b0 2019-08-08 stsp }
8687 2e1f37b0 2019-08-08 stsp }
8688 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8689 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8690 ad493afc 2019-08-03 stsp if (err)
8691 ad493afc 2019-08-03 stsp break;
8692 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8693 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8694 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8695 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8696 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8697 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8698 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8699 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8700 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8701 ea7786be 2020-07-23 stsp break;
8702 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8703 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8704 36bf999c 2020-07-23 stsp char *staged_target;
8705 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8706 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8707 36bf999c 2020-07-23 stsp if (err)
8708 36bf999c 2020-07-23 stsp goto done;
8709 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8710 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8711 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8712 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8713 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8714 36bf999c 2020-07-23 stsp free(staged_target);
8715 dfe9fba0 2020-07-23 stsp } else {
8716 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8717 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8718 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8719 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8720 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8721 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8722 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8723 dfe9fba0 2020-07-23 stsp }
8724 ea7786be 2020-07-23 stsp break;
8725 ea7786be 2020-07-23 stsp default:
8726 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8727 ea7786be 2020-07-23 stsp break;
8728 ea7786be 2020-07-23 stsp }
8729 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8730 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8731 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8732 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8733 2ac8aa02 2020-11-09 stsp }
8734 ad493afc 2019-08-03 stsp break;
8735 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8736 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8737 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8738 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8739 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8740 2e1f37b0 2019-08-08 stsp if (err)
8741 2e1f37b0 2019-08-08 stsp break;
8742 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8743 2e1f37b0 2019-08-08 stsp break;
8744 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8745 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8746 2e1f37b0 2019-08-08 stsp break;
8747 2e1f37b0 2019-08-08 stsp }
8748 2e1f37b0 2019-08-08 stsp }
8749 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8750 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8751 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8752 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8753 9bc94a15 2019-08-03 stsp if (err)
8754 9bc94a15 2019-08-03 stsp break;
8755 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8756 ad493afc 2019-08-03 stsp break;
8757 ad493afc 2019-08-03 stsp }
8758 f69721c3 2019-10-21 stsp done:
8759 ad493afc 2019-08-03 stsp free(ondisk_path);
8760 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8761 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8762 ad493afc 2019-08-03 stsp if (blob_base)
8763 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8764 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8765 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8766 ad493afc 2019-08-03 stsp if (blob_staged)
8767 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8768 f69721c3 2019-10-21 stsp free(id_str);
8769 f69721c3 2019-10-21 stsp free(label_orig);
8770 ad493afc 2019-08-03 stsp return err;
8771 ad493afc 2019-08-03 stsp }
8772 ad493afc 2019-08-03 stsp
8773 ad493afc 2019-08-03 stsp const struct got_error *
8774 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8775 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8776 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8777 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8778 ad493afc 2019-08-03 stsp struct got_repository *repo)
8779 ad493afc 2019-08-03 stsp {
8780 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8781 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8782 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8783 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8784 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8785 ad493afc 2019-08-03 stsp
8786 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8787 ad493afc 2019-08-03 stsp if (err)
8788 ad493afc 2019-08-03 stsp return err;
8789 ad493afc 2019-08-03 stsp
8790 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8791 ad493afc 2019-08-03 stsp if (err)
8792 ad493afc 2019-08-03 stsp goto done;
8793 ad493afc 2019-08-03 stsp
8794 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8795 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8796 ad493afc 2019-08-03 stsp upa.repo = repo;
8797 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8798 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8799 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8800 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8801 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8802 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8803 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8804 ad493afc 2019-08-03 stsp if (err)
8805 ad493afc 2019-08-03 stsp goto done;
8806 ad493afc 2019-08-03 stsp }
8807 ad493afc 2019-08-03 stsp
8808 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8809 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8810 ad493afc 2019-08-03 stsp err = sync_err;
8811 ad493afc 2019-08-03 stsp done:
8812 ad493afc 2019-08-03 stsp free(fileindex_path);
8813 ad493afc 2019-08-03 stsp if (fileindex)
8814 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8815 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8816 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8817 ad493afc 2019-08-03 stsp err = unlockerr;
8818 ad493afc 2019-08-03 stsp return err;
8819 ad493afc 2019-08-03 stsp }
8820 b2118c49 2020-07-28 stsp
8821 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8822 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8823 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8824 b2118c49 2020-07-28 stsp void *info_arg;
8825 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8826 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8827 b2118c49 2020-07-28 stsp void *cancel_arg;
8828 b2118c49 2020-07-28 stsp };
8829 b2118c49 2020-07-28 stsp
8830 b2118c49 2020-07-28 stsp static const struct got_error *
8831 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8832 b2118c49 2020-07-28 stsp {
8833 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8834 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8835 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8836 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8837 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8838 b2118c49 2020-07-28 stsp int stage;
8839 b2118c49 2020-07-28 stsp
8840 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8841 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8842 b2118c49 2020-07-28 stsp
8843 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8844 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8845 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8846 b2118c49 2020-07-28 stsp break;
8847 b2118c49 2020-07-28 stsp }
8848 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8849 b2118c49 2020-07-28 stsp return NULL;
8850 b2118c49 2020-07-28 stsp
8851 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8852 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8853 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8854 b2118c49 2020-07-28 stsp }
8855 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8856 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8857 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8858 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8859 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8860 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8861 b2118c49 2020-07-28 stsp }
8862 b2118c49 2020-07-28 stsp
8863 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8864 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8865 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8866 b2118c49 2020-07-28 stsp }
8867 b2118c49 2020-07-28 stsp
8868 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8869 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8870 b2118c49 2020-07-28 stsp }
8871 b2118c49 2020-07-28 stsp
8872 b2118c49 2020-07-28 stsp const struct got_error *
8873 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8874 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8875 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8876 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8877 b2118c49 2020-07-28 stsp
8878 b2118c49 2020-07-28 stsp {
8879 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8880 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8881 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8882 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8883 b2118c49 2020-07-28 stsp
8884 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8885 b2118c49 2020-07-28 stsp if (err)
8886 b2118c49 2020-07-28 stsp return err;
8887 b2118c49 2020-07-28 stsp
8888 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8889 b2118c49 2020-07-28 stsp if (err)
8890 b2118c49 2020-07-28 stsp goto done;
8891 b2118c49 2020-07-28 stsp
8892 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8893 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8894 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8895 b2118c49 2020-07-28 stsp arg.paths = paths;
8896 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8897 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8898 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8899 b2118c49 2020-07-28 stsp &arg);
8900 b2118c49 2020-07-28 stsp done:
8901 b2118c49 2020-07-28 stsp free(fileindex_path);
8902 b2118c49 2020-07-28 stsp if (fileindex)
8903 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8904 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8905 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8906 b2118c49 2020-07-28 stsp err = unlockerr;
8907 b2118c49 2020-07-28 stsp return err;
8908 b2118c49 2020-07-28 stsp }
8909 78f5ac24 2022-03-19 op
8910 78f5ac24 2022-03-19 op static const struct got_error *
8911 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
8912 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
8913 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
8914 78f5ac24 2022-03-19 op {
8915 78f5ac24 2022-03-19 op const struct got_error *err;
8916 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
8917 78f5ac24 2022-03-19 op struct stat sb;
8918 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
8919 78f5ac24 2022-03-19 op
8920 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
8921 78f5ac24 2022-03-19 op if (err)
8922 78f5ac24 2022-03-19 op return err;
8923 78f5ac24 2022-03-19 op
8924 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8925 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
8926 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
8927 78f5ac24 2022-03-19 op
8928 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8929 78f5ac24 2022-03-19 op if (ie) {
8930 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
8931 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8932 a05fb460 2022-04-23 op repo);
8933 78f5ac24 2022-03-19 op if (err)
8934 78f5ac24 2022-03-19 op goto done;
8935 78f5ac24 2022-03-19 op } else {
8936 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
8937 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
8938 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
8939 78f5ac24 2022-03-19 op if (errno != ENOENT) {
8940 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
8941 78f5ac24 2022-03-19 op ondisk_path);
8942 78f5ac24 2022-03-19 op goto done;
8943 78f5ac24 2022-03-19 op }
8944 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
8945 78f5ac24 2022-03-19 op }
8946 78f5ac24 2022-03-19 op }
8947 78f5ac24 2022-03-19 op
8948 78f5ac24 2022-03-19 op done:
8949 78f5ac24 2022-03-19 op free(ondisk_path);
8950 78f5ac24 2022-03-19 op return err;
8951 78f5ac24 2022-03-19 op }
8952 78f5ac24 2022-03-19 op
8953 78f5ac24 2022-03-19 op static const struct got_error *
8954 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
8955 78f5ac24 2022-03-19 op unsigned char staged_status)
8956 78f5ac24 2022-03-19 op {
8957 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8958 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8959 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8960 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8961 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
8962 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
8963 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8964 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
8965 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8966 78f5ac24 2022-03-19 op return NULL;
8967 78f5ac24 2022-03-19 op }
8968 78f5ac24 2022-03-19 op
8969 78f5ac24 2022-03-19 op static const struct got_error *
8970 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
8971 78f5ac24 2022-03-19 op {
8972 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
8973 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8974 78f5ac24 2022-03-19 op return NULL;
8975 78f5ac24 2022-03-19 op }
8976 78f5ac24 2022-03-19 op
8977 78f5ac24 2022-03-19 op static const struct got_error *
8978 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
8979 78f5ac24 2022-03-19 op unsigned char staged_status)
8980 78f5ac24 2022-03-19 op {
8981 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8982 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8983 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8984 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8985 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
8986 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8987 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
8988 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8989 78f5ac24 2022-03-19 op return NULL;
8990 78f5ac24 2022-03-19 op }
8991 78f5ac24 2022-03-19 op
8992 78f5ac24 2022-03-19 op const struct got_error *
8993 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
8994 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
8995 78f5ac24 2022-03-19 op {
8996 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
8997 78f5ac24 2022-03-19 op }
8998 78f5ac24 2022-03-19 op
8999 78f5ac24 2022-03-19 op const struct got_error *
9000 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9001 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9002 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9003 78f5ac24 2022-03-19 op {
9004 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9005 78f5ac24 2022-03-19 op int file_renamed = 0;
9006 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9007 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9008 78f5ac24 2022-03-19 op
9009 78f5ac24 2022-03-19 op *oldpath = NULL;
9010 78f5ac24 2022-03-19 op *newpath = NULL;
9011 78f5ac24 2022-03-19 op
9012 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9013 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9014 78f5ac24 2022-03-19 op if (err)
9015 78f5ac24 2022-03-19 op goto done;
9016 78f5ac24 2022-03-19 op
9017 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9018 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9019 78f5ac24 2022-03-19 op if (err)
9020 78f5ac24 2022-03-19 op goto done;
9021 78f5ac24 2022-03-19 op
9022 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9023 78f5ac24 2022-03-19 op file_renamed = 1;
9024 78f5ac24 2022-03-19 op
9025 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9026 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9027 78f5ac24 2022-03-19 op else if (file_renamed) {
9028 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9029 78f5ac24 2022-03-19 op if (err == NULL)
9030 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9031 78f5ac24 2022-03-19 op } else if (old == NULL)
9032 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9033 78f5ac24 2022-03-19 op else
9034 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9035 78f5ac24 2022-03-19 op
9036 78f5ac24 2022-03-19 op done:
9037 78f5ac24 2022-03-19 op if (err) {
9038 78f5ac24 2022-03-19 op free(*oldpath);
9039 78f5ac24 2022-03-19 op *oldpath = NULL;
9040 78f5ac24 2022-03-19 op free(*newpath);
9041 78f5ac24 2022-03-19 op *newpath = NULL;
9042 78f5ac24 2022-03-19 op }
9043 78f5ac24 2022-03-19 op return err;
9044 78f5ac24 2022-03-19 op }
9045 78f5ac24 2022-03-19 op
9046 78f5ac24 2022-03-19 op const struct got_error *
9047 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9048 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9049 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9050 78f5ac24 2022-03-19 op {
9051 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9052 f2dd7807 2022-05-17 op
9053 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9054 f2dd7807 2022-05-17 op saa.worktree = worktree;
9055 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9056 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9057 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9058 f2dd7807 2022-05-17 op saa.repo = repo;
9059 f2dd7807 2022-05-17 op
9060 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9061 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9062 78f5ac24 2022-03-19 op }
9063 f2dd7807 2022-05-17 op
9064 f2dd7807 2022-05-17 op const struct got_error *
9065 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9066 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9067 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9068 f2dd7807 2022-05-17 op {
9069 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9070 f2dd7807 2022-05-17 op
9071 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9072 f2dd7807 2022-05-17 op sda.worktree = worktree;
9073 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9074 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9075 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9076 f2dd7807 2022-05-17 op sda.repo = repo;
9077 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9078 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9079 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9080 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9081 f2dd7807 2022-05-17 op
9082 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9083 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9084 f2dd7807 2022-05-17 op }
9085 f2dd7807 2022-05-17 op
9086 f2dd7807 2022-05-17 op const struct got_error *
9087 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9088 4bcdc895 2022-05-17 op const char *fileindex_path)
9089 f2dd7807 2022-05-17 op {
9090 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9091 f2dd7807 2022-05-17 op
9092 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9093 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9094 f2dd7807 2022-05-17 op
9095 f2dd7807 2022-05-17 op return err;
9096 f2dd7807 2022-05-17 op }