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 eec2f5a9 2021-06-03 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 906c123b 2020-07-23 stsp char *path_got = NULL;
1260 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1262 8ba819a3 2020-07-23 stsp
1263 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1264 2e1fa222 2020-07-23 stsp
1265 8ba819a3 2020-07-23 stsp /*
1266 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1267 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1268 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1269 8ba819a3 2020-07-23 stsp * in the blob object.
1270 8ba819a3 2020-07-23 stsp */
1271 8ba819a3 2020-07-23 stsp do {
1272 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1273 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1274 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1276 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1277 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1278 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1280 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1281 3b9f0f87 2020-07-23 stsp progress_arg);
1282 8ba819a3 2020-07-23 stsp }
1283 8ba819a3 2020-07-23 stsp if (len > 0) {
1284 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1285 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1286 8ba819a3 2020-07-23 stsp len - hdrlen);
1287 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1288 8ba819a3 2020-07-23 stsp hdrlen = 0;
1289 8ba819a3 2020-07-23 stsp }
1290 8ba819a3 2020-07-23 stsp } while (len != 0);
1291 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1292 8ba819a3 2020-07-23 stsp
1293 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1295 3c1ec782 2020-07-23 stsp if (err)
1296 3c1ec782 2020-07-23 stsp return err;
1297 8ba819a3 2020-07-23 stsp
1298 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1299 906c123b 2020-07-23 stsp /* install as a regular file */
1300 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1301 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1302 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1304 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1305 906c123b 2020-07-23 stsp goto done;
1306 906c123b 2020-07-23 stsp }
1307 906c123b 2020-07-23 stsp
1308 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1309 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1310 c6e8a826 2021-04-05 stsp int symlink_replaced;
1311 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1312 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1313 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1314 c90c8ce3 2020-07-23 stsp goto done;
1315 c90c8ce3 2020-07-23 stsp }
1316 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1317 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1318 5a1fbc73 2020-07-23 stsp if (err)
1319 f35fa46a 2020-07-23 stsp goto done;
1320 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1321 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1322 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1323 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1324 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1326 c6e8a826 2021-04-05 stsp } else {
1327 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1328 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1329 c6e8a826 2021-04-05 stsp }
1330 f35fa46a 2020-07-23 stsp }
1331 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1332 f35fa46a 2020-07-23 stsp }
1333 f35fa46a 2020-07-23 stsp
1334 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1335 f4994adc 2020-10-20 stsp char *parent;
1336 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1337 f4994adc 2020-10-20 stsp if (err)
1338 f4994adc 2020-10-20 stsp goto done;
1339 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1340 f4994adc 2020-10-20 stsp free(parent);
1341 f35fa46a 2020-07-23 stsp if (err)
1342 f35fa46a 2020-07-23 stsp goto done;
1343 f35fa46a 2020-07-23 stsp /*
1344 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1345 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1346 f35fa46a 2020-07-23 stsp */
1347 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1348 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1349 f35fa46a 2020-07-23 stsp goto done;
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp }
1352 f35fa46a 2020-07-23 stsp
1353 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1354 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1355 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1356 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1357 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1358 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1359 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1361 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1362 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1363 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1364 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1365 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1366 8ba819a3 2020-07-23 stsp } else {
1367 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1368 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1369 8ba819a3 2020-07-23 stsp }
1370 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1371 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 8ba819a3 2020-07-23 stsp done:
1374 906c123b 2020-07-23 stsp free(path_got);
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 2020-01-02 naddy char *line = NULL;
1513 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1514 9bdd68dd 2020-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 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1518 9bdd68dd 2020-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 2020-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 234035bc 2019-06-01 stsp
3140 03415a1a 2019-06-02 stsp if (commit_id1) {
3141 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3142 a44927cc 2022-04-07 stsp if (err)
3143 a44927cc 2022-04-07 stsp goto done;
3144 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3145 03415a1a 2019-06-02 stsp worktree->path_prefix);
3146 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3147 03415a1a 2019-06-02 stsp goto done;
3148 69d57f3d 2020-07-31 stsp }
3149 69d57f3d 2020-07-31 stsp if (tree_id1) {
3150 69d57f3d 2020-07-31 stsp char *id_str;
3151 03415a1a 2019-06-02 stsp
3152 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3153 03415a1a 2019-06-02 stsp if (err)
3154 03415a1a 2019-06-02 stsp goto done;
3155 f69721c3 2019-10-21 stsp
3156 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3157 f69721c3 2019-10-21 stsp if (err)
3158 f69721c3 2019-10-21 stsp goto done;
3159 f69721c3 2019-10-21 stsp
3160 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3161 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3162 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3163 f69721c3 2019-10-21 stsp free(id_str);
3164 f69721c3 2019-10-21 stsp goto done;
3165 f69721c3 2019-10-21 stsp }
3166 f69721c3 2019-10-21 stsp free(id_str);
3167 b72706c3 2022-06-01 stsp
3168 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3169 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3170 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3171 b72706c3 2022-06-01 stsp goto done;
3172 b72706c3 2022-06-01 stsp }
3173 03415a1a 2019-06-02 stsp }
3174 234035bc 2019-06-01 stsp
3175 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3176 a44927cc 2022-04-07 stsp if (err)
3177 a44927cc 2022-04-07 stsp goto done;
3178 a44927cc 2022-04-07 stsp
3179 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3180 234035bc 2019-06-01 stsp worktree->path_prefix);
3181 234035bc 2019-06-01 stsp if (err)
3182 234035bc 2019-06-01 stsp goto done;
3183 234035bc 2019-06-01 stsp
3184 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3185 234035bc 2019-06-01 stsp if (err)
3186 234035bc 2019-06-01 stsp goto done;
3187 234035bc 2019-06-01 stsp
3188 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3189 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3190 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3191 b72706c3 2022-06-01 stsp goto done;
3192 b72706c3 2022-06-01 stsp }
3193 b72706c3 2022-06-01 stsp
3194 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3195 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3196 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3197 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3198 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3199 69de9dd4 2021-09-03 stsp if (err)
3200 69de9dd4 2021-09-03 stsp goto done;
3201 69de9dd4 2021-09-03 stsp
3202 234035bc 2019-06-01 stsp arg.worktree = worktree;
3203 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3204 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3205 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3206 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3207 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3208 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3209 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3210 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3211 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3212 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3213 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3214 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3215 af12c6b9 2019-06-04 stsp err = sync_err;
3216 234035bc 2019-06-01 stsp done:
3217 a44927cc 2022-04-07 stsp if (commit1)
3218 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3219 a44927cc 2022-04-07 stsp if (commit2)
3220 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3221 234035bc 2019-06-01 stsp if (tree1)
3222 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3223 234035bc 2019-06-01 stsp if (tree2)
3224 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3225 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3226 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3227 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3228 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3229 f69721c3 2019-10-21 stsp free(label_orig);
3230 818c7501 2019-07-11 stsp return err;
3231 818c7501 2019-07-11 stsp }
3232 234035bc 2019-06-01 stsp
3233 818c7501 2019-07-11 stsp const struct got_error *
3234 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3235 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3236 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3237 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3238 818c7501 2019-07-11 stsp {
3239 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3240 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3241 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3242 818c7501 2019-07-11 stsp
3243 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3244 818c7501 2019-07-11 stsp if (err)
3245 818c7501 2019-07-11 stsp return err;
3246 818c7501 2019-07-11 stsp
3247 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3248 818c7501 2019-07-11 stsp if (err)
3249 818c7501 2019-07-11 stsp goto done;
3250 818c7501 2019-07-11 stsp
3251 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3252 69de9dd4 2021-09-03 stsp worktree);
3253 818c7501 2019-07-11 stsp if (err)
3254 818c7501 2019-07-11 stsp goto done;
3255 818c7501 2019-07-11 stsp
3256 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3257 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3258 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3259 818c7501 2019-07-11 stsp done:
3260 818c7501 2019-07-11 stsp if (fileindex)
3261 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3262 818c7501 2019-07-11 stsp free(fileindex_path);
3263 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3264 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3265 234035bc 2019-06-01 stsp err = unlockerr;
3266 234035bc 2019-06-01 stsp return err;
3267 234035bc 2019-06-01 stsp }
3268 234035bc 2019-06-01 stsp
3269 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3270 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3271 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3272 927df6b7 2019-02-10 stsp const char *status_path;
3273 927df6b7 2019-02-10 stsp size_t status_path_len;
3274 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3275 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3276 f8d1f275 2019-02-04 stsp void *status_arg;
3277 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3278 f8d1f275 2019-02-04 stsp void *cancel_arg;
3279 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3280 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3281 f2a9dc41 2019-12-13 tracey int report_unchanged;
3282 3143d852 2020-06-25 stsp int no_ignores;
3283 f8d1f275 2019-02-04 stsp };
3284 88d0e355 2019-08-03 stsp
3285 f8d1f275 2019-02-04 stsp static const struct got_error *
3286 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3287 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3288 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3289 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3290 927df6b7 2019-02-10 stsp {
3291 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3292 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3293 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3294 927df6b7 2019-02-10 stsp struct stat sb;
3295 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3296 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3297 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3298 927df6b7 2019-02-10 stsp
3299 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3300 98eaaa12 2019-08-03 stsp if (err)
3301 98eaaa12 2019-08-03 stsp return err;
3302 98eaaa12 2019-08-03 stsp
3303 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3304 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3305 98eaaa12 2019-08-03 stsp return NULL;
3306 98eaaa12 2019-08-03 stsp
3307 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3308 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3309 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3310 98eaaa12 2019-08-03 stsp }
3311 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3312 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3313 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3314 927df6b7 2019-02-10 stsp }
3315 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3316 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3317 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3318 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3319 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3320 98eaaa12 2019-08-03 stsp }
3321 98eaaa12 2019-08-03 stsp
3322 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3323 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3324 927df6b7 2019-02-10 stsp }
3325 927df6b7 2019-02-10 stsp
3326 927df6b7 2019-02-10 stsp static const struct got_error *
3327 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3328 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3329 f8d1f275 2019-02-04 stsp {
3330 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3331 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3332 f8d1f275 2019-02-04 stsp char *abspath;
3333 f8d1f275 2019-02-04 stsp
3334 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3335 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3336 0584f854 2019-04-06 stsp
3337 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3338 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3339 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3340 927df6b7 2019-02-10 stsp return NULL;
3341 927df6b7 2019-02-10 stsp
3342 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3343 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3344 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3345 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3346 f8d1f275 2019-02-04 stsp } else {
3347 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3348 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3349 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3350 f8d1f275 2019-02-04 stsp }
3351 f8d1f275 2019-02-04 stsp
3352 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3353 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3354 f8d1f275 2019-02-04 stsp free(abspath);
3355 9d31a1d8 2018-03-11 stsp return err;
3356 9d31a1d8 2018-03-11 stsp }
3357 f8d1f275 2019-02-04 stsp
3358 f8d1f275 2019-02-04 stsp static const struct got_error *
3359 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3360 f8d1f275 2019-02-04 stsp {
3361 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3362 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3363 2ec1f75b 2019-03-26 stsp unsigned char status;
3364 927df6b7 2019-02-10 stsp
3365 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3367 0584f854 2019-04-06 stsp
3368 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3369 927df6b7 2019-02-10 stsp return NULL;
3370 927df6b7 2019-02-10 stsp
3371 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3372 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3373 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3374 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3375 2ec1f75b 2019-03-26 stsp else
3376 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3377 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3378 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3379 6841da00 2019-08-08 stsp }
3380 6841da00 2019-08-08 stsp
3381 336075a4 2022-06-25 op static void
3382 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3383 6841da00 2019-08-08 stsp {
3384 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3385 6841da00 2019-08-08 stsp
3386 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3387 6841da00 2019-08-08 stsp free((char *)pe->path);
3388 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3389 6841da00 2019-08-08 stsp }
3390 6841da00 2019-08-08 stsp
3391 336075a4 2022-06-25 op static void
3392 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3393 6841da00 2019-08-08 stsp {
3394 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3395 6841da00 2019-08-08 stsp
3396 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3397 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3398 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3399 6841da00 2019-08-08 stsp free((char *)pe->path);
3400 6841da00 2019-08-08 stsp }
3401 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3402 6841da00 2019-08-08 stsp }
3403 6841da00 2019-08-08 stsp
3404 6841da00 2019-08-08 stsp static const struct got_error *
3405 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3406 6841da00 2019-08-08 stsp {
3407 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3408 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3409 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3410 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3411 6841da00 2019-08-08 stsp size_t linesize = 0;
3412 6841da00 2019-08-08 stsp ssize_t linelen;
3413 6841da00 2019-08-08 stsp
3414 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3415 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3416 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3417 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3418 6841da00 2019-08-08 stsp
3419 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3420 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3421 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3422 bd8de430 2019-10-04 stsp
3423 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3424 bd8de430 2019-10-04 stsp if (line[0] == '#')
3425 bd8de430 2019-10-04 stsp continue;
3426 bd8de430 2019-10-04 stsp
3427 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3428 bd8de430 2019-10-04 stsp if (line[0] == '!')
3429 bd8de430 2019-10-04 stsp continue;
3430 bd8de430 2019-10-04 stsp
3431 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3432 6841da00 2019-08-08 stsp line) == -1) {
3433 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3434 6841da00 2019-08-08 stsp goto done;
3435 6841da00 2019-08-08 stsp }
3436 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3437 6841da00 2019-08-08 stsp if (err)
3438 6841da00 2019-08-08 stsp goto done;
3439 6841da00 2019-08-08 stsp }
3440 6841da00 2019-08-08 stsp if (ferror(f)) {
3441 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3442 6841da00 2019-08-08 stsp goto done;
3443 6841da00 2019-08-08 stsp }
3444 6841da00 2019-08-08 stsp
3445 6841da00 2019-08-08 stsp dirpath = strdup(path);
3446 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3447 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3448 6841da00 2019-08-08 stsp goto done;
3449 6841da00 2019-08-08 stsp }
3450 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3451 6841da00 2019-08-08 stsp done:
3452 6841da00 2019-08-08 stsp free(line);
3453 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3454 6841da00 2019-08-08 stsp free(dirpath);
3455 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3456 6841da00 2019-08-08 stsp }
3457 6841da00 2019-08-08 stsp return err;
3458 6841da00 2019-08-08 stsp }
3459 6841da00 2019-08-08 stsp
3460 336075a4 2022-06-25 op static int
3461 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3462 6841da00 2019-08-08 stsp {
3463 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3464 bd8de430 2019-10-04 stsp
3465 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3466 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3467 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3468 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3469 bd8de430 2019-10-04 stsp
3470 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3471 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3472 6841da00 2019-08-08 stsp
3473 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3474 bd8de430 2019-10-04 stsp continue;
3475 bd8de430 2019-10-04 stsp pattern += 3;
3476 bd8de430 2019-10-04 stsp p = path;
3477 bd8de430 2019-10-04 stsp while (*p) {
3478 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3479 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3480 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3481 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3482 bd8de430 2019-10-04 stsp p++;
3483 bd8de430 2019-10-04 stsp while (*p == '/')
3484 bd8de430 2019-10-04 stsp p++;
3485 bd8de430 2019-10-04 stsp continue;
3486 bd8de430 2019-10-04 stsp }
3487 bd8de430 2019-10-04 stsp return 1;
3488 bd8de430 2019-10-04 stsp }
3489 bd8de430 2019-10-04 stsp }
3490 bd8de430 2019-10-04 stsp }
3491 bd8de430 2019-10-04 stsp
3492 6841da00 2019-08-08 stsp /*
3493 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3494 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3495 6841da00 2019-08-08 stsp * ignores backwards.
3496 6841da00 2019-08-08 stsp */
3497 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3498 6841da00 2019-08-08 stsp while (pe) {
3499 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3500 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3501 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3502 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3503 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3504 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3505 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3506 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3507 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3508 6841da00 2019-08-08 stsp continue;
3509 6841da00 2019-08-08 stsp return 1;
3510 6841da00 2019-08-08 stsp }
3511 6841da00 2019-08-08 stsp }
3512 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3513 6841da00 2019-08-08 stsp }
3514 6841da00 2019-08-08 stsp
3515 6841da00 2019-08-08 stsp return 0;
3516 6841da00 2019-08-08 stsp }
3517 6841da00 2019-08-08 stsp
3518 6841da00 2019-08-08 stsp static const struct got_error *
3519 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3520 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3521 6841da00 2019-08-08 stsp {
3522 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3523 6841da00 2019-08-08 stsp char *ignorespath;
3524 886cec17 2019-12-15 stsp int fd = -1;
3525 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3526 6841da00 2019-08-08 stsp
3527 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3528 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3529 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3530 6841da00 2019-08-08 stsp
3531 886cec17 2019-12-15 stsp if (dirfd != -1) {
3532 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3533 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3534 886cec17 2019-12-15 stsp if (fd == -1) {
3535 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3536 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3537 886cec17 2019-12-15 stsp ignorespath);
3538 886cec17 2019-12-15 stsp } else {
3539 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3540 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3541 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3542 886cec17 2019-12-15 stsp ignorespath);
3543 886cec17 2019-12-15 stsp else {
3544 886cec17 2019-12-15 stsp fd = -1;
3545 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3546 886cec17 2019-12-15 stsp }
3547 886cec17 2019-12-15 stsp }
3548 886cec17 2019-12-15 stsp } else {
3549 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3550 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3551 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3552 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3553 886cec17 2019-12-15 stsp ignorespath);
3554 886cec17 2019-12-15 stsp } else
3555 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3556 886cec17 2019-12-15 stsp }
3557 6841da00 2019-08-08 stsp
3558 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3559 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3560 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3561 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3562 6841da00 2019-08-08 stsp free(ignorespath);
3563 6841da00 2019-08-08 stsp return err;
3564 f8d1f275 2019-02-04 stsp }
3565 f8d1f275 2019-02-04 stsp
3566 f8d1f275 2019-02-04 stsp static const struct got_error *
3567 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3568 62da3196 2021-10-01 stsp int dirfd)
3569 f8d1f275 2019-02-04 stsp {
3570 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3571 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3572 f8d1f275 2019-02-04 stsp char *path = NULL;
3573 62da3196 2021-10-01 stsp
3574 62da3196 2021-10-01 stsp if (ignore != NULL)
3575 62da3196 2021-10-01 stsp *ignore = 0;
3576 f8d1f275 2019-02-04 stsp
3577 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3578 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3579 0584f854 2019-04-06 stsp
3580 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3581 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3582 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3583 f8d1f275 2019-02-04 stsp } else {
3584 f8d1f275 2019-02-04 stsp path = de->d_name;
3585 f8d1f275 2019-02-04 stsp }
3586 f8d1f275 2019-02-04 stsp
3587 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3588 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3589 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3590 62da3196 2021-10-01 stsp *ignore = 1;
3591 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3592 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3593 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3594 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3595 f8d1f275 2019-02-04 stsp if (parent_path[0])
3596 f8d1f275 2019-02-04 stsp free(path);
3597 b72f483a 2019-02-05 stsp return err;
3598 f8d1f275 2019-02-04 stsp }
3599 f8d1f275 2019-02-04 stsp
3600 347d1d3e 2019-07-12 stsp static const struct got_error *
3601 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3602 3143d852 2020-06-25 stsp {
3603 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3604 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3605 3143d852 2020-06-25 stsp
3606 3143d852 2020-06-25 stsp if (a->no_ignores)
3607 3143d852 2020-06-25 stsp return NULL;
3608 3143d852 2020-06-25 stsp
3609 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3610 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3611 3143d852 2020-06-25 stsp if (err)
3612 3143d852 2020-06-25 stsp return err;
3613 3143d852 2020-06-25 stsp
3614 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3615 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3616 3143d852 2020-06-25 stsp
3617 3143d852 2020-06-25 stsp return err;
3618 3143d852 2020-06-25 stsp }
3619 3143d852 2020-06-25 stsp
3620 3143d852 2020-06-25 stsp static const struct got_error *
3621 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3622 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3623 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3624 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3625 abb4604f 2019-07-27 stsp {
3626 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3627 abb4604f 2019-07-27 stsp struct stat sb;
3628 ff56836b 2021-07-08 stsp
3629 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3630 abb4604f 2019-07-27 stsp if (ie)
3631 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3632 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3633 abb4604f 2019-07-27 stsp
3634 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3635 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3636 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3637 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3638 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3639 abb4604f 2019-07-27 stsp }
3640 abb4604f 2019-07-27 stsp
3641 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3642 916237f3 2022-02-11 stsp return NULL;
3643 916237f3 2022-02-11 stsp
3644 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3645 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3646 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3647 abb4604f 2019-07-27 stsp
3648 abb4604f 2019-07-27 stsp return NULL;
3649 3143d852 2020-06-25 stsp }
3650 3143d852 2020-06-25 stsp
3651 3143d852 2020-06-25 stsp static const struct got_error *
3652 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3653 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3654 3143d852 2020-06-25 stsp {
3655 3143d852 2020-06-25 stsp const struct got_error *err;
3656 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3657 3143d852 2020-06-25 stsp
3658 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3659 3143d852 2020-06-25 stsp ".cvsignore");
3660 3143d852 2020-06-25 stsp if (err)
3661 3143d852 2020-06-25 stsp return err;
3662 3143d852 2020-06-25 stsp
3663 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3664 3143d852 2020-06-25 stsp ".gitignore");
3665 3143d852 2020-06-25 stsp if (err)
3666 3143d852 2020-06-25 stsp return err;
3667 3143d852 2020-06-25 stsp
3668 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3669 3143d852 2020-06-25 stsp if (err) {
3670 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3671 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3672 3143d852 2020-06-25 stsp return err;
3673 3143d852 2020-06-25 stsp }
3674 3143d852 2020-06-25 stsp for (;;) {
3675 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3676 3143d852 2020-06-25 stsp ".cvsignore");
3677 3143d852 2020-06-25 stsp if (err)
3678 3143d852 2020-06-25 stsp break;
3679 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3680 3143d852 2020-06-25 stsp ".gitignore");
3681 3143d852 2020-06-25 stsp if (err)
3682 3143d852 2020-06-25 stsp break;
3683 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3684 3143d852 2020-06-25 stsp if (err) {
3685 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3686 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3687 3143d852 2020-06-25 stsp break;
3688 3143d852 2020-06-25 stsp }
3689 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3690 ff56836b 2021-07-08 stsp break;
3691 b737c85a 2020-06-26 stsp free(parent_path);
3692 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3693 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3694 3143d852 2020-06-25 stsp }
3695 3143d852 2020-06-25 stsp
3696 b737c85a 2020-06-26 stsp free(parent_path);
3697 b737c85a 2020-06-26 stsp free(next_parent_path);
3698 3143d852 2020-06-25 stsp return err;
3699 abb4604f 2019-07-27 stsp }
3700 abb4604f 2019-07-27 stsp
3701 abb4604f 2019-07-27 stsp static const struct got_error *
3702 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3703 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3704 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3705 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3706 f2a9dc41 2019-12-13 tracey int report_unchanged)
3707 f8d1f275 2019-02-04 stsp {
3708 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3709 6fc93f37 2019-12-13 stsp int fd = -1;
3710 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3711 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3712 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3713 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3714 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3715 3143d852 2020-06-25 stsp
3716 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3717 f8d1f275 2019-02-04 stsp
3718 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3719 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3720 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3721 8dc303cc 2019-07-27 stsp
3722 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3723 a84c0d30 2022-03-12 stsp if (ie) {
3724 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3725 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3726 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3727 a84c0d30 2022-03-12 stsp goto done;
3728 a84c0d30 2022-03-12 stsp }
3729 a84c0d30 2022-03-12 stsp
3730 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3731 6fc93f37 2019-12-13 stsp if (fd == -1) {
3732 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3733 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3734 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3735 ff56836b 2021-07-08 stsp else {
3736 ff56836b 2021-07-08 stsp if (!no_ignores) {
3737 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3738 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3739 ff56836b 2021-07-08 stsp if (err)
3740 ff56836b 2021-07-08 stsp goto done;
3741 ff56836b 2021-07-08 stsp }
3742 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3743 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3744 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3745 ff56836b 2021-07-08 stsp }
3746 abb4604f 2019-07-27 stsp } else {
3747 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3748 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3749 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3750 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3751 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3752 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3753 abb4604f 2019-07-27 stsp arg.status_path = path;
3754 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3755 abb4604f 2019-07-27 stsp arg.repo = repo;
3756 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3757 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3758 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3759 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3760 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3761 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3762 022fae89 2019-12-06 tracey if (!no_ignores) {
3763 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3764 3143d852 2020-06-25 stsp worktree->root_path, path);
3765 3143d852 2020-06-25 stsp if (err)
3766 3143d852 2020-06-25 stsp goto done;
3767 022fae89 2019-12-06 tracey }
3768 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3769 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3770 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3771 927df6b7 2019-02-10 stsp }
3772 3143d852 2020-06-25 stsp done:
3773 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3774 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3775 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3776 927df6b7 2019-02-10 stsp free(ondisk_path);
3777 347d1d3e 2019-07-12 stsp return err;
3778 347d1d3e 2019-07-12 stsp }
3779 347d1d3e 2019-07-12 stsp
3780 347d1d3e 2019-07-12 stsp const struct got_error *
3781 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3782 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3783 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3784 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3785 347d1d3e 2019-07-12 stsp {
3786 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3787 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3788 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3789 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3790 347d1d3e 2019-07-12 stsp
3791 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3792 347d1d3e 2019-07-12 stsp if (err)
3793 347d1d3e 2019-07-12 stsp return err;
3794 347d1d3e 2019-07-12 stsp
3795 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3796 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3797 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3798 f6343036 2021-06-22 stsp no_ignores, 0);
3799 72ea6654 2019-07-27 stsp if (err)
3800 72ea6654 2019-07-27 stsp break;
3801 72ea6654 2019-07-27 stsp }
3802 f8d1f275 2019-02-04 stsp free(fileindex_path);
3803 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3804 f8d1f275 2019-02-04 stsp return err;
3805 f8d1f275 2019-02-04 stsp }
3806 6c7ab921 2019-03-18 stsp
3807 6c7ab921 2019-03-18 stsp const struct got_error *
3808 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3809 6c7ab921 2019-03-18 stsp const char *arg)
3810 6c7ab921 2019-03-18 stsp {
3811 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3812 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3813 6c7ab921 2019-03-18 stsp size_t len;
3814 00bb5ea0 2020-07-23 stsp struct stat sb;
3815 01740607 2020-11-04 stsp char *abspath = NULL;
3816 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3817 6c7ab921 2019-03-18 stsp
3818 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3819 6c7ab921 2019-03-18 stsp
3820 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3821 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3822 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3823 00bb5ea0 2020-07-23 stsp
3824 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3825 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3826 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3827 00bb5ea0 2020-07-23 stsp goto done;
3828 00bb5ea0 2020-07-23 stsp }
3829 727173c3 2020-11-06 stsp sb.st_mode = 0;
3830 00bb5ea0 2020-07-23 stsp }
3831 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3832 00bb5ea0 2020-07-23 stsp /*
3833 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3834 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3835 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3836 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3837 00bb5ea0 2020-07-23 stsp */
3838 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3839 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3840 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3841 00bb5ea0 2020-07-23 stsp goto done;
3842 00bb5ea0 2020-07-23 stsp }
3843 00bb5ea0 2020-07-23 stsp
3844 00bb5ea0 2020-07-23 stsp }
3845 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3846 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3847 00bb5ea0 2020-07-23 stsp if (err)
3848 d0710d08 2019-07-22 stsp goto done;
3849 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3850 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3851 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3852 00bb5ea0 2020-07-23 stsp goto done;
3853 00bb5ea0 2020-07-23 stsp }
3854 00bb5ea0 2020-07-23 stsp } else {
3855 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3856 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3857 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3858 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3859 00bb5ea0 2020-07-23 stsp goto done;
3860 00bb5ea0 2020-07-23 stsp }
3861 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3862 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3863 01740607 2020-11-04 stsp goto done;
3864 01740607 2020-11-04 stsp }
3865 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3866 01740607 2020-11-04 stsp sizeof(canonpath));
3867 01740607 2020-11-04 stsp if (err)
3868 00bb5ea0 2020-07-23 stsp goto done;
3869 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3870 01740607 2020-11-04 stsp if (resolved == NULL) {
3871 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3872 01740607 2020-11-04 stsp goto done;
3873 00bb5ea0 2020-07-23 stsp }
3874 d0710d08 2019-07-22 stsp }
3875 d0710d08 2019-07-22 stsp }
3876 6c7ab921 2019-03-18 stsp
3877 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3878 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3879 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3880 6c7ab921 2019-03-18 stsp goto done;
3881 6c7ab921 2019-03-18 stsp }
3882 6c7ab921 2019-03-18 stsp
3883 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3884 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3885 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3886 f6d88e1a 2019-05-29 stsp if (err)
3887 f6d88e1a 2019-05-29 stsp goto done;
3888 f6d88e1a 2019-05-29 stsp } else {
3889 f6d88e1a 2019-05-29 stsp path = strdup("");
3890 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3891 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3892 f6d88e1a 2019-05-29 stsp goto done;
3893 f6d88e1a 2019-05-29 stsp }
3894 6c7ab921 2019-03-18 stsp }
3895 6c7ab921 2019-03-18 stsp
3896 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3897 6c7ab921 2019-03-18 stsp len = strlen(path);
3898 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3899 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3900 6c7ab921 2019-03-18 stsp len--;
3901 6c7ab921 2019-03-18 stsp }
3902 6c7ab921 2019-03-18 stsp done:
3903 01740607 2020-11-04 stsp free(abspath);
3904 6c7ab921 2019-03-18 stsp free(resolved);
3905 d0710d08 2019-07-22 stsp free(cwd);
3906 6c7ab921 2019-03-18 stsp if (err == NULL)
3907 6c7ab921 2019-03-18 stsp *wt_path = path;
3908 6c7ab921 2019-03-18 stsp else
3909 6c7ab921 2019-03-18 stsp free(path);
3910 d00136be 2019-03-26 stsp return err;
3911 d00136be 2019-03-26 stsp }
3912 d00136be 2019-03-26 stsp
3913 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3914 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3915 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3916 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3917 4e68cba3 2019-11-23 stsp void *progress_arg;
3918 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3919 4e68cba3 2019-11-23 stsp };
3920 4e68cba3 2019-11-23 stsp
3921 4e68cba3 2019-11-23 stsp static const struct got_error *
3922 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3923 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3924 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3925 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3926 07f5b47a 2019-06-02 stsp {
3927 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3928 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3929 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3930 6d022e97 2019-08-04 stsp struct stat sb;
3931 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3932 07f5b47a 2019-06-02 stsp
3933 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3934 dbb83fbd 2019-12-12 stsp relpath) == -1)
3935 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3936 dbb83fbd 2019-12-12 stsp
3937 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3938 6d022e97 2019-08-04 stsp if (ie) {
3939 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3940 12463d8b 2019-12-13 stsp de_name, a->repo);
3941 6d022e97 2019-08-04 stsp if (err)
3942 dbb83fbd 2019-12-12 stsp goto done;
3943 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3944 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3945 dbb83fbd 2019-12-12 stsp goto done;
3946 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3947 dbb83fbd 2019-12-12 stsp if (err)
3948 dbb83fbd 2019-12-12 stsp goto done;
3949 6d022e97 2019-08-04 stsp }
3950 07f5b47a 2019-06-02 stsp
3951 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3952 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3953 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3954 9b4603c0 2022-01-31 stsp else
3955 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3956 dbb83fbd 2019-12-12 stsp goto done;
3957 4e68cba3 2019-11-23 stsp }
3958 07f5b47a 2019-06-02 stsp
3959 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3960 4e68cba3 2019-11-23 stsp if (err)
3961 4e68cba3 2019-11-23 stsp goto done;
3962 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3963 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3964 3969253a 2020-03-07 stsp if (err) {
3965 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3966 3969253a 2020-03-07 stsp goto done;
3967 3969253a 2020-03-07 stsp }
3968 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3969 07f5b47a 2019-06-02 stsp if (err) {
3970 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3971 4e68cba3 2019-11-23 stsp goto done;
3972 07f5b47a 2019-06-02 stsp }
3973 4e68cba3 2019-11-23 stsp done:
3974 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3975 dbb83fbd 2019-12-12 stsp if (err)
3976 dbb83fbd 2019-12-12 stsp return err;
3977 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3978 dbb83fbd 2019-12-12 stsp return NULL;
3979 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3980 07f5b47a 2019-06-02 stsp }
3981 07f5b47a 2019-06-02 stsp
3982 d00136be 2019-03-26 stsp const struct got_error *
3983 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3984 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3985 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3986 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3987 d00136be 2019-03-26 stsp {
3988 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3989 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3990 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3991 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3992 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3993 d00136be 2019-03-26 stsp
3994 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3995 d00136be 2019-03-26 stsp if (err)
3996 d00136be 2019-03-26 stsp return err;
3997 d00136be 2019-03-26 stsp
3998 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3999 d00136be 2019-03-26 stsp if (err)
4000 d00136be 2019-03-26 stsp goto done;
4001 d00136be 2019-03-26 stsp
4002 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4003 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4004 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4005 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4006 4e68cba3 2019-11-23 stsp saa.repo = repo;
4007 4e68cba3 2019-11-23 stsp
4008 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4009 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4010 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4011 1dd54920 2019-05-11 stsp if (err)
4012 af12c6b9 2019-06-04 stsp break;
4013 1dd54920 2019-05-11 stsp }
4014 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4015 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4016 af12c6b9 2019-06-04 stsp err = sync_err;
4017 d00136be 2019-03-26 stsp done:
4018 fb399478 2019-07-12 stsp free(fileindex_path);
4019 d00136be 2019-03-26 stsp if (fileindex)
4020 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4021 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4022 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4023 d00136be 2019-03-26 stsp err = unlockerr;
4024 6c7ab921 2019-03-18 stsp return err;
4025 6c7ab921 2019-03-18 stsp }
4026 17ed4618 2019-06-02 stsp
4027 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4028 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4029 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4030 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4031 f2a9dc41 2019-12-13 tracey void *progress_arg;
4032 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4033 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4034 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4035 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4036 766841c2 2020-08-13 stsp const char *status_codes;
4037 f2a9dc41 2019-12-13 tracey };
4038 f2a9dc41 2019-12-13 tracey
4039 f2a9dc41 2019-12-13 tracey static const struct got_error *
4040 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4041 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4042 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4043 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4044 17ed4618 2019-06-02 stsp {
4045 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4046 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4047 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4048 17ed4618 2019-06-02 stsp struct stat sb;
4049 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4050 4e12cd97 2022-01-25 stsp
4051 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4052 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4053 4e12cd97 2022-01-25 stsp return NULL;
4054 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4055 4e12cd97 2022-01-25 stsp }
4056 17ed4618 2019-06-02 stsp
4057 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4058 17ed4618 2019-06-02 stsp if (ie == NULL)
4059 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4060 2ec1f75b 2019-03-26 stsp
4061 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4062 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4063 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4064 9acbc4fa 2019-08-03 stsp return NULL;
4065 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4066 9acbc4fa 2019-08-03 stsp }
4067 9acbc4fa 2019-08-03 stsp
4068 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4069 f2a9dc41 2019-12-13 tracey relpath) == -1)
4070 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4071 f2a9dc41 2019-12-13 tracey
4072 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4073 12463d8b 2019-12-13 stsp a->repo);
4074 17ed4618 2019-06-02 stsp if (err)
4075 f2a9dc41 2019-12-13 tracey goto done;
4076 17ed4618 2019-06-02 stsp
4077 766841c2 2020-08-13 stsp if (a->status_codes) {
4078 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4079 766841c2 2020-08-13 stsp int i;
4080 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4081 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4082 766841c2 2020-08-13 stsp break;
4083 766841c2 2020-08-13 stsp }
4084 766841c2 2020-08-13 stsp if (i == ncodes) {
4085 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4086 766841c2 2020-08-13 stsp free(ondisk_path);
4087 766841c2 2020-08-13 stsp return NULL;
4088 766841c2 2020-08-13 stsp }
4089 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4090 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4091 766841c2 2020-08-13 stsp static char msg[64];
4092 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4093 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4094 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4095 766841c2 2020-08-13 stsp goto done;
4096 766841c2 2020-08-13 stsp }
4097 766841c2 2020-08-13 stsp }
4098 766841c2 2020-08-13 stsp
4099 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4100 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4101 f2a9dc41 2019-12-13 tracey goto done;
4102 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4103 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4104 f2a9dc41 2019-12-13 tracey goto done;
4105 f2a9dc41 2019-12-13 tracey }
4106 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4107 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4108 4e12cd97 2022-01-25 stsp goto done;
4109 4e12cd97 2022-01-25 stsp }
4110 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4111 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4112 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4113 f2a9dc41 2019-12-13 tracey goto done;
4114 f2a9dc41 2019-12-13 tracey }
4115 17ed4618 2019-06-02 stsp }
4116 17ed4618 2019-06-02 stsp
4117 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4118 20a2ad1c 2020-10-20 stsp size_t root_len;
4119 20a2ad1c 2020-10-20 stsp
4120 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4121 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4122 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4123 12463d8b 2019-12-13 stsp ondisk_path);
4124 12463d8b 2019-12-13 stsp goto done;
4125 12463d8b 2019-12-13 stsp }
4126 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4127 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4128 12463d8b 2019-12-13 stsp goto done;
4129 15341bfd 2020-03-05 tracey }
4130 15341bfd 2020-03-05 tracey
4131 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4132 20a2ad1c 2020-10-20 stsp do {
4133 20a2ad1c 2020-10-20 stsp char *parent;
4134 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4135 20a2ad1c 2020-10-20 stsp if (err)
4136 2513f20a 2020-10-20 stsp goto done;
4137 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4138 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4139 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4140 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4141 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4142 20a2ad1c 2020-10-20 stsp ondisk_path);
4143 15341bfd 2020-03-05 tracey break;
4144 15341bfd 2020-03-05 tracey }
4145 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4146 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4147 f2a9dc41 2019-12-13 tracey }
4148 17ed4618 2019-06-02 stsp
4149 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4150 f2a9dc41 2019-12-13 tracey done:
4151 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4152 f2a9dc41 2019-12-13 tracey if (err)
4153 f2a9dc41 2019-12-13 tracey return err;
4154 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4155 f2a9dc41 2019-12-13 tracey return NULL;
4156 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4157 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4158 17ed4618 2019-06-02 stsp }
4159 17ed4618 2019-06-02 stsp
4160 2ec1f75b 2019-03-26 stsp const struct got_error *
4161 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4162 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4163 766841c2 2020-08-13 stsp const char *status_codes,
4164 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4165 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4166 2ec1f75b 2019-03-26 stsp {
4167 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4168 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4169 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4170 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4171 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4172 2ec1f75b 2019-03-26 stsp
4173 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4174 2ec1f75b 2019-03-26 stsp if (err)
4175 2ec1f75b 2019-03-26 stsp return err;
4176 2ec1f75b 2019-03-26 stsp
4177 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4178 2ec1f75b 2019-03-26 stsp if (err)
4179 2ec1f75b 2019-03-26 stsp goto done;
4180 f2a9dc41 2019-12-13 tracey
4181 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4182 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4183 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4184 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4185 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4186 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4187 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4188 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4189 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4190 2ec1f75b 2019-03-26 stsp
4191 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4192 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4193 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4194 17ed4618 2019-06-02 stsp if (err)
4195 af12c6b9 2019-06-04 stsp break;
4196 2ec1f75b 2019-03-26 stsp }
4197 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4198 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4199 af12c6b9 2019-06-04 stsp err = sync_err;
4200 2ec1f75b 2019-03-26 stsp done:
4201 fb399478 2019-07-12 stsp free(fileindex_path);
4202 2ec1f75b 2019-03-26 stsp if (fileindex)
4203 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4204 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4205 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4206 2ec1f75b 2019-03-26 stsp err = unlockerr;
4207 33aa809d 2019-08-08 stsp return err;
4208 33aa809d 2019-08-08 stsp }
4209 33aa809d 2019-08-08 stsp
4210 33aa809d 2019-08-08 stsp static const struct got_error *
4211 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4212 33aa809d 2019-08-08 stsp {
4213 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4214 33aa809d 2019-08-08 stsp char *line = NULL;
4215 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4216 33aa809d 2019-08-08 stsp ssize_t linelen;
4217 33aa809d 2019-08-08 stsp
4218 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4219 33aa809d 2019-08-08 stsp if (linelen == -1) {
4220 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4221 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4222 33aa809d 2019-08-08 stsp goto done;
4223 33aa809d 2019-08-08 stsp }
4224 33aa809d 2019-08-08 stsp return NULL;
4225 33aa809d 2019-08-08 stsp }
4226 33aa809d 2019-08-08 stsp if (outfile) {
4227 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4228 33aa809d 2019-08-08 stsp if (n != linelen) {
4229 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4230 33aa809d 2019-08-08 stsp goto done;
4231 33aa809d 2019-08-08 stsp }
4232 33aa809d 2019-08-08 stsp }
4233 33aa809d 2019-08-08 stsp if (rejectfile) {
4234 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4235 33aa809d 2019-08-08 stsp if (n != linelen)
4236 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4237 33aa809d 2019-08-08 stsp }
4238 33aa809d 2019-08-08 stsp done:
4239 33aa809d 2019-08-08 stsp free(line);
4240 2ec1f75b 2019-03-26 stsp return err;
4241 2ec1f75b 2019-03-26 stsp }
4242 1f1abb7e 2019-08-08 stsp
4243 33aa809d 2019-08-08 stsp static const struct got_error *
4244 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4245 33aa809d 2019-08-08 stsp {
4246 33aa809d 2019-08-08 stsp char *line = NULL;
4247 33aa809d 2019-08-08 stsp size_t linesize = 0;
4248 33aa809d 2019-08-08 stsp ssize_t linelen;
4249 33aa809d 2019-08-08 stsp
4250 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4251 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4252 500467ff 2019-09-25 hiltjo if (ferror(f))
4253 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4254 500467ff 2019-09-25 hiltjo return NULL;
4255 500467ff 2019-09-25 hiltjo }
4256 33aa809d 2019-08-08 stsp free(line);
4257 33aa809d 2019-08-08 stsp return NULL;
4258 33aa809d 2019-08-08 stsp }
4259 33aa809d 2019-08-08 stsp
4260 33aa809d 2019-08-08 stsp static const struct got_error *
4261 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4262 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4263 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4264 abc59930 2021-09-05 naddy {
4265 33aa809d 2019-08-08 stsp const struct got_error *err;
4266 33aa809d 2019-08-08 stsp
4267 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4268 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4269 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4270 33aa809d 2019-08-08 stsp if (err)
4271 33aa809d 2019-08-08 stsp return err;
4272 33aa809d 2019-08-08 stsp (*line_cur1)++;
4273 33aa809d 2019-08-08 stsp }
4274 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4275 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4276 33aa809d 2019-08-08 stsp if (rejectfile)
4277 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4278 33aa809d 2019-08-08 stsp else
4279 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4280 33aa809d 2019-08-08 stsp if (err)
4281 33aa809d 2019-08-08 stsp return err;
4282 33aa809d 2019-08-08 stsp (*line_cur2)++;
4283 33aa809d 2019-08-08 stsp }
4284 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4285 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4286 33aa809d 2019-08-08 stsp err = copy_one_line(f2, 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_cur2)++;
4290 33aa809d 2019-08-08 stsp }
4291 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4292 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4293 33aa809d 2019-08-08 stsp if (rejectfile)
4294 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4295 33aa809d 2019-08-08 stsp else
4296 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4297 33aa809d 2019-08-08 stsp if (err)
4298 33aa809d 2019-08-08 stsp return err;
4299 33aa809d 2019-08-08 stsp (*line_cur1)++;
4300 33aa809d 2019-08-08 stsp }
4301 f1e81a05 2019-08-10 stsp
4302 f1e81a05 2019-08-10 stsp return NULL;
4303 f1e81a05 2019-08-10 stsp }
4304 f1e81a05 2019-08-10 stsp
4305 f1e81a05 2019-08-10 stsp static const struct got_error *
4306 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4307 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4308 f1e81a05 2019-08-10 stsp {
4309 f1e81a05 2019-08-10 stsp const struct got_error *err;
4310 f1e81a05 2019-08-10 stsp
4311 f1e81a05 2019-08-10 stsp if (outfile) {
4312 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4313 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4314 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4315 f1e81a05 2019-08-10 stsp if (err)
4316 f1e81a05 2019-08-10 stsp return err;
4317 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4318 f1e81a05 2019-08-10 stsp }
4319 f1e81a05 2019-08-10 stsp }
4320 f1e81a05 2019-08-10 stsp if (rejectfile) {
4321 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4322 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4323 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4324 f1e81a05 2019-08-10 stsp if (err)
4325 f1e81a05 2019-08-10 stsp return err;
4326 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4327 f1e81a05 2019-08-10 stsp }
4328 33aa809d 2019-08-08 stsp }
4329 33aa809d 2019-08-08 stsp
4330 33aa809d 2019-08-08 stsp return NULL;
4331 33aa809d 2019-08-08 stsp }
4332 33aa809d 2019-08-08 stsp
4333 33aa809d 2019-08-08 stsp static const struct got_error *
4334 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4335 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4336 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4338 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4339 33aa809d 2019-08-08 stsp {
4340 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4341 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4342 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4343 33aa809d 2019-08-08 stsp FILE *hunkfile;
4344 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4345 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4346 fe621944 2020-11-10 stsp int rc;
4347 33aa809d 2019-08-08 stsp
4348 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4349 33aa809d 2019-08-08 stsp
4350 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4351 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4352 fe621944 2020-11-10 stsp start_old = cc.left.start;
4353 fe621944 2020-11-10 stsp end_old = cc.left.end;
4354 fe621944 2020-11-10 stsp start_new = cc.right.start;
4355 fe621944 2020-11-10 stsp end_new = cc.right.end;
4356 33aa809d 2019-08-08 stsp
4357 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4358 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4359 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4360 33aa809d 2019-08-08 stsp
4361 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4362 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4363 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4364 33aa809d 2019-08-08 stsp
4365 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4366 fe621944 2020-11-10 stsp if (diff_state == NULL)
4367 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4368 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4369 fe621944 2020-11-10 stsp
4370 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4371 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4372 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4373 33aa809d 2019-08-08 stsp goto done;
4374 33aa809d 2019-08-08 stsp }
4375 fe621944 2020-11-10 stsp
4376 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4377 fe621944 2020-11-10 stsp diff_result, &cc);
4378 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4379 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4380 33aa809d 2019-08-08 stsp goto done;
4381 33aa809d 2019-08-08 stsp }
4382 fe621944 2020-11-10 stsp
4383 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4384 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4385 33aa809d 2019-08-08 stsp goto done;
4386 33aa809d 2019-08-08 stsp }
4387 33aa809d 2019-08-08 stsp
4388 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4389 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4390 33aa809d 2019-08-08 stsp if (err)
4391 33aa809d 2019-08-08 stsp goto done;
4392 33aa809d 2019-08-08 stsp
4393 33aa809d 2019-08-08 stsp switch (*choice) {
4394 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4395 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4396 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4397 33aa809d 2019-08-08 stsp break;
4398 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4399 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4400 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4401 33aa809d 2019-08-08 stsp break;
4402 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4403 33aa809d 2019-08-08 stsp break;
4404 33aa809d 2019-08-08 stsp default:
4405 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4406 33aa809d 2019-08-08 stsp break;
4407 33aa809d 2019-08-08 stsp }
4408 33aa809d 2019-08-08 stsp done:
4409 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4410 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4411 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4412 33aa809d 2019-08-08 stsp return err;
4413 33aa809d 2019-08-08 stsp }
4414 33aa809d 2019-08-08 stsp
4415 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4416 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4417 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4418 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4419 1f1abb7e 2019-08-08 stsp void *progress_arg;
4420 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4421 33aa809d 2019-08-08 stsp void *patch_arg;
4422 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4423 f259c4c1 2021-09-24 stsp int unlink_added_files;
4424 1f1abb7e 2019-08-08 stsp };
4425 a129376b 2019-03-28 stsp
4426 e20a8b6f 2019-06-04 stsp static const struct got_error *
4427 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4428 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4429 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4430 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4431 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4432 33aa809d 2019-08-08 stsp {
4433 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4434 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4435 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4436 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4437 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4438 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4439 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4440 fe621944 2020-11-10 stsp struct stat sb2;
4441 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4442 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4443 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4444 33aa809d 2019-08-08 stsp
4445 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4446 33aa809d 2019-08-08 stsp
4447 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4448 33aa809d 2019-08-08 stsp if (err)
4449 33aa809d 2019-08-08 stsp return err;
4450 33aa809d 2019-08-08 stsp
4451 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4452 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4453 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4454 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4455 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4456 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4457 fa3cef63 2020-07-23 stsp goto done;
4458 fa3cef63 2020-07-23 stsp }
4459 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4460 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4461 c0df5966 2021-12-31 stsp if (link_len == -1) {
4462 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4463 c0df5966 2021-12-31 stsp path2);
4464 c0df5966 2021-12-31 stsp }
4465 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4466 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4467 12463d8b 2019-12-13 stsp }
4468 12463d8b 2019-12-13 stsp } else {
4469 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4470 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4471 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4472 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4473 fa3cef63 2020-07-23 stsp goto done;
4474 fa3cef63 2020-07-23 stsp }
4475 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4476 fa3cef63 2020-07-23 stsp sizeof(link_target));
4477 fa3cef63 2020-07-23 stsp if (link_len == -1)
4478 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4479 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4480 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4481 12463d8b 2019-12-13 stsp }
4482 1ebedb77 2019-10-19 stsp }
4483 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4484 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4485 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4486 fa3cef63 2020-07-23 stsp goto done;
4487 fa3cef63 2020-07-23 stsp }
4488 1ebedb77 2019-10-19 stsp
4489 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4490 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4491 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4492 fa3cef63 2020-07-23 stsp goto done;
4493 fa3cef63 2020-07-23 stsp }
4494 fa3cef63 2020-07-23 stsp fd2 = -1;
4495 fa3cef63 2020-07-23 stsp } else {
4496 fa3cef63 2020-07-23 stsp size_t n;
4497 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4498 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4499 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4500 fa3cef63 2020-07-23 stsp goto done;
4501 fa3cef63 2020-07-23 stsp }
4502 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4503 fa3cef63 2020-07-23 stsp if (n != link_len) {
4504 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4505 fa3cef63 2020-07-23 stsp goto done;
4506 fa3cef63 2020-07-23 stsp }
4507 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4508 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4509 fa3cef63 2020-07-23 stsp goto done;
4510 fa3cef63 2020-07-23 stsp }
4511 fa3cef63 2020-07-23 stsp rewind(f2);
4512 33aa809d 2019-08-08 stsp }
4513 33aa809d 2019-08-08 stsp
4514 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4515 eb81bc23 2022-06-28 tracey if (fd == -1) {
4516 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4517 eb81bc23 2022-06-28 tracey goto done;
4518 eb81bc23 2022-06-28 tracey }
4519 eb81bc23 2022-06-28 tracey
4520 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4521 33aa809d 2019-08-08 stsp if (err)
4522 33aa809d 2019-08-08 stsp goto done;
4523 33aa809d 2019-08-08 stsp
4524 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4525 33aa809d 2019-08-08 stsp if (err)
4526 33aa809d 2019-08-08 stsp goto done;
4527 33aa809d 2019-08-08 stsp
4528 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4529 33aa809d 2019-08-08 stsp if (err)
4530 33aa809d 2019-08-08 stsp goto done;
4531 33aa809d 2019-08-08 stsp
4532 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4533 fe621944 2020-11-10 stsp NULL);
4534 33aa809d 2019-08-08 stsp if (err)
4535 33aa809d 2019-08-08 stsp goto done;
4536 33aa809d 2019-08-08 stsp
4537 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4538 33aa809d 2019-08-08 stsp if (err)
4539 33aa809d 2019-08-08 stsp goto done;
4540 33aa809d 2019-08-08 stsp
4541 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4542 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4543 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4544 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4545 fe621944 2020-11-10 stsp
4546 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4547 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4548 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4549 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4550 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4551 fe621944 2020-11-10 stsp nchanges++;
4552 fe621944 2020-11-10 stsp }
4553 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4554 33aa809d 2019-08-08 stsp int choice;
4555 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4556 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4557 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4558 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4559 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4560 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4561 33aa809d 2019-08-08 stsp if (err)
4562 33aa809d 2019-08-08 stsp goto done;
4563 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4564 33aa809d 2019-08-08 stsp have_content = 1;
4565 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4566 33aa809d 2019-08-08 stsp break;
4567 33aa809d 2019-08-08 stsp }
4568 1ebedb77 2019-10-19 stsp if (have_content) {
4569 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4570 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4571 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4572 1ebedb77 2019-10-19 stsp if (err)
4573 1ebedb77 2019-10-19 stsp goto done;
4574 1ebedb77 2019-10-19 stsp
4575 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4576 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4577 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4578 fa3cef63 2020-07-23 stsp goto done;
4579 fa3cef63 2020-07-23 stsp }
4580 1ebedb77 2019-10-19 stsp }
4581 1ebedb77 2019-10-19 stsp }
4582 33aa809d 2019-08-08 stsp done:
4583 33aa809d 2019-08-08 stsp free(id_str);
4584 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4585 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4586 33aa809d 2019-08-08 stsp if (blob)
4587 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4588 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4589 fe621944 2020-11-10 stsp if (err == NULL)
4590 fe621944 2020-11-10 stsp err = free_err;
4591 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4592 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4593 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4594 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4595 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4596 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4597 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4598 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4599 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4600 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4601 33aa809d 2019-08-08 stsp if (err || !have_content) {
4602 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4603 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4604 33aa809d 2019-08-08 stsp free(*path_outfile);
4605 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4606 33aa809d 2019-08-08 stsp }
4607 33aa809d 2019-08-08 stsp free(path1);
4608 33aa809d 2019-08-08 stsp return err;
4609 33aa809d 2019-08-08 stsp }
4610 33aa809d 2019-08-08 stsp
4611 33aa809d 2019-08-08 stsp static const struct got_error *
4612 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4613 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4614 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4615 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4616 a129376b 2019-03-28 stsp {
4617 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4618 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4619 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4620 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4621 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4622 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4623 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4624 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4625 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4626 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4627 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4628 eb81bc23 2022-06-28 tracey int fd = -1;
4629 a129376b 2019-03-28 stsp
4630 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4631 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4632 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4633 d3bcc3d1 2019-08-08 stsp return NULL;
4634 3d69ad8d 2019-08-17 semarie
4635 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4636 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4637 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4638 d3bcc3d1 2019-08-08 stsp
4639 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4640 65084dad 2019-08-08 stsp if (ie == NULL)
4641 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4642 a129376b 2019-03-28 stsp
4643 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4644 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4645 a129376b 2019-03-28 stsp if (err) {
4646 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4647 a129376b 2019-03-28 stsp goto done;
4648 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4649 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4650 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4651 e20a8b6f 2019-06-04 stsp goto done;
4652 e20a8b6f 2019-06-04 stsp }
4653 a129376b 2019-03-28 stsp }
4654 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4655 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4656 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4657 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4658 a129376b 2019-03-28 stsp goto done;
4659 a129376b 2019-03-28 stsp }
4660 a129376b 2019-03-28 stsp } else {
4661 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4662 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4663 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4664 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4665 a129376b 2019-03-28 stsp goto done;
4666 a129376b 2019-03-28 stsp }
4667 a129376b 2019-03-28 stsp } else {
4668 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4669 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4670 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4671 a129376b 2019-03-28 stsp goto done;
4672 a129376b 2019-03-28 stsp }
4673 a129376b 2019-03-28 stsp }
4674 a129376b 2019-03-28 stsp }
4675 a129376b 2019-03-28 stsp
4676 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4677 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4678 a44927cc 2022-04-07 stsp if (err)
4679 a44927cc 2022-04-07 stsp goto done;
4680 a44927cc 2022-04-07 stsp
4681 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4682 a9fa2909 2019-07-27 stsp if (err) {
4683 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4684 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4685 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4686 a9fa2909 2019-07-27 stsp goto done;
4687 a9fa2909 2019-07-27 stsp } else {
4688 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4689 a9fa2909 2019-07-27 stsp if (err)
4690 a9fa2909 2019-07-27 stsp goto done;
4691 a9fa2909 2019-07-27 stsp
4692 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4693 1233e6b6 2020-10-19 stsp if (err)
4694 a9fa2909 2019-07-27 stsp goto done;
4695 a9fa2909 2019-07-27 stsp
4696 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4697 1233e6b6 2020-10-19 stsp free(te_name);
4698 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4699 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4700 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4701 a9fa2909 2019-07-27 stsp goto done;
4702 a9fa2909 2019-07-27 stsp }
4703 a129376b 2019-03-28 stsp }
4704 a129376b 2019-03-28 stsp
4705 a129376b 2019-03-28 stsp switch (status) {
4706 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4707 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4708 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4709 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4710 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4711 33aa809d 2019-08-08 stsp if (err)
4712 33aa809d 2019-08-08 stsp goto done;
4713 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4714 33aa809d 2019-08-08 stsp break;
4715 33aa809d 2019-08-08 stsp }
4716 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4717 1f1abb7e 2019-08-08 stsp ie->path);
4718 1ee397ad 2019-07-12 stsp if (err)
4719 1ee397ad 2019-07-12 stsp goto done;
4720 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4721 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4722 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4723 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4724 f259c4c1 2021-09-24 stsp relpath) == -1) {
4725 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4726 f259c4c1 2021-09-24 stsp goto done;
4727 f259c4c1 2021-09-24 stsp }
4728 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4729 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4730 f259c4c1 2021-09-24 stsp ondisk_path);
4731 f259c4c1 2021-09-24 stsp break;
4732 f259c4c1 2021-09-24 stsp }
4733 f259c4c1 2021-09-24 stsp }
4734 a129376b 2019-03-28 stsp break;
4735 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4736 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4737 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4738 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4739 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4740 33aa809d 2019-08-08 stsp if (err)
4741 33aa809d 2019-08-08 stsp goto done;
4742 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4743 33aa809d 2019-08-08 stsp break;
4744 33aa809d 2019-08-08 stsp }
4745 33aa809d 2019-08-08 stsp /* fall through */
4746 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4747 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4748 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4749 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4750 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4751 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4752 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4753 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4754 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4755 24278f30 2019-08-03 stsp } else
4756 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4757 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4758 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4759 eb81bc23 2022-06-28 tracey if (fd == -1) {
4760 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4761 eb81bc23 2022-06-28 tracey goto done;
4762 eb81bc23 2022-06-28 tracey }
4763 eb81bc23 2022-06-28 tracey
4764 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4765 a129376b 2019-03-28 stsp if (err)
4766 65084dad 2019-08-08 stsp goto done;
4767 65084dad 2019-08-08 stsp
4768 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4769 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4770 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4771 a129376b 2019-03-28 stsp goto done;
4772 65084dad 2019-08-08 stsp }
4773 65084dad 2019-08-08 stsp
4774 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4775 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4776 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4777 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4778 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4779 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4780 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4781 33aa809d 2019-08-08 stsp break;
4782 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4783 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4784 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4785 369fd7e5 2020-07-23 stsp path_content);
4786 369fd7e5 2020-07-23 stsp break;
4787 369fd7e5 2020-07-23 stsp }
4788 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4789 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4790 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4791 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4792 369fd7e5 2020-07-23 stsp } else {
4793 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4794 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4795 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4796 369fd7e5 2020-07-23 stsp goto done;
4797 369fd7e5 2020-07-23 stsp }
4798 33aa809d 2019-08-08 stsp }
4799 33aa809d 2019-08-08 stsp } else {
4800 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4801 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4802 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4803 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4804 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4805 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4806 2e1fa222 2020-07-23 stsp } else {
4807 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4808 2e1fa222 2020-07-23 stsp ie->path,
4809 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4810 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4811 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4812 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4813 2e1fa222 2020-07-23 stsp }
4814 a129376b 2019-03-28 stsp if (err)
4815 a129376b 2019-03-28 stsp goto done;
4816 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4817 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4818 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4819 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4820 437adc9d 2020-12-10 yzhong blob->id.sha1,
4821 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4822 2e1fa222 2020-07-23 stsp if (err)
4823 2e1fa222 2020-07-23 stsp goto done;
4824 2e1fa222 2020-07-23 stsp }
4825 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4826 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4827 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4828 33aa809d 2019-08-08 stsp }
4829 a129376b 2019-03-28 stsp }
4830 a129376b 2019-03-28 stsp break;
4831 e20a8b6f 2019-06-04 stsp }
4832 a129376b 2019-03-28 stsp default:
4833 1f1abb7e 2019-08-08 stsp break;
4834 a129376b 2019-03-28 stsp }
4835 a129376b 2019-03-28 stsp done:
4836 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4837 33aa809d 2019-08-08 stsp free(path_content);
4838 e20a8b6f 2019-06-04 stsp free(parent_path);
4839 a129376b 2019-03-28 stsp free(tree_path);
4840 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4841 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4842 a129376b 2019-03-28 stsp if (blob)
4843 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4844 a129376b 2019-03-28 stsp if (tree)
4845 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4846 a129376b 2019-03-28 stsp free(tree_id);
4847 a44927cc 2022-04-07 stsp if (base_commit)
4848 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4849 e20a8b6f 2019-06-04 stsp return err;
4850 e20a8b6f 2019-06-04 stsp }
4851 e20a8b6f 2019-06-04 stsp
4852 e20a8b6f 2019-06-04 stsp const struct got_error *
4853 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4854 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4855 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4856 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4857 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4858 e20a8b6f 2019-06-04 stsp {
4859 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4860 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4861 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4862 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4863 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4864 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4865 e20a8b6f 2019-06-04 stsp
4866 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4867 e20a8b6f 2019-06-04 stsp if (err)
4868 e20a8b6f 2019-06-04 stsp return err;
4869 e20a8b6f 2019-06-04 stsp
4870 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4871 e20a8b6f 2019-06-04 stsp if (err)
4872 e20a8b6f 2019-06-04 stsp goto done;
4873 e20a8b6f 2019-06-04 stsp
4874 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4875 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4876 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4877 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4878 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4879 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4880 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4881 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4882 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4883 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4884 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4885 e20a8b6f 2019-06-04 stsp if (err)
4886 af12c6b9 2019-06-04 stsp break;
4887 e20a8b6f 2019-06-04 stsp }
4888 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4889 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4890 e20a8b6f 2019-06-04 stsp err = sync_err;
4891 af12c6b9 2019-06-04 stsp done:
4892 fb399478 2019-07-12 stsp free(fileindex_path);
4893 a129376b 2019-03-28 stsp if (fileindex)
4894 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4895 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4896 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4897 a129376b 2019-03-28 stsp err = unlockerr;
4898 c4296144 2019-05-09 stsp return err;
4899 c4296144 2019-05-09 stsp }
4900 c4296144 2019-05-09 stsp
4901 cf066bf8 2019-05-09 stsp static void
4902 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4903 cf066bf8 2019-05-09 stsp {
4904 24519714 2019-05-09 stsp free(ct->path);
4905 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4906 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4907 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4908 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4909 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4910 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4911 cf066bf8 2019-05-09 stsp free(ct);
4912 cf066bf8 2019-05-09 stsp }
4913 24519714 2019-05-09 stsp
4914 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4915 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4916 24519714 2019-05-09 stsp struct got_repository *repo;
4917 24519714 2019-05-09 stsp struct got_worktree *worktree;
4918 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4919 5f8a88c6 2019-08-03 stsp int have_staged_files;
4920 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4921 24519714 2019-05-09 stsp };
4922 cf066bf8 2019-05-09 stsp
4923 c4296144 2019-05-09 stsp static const struct got_error *
4924 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4925 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4926 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4927 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4928 c4296144 2019-05-09 stsp {
4929 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4930 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4931 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4932 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4933 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4934 768aea60 2019-05-09 stsp struct stat sb;
4935 c4296144 2019-05-09 stsp
4936 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4937 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4938 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4939 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4940 5f8a88c6 2019-08-03 stsp return NULL;
4941 5f8a88c6 2019-08-03 stsp } else {
4942 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4943 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4944 c4296144 2019-05-09 stsp
4945 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4946 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4947 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4948 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4949 5f8a88c6 2019-08-03 stsp return NULL;
4950 5f8a88c6 2019-08-03 stsp }
4951 0b5cc0d6 2019-05-09 stsp
4952 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4953 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4954 036813ee 2019-05-09 stsp goto done;
4955 036813ee 2019-05-09 stsp }
4956 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4957 036813ee 2019-05-09 stsp parent_path = strdup("");
4958 69960a46 2019-05-09 stsp if (parent_path == NULL)
4959 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4960 69960a46 2019-05-09 stsp } else {
4961 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4962 69960a46 2019-05-09 stsp if (err)
4963 69960a46 2019-05-09 stsp return err;
4964 69960a46 2019-05-09 stsp }
4965 c4296144 2019-05-09 stsp
4966 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4967 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4968 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4969 c4296144 2019-05-09 stsp goto done;
4970 768aea60 2019-05-09 stsp }
4971 768aea60 2019-05-09 stsp
4972 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4973 768aea60 2019-05-09 stsp relpath) == -1) {
4974 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4975 768aea60 2019-05-09 stsp goto done;
4976 768aea60 2019-05-09 stsp }
4977 0aeb8099 2020-07-23 stsp
4978 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4979 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4980 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4981 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4982 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4983 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4984 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4985 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4986 0aeb8099 2020-07-23 stsp break;
4987 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4988 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4989 0aeb8099 2020-07-23 stsp break;
4990 0aeb8099 2020-07-23 stsp default:
4991 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4992 0aeb8099 2020-07-23 stsp goto done;
4993 0aeb8099 2020-07-23 stsp }
4994 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4995 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4996 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4997 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4998 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4999 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5000 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5001 12463d8b 2019-12-13 stsp ct->ondisk_path);
5002 12463d8b 2019-12-13 stsp goto done;
5003 12463d8b 2019-12-13 stsp }
5004 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5005 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5006 768aea60 2019-05-09 stsp goto done;
5007 768aea60 2019-05-09 stsp }
5008 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5009 c4296144 2019-05-09 stsp }
5010 c4296144 2019-05-09 stsp
5011 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5012 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5013 44d03001 2019-05-09 stsp relpath) == -1) {
5014 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5015 44d03001 2019-05-09 stsp goto done;
5016 44d03001 2019-05-09 stsp }
5017 44d03001 2019-05-09 stsp
5018 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5019 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5020 35213c7c 2020-07-23 stsp int is_bad_symlink;
5021 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5022 35213c7c 2020-07-23 stsp ssize_t target_len;
5023 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5024 35213c7c 2020-07-23 stsp sizeof(target_path));
5025 35213c7c 2020-07-23 stsp if (target_len == -1) {
5026 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5027 35213c7c 2020-07-23 stsp ct->ondisk_path);
5028 35213c7c 2020-07-23 stsp goto done;
5029 35213c7c 2020-07-23 stsp }
5030 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5031 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5032 35213c7c 2020-07-23 stsp if (err)
5033 35213c7c 2020-07-23 stsp goto done;
5034 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5035 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5036 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5037 35213c7c 2020-07-23 stsp goto done;
5038 35213c7c 2020-07-23 stsp }
5039 35213c7c 2020-07-23 stsp }
5040 35213c7c 2020-07-23 stsp
5041 35213c7c 2020-07-23 stsp
5042 cf066bf8 2019-05-09 stsp ct->status = status;
5043 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5044 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5045 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5046 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5047 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5048 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5049 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5050 b416585c 2019-05-13 stsp goto done;
5051 b416585c 2019-05-13 stsp }
5052 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5053 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5054 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5055 f0b75401 2019-08-03 stsp goto done;
5056 f0b75401 2019-08-03 stsp }
5057 f0b75401 2019-08-03 stsp }
5058 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5059 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5060 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5061 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5062 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5063 036813ee 2019-05-09 stsp goto done;
5064 036813ee 2019-05-09 stsp }
5065 ca2503ea 2019-05-09 stsp }
5066 24519714 2019-05-09 stsp ct->path = strdup(path);
5067 24519714 2019-05-09 stsp if (ct->path == NULL) {
5068 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5069 24519714 2019-05-09 stsp goto done;
5070 24519714 2019-05-09 stsp }
5071 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5072 c4296144 2019-05-09 stsp done:
5073 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5074 ed175427 2019-05-09 stsp free_commitable(ct);
5075 24519714 2019-05-09 stsp free(parent_path);
5076 036813ee 2019-05-09 stsp free(path);
5077 a129376b 2019-03-28 stsp return err;
5078 a129376b 2019-03-28 stsp }
5079 c4296144 2019-05-09 stsp
5080 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5081 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5082 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5083 036813ee 2019-05-09 stsp struct got_repository *);
5084 ed175427 2019-05-09 stsp
5085 ed175427 2019-05-09 stsp static const struct got_error *
5086 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5087 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5088 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5089 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5090 afa376bf 2019-05-09 stsp struct got_repository *repo)
5091 ed175427 2019-05-09 stsp {
5092 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5093 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5094 036813ee 2019-05-09 stsp char *subpath;
5095 ed175427 2019-05-09 stsp
5096 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5097 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5098 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5099 ed175427 2019-05-09 stsp
5100 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5101 ed175427 2019-05-09 stsp if (err)
5102 ed175427 2019-05-09 stsp return err;
5103 ed175427 2019-05-09 stsp
5104 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5105 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5106 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5107 036813ee 2019-05-09 stsp free(subpath);
5108 036813ee 2019-05-09 stsp return err;
5109 036813ee 2019-05-09 stsp }
5110 ed175427 2019-05-09 stsp
5111 036813ee 2019-05-09 stsp static const struct got_error *
5112 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5113 036813ee 2019-05-09 stsp {
5114 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5115 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5116 ed175427 2019-05-09 stsp
5117 036813ee 2019-05-09 stsp *match = 0;
5118 ed175427 2019-05-09 stsp
5119 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5120 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5121 0f63689d 2019-05-10 stsp return NULL;
5122 036813ee 2019-05-09 stsp }
5123 0b5cc0d6 2019-05-09 stsp
5124 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5125 0f63689d 2019-05-10 stsp if (err)
5126 0f63689d 2019-05-10 stsp return err;
5127 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5128 036813ee 2019-05-09 stsp free(ct_parent_path);
5129 036813ee 2019-05-09 stsp return err;
5130 036813ee 2019-05-09 stsp }
5131 0b5cc0d6 2019-05-09 stsp
5132 768aea60 2019-05-09 stsp static mode_t
5133 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5134 768aea60 2019-05-09 stsp {
5135 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5136 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5137 3d9a4ec4 2020-07-23 stsp
5138 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5139 768aea60 2019-05-09 stsp }
5140 768aea60 2019-05-09 stsp
5141 036813ee 2019-05-09 stsp static const struct got_error *
5142 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5143 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5144 036813ee 2019-05-09 stsp {
5145 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5146 ca2503ea 2019-05-09 stsp
5147 036813ee 2019-05-09 stsp *new_te = NULL;
5148 0b5cc0d6 2019-05-09 stsp
5149 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5150 036813ee 2019-05-09 stsp if (err)
5151 036813ee 2019-05-09 stsp goto done;
5152 0b5cc0d6 2019-05-09 stsp
5153 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5154 036813ee 2019-05-09 stsp
5155 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5156 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5157 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5158 5f8a88c6 2019-08-03 stsp else
5159 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5160 036813ee 2019-05-09 stsp done:
5161 036813ee 2019-05-09 stsp if (err && *new_te) {
5162 56e0773d 2019-11-28 stsp free(*new_te);
5163 036813ee 2019-05-09 stsp *new_te = NULL;
5164 036813ee 2019-05-09 stsp }
5165 036813ee 2019-05-09 stsp return err;
5166 036813ee 2019-05-09 stsp }
5167 036813ee 2019-05-09 stsp
5168 036813ee 2019-05-09 stsp static const struct got_error *
5169 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5170 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5171 036813ee 2019-05-09 stsp {
5172 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5173 102b254e 2020-10-19 stsp char *ct_name = NULL;
5174 036813ee 2019-05-09 stsp
5175 036813ee 2019-05-09 stsp *new_te = NULL;
5176 036813ee 2019-05-09 stsp
5177 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5178 036813ee 2019-05-09 stsp if (*new_te == NULL)
5179 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5180 036813ee 2019-05-09 stsp
5181 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5182 102b254e 2020-10-19 stsp if (err)
5183 036813ee 2019-05-09 stsp goto done;
5184 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5185 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5186 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5187 036813ee 2019-05-09 stsp goto done;
5188 036813ee 2019-05-09 stsp }
5189 036813ee 2019-05-09 stsp
5190 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5191 036813ee 2019-05-09 stsp
5192 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5193 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5194 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5195 5f8a88c6 2019-08-03 stsp else
5196 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5197 036813ee 2019-05-09 stsp done:
5198 102b254e 2020-10-19 stsp free(ct_name);
5199 036813ee 2019-05-09 stsp if (err && *new_te) {
5200 56e0773d 2019-11-28 stsp free(*new_te);
5201 036813ee 2019-05-09 stsp *new_te = NULL;
5202 036813ee 2019-05-09 stsp }
5203 036813ee 2019-05-09 stsp return err;
5204 036813ee 2019-05-09 stsp }
5205 036813ee 2019-05-09 stsp
5206 036813ee 2019-05-09 stsp static const struct got_error *
5207 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5208 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5209 036813ee 2019-05-09 stsp {
5210 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5211 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5212 036813ee 2019-05-09 stsp
5213 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5214 036813ee 2019-05-09 stsp if (err)
5215 036813ee 2019-05-09 stsp return err;
5216 036813ee 2019-05-09 stsp if (new_pe == NULL)
5217 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5218 036813ee 2019-05-09 stsp return NULL;
5219 afa376bf 2019-05-09 stsp }
5220 afa376bf 2019-05-09 stsp
5221 afa376bf 2019-05-09 stsp static const struct got_error *
5222 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5223 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5224 afa376bf 2019-05-09 stsp {
5225 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5226 5f8a88c6 2019-08-03 stsp unsigned char status;
5227 0ff8d236 2021-09-28 stsp
5228 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5229 0ff8d236 2021-09-28 stsp return NULL;
5230 5f8a88c6 2019-08-03 stsp
5231 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5232 afa376bf 2019-05-09 stsp ct_path++;
5233 5f8a88c6 2019-08-03 stsp
5234 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5235 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5236 5f8a88c6 2019-08-03 stsp else
5237 5f8a88c6 2019-08-03 stsp status = ct->status;
5238 5f8a88c6 2019-08-03 stsp
5239 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5240 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5241 036813ee 2019-05-09 stsp }
5242 036813ee 2019-05-09 stsp
5243 036813ee 2019-05-09 stsp static const struct got_error *
5244 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5245 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5246 44d03001 2019-05-09 stsp {
5247 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5248 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5249 44d03001 2019-05-09 stsp char *te_path;
5250 44d03001 2019-05-09 stsp
5251 44d03001 2019-05-09 stsp *modified = 0;
5252 44d03001 2019-05-09 stsp
5253 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5254 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5255 44d03001 2019-05-09 stsp te->name) == -1)
5256 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5257 44d03001 2019-05-09 stsp
5258 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5259 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5260 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5261 44d03001 2019-05-09 stsp strlen(te_path));
5262 62d463ca 2020-10-20 naddy if (*modified)
5263 44d03001 2019-05-09 stsp break;
5264 44d03001 2019-05-09 stsp }
5265 44d03001 2019-05-09 stsp
5266 44d03001 2019-05-09 stsp free(te_path);
5267 44d03001 2019-05-09 stsp return err;
5268 44d03001 2019-05-09 stsp }
5269 44d03001 2019-05-09 stsp
5270 44d03001 2019-05-09 stsp static const struct got_error *
5271 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5272 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5273 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5274 036813ee 2019-05-09 stsp {
5275 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5276 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5277 036813ee 2019-05-09 stsp
5278 036813ee 2019-05-09 stsp *ctp = NULL;
5279 036813ee 2019-05-09 stsp
5280 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5282 036813ee 2019-05-09 stsp char *ct_name = NULL;
5283 036813ee 2019-05-09 stsp int path_matches;
5284 036813ee 2019-05-09 stsp
5285 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5286 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5287 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5288 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5289 5f8a88c6 2019-08-03 stsp continue;
5290 5f8a88c6 2019-08-03 stsp } else {
5291 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5292 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5293 5f8a88c6 2019-08-03 stsp continue;
5294 5f8a88c6 2019-08-03 stsp }
5295 036813ee 2019-05-09 stsp
5296 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5297 036813ee 2019-05-09 stsp continue;
5298 036813ee 2019-05-09 stsp
5299 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5300 62d463ca 2020-10-20 naddy if (err)
5301 036813ee 2019-05-09 stsp return err;
5302 036813ee 2019-05-09 stsp if (!path_matches)
5303 036813ee 2019-05-09 stsp continue;
5304 036813ee 2019-05-09 stsp
5305 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5306 d34b633e 2020-10-19 stsp if (err)
5307 d34b633e 2020-10-19 stsp return err;
5308 d34b633e 2020-10-19 stsp
5309 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5310 d34b633e 2020-10-19 stsp free(ct_name);
5311 036813ee 2019-05-09 stsp continue;
5312 d34b633e 2020-10-19 stsp }
5313 d34b633e 2020-10-19 stsp free(ct_name);
5314 036813ee 2019-05-09 stsp
5315 036813ee 2019-05-09 stsp *ctp = ct;
5316 036813ee 2019-05-09 stsp break;
5317 036813ee 2019-05-09 stsp }
5318 2b496619 2019-07-10 stsp
5319 2b496619 2019-07-10 stsp return err;
5320 2b496619 2019-07-10 stsp }
5321 2b496619 2019-07-10 stsp
5322 2b496619 2019-07-10 stsp static const struct got_error *
5323 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5324 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5325 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5326 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5327 2b496619 2019-07-10 stsp struct got_repository *repo)
5328 2b496619 2019-07-10 stsp {
5329 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5330 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5331 2b496619 2019-07-10 stsp char *subtree_path;
5332 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5333 ba580f68 2020-03-22 stsp int nentries;
5334 2b496619 2019-07-10 stsp
5335 2b496619 2019-07-10 stsp *new_tep = NULL;
5336 2b496619 2019-07-10 stsp
5337 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5338 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5339 2b496619 2019-07-10 stsp child_path) == -1)
5340 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5341 036813ee 2019-05-09 stsp
5342 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5343 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5344 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5345 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5346 56e0773d 2019-11-28 stsp
5347 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5348 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5349 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5350 2b496619 2019-07-10 stsp goto done;
5351 2b496619 2019-07-10 stsp }
5352 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5353 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5354 2b496619 2019-07-10 stsp if (err) {
5355 56e0773d 2019-11-28 stsp free(new_te);
5356 2b496619 2019-07-10 stsp goto done;
5357 2b496619 2019-07-10 stsp }
5358 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5359 2b496619 2019-07-10 stsp done:
5360 56e0773d 2019-11-28 stsp free(id);
5361 2b496619 2019-07-10 stsp free(subtree_path);
5362 2b496619 2019-07-10 stsp if (err == NULL)
5363 2b496619 2019-07-10 stsp *new_tep = new_te;
5364 036813ee 2019-05-09 stsp return err;
5365 036813ee 2019-05-09 stsp }
5366 036813ee 2019-05-09 stsp
5367 036813ee 2019-05-09 stsp static const struct got_error *
5368 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5369 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5370 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5371 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5372 036813ee 2019-05-09 stsp struct got_repository *repo)
5373 036813ee 2019-05-09 stsp {
5374 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5375 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5376 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5377 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5378 036813ee 2019-05-09 stsp
5379 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5380 ba580f68 2020-03-22 stsp *nentries = 0;
5381 036813ee 2019-05-09 stsp
5382 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5383 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5384 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5385 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5386 036813ee 2019-05-09 stsp
5387 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5388 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5389 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5390 036813ee 2019-05-09 stsp continue;
5391 036813ee 2019-05-09 stsp
5392 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5393 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5394 036813ee 2019-05-09 stsp continue;
5395 036813ee 2019-05-09 stsp
5396 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5397 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5398 036813ee 2019-05-09 stsp if (err)
5399 036813ee 2019-05-09 stsp goto done;
5400 036813ee 2019-05-09 stsp
5401 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5402 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5403 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5404 036813ee 2019-05-09 stsp if (err)
5405 036813ee 2019-05-09 stsp goto done;
5406 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5407 afa376bf 2019-05-09 stsp if (err)
5408 afa376bf 2019-05-09 stsp goto done;
5409 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5410 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5411 2b496619 2019-07-10 stsp if (err)
5412 2f51b5b3 2019-05-09 stsp goto done;
5413 ba580f68 2020-03-22 stsp (*nentries)++;
5414 2b496619 2019-07-10 stsp } else {
5415 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5416 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5417 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5418 2b496619 2019-07-10 stsp == NULL) {
5419 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5420 2b496619 2019-07-10 stsp child_path, path_base_tree,
5421 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5422 2b496619 2019-07-10 stsp repo);
5423 2b496619 2019-07-10 stsp if (err)
5424 2b496619 2019-07-10 stsp goto done;
5425 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5426 2b496619 2019-07-10 stsp if (err)
5427 2b496619 2019-07-10 stsp goto done;
5428 ba580f68 2020-03-22 stsp (*nentries)++;
5429 9ba0479c 2019-05-10 stsp }
5430 ed175427 2019-05-09 stsp }
5431 2f51b5b3 2019-05-09 stsp }
5432 2f51b5b3 2019-05-09 stsp
5433 2f51b5b3 2019-05-09 stsp if (base_tree) {
5434 56e0773d 2019-11-28 stsp int i, nbase_entries;
5435 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5436 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5437 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5438 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5439 63c5ca5d 2019-08-24 stsp
5440 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5441 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5442 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5443 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5444 63c5ca5d 2019-08-24 stsp if (err)
5445 63c5ca5d 2019-08-24 stsp goto done;
5446 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5447 63c5ca5d 2019-08-24 stsp if (err)
5448 63c5ca5d 2019-08-24 stsp goto done;
5449 ba580f68 2020-03-22 stsp (*nentries)++;
5450 63c5ca5d 2019-08-24 stsp continue;
5451 63c5ca5d 2019-08-24 stsp }
5452 2f51b5b3 2019-05-09 stsp
5453 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5454 44d03001 2019-05-09 stsp int modified;
5455 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5456 036813ee 2019-05-09 stsp if (err)
5457 036813ee 2019-05-09 stsp goto done;
5458 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5459 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5460 2f51b5b3 2019-05-09 stsp if (err)
5461 2f51b5b3 2019-05-09 stsp goto done;
5462 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5463 44d03001 2019-05-09 stsp if (modified) {
5464 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5465 ba580f68 2020-03-22 stsp int nsubentries;
5466 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5467 ba580f68 2020-03-22 stsp &nsubentries, te,
5468 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5469 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5470 44d03001 2019-05-09 stsp if (err)
5471 44d03001 2019-05-09 stsp goto done;
5472 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5473 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5474 ba580f68 2020-03-22 stsp free(new_id);
5475 ba580f68 2020-03-22 stsp continue;
5476 ba580f68 2020-03-22 stsp }
5477 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5478 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5479 56e0773d 2019-11-28 stsp free(new_id);
5480 44d03001 2019-05-09 stsp }
5481 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5482 036813ee 2019-05-09 stsp if (err)
5483 036813ee 2019-05-09 stsp goto done;
5484 ba580f68 2020-03-22 stsp (*nentries)++;
5485 2f51b5b3 2019-05-09 stsp continue;
5486 036813ee 2019-05-09 stsp }
5487 2f51b5b3 2019-05-09 stsp
5488 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5489 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5490 f66c734c 2019-09-22 stsp if (err)
5491 f66c734c 2019-09-22 stsp goto done;
5492 2f51b5b3 2019-05-09 stsp if (ct) {
5493 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5494 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5495 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5496 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5497 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5498 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5499 2f51b5b3 2019-05-09 stsp if (err)
5500 2f51b5b3 2019-05-09 stsp goto done;
5501 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5502 2f51b5b3 2019-05-09 stsp if (err)
5503 2f51b5b3 2019-05-09 stsp goto done;
5504 ba580f68 2020-03-22 stsp (*nentries)++;
5505 2f51b5b3 2019-05-09 stsp }
5506 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5507 b416585c 2019-05-13 stsp status_arg);
5508 afa376bf 2019-05-09 stsp if (err)
5509 afa376bf 2019-05-09 stsp goto done;
5510 2f51b5b3 2019-05-09 stsp } else {
5511 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5512 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5513 2f51b5b3 2019-05-09 stsp if (err)
5514 2f51b5b3 2019-05-09 stsp goto done;
5515 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5516 2f51b5b3 2019-05-09 stsp if (err)
5517 2f51b5b3 2019-05-09 stsp goto done;
5518 ba580f68 2020-03-22 stsp (*nentries)++;
5519 2f51b5b3 2019-05-09 stsp }
5520 ca2503ea 2019-05-09 stsp }
5521 ed175427 2019-05-09 stsp }
5522 0b5cc0d6 2019-05-09 stsp
5523 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5524 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5525 ed175427 2019-05-09 stsp done:
5526 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5527 2e1fa222 2020-07-23 stsp return err;
5528 2e1fa222 2020-07-23 stsp }
5529 2e1fa222 2020-07-23 stsp
5530 2e1fa222 2020-07-23 stsp static const struct got_error *
5531 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5532 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5533 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5534 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5535 ebf99748 2019-05-09 stsp {
5536 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5537 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5538 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5539 ebf99748 2019-05-09 stsp
5540 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5541 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5542 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5543 ebf99748 2019-05-09 stsp
5544 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5545 437adc9d 2020-12-10 yzhong
5546 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5547 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5548 437adc9d 2020-12-10 yzhong if (err)
5549 437adc9d 2020-12-10 yzhong goto done;
5550 437adc9d 2020-12-10 yzhong
5551 ebf99748 2019-05-09 stsp if (ie) {
5552 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5553 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5554 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5555 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5556 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5557 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5558 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5559 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5560 437adc9d 2020-12-10 yzhong
5561 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5562 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5563 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5564 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5565 72fd46fa 2019-09-06 stsp !have_staged_files);
5566 ebf99748 2019-05-09 stsp } else
5567 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5568 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5569 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5570 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5571 72fd46fa 2019-09-06 stsp !have_staged_files);
5572 ebf99748 2019-05-09 stsp } else {
5573 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5574 ebf99748 2019-05-09 stsp if (err)
5575 437adc9d 2020-12-10 yzhong goto done;
5576 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5577 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5578 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5579 3969253a 2020-03-07 stsp if (err) {
5580 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5581 437adc9d 2020-12-10 yzhong goto done;
5582 3969253a 2020-03-07 stsp }
5583 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5584 3969253a 2020-03-07 stsp if (err) {
5585 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5586 437adc9d 2020-12-10 yzhong goto done;
5587 3969253a 2020-03-07 stsp }
5588 ebf99748 2019-05-09 stsp }
5589 437adc9d 2020-12-10 yzhong free(relpath);
5590 437adc9d 2020-12-10 yzhong relpath = NULL;
5591 ebf99748 2019-05-09 stsp }
5592 437adc9d 2020-12-10 yzhong done:
5593 437adc9d 2020-12-10 yzhong free(relpath);
5594 d56d26ce 2019-05-10 stsp return err;
5595 d56d26ce 2019-05-10 stsp }
5596 735ef5ac 2019-08-03 stsp
5597 d56d26ce 2019-05-10 stsp
5598 d56d26ce 2019-05-10 stsp static const struct got_error *
5599 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5600 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5601 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5602 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5603 735ef5ac 2019-08-03 stsp int ood_errcode)
5604 d56d26ce 2019-05-10 stsp {
5605 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5606 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5607 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5608 1a36436d 2019-06-10 stsp
5609 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5610 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5611 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5612 1a36436d 2019-06-10 stsp return NULL;
5613 9bead371 2019-07-28 stsp /*
5614 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5615 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5616 9bead371 2019-07-28 stsp */
5617 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5618 a44927cc 2022-04-07 stsp if (err)
5619 a44927cc 2022-04-07 stsp goto done;
5620 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5621 9bead371 2019-07-28 stsp if (err) {
5622 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5623 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5624 1a36436d 2019-06-10 stsp goto done;
5625 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5626 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5627 1a36436d 2019-06-10 stsp } else {
5628 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5629 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5630 a44927cc 2022-04-07 stsp if (err)
5631 a44927cc 2022-04-07 stsp goto done;
5632 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5633 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5634 1a36436d 2019-06-10 stsp goto done;
5635 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5636 1a36436d 2019-06-10 stsp }
5637 1a36436d 2019-06-10 stsp done:
5638 1a36436d 2019-06-10 stsp free(id);
5639 a44927cc 2022-04-07 stsp if (commit)
5640 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5641 1a36436d 2019-06-10 stsp return err;
5642 ebf99748 2019-05-09 stsp }
5643 ebf99748 2019-05-09 stsp
5644 336075a4 2022-06-25 op static const struct got_error *
5645 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5646 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5647 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5648 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5649 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5650 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5651 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5652 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5653 afa376bf 2019-05-09 stsp struct got_repository *repo)
5654 c4296144 2019-05-09 stsp {
5655 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5656 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5657 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5658 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5659 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5660 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5661 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5662 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5663 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5664 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5665 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5666 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5667 c4296144 2019-05-09 stsp
5668 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5669 c4296144 2019-05-09 stsp
5670 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5671 675c7539 2019-05-09 stsp
5672 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5673 588edf97 2019-05-10 stsp if (err)
5674 588edf97 2019-05-10 stsp goto done;
5675 de18fc63 2019-05-09 stsp
5676 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5677 588edf97 2019-05-10 stsp if (err)
5678 588edf97 2019-05-10 stsp goto done;
5679 588edf97 2019-05-10 stsp
5680 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5681 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5682 33ad4cbe 2019-05-12 jcs if (err)
5683 33ad4cbe 2019-05-12 jcs goto done;
5684 33ad4cbe 2019-05-12 jcs }
5685 c4296144 2019-05-09 stsp
5686 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5687 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5688 33ad4cbe 2019-05-12 jcs goto done;
5689 33ad4cbe 2019-05-12 jcs }
5690 33ad4cbe 2019-05-12 jcs
5691 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5692 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5693 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5694 cf066bf8 2019-05-09 stsp char *ondisk_path;
5695 cf066bf8 2019-05-09 stsp
5696 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5697 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5698 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5699 5f8a88c6 2019-08-03 stsp continue;
5700 5f8a88c6 2019-08-03 stsp
5701 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5702 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5703 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5704 cf066bf8 2019-05-09 stsp continue;
5705 cf066bf8 2019-05-09 stsp
5706 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5707 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5708 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5709 cf066bf8 2019-05-09 stsp goto done;
5710 cf066bf8 2019-05-09 stsp }
5711 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5712 2e1fa222 2020-07-23 stsp free(ondisk_path);
5713 2e1fa222 2020-07-23 stsp if (err)
5714 cf066bf8 2019-05-09 stsp goto done;
5715 cf066bf8 2019-05-09 stsp }
5716 036813ee 2019-05-09 stsp
5717 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5718 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5719 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5720 036813ee 2019-05-09 stsp if (err)
5721 036813ee 2019-05-09 stsp goto done;
5722 036813ee 2019-05-09 stsp
5723 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5724 de18fc63 2019-05-09 stsp if (err)
5725 de18fc63 2019-05-09 stsp goto done;
5726 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5727 f259c4c1 2021-09-24 stsp nparents++;
5728 f259c4c1 2021-09-24 stsp if (parent_id2) {
5729 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5730 f259c4c1 2021-09-24 stsp if (err)
5731 f259c4c1 2021-09-24 stsp goto done;
5732 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5733 f259c4c1 2021-09-24 stsp nparents++;
5734 f259c4c1 2021-09-24 stsp }
5735 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5736 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5737 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5738 33ad4cbe 2019-05-12 jcs free(logmsg);
5739 9d40349a 2019-05-09 stsp if (err)
5740 9d40349a 2019-05-09 stsp goto done;
5741 9d40349a 2019-05-09 stsp
5742 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5743 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5744 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5745 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5746 09f5bd90 2019-05-09 stsp goto done;
5747 09f5bd90 2019-05-09 stsp }
5748 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5749 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5750 09f5bd90 2019-05-09 stsp if (err)
5751 09f5bd90 2019-05-09 stsp goto done;
5752 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5753 ebf99748 2019-05-09 stsp if (err)
5754 ebf99748 2019-05-09 stsp goto done;
5755 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5756 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5757 09f5bd90 2019-05-09 stsp goto done;
5758 09f5bd90 2019-05-09 stsp }
5759 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5760 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5761 f2c16586 2019-05-09 stsp if (err)
5762 f2c16586 2019-05-09 stsp goto done;
5763 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5764 f2c16586 2019-05-09 stsp if (err)
5765 f2c16586 2019-05-09 stsp goto done;
5766 f2c16586 2019-05-09 stsp
5767 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5768 09f5bd90 2019-05-09 stsp if (err)
5769 09f5bd90 2019-05-09 stsp goto done;
5770 09f5bd90 2019-05-09 stsp
5771 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5772 ebf99748 2019-05-09 stsp if (err)
5773 ebf99748 2019-05-09 stsp goto done;
5774 c4296144 2019-05-09 stsp done:
5775 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5776 588edf97 2019-05-10 stsp if (head_tree)
5777 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5778 588edf97 2019-05-10 stsp if (head_commit)
5779 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5780 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5781 2f17228e 2019-05-12 stsp if (head_ref2) {
5782 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5783 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5784 2f17228e 2019-05-12 stsp err = unlockerr;
5785 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5786 39cd0ff6 2019-07-12 stsp }
5787 39cd0ff6 2019-07-12 stsp return err;
5788 39cd0ff6 2019-07-12 stsp }
5789 5c1e53bc 2019-07-28 stsp
5790 5c1e53bc 2019-07-28 stsp static const struct got_error *
5791 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5792 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5793 5c1e53bc 2019-07-28 stsp {
5794 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5795 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5796 39cd0ff6 2019-07-12 stsp
5797 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5798 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5799 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5800 5c1e53bc 2019-07-28 stsp
5801 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5802 5c1e53bc 2019-07-28 stsp ct_path++;
5803 5c1e53bc 2019-07-28 stsp
5804 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5805 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5806 5c1e53bc 2019-07-28 stsp break;
5807 5c1e53bc 2019-07-28 stsp }
5808 5c1e53bc 2019-07-28 stsp
5809 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5810 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5811 5c1e53bc 2019-07-28 stsp
5812 5c1e53bc 2019-07-28 stsp return NULL;
5813 5c1e53bc 2019-07-28 stsp }
5814 5c1e53bc 2019-07-28 stsp
5815 f0b75401 2019-08-03 stsp static const struct got_error *
5816 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5817 f0b75401 2019-08-03 stsp {
5818 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5819 f0b75401 2019-08-03 stsp
5820 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5821 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5822 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5823 f0b75401 2019-08-03 stsp }
5824 f0b75401 2019-08-03 stsp
5825 f0b75401 2019-08-03 stsp return NULL;
5826 f0b75401 2019-08-03 stsp }
5827 f0b75401 2019-08-03 stsp
5828 5f8a88c6 2019-08-03 stsp static const struct got_error *
5829 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5830 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5831 5f8a88c6 2019-08-03 stsp {
5832 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5833 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5834 5f8a88c6 2019-08-03 stsp
5835 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5836 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5837 5f8a88c6 2019-08-03 stsp continue;
5838 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5839 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5840 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5841 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5842 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5843 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5844 5f8a88c6 2019-08-03 stsp }
5845 5f8a88c6 2019-08-03 stsp
5846 5f8a88c6 2019-08-03 stsp return NULL;
5847 5f8a88c6 2019-08-03 stsp }
5848 5f8a88c6 2019-08-03 stsp
5849 39cd0ff6 2019-07-12 stsp const struct got_error *
5850 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5851 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5852 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5853 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5854 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5855 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5856 39cd0ff6 2019-07-12 stsp {
5857 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5858 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5859 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5860 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5861 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5862 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5863 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5864 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5865 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5866 39cd0ff6 2019-07-12 stsp
5867 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5868 39cd0ff6 2019-07-12 stsp
5869 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5870 39cd0ff6 2019-07-12 stsp
5871 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5872 39cd0ff6 2019-07-12 stsp if (err)
5873 39cd0ff6 2019-07-12 stsp goto done;
5874 39cd0ff6 2019-07-12 stsp
5875 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5876 39cd0ff6 2019-07-12 stsp if (err)
5877 39cd0ff6 2019-07-12 stsp goto done;
5878 39cd0ff6 2019-07-12 stsp
5879 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5880 39cd0ff6 2019-07-12 stsp if (err)
5881 39cd0ff6 2019-07-12 stsp goto done;
5882 39cd0ff6 2019-07-12 stsp
5883 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5884 39cd0ff6 2019-07-12 stsp if (err)
5885 39cd0ff6 2019-07-12 stsp goto done;
5886 39cd0ff6 2019-07-12 stsp
5887 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5888 5f8a88c6 2019-08-03 stsp &have_staged_files);
5889 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5890 5f8a88c6 2019-08-03 stsp goto done;
5891 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5892 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5893 5f8a88c6 2019-08-03 stsp if (err)
5894 5f8a88c6 2019-08-03 stsp goto done;
5895 5f8a88c6 2019-08-03 stsp }
5896 5f8a88c6 2019-08-03 stsp
5897 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5898 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5899 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5900 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5901 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5902 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5903 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5904 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5905 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5906 5c1e53bc 2019-07-28 stsp if (err)
5907 5c1e53bc 2019-07-28 stsp goto done;
5908 5c1e53bc 2019-07-28 stsp }
5909 39cd0ff6 2019-07-12 stsp
5910 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5911 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5912 39cd0ff6 2019-07-12 stsp goto done;
5913 5c1e53bc 2019-07-28 stsp }
5914 5c1e53bc 2019-07-28 stsp
5915 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5916 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5917 5c1e53bc 2019-07-28 stsp if (err)
5918 5c1e53bc 2019-07-28 stsp goto done;
5919 39cd0ff6 2019-07-12 stsp }
5920 f0b75401 2019-08-03 stsp
5921 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5922 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5923 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5924 f0b75401 2019-08-03 stsp
5925 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5926 f0b75401 2019-08-03 stsp ct_path++;
5927 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5928 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5929 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5930 b50cabdf 2019-07-12 stsp if (err)
5931 b50cabdf 2019-07-12 stsp goto done;
5932 f0b75401 2019-08-03 stsp
5933 b50cabdf 2019-07-12 stsp }
5934 b50cabdf 2019-07-12 stsp
5935 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5936 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5937 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5938 39cd0ff6 2019-07-12 stsp if (err)
5939 39cd0ff6 2019-07-12 stsp goto done;
5940 39cd0ff6 2019-07-12 stsp
5941 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5942 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5943 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5944 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5945 39cd0ff6 2019-07-12 stsp err = sync_err;
5946 39cd0ff6 2019-07-12 stsp done:
5947 39cd0ff6 2019-07-12 stsp if (fileindex)
5948 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5949 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5950 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5951 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5952 39cd0ff6 2019-07-12 stsp err = unlockerr;
5953 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5954 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5955 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5956 39cd0ff6 2019-07-12 stsp }
5957 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5958 c4296144 2019-05-09 stsp return err;
5959 8656d6c4 2019-05-20 stsp }
5960 8656d6c4 2019-05-20 stsp
5961 8656d6c4 2019-05-20 stsp const char *
5962 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5963 8656d6c4 2019-05-20 stsp {
5964 8656d6c4 2019-05-20 stsp return ct->path;
5965 8656d6c4 2019-05-20 stsp }
5966 8656d6c4 2019-05-20 stsp
5967 8656d6c4 2019-05-20 stsp unsigned int
5968 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5969 8656d6c4 2019-05-20 stsp {
5970 8656d6c4 2019-05-20 stsp return ct->status;
5971 818c7501 2019-07-11 stsp }
5972 818c7501 2019-07-11 stsp
5973 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5974 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5975 818c7501 2019-07-11 stsp struct got_repository *repo;
5976 818c7501 2019-07-11 stsp };
5977 818c7501 2019-07-11 stsp
5978 818c7501 2019-07-11 stsp static const struct got_error *
5979 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5980 818c7501 2019-07-11 stsp {
5981 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5982 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5983 818c7501 2019-07-11 stsp unsigned char status;
5984 818c7501 2019-07-11 stsp struct stat sb;
5985 818c7501 2019-07-11 stsp char *ondisk_path;
5986 818c7501 2019-07-11 stsp
5987 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5988 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5989 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5990 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5991 818c7501 2019-07-11 stsp
5992 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5993 818c7501 2019-07-11 stsp == -1)
5994 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5995 818c7501 2019-07-11 stsp
5996 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5997 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5998 818c7501 2019-07-11 stsp free(ondisk_path);
5999 818c7501 2019-07-11 stsp if (err)
6000 818c7501 2019-07-11 stsp return err;
6001 818c7501 2019-07-11 stsp
6002 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6003 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6004 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6005 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6006 818c7501 2019-07-11 stsp
6007 818c7501 2019-07-11 stsp return NULL;
6008 818c7501 2019-07-11 stsp }
6009 818c7501 2019-07-11 stsp
6010 818c7501 2019-07-11 stsp const struct got_error *
6011 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6012 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6013 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6014 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6015 818c7501 2019-07-11 stsp {
6016 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6017 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6018 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6019 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6020 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6021 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6022 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6023 818c7501 2019-07-11 stsp
6024 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6025 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6026 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6027 818c7501 2019-07-11 stsp
6028 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6029 818c7501 2019-07-11 stsp if (err)
6030 818c7501 2019-07-11 stsp return err;
6031 818c7501 2019-07-11 stsp
6032 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6033 818c7501 2019-07-11 stsp if (err)
6034 818c7501 2019-07-11 stsp goto done;
6035 818c7501 2019-07-11 stsp
6036 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6037 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6038 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6039 818c7501 2019-07-11 stsp &ok_arg);
6040 818c7501 2019-07-11 stsp if (err)
6041 818c7501 2019-07-11 stsp goto done;
6042 818c7501 2019-07-11 stsp
6043 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6044 818c7501 2019-07-11 stsp if (err)
6045 818c7501 2019-07-11 stsp goto done;
6046 818c7501 2019-07-11 stsp
6047 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6048 818c7501 2019-07-11 stsp if (err)
6049 818c7501 2019-07-11 stsp goto done;
6050 818c7501 2019-07-11 stsp
6051 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, 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 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6056 818c7501 2019-07-11 stsp 0);
6057 e51d7b55 2020-01-04 stsp if (err)
6058 e51d7b55 2020-01-04 stsp goto done;
6059 e51d7b55 2020-01-04 stsp
6060 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6061 818c7501 2019-07-11 stsp if (err)
6062 818c7501 2019-07-11 stsp goto done;
6063 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6064 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6065 e51d7b55 2020-01-04 stsp goto done;
6066 e51d7b55 2020-01-04 stsp }
6067 818c7501 2019-07-11 stsp
6068 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6069 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6070 818c7501 2019-07-11 stsp if (err)
6071 818c7501 2019-07-11 stsp goto done;
6072 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6073 818c7501 2019-07-11 stsp if (err)
6074 818c7501 2019-07-11 stsp goto done;
6075 818c7501 2019-07-11 stsp
6076 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6077 818c7501 2019-07-11 stsp
6078 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6079 818c7501 2019-07-11 stsp if (err)
6080 818c7501 2019-07-11 stsp goto done;
6081 818c7501 2019-07-11 stsp
6082 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6083 818c7501 2019-07-11 stsp if (err)
6084 818c7501 2019-07-11 stsp goto done;
6085 818c7501 2019-07-11 stsp
6086 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6087 818c7501 2019-07-11 stsp worktree->base_commit_id);
6088 818c7501 2019-07-11 stsp if (err)
6089 818c7501 2019-07-11 stsp goto done;
6090 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6091 818c7501 2019-07-11 stsp if (err)
6092 818c7501 2019-07-11 stsp goto done;
6093 818c7501 2019-07-11 stsp
6094 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6095 818c7501 2019-07-11 stsp if (err)
6096 818c7501 2019-07-11 stsp goto done;
6097 818c7501 2019-07-11 stsp done:
6098 818c7501 2019-07-11 stsp free(fileindex_path);
6099 818c7501 2019-07-11 stsp free(tmp_branch_name);
6100 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6101 818c7501 2019-07-11 stsp free(branch_ref_name);
6102 818c7501 2019-07-11 stsp if (branch_ref)
6103 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6104 818c7501 2019-07-11 stsp if (wt_branch)
6105 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6106 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6107 818c7501 2019-07-11 stsp if (err) {
6108 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6109 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6110 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6111 818c7501 2019-07-11 stsp }
6112 818c7501 2019-07-11 stsp if (*tmp_branch) {
6113 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6114 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6115 818c7501 2019-07-11 stsp }
6116 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6117 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6118 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6119 3e3a69f1 2019-07-25 stsp }
6120 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6121 818c7501 2019-07-11 stsp }
6122 818c7501 2019-07-11 stsp return err;
6123 818c7501 2019-07-11 stsp }
6124 818c7501 2019-07-11 stsp
6125 818c7501 2019-07-11 stsp const struct got_error *
6126 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6127 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6128 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6129 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6130 818c7501 2019-07-11 stsp {
6131 818c7501 2019-07-11 stsp const struct got_error *err;
6132 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6133 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6134 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6135 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6136 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6137 818c7501 2019-07-11 stsp
6138 818c7501 2019-07-11 stsp *commit_id = NULL;
6139 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6140 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6141 3e3a69f1 2019-07-25 stsp *branch = NULL;
6142 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6143 3e3a69f1 2019-07-25 stsp
6144 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6145 3e3a69f1 2019-07-25 stsp if (err)
6146 3e3a69f1 2019-07-25 stsp return err;
6147 818c7501 2019-07-11 stsp
6148 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6149 3e3a69f1 2019-07-25 stsp if (err)
6150 f032f1f7 2019-08-04 stsp goto done;
6151 f032f1f7 2019-08-04 stsp
6152 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6153 f032f1f7 2019-08-04 stsp &have_staged_files);
6154 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6155 f032f1f7 2019-08-04 stsp goto done;
6156 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6157 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6158 3e3a69f1 2019-07-25 stsp goto done;
6159 f032f1f7 2019-08-04 stsp }
6160 3e3a69f1 2019-07-25 stsp
6161 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6162 818c7501 2019-07-11 stsp if (err)
6163 f032f1f7 2019-08-04 stsp goto done;
6164 818c7501 2019-07-11 stsp
6165 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6166 818c7501 2019-07-11 stsp if (err)
6167 818c7501 2019-07-11 stsp goto done;
6168 818c7501 2019-07-11 stsp
6169 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6170 818c7501 2019-07-11 stsp if (err)
6171 818c7501 2019-07-11 stsp goto done;
6172 818c7501 2019-07-11 stsp
6173 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6174 818c7501 2019-07-11 stsp if (err)
6175 818c7501 2019-07-11 stsp goto done;
6176 818c7501 2019-07-11 stsp
6177 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6178 818c7501 2019-07-11 stsp if (err)
6179 818c7501 2019-07-11 stsp goto done;
6180 818c7501 2019-07-11 stsp
6181 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6182 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6183 818c7501 2019-07-11 stsp if (err)
6184 818c7501 2019-07-11 stsp goto done;
6185 818c7501 2019-07-11 stsp
6186 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6187 818c7501 2019-07-11 stsp if (err)
6188 818c7501 2019-07-11 stsp goto done;
6189 818c7501 2019-07-11 stsp
6190 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6191 818c7501 2019-07-11 stsp if (err)
6192 818c7501 2019-07-11 stsp goto done;
6193 818c7501 2019-07-11 stsp
6194 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6195 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6196 818c7501 2019-07-11 stsp if (err)
6197 818c7501 2019-07-11 stsp goto done;
6198 818c7501 2019-07-11 stsp
6199 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6200 818c7501 2019-07-11 stsp if (err)
6201 818c7501 2019-07-11 stsp goto done;
6202 818c7501 2019-07-11 stsp done:
6203 818c7501 2019-07-11 stsp free(commit_ref_name);
6204 818c7501 2019-07-11 stsp free(branch_ref_name);
6205 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6206 818c7501 2019-07-11 stsp if (commit_ref)
6207 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6208 818c7501 2019-07-11 stsp if (branch_ref)
6209 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6210 818c7501 2019-07-11 stsp if (err) {
6211 818c7501 2019-07-11 stsp free(*commit_id);
6212 818c7501 2019-07-11 stsp *commit_id = NULL;
6213 818c7501 2019-07-11 stsp if (*tmp_branch) {
6214 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6215 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6216 818c7501 2019-07-11 stsp }
6217 818c7501 2019-07-11 stsp if (*new_base_branch) {
6218 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6219 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6220 818c7501 2019-07-11 stsp }
6221 818c7501 2019-07-11 stsp if (*branch) {
6222 818c7501 2019-07-11 stsp got_ref_close(*branch);
6223 818c7501 2019-07-11 stsp *branch = NULL;
6224 818c7501 2019-07-11 stsp }
6225 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6226 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6227 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6228 3e3a69f1 2019-07-25 stsp }
6229 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6230 818c7501 2019-07-11 stsp }
6231 818c7501 2019-07-11 stsp return err;
6232 c4296144 2019-05-09 stsp }
6233 818c7501 2019-07-11 stsp
6234 818c7501 2019-07-11 stsp const struct got_error *
6235 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6236 818c7501 2019-07-11 stsp {
6237 818c7501 2019-07-11 stsp const struct got_error *err;
6238 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6239 818c7501 2019-07-11 stsp
6240 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6241 818c7501 2019-07-11 stsp if (err)
6242 818c7501 2019-07-11 stsp return err;
6243 818c7501 2019-07-11 stsp
6244 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6245 818c7501 2019-07-11 stsp free(tmp_branch_name);
6246 818c7501 2019-07-11 stsp return NULL;
6247 818c7501 2019-07-11 stsp }
6248 818c7501 2019-07-11 stsp
6249 818c7501 2019-07-11 stsp static const struct got_error *
6250 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6251 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6252 818c7501 2019-07-11 stsp {
6253 0ebf8283 2019-07-24 stsp *logmsg = arg;
6254 818c7501 2019-07-11 stsp return NULL;
6255 818c7501 2019-07-11 stsp }
6256 818c7501 2019-07-11 stsp
6257 818c7501 2019-07-11 stsp static const struct got_error *
6258 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6259 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6260 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6261 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6262 818c7501 2019-07-11 stsp {
6263 818c7501 2019-07-11 stsp return NULL;
6264 01757395 2019-07-12 stsp }
6265 01757395 2019-07-12 stsp
6266 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6267 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6268 01757395 2019-07-12 stsp void *progress_arg;
6269 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6270 01757395 2019-07-12 stsp };
6271 01757395 2019-07-12 stsp
6272 01757395 2019-07-12 stsp static const struct got_error *
6273 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6274 01757395 2019-07-12 stsp {
6275 01757395 2019-07-12 stsp const struct got_error *err;
6276 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6277 01757395 2019-07-12 stsp char *p;
6278 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6279 01757395 2019-07-12 stsp
6280 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6281 01757395 2019-07-12 stsp if (err)
6282 01757395 2019-07-12 stsp return err;
6283 01757395 2019-07-12 stsp
6284 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6285 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6286 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6287 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6288 01757395 2019-07-12 stsp return NULL;
6289 01757395 2019-07-12 stsp
6290 01757395 2019-07-12 stsp p = strdup(path);
6291 01757395 2019-07-12 stsp if (p == NULL)
6292 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6293 01757395 2019-07-12 stsp
6294 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6295 01757395 2019-07-12 stsp if (err || new == NULL)
6296 01757395 2019-07-12 stsp free(p);
6297 01757395 2019-07-12 stsp return err;
6298 01757395 2019-07-12 stsp }
6299 01757395 2019-07-12 stsp
6300 01757395 2019-07-12 stsp void
6301 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6302 01757395 2019-07-12 stsp {
6303 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6304 01757395 2019-07-12 stsp
6305 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6306 01757395 2019-07-12 stsp free((char *)pe->path);
6307 01757395 2019-07-12 stsp
6308 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6309 818c7501 2019-07-11 stsp }
6310 818c7501 2019-07-11 stsp
6311 0ebf8283 2019-07-24 stsp static const struct got_error *
6312 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6313 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6314 818c7501 2019-07-11 stsp {
6315 818c7501 2019-07-11 stsp const struct got_error *err;
6316 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6317 818c7501 2019-07-11 stsp
6318 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6319 818c7501 2019-07-11 stsp if (err) {
6320 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6321 818c7501 2019-07-11 stsp goto done;
6322 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6323 818c7501 2019-07-11 stsp if (err)
6324 818c7501 2019-07-11 stsp goto done;
6325 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6326 818c7501 2019-07-11 stsp if (err)
6327 818c7501 2019-07-11 stsp goto done;
6328 de05890f 2020-03-05 stsp } else if (is_rebase) {
6329 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6330 818c7501 2019-07-11 stsp int cmp;
6331 818c7501 2019-07-11 stsp
6332 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6333 818c7501 2019-07-11 stsp if (err)
6334 818c7501 2019-07-11 stsp goto done;
6335 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6336 818c7501 2019-07-11 stsp free(stored_id);
6337 818c7501 2019-07-11 stsp if (cmp != 0) {
6338 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6339 818c7501 2019-07-11 stsp goto done;
6340 818c7501 2019-07-11 stsp }
6341 818c7501 2019-07-11 stsp }
6342 0ebf8283 2019-07-24 stsp done:
6343 0ebf8283 2019-07-24 stsp if (commit_ref)
6344 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6345 0ebf8283 2019-07-24 stsp return err;
6346 0ebf8283 2019-07-24 stsp }
6347 0ebf8283 2019-07-24 stsp
6348 0ebf8283 2019-07-24 stsp static const struct got_error *
6349 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6350 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6351 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6352 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6353 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6354 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6355 0ebf8283 2019-07-24 stsp {
6356 0ebf8283 2019-07-24 stsp const struct got_error *err;
6357 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6358 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6359 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6360 818c7501 2019-07-11 stsp
6361 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6362 0ebf8283 2019-07-24 stsp
6363 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6364 0ebf8283 2019-07-24 stsp if (err)
6365 0ebf8283 2019-07-24 stsp return err;
6366 0ebf8283 2019-07-24 stsp
6367 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6368 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6369 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6370 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6371 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6372 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6373 818c7501 2019-07-11 stsp if (commit_ref)
6374 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6375 818c7501 2019-07-11 stsp return err;
6376 818c7501 2019-07-11 stsp }
6377 818c7501 2019-07-11 stsp
6378 818c7501 2019-07-11 stsp const struct got_error *
6379 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6380 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6381 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6382 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6383 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6384 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6385 818c7501 2019-07-11 stsp {
6386 0ebf8283 2019-07-24 stsp const struct got_error *err;
6387 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6388 0ebf8283 2019-07-24 stsp
6389 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6390 0ebf8283 2019-07-24 stsp if (err)
6391 0ebf8283 2019-07-24 stsp return err;
6392 0ebf8283 2019-07-24 stsp
6393 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6394 0ebf8283 2019-07-24 stsp if (err)
6395 0ebf8283 2019-07-24 stsp goto done;
6396 0ebf8283 2019-07-24 stsp
6397 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6398 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6399 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6400 0ebf8283 2019-07-24 stsp done:
6401 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6402 0ebf8283 2019-07-24 stsp return err;
6403 0ebf8283 2019-07-24 stsp }
6404 0ebf8283 2019-07-24 stsp
6405 0ebf8283 2019-07-24 stsp const struct got_error *
6406 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6407 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6408 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6409 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6410 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6411 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6412 0ebf8283 2019-07-24 stsp {
6413 0ebf8283 2019-07-24 stsp const struct got_error *err;
6414 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6417 0ebf8283 2019-07-24 stsp if (err)
6418 0ebf8283 2019-07-24 stsp return err;
6419 0ebf8283 2019-07-24 stsp
6420 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6421 0ebf8283 2019-07-24 stsp if (err)
6422 0ebf8283 2019-07-24 stsp goto done;
6423 0ebf8283 2019-07-24 stsp
6424 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6425 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6426 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6427 0ebf8283 2019-07-24 stsp done:
6428 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6429 0ebf8283 2019-07-24 stsp return err;
6430 0ebf8283 2019-07-24 stsp }
6431 0ebf8283 2019-07-24 stsp
6432 0ebf8283 2019-07-24 stsp static const struct got_error *
6433 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6434 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6435 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6436 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6437 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6438 0ebf8283 2019-07-24 stsp {
6439 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6440 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6441 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6442 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6443 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6444 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6445 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6446 818c7501 2019-07-11 stsp
6447 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6448 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6449 a0e95631 2019-07-12 stsp
6450 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6451 818c7501 2019-07-11 stsp
6452 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6453 a0e95631 2019-07-12 stsp if (err)
6454 3e3a69f1 2019-07-25 stsp return err;
6455 a0e95631 2019-07-12 stsp
6456 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6457 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6458 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6459 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6460 01757395 2019-07-12 stsp /*
6461 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6462 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6463 6c13b005 2021-09-02 stsp *
6464 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6465 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6466 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6467 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6468 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6469 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6470 01757395 2019-07-12 stsp */
6471 01757395 2019-07-12 stsp if (merged_paths) {
6472 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6473 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6474 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6475 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6476 f2a9dc41 2019-12-13 tracey 0);
6477 01757395 2019-07-12 stsp if (err)
6478 01757395 2019-07-12 stsp goto done;
6479 01757395 2019-07-12 stsp }
6480 01757395 2019-07-12 stsp } else {
6481 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6482 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6483 01757395 2019-07-12 stsp if (err)
6484 01757395 2019-07-12 stsp goto done;
6485 01757395 2019-07-12 stsp }
6486 a0e95631 2019-07-12 stsp
6487 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6488 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6489 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6490 a0e95631 2019-07-12 stsp if (err)
6491 a0e95631 2019-07-12 stsp goto done;
6492 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6493 a0e95631 2019-07-12 stsp goto done;
6494 ff0d2220 2019-07-11 stsp }
6495 818c7501 2019-07-11 stsp
6496 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6497 edd02c5e 2019-07-11 stsp if (err)
6498 edd02c5e 2019-07-11 stsp goto done;
6499 edd02c5e 2019-07-11 stsp
6500 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6501 818c7501 2019-07-11 stsp if (err)
6502 818c7501 2019-07-11 stsp goto done;
6503 818c7501 2019-07-11 stsp
6504 5943eee2 2019-08-13 stsp if (new_logmsg) {
6505 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6506 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6507 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6508 5943eee2 2019-08-13 stsp goto done;
6509 5943eee2 2019-08-13 stsp }
6510 5943eee2 2019-08-13 stsp } else {
6511 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6512 5943eee2 2019-08-13 stsp if (err)
6513 5943eee2 2019-08-13 stsp goto done;
6514 5943eee2 2019-08-13 stsp }
6515 0ebf8283 2019-07-24 stsp
6516 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6517 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6518 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6519 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6520 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6521 a0e95631 2019-07-12 stsp if (err)
6522 a0e95631 2019-07-12 stsp goto done;
6523 a0e95631 2019-07-12 stsp
6524 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6525 a0e95631 2019-07-12 stsp if (err)
6526 a0e95631 2019-07-12 stsp goto done;
6527 a0e95631 2019-07-12 stsp
6528 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6529 a0e95631 2019-07-12 stsp if (err)
6530 a0e95631 2019-07-12 stsp goto done;
6531 a0e95631 2019-07-12 stsp
6532 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6533 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6534 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6535 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6536 a0e95631 2019-07-12 stsp err = sync_err;
6537 818c7501 2019-07-11 stsp done:
6538 a0e95631 2019-07-12 stsp free(fileindex_path);
6539 a0e95631 2019-07-12 stsp free(head_commit_id);
6540 a0e95631 2019-07-12 stsp if (head_ref)
6541 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6542 818c7501 2019-07-11 stsp if (err) {
6543 818c7501 2019-07-11 stsp free(*new_commit_id);
6544 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6545 818c7501 2019-07-11 stsp }
6546 818c7501 2019-07-11 stsp return err;
6547 818c7501 2019-07-11 stsp }
6548 818c7501 2019-07-11 stsp
6549 818c7501 2019-07-11 stsp const struct got_error *
6550 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6551 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6552 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6553 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6554 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6555 0ebf8283 2019-07-24 stsp {
6556 0ebf8283 2019-07-24 stsp const struct got_error *err;
6557 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6558 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6559 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6560 0ebf8283 2019-07-24 stsp
6561 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6562 0ebf8283 2019-07-24 stsp if (err)
6563 0ebf8283 2019-07-24 stsp return err;
6564 0ebf8283 2019-07-24 stsp
6565 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6566 0ebf8283 2019-07-24 stsp if (err)
6567 0ebf8283 2019-07-24 stsp goto done;
6568 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6569 0ebf8283 2019-07-24 stsp if (err)
6570 0ebf8283 2019-07-24 stsp goto done;
6571 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6572 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp }
6575 0ebf8283 2019-07-24 stsp
6576 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6577 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6578 0ebf8283 2019-07-24 stsp done:
6579 0ebf8283 2019-07-24 stsp if (commit_ref)
6580 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6581 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6582 0ebf8283 2019-07-24 stsp free(commit_id);
6583 0ebf8283 2019-07-24 stsp return err;
6584 0ebf8283 2019-07-24 stsp }
6585 0ebf8283 2019-07-24 stsp
6586 0ebf8283 2019-07-24 stsp const struct got_error *
6587 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6588 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6589 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6590 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6591 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6592 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6593 0ebf8283 2019-07-24 stsp {
6594 0ebf8283 2019-07-24 stsp const struct got_error *err;
6595 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6596 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6597 0ebf8283 2019-07-24 stsp
6598 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6599 0ebf8283 2019-07-24 stsp if (err)
6600 0ebf8283 2019-07-24 stsp return err;
6601 0ebf8283 2019-07-24 stsp
6602 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6603 0ebf8283 2019-07-24 stsp if (err)
6604 0ebf8283 2019-07-24 stsp goto done;
6605 0ebf8283 2019-07-24 stsp
6606 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6607 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6608 0ebf8283 2019-07-24 stsp done:
6609 0ebf8283 2019-07-24 stsp if (commit_ref)
6610 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6611 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6612 0ebf8283 2019-07-24 stsp return err;
6613 0ebf8283 2019-07-24 stsp }
6614 0ebf8283 2019-07-24 stsp
6615 0ebf8283 2019-07-24 stsp const struct got_error *
6616 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6617 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6618 818c7501 2019-07-11 stsp {
6619 3e3a69f1 2019-07-25 stsp if (fileindex)
6620 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6621 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6622 69844fba 2019-07-11 stsp }
6623 69844fba 2019-07-11 stsp
6624 69844fba 2019-07-11 stsp static const struct got_error *
6625 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6626 69844fba 2019-07-11 stsp {
6627 69844fba 2019-07-11 stsp const struct got_error *err;
6628 69844fba 2019-07-11 stsp struct got_reference *ref;
6629 69844fba 2019-07-11 stsp
6630 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6631 69844fba 2019-07-11 stsp if (err) {
6632 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6633 69844fba 2019-07-11 stsp return NULL;
6634 69844fba 2019-07-11 stsp return err;
6635 69844fba 2019-07-11 stsp }
6636 69844fba 2019-07-11 stsp
6637 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6638 69844fba 2019-07-11 stsp got_ref_close(ref);
6639 69844fba 2019-07-11 stsp return err;
6640 818c7501 2019-07-11 stsp }
6641 818c7501 2019-07-11 stsp
6642 69844fba 2019-07-11 stsp static const struct got_error *
6643 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6644 69844fba 2019-07-11 stsp {
6645 69844fba 2019-07-11 stsp const struct got_error *err;
6646 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6647 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6648 69844fba 2019-07-11 stsp
6649 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6650 69844fba 2019-07-11 stsp if (err)
6651 69844fba 2019-07-11 stsp goto done;
6652 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6653 69844fba 2019-07-11 stsp if (err)
6654 69844fba 2019-07-11 stsp goto done;
6655 69844fba 2019-07-11 stsp
6656 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6657 69844fba 2019-07-11 stsp if (err)
6658 69844fba 2019-07-11 stsp goto done;
6659 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6660 69844fba 2019-07-11 stsp if (err)
6661 69844fba 2019-07-11 stsp goto done;
6662 69844fba 2019-07-11 stsp
6663 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6664 69844fba 2019-07-11 stsp if (err)
6665 69844fba 2019-07-11 stsp goto done;
6666 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6667 69844fba 2019-07-11 stsp if (err)
6668 69844fba 2019-07-11 stsp goto done;
6669 69844fba 2019-07-11 stsp
6670 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6671 69844fba 2019-07-11 stsp if (err)
6672 69844fba 2019-07-11 stsp goto done;
6673 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6674 69844fba 2019-07-11 stsp if (err)
6675 69844fba 2019-07-11 stsp goto done;
6676 69844fba 2019-07-11 stsp
6677 69844fba 2019-07-11 stsp done:
6678 69844fba 2019-07-11 stsp free(tmp_branch_name);
6679 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6680 69844fba 2019-07-11 stsp free(branch_ref_name);
6681 69844fba 2019-07-11 stsp free(commit_ref_name);
6682 e600f124 2021-03-21 stsp return err;
6683 e600f124 2021-03-21 stsp }
6684 e600f124 2021-03-21 stsp
6685 336075a4 2022-06-25 op static const struct got_error *
6686 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6687 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6688 e600f124 2021-03-21 stsp {
6689 e600f124 2021-03-21 stsp const struct got_error *err;
6690 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6691 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6692 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6693 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6694 e600f124 2021-03-21 stsp char *refname = NULL;
6695 e600f124 2021-03-21 stsp
6696 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6697 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6698 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6699 e600f124 2021-03-21 stsp branch_name += 11;
6700 e600f124 2021-03-21 stsp
6701 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6702 e600f124 2021-03-21 stsp if (err)
6703 e600f124 2021-03-21 stsp return err;
6704 e600f124 2021-03-21 stsp
6705 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6706 e600f124 2021-03-21 stsp new_id_str) == -1) {
6707 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6708 e600f124 2021-03-21 stsp goto done;
6709 e600f124 2021-03-21 stsp }
6710 e600f124 2021-03-21 stsp
6711 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6712 e600f124 2021-03-21 stsp if (err)
6713 e600f124 2021-03-21 stsp goto done;
6714 e600f124 2021-03-21 stsp
6715 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6716 e600f124 2021-03-21 stsp if (err)
6717 e600f124 2021-03-21 stsp goto done;
6718 e600f124 2021-03-21 stsp
6719 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6720 e600f124 2021-03-21 stsp done:
6721 e600f124 2021-03-21 stsp free(new_id_str);
6722 e600f124 2021-03-21 stsp free(refname);
6723 e600f124 2021-03-21 stsp free(old_commit_id);
6724 e600f124 2021-03-21 stsp if (ref)
6725 e600f124 2021-03-21 stsp got_ref_close(ref);
6726 69844fba 2019-07-11 stsp return err;
6727 69844fba 2019-07-11 stsp }
6728 69844fba 2019-07-11 stsp
6729 818c7501 2019-07-11 stsp const struct got_error *
6730 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6731 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6732 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6733 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6734 818c7501 2019-07-11 stsp {
6735 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6736 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6737 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6738 818c7501 2019-07-11 stsp
6739 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6740 818c7501 2019-07-11 stsp if (err)
6741 818c7501 2019-07-11 stsp return err;
6742 e600f124 2021-03-21 stsp
6743 e600f124 2021-03-21 stsp if (create_backup) {
6744 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6745 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6746 e600f124 2021-03-21 stsp if (err)
6747 e600f124 2021-03-21 stsp goto done;
6748 e600f124 2021-03-21 stsp }
6749 818c7501 2019-07-11 stsp
6750 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6751 818c7501 2019-07-11 stsp if (err)
6752 818c7501 2019-07-11 stsp goto done;
6753 818c7501 2019-07-11 stsp
6754 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6755 818c7501 2019-07-11 stsp if (err)
6756 818c7501 2019-07-11 stsp goto done;
6757 818c7501 2019-07-11 stsp
6758 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6759 818c7501 2019-07-11 stsp if (err)
6760 818c7501 2019-07-11 stsp goto done;
6761 818c7501 2019-07-11 stsp
6762 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6763 a615e0e7 2020-12-16 stsp if (err)
6764 a615e0e7 2020-12-16 stsp goto done;
6765 a615e0e7 2020-12-16 stsp
6766 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6767 a615e0e7 2020-12-16 stsp if (err)
6768 a615e0e7 2020-12-16 stsp goto done;
6769 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6770 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6771 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6772 a615e0e7 2020-12-16 stsp err = sync_err;
6773 818c7501 2019-07-11 stsp done:
6774 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6775 a615e0e7 2020-12-16 stsp free(fileindex_path);
6776 818c7501 2019-07-11 stsp free(new_head_commit_id);
6777 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6778 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6779 818c7501 2019-07-11 stsp err = unlockerr;
6780 818c7501 2019-07-11 stsp return err;
6781 818c7501 2019-07-11 stsp }
6782 818c7501 2019-07-11 stsp
6783 818c7501 2019-07-11 stsp const struct got_error *
6784 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6785 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6786 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6787 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6788 818c7501 2019-07-11 stsp {
6789 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6790 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6791 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6792 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6793 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6794 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6795 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6796 818c7501 2019-07-11 stsp
6797 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp return err;
6800 818c7501 2019-07-11 stsp
6801 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
6802 a44927cc 2022-04-07 stsp worktree->base_commit_id);
6803 a44927cc 2022-04-07 stsp if (err)
6804 a44927cc 2022-04-07 stsp goto done;
6805 a44927cc 2022-04-07 stsp
6806 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6807 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6808 818c7501 2019-07-11 stsp if (err)
6809 818c7501 2019-07-11 stsp goto done;
6810 818c7501 2019-07-11 stsp
6811 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6812 818c7501 2019-07-11 stsp if (err)
6813 818c7501 2019-07-11 stsp goto done;
6814 818c7501 2019-07-11 stsp
6815 818c7501 2019-07-11 stsp /*
6816 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6817 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6818 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6819 818c7501 2019-07-11 stsp */
6820 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6821 818c7501 2019-07-11 stsp if (err)
6822 818c7501 2019-07-11 stsp goto done;
6823 818c7501 2019-07-11 stsp
6824 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6825 818c7501 2019-07-11 stsp if (err)
6826 818c7501 2019-07-11 stsp goto done;
6827 818c7501 2019-07-11 stsp
6828 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
6829 a44927cc 2022-04-07 stsp worktree->path_prefix);
6830 ca355955 2019-07-12 stsp if (err)
6831 ca355955 2019-07-12 stsp goto done;
6832 ca355955 2019-07-12 stsp
6833 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6834 ca355955 2019-07-12 stsp if (err)
6835 ca355955 2019-07-12 stsp goto done;
6836 ca355955 2019-07-12 stsp
6837 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6838 818c7501 2019-07-11 stsp if (err)
6839 818c7501 2019-07-11 stsp goto done;
6840 818c7501 2019-07-11 stsp
6841 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6842 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6843 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6844 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6845 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6846 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6847 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6848 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6849 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6850 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6851 55bd499d 2019-07-12 stsp if (err)
6852 1f1abb7e 2019-08-08 stsp goto sync;
6853 55bd499d 2019-07-12 stsp
6854 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6855 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6856 ca355955 2019-07-12 stsp sync:
6857 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6858 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6859 ca355955 2019-07-12 stsp err = sync_err;
6860 818c7501 2019-07-11 stsp done:
6861 818c7501 2019-07-11 stsp got_ref_close(resolved);
6862 a3a2faf2 2019-07-12 stsp free(tree_id);
6863 818c7501 2019-07-11 stsp free(commit_id);
6864 a44927cc 2022-04-07 stsp if (commit)
6865 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6866 0ebf8283 2019-07-24 stsp if (fileindex)
6867 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6868 0ebf8283 2019-07-24 stsp free(fileindex_path);
6869 0ebf8283 2019-07-24 stsp
6870 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6871 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6872 0ebf8283 2019-07-24 stsp err = unlockerr;
6873 0ebf8283 2019-07-24 stsp return err;
6874 0ebf8283 2019-07-24 stsp }
6875 0ebf8283 2019-07-24 stsp
6876 0ebf8283 2019-07-24 stsp const struct got_error *
6877 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6878 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6879 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6880 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6881 0ebf8283 2019-07-24 stsp {
6882 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6883 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6884 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6885 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6886 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6887 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6888 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6889 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6890 0ebf8283 2019-07-24 stsp
6891 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6892 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6893 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6894 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6895 0ebf8283 2019-07-24 stsp
6896 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6897 0ebf8283 2019-07-24 stsp if (err)
6898 0ebf8283 2019-07-24 stsp return err;
6899 0ebf8283 2019-07-24 stsp
6900 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6901 0ebf8283 2019-07-24 stsp if (err)
6902 0ebf8283 2019-07-24 stsp goto done;
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6905 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6906 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6907 0ebf8283 2019-07-24 stsp &ok_arg);
6908 0ebf8283 2019-07-24 stsp if (err)
6909 0ebf8283 2019-07-24 stsp goto done;
6910 0ebf8283 2019-07-24 stsp
6911 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6912 0ebf8283 2019-07-24 stsp if (err)
6913 0ebf8283 2019-07-24 stsp goto done;
6914 0ebf8283 2019-07-24 stsp
6915 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6916 0ebf8283 2019-07-24 stsp if (err)
6917 0ebf8283 2019-07-24 stsp goto done;
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6920 0ebf8283 2019-07-24 stsp worktree);
6921 0ebf8283 2019-07-24 stsp if (err)
6922 0ebf8283 2019-07-24 stsp goto done;
6923 0ebf8283 2019-07-24 stsp
6924 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6925 0ebf8283 2019-07-24 stsp 0);
6926 0ebf8283 2019-07-24 stsp if (err)
6927 0ebf8283 2019-07-24 stsp goto done;
6928 0ebf8283 2019-07-24 stsp
6929 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp
6933 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6934 0ebf8283 2019-07-24 stsp if (err)
6935 0ebf8283 2019-07-24 stsp goto done;
6936 0ebf8283 2019-07-24 stsp
6937 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6938 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6939 0ebf8283 2019-07-24 stsp if (err)
6940 0ebf8283 2019-07-24 stsp goto done;
6941 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6942 0ebf8283 2019-07-24 stsp if (err)
6943 0ebf8283 2019-07-24 stsp goto done;
6944 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6945 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6946 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6947 0ebf8283 2019-07-24 stsp goto done;
6948 0ebf8283 2019-07-24 stsp }
6949 0ebf8283 2019-07-24 stsp
6950 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6951 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6952 0ebf8283 2019-07-24 stsp if (err)
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6955 0ebf8283 2019-07-24 stsp if (err)
6956 0ebf8283 2019-07-24 stsp goto done;
6957 0ebf8283 2019-07-24 stsp
6958 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6959 0ebf8283 2019-07-24 stsp if (err)
6960 0ebf8283 2019-07-24 stsp goto done;
6961 0ebf8283 2019-07-24 stsp done:
6962 0ebf8283 2019-07-24 stsp free(fileindex_path);
6963 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6964 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6965 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6966 0ebf8283 2019-07-24 stsp if (wt_branch)
6967 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6968 0ebf8283 2019-07-24 stsp if (err) {
6969 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6970 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6971 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6972 0ebf8283 2019-07-24 stsp }
6973 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6974 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6975 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6976 0ebf8283 2019-07-24 stsp }
6977 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6978 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6979 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6980 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6981 3e3a69f1 2019-07-25 stsp }
6982 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6983 0ebf8283 2019-07-24 stsp }
6984 0ebf8283 2019-07-24 stsp return err;
6985 0ebf8283 2019-07-24 stsp }
6986 0ebf8283 2019-07-24 stsp
6987 0ebf8283 2019-07-24 stsp const struct got_error *
6988 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6989 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6990 0ebf8283 2019-07-24 stsp {
6991 3e3a69f1 2019-07-25 stsp if (fileindex)
6992 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6993 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6994 0ebf8283 2019-07-24 stsp }
6995 0ebf8283 2019-07-24 stsp
6996 0ebf8283 2019-07-24 stsp const struct got_error *
6997 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6998 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6999 0ebf8283 2019-07-24 stsp {
7000 0ebf8283 2019-07-24 stsp const struct got_error *err;
7001 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7002 0ebf8283 2019-07-24 stsp
7003 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 0ebf8283 2019-07-24 stsp if (err)
7005 0ebf8283 2019-07-24 stsp return err;
7006 0ebf8283 2019-07-24 stsp
7007 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7009 0ebf8283 2019-07-24 stsp return NULL;
7010 0ebf8283 2019-07-24 stsp }
7011 0ebf8283 2019-07-24 stsp
7012 0ebf8283 2019-07-24 stsp const struct got_error *
7013 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7014 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7015 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7016 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7017 0ebf8283 2019-07-24 stsp {
7018 0ebf8283 2019-07-24 stsp const struct got_error *err;
7019 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7020 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7021 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7022 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7023 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7024 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7027 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7028 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7029 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7030 0ebf8283 2019-07-24 stsp
7031 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7032 3e3a69f1 2019-07-25 stsp if (err)
7033 3e3a69f1 2019-07-25 stsp return err;
7034 3e3a69f1 2019-07-25 stsp
7035 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7036 3e3a69f1 2019-07-25 stsp if (err)
7037 f032f1f7 2019-08-04 stsp goto done;
7038 f032f1f7 2019-08-04 stsp
7039 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7040 f032f1f7 2019-08-04 stsp &have_staged_files);
7041 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7042 f032f1f7 2019-08-04 stsp goto done;
7043 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7044 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7045 3e3a69f1 2019-07-25 stsp goto done;
7046 f032f1f7 2019-08-04 stsp }
7047 3e3a69f1 2019-07-25 stsp
7048 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7049 0ebf8283 2019-07-24 stsp if (err)
7050 f032f1f7 2019-08-04 stsp goto done;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7053 0ebf8283 2019-07-24 stsp if (err)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp
7056 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7057 0ebf8283 2019-07-24 stsp if (err)
7058 0ebf8283 2019-07-24 stsp goto done;
7059 0ebf8283 2019-07-24 stsp
7060 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7061 0ebf8283 2019-07-24 stsp worktree);
7062 0ebf8283 2019-07-24 stsp if (err)
7063 0ebf8283 2019-07-24 stsp goto done;
7064 0ebf8283 2019-07-24 stsp
7065 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7066 0ebf8283 2019-07-24 stsp if (err)
7067 0ebf8283 2019-07-24 stsp goto done;
7068 0ebf8283 2019-07-24 stsp
7069 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7070 0ebf8283 2019-07-24 stsp if (err)
7071 0ebf8283 2019-07-24 stsp goto done;
7072 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7073 0ebf8283 2019-07-24 stsp if (err)
7074 0ebf8283 2019-07-24 stsp goto done;
7075 0ebf8283 2019-07-24 stsp
7076 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7077 0ebf8283 2019-07-24 stsp if (err)
7078 0ebf8283 2019-07-24 stsp goto done;
7079 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7080 0ebf8283 2019-07-24 stsp if (err)
7081 0ebf8283 2019-07-24 stsp goto done;
7082 0ebf8283 2019-07-24 stsp
7083 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7084 0ebf8283 2019-07-24 stsp if (err)
7085 0ebf8283 2019-07-24 stsp goto done;
7086 0ebf8283 2019-07-24 stsp done:
7087 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7088 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7089 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7090 0ebf8283 2019-07-24 stsp if (commit_ref)
7091 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7092 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7093 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7094 0ebf8283 2019-07-24 stsp if (err) {
7095 0ebf8283 2019-07-24 stsp free(*commit_id);
7096 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7097 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7098 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7099 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7100 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7101 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7102 0ebf8283 2019-07-24 stsp }
7103 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7104 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7105 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7106 3e3a69f1 2019-07-25 stsp }
7107 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7108 0ebf8283 2019-07-24 stsp }
7109 0ebf8283 2019-07-24 stsp return err;
7110 0ebf8283 2019-07-24 stsp }
7111 0ebf8283 2019-07-24 stsp
7112 0ebf8283 2019-07-24 stsp static const struct got_error *
7113 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7114 0ebf8283 2019-07-24 stsp {
7115 0ebf8283 2019-07-24 stsp const struct got_error *err;
7116 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7117 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7118 0ebf8283 2019-07-24 stsp
7119 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7120 0ebf8283 2019-07-24 stsp if (err)
7121 0ebf8283 2019-07-24 stsp goto done;
7122 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7123 0ebf8283 2019-07-24 stsp if (err)
7124 0ebf8283 2019-07-24 stsp goto done;
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7127 0ebf8283 2019-07-24 stsp worktree);
7128 0ebf8283 2019-07-24 stsp if (err)
7129 0ebf8283 2019-07-24 stsp goto done;
7130 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7131 0ebf8283 2019-07-24 stsp if (err)
7132 0ebf8283 2019-07-24 stsp goto done;
7133 0ebf8283 2019-07-24 stsp
7134 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7135 0ebf8283 2019-07-24 stsp if (err)
7136 0ebf8283 2019-07-24 stsp goto done;
7137 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7138 0ebf8283 2019-07-24 stsp if (err)
7139 0ebf8283 2019-07-24 stsp goto done;
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp goto done;
7144 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7145 0ebf8283 2019-07-24 stsp if (err)
7146 0ebf8283 2019-07-24 stsp goto done;
7147 0ebf8283 2019-07-24 stsp done:
7148 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7149 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7150 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7151 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7152 0ebf8283 2019-07-24 stsp return err;
7153 0ebf8283 2019-07-24 stsp }
7154 0ebf8283 2019-07-24 stsp
7155 0ebf8283 2019-07-24 stsp const struct got_error *
7156 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7157 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7158 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7159 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7160 0ebf8283 2019-07-24 stsp {
7161 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7162 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7163 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7164 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7165 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7166 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7169 0ebf8283 2019-07-24 stsp if (err)
7170 0ebf8283 2019-07-24 stsp return err;
7171 0ebf8283 2019-07-24 stsp
7172 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7173 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7174 a44927cc 2022-04-07 stsp if (err)
7175 a44927cc 2022-04-07 stsp goto done;
7176 a44927cc 2022-04-07 stsp
7177 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7178 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7179 0ebf8283 2019-07-24 stsp if (err)
7180 0ebf8283 2019-07-24 stsp goto done;
7181 0ebf8283 2019-07-24 stsp
7182 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7183 0ebf8283 2019-07-24 stsp if (err)
7184 0ebf8283 2019-07-24 stsp goto done;
7185 0ebf8283 2019-07-24 stsp
7186 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7187 0ebf8283 2019-07-24 stsp if (err)
7188 0ebf8283 2019-07-24 stsp goto done;
7189 0ebf8283 2019-07-24 stsp
7190 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7191 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7192 0ebf8283 2019-07-24 stsp if (err)
7193 0ebf8283 2019-07-24 stsp goto done;
7194 0ebf8283 2019-07-24 stsp
7195 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7196 0ebf8283 2019-07-24 stsp if (err)
7197 0ebf8283 2019-07-24 stsp goto done;
7198 0ebf8283 2019-07-24 stsp
7199 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7200 0ebf8283 2019-07-24 stsp if (err)
7201 0ebf8283 2019-07-24 stsp goto done;
7202 0ebf8283 2019-07-24 stsp
7203 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7204 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7205 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7206 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7207 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7208 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7209 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7210 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7211 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7212 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7213 0ebf8283 2019-07-24 stsp if (err)
7214 1f1abb7e 2019-08-08 stsp goto sync;
7215 0ebf8283 2019-07-24 stsp
7216 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7217 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7218 0ebf8283 2019-07-24 stsp sync:
7219 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7220 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7221 0ebf8283 2019-07-24 stsp err = sync_err;
7222 0ebf8283 2019-07-24 stsp done:
7223 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7224 0ebf8283 2019-07-24 stsp free(tree_id);
7225 818c7501 2019-07-11 stsp free(fileindex_path);
7226 818c7501 2019-07-11 stsp
7227 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7228 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7229 818c7501 2019-07-11 stsp err = unlockerr;
7230 818c7501 2019-07-11 stsp return err;
7231 818c7501 2019-07-11 stsp }
7232 0ebf8283 2019-07-24 stsp
7233 0ebf8283 2019-07-24 stsp const struct got_error *
7234 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7235 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7236 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7237 0ebf8283 2019-07-24 stsp {
7238 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7239 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7240 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7241 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7242 0ebf8283 2019-07-24 stsp
7243 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7244 0ebf8283 2019-07-24 stsp if (err)
7245 0ebf8283 2019-07-24 stsp return err;
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7248 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7249 e600f124 2021-03-21 stsp if (err)
7250 e600f124 2021-03-21 stsp goto done;
7251 e600f124 2021-03-21 stsp
7252 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7253 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7254 0ebf8283 2019-07-24 stsp if (err)
7255 0ebf8283 2019-07-24 stsp goto done;
7256 0ebf8283 2019-07-24 stsp
7257 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7258 0ebf8283 2019-07-24 stsp if (err)
7259 0ebf8283 2019-07-24 stsp goto done;
7260 0ebf8283 2019-07-24 stsp
7261 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7262 0ebf8283 2019-07-24 stsp if (err)
7263 0ebf8283 2019-07-24 stsp goto done;
7264 0ebf8283 2019-07-24 stsp
7265 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7266 0ebf8283 2019-07-24 stsp if (err)
7267 0ebf8283 2019-07-24 stsp goto done;
7268 0ebf8283 2019-07-24 stsp
7269 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7270 a615e0e7 2020-12-16 stsp if (err)
7271 a615e0e7 2020-12-16 stsp goto done;
7272 a615e0e7 2020-12-16 stsp
7273 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7274 a615e0e7 2020-12-16 stsp if (err)
7275 a615e0e7 2020-12-16 stsp goto done;
7276 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7277 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7278 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7279 a615e0e7 2020-12-16 stsp err = sync_err;
7280 0ebf8283 2019-07-24 stsp done:
7281 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7282 a615e0e7 2020-12-16 stsp free(fileindex_path);
7283 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7284 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7285 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7286 0ebf8283 2019-07-24 stsp err = unlockerr;
7287 0ebf8283 2019-07-24 stsp return err;
7288 0ebf8283 2019-07-24 stsp }
7289 0ebf8283 2019-07-24 stsp
7290 0ebf8283 2019-07-24 stsp const struct got_error *
7291 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7292 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7293 0ebf8283 2019-07-24 stsp {
7294 0ebf8283 2019-07-24 stsp const struct got_error *err;
7295 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7296 0ebf8283 2019-07-24 stsp
7297 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7298 0ebf8283 2019-07-24 stsp if (err)
7299 0ebf8283 2019-07-24 stsp return err;
7300 0ebf8283 2019-07-24 stsp
7301 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7302 0ebf8283 2019-07-24 stsp if (err)
7303 0ebf8283 2019-07-24 stsp goto done;
7304 0ebf8283 2019-07-24 stsp
7305 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7306 0ebf8283 2019-07-24 stsp done:
7307 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7308 2822a352 2019-10-15 stsp return err;
7309 2822a352 2019-10-15 stsp }
7310 2822a352 2019-10-15 stsp
7311 2822a352 2019-10-15 stsp const struct got_error *
7312 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7313 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7314 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7315 2822a352 2019-10-15 stsp struct got_repository *repo)
7316 2822a352 2019-10-15 stsp {
7317 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7318 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7319 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7320 2822a352 2019-10-15 stsp
7321 2822a352 2019-10-15 stsp *fileindex = NULL;
7322 2822a352 2019-10-15 stsp *branch_ref = NULL;
7323 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7324 2822a352 2019-10-15 stsp
7325 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7326 2822a352 2019-10-15 stsp if (err)
7327 2822a352 2019-10-15 stsp return err;
7328 2822a352 2019-10-15 stsp
7329 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7330 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7331 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7332 2822a352 2019-10-15 stsp "update -b or different branch name required");
7333 2822a352 2019-10-15 stsp goto done;
7334 2822a352 2019-10-15 stsp }
7335 2822a352 2019-10-15 stsp
7336 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7337 2822a352 2019-10-15 stsp if (err)
7338 2822a352 2019-10-15 stsp goto done;
7339 2822a352 2019-10-15 stsp
7340 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7341 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7342 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7343 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7344 2822a352 2019-10-15 stsp &ok_arg);
7345 2822a352 2019-10-15 stsp if (err)
7346 2822a352 2019-10-15 stsp goto done;
7347 2822a352 2019-10-15 stsp
7348 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7349 2822a352 2019-10-15 stsp if (err)
7350 2822a352 2019-10-15 stsp goto done;
7351 2822a352 2019-10-15 stsp
7352 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7353 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7354 2822a352 2019-10-15 stsp done:
7355 2822a352 2019-10-15 stsp if (err) {
7356 2822a352 2019-10-15 stsp if (*branch_ref) {
7357 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7358 2822a352 2019-10-15 stsp *branch_ref = NULL;
7359 2822a352 2019-10-15 stsp }
7360 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7361 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7362 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7363 2822a352 2019-10-15 stsp }
7364 2822a352 2019-10-15 stsp if (*fileindex) {
7365 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7366 2822a352 2019-10-15 stsp *fileindex = NULL;
7367 2822a352 2019-10-15 stsp }
7368 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7369 2822a352 2019-10-15 stsp }
7370 0cb83759 2019-08-03 stsp return err;
7371 0cb83759 2019-08-03 stsp }
7372 0cb83759 2019-08-03 stsp
7373 2822a352 2019-10-15 stsp const struct got_error *
7374 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7375 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7376 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7377 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7378 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7379 2822a352 2019-10-15 stsp {
7380 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7381 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7382 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7383 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7384 2822a352 2019-10-15 stsp
7385 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7386 2822a352 2019-10-15 stsp if (err)
7387 2822a352 2019-10-15 stsp goto done;
7388 2822a352 2019-10-15 stsp
7389 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7390 2822a352 2019-10-15 stsp if (err)
7391 2822a352 2019-10-15 stsp goto done;
7392 2822a352 2019-10-15 stsp
7393 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7394 a44927cc 2022-04-07 stsp if (err)
7395 a44927cc 2022-04-07 stsp goto done;
7396 a44927cc 2022-04-07 stsp
7397 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7398 2822a352 2019-10-15 stsp worktree->path_prefix);
7399 2822a352 2019-10-15 stsp if (err)
7400 2822a352 2019-10-15 stsp goto done;
7401 2822a352 2019-10-15 stsp
7402 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7403 2822a352 2019-10-15 stsp if (err)
7404 2822a352 2019-10-15 stsp goto done;
7405 2822a352 2019-10-15 stsp
7406 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7407 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7408 2822a352 2019-10-15 stsp if (err)
7409 2822a352 2019-10-15 stsp goto sync;
7410 2822a352 2019-10-15 stsp
7411 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7412 2822a352 2019-10-15 stsp if (err)
7413 2822a352 2019-10-15 stsp goto sync;
7414 2822a352 2019-10-15 stsp
7415 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7416 6e210706 2021-01-22 stsp if (err)
7417 6e210706 2021-01-22 stsp goto sync;
7418 6e210706 2021-01-22 stsp
7419 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7420 2822a352 2019-10-15 stsp sync:
7421 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7422 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7423 2822a352 2019-10-15 stsp err = sync_err;
7424 2822a352 2019-10-15 stsp
7425 2822a352 2019-10-15 stsp done:
7426 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7427 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7428 2822a352 2019-10-15 stsp err = unlockerr;
7429 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7430 2822a352 2019-10-15 stsp
7431 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7432 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7433 2822a352 2019-10-15 stsp err = unlockerr;
7434 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7435 2822a352 2019-10-15 stsp
7436 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7437 2822a352 2019-10-15 stsp free(fileindex_path);
7438 2822a352 2019-10-15 stsp free(tree_id);
7439 a44927cc 2022-04-07 stsp if (commit)
7440 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7441 2822a352 2019-10-15 stsp
7442 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7443 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7444 2822a352 2019-10-15 stsp err = unlockerr;
7445 2822a352 2019-10-15 stsp return err;
7446 2822a352 2019-10-15 stsp }
7447 2822a352 2019-10-15 stsp
7448 2822a352 2019-10-15 stsp const struct got_error *
7449 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7450 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7451 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7452 2822a352 2019-10-15 stsp {
7453 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7454 8b692cd0 2019-10-21 stsp
7455 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7456 8b692cd0 2019-10-21 stsp
7457 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7458 8b692cd0 2019-10-21 stsp
7459 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7460 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7461 8b692cd0 2019-10-21 stsp err = unlockerr;
7462 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7463 8b692cd0 2019-10-21 stsp
7464 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7465 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7466 8b692cd0 2019-10-21 stsp err = unlockerr;
7467 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7468 f259c4c1 2021-09-24 stsp
7469 f259c4c1 2021-09-24 stsp return err;
7470 f259c4c1 2021-09-24 stsp }
7471 f259c4c1 2021-09-24 stsp
7472 f259c4c1 2021-09-24 stsp const struct got_error *
7473 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7474 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7475 f259c4c1 2021-09-24 stsp {
7476 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7477 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7478 f259c4c1 2021-09-24 stsp
7479 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7480 f259c4c1 2021-09-24 stsp if (err)
7481 f259c4c1 2021-09-24 stsp goto done;
7482 8b692cd0 2019-10-21 stsp
7483 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7484 f259c4c1 2021-09-24 stsp
7485 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7486 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7487 f259c4c1 2021-09-24 stsp err = sync_err;
7488 f259c4c1 2021-09-24 stsp done:
7489 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7490 f259c4c1 2021-09-24 stsp free(fileindex_path);
7491 8b692cd0 2019-10-21 stsp return err;
7492 2822a352 2019-10-15 stsp }
7493 2822a352 2019-10-15 stsp
7494 f259c4c1 2021-09-24 stsp static const struct got_error *
7495 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7496 f259c4c1 2021-09-24 stsp {
7497 f259c4c1 2021-09-24 stsp const struct got_error *err;
7498 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7499 f259c4c1 2021-09-24 stsp
7500 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7501 f259c4c1 2021-09-24 stsp if (err)
7502 f259c4c1 2021-09-24 stsp goto done;
7503 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7504 f259c4c1 2021-09-24 stsp if (err)
7505 f259c4c1 2021-09-24 stsp goto done;
7506 f259c4c1 2021-09-24 stsp
7507 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7508 f259c4c1 2021-09-24 stsp if (err)
7509 f259c4c1 2021-09-24 stsp goto done;
7510 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7511 f259c4c1 2021-09-24 stsp if (err)
7512 f259c4c1 2021-09-24 stsp goto done;
7513 f259c4c1 2021-09-24 stsp
7514 f259c4c1 2021-09-24 stsp done:
7515 f259c4c1 2021-09-24 stsp free(branch_refname);
7516 f259c4c1 2021-09-24 stsp free(commit_refname);
7517 f259c4c1 2021-09-24 stsp return err;
7518 f259c4c1 2021-09-24 stsp }
7519 f259c4c1 2021-09-24 stsp
7520 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7521 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7522 f259c4c1 2021-09-24 stsp const char *branch_name;
7523 f259c4c1 2021-09-24 stsp };
7524 f259c4c1 2021-09-24 stsp
7525 f259c4c1 2021-09-24 stsp static const struct got_error *
7526 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7527 f259c4c1 2021-09-24 stsp void *arg)
7528 f259c4c1 2021-09-24 stsp {
7529 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7530 f259c4c1 2021-09-24 stsp
7531 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7532 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7533 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7534 f259c4c1 2021-09-24 stsp
7535 f259c4c1 2021-09-24 stsp return NULL;
7536 f259c4c1 2021-09-24 stsp }
7537 f259c4c1 2021-09-24 stsp
7538 f259c4c1 2021-09-24 stsp
7539 f259c4c1 2021-09-24 stsp const struct got_error *
7540 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7541 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7542 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7543 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7544 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7545 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7546 f259c4c1 2021-09-24 stsp {
7547 f259c4c1 2021-09-24 stsp const struct got_error *err;
7548 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7549 f259c4c1 2021-09-24 stsp
7550 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7551 f259c4c1 2021-09-24 stsp if (err)
7552 f259c4c1 2021-09-24 stsp goto done;
7553 f259c4c1 2021-09-24 stsp
7554 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7555 f259c4c1 2021-09-24 stsp worktree);
7556 f259c4c1 2021-09-24 stsp if (err)
7557 f259c4c1 2021-09-24 stsp goto done;
7558 f259c4c1 2021-09-24 stsp
7559 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7560 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7561 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7562 f259c4c1 2021-09-24 stsp done:
7563 f259c4c1 2021-09-24 stsp free(fileindex_path);
7564 f259c4c1 2021-09-24 stsp return err;
7565 f259c4c1 2021-09-24 stsp }
7566 f259c4c1 2021-09-24 stsp
7567 f259c4c1 2021-09-24 stsp const struct got_error *
7568 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7569 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7570 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7571 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7572 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7573 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7574 0ff8d236 2021-09-28 stsp
7575 f259c4c1 2021-09-24 stsp {
7576 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7577 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7578 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7579 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7580 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7581 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7582 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7583 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7584 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7585 f259c4c1 2021-09-24 stsp
7586 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7587 f259c4c1 2021-09-24 stsp
7588 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7589 f259c4c1 2021-09-24 stsp
7590 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7591 f259c4c1 2021-09-24 stsp if (err)
7592 f259c4c1 2021-09-24 stsp goto done;
7593 f259c4c1 2021-09-24 stsp
7594 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7595 f259c4c1 2021-09-24 stsp if (err)
7596 f259c4c1 2021-09-24 stsp goto done;
7597 f259c4c1 2021-09-24 stsp
7598 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7599 f259c4c1 2021-09-24 stsp if (err)
7600 f259c4c1 2021-09-24 stsp goto done;
7601 f259c4c1 2021-09-24 stsp
7602 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7603 f259c4c1 2021-09-24 stsp &have_staged_files);
7604 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7605 f259c4c1 2021-09-24 stsp goto done;
7606 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7607 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7608 f259c4c1 2021-09-24 stsp goto done;
7609 f259c4c1 2021-09-24 stsp }
7610 f259c4c1 2021-09-24 stsp
7611 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7612 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7613 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7614 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7615 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7616 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7617 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7618 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7619 f259c4c1 2021-09-24 stsp if (err)
7620 f259c4c1 2021-09-24 stsp goto done;
7621 f259c4c1 2021-09-24 stsp
7622 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7623 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7624 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7625 f259c4c1 2021-09-24 stsp goto done;
7626 f259c4c1 2021-09-24 stsp }
7627 f259c4c1 2021-09-24 stsp
7628 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7629 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7630 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7631 f259c4c1 2021-09-24 stsp
7632 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7633 f259c4c1 2021-09-24 stsp ct_path++;
7634 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7635 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7636 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7637 f259c4c1 2021-09-24 stsp if (err)
7638 f259c4c1 2021-09-24 stsp goto done;
7639 f259c4c1 2021-09-24 stsp
7640 f259c4c1 2021-09-24 stsp }
7641 f259c4c1 2021-09-24 stsp
7642 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7643 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7644 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7645 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7646 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7647 f259c4c1 2021-09-24 stsp if (err)
7648 f259c4c1 2021-09-24 stsp goto done;
7649 f259c4c1 2021-09-24 stsp
7650 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7651 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7652 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7653 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7654 f259c4c1 2021-09-24 stsp err = sync_err;
7655 f259c4c1 2021-09-24 stsp done:
7656 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7657 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7658 f259c4c1 2021-09-24 stsp free_commitable(ct);
7659 f259c4c1 2021-09-24 stsp }
7660 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7661 f259c4c1 2021-09-24 stsp free(fileindex_path);
7662 f259c4c1 2021-09-24 stsp return err;
7663 f259c4c1 2021-09-24 stsp }
7664 f259c4c1 2021-09-24 stsp
7665 f259c4c1 2021-09-24 stsp const struct got_error *
7666 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7667 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7668 f259c4c1 2021-09-24 stsp {
7669 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7670 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7671 f259c4c1 2021-09-24 stsp
7672 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7673 f259c4c1 2021-09-24 stsp if (err)
7674 f259c4c1 2021-09-24 stsp goto done;
7675 f259c4c1 2021-09-24 stsp
7676 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7677 f259c4c1 2021-09-24 stsp if (err)
7678 f259c4c1 2021-09-24 stsp goto done;
7679 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7680 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7681 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7682 f259c4c1 2021-09-24 stsp err = sync_err;
7683 f259c4c1 2021-09-24 stsp done:
7684 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7685 f259c4c1 2021-09-24 stsp free(fileindex_path);
7686 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7687 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7688 f259c4c1 2021-09-24 stsp err = unlockerr;
7689 f259c4c1 2021-09-24 stsp return err;
7690 f259c4c1 2021-09-24 stsp }
7691 f259c4c1 2021-09-24 stsp
7692 f259c4c1 2021-09-24 stsp const struct got_error *
7693 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7694 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7695 f259c4c1 2021-09-24 stsp {
7696 f259c4c1 2021-09-24 stsp const struct got_error *err;
7697 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7698 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7699 f259c4c1 2021-09-24 stsp
7700 f259c4c1 2021-09-24 stsp *in_progress = 0;
7701 f259c4c1 2021-09-24 stsp
7702 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7703 f259c4c1 2021-09-24 stsp if (err)
7704 f259c4c1 2021-09-24 stsp return err;
7705 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7706 793fcac3 2021-09-24 stsp free(branch_refname);
7707 f259c4c1 2021-09-24 stsp if (err) {
7708 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7709 f259c4c1 2021-09-24 stsp return err;
7710 f259c4c1 2021-09-24 stsp } else
7711 f259c4c1 2021-09-24 stsp *in_progress = 1;
7712 f259c4c1 2021-09-24 stsp
7713 f259c4c1 2021-09-24 stsp return NULL;
7714 f259c4c1 2021-09-24 stsp }
7715 f259c4c1 2021-09-24 stsp
7716 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7717 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7718 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7719 f259c4c1 2021-09-24 stsp {
7720 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7721 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7722 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7723 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7724 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7725 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7726 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7727 f259c4c1 2021-09-24 stsp
7728 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7729 f259c4c1 2021-09-24 stsp
7730 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7731 f259c4c1 2021-09-24 stsp if (err)
7732 f259c4c1 2021-09-24 stsp return err;
7733 f259c4c1 2021-09-24 stsp
7734 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7735 f259c4c1 2021-09-24 stsp if (err)
7736 f259c4c1 2021-09-24 stsp goto done;
7737 f259c4c1 2021-09-24 stsp
7738 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7739 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7740 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7741 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7742 f259c4c1 2021-09-24 stsp &ok_arg);
7743 f259c4c1 2021-09-24 stsp if (err)
7744 f259c4c1 2021-09-24 stsp goto done;
7745 f259c4c1 2021-09-24 stsp
7746 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7747 f259c4c1 2021-09-24 stsp if (err)
7748 f259c4c1 2021-09-24 stsp return err;
7749 f259c4c1 2021-09-24 stsp
7750 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7751 f259c4c1 2021-09-24 stsp if (err)
7752 f259c4c1 2021-09-24 stsp return err;
7753 f259c4c1 2021-09-24 stsp
7754 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7755 f259c4c1 2021-09-24 stsp 0);
7756 f259c4c1 2021-09-24 stsp if (err)
7757 f259c4c1 2021-09-24 stsp goto done;
7758 f259c4c1 2021-09-24 stsp
7759 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7760 f259c4c1 2021-09-24 stsp if (err)
7761 f259c4c1 2021-09-24 stsp goto done;
7762 f259c4c1 2021-09-24 stsp
7763 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7764 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7765 f259c4c1 2021-09-24 stsp goto done;
7766 f259c4c1 2021-09-24 stsp }
7767 f259c4c1 2021-09-24 stsp
7768 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7769 f259c4c1 2021-09-24 stsp if (err)
7770 f259c4c1 2021-09-24 stsp goto done;
7771 f259c4c1 2021-09-24 stsp
7772 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7773 f259c4c1 2021-09-24 stsp if (err)
7774 f259c4c1 2021-09-24 stsp goto done;
7775 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7776 f259c4c1 2021-09-24 stsp if (err)
7777 f259c4c1 2021-09-24 stsp goto done;
7778 f259c4c1 2021-09-24 stsp
7779 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7780 f259c4c1 2021-09-24 stsp if (err)
7781 f259c4c1 2021-09-24 stsp goto done;
7782 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
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 done:
7787 f259c4c1 2021-09-24 stsp free(branch_refname);
7788 f259c4c1 2021-09-24 stsp free(commit_refname);
7789 f259c4c1 2021-09-24 stsp free(fileindex_path);
7790 f259c4c1 2021-09-24 stsp if (branch_ref)
7791 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7792 f259c4c1 2021-09-24 stsp if (commit_ref)
7793 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7794 f259c4c1 2021-09-24 stsp if (wt_branch)
7795 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7796 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7797 f259c4c1 2021-09-24 stsp if (err) {
7798 f259c4c1 2021-09-24 stsp if (*fileindex) {
7799 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7800 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7801 f259c4c1 2021-09-24 stsp }
7802 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7803 f259c4c1 2021-09-24 stsp }
7804 f259c4c1 2021-09-24 stsp return err;
7805 f259c4c1 2021-09-24 stsp }
7806 f259c4c1 2021-09-24 stsp
7807 f259c4c1 2021-09-24 stsp const struct got_error *
7808 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7809 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7810 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7811 f259c4c1 2021-09-24 stsp {
7812 f259c4c1 2021-09-24 stsp const struct got_error *err;
7813 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7814 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7815 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7816 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7817 f259c4c1 2021-09-24 stsp
7818 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7819 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7820 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7821 f259c4c1 2021-09-24 stsp
7822 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7823 f259c4c1 2021-09-24 stsp if (err)
7824 f259c4c1 2021-09-24 stsp return err;
7825 f259c4c1 2021-09-24 stsp
7826 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7827 f259c4c1 2021-09-24 stsp if (err)
7828 f259c4c1 2021-09-24 stsp goto done;
7829 f259c4c1 2021-09-24 stsp
7830 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7831 f259c4c1 2021-09-24 stsp &have_staged_files);
7832 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7833 f259c4c1 2021-09-24 stsp goto done;
7834 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7835 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7836 f259c4c1 2021-09-24 stsp goto done;
7837 f259c4c1 2021-09-24 stsp }
7838 f259c4c1 2021-09-24 stsp
7839 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7840 f259c4c1 2021-09-24 stsp if (err)
7841 f259c4c1 2021-09-24 stsp goto done;
7842 f259c4c1 2021-09-24 stsp
7843 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7844 f259c4c1 2021-09-24 stsp if (err)
7845 f259c4c1 2021-09-24 stsp goto done;
7846 f259c4c1 2021-09-24 stsp
7847 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7848 f259c4c1 2021-09-24 stsp if (err)
7849 f259c4c1 2021-09-24 stsp goto done;
7850 f259c4c1 2021-09-24 stsp
7851 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7852 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7853 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7854 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7855 f259c4c1 2021-09-24 stsp goto done;
7856 f259c4c1 2021-09-24 stsp }
7857 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7858 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7859 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7860 f259c4c1 2021-09-24 stsp goto done;
7861 f259c4c1 2021-09-24 stsp }
7862 f259c4c1 2021-09-24 stsp
7863 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7864 f259c4c1 2021-09-24 stsp if (err)
7865 f259c4c1 2021-09-24 stsp goto done;
7866 f259c4c1 2021-09-24 stsp
7867 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7868 f259c4c1 2021-09-24 stsp if (err)
7869 f259c4c1 2021-09-24 stsp goto done;
7870 f259c4c1 2021-09-24 stsp done:
7871 f259c4c1 2021-09-24 stsp free(commit_refname);
7872 f259c4c1 2021-09-24 stsp free(branch_refname);
7873 f259c4c1 2021-09-24 stsp free(fileindex_path);
7874 f259c4c1 2021-09-24 stsp if (commit_ref)
7875 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7876 f259c4c1 2021-09-24 stsp if (branch_ref)
7877 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7878 f259c4c1 2021-09-24 stsp if (err) {
7879 f259c4c1 2021-09-24 stsp if (*branch_name) {
7880 f259c4c1 2021-09-24 stsp free(*branch_name);
7881 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7882 f259c4c1 2021-09-24 stsp }
7883 f259c4c1 2021-09-24 stsp free(*branch_tip);
7884 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7885 f259c4c1 2021-09-24 stsp if (*fileindex) {
7886 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7887 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7888 f259c4c1 2021-09-24 stsp }
7889 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7890 f259c4c1 2021-09-24 stsp }
7891 f259c4c1 2021-09-24 stsp return err;
7892 f259c4c1 2021-09-24 stsp }
7893 f259c4c1 2021-09-24 stsp
7894 f259c4c1 2021-09-24 stsp const struct got_error *
7895 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7896 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7897 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7898 f259c4c1 2021-09-24 stsp {
7899 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7900 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7901 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7902 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7903 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7904 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7905 f259c4c1 2021-09-24 stsp
7906 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7907 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7908 a44927cc 2022-04-07 stsp if (err)
7909 a44927cc 2022-04-07 stsp goto done;
7910 a44927cc 2022-04-07 stsp
7911 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7912 a44927cc 2022-04-07 stsp worktree->path_prefix);
7913 f259c4c1 2021-09-24 stsp if (err)
7914 f259c4c1 2021-09-24 stsp goto done;
7915 f259c4c1 2021-09-24 stsp
7916 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7917 f259c4c1 2021-09-24 stsp if (err)
7918 f259c4c1 2021-09-24 stsp goto done;
7919 f259c4c1 2021-09-24 stsp
7920 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7921 f259c4c1 2021-09-24 stsp if (err)
7922 f259c4c1 2021-09-24 stsp goto done;
7923 f259c4c1 2021-09-24 stsp
7924 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7925 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7926 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7927 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7928 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7929 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7930 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7931 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7932 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7933 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7934 f259c4c1 2021-09-24 stsp if (err)
7935 f259c4c1 2021-09-24 stsp goto sync;
7936 f259c4c1 2021-09-24 stsp
7937 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7938 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7939 f259c4c1 2021-09-24 stsp sync:
7940 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7941 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7942 f259c4c1 2021-09-24 stsp err = sync_err;
7943 f259c4c1 2021-09-24 stsp done:
7944 f259c4c1 2021-09-24 stsp free(tree_id);
7945 f259c4c1 2021-09-24 stsp free(commit_id);
7946 a44927cc 2022-04-07 stsp if (commit)
7947 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7948 f259c4c1 2021-09-24 stsp if (fileindex)
7949 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7950 f259c4c1 2021-09-24 stsp free(fileindex_path);
7951 f259c4c1 2021-09-24 stsp
7952 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7953 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7954 f259c4c1 2021-09-24 stsp err = unlockerr;
7955 f259c4c1 2021-09-24 stsp return err;
7956 f259c4c1 2021-09-24 stsp }
7957 f259c4c1 2021-09-24 stsp
7958 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7959 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7960 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7961 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7962 2db2652d 2019-08-07 stsp struct got_repository *repo;
7963 2db2652d 2019-08-07 stsp int have_changes;
7964 2db2652d 2019-08-07 stsp };
7965 2db2652d 2019-08-07 stsp
7966 336075a4 2022-06-25 op static const struct got_error *
7967 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7968 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7969 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7970 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7971 735ef5ac 2019-08-03 stsp {
7972 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7973 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7974 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7975 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7976 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7977 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7978 8b13ce36 2019-08-08 stsp
7979 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7980 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7981 8b13ce36 2019-08-08 stsp return NULL;
7982 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7983 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7984 735ef5ac 2019-08-03 stsp
7985 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7986 735ef5ac 2019-08-03 stsp if (ie == NULL)
7987 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7988 735ef5ac 2019-08-03 stsp
7989 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7990 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7991 735ef5ac 2019-08-03 stsp relpath) == -1)
7992 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7993 735ef5ac 2019-08-03 stsp
7994 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7995 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7996 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7997 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7998 735ef5ac 2019-08-03 stsp }
7999 735ef5ac 2019-08-03 stsp
8000 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8001 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8002 3aa5969e 2019-08-06 stsp goto done;
8003 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8004 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8005 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8006 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8007 735ef5ac 2019-08-03 stsp goto done;
8008 3aa5969e 2019-08-06 stsp }
8009 735ef5ac 2019-08-03 stsp
8010 2db2652d 2019-08-07 stsp a->have_changes = 1;
8011 2db2652d 2019-08-07 stsp
8012 735ef5ac 2019-08-03 stsp p = in_repo_path;
8013 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8014 735ef5ac 2019-08-03 stsp p++;
8015 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8016 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8017 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8018 735ef5ac 2019-08-03 stsp done:
8019 735ef5ac 2019-08-03 stsp free(in_repo_path);
8020 dc424a06 2019-08-07 stsp return err;
8021 dc424a06 2019-08-07 stsp }
8022 dc424a06 2019-08-07 stsp
8023 2db2652d 2019-08-07 stsp struct stage_path_arg {
8024 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8025 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8026 2db2652d 2019-08-07 stsp struct got_repository *repo;
8027 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8028 2db2652d 2019-08-07 stsp void *status_arg;
8029 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8030 2db2652d 2019-08-07 stsp void *patch_arg;
8031 7b5dc508 2019-10-28 stsp int staged_something;
8032 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8033 2db2652d 2019-08-07 stsp };
8034 2db2652d 2019-08-07 stsp
8035 2db2652d 2019-08-07 stsp static const struct got_error *
8036 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8037 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8038 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8039 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8040 0cb83759 2019-08-03 stsp {
8041 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8042 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8043 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8044 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8045 0cb83759 2019-08-03 stsp uint32_t stage;
8046 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8047 0aeb8099 2020-07-23 stsp struct stat sb;
8048 8b13ce36 2019-08-08 stsp
8049 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8050 8b13ce36 2019-08-08 stsp return NULL;
8051 0cb83759 2019-08-03 stsp
8052 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8053 d3e7c587 2019-08-03 stsp if (ie == NULL)
8054 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8055 0cb83759 2019-08-03 stsp
8056 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8057 2db2652d 2019-08-07 stsp relpath)== -1)
8058 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8059 0cb83759 2019-08-03 stsp
8060 0cb83759 2019-08-03 stsp switch (status) {
8061 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8062 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8063 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8064 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8065 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8066 0aeb8099 2020-07-23 stsp break;
8067 0aeb8099 2020-07-23 stsp }
8068 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8069 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8070 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8071 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8072 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8073 dc424a06 2019-08-07 stsp if (err)
8074 dc424a06 2019-08-07 stsp break;
8075 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8076 dc424a06 2019-08-07 stsp break;
8077 dc424a06 2019-08-07 stsp } else {
8078 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8079 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8080 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8081 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8082 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8083 dc424a06 2019-08-07 stsp break;
8084 dc424a06 2019-08-07 stsp }
8085 dc424a06 2019-08-07 stsp }
8086 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8087 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8088 0cb83759 2019-08-03 stsp if (err)
8089 dc424a06 2019-08-07 stsp break;
8090 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8091 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8092 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8093 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8094 0cb83759 2019-08-03 stsp else
8095 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8096 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8097 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8098 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8099 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8100 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8101 35213c7c 2020-07-23 stsp ssize_t target_len;
8102 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8103 35213c7c 2020-07-23 stsp sizeof(target_path));
8104 35213c7c 2020-07-23 stsp if (target_len == -1) {
8105 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8106 35213c7c 2020-07-23 stsp ondisk_path);
8107 35213c7c 2020-07-23 stsp break;
8108 35213c7c 2020-07-23 stsp }
8109 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8110 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8111 35213c7c 2020-07-23 stsp a->worktree->root_path);
8112 35213c7c 2020-07-23 stsp if (err)
8113 35213c7c 2020-07-23 stsp break;
8114 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8115 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8116 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8117 35213c7c 2020-07-23 stsp break;
8118 35213c7c 2020-07-23 stsp }
8119 35213c7c 2020-07-23 stsp }
8120 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8121 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8122 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8123 35213c7c 2020-07-23 stsp else
8124 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8125 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8126 0aeb8099 2020-07-23 stsp } else {
8127 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8128 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8129 0aeb8099 2020-07-23 stsp }
8130 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8131 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8132 dc424a06 2019-08-07 stsp break;
8133 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8134 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8135 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8136 9fdde394 2022-06-04 op if (err)
8137 9fdde394 2022-06-04 op break;
8138 9fdde394 2022-06-04 op /*
8139 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8140 9fdde394 2022-06-04 op * implicitly unstage the file.
8141 9fdde394 2022-06-04 op */
8142 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8143 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8144 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8145 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8146 9fdde394 2022-06-04 op }
8147 0cb83759 2019-08-03 stsp break;
8148 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8149 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8150 d3e7c587 2019-08-03 stsp break;
8151 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8152 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8153 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8154 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8155 dc424a06 2019-08-07 stsp if (err)
8156 dc424a06 2019-08-07 stsp break;
8157 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8158 88f33a19 2019-08-08 stsp break;
8159 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8160 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8161 dc424a06 2019-08-07 stsp break;
8162 88f33a19 2019-08-08 stsp }
8163 dc424a06 2019-08-07 stsp }
8164 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8165 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8166 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8167 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8168 dc424a06 2019-08-07 stsp break;
8169 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8170 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8171 12463d8b 2019-12-13 stsp de_name);
8172 0cb83759 2019-08-03 stsp break;
8173 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8174 d3e7c587 2019-08-03 stsp break;
8175 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8176 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8177 ebf48fd5 2019-08-03 stsp break;
8178 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8179 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8180 2a06fe5f 2019-08-24 stsp break;
8181 0cb83759 2019-08-03 stsp default:
8182 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8183 537ac44b 2019-08-03 stsp break;
8184 0cb83759 2019-08-03 stsp }
8185 dc424a06 2019-08-07 stsp
8186 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8187 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8188 dc424a06 2019-08-07 stsp free(path_content);
8189 2db2652d 2019-08-07 stsp free(ondisk_path);
8190 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8191 0ebf8283 2019-07-24 stsp return err;
8192 0ebf8283 2019-07-24 stsp }
8193 0cb83759 2019-08-03 stsp
8194 0cb83759 2019-08-03 stsp const struct got_error *
8195 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8196 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8197 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8198 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8199 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8200 0cb83759 2019-08-03 stsp {
8201 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8202 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8203 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8204 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8205 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8206 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8207 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8208 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8209 0cb83759 2019-08-03 stsp
8210 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8211 0cb83759 2019-08-03 stsp if (err)
8212 0cb83759 2019-08-03 stsp return err;
8213 0cb83759 2019-08-03 stsp
8214 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8215 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8216 735ef5ac 2019-08-03 stsp if (err)
8217 735ef5ac 2019-08-03 stsp goto done;
8218 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8219 735ef5ac 2019-08-03 stsp if (err)
8220 735ef5ac 2019-08-03 stsp goto done;
8221 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8222 0cb83759 2019-08-03 stsp if (err)
8223 0cb83759 2019-08-03 stsp goto done;
8224 0cb83759 2019-08-03 stsp
8225 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8226 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8227 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8228 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8229 2db2652d 2019-08-07 stsp oka.repo = repo;
8230 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8231 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8232 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8233 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8234 735ef5ac 2019-08-03 stsp if (err)
8235 735ef5ac 2019-08-03 stsp goto done;
8236 735ef5ac 2019-08-03 stsp }
8237 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8238 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8239 2db2652d 2019-08-07 stsp goto done;
8240 2db2652d 2019-08-07 stsp }
8241 735ef5ac 2019-08-03 stsp
8242 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8243 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8244 2db2652d 2019-08-07 stsp spa.repo = repo;
8245 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8246 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8247 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8248 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8249 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8250 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8251 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8252 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8253 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8254 42005733 2019-08-03 stsp if (err)
8255 2db2652d 2019-08-07 stsp goto done;
8256 7b5dc508 2019-10-28 stsp }
8257 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8258 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8259 7b5dc508 2019-10-28 stsp goto done;
8260 0cb83759 2019-08-03 stsp }
8261 0cb83759 2019-08-03 stsp
8262 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8263 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8264 0cb83759 2019-08-03 stsp err = sync_err;
8265 0cb83759 2019-08-03 stsp done:
8266 735ef5ac 2019-08-03 stsp if (head_ref)
8267 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8268 735ef5ac 2019-08-03 stsp free(head_commit_id);
8269 0cb83759 2019-08-03 stsp free(fileindex_path);
8270 0cb83759 2019-08-03 stsp if (fileindex)
8271 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8272 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8273 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8274 0cb83759 2019-08-03 stsp err = unlockerr;
8275 0cb83759 2019-08-03 stsp return err;
8276 0cb83759 2019-08-03 stsp }
8277 ad493afc 2019-08-03 stsp
8278 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8279 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8280 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8281 ad493afc 2019-08-03 stsp struct got_repository *repo;
8282 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8283 ad493afc 2019-08-03 stsp void *progress_arg;
8284 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8285 2e1f37b0 2019-08-08 stsp void *patch_arg;
8286 ad493afc 2019-08-03 stsp };
8287 2e1f37b0 2019-08-08 stsp
8288 2e1f37b0 2019-08-08 stsp static const struct got_error *
8289 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8290 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8291 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8292 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8293 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8294 2e1f37b0 2019-08-08 stsp {
8295 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8296 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8297 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8298 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8299 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8300 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8301 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8302 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8303 2e1f37b0 2019-08-08 stsp
8304 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8305 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8306 2e1f37b0 2019-08-08 stsp
8307 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8308 2e1f37b0 2019-08-08 stsp if (err)
8309 2e1f37b0 2019-08-08 stsp return err;
8310 eb81bc23 2022-06-28 tracey
8311 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8312 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8313 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8314 eb81bc23 2022-06-28 tracey goto done;
8315 eb81bc23 2022-06-28 tracey }
8316 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8317 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8318 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8319 eb81bc23 2022-06-28 tracey goto done;
8320 eb81bc23 2022-06-28 tracey }
8321 eb81bc23 2022-06-28 tracey
8322 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8323 2e1f37b0 2019-08-08 stsp if (err)
8324 2e1f37b0 2019-08-08 stsp goto done;
8325 2e1f37b0 2019-08-08 stsp
8326 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8327 2e1f37b0 2019-08-08 stsp if (err)
8328 2e1f37b0 2019-08-08 stsp goto done;
8329 2e1f37b0 2019-08-08 stsp
8330 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8331 2e1f37b0 2019-08-08 stsp if (err)
8332 2e1f37b0 2019-08-08 stsp goto done;
8333 2e1f37b0 2019-08-08 stsp
8334 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8335 eb81bc23 2022-06-28 tracey fd2);
8336 2e1f37b0 2019-08-08 stsp if (err)
8337 2e1f37b0 2019-08-08 stsp goto done;
8338 2e1f37b0 2019-08-08 stsp
8339 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8340 2e1f37b0 2019-08-08 stsp if (err)
8341 2e1f37b0 2019-08-08 stsp goto done;
8342 ad493afc 2019-08-03 stsp
8343 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8344 2e1f37b0 2019-08-08 stsp if (err)
8345 2e1f37b0 2019-08-08 stsp goto done;
8346 2e1f37b0 2019-08-08 stsp
8347 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8348 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8349 2e1f37b0 2019-08-08 stsp if (err)
8350 2e1f37b0 2019-08-08 stsp goto done;
8351 2e1f37b0 2019-08-08 stsp
8352 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8353 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8354 2e1f37b0 2019-08-08 stsp if (err)
8355 2e1f37b0 2019-08-08 stsp goto done;
8356 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8357 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8358 2e1f37b0 2019-08-08 stsp if (err)
8359 2e1f37b0 2019-08-08 stsp goto done;
8360 2e1f37b0 2019-08-08 stsp
8361 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8362 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8363 2e1f37b0 2019-08-08 stsp goto done;
8364 2e1f37b0 2019-08-08 stsp }
8365 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8366 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8367 2e1f37b0 2019-08-08 stsp goto done;
8368 2e1f37b0 2019-08-08 stsp }
8369 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8370 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8371 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8372 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8373 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8374 fe621944 2020-11-10 stsp nchanges++;
8375 fe621944 2020-11-10 stsp }
8376 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8377 2e1f37b0 2019-08-08 stsp int choice;
8378 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8379 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8380 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8381 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8382 2e1f37b0 2019-08-08 stsp if (err)
8383 2e1f37b0 2019-08-08 stsp goto done;
8384 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8385 2e1f37b0 2019-08-08 stsp have_content = 1;
8386 19e4b907 2019-08-08 stsp else
8387 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8388 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8389 2e1f37b0 2019-08-08 stsp break;
8390 2e1f37b0 2019-08-08 stsp }
8391 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8392 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8393 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8394 2e1f37b0 2019-08-08 stsp done:
8395 2e1f37b0 2019-08-08 stsp free(label1);
8396 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8397 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8398 2e1f37b0 2019-08-08 stsp if (blob)
8399 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8400 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8401 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8402 2e1f37b0 2019-08-08 stsp if (staged_blob)
8403 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8404 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8405 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8406 fe621944 2020-11-10 stsp err = free_err;
8407 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8408 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8409 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8410 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8411 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8412 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8413 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8414 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8415 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8416 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8417 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8418 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8419 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8420 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8421 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8422 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8423 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8424 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8425 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8426 2e1f37b0 2019-08-08 stsp }
8427 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8428 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8429 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8430 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8431 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8432 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8433 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8434 2e1f37b0 2019-08-08 stsp }
8435 2e1f37b0 2019-08-08 stsp free(path1);
8436 2e1f37b0 2019-08-08 stsp free(path2);
8437 fda8017d 2020-07-23 stsp return err;
8438 fda8017d 2020-07-23 stsp }
8439 fda8017d 2020-07-23 stsp
8440 fda8017d 2020-07-23 stsp static const struct got_error *
8441 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8442 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8443 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8444 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8445 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8446 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8447 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8448 fda8017d 2020-07-23 stsp {
8449 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8450 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8451 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8452 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8453 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8454 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8455 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8456 fda8017d 2020-07-23 stsp struct stat sb;
8457 fda8017d 2020-07-23 stsp
8458 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8459 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8460 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8461 fda8017d 2020-07-23 stsp if (err)
8462 fda8017d 2020-07-23 stsp return err;
8463 fda8017d 2020-07-23 stsp
8464 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8465 fda8017d 2020-07-23 stsp return NULL;
8466 fda8017d 2020-07-23 stsp
8467 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8468 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8469 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8470 fda8017d 2020-07-23 stsp if (err)
8471 fda8017d 2020-07-23 stsp goto done;
8472 fda8017d 2020-07-23 stsp }
8473 fda8017d 2020-07-23 stsp
8474 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8475 fda8017d 2020-07-23 stsp if (f == NULL) {
8476 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8477 fda8017d 2020-07-23 stsp path_unstaged_content);
8478 fda8017d 2020-07-23 stsp goto done;
8479 fda8017d 2020-07-23 stsp }
8480 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8481 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8482 fda8017d 2020-07-23 stsp goto done;
8483 fda8017d 2020-07-23 stsp }
8484 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8485 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8486 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8487 fda8017d 2020-07-23 stsp size_t r;
8488 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8489 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8490 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8491 fda8017d 2020-07-23 stsp goto done;
8492 fda8017d 2020-07-23 stsp }
8493 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8494 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8495 fda8017d 2020-07-23 stsp goto done;
8496 fda8017d 2020-07-23 stsp }
8497 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8498 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8499 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8500 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8501 fda8017d 2020-07-23 stsp progress_arg);
8502 fda8017d 2020-07-23 stsp } else {
8503 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8504 67a66647 2021-05-31 stsp
8505 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8506 67a66647 2021-05-31 stsp if (err)
8507 67a66647 2021-05-31 stsp return err;
8508 67a66647 2021-05-31 stsp
8509 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8510 67a66647 2021-05-31 stsp parent) == -1) {
8511 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8512 67a66647 2021-05-31 stsp base_path = NULL;
8513 67a66647 2021-05-31 stsp goto done;
8514 67a66647 2021-05-31 stsp }
8515 67a66647 2021-05-31 stsp
8516 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8517 67a66647 2021-05-31 stsp if (err)
8518 67a66647 2021-05-31 stsp goto done;
8519 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8520 67a66647 2021-05-31 stsp blob_base);
8521 67a66647 2021-05-31 stsp if (err)
8522 67a66647 2021-05-31 stsp goto done;
8523 67a66647 2021-05-31 stsp
8524 eec2f5a9 2021-06-03 stsp /*
8525 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8526 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8527 eec2f5a9 2021-06-03 stsp */
8528 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8529 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8530 eec2f5a9 2021-06-03 stsp ondisk_path);
8531 eec2f5a9 2021-06-03 stsp if (err)
8532 eec2f5a9 2021-06-03 stsp goto done;
8533 eec2f5a9 2021-06-03 stsp } else {
8534 eec2f5a9 2021-06-03 stsp int fd;
8535 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8536 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8537 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8538 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8539 eec2f5a9 2021-06-03 stsp goto done;
8540 eec2f5a9 2021-06-03 stsp }
8541 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8542 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8543 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8544 eec2f5a9 2021-06-03 stsp close(fd);
8545 eec2f5a9 2021-06-03 stsp goto done;
8546 eec2f5a9 2021-06-03 stsp }
8547 eec2f5a9 2021-06-03 stsp }
8548 eec2f5a9 2021-06-03 stsp
8549 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8550 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8551 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8552 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8553 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8554 fda8017d 2020-07-23 stsp }
8555 fda8017d 2020-07-23 stsp if (err)
8556 fda8017d 2020-07-23 stsp goto done;
8557 fda8017d 2020-07-23 stsp
8558 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8559 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8560 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8561 2ac8aa02 2020-11-09 stsp } else {
8562 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8563 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8564 2ac8aa02 2020-11-09 stsp }
8565 fda8017d 2020-07-23 stsp done:
8566 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8567 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8568 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8569 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8570 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8571 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8572 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8573 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8574 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8575 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8576 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8577 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8578 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8579 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8580 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8581 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8582 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8583 67a66647 2021-05-31 stsp free(blob_base_path);
8584 67a66647 2021-05-31 stsp free(parent);
8585 67a66647 2021-05-31 stsp free(base_path);
8586 2e1f37b0 2019-08-08 stsp return err;
8587 2e1f37b0 2019-08-08 stsp }
8588 2e1f37b0 2019-08-08 stsp
8589 ad493afc 2019-08-03 stsp static const struct got_error *
8590 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8591 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8592 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8593 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8594 ad493afc 2019-08-03 stsp {
8595 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8596 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8597 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8598 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8599 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8600 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8601 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8602 9bc94a15 2019-08-03 stsp struct stat sb;
8603 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8604 ad493afc 2019-08-03 stsp
8605 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8606 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8607 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8608 2e1f37b0 2019-08-08 stsp return NULL;
8609 2e1f37b0 2019-08-08 stsp
8610 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8611 ad493afc 2019-08-03 stsp if (ie == NULL)
8612 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8613 9bc94a15 2019-08-03 stsp
8614 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8615 9bc94a15 2019-08-03 stsp == -1)
8616 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8617 ad493afc 2019-08-03 stsp
8618 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8619 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8620 f69721c3 2019-10-21 stsp if (err)
8621 f69721c3 2019-10-21 stsp goto done;
8622 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8623 f69721c3 2019-10-21 stsp id_str) == -1) {
8624 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8625 eb81bc23 2022-06-28 tracey goto done;
8626 eb81bc23 2022-06-28 tracey }
8627 eb81bc23 2022-06-28 tracey
8628 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8629 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8630 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8631 eb81bc23 2022-06-28 tracey goto done;
8632 eb81bc23 2022-06-28 tracey }
8633 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8634 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8635 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8636 f69721c3 2019-10-21 stsp goto done;
8637 f69721c3 2019-10-21 stsp }
8638 f69721c3 2019-10-21 stsp
8639 ad493afc 2019-08-03 stsp switch (staged_status) {
8640 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8641 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8642 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8643 ad493afc 2019-08-03 stsp if (err)
8644 ad493afc 2019-08-03 stsp break;
8645 ad493afc 2019-08-03 stsp /* fall through */
8646 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8647 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8648 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8649 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8650 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8651 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8652 2e1f37b0 2019-08-08 stsp if (err)
8653 2e1f37b0 2019-08-08 stsp break;
8654 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8655 2e1f37b0 2019-08-08 stsp break;
8656 2e1f37b0 2019-08-08 stsp } else {
8657 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8658 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8659 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8660 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8661 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8662 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8663 2e1f37b0 2019-08-08 stsp }
8664 2e1f37b0 2019-08-08 stsp }
8665 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8666 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8667 ad493afc 2019-08-03 stsp if (err)
8668 ad493afc 2019-08-03 stsp break;
8669 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8670 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8671 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8672 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8673 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8674 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8675 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8676 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8677 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8678 ea7786be 2020-07-23 stsp break;
8679 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8680 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8681 36bf999c 2020-07-23 stsp char *staged_target;
8682 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8683 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8684 36bf999c 2020-07-23 stsp if (err)
8685 36bf999c 2020-07-23 stsp goto done;
8686 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8687 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8688 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8689 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8690 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8691 36bf999c 2020-07-23 stsp free(staged_target);
8692 dfe9fba0 2020-07-23 stsp } else {
8693 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8694 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8695 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8696 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8697 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8698 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8699 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8700 dfe9fba0 2020-07-23 stsp }
8701 ea7786be 2020-07-23 stsp break;
8702 ea7786be 2020-07-23 stsp default:
8703 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8704 ea7786be 2020-07-23 stsp break;
8705 ea7786be 2020-07-23 stsp }
8706 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8707 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8708 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8709 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8710 2ac8aa02 2020-11-09 stsp }
8711 ad493afc 2019-08-03 stsp break;
8712 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8713 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8714 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8715 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8716 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8717 2e1f37b0 2019-08-08 stsp if (err)
8718 2e1f37b0 2019-08-08 stsp break;
8719 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8720 2e1f37b0 2019-08-08 stsp break;
8721 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8722 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8723 2e1f37b0 2019-08-08 stsp break;
8724 2e1f37b0 2019-08-08 stsp }
8725 2e1f37b0 2019-08-08 stsp }
8726 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8727 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8728 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8729 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8730 9bc94a15 2019-08-03 stsp if (err)
8731 9bc94a15 2019-08-03 stsp break;
8732 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8733 ad493afc 2019-08-03 stsp break;
8734 ad493afc 2019-08-03 stsp }
8735 f69721c3 2019-10-21 stsp done:
8736 ad493afc 2019-08-03 stsp free(ondisk_path);
8737 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8738 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8739 ad493afc 2019-08-03 stsp if (blob_base)
8740 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8741 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8742 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8743 ad493afc 2019-08-03 stsp if (blob_staged)
8744 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8745 f69721c3 2019-10-21 stsp free(id_str);
8746 f69721c3 2019-10-21 stsp free(label_orig);
8747 ad493afc 2019-08-03 stsp return err;
8748 ad493afc 2019-08-03 stsp }
8749 ad493afc 2019-08-03 stsp
8750 ad493afc 2019-08-03 stsp const struct got_error *
8751 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8752 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8753 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8754 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8755 ad493afc 2019-08-03 stsp struct got_repository *repo)
8756 ad493afc 2019-08-03 stsp {
8757 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8758 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8759 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8760 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8761 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8762 ad493afc 2019-08-03 stsp
8763 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8764 ad493afc 2019-08-03 stsp if (err)
8765 ad493afc 2019-08-03 stsp return err;
8766 ad493afc 2019-08-03 stsp
8767 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8768 ad493afc 2019-08-03 stsp if (err)
8769 ad493afc 2019-08-03 stsp goto done;
8770 ad493afc 2019-08-03 stsp
8771 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8772 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8773 ad493afc 2019-08-03 stsp upa.repo = repo;
8774 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8775 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8776 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8777 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8778 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8779 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8780 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8781 ad493afc 2019-08-03 stsp if (err)
8782 ad493afc 2019-08-03 stsp goto done;
8783 ad493afc 2019-08-03 stsp }
8784 ad493afc 2019-08-03 stsp
8785 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8786 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8787 ad493afc 2019-08-03 stsp err = sync_err;
8788 ad493afc 2019-08-03 stsp done:
8789 ad493afc 2019-08-03 stsp free(fileindex_path);
8790 ad493afc 2019-08-03 stsp if (fileindex)
8791 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8792 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8793 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8794 ad493afc 2019-08-03 stsp err = unlockerr;
8795 ad493afc 2019-08-03 stsp return err;
8796 ad493afc 2019-08-03 stsp }
8797 b2118c49 2020-07-28 stsp
8798 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8799 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8800 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8801 b2118c49 2020-07-28 stsp void *info_arg;
8802 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8803 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8804 b2118c49 2020-07-28 stsp void *cancel_arg;
8805 b2118c49 2020-07-28 stsp };
8806 b2118c49 2020-07-28 stsp
8807 b2118c49 2020-07-28 stsp static const struct got_error *
8808 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8809 b2118c49 2020-07-28 stsp {
8810 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8811 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8812 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8813 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8814 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8815 b2118c49 2020-07-28 stsp int stage;
8816 b2118c49 2020-07-28 stsp
8817 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8818 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8819 b2118c49 2020-07-28 stsp
8820 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8821 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8822 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8823 b2118c49 2020-07-28 stsp break;
8824 b2118c49 2020-07-28 stsp }
8825 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8826 b2118c49 2020-07-28 stsp return NULL;
8827 b2118c49 2020-07-28 stsp
8828 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8829 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8830 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8831 b2118c49 2020-07-28 stsp }
8832 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8833 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8834 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8835 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8836 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8837 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8838 b2118c49 2020-07-28 stsp }
8839 b2118c49 2020-07-28 stsp
8840 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8841 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8842 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8843 b2118c49 2020-07-28 stsp }
8844 b2118c49 2020-07-28 stsp
8845 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8846 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8847 b2118c49 2020-07-28 stsp }
8848 b2118c49 2020-07-28 stsp
8849 b2118c49 2020-07-28 stsp const struct got_error *
8850 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8851 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8852 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8853 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8854 b2118c49 2020-07-28 stsp
8855 b2118c49 2020-07-28 stsp {
8856 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8857 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8858 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8859 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8860 b2118c49 2020-07-28 stsp
8861 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8862 b2118c49 2020-07-28 stsp if (err)
8863 b2118c49 2020-07-28 stsp return err;
8864 b2118c49 2020-07-28 stsp
8865 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8866 b2118c49 2020-07-28 stsp if (err)
8867 b2118c49 2020-07-28 stsp goto done;
8868 b2118c49 2020-07-28 stsp
8869 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8870 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8871 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8872 b2118c49 2020-07-28 stsp arg.paths = paths;
8873 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8874 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8875 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8876 b2118c49 2020-07-28 stsp &arg);
8877 b2118c49 2020-07-28 stsp done:
8878 b2118c49 2020-07-28 stsp free(fileindex_path);
8879 b2118c49 2020-07-28 stsp if (fileindex)
8880 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8881 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8882 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8883 b2118c49 2020-07-28 stsp err = unlockerr;
8884 b2118c49 2020-07-28 stsp return err;
8885 b2118c49 2020-07-28 stsp }
8886 78f5ac24 2022-03-19 op
8887 78f5ac24 2022-03-19 op static const struct got_error *
8888 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
8889 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
8890 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
8891 78f5ac24 2022-03-19 op {
8892 78f5ac24 2022-03-19 op const struct got_error *err;
8893 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
8894 78f5ac24 2022-03-19 op struct stat sb;
8895 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
8896 78f5ac24 2022-03-19 op
8897 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
8898 78f5ac24 2022-03-19 op if (err)
8899 78f5ac24 2022-03-19 op return err;
8900 78f5ac24 2022-03-19 op
8901 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8902 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
8903 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
8904 78f5ac24 2022-03-19 op
8905 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8906 78f5ac24 2022-03-19 op if (ie) {
8907 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
8908 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8909 a05fb460 2022-04-23 op repo);
8910 78f5ac24 2022-03-19 op if (err)
8911 78f5ac24 2022-03-19 op goto done;
8912 78f5ac24 2022-03-19 op } else {
8913 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
8914 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
8915 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
8916 78f5ac24 2022-03-19 op if (errno != ENOENT) {
8917 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
8918 78f5ac24 2022-03-19 op ondisk_path);
8919 78f5ac24 2022-03-19 op goto done;
8920 78f5ac24 2022-03-19 op }
8921 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
8922 78f5ac24 2022-03-19 op }
8923 78f5ac24 2022-03-19 op }
8924 78f5ac24 2022-03-19 op
8925 78f5ac24 2022-03-19 op done:
8926 78f5ac24 2022-03-19 op free(ondisk_path);
8927 78f5ac24 2022-03-19 op return err;
8928 78f5ac24 2022-03-19 op }
8929 78f5ac24 2022-03-19 op
8930 78f5ac24 2022-03-19 op static const struct got_error *
8931 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
8932 78f5ac24 2022-03-19 op unsigned char staged_status)
8933 78f5ac24 2022-03-19 op {
8934 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8935 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8936 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8937 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8938 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
8939 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
8940 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8941 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
8942 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8943 78f5ac24 2022-03-19 op return NULL;
8944 78f5ac24 2022-03-19 op }
8945 78f5ac24 2022-03-19 op
8946 78f5ac24 2022-03-19 op static const struct got_error *
8947 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
8948 78f5ac24 2022-03-19 op {
8949 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
8950 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8951 78f5ac24 2022-03-19 op return NULL;
8952 78f5ac24 2022-03-19 op }
8953 78f5ac24 2022-03-19 op
8954 78f5ac24 2022-03-19 op static const struct got_error *
8955 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
8956 78f5ac24 2022-03-19 op unsigned char staged_status)
8957 78f5ac24 2022-03-19 op {
8958 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8959 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8960 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8961 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8962 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
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 const struct got_error *
8970 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
8971 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
8972 78f5ac24 2022-03-19 op {
8973 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
8974 78f5ac24 2022-03-19 op }
8975 78f5ac24 2022-03-19 op
8976 78f5ac24 2022-03-19 op const struct got_error *
8977 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
8978 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
8979 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
8980 78f5ac24 2022-03-19 op {
8981 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
8982 78f5ac24 2022-03-19 op int file_renamed = 0;
8983 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
8984 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
8985 78f5ac24 2022-03-19 op
8986 78f5ac24 2022-03-19 op *oldpath = NULL;
8987 78f5ac24 2022-03-19 op *newpath = NULL;
8988 78f5ac24 2022-03-19 op
8989 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
8990 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
8991 78f5ac24 2022-03-19 op if (err)
8992 78f5ac24 2022-03-19 op goto done;
8993 78f5ac24 2022-03-19 op
8994 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
8995 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
8996 78f5ac24 2022-03-19 op if (err)
8997 78f5ac24 2022-03-19 op goto done;
8998 78f5ac24 2022-03-19 op
8999 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9000 78f5ac24 2022-03-19 op file_renamed = 1;
9001 78f5ac24 2022-03-19 op
9002 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9003 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9004 78f5ac24 2022-03-19 op else if (file_renamed) {
9005 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9006 78f5ac24 2022-03-19 op if (err == NULL)
9007 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9008 78f5ac24 2022-03-19 op } else if (old == NULL)
9009 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9010 78f5ac24 2022-03-19 op else
9011 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9012 78f5ac24 2022-03-19 op
9013 78f5ac24 2022-03-19 op done:
9014 78f5ac24 2022-03-19 op if (err) {
9015 78f5ac24 2022-03-19 op free(*oldpath);
9016 78f5ac24 2022-03-19 op *oldpath = NULL;
9017 78f5ac24 2022-03-19 op free(*newpath);
9018 78f5ac24 2022-03-19 op *newpath = NULL;
9019 78f5ac24 2022-03-19 op }
9020 78f5ac24 2022-03-19 op return err;
9021 78f5ac24 2022-03-19 op }
9022 78f5ac24 2022-03-19 op
9023 78f5ac24 2022-03-19 op const struct got_error *
9024 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9025 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9026 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9027 78f5ac24 2022-03-19 op {
9028 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9029 f2dd7807 2022-05-17 op
9030 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9031 f2dd7807 2022-05-17 op saa.worktree = worktree;
9032 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9033 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9034 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9035 f2dd7807 2022-05-17 op saa.repo = repo;
9036 f2dd7807 2022-05-17 op
9037 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9038 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9039 78f5ac24 2022-03-19 op }
9040 f2dd7807 2022-05-17 op
9041 f2dd7807 2022-05-17 op const struct got_error *
9042 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9043 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9044 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9045 f2dd7807 2022-05-17 op {
9046 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9047 f2dd7807 2022-05-17 op
9048 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9049 f2dd7807 2022-05-17 op sda.worktree = worktree;
9050 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9051 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9052 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9053 f2dd7807 2022-05-17 op sda.repo = repo;
9054 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9055 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9056 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9057 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9058 f2dd7807 2022-05-17 op
9059 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9060 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9061 f2dd7807 2022-05-17 op }
9062 f2dd7807 2022-05-17 op
9063 f2dd7807 2022-05-17 op const struct got_error *
9064 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9065 4bcdc895 2022-05-17 op const char *fileindex_path)
9066 f2dd7807 2022-05-17 op {
9067 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9068 f2dd7807 2022-05-17 op
9069 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9070 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9071 f2dd7807 2022-05-17 op
9072 f2dd7807 2022-05-17 op return err;
9073 f2dd7807 2022-05-17 op }