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 5822e79e 2023-02-23 op #include <sha2.h>
33 9d31a1d8 2018-03-11 stsp #include <zlib.h>
34 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
35 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 ec22038e 2019-03-10 stsp #include <uuid.h>
37 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp
39 86c3caaf 2018-03-09 stsp #include "got_error.h"
40 86c3caaf 2018-03-09 stsp #include "got_repository.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 9d31a1d8 2018-03-11 stsp #include "got_object.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 234035bc 2019-06-01 stsp #include "got_diff.h"
48 86c3caaf 2018-03-09 stsp
49 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
50 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
54 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
55 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
56 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
57 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
58 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
60 9d31a1d8 2018-03-11 stsp
61 9d31a1d8 2018-03-11 stsp #ifndef MIN
62 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 9d31a1d8 2018-03-11 stsp #endif
64 f69721c3 2019-10-21 stsp
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
66 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 b2b3fce1 2022-10-29 op
68 d556241a 2023-07-03 jrick static mode_t
69 d556241a 2023-07-03 jrick apply_umask(mode_t mode)
70 d556241a 2023-07-03 jrick {
71 d556241a 2023-07-03 jrick mode_t um;
72 d556241a 2023-07-03 jrick
73 d556241a 2023-07-03 jrick um = umask(000);
74 d556241a 2023-07-03 jrick umask(um);
75 d556241a 2023-07-03 jrick return mode & ~um;
76 d556241a 2023-07-03 jrick }
77 86c3caaf 2018-03-09 stsp
78 99724ed4 2018-03-10 stsp static const struct got_error *
79 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
80 99724ed4 2018-03-10 stsp {
81 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
82 99724ed4 2018-03-10 stsp char *path;
83 99724ed4 2018-03-10 stsp
84 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
86 99724ed4 2018-03-10 stsp
87 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
88 99724ed4 2018-03-10 stsp free(path);
89 507dc3bb 2018-12-29 stsp return err;
90 507dc3bb 2018-12-29 stsp }
91 507dc3bb 2018-12-29 stsp
92 507dc3bb 2018-12-29 stsp static const struct got_error *
93 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
94 507dc3bb 2018-12-29 stsp {
95 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
96 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
97 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
98 507dc3bb 2018-12-29 stsp char *path = NULL;
99 507dc3bb 2018-12-29 stsp
100 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
102 507dc3bb 2018-12-29 stsp path = NULL;
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 507dc3bb 2018-12-29 stsp if (err)
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp if (content) {
111 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
112 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
114 507dc3bb 2018-12-29 stsp goto done;
115 507dc3bb 2018-12-29 stsp }
116 507dc3bb 2018-12-29 stsp }
117 507dc3bb 2018-12-29 stsp
118 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
119 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
120 2a57020b 2019-02-20 stsp unlink(tmppath);
121 507dc3bb 2018-12-29 stsp goto done;
122 507dc3bb 2018-12-29 stsp }
123 507dc3bb 2018-12-29 stsp
124 507dc3bb 2018-12-29 stsp done:
125 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
126 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
127 230a42bd 2019-05-11 jcs free(tmppath);
128 09fe317a 2018-03-11 stsp return err;
129 09fe317a 2018-03-11 stsp }
130 09fe317a 2018-03-11 stsp
131 024e9686 2019-05-14 stsp static const struct got_error *
132 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
133 024e9686 2019-05-14 stsp {
134 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
135 024e9686 2019-05-14 stsp char *refstr = NULL;
136 024e9686 2019-05-14 stsp
137 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
138 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
139 024e9686 2019-05-14 stsp if (refstr == NULL)
140 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
141 024e9686 2019-05-14 stsp } else {
142 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
143 024e9686 2019-05-14 stsp if (refstr == NULL)
144 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
145 024e9686 2019-05-14 stsp }
146 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 024e9686 2019-05-14 stsp free(refstr);
148 024e9686 2019-05-14 stsp return err;
149 024e9686 2019-05-14 stsp }
150 024e9686 2019-05-14 stsp
151 86c3caaf 2018-03-09 stsp const struct got_error *
152 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
153 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
154 86c3caaf 2018-03-09 stsp {
155 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
156 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
157 ec22038e 2019-03-10 stsp uuid_t uuid;
158 ec22038e 2019-03-10 stsp uint32_t uuid_status;
159 65596e15 2018-12-24 stsp int obj_type;
160 7ac97322 2018-03-11 stsp char *path_got = NULL;
161 1451e70d 2018-03-10 stsp char *formatstr = NULL;
162 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
163 65596e15 2018-12-24 stsp char *basestr = NULL;
164 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
165 65596e15 2018-12-24 stsp
166 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
168 0c48fee2 2019-03-11 stsp goto done;
169 0c48fee2 2019-03-11 stsp }
170 0c48fee2 2019-03-11 stsp
171 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
172 65596e15 2018-12-24 stsp if (err)
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
175 65596e15 2018-12-24 stsp if (err)
176 65596e15 2018-12-24 stsp return err;
177 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
179 86c3caaf 2018-03-09 stsp
180 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
181 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
182 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
183 0bb8a95e 2018-03-12 stsp }
184 577ec78f 2018-03-11 stsp
185 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
186 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
188 86c3caaf 2018-03-09 stsp goto done;
189 86c3caaf 2018-03-09 stsp }
190 86c3caaf 2018-03-09 stsp
191 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
192 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
194 86c3caaf 2018-03-09 stsp goto done;
195 86c3caaf 2018-03-09 stsp }
196 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp }
200 86c3caaf 2018-03-09 stsp
201 056e7441 2018-03-11 stsp /* Create an empty lock file. */
202 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 056e7441 2018-03-11 stsp if (err)
204 056e7441 2018-03-11 stsp goto done;
205 056e7441 2018-03-11 stsp
206 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
207 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 99724ed4 2018-03-10 stsp if (err)
209 86c3caaf 2018-03-09 stsp goto done;
210 86c3caaf 2018-03-09 stsp
211 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
212 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
213 65596e15 2018-12-24 stsp if (err)
214 65596e15 2018-12-24 stsp goto done;
215 65596e15 2018-12-24 stsp
216 65596e15 2018-12-24 stsp /* Record our base commit. */
217 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
218 65596e15 2018-12-24 stsp if (err)
219 65596e15 2018-12-24 stsp goto done;
220 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 99724ed4 2018-03-10 stsp if (err)
222 86c3caaf 2018-03-09 stsp goto done;
223 86c3caaf 2018-03-09 stsp
224 1451e70d 2018-03-10 stsp /* Store path to repository. */
225 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
227 99724ed4 2018-03-10 stsp if (err)
228 86c3caaf 2018-03-09 stsp goto done;
229 86c3caaf 2018-03-09 stsp
230 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
231 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
233 ec22038e 2019-03-10 stsp if (err)
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp
236 ec22038e 2019-03-10 stsp /* Generate UUID. */
237 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
238 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
239 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
240 ec22038e 2019-03-10 stsp goto done;
241 ec22038e 2019-03-10 stsp }
242 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
244 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
245 ec22038e 2019-03-10 stsp goto done;
246 ec22038e 2019-03-10 stsp }
247 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 577ec78f 2018-03-11 stsp if (err)
249 577ec78f 2018-03-11 stsp goto done;
250 577ec78f 2018-03-11 stsp
251 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
252 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
254 1451e70d 2018-03-10 stsp goto done;
255 1451e70d 2018-03-10 stsp }
256 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 99724ed4 2018-03-10 stsp if (err)
258 1451e70d 2018-03-10 stsp goto done;
259 1451e70d 2018-03-10 stsp
260 86c3caaf 2018-03-09 stsp done:
261 65596e15 2018-12-24 stsp free(commit_id);
262 7ac97322 2018-03-11 stsp free(path_got);
263 1451e70d 2018-03-10 stsp free(formatstr);
264 0bb8a95e 2018-03-12 stsp free(absprefix);
265 65596e15 2018-12-24 stsp free(basestr);
266 ec22038e 2019-03-10 stsp free(uuidstr);
267 86c3caaf 2018-03-09 stsp return err;
268 86c3caaf 2018-03-09 stsp }
269 86c3caaf 2018-03-09 stsp
270 247140b2 2019-02-05 stsp const struct got_error *
271 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 e5dc7198 2018-12-29 stsp const char *path_prefix)
273 e5dc7198 2018-12-29 stsp {
274 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
275 e5dc7198 2018-12-29 stsp
276 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
277 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
279 e5dc7198 2018-12-29 stsp }
280 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
281 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
282 e5dc7198 2018-12-29 stsp free(absprefix);
283 e5dc7198 2018-12-29 stsp return NULL;
284 86c3caaf 2018-03-09 stsp }
285 86c3caaf 2018-03-09 stsp
286 bc70eb79 2019-05-09 stsp const char *
287 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
288 86c3caaf 2018-03-09 stsp {
289 36a38700 2019-05-10 stsp return worktree->head_ref_name;
290 024e9686 2019-05-14 stsp }
291 024e9686 2019-05-14 stsp
292 024e9686 2019-05-14 stsp const struct got_error *
293 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
294 024e9686 2019-05-14 stsp struct got_reference *head_ref)
295 024e9686 2019-05-14 stsp {
296 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
297 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
298 024e9686 2019-05-14 stsp
299 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
301 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
302 024e9686 2019-05-14 stsp path_got = NULL;
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp }
305 024e9686 2019-05-14 stsp
306 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
307 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
308 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
309 024e9686 2019-05-14 stsp goto done;
310 024e9686 2019-05-14 stsp }
311 024e9686 2019-05-14 stsp
312 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
313 024e9686 2019-05-14 stsp if (err)
314 024e9686 2019-05-14 stsp goto done;
315 024e9686 2019-05-14 stsp
316 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
317 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
318 024e9686 2019-05-14 stsp done:
319 024e9686 2019-05-14 stsp free(path_got);
320 024e9686 2019-05-14 stsp if (err)
321 024e9686 2019-05-14 stsp free(head_ref_name);
322 024e9686 2019-05-14 stsp return err;
323 507dc3bb 2018-12-29 stsp }
324 507dc3bb 2018-12-29 stsp
325 b72f483a 2019-02-05 stsp struct got_object_id *
326 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
327 507dc3bb 2018-12-29 stsp {
328 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
329 86c3caaf 2018-03-09 stsp }
330 86c3caaf 2018-03-09 stsp
331 507dc3bb 2018-12-29 stsp const struct got_error *
332 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
334 507dc3bb 2018-12-29 stsp {
335 507dc3bb 2018-12-29 stsp const struct got_error *err;
336 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
337 507dc3bb 2018-12-29 stsp char *id_str = NULL;
338 507dc3bb 2018-12-29 stsp char *path_got = NULL;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
342 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
343 507dc3bb 2018-12-29 stsp path_got = NULL;
344 507dc3bb 2018-12-29 stsp goto done;
345 507dc3bb 2018-12-29 stsp }
346 507dc3bb 2018-12-29 stsp
347 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
348 507dc3bb 2018-12-29 stsp if (err)
349 507dc3bb 2018-12-29 stsp return err;
350 507dc3bb 2018-12-29 stsp
351 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
353 507dc3bb 2018-12-29 stsp goto done;
354 507dc3bb 2018-12-29 stsp }
355 507dc3bb 2018-12-29 stsp
356 507dc3bb 2018-12-29 stsp /* Record our base commit. */
357 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
358 507dc3bb 2018-12-29 stsp if (err)
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 507dc3bb 2018-12-29 stsp if (err)
362 507dc3bb 2018-12-29 stsp goto done;
363 507dc3bb 2018-12-29 stsp
364 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
365 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
366 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
367 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
368 507dc3bb 2018-12-29 stsp goto done;
369 507dc3bb 2018-12-29 stsp }
370 507dc3bb 2018-12-29 stsp done:
371 507dc3bb 2018-12-29 stsp if (obj)
372 507dc3bb 2018-12-29 stsp got_object_close(obj);
373 507dc3bb 2018-12-29 stsp free(id_str);
374 507dc3bb 2018-12-29 stsp free(path_got);
375 507dc3bb 2018-12-29 stsp return err;
376 50b0790e 2020-09-11 stsp }
377 50b0790e 2020-09-11 stsp
378 50b0790e 2020-09-11 stsp const struct got_gotconfig *
379 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
380 50b0790e 2020-09-11 stsp {
381 50b0790e 2020-09-11 stsp return worktree->gotconfig;
382 507dc3bb 2018-12-29 stsp }
383 507dc3bb 2018-12-29 stsp
384 9d31a1d8 2018-03-11 stsp static const struct got_error *
385 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
386 86c3caaf 2018-03-09 stsp {
387 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
390 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
391 86c3caaf 2018-03-09 stsp return NULL;
392 21908da4 2019-01-13 stsp }
393 21908da4 2019-01-13 stsp
394 21908da4 2019-01-13 stsp static const struct got_error *
395 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
396 4a1ddfc2 2019-01-12 stsp {
397 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
398 4a1ddfc2 2019-01-12 stsp char *abspath;
399 4a1ddfc2 2019-01-12 stsp
400 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
401 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
402 4a1ddfc2 2019-01-12 stsp
403 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
404 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
405 ddcd8544 2019-03-11 stsp struct stat sb;
406 ddcd8544 2019-03-11 stsp err = NULL;
407 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
408 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
409 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
410 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
411 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
412 ddcd8544 2019-03-11 stsp }
413 ddcd8544 2019-03-11 stsp }
414 4a1ddfc2 2019-01-12 stsp free(abspath);
415 68c76935 2019-02-19 stsp return err;
416 68c76935 2019-02-19 stsp }
417 68c76935 2019-02-19 stsp
418 68c76935 2019-02-19 stsp static const struct got_error *
419 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
420 68c76935 2019-02-19 stsp {
421 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
422 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
423 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
424 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
425 68c76935 2019-02-19 stsp
426 68c76935 2019-02-19 stsp *same = 1;
427 68c76935 2019-02-19 stsp
428 230a42bd 2019-05-11 jcs for (;;) {
429 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
430 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
431 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
432 68c76935 2019-02-19 stsp break;
433 68c76935 2019-02-19 stsp }
434 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
435 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
436 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
437 68c76935 2019-02-19 stsp break;
438 68c76935 2019-02-19 stsp }
439 68c76935 2019-02-19 stsp if (flen1 == 0) {
440 68c76935 2019-02-19 stsp if (flen2 != 0)
441 68c76935 2019-02-19 stsp *same = 0;
442 68c76935 2019-02-19 stsp break;
443 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
444 68c76935 2019-02-19 stsp if (flen1 != 0)
445 68c76935 2019-02-19 stsp *same = 0;
446 68c76935 2019-02-19 stsp break;
447 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
448 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
449 68c76935 2019-02-19 stsp *same = 0;
450 68c76935 2019-02-19 stsp break;
451 68c76935 2019-02-19 stsp }
452 68c76935 2019-02-19 stsp } else {
453 68c76935 2019-02-19 stsp *same = 0;
454 68c76935 2019-02-19 stsp break;
455 68c76935 2019-02-19 stsp }
456 68c76935 2019-02-19 stsp }
457 68c76935 2019-02-19 stsp
458 68c76935 2019-02-19 stsp return err;
459 68c76935 2019-02-19 stsp }
460 68c76935 2019-02-19 stsp
461 68c76935 2019-02-19 stsp static const struct got_error *
462 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
463 68c76935 2019-02-19 stsp {
464 68c76935 2019-02-19 stsp struct stat sb;
465 68c76935 2019-02-19 stsp size_t size1, size2;
466 68c76935 2019-02-19 stsp
467 68c76935 2019-02-19 stsp *same = 1;
468 68c76935 2019-02-19 stsp
469 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
470 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
471 68c76935 2019-02-19 stsp size1 = sb.st_size;
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
474 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
475 68c76935 2019-02-19 stsp size2 = sb.st_size;
476 68c76935 2019-02-19 stsp
477 68c76935 2019-02-19 stsp if (size1 != size2) {
478 68c76935 2019-02-19 stsp *same = 0;
479 68c76935 2019-02-19 stsp return NULL;
480 68c76935 2019-02-19 stsp }
481 68c76935 2019-02-19 stsp
482 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
483 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
484 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
485 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
486 68c76935 2019-02-19 stsp
487 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
488 0e039681 2021-11-15 stsp }
489 0e039681 2021-11-15 stsp
490 0e039681 2021-11-15 stsp static const struct got_error *
491 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
492 0e039681 2021-11-15 stsp {
493 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
494 0e039681 2021-11-15 stsp size_t flen;
495 0e039681 2021-11-15 stsp ssize_t outlen;
496 0e039681 2021-11-15 stsp
497 0e039681 2021-11-15 stsp *outsize = 0;
498 0e039681 2021-11-15 stsp
499 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
500 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
501 0e039681 2021-11-15 stsp
502 0e039681 2021-11-15 stsp for (;;) {
503 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
504 0e039681 2021-11-15 stsp if (flen == 0) {
505 0e039681 2021-11-15 stsp if (ferror(f))
506 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
507 0e039681 2021-11-15 stsp if (feof(f))
508 0e039681 2021-11-15 stsp break;
509 0e039681 2021-11-15 stsp }
510 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
511 0e039681 2021-11-15 stsp if (outlen == -1)
512 0e039681 2021-11-15 stsp return got_error_from_errno("write");
513 0e039681 2021-11-15 stsp if (outlen != flen)
514 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
515 0e039681 2021-11-15 stsp *outsize += outlen;
516 0e039681 2021-11-15 stsp }
517 0e039681 2021-11-15 stsp
518 0e039681 2021-11-15 stsp return NULL;
519 0e039681 2021-11-15 stsp }
520 0e039681 2021-11-15 stsp
521 0e039681 2021-11-15 stsp static const struct got_error *
522 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
523 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
524 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
525 0e039681 2021-11-15 stsp const char *ondisk_path)
526 0e039681 2021-11-15 stsp {
527 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
528 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
529 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
530 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
531 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
532 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
533 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
534 0e039681 2021-11-15 stsp
535 0e039681 2021-11-15 stsp *overlapcnt = 0;
536 0e039681 2021-11-15 stsp
537 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
538 0e039681 2021-11-15 stsp if (err)
539 0e039681 2021-11-15 stsp return err;
540 0e039681 2021-11-15 stsp
541 0e039681 2021-11-15 stsp if (same_content)
542 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
543 0e039681 2021-11-15 stsp
544 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
545 0e039681 2021-11-15 stsp if (err)
546 0e039681 2021-11-15 stsp return err;
547 0e039681 2021-11-15 stsp changed_deriv = !same_content;
548 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
549 0e039681 2021-11-15 stsp if (err)
550 0e039681 2021-11-15 stsp return err;
551 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
552 0e039681 2021-11-15 stsp
553 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
554 0e039681 2021-11-15 stsp *overlapcnt = 1;
555 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
556 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
557 0e039681 2021-11-15 stsp goto done;
558 0e039681 2021-11-15 stsp }
559 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
560 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
561 0e039681 2021-11-15 stsp goto done;
562 0e039681 2021-11-15 stsp }
563 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
564 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
565 0e039681 2021-11-15 stsp goto done;
566 0e039681 2021-11-15 stsp }
567 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
568 b90054ed 2022-11-01 stsp base_path_orig, "");
569 0e039681 2021-11-15 stsp if (err)
570 0e039681 2021-11-15 stsp goto done;
571 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
572 b90054ed 2022-11-01 stsp base_path_deriv, "");
573 0e039681 2021-11-15 stsp if (err)
574 0e039681 2021-11-15 stsp goto done;
575 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
576 b90054ed 2022-11-01 stsp base_path_deriv2, "");
577 0e039681 2021-11-15 stsp if (err)
578 0e039681 2021-11-15 stsp goto done;
579 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
580 0e039681 2021-11-15 stsp if (err)
581 0e039681 2021-11-15 stsp goto done;
582 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
583 0e039681 2021-11-15 stsp if (err)
584 0e039681 2021-11-15 stsp goto done;
585 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
586 0e039681 2021-11-15 stsp if (err)
587 0e039681 2021-11-15 stsp goto done;
588 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
589 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
590 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
591 0e039681 2021-11-15 stsp goto done;
592 0e039681 2021-11-15 stsp }
593 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
595 0e039681 2021-11-15 stsp label_deriv ? " " : "",
596 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
597 0e039681 2021-11-15 stsp path_deriv) < 0) {
598 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
599 0e039681 2021-11-15 stsp goto done;
600 0e039681 2021-11-15 stsp }
601 0e039681 2021-11-15 stsp if (size_orig > 0) {
602 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
604 0e039681 2021-11-15 stsp label_orig ? " " : "",
605 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
606 0e039681 2021-11-15 stsp path_orig) < 0) {
607 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
608 0e039681 2021-11-15 stsp goto done;
609 0e039681 2021-11-15 stsp }
610 0e039681 2021-11-15 stsp }
611 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
612 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
613 0e039681 2021-11-15 stsp path_deriv2,
614 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
615 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
616 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
617 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
618 0e039681 2021-11-15 stsp goto done;
619 0e039681 2021-11-15 stsp }
620 0e039681 2021-11-15 stsp } else if (changed_deriv)
621 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
622 0e039681 2021-11-15 stsp else if (changed_deriv2)
623 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
624 0e039681 2021-11-15 stsp done:
625 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
626 0e039681 2021-11-15 stsp err == NULL)
627 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
628 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
629 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
630 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
631 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
632 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
633 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
634 0e039681 2021-11-15 stsp free(path_orig);
635 0e039681 2021-11-15 stsp free(path_deriv);
636 0e039681 2021-11-15 stsp free(path_deriv2);
637 0e039681 2021-11-15 stsp free(base_path_orig);
638 0e039681 2021-11-15 stsp free(base_path_deriv);
639 0e039681 2021-11-15 stsp free(base_path_deriv2);
640 0e039681 2021-11-15 stsp return err;
641 6353ad76 2019-02-08 stsp }
642 6353ad76 2019-02-08 stsp
643 6353ad76 2019-02-08 stsp /*
644 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
645 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
646 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
647 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
648 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
649 6353ad76 2019-02-08 stsp */
650 6353ad76 2019-02-08 stsp static const struct got_error *
651 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
652 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
653 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
654 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
655 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
656 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
657 6353ad76 2019-02-08 stsp {
658 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
659 6353ad76 2019-02-08 stsp int merged_fd = -1;
660 db590691 2021-06-02 stsp FILE *f_merged = NULL;
661 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
662 234035bc 2019-06-01 stsp int overlapcnt = 0;
663 3524bbf9 2020-10-20 stsp char *parent = NULL;
664 6353ad76 2019-02-08 stsp
665 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
666 234035bc 2019-06-01 stsp
667 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
668 3524bbf9 2020-10-20 stsp if (err)
669 3524bbf9 2020-10-20 stsp return err;
670 af54ae4a 2019-02-19 stsp
671 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
672 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
673 3524bbf9 2020-10-20 stsp goto done;
674 3524bbf9 2020-10-20 stsp }
675 af54ae4a 2019-02-19 stsp
676 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
677 6353ad76 2019-02-08 stsp if (err)
678 6353ad76 2019-02-08 stsp goto done;
679 3b9f0f87 2020-07-23 stsp
680 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
681 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
682 0e039681 2021-11-15 stsp if (err) {
683 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
684 0e039681 2021-11-15 stsp goto done;
685 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
686 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
687 0e039681 2021-11-15 stsp ondisk_path);
688 0e039681 2021-11-15 stsp if (err)
689 0e039681 2021-11-15 stsp goto done;
690 0e039681 2021-11-15 stsp }
691 6353ad76 2019-02-08 stsp
692 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
693 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
694 1ee397ad 2019-07-12 stsp if (err)
695 1ee397ad 2019-07-12 stsp goto done;
696 6353ad76 2019-02-08 stsp
697 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
698 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
699 816dc654 2019-02-16 stsp goto done;
700 68c76935 2019-02-19 stsp }
701 68c76935 2019-02-19 stsp
702 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
703 db590691 2021-06-02 stsp if (f_merged == NULL) {
704 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
705 db590691 2021-06-02 stsp goto done;
706 db590691 2021-06-02 stsp }
707 db590691 2021-06-02 stsp merged_fd = -1;
708 db590691 2021-06-02 stsp
709 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
710 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
711 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
712 db590691 2021-06-02 stsp f_merged);
713 68c76935 2019-02-19 stsp if (err)
714 68c76935 2019-02-19 stsp goto done;
715 816dc654 2019-02-16 stsp }
716 6353ad76 2019-02-08 stsp
717 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
718 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
719 70a0c8ec 2019-02-20 stsp goto done;
720 70a0c8ec 2019-02-20 stsp }
721 70a0c8ec 2019-02-20 stsp
722 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
723 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
724 230a42bd 2019-05-11 jcs ondisk_path);
725 6353ad76 2019-02-08 stsp goto done;
726 6353ad76 2019-02-08 stsp }
727 6353ad76 2019-02-08 stsp done:
728 fdcb7daf 2019-12-15 stsp if (err) {
729 fdcb7daf 2019-12-15 stsp if (merged_path)
730 fdcb7daf 2019-12-15 stsp unlink(merged_path);
731 3b9f0f87 2020-07-23 stsp }
732 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
733 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
734 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
735 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
736 6353ad76 2019-02-08 stsp free(merged_path);
737 af54ae4a 2019-02-19 stsp free(base_path);
738 3524bbf9 2020-10-20 stsp free(parent);
739 af57b12a 2020-07-23 stsp return err;
740 af57b12a 2020-07-23 stsp }
741 af57b12a 2020-07-23 stsp
742 af57b12a 2020-07-23 stsp static const struct got_error *
743 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
744 af57b12a 2020-07-23 stsp size_t target_len)
745 af57b12a 2020-07-23 stsp {
746 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
747 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
748 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
749 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
750 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
751 af57b12a 2020-07-23 stsp ondisk_path);
752 af57b12a 2020-07-23 stsp return NULL;
753 af57b12a 2020-07-23 stsp }
754 af57b12a 2020-07-23 stsp
755 af57b12a 2020-07-23 stsp /*
756 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
757 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
758 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
759 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
760 993e2a1b 2020-07-23 stsp *
761 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
762 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
763 993e2a1b 2020-07-23 stsp *
764 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
765 993e2a1b 2020-07-23 stsp * has deleted this symlink.
766 11cc08c1 2020-07-23 stsp */
767 11cc08c1 2020-07-23 stsp static const struct got_error *
768 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
769 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
770 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
771 11cc08c1 2020-07-23 stsp {
772 11cc08c1 2020-07-23 stsp const struct got_error *err;
773 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
774 11cc08c1 2020-07-23 stsp FILE *f = NULL;
775 11cc08c1 2020-07-23 stsp
776 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
777 11cc08c1 2020-07-23 stsp if (err)
778 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
779 11cc08c1 2020-07-23 stsp
780 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
781 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
782 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
783 11cc08c1 2020-07-23 stsp goto done;
784 11cc08c1 2020-07-23 stsp }
785 11cc08c1 2020-07-23 stsp
786 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
787 11cc08c1 2020-07-23 stsp if (err)
788 3818e3c4 2020-11-01 naddy goto done;
789 3818e3c4 2020-11-01 naddy
790 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
791 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
792 11cc08c1 2020-07-23 stsp goto done;
793 3818e3c4 2020-11-01 naddy }
794 11cc08c1 2020-07-23 stsp
795 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
796 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
797 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
798 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
799 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
800 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
801 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
802 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
803 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
805 11cc08c1 2020-07-23 stsp goto done;
806 11cc08c1 2020-07-23 stsp }
807 11cc08c1 2020-07-23 stsp
808 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
809 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
810 11cc08c1 2020-07-23 stsp goto done;
811 11cc08c1 2020-07-23 stsp }
812 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
813 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
814 11cc08c1 2020-07-23 stsp goto done;
815 11cc08c1 2020-07-23 stsp }
816 11cc08c1 2020-07-23 stsp done:
817 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
818 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
819 11cc08c1 2020-07-23 stsp free(path);
820 11cc08c1 2020-07-23 stsp free(id_str);
821 11cc08c1 2020-07-23 stsp free(label_deriv);
822 11cc08c1 2020-07-23 stsp return err;
823 11cc08c1 2020-07-23 stsp }
824 d219f183 2020-07-23 stsp
825 d219f183 2020-07-23 stsp /* forward declaration */
826 d219f183 2020-07-23 stsp static const struct got_error *
827 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
828 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
829 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
830 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
831 11cc08c1 2020-07-23 stsp
832 11cc08c1 2020-07-23 stsp /*
833 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
834 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
835 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
836 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
837 af57b12a 2020-07-23 stsp */
838 af57b12a 2020-07-23 stsp static const struct got_error *
839 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
840 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
841 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
842 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
843 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
844 af57b12a 2020-07-23 stsp {
845 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
846 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
847 af57b12a 2020-07-23 stsp struct stat sb;
848 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
849 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
850 11cc08c1 2020-07-23 stsp int have_local_change = 0;
851 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
852 af57b12a 2020-07-23 stsp
853 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
854 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
855 af57b12a 2020-07-23 stsp
856 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
857 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
858 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
859 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
860 af57b12a 2020-07-23 stsp ondisk_path);
861 af57b12a 2020-07-23 stsp goto done;
862 af57b12a 2020-07-23 stsp }
863 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
864 af57b12a 2020-07-23 stsp
865 526a746f 2020-07-23 stsp if (blob_orig) {
866 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
867 526a746f 2020-07-23 stsp if (err)
868 526a746f 2020-07-23 stsp goto done;
869 526a746f 2020-07-23 stsp }
870 af57b12a 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
872 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
873 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
874 11cc08c1 2020-07-23 stsp have_local_change = 1;
875 11cc08c1 2020-07-23 stsp
876 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
877 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
878 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
879 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
880 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
881 11cc08c1 2020-07-23 stsp
882 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
883 11cc08c1 2020-07-23 stsp if (ancestor_target) {
884 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
885 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
886 11cc08c1 2020-07-23 stsp path);
887 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
888 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
889 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
890 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
891 11cc08c1 2020-07-23 stsp path);
892 11cc08c1 2020-07-23 stsp } else {
893 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
894 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
895 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
896 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
897 11cc08c1 2020-07-23 stsp if (err)
898 11cc08c1 2020-07-23 stsp goto done;
899 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
900 11cc08c1 2020-07-23 stsp path);
901 11cc08c1 2020-07-23 stsp }
902 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
903 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
904 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
905 af57b12a 2020-07-23 stsp strlen(deriv_target));
906 af57b12a 2020-07-23 stsp if (err)
907 af57b12a 2020-07-23 stsp goto done;
908 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
909 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
910 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
911 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
912 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
913 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
914 ea7786be 2020-07-23 stsp path);
915 ea7786be 2020-07-23 stsp } else {
916 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
917 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
918 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
919 ea7786be 2020-07-23 stsp if (err)
920 ea7786be 2020-07-23 stsp goto done;
921 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
922 ea7786be 2020-07-23 stsp path);
923 ea7786be 2020-07-23 stsp }
924 6353ad76 2019-02-08 stsp }
925 11cc08c1 2020-07-23 stsp
926 af57b12a 2020-07-23 stsp done:
927 af57b12a 2020-07-23 stsp free(ancestor_target);
928 eec2f5a9 2021-06-03 stsp return err;
929 eec2f5a9 2021-06-03 stsp }
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp static const struct got_error *
932 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
933 eec2f5a9 2021-06-03 stsp {
934 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
935 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
936 eec2f5a9 2021-06-03 stsp ssize_t target_len;
937 eec2f5a9 2021-06-03 stsp size_t n;
938 eec2f5a9 2021-06-03 stsp FILE *f;
939 eec2f5a9 2021-06-03 stsp
940 eec2f5a9 2021-06-03 stsp *outfile = NULL;
941 eec2f5a9 2021-06-03 stsp
942 eec2f5a9 2021-06-03 stsp f = got_opentemp();
943 eec2f5a9 2021-06-03 stsp if (f == NULL)
944 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
945 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
946 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
947 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
948 eec2f5a9 2021-06-03 stsp goto done;
949 eec2f5a9 2021-06-03 stsp }
950 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
951 eec2f5a9 2021-06-03 stsp if (n != target_len) {
952 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
953 eec2f5a9 2021-06-03 stsp goto done;
954 eec2f5a9 2021-06-03 stsp }
955 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
956 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
957 eec2f5a9 2021-06-03 stsp goto done;
958 eec2f5a9 2021-06-03 stsp }
959 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
960 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
961 eec2f5a9 2021-06-03 stsp goto done;
962 eec2f5a9 2021-06-03 stsp }
963 eec2f5a9 2021-06-03 stsp done:
964 eec2f5a9 2021-06-03 stsp if (err)
965 eec2f5a9 2021-06-03 stsp fclose(f);
966 eec2f5a9 2021-06-03 stsp else
967 eec2f5a9 2021-06-03 stsp *outfile = f;
968 14c901f1 2019-08-08 stsp return err;
969 14c901f1 2019-08-08 stsp }
970 14c901f1 2019-08-08 stsp
971 14c901f1 2019-08-08 stsp /*
972 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
973 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
974 14c901f1 2019-08-08 stsp * acts as the second derived version.
975 14c901f1 2019-08-08 stsp */
976 14c901f1 2019-08-08 stsp static const struct got_error *
977 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
978 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
979 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
980 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
981 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
982 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
983 14c901f1 2019-08-08 stsp {
984 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
985 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
986 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
987 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
988 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
989 14c901f1 2019-08-08 stsp
990 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
991 14c901f1 2019-08-08 stsp
992 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
993 ed6b5030 2020-10-20 stsp if (err)
994 ed6b5030 2020-10-20 stsp return err;
995 14c901f1 2019-08-08 stsp
996 67a66647 2021-05-31 stsp if (blob_orig) {
997 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
998 67a66647 2021-05-31 stsp parent) == -1) {
999 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1000 67a66647 2021-05-31 stsp base_path = NULL;
1001 67a66647 2021-05-31 stsp goto done;
1002 67a66647 2021-05-31 stsp }
1003 67a66647 2021-05-31 stsp
1004 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
1005 b90054ed 2022-11-01 stsp base_path, "");
1006 67a66647 2021-05-31 stsp if (err)
1007 67a66647 2021-05-31 stsp goto done;
1008 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1009 67a66647 2021-05-31 stsp blob_orig);
1010 67a66647 2021-05-31 stsp if (err)
1011 67a66647 2021-05-31 stsp goto done;
1012 67a66647 2021-05-31 stsp free(base_path);
1013 db590691 2021-06-02 stsp } else {
1014 db590691 2021-06-02 stsp /*
1015 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1016 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1017 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1018 db590691 2021-06-02 stsp */
1019 db590691 2021-06-02 stsp f_orig = got_opentemp();
1020 db590691 2021-06-02 stsp if (f_orig == NULL) {
1021 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1022 db590691 2021-06-02 stsp goto done;
1023 db590691 2021-06-02 stsp }
1024 67a66647 2021-05-31 stsp }
1025 67a66647 2021-05-31 stsp
1026 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1027 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1028 14c901f1 2019-08-08 stsp base_path = NULL;
1029 14c901f1 2019-08-08 stsp goto done;
1030 14c901f1 2019-08-08 stsp }
1031 14c901f1 2019-08-08 stsp
1032 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1033 14c901f1 2019-08-08 stsp if (err)
1034 14c901f1 2019-08-08 stsp goto done;
1035 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1036 14c901f1 2019-08-08 stsp blob_deriv);
1037 14c901f1 2019-08-08 stsp if (err)
1038 14c901f1 2019-08-08 stsp goto done;
1039 14c901f1 2019-08-08 stsp
1040 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1041 14c901f1 2019-08-08 stsp if (err)
1042 14c901f1 2019-08-08 stsp goto done;
1043 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1044 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1045 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1046 14c901f1 2019-08-08 stsp goto done;
1047 eec2f5a9 2021-06-03 stsp }
1048 eec2f5a9 2021-06-03 stsp
1049 5e91dae4 2022-08-30 stsp /*
1050 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1051 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1052 eec2f5a9 2021-06-03 stsp */
1053 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1054 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1055 eec2f5a9 2021-06-03 stsp if (err)
1056 eec2f5a9 2021-06-03 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp } else {
1058 eec2f5a9 2021-06-03 stsp int fd;
1059 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1060 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1061 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1062 eec2f5a9 2021-06-03 stsp goto done;
1063 eec2f5a9 2021-06-03 stsp }
1064 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1065 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1066 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1067 eec2f5a9 2021-06-03 stsp close(fd);
1068 eec2f5a9 2021-06-03 stsp goto done;
1069 eec2f5a9 2021-06-03 stsp }
1070 14c901f1 2019-08-08 stsp }
1071 14c901f1 2019-08-08 stsp
1072 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1073 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1074 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1075 14c901f1 2019-08-08 stsp done:
1076 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1077 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1078 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1079 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1080 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1081 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1082 14c901f1 2019-08-08 stsp free(base_path);
1083 67a66647 2021-05-31 stsp if (blob_orig_path) {
1084 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1085 67a66647 2021-05-31 stsp free(blob_orig_path);
1086 67a66647 2021-05-31 stsp }
1087 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1088 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1089 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1090 14c901f1 2019-08-08 stsp }
1091 6353ad76 2019-02-08 stsp free(id_str);
1092 818c7501 2019-07-11 stsp free(label_deriv);
1093 ed6b5030 2020-10-20 stsp free(parent);
1094 4a1ddfc2 2019-01-12 stsp return err;
1095 4a1ddfc2 2019-01-12 stsp }
1096 4a1ddfc2 2019-01-12 stsp
1097 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1098 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1099 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1100 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1101 13d9040b 2019-03-27 stsp {
1102 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1103 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1104 13d9040b 2019-03-27 stsp
1105 65b05cec 2020-07-23 stsp *new_iep = NULL;
1106 65b05cec 2020-07-23 stsp
1107 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1108 054041d0 2020-06-24 stsp if (err)
1109 054041d0 2020-06-24 stsp return err;
1110 054041d0 2020-06-24 stsp
1111 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1112 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1113 054041d0 2020-06-24 stsp if (err)
1114 054041d0 2020-06-24 stsp goto done;
1115 054041d0 2020-06-24 stsp
1116 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1117 054041d0 2020-06-24 stsp done:
1118 054041d0 2020-06-24 stsp if (err)
1119 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1120 65b05cec 2020-07-23 stsp else
1121 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1122 13d9040b 2019-03-27 stsp return err;
1123 1ebedb77 2019-10-19 stsp }
1124 1ebedb77 2019-10-19 stsp
1125 1ebedb77 2019-10-19 stsp static mode_t
1126 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1127 1ebedb77 2019-10-19 stsp {
1128 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1129 1ebedb77 2019-10-19 stsp
1130 1ebedb77 2019-10-19 stsp if (executable) {
1131 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1132 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1133 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1134 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1135 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1136 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1137 1ebedb77 2019-10-19 stsp }
1138 1ebedb77 2019-10-19 stsp
1139 b2b3fce1 2022-10-29 op return st_mode;
1140 13d9040b 2019-03-27 stsp }
1141 13d9040b 2019-03-27 stsp
1142 8ba819a3 2020-07-23 stsp /* forward declaration */
1143 13d9040b 2019-03-27 stsp static const struct got_error *
1144 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1146 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1147 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1148 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1149 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1150 8ba819a3 2020-07-23 stsp
1151 5a1fbc73 2020-07-23 stsp /*
1152 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1153 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1154 5a1fbc73 2020-07-23 stsp */
1155 8ba819a3 2020-07-23 stsp static const struct got_error *
1156 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1158 5a1fbc73 2020-07-23 stsp {
1159 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1160 5a1fbc73 2020-07-23 stsp ssize_t elen;
1161 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1162 5a1fbc73 2020-07-23 stsp int fd;
1163 5a1fbc73 2020-07-23 stsp
1164 c6e8a826 2021-04-05 stsp *did_something = 0;
1165 c6e8a826 2021-04-05 stsp
1166 5a1fbc73 2020-07-23 stsp /*
1167 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1168 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1169 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1170 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1171 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1172 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1173 5a1fbc73 2020-07-23 stsp */
1174 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1176 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1177 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1178 5a1fbc73 2020-07-23 stsp
1179 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1180 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 5a1fbc73 2020-07-23 stsp if (elen == -1)
1182 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp
1184 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1185 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1186 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1187 5a1fbc73 2020-07-23 stsp }
1188 5a1fbc73 2020-07-23 stsp
1189 c6e8a826 2021-04-05 stsp *did_something = 1;
1190 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1191 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1192 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1193 5a1fbc73 2020-07-23 stsp return err;
1194 3c1ec782 2020-07-23 stsp }
1195 3c1ec782 2020-07-23 stsp
1196 3c1ec782 2020-07-23 stsp static const struct got_error *
1197 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 3c1ec782 2020-07-23 stsp {
1200 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1201 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1202 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1203 3c1ec782 2020-07-23 stsp
1204 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1205 3c1ec782 2020-07-23 stsp
1206 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1207 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1208 3c1ec782 2020-07-23 stsp return NULL;
1209 3c1ec782 2020-07-23 stsp }
1210 3c1ec782 2020-07-23 stsp
1211 3c1ec782 2020-07-23 stsp /*
1212 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1213 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1214 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1215 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1216 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1217 3c1ec782 2020-07-23 stsp */
1218 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1219 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1220 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1221 ce031e9e 2020-10-20 stsp if (err)
1222 ce031e9e 2020-10-20 stsp return err;
1223 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 ce031e9e 2020-10-20 stsp free(parent);
1225 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1226 ce031e9e 2020-10-20 stsp }
1227 ce031e9e 2020-10-20 stsp free(parent);
1228 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1229 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 3c1ec782 2020-07-23 stsp free(abspath);
1231 3c1ec782 2020-07-23 stsp return err;
1232 3c1ec782 2020-07-23 stsp }
1233 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp if (err)
1236 3c1ec782 2020-07-23 stsp return err;
1237 3c1ec782 2020-07-23 stsp } else {
1238 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 3c1ec782 2020-07-23 stsp if (err)
1240 3c1ec782 2020-07-23 stsp return err;
1241 3c1ec782 2020-07-23 stsp }
1242 3c1ec782 2020-07-23 stsp
1243 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1244 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1246 3c1ec782 2020-07-23 stsp return NULL;
1247 3c1ec782 2020-07-23 stsp }
1248 3c1ec782 2020-07-23 stsp
1249 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1250 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1252 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1253 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1255 3c1ec782 2020-07-23 stsp
1256 3c1ec782 2020-07-23 stsp free(path_got);
1257 3c1ec782 2020-07-23 stsp return NULL;
1258 5a1fbc73 2020-07-23 stsp }
1259 5a1fbc73 2020-07-23 stsp
1260 5a1fbc73 2020-07-23 stsp static const struct got_error *
1261 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1264 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1265 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1266 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 9d31a1d8 2018-03-11 stsp {
1268 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1269 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1270 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1271 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 8ba819a3 2020-07-23 stsp
1274 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1275 2e1fa222 2020-07-23 stsp
1276 3c29341b 2022-07-21 florian /*
1277 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1278 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1279 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1280 8ba819a3 2020-07-23 stsp * in the blob object.
1281 8ba819a3 2020-07-23 stsp */
1282 8ba819a3 2020-07-23 stsp do {
1283 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1284 538b6881 2022-07-21 florian if (err)
1285 538b6881 2022-07-21 florian return err;
1286 538b6881 2022-07-21 florian
1287 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1288 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1289 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1290 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1291 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1292 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1294 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1295 3b9f0f87 2020-07-23 stsp progress_arg);
1296 8ba819a3 2020-07-23 stsp }
1297 8ba819a3 2020-07-23 stsp if (len > 0) {
1298 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1299 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1300 8ba819a3 2020-07-23 stsp len - hdrlen);
1301 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1302 8ba819a3 2020-07-23 stsp hdrlen = 0;
1303 8ba819a3 2020-07-23 stsp }
1304 8ba819a3 2020-07-23 stsp } while (len != 0);
1305 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1306 8ba819a3 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1309 3c1ec782 2020-07-23 stsp if (err)
1310 3c1ec782 2020-07-23 stsp return err;
1311 8ba819a3 2020-07-23 stsp
1312 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1313 906c123b 2020-07-23 stsp /* install as a regular file */
1314 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1315 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1316 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1318 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1319 3c29341b 2022-07-21 florian return err;
1320 906c123b 2020-07-23 stsp }
1321 906c123b 2020-07-23 stsp
1322 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1323 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1324 c6e8a826 2021-04-05 stsp int symlink_replaced;
1325 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1326 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1327 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1328 3c29341b 2022-07-21 florian return err;
1329 c90c8ce3 2020-07-23 stsp }
1330 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1331 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1332 5a1fbc73 2020-07-23 stsp if (err)
1333 3c29341b 2022-07-21 florian return err;
1334 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1335 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1336 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1337 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1340 c6e8a826 2021-04-05 stsp } else {
1341 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1343 c6e8a826 2021-04-05 stsp }
1344 f35fa46a 2020-07-23 stsp }
1345 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1346 f35fa46a 2020-07-23 stsp }
1347 f35fa46a 2020-07-23 stsp
1348 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1349 f4994adc 2020-10-20 stsp char *parent;
1350 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1351 f4994adc 2020-10-20 stsp if (err)
1352 3c29341b 2022-07-21 florian return err;
1353 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1354 f4994adc 2020-10-20 stsp free(parent);
1355 f35fa46a 2020-07-23 stsp if (err)
1356 3c29341b 2022-07-21 florian return err;
1357 f35fa46a 2020-07-23 stsp /*
1358 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1359 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1360 f35fa46a 2020-07-23 stsp */
1361 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1362 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1363 3c29341b 2022-07-21 florian return err;
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp }
1366 f35fa46a 2020-07-23 stsp
1367 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1368 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1369 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1370 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1371 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1372 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1373 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1375 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1376 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1377 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1378 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1379 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1380 8ba819a3 2020-07-23 stsp } else {
1381 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1382 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1383 8ba819a3 2020-07-23 stsp }
1384 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1385 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 8ba819a3 2020-07-23 stsp return err;
1388 8ba819a3 2020-07-23 stsp }
1389 8ba819a3 2020-07-23 stsp
1390 8ba819a3 2020-07-23 stsp static const struct got_error *
1391 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1393 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1394 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1395 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1396 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 8ba819a3 2020-07-23 stsp {
1398 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1399 507dc3bb 2018-12-29 stsp int fd = -1;
1400 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1401 507dc3bb 2018-12-29 stsp int update = 0;
1402 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1403 b2b3fce1 2022-10-29 op mode_t mode;
1404 8ba819a3 2020-07-23 stsp
1405 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1408 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1409 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1410 f5375317 2020-10-20 stsp char *parent;
1411 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1412 f5375317 2020-10-20 stsp if (err)
1413 f5375317 2020-10-20 stsp return err;
1414 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1415 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1416 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1417 f5375317 2020-10-20 stsp free(parent);
1418 21908da4 2019-01-13 stsp if (err)
1419 21908da4 2019-01-13 stsp return err;
1420 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1421 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 b2b3fce1 2022-10-29 op mode);
1423 21908da4 2019-01-13 stsp if (fd == -1)
1424 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1425 230a42bd 2019-05-11 jcs ondisk_path);
1426 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1427 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1428 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1429 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1430 3b9f0f87 2020-07-23 stsp goto done;
1431 3b9f0f87 2020-07-23 stsp }
1432 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1435 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1436 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1437 507dc3bb 2018-12-29 stsp goto done;
1438 d70b8e30 2018-12-27 stsp } else {
1439 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1440 b90054ed 2022-11-01 stsp ondisk_path, "");
1441 507dc3bb 2018-12-29 stsp if (err)
1442 507dc3bb 2018-12-29 stsp goto done;
1443 507dc3bb 2018-12-29 stsp update = 1;
1444 b2b3fce1 2022-10-29 op
1445 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1446 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1447 b2b3fce1 2022-10-29 op tmppath);
1448 b2b3fce1 2022-10-29 op goto done;
1449 b2b3fce1 2022-10-29 op }
1450 9d31a1d8 2018-03-11 stsp }
1451 507dc3bb 2018-12-29 stsp } else
1452 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1453 3818e3c4 2020-11-01 naddy }
1454 3818e3c4 2020-11-01 naddy
1455 bd6aa359 2020-07-23 stsp if (progress_cb) {
1456 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 bd6aa359 2020-07-23 stsp path);
1462 bd6aa359 2020-07-23 stsp else
1463 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1464 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 bd6aa359 2020-07-23 stsp if (err)
1466 bd6aa359 2020-07-23 stsp goto done;
1467 bd6aa359 2020-07-23 stsp }
1468 d7b62c98 2018-12-27 stsp
1469 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1470 9d31a1d8 2018-03-11 stsp do {
1471 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1473 9d31a1d8 2018-03-11 stsp if (err)
1474 9d31a1d8 2018-03-11 stsp break;
1475 9d31a1d8 2018-03-11 stsp if (len > 0) {
1476 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1477 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1479 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1480 b87c6f83 2018-12-24 stsp goto done;
1481 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1482 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1483 b87c6f83 2018-12-24 stsp goto done;
1484 9d31a1d8 2018-03-11 stsp }
1485 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1486 9d31a1d8 2018-03-11 stsp }
1487 9d31a1d8 2018-03-11 stsp } while (len != 0);
1488 9d31a1d8 2018-03-11 stsp
1489 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1490 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1491 816dc654 2019-02-16 stsp goto done;
1492 816dc654 2019-02-16 stsp }
1493 9d31a1d8 2018-03-11 stsp
1494 507dc3bb 2018-12-29 stsp if (update) {
1495 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1497 f6d8c0ac 2020-11-09 stsp goto done;
1498 f6d8c0ac 2020-11-09 stsp }
1499 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1501 230a42bd 2019-05-11 jcs ondisk_path);
1502 68ed9ba5 2019-02-10 stsp goto done;
1503 68ed9ba5 2019-02-10 stsp }
1504 63df146d 2020-11-09 stsp free(tmppath);
1505 63df146d 2020-11-09 stsp tmppath = NULL;
1506 507dc3bb 2018-12-29 stsp }
1507 507dc3bb 2018-12-29 stsp
1508 9d31a1d8 2018-03-11 stsp done:
1509 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1510 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1511 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1513 507dc3bb 2018-12-29 stsp free(tmppath);
1514 6353ad76 2019-02-08 stsp return err;
1515 6353ad76 2019-02-08 stsp }
1516 6353ad76 2019-02-08 stsp
1517 12383673 2023-02-18 mark /*
1518 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1519 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1520 12383673 2023-02-18 mark */
1521 6353ad76 2019-02-08 stsp static const struct got_error *
1522 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1523 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1524 12383673 2023-02-18 mark FILE *ondisk_file)
1525 7154f6ce 2019-03-27 stsp {
1526 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1527 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1530 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1531 7154f6ce 2019-03-27 stsp };
1532 d952957d 2023-02-19 mark FILE *f1 = NULL;
1533 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1534 d952957d 2023-02-19 mark struct diff_result *r;
1535 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1536 9bdd68dd 2021-01-02 naddy char *line = NULL;
1537 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1538 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1539 7154f6ce 2019-03-27 stsp
1540 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1541 d952957d 2023-02-19 mark return NULL;
1542 12383673 2023-02-18 mark
1543 d952957d 2023-02-19 mark f1 = got_opentemp();
1544 d952957d 2023-02-19 mark if (f1 == NULL)
1545 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1546 12383673 2023-02-18 mark
1547 d952957d 2023-02-19 mark if (blob) {
1548 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1549 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1550 12383673 2023-02-18 mark if (err)
1551 12383673 2023-02-18 mark goto done;
1552 d952957d 2023-02-19 mark }
1553 12383673 2023-02-18 mark
1554 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1555 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 d952957d 2023-02-19 mark if (err)
1557 d952957d 2023-02-19 mark goto done;
1558 d952957d 2023-02-19 mark
1559 d952957d 2023-02-19 mark r = diffreg_result->result;
1560 d952957d 2023-02-19 mark
1561 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1562 d952957d 2023-02-19 mark struct diff_chunk *c;
1563 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1564 7783f73c 2023-02-20 mark off_t pos;
1565 d952957d 2023-02-19 mark
1566 d952957d 2023-02-19 mark /*
1567 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1568 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1569 d952957d 2023-02-19 mark */
1570 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1571 d952957d 2023-02-19 mark
1572 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 d952957d 2023-02-19 mark nchunks_parsed = 1;
1574 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1575 7154f6ce 2019-03-27 stsp }
1576 7154f6ce 2019-03-27 stsp
1577 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1578 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1579 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 7783f73c 2023-02-20 mark goto done;
1581 7154f6ce 2019-03-27 stsp }
1582 d952957d 2023-02-19 mark
1583 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1584 7783f73c 2023-02-20 mark ln = cc.right.start;
1585 7783f73c 2023-02-20 mark
1586 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1587 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1588 d952957d 2023-02-19 mark if (linelen == -1) {
1589 d952957d 2023-02-19 mark if (feof(ondisk_file))
1590 d952957d 2023-02-19 mark break;
1591 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 d952957d 2023-02-19 mark break;
1593 d952957d 2023-02-19 mark }
1594 d952957d 2023-02-19 mark
1595 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1596 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1597 d952957d 2023-02-19 mark if (strcmp(markers[i],
1598 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1599 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1600 d952957d 2023-02-19 mark goto done;
1601 d952957d 2023-02-19 mark } else
1602 d952957d 2023-02-19 mark i++;
1603 d952957d 2023-02-19 mark }
1604 d952957d 2023-02-19 mark ++ln;
1605 d952957d 2023-02-19 mark }
1606 7154f6ce 2019-03-27 stsp }
1607 12383673 2023-02-18 mark
1608 12383673 2023-02-18 mark done:
1609 9bdd68dd 2021-01-02 naddy free(line);
1610 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1611 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1612 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1613 d952957d 2023-02-19 mark if (err == NULL)
1614 d952957d 2023-02-19 mark err = free_err;
1615 7154f6ce 2019-03-27 stsp
1616 7154f6ce 2019-03-27 stsp return err;
1617 e2b1e152 2019-07-13 stsp }
1618 e2b1e152 2019-07-13 stsp
1619 e2b1e152 2019-07-13 stsp static int
1620 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 1ebedb77 2019-10-19 stsp {
1622 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1623 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1624 1ebedb77 2019-10-19 stsp }
1625 1ebedb77 2019-10-19 stsp
1626 1ebedb77 2019-10-19 stsp static int
1627 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 e2b1e152 2019-07-13 stsp {
1629 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1630 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1631 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1632 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1633 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1634 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1635 c363b2c1 2019-08-03 stsp }
1636 c363b2c1 2019-08-03 stsp
1637 c363b2c1 2019-08-03 stsp static unsigned char
1638 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1639 c363b2c1 2019-08-03 stsp {
1640 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1641 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1642 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1645 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1647 c363b2c1 2019-08-03 stsp default:
1648 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1649 a919d5c4 2020-07-23 stsp }
1650 a919d5c4 2020-07-23 stsp }
1651 a919d5c4 2020-07-23 stsp
1652 a919d5c4 2020-07-23 stsp static const struct got_error *
1653 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1654 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1655 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1656 a919d5c4 2020-07-23 stsp {
1657 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1658 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1659 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1660 a919d5c4 2020-07-23 stsp ssize_t elen;
1661 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1662 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1663 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1666 a919d5c4 2020-07-23 stsp
1667 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1668 a919d5c4 2020-07-23 stsp do {
1669 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1670 a919d5c4 2020-07-23 stsp if (err)
1671 a919d5c4 2020-07-23 stsp return err;
1672 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1673 a919d5c4 2020-07-23 stsp /*
1674 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1675 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1676 a919d5c4 2020-07-23 stsp */
1677 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1678 a919d5c4 2020-07-23 stsp }
1679 a919d5c4 2020-07-23 stsp if (len > 0) {
1680 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1681 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1682 a919d5c4 2020-07-23 stsp len - hdrlen);
1683 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1684 a919d5c4 2020-07-23 stsp hdrlen = 0;
1685 a919d5c4 2020-07-23 stsp }
1686 a919d5c4 2020-07-23 stsp } while (len != 0);
1687 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1688 a919d5c4 2020-07-23 stsp
1689 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1690 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 a919d5c4 2020-07-23 stsp if (elen == -1)
1692 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1693 a919d5c4 2020-07-23 stsp } else {
1694 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1695 a919d5c4 2020-07-23 stsp if (elen == -1)
1696 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1697 c363b2c1 2019-08-03 stsp }
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1700 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1701 a919d5c4 2020-07-23 stsp
1702 a919d5c4 2020-07-23 stsp return NULL;
1703 7154f6ce 2019-03-27 stsp }
1704 7154f6ce 2019-03-27 stsp
1705 7154f6ce 2019-03-27 stsp static const struct got_error *
1706 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1707 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1708 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1709 6353ad76 2019-02-08 stsp {
1710 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1711 6353ad76 2019-02-08 stsp struct got_object_id id;
1712 6353ad76 2019-02-08 stsp size_t hdrlen;
1713 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1714 6353ad76 2019-02-08 stsp FILE *f = NULL;
1715 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1716 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1717 6353ad76 2019-02-08 stsp size_t flen, blen;
1718 2c4740ad 2023-02-12 mark unsigned char staged_status;
1719 6353ad76 2019-02-08 stsp
1720 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1721 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1722 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1723 6353ad76 2019-02-08 stsp
1724 7f91a133 2019-12-13 stsp /*
1725 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1726 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1727 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1728 7f91a133 2019-12-13 stsp */
1729 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1730 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1731 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1732 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1733 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1734 882ef1b9 2019-12-13 stsp else
1735 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1736 882ef1b9 2019-12-13 stsp goto done;
1737 882ef1b9 2019-12-13 stsp }
1738 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1739 3d35a492 2019-12-13 stsp goto done;
1740 3d35a492 2019-12-13 stsp }
1741 7f91a133 2019-12-13 stsp } else {
1742 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1744 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1745 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1746 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1747 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1748 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1749 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1750 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1751 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1752 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1753 3d35a492 2019-12-13 stsp else
1754 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1755 3d35a492 2019-12-13 stsp goto done;
1756 3d35a492 2019-12-13 stsp }
1757 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1758 1338848f 2019-12-13 stsp goto done;
1759 a378724f 2019-02-10 stsp }
1760 a378724f 2019-02-10 stsp }
1761 6353ad76 2019-02-08 stsp
1762 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1763 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1764 1338848f 2019-12-13 stsp goto done;
1765 3f148bc6 2019-07-27 stsp }
1766 efdd40df 2019-07-27 stsp
1767 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1768 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1769 1338848f 2019-12-13 stsp goto done;
1770 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1771 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1772 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1773 1338848f 2019-12-13 stsp goto done;
1774 d00136be 2019-03-26 stsp }
1775 b8f41171 2019-02-10 stsp
1776 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1777 f179e66d 2020-07-23 stsp goto done;
1778 f179e66d 2020-07-23 stsp
1779 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1780 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1781 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1782 1338848f 2019-12-13 stsp goto done;
1783 f179e66d 2020-07-23 stsp }
1784 6353ad76 2019-02-08 stsp
1785 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1786 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1787 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 c363b2c1 2019-08-03 stsp else
1789 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1790 c363b2c1 2019-08-03 stsp
1791 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1792 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1793 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1794 eb81bc23 2022-06-28 tracey goto done;
1795 eb81bc23 2022-06-28 tracey }
1796 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 6353ad76 2019-02-08 stsp if (err)
1798 1338848f 2019-12-13 stsp goto done;
1799 6353ad76 2019-02-08 stsp
1800 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1801 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1802 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1803 a919d5c4 2020-07-23 stsp goto done;
1804 a919d5c4 2020-07-23 stsp }
1805 a919d5c4 2020-07-23 stsp
1806 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1807 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 ab0d4361 2019-12-13 stsp if (fd == -1) {
1809 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1810 ab0d4361 2019-12-13 stsp goto done;
1811 ab0d4361 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp }
1813 3d35a492 2019-12-13 stsp
1814 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1815 6353ad76 2019-02-08 stsp if (f == NULL) {
1816 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1817 6353ad76 2019-02-08 stsp goto done;
1818 6353ad76 2019-02-08 stsp }
1819 1338848f 2019-12-13 stsp fd = -1;
1820 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1821 656b1f76 2019-05-11 jcs for (;;) {
1822 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1824 6353ad76 2019-02-08 stsp if (err)
1825 7154f6ce 2019-03-27 stsp goto done;
1826 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1827 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1829 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1830 7154f6ce 2019-03-27 stsp goto done;
1831 80c5c120 2019-02-19 stsp }
1832 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1833 6353ad76 2019-02-08 stsp if (flen != 0)
1834 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1835 6353ad76 2019-02-08 stsp break;
1836 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1837 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1838 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1839 6353ad76 2019-02-08 stsp break;
1840 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1841 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1842 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1844 6353ad76 2019-02-08 stsp break;
1845 6353ad76 2019-02-08 stsp }
1846 6353ad76 2019-02-08 stsp } else {
1847 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1848 6353ad76 2019-02-08 stsp break;
1849 6353ad76 2019-02-08 stsp }
1850 6353ad76 2019-02-08 stsp hdrlen = 0;
1851 6353ad76 2019-02-08 stsp }
1852 7154f6ce 2019-03-27 stsp
1853 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1854 7154f6ce 2019-03-27 stsp rewind(f);
1855 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1856 12383673 2023-02-18 mark sb, f);
1857 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1858 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1859 6353ad76 2019-02-08 stsp done:
1860 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1861 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1862 6353ad76 2019-02-08 stsp if (blob)
1863 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1864 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1865 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1866 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1867 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1868 9d31a1d8 2018-03-11 stsp return err;
1869 e2b1e152 2019-07-13 stsp }
1870 e2b1e152 2019-07-13 stsp
1871 e2b1e152 2019-07-13 stsp /*
1872 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1873 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1874 e2b1e152 2019-07-13 stsp */
1875 e2b1e152 2019-07-13 stsp static const struct got_error *
1876 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1877 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1878 e2b1e152 2019-07-13 stsp {
1879 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1880 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1881 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1882 e2b1e152 2019-07-13 stsp
1883 e2b1e152 2019-07-13 stsp return NULL;
1884 9d31a1d8 2018-03-11 stsp }
1885 9d31a1d8 2018-03-11 stsp
1886 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1887 07fa9365 2023-03-10 stsp
1888 9d31a1d8 2018-03-11 stsp static const struct got_error *
1889 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1890 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1892 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 0584f854 2019-04-06 stsp void *progress_arg)
1894 9d31a1d8 2018-03-11 stsp {
1895 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1896 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1897 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1898 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1899 b8f41171 2019-02-10 stsp struct stat sb;
1900 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1901 9d31a1d8 2018-03-11 stsp
1902 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1904 6353ad76 2019-02-08 stsp
1905 abb4604f 2019-07-27 stsp if (ie) {
1906 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 a76c42e6 2019-08-03 stsp goto done;
1909 a76c42e6 2019-08-03 stsp }
1910 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 7f91a133 2019-12-13 stsp repo);
1912 abb4604f 2019-07-27 stsp if (err)
1913 abb4604f 2019-07-27 stsp goto done;
1914 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1916 c90c8ce3 2020-07-23 stsp } else {
1917 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1918 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1919 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1920 f6764181 2021-09-24 stsp ondisk_path);
1921 f6764181 2021-09-24 stsp goto done;
1922 f6764181 2021-09-24 stsp }
1923 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1925 f6764181 2021-09-24 stsp } else {
1926 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1928 f6764181 2021-09-24 stsp else
1929 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1930 f6764181 2021-09-24 stsp }
1931 c90c8ce3 2020-07-23 stsp }
1932 6353ad76 2019-02-08 stsp
1933 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1934 2c41dce7 2021-06-27 stsp if (ie)
1935 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1936 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1937 b8f41171 2019-02-10 stsp goto done;
1938 b8f41171 2019-02-10 stsp }
1939 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1940 a769b60b 2021-06-27 stsp if (ie)
1941 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1942 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1943 5036ab18 2020-04-18 stsp path);
1944 5036ab18 2020-04-18 stsp goto done;
1945 07fa9365 2023-03-10 stsp }
1946 07fa9365 2023-03-10 stsp
1947 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1948 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1949 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1950 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1951 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1952 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1953 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1954 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1955 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1956 07fa9365 2023-03-10 stsp } else if (ie) {
1957 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1958 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1959 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1960 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1961 07fa9365 2023-03-10 stsp ie->path);
1962 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1963 07fa9365 2023-03-10 stsp errno == ENOENT))
1964 07fa9365 2023-03-10 stsp goto done;
1965 07fa9365 2023-03-10 stsp }
1966 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1967 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1968 07fa9365 2023-03-10 stsp ie->path);
1969 07fa9365 2023-03-10 stsp }
1970 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1971 5036ab18 2020-04-18 stsp }
1972 b8f41171 2019-02-10 stsp
1973 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1974 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1975 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1976 c6e8a826 2021-04-05 stsp /*
1977 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1978 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1979 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1980 c6e8a826 2021-04-05 stsp * updating contents of this file.
1981 c6e8a826 2021-04-05 stsp */
1982 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1983 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1984 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1985 c6e8a826 2021-04-05 stsp /* Same commit. */
1986 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1987 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1988 e2b1e152 2019-07-13 stsp if (err)
1989 e2b1e152 2019-07-13 stsp goto done;
1990 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 b8f41171 2019-02-10 stsp path);
1992 6353ad76 2019-02-08 stsp goto done;
1993 a378724f 2019-02-10 stsp }
1994 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1995 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1996 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1997 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1998 fd785a9a 2023-04-12 stsp if (got_fileindex_entry_has_commit(ie)) {
1999 fd785a9a 2023-04-12 stsp /* Update the base commit ID of this file. */
2000 fd785a9a 2023-04-12 stsp memcpy(ie->commit_sha1,
2001 fd785a9a 2023-04-12 stsp worktree->base_commit_id->sha1,
2002 fd785a9a 2023-04-12 stsp sizeof(ie->commit_sha1));
2003 fd785a9a 2023-04-12 stsp }
2004 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2005 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2006 0f58026f 2021-04-05 stsp if (err)
2007 0f58026f 2021-04-05 stsp goto done;
2008 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2009 0f58026f 2021-04-05 stsp path);
2010 b8f41171 2019-02-10 stsp goto done;
2011 e2b1e152 2019-07-13 stsp }
2012 9d31a1d8 2018-03-11 stsp }
2013 9d31a1d8 2018-03-11 stsp
2014 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2015 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2016 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2017 eb81bc23 2022-06-28 tracey goto done;
2018 eb81bc23 2022-06-28 tracey }
2019 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2020 8da9e5f4 2019-01-12 stsp if (err)
2021 6353ad76 2019-02-08 stsp goto done;
2022 512f0d0e 2019-01-02 stsp
2023 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2024 234035bc 2019-06-01 stsp int update_timestamps;
2025 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2026 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2027 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2028 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2029 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2030 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2031 eb81bc23 2022-06-28 tracey goto done;
2032 eb81bc23 2022-06-28 tracey }
2033 234035bc 2019-06-01 stsp struct got_object_id id2;
2034 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2035 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2036 eb81bc23 2022-06-28 tracey fd2);
2037 234035bc 2019-06-01 stsp if (err)
2038 f69721c3 2019-10-21 stsp goto done;
2039 f69721c3 2019-10-21 stsp }
2040 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2041 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2042 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2043 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2044 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2045 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2046 f69721c3 2019-10-21 stsp goto done;
2047 f69721c3 2019-10-21 stsp }
2048 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2049 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2050 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2051 234035bc 2019-06-01 stsp goto done;
2052 f69721c3 2019-10-21 stsp }
2053 234035bc 2019-06-01 stsp }
2054 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2055 36bf999c 2020-07-23 stsp char *link_target;
2056 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2057 36bf999c 2020-07-23 stsp if (err)
2058 36bf999c 2020-07-23 stsp goto done;
2059 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2060 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2061 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2062 36bf999c 2020-07-23 stsp free(link_target);
2063 993e2a1b 2020-07-23 stsp } else {
2064 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2065 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2066 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2067 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2068 993e2a1b 2020-07-23 stsp }
2069 f69721c3 2019-10-21 stsp free(label_orig);
2070 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2071 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2072 eb81bc23 2022-06-28 tracey goto done;
2073 eb81bc23 2022-06-28 tracey }
2074 234035bc 2019-06-01 stsp if (blob2)
2075 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2076 909d120e 2019-09-22 stsp if (err)
2077 909d120e 2019-09-22 stsp goto done;
2078 234035bc 2019-06-01 stsp /*
2079 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2080 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2081 234035bc 2019-06-01 stsp * unmodified files again.
2082 234035bc 2019-06-01 stsp */
2083 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2084 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2085 234035bc 2019-06-01 stsp update_timestamps);
2086 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2087 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2088 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2089 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2090 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2091 1ee397ad 2019-07-12 stsp if (err)
2092 1ee397ad 2019-07-12 stsp goto done;
2093 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2095 13d9040b 2019-03-27 stsp if (err)
2096 13d9040b 2019-03-27 stsp goto done;
2097 13d9040b 2019-03-27 stsp } else {
2098 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2099 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2100 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2101 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2102 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2103 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2104 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2105 2e1fa222 2020-07-23 stsp } else {
2106 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2107 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2108 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2109 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2110 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2111 2e1fa222 2020-07-23 stsp }
2112 13d9040b 2019-03-27 stsp if (err)
2113 13d9040b 2019-03-27 stsp goto done;
2114 65b05cec 2020-07-23 stsp
2115 054041d0 2020-06-24 stsp if (ie) {
2116 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2117 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2118 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2119 054041d0 2020-06-24 stsp } else {
2120 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2121 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2122 054041d0 2020-06-24 stsp &blob->id);
2123 054041d0 2020-06-24 stsp }
2124 13d9040b 2019-03-27 stsp if (err)
2125 13d9040b 2019-03-27 stsp goto done;
2126 65b05cec 2020-07-23 stsp
2127 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2128 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2129 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2130 2e1fa222 2020-07-23 stsp }
2131 13d9040b 2019-03-27 stsp }
2132 eb81bc23 2022-06-28 tracey
2133 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2134 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2135 eb81bc23 2022-06-28 tracey goto done;
2136 eb81bc23 2022-06-28 tracey }
2137 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2138 6353ad76 2019-02-08 stsp done:
2139 6353ad76 2019-02-08 stsp free(ondisk_path);
2140 8da9e5f4 2019-01-12 stsp return err;
2141 90285c3b 2019-01-08 stsp }
2142 90285c3b 2019-01-08 stsp
2143 90285c3b 2019-01-08 stsp static const struct got_error *
2144 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2145 90285c3b 2019-01-08 stsp {
2146 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2147 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2148 90285c3b 2019-01-08 stsp
2149 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2150 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2151 90285c3b 2019-01-08 stsp
2152 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2153 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2154 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2155 c97665ae 2019-01-13 stsp } else {
2156 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2157 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2158 1c4cdd89 2021-06-20 stsp if (err)
2159 1c4cdd89 2021-06-20 stsp goto done;
2160 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2161 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2162 fddefe3b 2020-10-20 stsp free(ondisk_path);
2163 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2164 1c4cdd89 2021-06-20 stsp parent = NULL;
2165 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2166 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2167 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2168 fddefe3b 2020-10-20 stsp ondisk_path);
2169 90285c3b 2019-01-08 stsp break;
2170 90285c3b 2019-01-08 stsp }
2171 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2172 1c4cdd89 2021-06-20 stsp if (err)
2173 1c4cdd89 2021-06-20 stsp break;
2174 1c4cdd89 2021-06-20 stsp }
2175 90285c3b 2019-01-08 stsp }
2176 1c4cdd89 2021-06-20 stsp done:
2177 90285c3b 2019-01-08 stsp free(ondisk_path);
2178 1c4cdd89 2021-06-20 stsp free(parent);
2179 90285c3b 2019-01-08 stsp return err;
2180 512f0d0e 2019-01-02 stsp }
2181 512f0d0e 2019-01-02 stsp
2182 708d8e67 2019-03-27 stsp static const struct got_error *
2183 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2184 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2185 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2186 708d8e67 2019-03-27 stsp {
2187 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2188 708d8e67 2019-03-27 stsp unsigned char status;
2189 708d8e67 2019-03-27 stsp struct stat sb;
2190 708d8e67 2019-03-27 stsp char *ondisk_path;
2191 708d8e67 2019-03-27 stsp
2192 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2193 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2194 a76c42e6 2019-08-03 stsp
2195 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2196 708d8e67 2019-03-27 stsp == -1)
2197 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2198 708d8e67 2019-03-27 stsp
2199 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2200 708d8e67 2019-03-27 stsp if (err)
2201 5a58a424 2020-06-23 stsp goto done;
2202 708d8e67 2019-03-27 stsp
2203 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2204 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2205 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2206 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2207 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2208 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2209 993e2a1b 2020-07-23 stsp goto done;
2210 993e2a1b 2020-07-23 stsp }
2211 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2212 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2213 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2214 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2215 993e2a1b 2020-07-23 stsp if (err)
2216 993e2a1b 2020-07-23 stsp goto done;
2217 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2218 993e2a1b 2020-07-23 stsp ie->path);
2219 993e2a1b 2020-07-23 stsp goto done;
2220 993e2a1b 2020-07-23 stsp }
2221 993e2a1b 2020-07-23 stsp
2222 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2223 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2224 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2225 1ee397ad 2019-07-12 stsp if (err)
2226 5a58a424 2020-06-23 stsp goto done;
2227 fc6346c4 2019-03-27 stsp /*
2228 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2229 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2230 fc6346c4 2019-03-27 stsp */
2231 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2232 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2233 fc6346c4 2019-03-27 stsp } else {
2234 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2235 1ee397ad 2019-07-12 stsp if (err)
2236 5a58a424 2020-06-23 stsp goto done;
2237 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2238 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2239 fc6346c4 2019-03-27 stsp if (err)
2240 5a58a424 2020-06-23 stsp goto done;
2241 fc6346c4 2019-03-27 stsp }
2242 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2243 708d8e67 2019-03-27 stsp }
2244 5a58a424 2020-06-23 stsp done:
2245 5a58a424 2020-06-23 stsp free(ondisk_path);
2246 fc6346c4 2019-03-27 stsp return err;
2247 708d8e67 2019-03-27 stsp }
2248 708d8e67 2019-03-27 stsp
2249 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2250 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2251 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2252 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2253 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2254 8da9e5f4 2019-01-12 stsp void *progress_arg;
2255 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2256 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2257 8da9e5f4 2019-01-12 stsp };
2258 8da9e5f4 2019-01-12 stsp
2259 512f0d0e 2019-01-02 stsp static const struct got_error *
2260 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2261 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2262 512f0d0e 2019-01-02 stsp {
2263 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2264 0584f854 2019-04-06 stsp
2265 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2266 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2267 512f0d0e 2019-01-02 stsp
2268 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2269 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2270 8da9e5f4 2019-01-12 stsp }
2271 512f0d0e 2019-01-02 stsp
2272 8da9e5f4 2019-01-12 stsp static const struct got_error *
2273 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2274 8da9e5f4 2019-01-12 stsp {
2275 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2276 7a9df742 2019-01-08 stsp
2277 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2278 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2279 0584f854 2019-04-06 stsp
2280 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2281 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2282 9d31a1d8 2018-03-11 stsp }
2283 9d31a1d8 2018-03-11 stsp
2284 9d31a1d8 2018-03-11 stsp static const struct got_error *
2285 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2286 9d31a1d8 2018-03-11 stsp {
2287 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2288 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2289 8da9e5f4 2019-01-12 stsp char *path;
2290 9d31a1d8 2018-03-11 stsp
2291 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2292 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2293 0584f854 2019-04-06 stsp
2294 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2295 63c5ca5d 2019-08-24 stsp return NULL;
2296 63c5ca5d 2019-08-24 stsp
2297 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2298 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2299 8da9e5f4 2019-01-12 stsp == -1)
2300 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2301 9d31a1d8 2018-03-11 stsp
2302 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2303 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2304 8da9e5f4 2019-01-12 stsp else
2305 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2306 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2307 9d31a1d8 2018-03-11 stsp
2308 8da9e5f4 2019-01-12 stsp free(path);
2309 0cd1c46a 2019-03-11 stsp return err;
2310 0cd1c46a 2019-03-11 stsp }
2311 0cd1c46a 2019-03-11 stsp
2312 b2118c49 2020-07-28 stsp const struct got_error *
2313 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2314 b2118c49 2020-07-28 stsp {
2315 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2316 b2118c49 2020-07-28 stsp
2317 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2318 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2319 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2320 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2321 b2118c49 2020-07-28 stsp }
2322 b2118c49 2020-07-28 stsp
2323 b2118c49 2020-07-28 stsp return NULL;
2324 b2118c49 2020-07-28 stsp }
2325 b2118c49 2020-07-28 stsp
2326 818c7501 2019-07-11 stsp static const struct got_error *
2327 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2328 0cd1c46a 2019-03-11 stsp {
2329 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2330 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2331 0cd1c46a 2019-03-11 stsp
2332 517bab73 2019-03-11 stsp *refname = NULL;
2333 517bab73 2019-03-11 stsp
2334 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2335 b2118c49 2020-07-28 stsp if (err)
2336 b2118c49 2020-07-28 stsp return err;
2337 0cd1c46a 2019-03-11 stsp
2338 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2340 0647c563 2019-03-11 stsp *refname = NULL;
2341 0cd1c46a 2019-03-11 stsp }
2342 517bab73 2019-03-11 stsp free(uuidstr);
2343 517bab73 2019-03-11 stsp return err;
2344 517bab73 2019-03-11 stsp }
2345 517bab73 2019-03-11 stsp
2346 818c7501 2019-07-11 stsp const struct got_error *
2347 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2348 9587e6cc 2023-01-28 mark const char *prefix)
2349 9587e6cc 2023-01-28 mark {
2350 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2351 9587e6cc 2023-01-28 mark }
2352 9587e6cc 2023-01-28 mark
2353 5662d61d 2023-07-03 jrick static const struct got_error *
2354 5662d61d 2023-07-03 jrick get_base_ref_name(char **refname, struct got_worktree *worktree)
2355 818c7501 2019-07-11 stsp {
2356 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2357 818c7501 2019-07-11 stsp }
2358 818c7501 2019-07-11 stsp
2359 818c7501 2019-07-11 stsp static const struct got_error *
2360 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2361 818c7501 2019-07-11 stsp {
2362 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2363 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2364 818c7501 2019-07-11 stsp }
2365 818c7501 2019-07-11 stsp
2366 818c7501 2019-07-11 stsp static const struct got_error *
2367 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2368 818c7501 2019-07-11 stsp {
2369 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2370 818c7501 2019-07-11 stsp }
2371 818c7501 2019-07-11 stsp
2372 818c7501 2019-07-11 stsp static const struct got_error *
2373 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2374 818c7501 2019-07-11 stsp {
2375 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2376 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2377 818c7501 2019-07-11 stsp }
2378 818c7501 2019-07-11 stsp
2379 818c7501 2019-07-11 stsp static const struct got_error *
2380 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 818c7501 2019-07-11 stsp {
2382 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2383 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2384 0ebf8283 2019-07-24 stsp }
2385 0ebf8283 2019-07-24 stsp
2386 0ebf8283 2019-07-24 stsp static const struct got_error *
2387 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2388 0ebf8283 2019-07-24 stsp {
2389 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2390 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2391 0ebf8283 2019-07-24 stsp }
2392 0ebf8283 2019-07-24 stsp
2393 0ebf8283 2019-07-24 stsp static const struct got_error *
2394 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2395 0ebf8283 2019-07-24 stsp {
2396 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2397 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2398 0ebf8283 2019-07-24 stsp }
2399 0ebf8283 2019-07-24 stsp
2400 0ebf8283 2019-07-24 stsp static const struct got_error *
2401 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 0ebf8283 2019-07-24 stsp {
2403 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2404 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2405 818c7501 2019-07-11 stsp }
2406 818c7501 2019-07-11 stsp
2407 0ebf8283 2019-07-24 stsp static const struct got_error *
2408 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2409 0ebf8283 2019-07-24 stsp {
2410 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2411 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2412 0ebf8283 2019-07-24 stsp }
2413 818c7501 2019-07-11 stsp
2414 0ebf8283 2019-07-24 stsp const struct got_error *
2415 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2416 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2417 0ebf8283 2019-07-24 stsp {
2418 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2419 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2420 0ebf8283 2019-07-24 stsp *path = NULL;
2421 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2422 0ebf8283 2019-07-24 stsp }
2423 0ebf8283 2019-07-24 stsp return NULL;
2424 f259c4c1 2021-09-24 stsp }
2425 f259c4c1 2021-09-24 stsp
2426 f259c4c1 2021-09-24 stsp static const struct got_error *
2427 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2428 f259c4c1 2021-09-24 stsp {
2429 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2430 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2431 0ebf8283 2019-07-24 stsp }
2432 0ebf8283 2019-07-24 stsp
2433 f259c4c1 2021-09-24 stsp static const struct got_error *
2434 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2435 f259c4c1 2021-09-24 stsp {
2436 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2437 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2438 f259c4c1 2021-09-24 stsp }
2439 f259c4c1 2021-09-24 stsp
2440 517bab73 2019-03-11 stsp /*
2441 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2442 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2443 517bab73 2019-03-11 stsp */
2444 517bab73 2019-03-11 stsp static const struct got_error *
2445 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2446 517bab73 2019-03-11 stsp {
2447 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2448 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2449 517bab73 2019-03-11 stsp char *refname;
2450 517bab73 2019-03-11 stsp
2451 5662d61d 2023-07-03 jrick err = get_base_ref_name(&refname, worktree);
2452 517bab73 2019-03-11 stsp if (err)
2453 517bab73 2019-03-11 stsp return err;
2454 0cd1c46a 2019-03-11 stsp
2455 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2456 0cd1c46a 2019-03-11 stsp if (err)
2457 0cd1c46a 2019-03-11 stsp goto done;
2458 0cd1c46a 2019-03-11 stsp
2459 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2460 0cd1c46a 2019-03-11 stsp done:
2461 0cd1c46a 2019-03-11 stsp free(refname);
2462 0cd1c46a 2019-03-11 stsp if (ref)
2463 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2464 3e3a69f1 2019-07-25 stsp return err;
2465 3e3a69f1 2019-07-25 stsp }
2466 3e3a69f1 2019-07-25 stsp
2467 3e3a69f1 2019-07-25 stsp static const struct got_error *
2468 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2469 3e3a69f1 2019-07-25 stsp {
2470 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2471 3e3a69f1 2019-07-25 stsp
2472 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2473 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2474 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2475 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2476 3e3a69f1 2019-07-25 stsp }
2477 9d31a1d8 2018-03-11 stsp return err;
2478 9d31a1d8 2018-03-11 stsp }
2479 9d31a1d8 2018-03-11 stsp
2480 3e3a69f1 2019-07-25 stsp
2481 ebf99748 2019-05-09 stsp static const struct got_error *
2482 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2483 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2484 ebf99748 2019-05-09 stsp {
2485 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2486 ebf99748 2019-05-09 stsp FILE *index = NULL;
2487 0cd1c46a 2019-03-11 stsp
2488 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2489 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2490 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2491 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2492 ebf99748 2019-05-09 stsp
2493 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2494 3e3a69f1 2019-07-25 stsp if (err)
2495 ebf99748 2019-05-09 stsp goto done;
2496 ebf99748 2019-05-09 stsp
2497 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2498 ebf99748 2019-05-09 stsp if (index == NULL) {
2499 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2500 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2501 ebf99748 2019-05-09 stsp } else {
2502 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2503 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2504 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2505 ebf99748 2019-05-09 stsp }
2506 ebf99748 2019-05-09 stsp done:
2507 ebf99748 2019-05-09 stsp if (err) {
2508 ebf99748 2019-05-09 stsp free(*fileindex_path);
2509 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2510 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2511 ebf99748 2019-05-09 stsp *fileindex = NULL;
2512 ebf99748 2019-05-09 stsp }
2513 ebf99748 2019-05-09 stsp return err;
2514 ebf99748 2019-05-09 stsp }
2515 c932eeeb 2019-05-22 stsp
2516 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2517 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2518 c932eeeb 2019-05-22 stsp const char *path;
2519 c932eeeb 2019-05-22 stsp size_t path_len;
2520 c932eeeb 2019-05-22 stsp const char *entry_name;
2521 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2522 a484d721 2019-06-10 stsp void *progress_arg;
2523 c932eeeb 2019-05-22 stsp };
2524 ebf99748 2019-05-09 stsp
2525 c932eeeb 2019-05-22 stsp static const struct got_error *
2526 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2527 c932eeeb 2019-05-22 stsp {
2528 1ee397ad 2019-07-12 stsp const struct got_error *err;
2529 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2530 c932eeeb 2019-05-22 stsp
2531 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2532 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2533 c932eeeb 2019-05-22 stsp return NULL;
2534 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2535 102ff934 2019-06-11 stsp return NULL;
2536 102ff934 2019-06-11 stsp
2537 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2538 a769b60b 2021-06-27 stsp return NULL;
2539 a769b60b 2021-06-27 stsp
2540 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2541 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2542 c932eeeb 2019-05-22 stsp return NULL;
2543 c932eeeb 2019-05-22 stsp
2544 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2545 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2546 edd02c5e 2019-07-11 stsp ie->path);
2547 1ee397ad 2019-07-12 stsp if (err)
2548 1ee397ad 2019-07-12 stsp return err;
2549 1ee397ad 2019-07-12 stsp }
2550 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2551 c932eeeb 2019-05-22 stsp return NULL;
2552 a615e0e7 2020-12-16 stsp }
2553 a615e0e7 2020-12-16 stsp
2554 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2555 a615e0e7 2020-12-16 stsp static const struct got_error *
2556 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2557 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2558 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2559 a615e0e7 2020-12-16 stsp {
2560 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2561 a615e0e7 2020-12-16 stsp
2562 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2563 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2564 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2565 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2566 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2567 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2568 a615e0e7 2020-12-16 stsp
2569 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2570 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2571 9c6338c4 2019-06-02 stsp }
2572 9c6338c4 2019-06-02 stsp
2573 9c6338c4 2019-06-02 stsp static const struct got_error *
2574 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2575 9c6338c4 2019-06-02 stsp {
2576 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2577 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2578 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2579 867630bb 2020-01-17 stsp struct timespec timeout;
2580 9c6338c4 2019-06-02 stsp
2581 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2582 b90054ed 2022-11-01 stsp fileindex_path, "");
2583 9c6338c4 2019-06-02 stsp if (err)
2584 9c6338c4 2019-06-02 stsp goto done;
2585 9c6338c4 2019-06-02 stsp
2586 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2587 9c6338c4 2019-06-02 stsp if (err)
2588 9c6338c4 2019-06-02 stsp goto done;
2589 9c6338c4 2019-06-02 stsp
2590 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2591 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2592 9c6338c4 2019-06-02 stsp fileindex_path);
2593 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2594 9c6338c4 2019-06-02 stsp }
2595 867630bb 2020-01-17 stsp
2596 867630bb 2020-01-17 stsp /*
2597 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2598 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2599 867630bb 2020-01-17 stsp * was recorded in the file index.
2600 867630bb 2020-01-17 stsp */
2601 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2602 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2603 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2604 9c6338c4 2019-06-02 stsp done:
2605 9c6338c4 2019-06-02 stsp if (new_index)
2606 9c6338c4 2019-06-02 stsp fclose(new_index);
2607 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2608 9c6338c4 2019-06-02 stsp return err;
2609 c932eeeb 2019-05-22 stsp }
2610 6ced4176 2019-07-12 stsp
2611 6ced4176 2019-07-12 stsp static const struct got_error *
2612 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2613 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2614 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2615 a44927cc 2022-04-07 stsp struct got_repository *repo)
2616 6ced4176 2019-07-12 stsp {
2617 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2618 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2619 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2620 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2621 6ced4176 2019-07-12 stsp
2622 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2623 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2624 6ced4176 2019-07-12 stsp *tree_id = NULL;
2625 6ced4176 2019-07-12 stsp
2626 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2627 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2628 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2629 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2630 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2631 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2632 6ced4176 2019-07-12 stsp goto done;
2633 6ced4176 2019-07-12 stsp }
2634 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2635 a44927cc 2022-04-07 stsp worktree->path_prefix);
2636 6ced4176 2019-07-12 stsp if (err)
2637 6ced4176 2019-07-12 stsp goto done;
2638 6ced4176 2019-07-12 stsp return NULL;
2639 6ced4176 2019-07-12 stsp }
2640 6ced4176 2019-07-12 stsp
2641 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2642 6ced4176 2019-07-12 stsp
2643 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2644 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2645 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2646 6ced4176 2019-07-12 stsp goto done;
2647 6ced4176 2019-07-12 stsp }
2648 6ced4176 2019-07-12 stsp
2649 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2650 6ced4176 2019-07-12 stsp if (err)
2651 6ced4176 2019-07-12 stsp goto done;
2652 6ced4176 2019-07-12 stsp
2653 6ced4176 2019-07-12 stsp free(in_repo_path);
2654 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2655 6ced4176 2019-07-12 stsp
2656 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2657 6ced4176 2019-07-12 stsp if (err)
2658 6ced4176 2019-07-12 stsp goto done;
2659 c932eeeb 2019-05-22 stsp
2660 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2661 6ced4176 2019-07-12 stsp /* Check out a single file. */
2662 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2663 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2664 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2665 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2670 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2671 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2672 6ced4176 2019-07-12 stsp goto done;
2673 6ced4176 2019-07-12 stsp }
2674 6ced4176 2019-07-12 stsp } else {
2675 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2676 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2677 6ced4176 2019-07-12 stsp if (err)
2678 6ced4176 2019-07-12 stsp return err;
2679 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2680 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2681 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2682 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2683 6ced4176 2019-07-12 stsp goto done;
2684 6ced4176 2019-07-12 stsp }
2685 6ced4176 2019-07-12 stsp }
2686 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2687 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2688 6ced4176 2019-07-12 stsp } else {
2689 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2690 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2691 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2692 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2693 6ced4176 2019-07-12 stsp goto done;
2694 6ced4176 2019-07-12 stsp }
2695 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2696 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2698 6ced4176 2019-07-12 stsp goto done;
2699 6ced4176 2019-07-12 stsp }
2700 6ced4176 2019-07-12 stsp }
2701 6ced4176 2019-07-12 stsp done:
2702 6ced4176 2019-07-12 stsp free(id);
2703 6ced4176 2019-07-12 stsp free(in_repo_path);
2704 6ced4176 2019-07-12 stsp if (err) {
2705 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2706 6ced4176 2019-07-12 stsp free(*tree_relpath);
2707 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2708 6ced4176 2019-07-12 stsp free(*tree_id);
2709 6ced4176 2019-07-12 stsp *tree_id = NULL;
2710 6ced4176 2019-07-12 stsp }
2711 07ed472d 2019-07-12 stsp return err;
2712 07ed472d 2019-07-12 stsp }
2713 07ed472d 2019-07-12 stsp
2714 07ed472d 2019-07-12 stsp static const struct got_error *
2715 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2716 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2717 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2718 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2719 07ed472d 2019-07-12 stsp {
2720 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2721 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2722 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2723 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2724 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2725 07ed472d 2019-07-12 stsp
2726 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2727 7f47418f 2019-12-20 stsp if (err) {
2728 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2729 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2730 7f47418f 2019-12-20 stsp goto done;
2731 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2732 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2733 7f47418f 2019-12-20 stsp if (err)
2734 7f47418f 2019-12-20 stsp return err;
2735 7f47418f 2019-12-20 stsp }
2736 07ed472d 2019-07-12 stsp
2737 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2738 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2739 07ed472d 2019-07-12 stsp if (err)
2740 07ed472d 2019-07-12 stsp goto done;
2741 07ed472d 2019-07-12 stsp
2742 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2743 07ed472d 2019-07-12 stsp if (err)
2744 07ed472d 2019-07-12 stsp goto done;
2745 07ed472d 2019-07-12 stsp
2746 07ed472d 2019-07-12 stsp if (entry_name &&
2747 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2748 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2749 07ed472d 2019-07-12 stsp goto done;
2750 07ed472d 2019-07-12 stsp }
2751 07ed472d 2019-07-12 stsp
2752 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2753 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2754 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2755 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2756 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2757 07ed472d 2019-07-12 stsp arg.repo = repo;
2758 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2759 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2760 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2761 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2762 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2763 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2764 07ed472d 2019-07-12 stsp done:
2765 07ed472d 2019-07-12 stsp if (tree)
2766 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2767 07ed472d 2019-07-12 stsp if (commit)
2768 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2769 6ced4176 2019-07-12 stsp return err;
2770 6ced4176 2019-07-12 stsp }
2771 6ced4176 2019-07-12 stsp
2772 9d31a1d8 2018-03-11 stsp const struct got_error *
2773 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2774 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2775 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2776 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2777 9d31a1d8 2018-03-11 stsp {
2778 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2779 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2780 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2781 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2782 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2783 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2784 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2785 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2786 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2787 f2ea84fa 2019-07-27 stsp int entry_type;
2788 f2ea84fa 2019-07-27 stsp char *relpath;
2789 f2ea84fa 2019-07-27 stsp char *entry_name;
2790 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2791 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2792 9d31a1d8 2018-03-11 stsp
2793 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2794 f2ea84fa 2019-07-27 stsp
2795 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2796 9d31a1d8 2018-03-11 stsp if (err)
2797 9d31a1d8 2018-03-11 stsp return err;
2798 a44927cc 2022-04-07 stsp
2799 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2800 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2801 a44927cc 2022-04-07 stsp if (err)
2802 a44927cc 2022-04-07 stsp goto done;
2803 93a30277 2018-12-24 stsp
2804 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2805 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2806 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2807 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2808 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2809 f2ea84fa 2019-07-27 stsp goto done;
2810 f2ea84fa 2019-07-27 stsp }
2811 6ced4176 2019-07-12 stsp
2812 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2813 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2814 a44927cc 2022-04-07 stsp worktree, repo);
2815 f2ea84fa 2019-07-27 stsp if (err) {
2816 f2ea84fa 2019-07-27 stsp free(tpd);
2817 6ced4176 2019-07-12 stsp goto done;
2818 6ced4176 2019-07-12 stsp }
2819 f2ea84fa 2019-07-27 stsp
2820 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2821 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2822 f2ea84fa 2019-07-27 stsp if (err) {
2823 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2824 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2825 f2ea84fa 2019-07-27 stsp free(tpd);
2826 f2ea84fa 2019-07-27 stsp goto done;
2827 f2ea84fa 2019-07-27 stsp }
2828 f2ea84fa 2019-07-27 stsp } else
2829 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2830 f2ea84fa 2019-07-27 stsp
2831 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2832 6ced4176 2019-07-12 stsp }
2833 6ced4176 2019-07-12 stsp
2834 51514078 2018-12-25 stsp /*
2835 51514078 2018-12-25 stsp * Read the file index.
2836 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2837 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2838 51514078 2018-12-25 stsp */
2839 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2840 8da9e5f4 2019-01-12 stsp if (err)
2841 c4cdcb68 2019-04-03 stsp goto done;
2842 c4cdcb68 2019-04-03 stsp
2843 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2844 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2845 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2846 f2ea84fa 2019-07-27 stsp
2847 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2848 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2849 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2850 f2ea84fa 2019-07-27 stsp if (err)
2851 f2ea84fa 2019-07-27 stsp break;
2852 f2ea84fa 2019-07-27 stsp
2853 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2854 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2855 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2856 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2857 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2858 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2859 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2860 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2861 f2ea84fa 2019-07-27 stsp if (err)
2862 f2ea84fa 2019-07-27 stsp break;
2863 f2ea84fa 2019-07-27 stsp
2864 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2865 711d9cd9 2019-07-12 stsp }
2866 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2867 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2868 af12c6b9 2019-06-04 stsp err = sync_err;
2869 9d31a1d8 2018-03-11 stsp done:
2870 9c6338c4 2019-06-02 stsp free(fileindex_path);
2871 edfa7d7f 2018-09-11 stsp if (tree)
2872 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2873 9d31a1d8 2018-03-11 stsp if (commit)
2874 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2875 5ade8233 2019-07-12 stsp if (fileindex)
2876 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2877 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2878 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2879 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2880 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2881 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2882 f2ea84fa 2019-07-27 stsp free(tpd);
2883 f2ea84fa 2019-07-27 stsp }
2884 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2885 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2886 9d31a1d8 2018-03-11 stsp err = unlockerr;
2887 f8d1f275 2019-02-04 stsp return err;
2888 f8d1f275 2019-02-04 stsp }
2889 f8d1f275 2019-02-04 stsp
2890 9a298e5c 2023-03-10 stsp static const struct got_error *
2891 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2892 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2893 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2894 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2895 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2896 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2897 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2898 9a298e5c 2023-03-10 stsp {
2899 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2900 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2901 9a298e5c 2023-03-10 stsp
2902 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2903 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2904 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2905 9a298e5c 2023-03-10 stsp restoring_missing_file,
2906 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2907 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2908 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2909 9a298e5c 2023-03-10 stsp } else {
2910 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2911 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2912 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2913 9a298e5c 2023-03-10 stsp path_is_unversioned, repo, progress_cb, progress_arg);
2914 9a298e5c 2023-03-10 stsp }
2915 9a298e5c 2023-03-10 stsp if (err)
2916 9a298e5c 2023-03-10 stsp return err;
2917 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2918 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2919 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2920 9a298e5c 2023-03-10 stsp if (err)
2921 9a298e5c 2023-03-10 stsp return err;
2922 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2923 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2924 9a298e5c 2023-03-10 stsp if (err) {
2925 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2926 9a298e5c 2023-03-10 stsp return err;
2927 9a298e5c 2023-03-10 stsp }
2928 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2929 9a298e5c 2023-03-10 stsp if (err) {
2930 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2931 9a298e5c 2023-03-10 stsp return err;
2932 9a298e5c 2023-03-10 stsp }
2933 9a298e5c 2023-03-10 stsp } else {
2934 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2935 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2936 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2937 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2938 9a298e5c 2023-03-10 stsp if (err)
2939 9a298e5c 2023-03-10 stsp return err;
2940 9a298e5c 2023-03-10 stsp }
2941 9a298e5c 2023-03-10 stsp
2942 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2943 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2944 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2945 9a298e5c 2023-03-10 stsp }
2946 9a298e5c 2023-03-10 stsp
2947 9a298e5c 2023-03-10 stsp return NULL;
2948 9a298e5c 2023-03-10 stsp }
2949 9a298e5c 2023-03-10 stsp
2950 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2951 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2952 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2953 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2954 234035bc 2019-06-01 stsp void *progress_arg;
2955 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2956 234035bc 2019-06-01 stsp void *cancel_arg;
2957 f69721c3 2019-10-21 stsp const char *label_orig;
2958 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2959 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2960 234035bc 2019-06-01 stsp };
2961 234035bc 2019-06-01 stsp
2962 234035bc 2019-06-01 stsp static const struct got_error *
2963 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2964 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2965 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2966 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2967 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2968 234035bc 2019-06-01 stsp {
2969 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2970 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2971 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2972 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2973 234035bc 2019-06-01 stsp struct stat sb;
2974 234035bc 2019-06-01 stsp unsigned char status;
2975 234035bc 2019-06-01 stsp int local_changes_subsumed;
2976 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2977 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2978 234035bc 2019-06-01 stsp
2979 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2980 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2981 d6c87207 2019-08-02 stsp strlen(path2));
2982 1ee397ad 2019-07-12 stsp if (ie == NULL)
2983 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2984 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2985 234035bc 2019-06-01 stsp
2986 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2987 234035bc 2019-06-01 stsp path2) == -1)
2988 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2989 234035bc 2019-06-01 stsp
2990 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2991 7f91a133 2019-12-13 stsp repo);
2992 234035bc 2019-06-01 stsp if (err)
2993 234035bc 2019-06-01 stsp goto done;
2994 234035bc 2019-06-01 stsp
2995 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2996 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2997 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2998 234035bc 2019-06-01 stsp goto done;
2999 234035bc 2019-06-01 stsp }
3000 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3001 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3002 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3003 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3004 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3005 234035bc 2019-06-01 stsp goto done;
3006 234035bc 2019-06-01 stsp }
3007 234035bc 2019-06-01 stsp
3008 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3009 36bf999c 2020-07-23 stsp char *link_target2;
3010 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3011 36bf999c 2020-07-23 stsp if (err)
3012 36bf999c 2020-07-23 stsp goto done;
3013 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3014 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3015 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3016 36bf999c 2020-07-23 stsp free(link_target2);
3017 af57b12a 2020-07-23 stsp } else {
3018 1af628f4 2021-06-03 stsp int fd;
3019 1af628f4 2021-06-03 stsp
3020 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3021 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3022 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3023 1af628f4 2021-06-03 stsp goto done;
3024 1af628f4 2021-06-03 stsp }
3025 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3026 1af628f4 2021-06-03 stsp f_orig, blob1);
3027 1af628f4 2021-06-03 stsp if (err)
3028 1af628f4 2021-06-03 stsp goto done;
3029 1af628f4 2021-06-03 stsp
3030 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3031 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3032 1af628f4 2021-06-03 stsp goto done;
3033 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3034 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3035 1af628f4 2021-06-03 stsp if (err)
3036 1af628f4 2021-06-03 stsp goto done;
3037 1af628f4 2021-06-03 stsp
3038 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3039 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3040 1af628f4 2021-06-03 stsp if (fd == -1) {
3041 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3042 1af628f4 2021-06-03 stsp ondisk_path);
3043 1af628f4 2021-06-03 stsp goto done;
3044 1af628f4 2021-06-03 stsp }
3045 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3046 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3047 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3048 1af628f4 2021-06-03 stsp ondisk_path);
3049 1af628f4 2021-06-03 stsp close(fd);
3050 1af628f4 2021-06-03 stsp goto done;
3051 1af628f4 2021-06-03 stsp }
3052 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3053 1af628f4 2021-06-03 stsp if (err)
3054 1af628f4 2021-06-03 stsp goto done;
3055 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3056 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3058 1af628f4 2021-06-03 stsp goto done;
3059 1af628f4 2021-06-03 stsp }
3060 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3061 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3062 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3063 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3064 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3065 af57b12a 2020-07-23 stsp }
3066 234035bc 2019-06-01 stsp } else if (blob1) {
3067 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3068 d6c87207 2019-08-02 stsp strlen(path1));
3069 1ee397ad 2019-07-12 stsp if (ie == NULL)
3070 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3071 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3072 2b92fad7 2019-06-02 stsp
3073 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3074 2b92fad7 2019-06-02 stsp path1) == -1)
3075 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3076 2b92fad7 2019-06-02 stsp
3077 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3078 7f91a133 2019-12-13 stsp repo);
3079 2b92fad7 2019-06-02 stsp if (err)
3080 2b92fad7 2019-06-02 stsp goto done;
3081 2b92fad7 2019-06-02 stsp
3082 2b92fad7 2019-06-02 stsp switch (status) {
3083 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3084 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3085 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3086 1ee397ad 2019-07-12 stsp if (err)
3087 1ee397ad 2019-07-12 stsp goto done;
3088 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3089 2b92fad7 2019-06-02 stsp if (err)
3090 2b92fad7 2019-06-02 stsp goto done;
3091 2b92fad7 2019-06-02 stsp if (ie)
3092 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3093 2b92fad7 2019-06-02 stsp break;
3094 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3095 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3096 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3097 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3098 1ee397ad 2019-07-12 stsp if (err)
3099 1ee397ad 2019-07-12 stsp goto done;
3100 2b92fad7 2019-06-02 stsp if (ie)
3101 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3102 2b92fad7 2019-06-02 stsp break;
3103 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3104 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3105 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3106 72840534 2022-01-19 stsp off_t blob1_size;
3107 0a22ca1a 2020-09-23 stsp /*
3108 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3109 0a22ca1a 2020-09-23 stsp * exists in the repository.
3110 0a22ca1a 2020-09-23 stsp */
3111 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3112 72840534 2022-01-19 stsp &blob1_size, path1);
3113 0a22ca1a 2020-09-23 stsp if (err)
3114 0a22ca1a 2020-09-23 stsp goto done;
3115 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3116 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3117 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3118 0a22ca1a 2020-09-23 stsp if (err)
3119 0a22ca1a 2020-09-23 stsp goto done;
3120 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3121 0a22ca1a 2020-09-23 stsp path1);
3122 0a22ca1a 2020-09-23 stsp if (err)
3123 0a22ca1a 2020-09-23 stsp goto done;
3124 0a22ca1a 2020-09-23 stsp if (ie)
3125 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3126 0a22ca1a 2020-09-23 stsp ie);
3127 0a22ca1a 2020-09-23 stsp } else {
3128 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3129 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3130 0a22ca1a 2020-09-23 stsp }
3131 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3132 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3133 0a22ca1a 2020-09-23 stsp free(id);
3134 0a22ca1a 2020-09-23 stsp if (err)
3135 0a22ca1a 2020-09-23 stsp goto done;
3136 0a22ca1a 2020-09-23 stsp break;
3137 0a22ca1a 2020-09-23 stsp }
3138 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3139 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3140 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3141 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3142 1ee397ad 2019-07-12 stsp if (err)
3143 1ee397ad 2019-07-12 stsp goto done;
3144 2b92fad7 2019-06-02 stsp break;
3145 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3146 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3147 1ee397ad 2019-07-12 stsp if (err)
3148 1ee397ad 2019-07-12 stsp goto done;
3149 2b92fad7 2019-06-02 stsp break;
3150 2b92fad7 2019-06-02 stsp default:
3151 2b92fad7 2019-06-02 stsp break;
3152 2b92fad7 2019-06-02 stsp }
3153 234035bc 2019-06-01 stsp } else if (blob2) {
3154 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3155 234035bc 2019-06-01 stsp path2) == -1)
3156 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3157 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3158 d6c87207 2019-08-02 stsp strlen(path2));
3159 234035bc 2019-06-01 stsp if (ie) {
3160 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3161 7f91a133 2019-12-13 stsp -1, NULL, repo);
3162 234035bc 2019-06-01 stsp if (err)
3163 234035bc 2019-06-01 stsp goto done;
3164 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3165 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3166 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3167 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3168 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3169 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3170 1ee397ad 2019-07-12 stsp status, path2);
3171 234035bc 2019-06-01 stsp goto done;
3172 234035bc 2019-06-01 stsp }
3173 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3174 36bf999c 2020-07-23 stsp char *link_target2;
3175 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3176 36bf999c 2020-07-23 stsp blob2);
3177 36bf999c 2020-07-23 stsp if (err)
3178 36bf999c 2020-07-23 stsp goto done;
3179 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3180 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3181 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3182 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3183 36bf999c 2020-07-23 stsp free(link_target2);
3184 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3185 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3186 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3187 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3188 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3189 526a746f 2020-07-23 stsp a->progress_arg);
3190 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3191 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3192 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3193 526a746f 2020-07-23 stsp }
3194 dfe9fba0 2020-07-23 stsp if (err)
3195 dfe9fba0 2020-07-23 stsp goto done;
3196 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3197 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3198 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3199 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3200 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3201 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3202 234035bc 2019-06-01 stsp if (err)
3203 234035bc 2019-06-01 stsp goto done;
3204 234035bc 2019-06-01 stsp }
3205 234035bc 2019-06-01 stsp } else {
3206 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3207 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3208 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3209 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3210 234035bc 2019-06-01 stsp if (err)
3211 2b92fad7 2019-06-02 stsp goto done;
3212 234035bc 2019-06-01 stsp }
3213 234035bc 2019-06-01 stsp }
3214 234035bc 2019-06-01 stsp done:
3215 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3216 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3217 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3218 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3219 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3220 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3221 1af628f4 2021-06-03 stsp free(id_str);
3222 54d5be07 2021-06-03 stsp free(label_deriv2);
3223 234035bc 2019-06-01 stsp free(ondisk_path);
3224 234035bc 2019-06-01 stsp return err;
3225 234035bc 2019-06-01 stsp }
3226 234035bc 2019-06-01 stsp
3227 69de9dd4 2021-09-03 stsp static const struct got_error *
3228 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3229 69de9dd4 2021-09-03 stsp {
3230 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3231 69de9dd4 2021-09-03 stsp
3232 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3233 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3234 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3235 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3236 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3237 69de9dd4 2021-09-03 stsp
3238 69de9dd4 2021-09-03 stsp return NULL;
3239 69de9dd4 2021-09-03 stsp }
3240 69de9dd4 2021-09-03 stsp
3241 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3242 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3243 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3244 234035bc 2019-06-01 stsp struct got_repository *repo;
3245 234035bc 2019-06-01 stsp };
3246 234035bc 2019-06-01 stsp
3247 234035bc 2019-06-01 stsp static const struct got_error *
3248 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3249 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3250 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3251 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3252 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3253 234035bc 2019-06-01 stsp {
3254 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3255 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3256 234035bc 2019-06-01 stsp unsigned char status;
3257 234035bc 2019-06-01 stsp struct stat sb;
3258 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3259 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3260 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3261 234035bc 2019-06-01 stsp char *ondisk_path;
3262 234035bc 2019-06-01 stsp
3263 69de9dd4 2021-09-03 stsp if (id == NULL)
3264 69de9dd4 2021-09-03 stsp return NULL;
3265 234035bc 2019-06-01 stsp
3266 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3267 69de9dd4 2021-09-03 stsp if (ie == NULL)
3268 69de9dd4 2021-09-03 stsp return NULL;
3269 69de9dd4 2021-09-03 stsp
3270 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3271 234035bc 2019-06-01 stsp == -1)
3272 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3273 234035bc 2019-06-01 stsp
3274 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3275 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3276 5546d466 2021-09-02 stsp free(ondisk_path);
3277 234035bc 2019-06-01 stsp if (err)
3278 234035bc 2019-06-01 stsp return err;
3279 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3280 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3281 234035bc 2019-06-01 stsp
3282 234035bc 2019-06-01 stsp return NULL;
3283 234035bc 2019-06-01 stsp }
3284 234035bc 2019-06-01 stsp
3285 818c7501 2019-07-11 stsp static const struct got_error *
3286 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3287 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3288 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3289 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3290 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3291 234035bc 2019-06-01 stsp {
3292 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3293 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3294 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3295 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3296 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3297 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3298 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3299 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3300 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3301 234035bc 2019-06-01 stsp
3302 03415a1a 2019-06-02 stsp if (commit_id1) {
3303 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3304 a44927cc 2022-04-07 stsp if (err)
3305 a44927cc 2022-04-07 stsp goto done;
3306 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3307 03415a1a 2019-06-02 stsp worktree->path_prefix);
3308 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3309 03415a1a 2019-06-02 stsp goto done;
3310 69d57f3d 2020-07-31 stsp }
3311 69d57f3d 2020-07-31 stsp if (tree_id1) {
3312 69d57f3d 2020-07-31 stsp char *id_str;
3313 03415a1a 2019-06-02 stsp
3314 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3315 03415a1a 2019-06-02 stsp if (err)
3316 03415a1a 2019-06-02 stsp goto done;
3317 f69721c3 2019-10-21 stsp
3318 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3319 f69721c3 2019-10-21 stsp if (err)
3320 f69721c3 2019-10-21 stsp goto done;
3321 f69721c3 2019-10-21 stsp
3322 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3323 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3324 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3325 f69721c3 2019-10-21 stsp free(id_str);
3326 f69721c3 2019-10-21 stsp goto done;
3327 f69721c3 2019-10-21 stsp }
3328 f69721c3 2019-10-21 stsp free(id_str);
3329 b72706c3 2022-06-01 stsp
3330 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3331 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3332 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3333 b72706c3 2022-06-01 stsp goto done;
3334 b72706c3 2022-06-01 stsp }
3335 03415a1a 2019-06-02 stsp }
3336 234035bc 2019-06-01 stsp
3337 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3338 a44927cc 2022-04-07 stsp if (err)
3339 a44927cc 2022-04-07 stsp goto done;
3340 a44927cc 2022-04-07 stsp
3341 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3342 234035bc 2019-06-01 stsp worktree->path_prefix);
3343 234035bc 2019-06-01 stsp if (err)
3344 234035bc 2019-06-01 stsp goto done;
3345 234035bc 2019-06-01 stsp
3346 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3347 234035bc 2019-06-01 stsp if (err)
3348 234035bc 2019-06-01 stsp goto done;
3349 234035bc 2019-06-01 stsp
3350 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3351 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3352 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3353 f9d37699 2022-06-28 stsp goto done;
3354 f9d37699 2022-06-28 stsp }
3355 f9d37699 2022-06-28 stsp
3356 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3357 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3358 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3359 b72706c3 2022-06-01 stsp goto done;
3360 b72706c3 2022-06-01 stsp }
3361 b72706c3 2022-06-01 stsp
3362 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3363 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3364 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3365 f9d37699 2022-06-28 stsp goto done;
3366 f9d37699 2022-06-28 stsp }
3367 f9d37699 2022-06-28 stsp
3368 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3369 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3370 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3371 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3372 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3373 69de9dd4 2021-09-03 stsp if (err)
3374 69de9dd4 2021-09-03 stsp goto done;
3375 69de9dd4 2021-09-03 stsp
3376 234035bc 2019-06-01 stsp arg.worktree = worktree;
3377 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3378 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3379 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3380 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3381 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3382 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3383 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3384 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3385 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3386 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3387 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3388 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3389 af12c6b9 2019-06-04 stsp err = sync_err;
3390 234035bc 2019-06-01 stsp done:
3391 a44927cc 2022-04-07 stsp if (commit1)
3392 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3393 a44927cc 2022-04-07 stsp if (commit2)
3394 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3395 234035bc 2019-06-01 stsp if (tree1)
3396 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3397 234035bc 2019-06-01 stsp if (tree2)
3398 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3399 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3400 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3401 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3402 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3403 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3404 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3405 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3406 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3407 f69721c3 2019-10-21 stsp free(label_orig);
3408 818c7501 2019-07-11 stsp return err;
3409 818c7501 2019-07-11 stsp }
3410 234035bc 2019-06-01 stsp
3411 818c7501 2019-07-11 stsp const struct got_error *
3412 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3413 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3414 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3415 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3416 818c7501 2019-07-11 stsp {
3417 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3418 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3419 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3420 818c7501 2019-07-11 stsp
3421 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3422 818c7501 2019-07-11 stsp if (err)
3423 818c7501 2019-07-11 stsp return err;
3424 818c7501 2019-07-11 stsp
3425 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3426 818c7501 2019-07-11 stsp if (err)
3427 818c7501 2019-07-11 stsp goto done;
3428 818c7501 2019-07-11 stsp
3429 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3430 69de9dd4 2021-09-03 stsp worktree);
3431 818c7501 2019-07-11 stsp if (err)
3432 818c7501 2019-07-11 stsp goto done;
3433 818c7501 2019-07-11 stsp
3434 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3435 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3436 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3437 818c7501 2019-07-11 stsp done:
3438 818c7501 2019-07-11 stsp if (fileindex)
3439 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3440 818c7501 2019-07-11 stsp free(fileindex_path);
3441 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3442 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3443 234035bc 2019-06-01 stsp err = unlockerr;
3444 234035bc 2019-06-01 stsp return err;
3445 234035bc 2019-06-01 stsp }
3446 234035bc 2019-06-01 stsp
3447 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3448 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3449 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3450 927df6b7 2019-02-10 stsp const char *status_path;
3451 927df6b7 2019-02-10 stsp size_t status_path_len;
3452 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3453 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3454 f8d1f275 2019-02-04 stsp void *status_arg;
3455 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3456 f8d1f275 2019-02-04 stsp void *cancel_arg;
3457 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3458 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3459 f2a9dc41 2019-12-13 tracey int report_unchanged;
3460 3143d852 2020-06-25 stsp int no_ignores;
3461 f8d1f275 2019-02-04 stsp };
3462 88d0e355 2019-08-03 stsp
3463 f8d1f275 2019-02-04 stsp static const struct got_error *
3464 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3465 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3466 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3467 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3468 927df6b7 2019-02-10 stsp {
3469 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3470 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3471 2c4740ad 2023-02-12 mark unsigned char staged_status;
3472 927df6b7 2019-02-10 stsp struct stat sb;
3473 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3474 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3475 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3476 927df6b7 2019-02-10 stsp
3477 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3478 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3479 98eaaa12 2019-08-03 stsp if (err)
3480 98eaaa12 2019-08-03 stsp return err;
3481 98eaaa12 2019-08-03 stsp
3482 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3483 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3484 98eaaa12 2019-08-03 stsp return NULL;
3485 98eaaa12 2019-08-03 stsp
3486 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3487 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3488 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3489 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3490 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3491 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3492 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3493 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3494 98eaaa12 2019-08-03 stsp }
3495 98eaaa12 2019-08-03 stsp
3496 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3497 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3498 927df6b7 2019-02-10 stsp }
3499 927df6b7 2019-02-10 stsp
3500 927df6b7 2019-02-10 stsp static const struct got_error *
3501 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3502 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3503 f8d1f275 2019-02-04 stsp {
3504 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3505 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3506 f8d1f275 2019-02-04 stsp char *abspath;
3507 f8d1f275 2019-02-04 stsp
3508 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3510 0584f854 2019-04-06 stsp
3511 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3512 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3513 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3514 927df6b7 2019-02-10 stsp return NULL;
3515 927df6b7 2019-02-10 stsp
3516 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3517 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3518 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3519 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3520 f8d1f275 2019-02-04 stsp } else {
3521 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3522 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3523 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3524 f8d1f275 2019-02-04 stsp }
3525 f8d1f275 2019-02-04 stsp
3526 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3527 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3528 f8d1f275 2019-02-04 stsp free(abspath);
3529 9d31a1d8 2018-03-11 stsp return err;
3530 9d31a1d8 2018-03-11 stsp }
3531 f8d1f275 2019-02-04 stsp
3532 f8d1f275 2019-02-04 stsp static const struct got_error *
3533 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3534 f8d1f275 2019-02-04 stsp {
3535 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3536 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3537 2ec1f75b 2019-03-26 stsp unsigned char status;
3538 927df6b7 2019-02-10 stsp
3539 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3540 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3541 0584f854 2019-04-06 stsp
3542 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3543 927df6b7 2019-02-10 stsp return NULL;
3544 927df6b7 2019-02-10 stsp
3545 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3546 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3547 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3548 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3549 2ec1f75b 2019-03-26 stsp else
3550 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3551 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3552 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3553 6841da00 2019-08-08 stsp }
3554 6841da00 2019-08-08 stsp
3555 336075a4 2022-06-25 op static void
3556 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3557 6841da00 2019-08-08 stsp {
3558 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3559 6841da00 2019-08-08 stsp
3560 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3561 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3562 d8bacb93 2023-01-10 mark
3563 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3564 6841da00 2019-08-08 stsp }
3565 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3566 6841da00 2019-08-08 stsp }
3567 6841da00 2019-08-08 stsp
3568 6841da00 2019-08-08 stsp static const struct got_error *
3569 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3570 6841da00 2019-08-08 stsp {
3571 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3572 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3573 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3574 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3575 6841da00 2019-08-08 stsp size_t linesize = 0;
3576 6841da00 2019-08-08 stsp ssize_t linelen;
3577 6841da00 2019-08-08 stsp
3578 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3579 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3580 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3581 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3582 6841da00 2019-08-08 stsp
3583 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3584 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3585 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3586 bd8de430 2019-10-04 stsp
3587 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3588 bd8de430 2019-10-04 stsp if (line[0] == '#')
3589 bd8de430 2019-10-04 stsp continue;
3590 bd8de430 2019-10-04 stsp
3591 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3592 bd8de430 2019-10-04 stsp if (line[0] == '!')
3593 bd8de430 2019-10-04 stsp continue;
3594 bd8de430 2019-10-04 stsp
3595 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3596 6841da00 2019-08-08 stsp line) == -1) {
3597 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3598 6841da00 2019-08-08 stsp goto done;
3599 6841da00 2019-08-08 stsp }
3600 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3601 6841da00 2019-08-08 stsp if (err)
3602 6841da00 2019-08-08 stsp goto done;
3603 6841da00 2019-08-08 stsp }
3604 6841da00 2019-08-08 stsp if (ferror(f)) {
3605 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3606 6841da00 2019-08-08 stsp goto done;
3607 6841da00 2019-08-08 stsp }
3608 6841da00 2019-08-08 stsp
3609 6841da00 2019-08-08 stsp dirpath = strdup(path);
3610 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3611 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3612 6841da00 2019-08-08 stsp goto done;
3613 6841da00 2019-08-08 stsp }
3614 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3615 6841da00 2019-08-08 stsp done:
3616 6841da00 2019-08-08 stsp free(line);
3617 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3618 6841da00 2019-08-08 stsp free(dirpath);
3619 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3620 6841da00 2019-08-08 stsp }
3621 6841da00 2019-08-08 stsp return err;
3622 249b637c 2023-02-20 stsp }
3623 249b637c 2023-02-20 stsp
3624 249b637c 2023-02-20 stsp static int
3625 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3626 249b637c 2023-02-20 stsp int flags)
3627 249b637c 2023-02-20 stsp {
3628 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3629 249b637c 2023-02-20 stsp
3630 249b637c 2023-02-20 stsp /*
3631 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3632 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3633 249b637c 2023-02-20 stsp */
3634 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3635 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3636 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3637 249b637c 2023-02-20 stsp
3638 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3639 249b637c 2023-02-20 stsp }
3640 249b637c 2023-02-20 stsp
3641 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3642 6841da00 2019-08-08 stsp }
3643 6841da00 2019-08-08 stsp
3644 336075a4 2022-06-25 op static int
3645 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3646 6841da00 2019-08-08 stsp {
3647 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3648 bd8de430 2019-10-04 stsp
3649 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3650 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3651 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3652 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3653 bd8de430 2019-10-04 stsp
3654 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3655 249b637c 2023-02-20 stsp const char *p;
3656 6841da00 2019-08-08 stsp
3657 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3658 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3659 bd8de430 2019-10-04 stsp continue;
3660 bd8de430 2019-10-04 stsp p = path;
3661 bd8de430 2019-10-04 stsp while (*p) {
3662 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3663 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3664 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3665 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3666 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3667 bd8de430 2019-10-04 stsp p++;
3668 bd8de430 2019-10-04 stsp while (*p == '/')
3669 bd8de430 2019-10-04 stsp p++;
3670 bd8de430 2019-10-04 stsp continue;
3671 bd8de430 2019-10-04 stsp }
3672 bd8de430 2019-10-04 stsp return 1;
3673 bd8de430 2019-10-04 stsp }
3674 bd8de430 2019-10-04 stsp }
3675 bd8de430 2019-10-04 stsp }
3676 bd8de430 2019-10-04 stsp
3677 6841da00 2019-08-08 stsp /*
3678 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3679 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3680 6841da00 2019-08-08 stsp * ignores backwards.
3681 6841da00 2019-08-08 stsp */
3682 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3683 6841da00 2019-08-08 stsp while (pe) {
3684 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3685 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3686 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3687 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3688 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3689 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3690 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3691 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3692 249b637c 2023-02-20 stsp path, flags))
3693 6841da00 2019-08-08 stsp continue;
3694 6841da00 2019-08-08 stsp return 1;
3695 6841da00 2019-08-08 stsp }
3696 6841da00 2019-08-08 stsp }
3697 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3698 6841da00 2019-08-08 stsp }
3699 6841da00 2019-08-08 stsp
3700 6841da00 2019-08-08 stsp return 0;
3701 6841da00 2019-08-08 stsp }
3702 6841da00 2019-08-08 stsp
3703 6841da00 2019-08-08 stsp static const struct got_error *
3704 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3705 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3706 6841da00 2019-08-08 stsp {
3707 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3708 6841da00 2019-08-08 stsp char *ignorespath;
3709 886cec17 2019-12-15 stsp int fd = -1;
3710 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3711 6841da00 2019-08-08 stsp
3712 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3713 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3714 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3715 6841da00 2019-08-08 stsp
3716 886cec17 2019-12-15 stsp if (dirfd != -1) {
3717 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3718 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3719 886cec17 2019-12-15 stsp if (fd == -1) {
3720 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3721 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3722 886cec17 2019-12-15 stsp ignorespath);
3723 886cec17 2019-12-15 stsp } else {
3724 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3725 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3726 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3727 886cec17 2019-12-15 stsp ignorespath);
3728 886cec17 2019-12-15 stsp else {
3729 886cec17 2019-12-15 stsp fd = -1;
3730 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3731 886cec17 2019-12-15 stsp }
3732 886cec17 2019-12-15 stsp }
3733 886cec17 2019-12-15 stsp } else {
3734 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3735 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3736 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3737 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3738 886cec17 2019-12-15 stsp ignorespath);
3739 886cec17 2019-12-15 stsp } else
3740 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3741 886cec17 2019-12-15 stsp }
3742 6841da00 2019-08-08 stsp
3743 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3744 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3745 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3746 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3747 6841da00 2019-08-08 stsp free(ignorespath);
3748 6841da00 2019-08-08 stsp return err;
3749 f8d1f275 2019-02-04 stsp }
3750 f8d1f275 2019-02-04 stsp
3751 f8d1f275 2019-02-04 stsp static const struct got_error *
3752 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3753 62da3196 2021-10-01 stsp int dirfd)
3754 f8d1f275 2019-02-04 stsp {
3755 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3756 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3757 f8d1f275 2019-02-04 stsp char *path = NULL;
3758 62da3196 2021-10-01 stsp
3759 62da3196 2021-10-01 stsp if (ignore != NULL)
3760 62da3196 2021-10-01 stsp *ignore = 0;
3761 f8d1f275 2019-02-04 stsp
3762 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3763 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3764 0584f854 2019-04-06 stsp
3765 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3766 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3767 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3768 f8d1f275 2019-02-04 stsp } else {
3769 f8d1f275 2019-02-04 stsp path = de->d_name;
3770 f8d1f275 2019-02-04 stsp }
3771 f8d1f275 2019-02-04 stsp
3772 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3773 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3774 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3775 62da3196 2021-10-01 stsp *ignore = 1;
3776 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3777 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3778 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3779 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3780 f8d1f275 2019-02-04 stsp if (parent_path[0])
3781 f8d1f275 2019-02-04 stsp free(path);
3782 b72f483a 2019-02-05 stsp return err;
3783 f8d1f275 2019-02-04 stsp }
3784 f8d1f275 2019-02-04 stsp
3785 347d1d3e 2019-07-12 stsp static const struct got_error *
3786 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3787 3143d852 2020-06-25 stsp {
3788 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3789 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3790 3143d852 2020-06-25 stsp
3791 3143d852 2020-06-25 stsp if (a->no_ignores)
3792 3143d852 2020-06-25 stsp return NULL;
3793 3143d852 2020-06-25 stsp
3794 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3795 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3796 3143d852 2020-06-25 stsp if (err)
3797 3143d852 2020-06-25 stsp return err;
3798 3143d852 2020-06-25 stsp
3799 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3800 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3801 3143d852 2020-06-25 stsp
3802 3143d852 2020-06-25 stsp return err;
3803 3143d852 2020-06-25 stsp }
3804 3143d852 2020-06-25 stsp
3805 3143d852 2020-06-25 stsp static const struct got_error *
3806 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3807 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3808 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3809 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3810 abb4604f 2019-07-27 stsp {
3811 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3812 abb4604f 2019-07-27 stsp struct stat sb;
3813 ff56836b 2021-07-08 stsp
3814 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3815 abb4604f 2019-07-27 stsp if (ie)
3816 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3817 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3818 abb4604f 2019-07-27 stsp
3819 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3820 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3821 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3822 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3823 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3824 abb4604f 2019-07-27 stsp }
3825 abb4604f 2019-07-27 stsp
3826 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3827 916237f3 2022-02-11 stsp return NULL;
3828 916237f3 2022-02-11 stsp
3829 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3830 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3831 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3832 abb4604f 2019-07-27 stsp
3833 abb4604f 2019-07-27 stsp return NULL;
3834 3143d852 2020-06-25 stsp }
3835 3143d852 2020-06-25 stsp
3836 3143d852 2020-06-25 stsp static const struct got_error *
3837 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3838 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3839 3143d852 2020-06-25 stsp {
3840 3143d852 2020-06-25 stsp const struct got_error *err;
3841 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3842 3143d852 2020-06-25 stsp
3843 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3844 3143d852 2020-06-25 stsp ".cvsignore");
3845 3143d852 2020-06-25 stsp if (err)
3846 3143d852 2020-06-25 stsp return err;
3847 3143d852 2020-06-25 stsp
3848 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3849 3143d852 2020-06-25 stsp ".gitignore");
3850 3143d852 2020-06-25 stsp if (err)
3851 3143d852 2020-06-25 stsp return err;
3852 3143d852 2020-06-25 stsp
3853 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3854 3143d852 2020-06-25 stsp if (err) {
3855 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3856 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3857 3143d852 2020-06-25 stsp return err;
3858 3143d852 2020-06-25 stsp }
3859 3143d852 2020-06-25 stsp for (;;) {
3860 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3861 3143d852 2020-06-25 stsp ".cvsignore");
3862 3143d852 2020-06-25 stsp if (err)
3863 3143d852 2020-06-25 stsp break;
3864 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3865 3143d852 2020-06-25 stsp ".gitignore");
3866 3143d852 2020-06-25 stsp if (err)
3867 3143d852 2020-06-25 stsp break;
3868 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3869 3143d852 2020-06-25 stsp if (err) {
3870 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3871 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3872 3143d852 2020-06-25 stsp break;
3873 3143d852 2020-06-25 stsp }
3874 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3875 ff56836b 2021-07-08 stsp break;
3876 b737c85a 2020-06-26 stsp free(parent_path);
3877 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3878 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3879 3143d852 2020-06-25 stsp }
3880 3143d852 2020-06-25 stsp
3881 b737c85a 2020-06-26 stsp free(parent_path);
3882 b737c85a 2020-06-26 stsp free(next_parent_path);
3883 3143d852 2020-06-25 stsp return err;
3884 abb4604f 2019-07-27 stsp }
3885 31cf15ec 2023-04-12 stsp
3886 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
3887 31cf15ec 2023-04-12 stsp const char *parent_path;
3888 31cf15ec 2023-04-12 stsp size_t parent_len;
3889 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
3890 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
3891 31cf15ec 2023-04-12 stsp void *cancel_arg;
3892 31cf15ec 2023-04-12 stsp };
3893 31cf15ec 2023-04-12 stsp
3894 abb4604f 2019-07-27 stsp static const struct got_error *
3895 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
3896 31cf15ec 2023-04-12 stsp {
3897 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
3898 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
3899 31cf15ec 2023-04-12 stsp
3900 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
3901 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
3902 31cf15ec 2023-04-12 stsp if (err)
3903 31cf15ec 2023-04-12 stsp return err;
3904 31cf15ec 2023-04-12 stsp }
3905 31cf15ec 2023-04-12 stsp
3906 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3907 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
3908 31cf15ec 2023-04-12 stsp
3909 31cf15ec 2023-04-12 stsp return err;
3910 31cf15ec 2023-04-12 stsp }
3911 31cf15ec 2023-04-12 stsp
3912 31cf15ec 2023-04-12 stsp static const struct got_error *
3913 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
3914 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3915 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
3916 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
3917 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3918 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3919 31cf15ec 2023-04-12 stsp {
3920 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
3921 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
3922 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
3923 31cf15ec 2023-04-12 stsp
3924 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
3925 31cf15ec 2023-04-12 stsp if (cancel_cb) {
3926 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
3927 31cf15ec 2023-04-12 stsp if (err)
3928 31cf15ec 2023-04-12 stsp break;
3929 31cf15ec 2023-04-12 stsp }
3930 31cf15ec 2023-04-12 stsp
3931 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3932 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
3933 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
3934 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
3935 31cf15ec 2023-04-12 stsp break;
3936 31cf15ec 2023-04-12 stsp }
3937 31cf15ec 2023-04-12 stsp
3938 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
3939 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
3940 31cf15ec 2023-04-12 stsp ignores, no_ignores);
3941 31cf15ec 2023-04-12 stsp if (err)
3942 31cf15ec 2023-04-12 stsp break;
3943 31cf15ec 2023-04-12 stsp
3944 31cf15ec 2023-04-12 stsp free(ondisk_path);
3945 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
3946 31cf15ec 2023-04-12 stsp }
3947 31cf15ec 2023-04-12 stsp
3948 31cf15ec 2023-04-12 stsp free(ondisk_path);
3949 31cf15ec 2023-04-12 stsp return err;
3950 31cf15ec 2023-04-12 stsp }
3951 31cf15ec 2023-04-12 stsp
3952 31cf15ec 2023-04-12 stsp static const struct got_error *
3953 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3954 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3955 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3956 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3957 f2a9dc41 2019-12-13 tracey int report_unchanged)
3958 f8d1f275 2019-02-04 stsp {
3959 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3960 6fc93f37 2019-12-13 stsp int fd = -1;
3961 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3962 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3963 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3964 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
3965 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3966 3143d852 2020-06-25 stsp
3967 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3968 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
3969 f8d1f275 2019-02-04 stsp
3970 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3971 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3972 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3973 8dc303cc 2019-07-27 stsp
3974 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 a84c0d30 2022-03-12 stsp if (ie) {
3976 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3977 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3978 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3979 a84c0d30 2022-03-12 stsp goto done;
3980 31cf15ec 2023-04-12 stsp } else {
3981 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
3982 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
3983 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
3984 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
3985 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
3986 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
3987 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
3988 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
3989 31cf15ec 2023-04-12 stsp if (err)
3990 31cf15ec 2023-04-12 stsp goto done;
3991 a84c0d30 2022-03-12 stsp }
3992 a84c0d30 2022-03-12 stsp
3993 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3994 6fc93f37 2019-12-13 stsp if (fd == -1) {
3995 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3996 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3997 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3998 ff56836b 2021-07-08 stsp else {
3999 ff56836b 2021-07-08 stsp if (!no_ignores) {
4000 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4001 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4002 ff56836b 2021-07-08 stsp if (err)
4003 ff56836b 2021-07-08 stsp goto done;
4004 ff56836b 2021-07-08 stsp }
4005 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4006 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4007 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4008 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4009 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4010 31cf15ec 2023-04-12 stsp if (err)
4011 31cf15ec 2023-04-12 stsp goto done;
4012 31cf15ec 2023-04-12 stsp } else {
4013 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4014 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4015 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4016 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4017 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4018 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4019 46108e23 2023-04-12 stsp if (err)
4020 46108e23 2023-04-12 stsp goto done;
4021 31cf15ec 2023-04-12 stsp }
4022 ff56836b 2021-07-08 stsp }
4023 abb4604f 2019-07-27 stsp } else {
4024 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4025 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4026 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4027 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4028 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4029 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4030 abb4604f 2019-07-27 stsp arg.status_path = path;
4031 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4032 abb4604f 2019-07-27 stsp arg.repo = repo;
4033 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4034 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4035 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4036 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4037 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4038 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4039 022fae89 2019-12-06 tracey if (!no_ignores) {
4040 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4041 3143d852 2020-06-25 stsp worktree->root_path, path);
4042 3143d852 2020-06-25 stsp if (err)
4043 3143d852 2020-06-25 stsp goto done;
4044 022fae89 2019-12-06 tracey }
4045 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4046 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4047 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4048 927df6b7 2019-02-10 stsp }
4049 3143d852 2020-06-25 stsp done:
4050 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4051 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4052 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4053 927df6b7 2019-02-10 stsp free(ondisk_path);
4054 347d1d3e 2019-07-12 stsp return err;
4055 347d1d3e 2019-07-12 stsp }
4056 347d1d3e 2019-07-12 stsp
4057 347d1d3e 2019-07-12 stsp const struct got_error *
4058 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4059 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4060 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4061 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4062 347d1d3e 2019-07-12 stsp {
4063 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4064 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4065 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4066 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4067 347d1d3e 2019-07-12 stsp
4068 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4069 347d1d3e 2019-07-12 stsp if (err)
4070 347d1d3e 2019-07-12 stsp return err;
4071 347d1d3e 2019-07-12 stsp
4072 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4073 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4074 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4075 f6343036 2021-06-22 stsp no_ignores, 0);
4076 72ea6654 2019-07-27 stsp if (err)
4077 72ea6654 2019-07-27 stsp break;
4078 72ea6654 2019-07-27 stsp }
4079 f8d1f275 2019-02-04 stsp free(fileindex_path);
4080 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4081 f8d1f275 2019-02-04 stsp return err;
4082 f8d1f275 2019-02-04 stsp }
4083 6c7ab921 2019-03-18 stsp
4084 6c7ab921 2019-03-18 stsp const struct got_error *
4085 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4086 6c7ab921 2019-03-18 stsp const char *arg)
4087 6c7ab921 2019-03-18 stsp {
4088 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4089 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4090 6c7ab921 2019-03-18 stsp size_t len;
4091 00bb5ea0 2020-07-23 stsp struct stat sb;
4092 01740607 2020-11-04 stsp char *abspath = NULL;
4093 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4094 6c7ab921 2019-03-18 stsp
4095 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4096 6c7ab921 2019-03-18 stsp
4097 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4098 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4099 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4100 00bb5ea0 2020-07-23 stsp
4101 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4102 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4103 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4104 00bb5ea0 2020-07-23 stsp goto done;
4105 00bb5ea0 2020-07-23 stsp }
4106 727173c3 2020-11-06 stsp sb.st_mode = 0;
4107 00bb5ea0 2020-07-23 stsp }
4108 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4109 00bb5ea0 2020-07-23 stsp /*
4110 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4111 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4112 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4113 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4114 00bb5ea0 2020-07-23 stsp */
4115 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4116 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4117 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4118 00bb5ea0 2020-07-23 stsp goto done;
4119 00bb5ea0 2020-07-23 stsp }
4120 00bb5ea0 2020-07-23 stsp
4121 00bb5ea0 2020-07-23 stsp }
4122 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4123 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4124 00bb5ea0 2020-07-23 stsp if (err)
4125 d0710d08 2019-07-22 stsp goto done;
4126 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4127 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4128 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4129 00bb5ea0 2020-07-23 stsp goto done;
4130 00bb5ea0 2020-07-23 stsp }
4131 00bb5ea0 2020-07-23 stsp } else {
4132 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4133 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4134 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4135 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4136 00bb5ea0 2020-07-23 stsp goto done;
4137 00bb5ea0 2020-07-23 stsp }
4138 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4139 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4140 01740607 2020-11-04 stsp goto done;
4141 01740607 2020-11-04 stsp }
4142 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4143 01740607 2020-11-04 stsp sizeof(canonpath));
4144 01740607 2020-11-04 stsp if (err)
4145 00bb5ea0 2020-07-23 stsp goto done;
4146 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4147 01740607 2020-11-04 stsp if (resolved == NULL) {
4148 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4149 01740607 2020-11-04 stsp goto done;
4150 00bb5ea0 2020-07-23 stsp }
4151 d0710d08 2019-07-22 stsp }
4152 d0710d08 2019-07-22 stsp }
4153 6c7ab921 2019-03-18 stsp
4154 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4155 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4156 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4157 6c7ab921 2019-03-18 stsp goto done;
4158 6c7ab921 2019-03-18 stsp }
4159 6c7ab921 2019-03-18 stsp
4160 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4161 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4162 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4163 f6d88e1a 2019-05-29 stsp if (err)
4164 f6d88e1a 2019-05-29 stsp goto done;
4165 f6d88e1a 2019-05-29 stsp } else {
4166 f6d88e1a 2019-05-29 stsp path = strdup("");
4167 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4168 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4169 f6d88e1a 2019-05-29 stsp goto done;
4170 f6d88e1a 2019-05-29 stsp }
4171 6c7ab921 2019-03-18 stsp }
4172 6c7ab921 2019-03-18 stsp
4173 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4174 6c7ab921 2019-03-18 stsp len = strlen(path);
4175 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4176 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4177 6c7ab921 2019-03-18 stsp len--;
4178 6c7ab921 2019-03-18 stsp }
4179 6c7ab921 2019-03-18 stsp done:
4180 01740607 2020-11-04 stsp free(abspath);
4181 6c7ab921 2019-03-18 stsp free(resolved);
4182 d0710d08 2019-07-22 stsp free(cwd);
4183 6c7ab921 2019-03-18 stsp if (err == NULL)
4184 6c7ab921 2019-03-18 stsp *wt_path = path;
4185 6c7ab921 2019-03-18 stsp else
4186 6c7ab921 2019-03-18 stsp free(path);
4187 d00136be 2019-03-26 stsp return err;
4188 d00136be 2019-03-26 stsp }
4189 d00136be 2019-03-26 stsp
4190 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4191 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4192 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4193 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4194 4e68cba3 2019-11-23 stsp void *progress_arg;
4195 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4196 4e68cba3 2019-11-23 stsp };
4197 4e68cba3 2019-11-23 stsp
4198 b88936d3 2023-06-21 stsp static int
4199 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4200 b88936d3 2023-06-21 stsp {
4201 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4202 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4203 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4204 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4205 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4206 b88936d3 2023-06-21 stsp }
4207 b88936d3 2023-06-21 stsp
4208 4e68cba3 2019-11-23 stsp static const struct got_error *
4209 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4210 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4211 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4212 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4213 07f5b47a 2019-06-02 stsp {
4214 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4215 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4216 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4217 6d022e97 2019-08-04 stsp struct stat sb;
4218 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4219 07f5b47a 2019-06-02 stsp
4220 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4221 dbb83fbd 2019-12-12 stsp relpath) == -1)
4222 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4223 dbb83fbd 2019-12-12 stsp
4224 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4225 6d022e97 2019-08-04 stsp if (ie) {
4226 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4227 12463d8b 2019-12-13 stsp de_name, a->repo);
4228 6d022e97 2019-08-04 stsp if (err)
4229 dbb83fbd 2019-12-12 stsp goto done;
4230 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4231 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4232 b88936d3 2023-06-21 stsp add_noop_status(status))
4233 dbb83fbd 2019-12-12 stsp goto done;
4234 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4235 dbb83fbd 2019-12-12 stsp if (err)
4236 dbb83fbd 2019-12-12 stsp goto done;
4237 6d022e97 2019-08-04 stsp }
4238 07f5b47a 2019-06-02 stsp
4239 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4240 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4241 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4242 9b4603c0 2022-01-31 stsp else
4243 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4244 dbb83fbd 2019-12-12 stsp goto done;
4245 4e68cba3 2019-11-23 stsp }
4246 07f5b47a 2019-06-02 stsp
4247 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4248 4e68cba3 2019-11-23 stsp if (err)
4249 4e68cba3 2019-11-23 stsp goto done;
4250 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4251 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4252 3969253a 2020-03-07 stsp if (err) {
4253 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4254 3969253a 2020-03-07 stsp goto done;
4255 3969253a 2020-03-07 stsp }
4256 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4257 07f5b47a 2019-06-02 stsp if (err) {
4258 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4259 4e68cba3 2019-11-23 stsp goto done;
4260 07f5b47a 2019-06-02 stsp }
4261 4e68cba3 2019-11-23 stsp done:
4262 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4263 dbb83fbd 2019-12-12 stsp if (err)
4264 dbb83fbd 2019-12-12 stsp return err;
4265 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4266 dbb83fbd 2019-12-12 stsp return NULL;
4267 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4268 07f5b47a 2019-06-02 stsp }
4269 07f5b47a 2019-06-02 stsp
4270 d00136be 2019-03-26 stsp const struct got_error *
4271 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4272 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4273 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4274 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4275 d00136be 2019-03-26 stsp {
4276 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4277 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4278 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4279 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4280 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4281 d00136be 2019-03-26 stsp
4282 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4283 d00136be 2019-03-26 stsp if (err)
4284 d00136be 2019-03-26 stsp return err;
4285 d00136be 2019-03-26 stsp
4286 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4287 d00136be 2019-03-26 stsp if (err)
4288 d00136be 2019-03-26 stsp goto done;
4289 d00136be 2019-03-26 stsp
4290 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4291 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4292 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4293 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4294 4e68cba3 2019-11-23 stsp saa.repo = repo;
4295 4e68cba3 2019-11-23 stsp
4296 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4297 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4298 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4299 1dd54920 2019-05-11 stsp if (err)
4300 af12c6b9 2019-06-04 stsp break;
4301 1dd54920 2019-05-11 stsp }
4302 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4303 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4304 af12c6b9 2019-06-04 stsp err = sync_err;
4305 d00136be 2019-03-26 stsp done:
4306 fb399478 2019-07-12 stsp free(fileindex_path);
4307 d00136be 2019-03-26 stsp if (fileindex)
4308 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4309 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4310 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4311 d00136be 2019-03-26 stsp err = unlockerr;
4312 6c7ab921 2019-03-18 stsp return err;
4313 6c7ab921 2019-03-18 stsp }
4314 17ed4618 2019-06-02 stsp
4315 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4316 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4317 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4318 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4319 f2a9dc41 2019-12-13 tracey void *progress_arg;
4320 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4321 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4322 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4323 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4324 7f4e5320 2023-05-29 stsp const char *status_path;
4325 7f4e5320 2023-05-29 stsp size_t status_path_len;
4326 766841c2 2020-08-13 stsp const char *status_codes;
4327 f2a9dc41 2019-12-13 tracey };
4328 f2a9dc41 2019-12-13 tracey
4329 f2a9dc41 2019-12-13 tracey static const struct got_error *
4330 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4331 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4332 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4333 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4334 17ed4618 2019-06-02 stsp {
4335 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4336 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4337 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4338 17ed4618 2019-06-02 stsp struct stat sb;
4339 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4340 4e12cd97 2022-01-25 stsp
4341 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4342 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4343 4e12cd97 2022-01-25 stsp return NULL;
4344 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4345 4e12cd97 2022-01-25 stsp }
4346 17ed4618 2019-06-02 stsp
4347 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4348 17ed4618 2019-06-02 stsp if (ie == NULL)
4349 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4350 2ec1f75b 2019-03-26 stsp
4351 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4352 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4353 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4354 9acbc4fa 2019-08-03 stsp return NULL;
4355 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4356 9acbc4fa 2019-08-03 stsp }
4357 9acbc4fa 2019-08-03 stsp
4358 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4359 f2a9dc41 2019-12-13 tracey relpath) == -1)
4360 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4361 f2a9dc41 2019-12-13 tracey
4362 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4363 12463d8b 2019-12-13 stsp a->repo);
4364 17ed4618 2019-06-02 stsp if (err)
4365 f2a9dc41 2019-12-13 tracey goto done;
4366 17ed4618 2019-06-02 stsp
4367 766841c2 2020-08-13 stsp if (a->status_codes) {
4368 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4369 766841c2 2020-08-13 stsp int i;
4370 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4371 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4372 766841c2 2020-08-13 stsp break;
4373 766841c2 2020-08-13 stsp }
4374 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4375 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4376 766841c2 2020-08-13 stsp free(ondisk_path);
4377 766841c2 2020-08-13 stsp return NULL;
4378 766841c2 2020-08-13 stsp }
4379 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4380 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4381 766841c2 2020-08-13 stsp static char msg[64];
4382 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4383 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4384 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4385 766841c2 2020-08-13 stsp goto done;
4386 766841c2 2020-08-13 stsp }
4387 766841c2 2020-08-13 stsp }
4388 766841c2 2020-08-13 stsp
4389 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4390 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4391 f2a9dc41 2019-12-13 tracey goto done;
4392 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4393 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4394 f2a9dc41 2019-12-13 tracey goto done;
4395 f2a9dc41 2019-12-13 tracey }
4396 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4397 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4398 4e12cd97 2022-01-25 stsp goto done;
4399 4e12cd97 2022-01-25 stsp }
4400 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4401 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4402 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4403 f2a9dc41 2019-12-13 tracey goto done;
4404 f2a9dc41 2019-12-13 tracey }
4405 17ed4618 2019-06-02 stsp }
4406 17ed4618 2019-06-02 stsp
4407 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4408 20a2ad1c 2020-10-20 stsp size_t root_len;
4409 20a2ad1c 2020-10-20 stsp
4410 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4411 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4412 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4413 12463d8b 2019-12-13 stsp ondisk_path);
4414 12463d8b 2019-12-13 stsp goto done;
4415 12463d8b 2019-12-13 stsp }
4416 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4417 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4418 12463d8b 2019-12-13 stsp goto done;
4419 15341bfd 2020-03-05 tracey }
4420 15341bfd 2020-03-05 tracey
4421 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4422 20a2ad1c 2020-10-20 stsp do {
4423 20a2ad1c 2020-10-20 stsp char *parent;
4424 7f4e5320 2023-05-29 stsp
4425 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4426 20a2ad1c 2020-10-20 stsp if (err)
4427 2513f20a 2020-10-20 stsp goto done;
4428 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4429 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4430 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4431 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4432 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4433 7f4e5320 2023-05-29 stsp a->status_path_len))
4434 7f4e5320 2023-05-29 stsp break;
4435 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4436 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4437 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4438 20a2ad1c 2020-10-20 stsp ondisk_path);
4439 15341bfd 2020-03-05 tracey break;
4440 15341bfd 2020-03-05 tracey }
4441 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4442 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4443 f2a9dc41 2019-12-13 tracey }
4444 17ed4618 2019-06-02 stsp
4445 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4446 f2a9dc41 2019-12-13 tracey done:
4447 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4448 f2a9dc41 2019-12-13 tracey if (err)
4449 f2a9dc41 2019-12-13 tracey return err;
4450 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4451 f2a9dc41 2019-12-13 tracey return NULL;
4452 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4453 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4454 17ed4618 2019-06-02 stsp }
4455 17ed4618 2019-06-02 stsp
4456 2ec1f75b 2019-03-26 stsp const struct got_error *
4457 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4458 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4459 766841c2 2020-08-13 stsp const char *status_codes,
4460 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4461 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4462 2ec1f75b 2019-03-26 stsp {
4463 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4464 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4465 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4466 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4467 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4468 2ec1f75b 2019-03-26 stsp
4469 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4470 2ec1f75b 2019-03-26 stsp if (err)
4471 2ec1f75b 2019-03-26 stsp return err;
4472 2ec1f75b 2019-03-26 stsp
4473 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4474 2ec1f75b 2019-03-26 stsp if (err)
4475 2ec1f75b 2019-03-26 stsp goto done;
4476 f2a9dc41 2019-12-13 tracey
4477 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4478 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4479 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4480 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4481 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4482 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4483 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4484 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4485 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4486 2ec1f75b 2019-03-26 stsp
4487 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4488 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4489 7f4e5320 2023-05-29 stsp
4490 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4491 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4492 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4493 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4494 7f4e5320 2023-05-29 stsp goto done;
4495 7f4e5320 2023-05-29 stsp }
4496 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4497 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4498 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4499 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4500 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4501 17ed4618 2019-06-02 stsp if (err)
4502 af12c6b9 2019-06-04 stsp break;
4503 2ec1f75b 2019-03-26 stsp }
4504 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4505 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4506 af12c6b9 2019-06-04 stsp err = sync_err;
4507 2ec1f75b 2019-03-26 stsp done:
4508 fb399478 2019-07-12 stsp free(fileindex_path);
4509 2ec1f75b 2019-03-26 stsp if (fileindex)
4510 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4511 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4512 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4513 2ec1f75b 2019-03-26 stsp err = unlockerr;
4514 33aa809d 2019-08-08 stsp return err;
4515 33aa809d 2019-08-08 stsp }
4516 33aa809d 2019-08-08 stsp
4517 33aa809d 2019-08-08 stsp static const struct got_error *
4518 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4519 33aa809d 2019-08-08 stsp {
4520 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4521 33aa809d 2019-08-08 stsp char *line = NULL;
4522 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4523 33aa809d 2019-08-08 stsp ssize_t linelen;
4524 33aa809d 2019-08-08 stsp
4525 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4526 33aa809d 2019-08-08 stsp if (linelen == -1) {
4527 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4528 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4529 33aa809d 2019-08-08 stsp goto done;
4530 33aa809d 2019-08-08 stsp }
4531 33aa809d 2019-08-08 stsp return NULL;
4532 33aa809d 2019-08-08 stsp }
4533 33aa809d 2019-08-08 stsp if (outfile) {
4534 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4535 33aa809d 2019-08-08 stsp if (n != linelen) {
4536 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4537 33aa809d 2019-08-08 stsp goto done;
4538 33aa809d 2019-08-08 stsp }
4539 33aa809d 2019-08-08 stsp }
4540 33aa809d 2019-08-08 stsp if (rejectfile) {
4541 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4542 33aa809d 2019-08-08 stsp if (n != linelen)
4543 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4544 33aa809d 2019-08-08 stsp }
4545 33aa809d 2019-08-08 stsp done:
4546 33aa809d 2019-08-08 stsp free(line);
4547 2ec1f75b 2019-03-26 stsp return err;
4548 2ec1f75b 2019-03-26 stsp }
4549 1f1abb7e 2019-08-08 stsp
4550 33aa809d 2019-08-08 stsp static const struct got_error *
4551 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4552 33aa809d 2019-08-08 stsp {
4553 33aa809d 2019-08-08 stsp char *line = NULL;
4554 33aa809d 2019-08-08 stsp size_t linesize = 0;
4555 33aa809d 2019-08-08 stsp ssize_t linelen;
4556 33aa809d 2019-08-08 stsp
4557 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4558 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4559 500467ff 2019-09-25 hiltjo if (ferror(f))
4560 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4561 500467ff 2019-09-25 hiltjo return NULL;
4562 500467ff 2019-09-25 hiltjo }
4563 33aa809d 2019-08-08 stsp free(line);
4564 33aa809d 2019-08-08 stsp return NULL;
4565 33aa809d 2019-08-08 stsp }
4566 33aa809d 2019-08-08 stsp
4567 33aa809d 2019-08-08 stsp static const struct got_error *
4568 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4569 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4570 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4571 abc59930 2021-09-05 naddy {
4572 33aa809d 2019-08-08 stsp const struct got_error *err;
4573 33aa809d 2019-08-08 stsp
4574 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4575 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4576 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4577 33aa809d 2019-08-08 stsp if (err)
4578 33aa809d 2019-08-08 stsp return err;
4579 33aa809d 2019-08-08 stsp (*line_cur1)++;
4580 33aa809d 2019-08-08 stsp }
4581 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4582 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4583 33aa809d 2019-08-08 stsp if (rejectfile)
4584 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4585 33aa809d 2019-08-08 stsp else
4586 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4587 33aa809d 2019-08-08 stsp if (err)
4588 33aa809d 2019-08-08 stsp return err;
4589 33aa809d 2019-08-08 stsp (*line_cur2)++;
4590 33aa809d 2019-08-08 stsp }
4591 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4592 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4593 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4594 33aa809d 2019-08-08 stsp if (err)
4595 33aa809d 2019-08-08 stsp return err;
4596 33aa809d 2019-08-08 stsp (*line_cur2)++;
4597 33aa809d 2019-08-08 stsp }
4598 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4599 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4600 33aa809d 2019-08-08 stsp if (rejectfile)
4601 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4602 33aa809d 2019-08-08 stsp else
4603 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4604 33aa809d 2019-08-08 stsp if (err)
4605 33aa809d 2019-08-08 stsp return err;
4606 33aa809d 2019-08-08 stsp (*line_cur1)++;
4607 33aa809d 2019-08-08 stsp }
4608 f1e81a05 2019-08-10 stsp
4609 f1e81a05 2019-08-10 stsp return NULL;
4610 f1e81a05 2019-08-10 stsp }
4611 f1e81a05 2019-08-10 stsp
4612 f1e81a05 2019-08-10 stsp static const struct got_error *
4613 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4614 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4615 f1e81a05 2019-08-10 stsp {
4616 f1e81a05 2019-08-10 stsp const struct got_error *err;
4617 f1e81a05 2019-08-10 stsp
4618 f1e81a05 2019-08-10 stsp if (outfile) {
4619 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4620 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4621 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4622 f1e81a05 2019-08-10 stsp if (err)
4623 f1e81a05 2019-08-10 stsp return err;
4624 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4625 f1e81a05 2019-08-10 stsp }
4626 f1e81a05 2019-08-10 stsp }
4627 f1e81a05 2019-08-10 stsp if (rejectfile) {
4628 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4629 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4630 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4631 f1e81a05 2019-08-10 stsp if (err)
4632 f1e81a05 2019-08-10 stsp return err;
4633 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4634 f1e81a05 2019-08-10 stsp }
4635 33aa809d 2019-08-08 stsp }
4636 33aa809d 2019-08-08 stsp
4637 33aa809d 2019-08-08 stsp return NULL;
4638 33aa809d 2019-08-08 stsp }
4639 33aa809d 2019-08-08 stsp
4640 33aa809d 2019-08-08 stsp static const struct got_error *
4641 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4642 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4643 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4644 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4645 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4646 33aa809d 2019-08-08 stsp {
4647 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4648 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4649 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4650 33aa809d 2019-08-08 stsp FILE *hunkfile;
4651 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4652 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4653 fe621944 2020-11-10 stsp int rc;
4654 33aa809d 2019-08-08 stsp
4655 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4656 33aa809d 2019-08-08 stsp
4657 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4658 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4659 fe621944 2020-11-10 stsp start_old = cc.left.start;
4660 fe621944 2020-11-10 stsp end_old = cc.left.end;
4661 fe621944 2020-11-10 stsp start_new = cc.right.start;
4662 fe621944 2020-11-10 stsp end_new = cc.right.end;
4663 33aa809d 2019-08-08 stsp
4664 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4665 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4666 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4667 33aa809d 2019-08-08 stsp
4668 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4669 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4670 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4671 33aa809d 2019-08-08 stsp
4672 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4673 fe621944 2020-11-10 stsp if (diff_state == NULL)
4674 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4675 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4676 fe621944 2020-11-10 stsp
4677 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4678 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4679 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4680 33aa809d 2019-08-08 stsp goto done;
4681 33aa809d 2019-08-08 stsp }
4682 fe621944 2020-11-10 stsp
4683 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4684 fe621944 2020-11-10 stsp diff_result, &cc);
4685 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4686 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4687 33aa809d 2019-08-08 stsp goto done;
4688 33aa809d 2019-08-08 stsp }
4689 fe621944 2020-11-10 stsp
4690 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4691 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4692 33aa809d 2019-08-08 stsp goto done;
4693 33aa809d 2019-08-08 stsp }
4694 33aa809d 2019-08-08 stsp
4695 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4696 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4697 33aa809d 2019-08-08 stsp if (err)
4698 33aa809d 2019-08-08 stsp goto done;
4699 33aa809d 2019-08-08 stsp
4700 33aa809d 2019-08-08 stsp switch (*choice) {
4701 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4702 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4703 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4704 33aa809d 2019-08-08 stsp break;
4705 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4706 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4707 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4708 33aa809d 2019-08-08 stsp break;
4709 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4710 33aa809d 2019-08-08 stsp break;
4711 33aa809d 2019-08-08 stsp default:
4712 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4713 33aa809d 2019-08-08 stsp break;
4714 33aa809d 2019-08-08 stsp }
4715 33aa809d 2019-08-08 stsp done:
4716 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4717 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4718 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4719 33aa809d 2019-08-08 stsp return err;
4720 33aa809d 2019-08-08 stsp }
4721 33aa809d 2019-08-08 stsp
4722 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4723 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4724 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4725 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4726 1f1abb7e 2019-08-08 stsp void *progress_arg;
4727 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4728 33aa809d 2019-08-08 stsp void *patch_arg;
4729 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4730 f259c4c1 2021-09-24 stsp int unlink_added_files;
4731 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4732 1f1abb7e 2019-08-08 stsp };
4733 a129376b 2019-03-28 stsp
4734 e20a8b6f 2019-06-04 stsp static const struct got_error *
4735 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4736 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4737 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4738 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4739 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4740 33aa809d 2019-08-08 stsp {
4741 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4742 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4743 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4744 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4745 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4746 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4747 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4748 fe621944 2020-11-10 stsp struct stat sb2;
4749 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4750 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4751 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4752 33aa809d 2019-08-08 stsp
4753 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4754 33aa809d 2019-08-08 stsp
4755 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4756 33aa809d 2019-08-08 stsp if (err)
4757 33aa809d 2019-08-08 stsp return err;
4758 33aa809d 2019-08-08 stsp
4759 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4760 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4761 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4762 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4763 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4764 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4765 fa3cef63 2020-07-23 stsp goto done;
4766 fa3cef63 2020-07-23 stsp }
4767 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4768 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4769 c0df5966 2021-12-31 stsp if (link_len == -1) {
4770 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4771 c0df5966 2021-12-31 stsp path2);
4772 c0df5966 2021-12-31 stsp }
4773 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4774 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4775 12463d8b 2019-12-13 stsp }
4776 12463d8b 2019-12-13 stsp } else {
4777 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4778 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4779 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4780 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4781 fa3cef63 2020-07-23 stsp goto done;
4782 fa3cef63 2020-07-23 stsp }
4783 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4784 fa3cef63 2020-07-23 stsp sizeof(link_target));
4785 fa3cef63 2020-07-23 stsp if (link_len == -1)
4786 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4787 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4788 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4789 12463d8b 2019-12-13 stsp }
4790 1ebedb77 2019-10-19 stsp }
4791 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4792 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4793 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4794 fa3cef63 2020-07-23 stsp goto done;
4795 fa3cef63 2020-07-23 stsp }
4796 1ebedb77 2019-10-19 stsp
4797 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4798 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4799 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4800 fa3cef63 2020-07-23 stsp goto done;
4801 fa3cef63 2020-07-23 stsp }
4802 fa3cef63 2020-07-23 stsp fd2 = -1;
4803 fa3cef63 2020-07-23 stsp } else {
4804 fa3cef63 2020-07-23 stsp size_t n;
4805 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4806 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4807 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4808 fa3cef63 2020-07-23 stsp goto done;
4809 fa3cef63 2020-07-23 stsp }
4810 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4811 fa3cef63 2020-07-23 stsp if (n != link_len) {
4812 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4813 fa3cef63 2020-07-23 stsp goto done;
4814 fa3cef63 2020-07-23 stsp }
4815 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4816 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4817 fa3cef63 2020-07-23 stsp goto done;
4818 fa3cef63 2020-07-23 stsp }
4819 fa3cef63 2020-07-23 stsp rewind(f2);
4820 33aa809d 2019-08-08 stsp }
4821 33aa809d 2019-08-08 stsp
4822 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4823 eb81bc23 2022-06-28 tracey if (fd == -1) {
4824 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4825 eb81bc23 2022-06-28 tracey goto done;
4826 eb81bc23 2022-06-28 tracey }
4827 eb81bc23 2022-06-28 tracey
4828 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4829 33aa809d 2019-08-08 stsp if (err)
4830 33aa809d 2019-08-08 stsp goto done;
4831 33aa809d 2019-08-08 stsp
4832 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4833 33aa809d 2019-08-08 stsp if (err)
4834 33aa809d 2019-08-08 stsp goto done;
4835 33aa809d 2019-08-08 stsp
4836 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4837 33aa809d 2019-08-08 stsp if (err)
4838 33aa809d 2019-08-08 stsp goto done;
4839 33aa809d 2019-08-08 stsp
4840 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4841 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4842 33aa809d 2019-08-08 stsp if (err)
4843 33aa809d 2019-08-08 stsp goto done;
4844 33aa809d 2019-08-08 stsp
4845 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4846 b90054ed 2022-11-01 stsp "");
4847 33aa809d 2019-08-08 stsp if (err)
4848 33aa809d 2019-08-08 stsp goto done;
4849 33aa809d 2019-08-08 stsp
4850 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4851 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4852 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4853 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4854 fe621944 2020-11-10 stsp
4855 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4856 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4857 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4858 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4859 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4860 fe621944 2020-11-10 stsp nchanges++;
4861 fe621944 2020-11-10 stsp }
4862 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4863 33aa809d 2019-08-08 stsp int choice;
4864 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4865 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4866 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4867 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4868 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4869 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4870 33aa809d 2019-08-08 stsp if (err)
4871 33aa809d 2019-08-08 stsp goto done;
4872 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4873 33aa809d 2019-08-08 stsp have_content = 1;
4874 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4875 33aa809d 2019-08-08 stsp break;
4876 33aa809d 2019-08-08 stsp }
4877 1ebedb77 2019-10-19 stsp if (have_content) {
4878 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4879 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4880 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4881 1ebedb77 2019-10-19 stsp if (err)
4882 1ebedb77 2019-10-19 stsp goto done;
4883 1ebedb77 2019-10-19 stsp
4884 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4885 b2b3fce1 2022-10-29 op mode_t mode;
4886 b2b3fce1 2022-10-29 op
4887 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4888 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4889 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4890 fa3cef63 2020-07-23 stsp goto done;
4891 fa3cef63 2020-07-23 stsp }
4892 1ebedb77 2019-10-19 stsp }
4893 1ebedb77 2019-10-19 stsp }
4894 33aa809d 2019-08-08 stsp done:
4895 33aa809d 2019-08-08 stsp free(id_str);
4896 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4897 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4898 33aa809d 2019-08-08 stsp if (blob)
4899 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4900 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4901 fe621944 2020-11-10 stsp if (err == NULL)
4902 fe621944 2020-11-10 stsp err = free_err;
4903 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4904 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4905 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4906 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4907 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4908 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4909 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4910 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4911 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4912 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4913 33aa809d 2019-08-08 stsp if (err || !have_content) {
4914 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4915 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4916 33aa809d 2019-08-08 stsp free(*path_outfile);
4917 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4918 33aa809d 2019-08-08 stsp }
4919 33aa809d 2019-08-08 stsp free(path1);
4920 33aa809d 2019-08-08 stsp return err;
4921 33aa809d 2019-08-08 stsp }
4922 33aa809d 2019-08-08 stsp
4923 33aa809d 2019-08-08 stsp static const struct got_error *
4924 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4925 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4926 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4927 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4928 a129376b 2019-03-28 stsp {
4929 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4930 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4931 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4932 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4933 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4934 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4935 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4936 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4937 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4938 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4939 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4940 eb81bc23 2022-06-28 tracey int fd = -1;
4941 a129376b 2019-03-28 stsp
4942 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4943 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4944 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4945 d3bcc3d1 2019-08-08 stsp return NULL;
4946 3d69ad8d 2019-08-17 semarie
4947 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4948 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4949 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4950 d3bcc3d1 2019-08-08 stsp
4951 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4952 65084dad 2019-08-08 stsp if (ie == NULL)
4953 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4954 a129376b 2019-03-28 stsp
4955 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4956 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4957 a129376b 2019-03-28 stsp if (err) {
4958 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4959 a129376b 2019-03-28 stsp goto done;
4960 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4961 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4962 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4963 e20a8b6f 2019-06-04 stsp goto done;
4964 e20a8b6f 2019-06-04 stsp }
4965 a129376b 2019-03-28 stsp }
4966 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4967 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4968 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4969 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4970 a129376b 2019-03-28 stsp goto done;
4971 a129376b 2019-03-28 stsp }
4972 a129376b 2019-03-28 stsp } else {
4973 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4974 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4975 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4976 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4977 a129376b 2019-03-28 stsp goto done;
4978 a129376b 2019-03-28 stsp }
4979 a129376b 2019-03-28 stsp } else {
4980 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4981 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4982 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4983 a129376b 2019-03-28 stsp goto done;
4984 a129376b 2019-03-28 stsp }
4985 a129376b 2019-03-28 stsp }
4986 a129376b 2019-03-28 stsp }
4987 a129376b 2019-03-28 stsp
4988 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4989 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4990 a44927cc 2022-04-07 stsp if (err)
4991 a44927cc 2022-04-07 stsp goto done;
4992 a44927cc 2022-04-07 stsp
4993 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4994 a9fa2909 2019-07-27 stsp if (err) {
4995 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4996 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4997 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4998 a9fa2909 2019-07-27 stsp goto done;
4999 a9fa2909 2019-07-27 stsp } else {
5000 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5001 a9fa2909 2019-07-27 stsp if (err)
5002 a9fa2909 2019-07-27 stsp goto done;
5003 a9fa2909 2019-07-27 stsp
5004 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5005 1233e6b6 2020-10-19 stsp if (err)
5006 a9fa2909 2019-07-27 stsp goto done;
5007 a9fa2909 2019-07-27 stsp
5008 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5009 1233e6b6 2020-10-19 stsp free(te_name);
5010 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5011 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5012 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5013 a9fa2909 2019-07-27 stsp goto done;
5014 a9fa2909 2019-07-27 stsp }
5015 a129376b 2019-03-28 stsp }
5016 a129376b 2019-03-28 stsp
5017 a129376b 2019-03-28 stsp switch (status) {
5018 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5019 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5020 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5021 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5022 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5023 33aa809d 2019-08-08 stsp if (err)
5024 33aa809d 2019-08-08 stsp goto done;
5025 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5026 33aa809d 2019-08-08 stsp break;
5027 33aa809d 2019-08-08 stsp }
5028 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5029 1f1abb7e 2019-08-08 stsp ie->path);
5030 1ee397ad 2019-07-12 stsp if (err)
5031 1ee397ad 2019-07-12 stsp goto done;
5032 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5033 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5034 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5035 af179be7 2023-04-14 stsp
5036 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5037 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5038 af179be7 2023-04-14 stsp
5039 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5040 af179be7 2023-04-14 stsp entry) {
5041 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5042 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5043 af179be7 2023-04-14 stsp continue;
5044 af179be7 2023-04-14 stsp do_unlink = 1;
5045 af179be7 2023-04-14 stsp break;
5046 af179be7 2023-04-14 stsp }
5047 f259c4c1 2021-09-24 stsp }
5048 af179be7 2023-04-14 stsp
5049 af179be7 2023-04-14 stsp if (do_unlink) {
5050 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5051 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5052 af179be7 2023-04-14 stsp relpath) == -1) {
5053 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5054 af179be7 2023-04-14 stsp goto done;
5055 af179be7 2023-04-14 stsp }
5056 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5057 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5058 af179be7 2023-04-14 stsp ondisk_path);
5059 af179be7 2023-04-14 stsp break;
5060 af179be7 2023-04-14 stsp }
5061 f259c4c1 2021-09-24 stsp }
5062 f259c4c1 2021-09-24 stsp }
5063 a129376b 2019-03-28 stsp break;
5064 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5065 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5066 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5067 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5068 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5069 33aa809d 2019-08-08 stsp if (err)
5070 33aa809d 2019-08-08 stsp goto done;
5071 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5072 33aa809d 2019-08-08 stsp break;
5073 33aa809d 2019-08-08 stsp }
5074 33aa809d 2019-08-08 stsp /* fall through */
5075 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5076 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5077 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5078 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5079 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5080 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5081 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5082 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5083 b4b2adf5 2023-02-09 op else
5084 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5085 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5086 eb81bc23 2022-06-28 tracey if (fd == -1) {
5087 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5088 eb81bc23 2022-06-28 tracey goto done;
5089 eb81bc23 2022-06-28 tracey }
5090 eb81bc23 2022-06-28 tracey
5091 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5092 a129376b 2019-03-28 stsp if (err)
5093 65084dad 2019-08-08 stsp goto done;
5094 65084dad 2019-08-08 stsp
5095 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5096 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5097 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5098 a129376b 2019-03-28 stsp goto done;
5099 65084dad 2019-08-08 stsp }
5100 65084dad 2019-08-08 stsp
5101 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5102 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5103 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5104 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5105 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5106 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5107 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5108 33aa809d 2019-08-08 stsp break;
5109 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5110 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5111 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5112 369fd7e5 2020-07-23 stsp path_content);
5113 369fd7e5 2020-07-23 stsp break;
5114 369fd7e5 2020-07-23 stsp }
5115 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5116 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5117 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5118 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5119 369fd7e5 2020-07-23 stsp } else {
5120 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5121 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5122 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5123 369fd7e5 2020-07-23 stsp goto done;
5124 369fd7e5 2020-07-23 stsp }
5125 33aa809d 2019-08-08 stsp }
5126 33aa809d 2019-08-08 stsp } else {
5127 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5128 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5129 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5130 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5131 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5132 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5133 2e1fa222 2020-07-23 stsp } else {
5134 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5135 2e1fa222 2020-07-23 stsp ie->path,
5136 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5137 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5138 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5139 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5140 2e1fa222 2020-07-23 stsp }
5141 a129376b 2019-03-28 stsp if (err)
5142 a129376b 2019-03-28 stsp goto done;
5143 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5144 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5145 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5146 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5147 437adc9d 2020-12-10 yzhong blob->id.sha1,
5148 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5149 2e1fa222 2020-07-23 stsp if (err)
5150 2e1fa222 2020-07-23 stsp goto done;
5151 2e1fa222 2020-07-23 stsp }
5152 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5153 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5154 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5155 33aa809d 2019-08-08 stsp }
5156 a129376b 2019-03-28 stsp }
5157 a129376b 2019-03-28 stsp break;
5158 e20a8b6f 2019-06-04 stsp }
5159 a129376b 2019-03-28 stsp default:
5160 1f1abb7e 2019-08-08 stsp break;
5161 a129376b 2019-03-28 stsp }
5162 a129376b 2019-03-28 stsp done:
5163 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5164 33aa809d 2019-08-08 stsp free(path_content);
5165 e20a8b6f 2019-06-04 stsp free(parent_path);
5166 a129376b 2019-03-28 stsp free(tree_path);
5167 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5168 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5169 a129376b 2019-03-28 stsp if (blob)
5170 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5171 a129376b 2019-03-28 stsp if (tree)
5172 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5173 a129376b 2019-03-28 stsp free(tree_id);
5174 a44927cc 2022-04-07 stsp if (base_commit)
5175 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5176 e20a8b6f 2019-06-04 stsp return err;
5177 e20a8b6f 2019-06-04 stsp }
5178 e20a8b6f 2019-06-04 stsp
5179 e20a8b6f 2019-06-04 stsp const struct got_error *
5180 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5181 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5182 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5183 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5184 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5185 e20a8b6f 2019-06-04 stsp {
5186 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5187 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5188 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5189 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5190 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5191 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5192 e20a8b6f 2019-06-04 stsp
5193 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5194 e20a8b6f 2019-06-04 stsp if (err)
5195 e20a8b6f 2019-06-04 stsp return err;
5196 e20a8b6f 2019-06-04 stsp
5197 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5198 e20a8b6f 2019-06-04 stsp if (err)
5199 e20a8b6f 2019-06-04 stsp goto done;
5200 e20a8b6f 2019-06-04 stsp
5201 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5202 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5203 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5204 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5205 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5206 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5207 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5208 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5209 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5210 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5211 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5212 e20a8b6f 2019-06-04 stsp if (err)
5213 af12c6b9 2019-06-04 stsp break;
5214 e20a8b6f 2019-06-04 stsp }
5215 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5216 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5217 e20a8b6f 2019-06-04 stsp err = sync_err;
5218 af12c6b9 2019-06-04 stsp done:
5219 fb399478 2019-07-12 stsp free(fileindex_path);
5220 a129376b 2019-03-28 stsp if (fileindex)
5221 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5222 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5223 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5224 a129376b 2019-03-28 stsp err = unlockerr;
5225 c4296144 2019-05-09 stsp return err;
5226 c4296144 2019-05-09 stsp }
5227 c4296144 2019-05-09 stsp
5228 cf066bf8 2019-05-09 stsp static void
5229 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5230 cf066bf8 2019-05-09 stsp {
5231 24519714 2019-05-09 stsp free(ct->path);
5232 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5233 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5234 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5235 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5236 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5237 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5238 cf066bf8 2019-05-09 stsp free(ct);
5239 cf066bf8 2019-05-09 stsp }
5240 24519714 2019-05-09 stsp
5241 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5242 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5243 24519714 2019-05-09 stsp struct got_repository *repo;
5244 24519714 2019-05-09 stsp struct got_worktree *worktree;
5245 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5246 5f8a88c6 2019-08-03 stsp int have_staged_files;
5247 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5248 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5249 12383673 2023-02-18 mark int commit_conflicts;
5250 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5251 2a47b1e5 2022-11-01 stsp FILE *f1;
5252 2a47b1e5 2022-11-01 stsp FILE *f2;
5253 24519714 2019-05-09 stsp };
5254 2a47b1e5 2022-11-01 stsp
5255 2a47b1e5 2022-11-01 stsp /*
5256 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5257 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5258 2a47b1e5 2022-11-01 stsp */
5259 2a47b1e5 2022-11-01 stsp static const struct got_error *
5260 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5261 2a47b1e5 2022-11-01 stsp const char *abspath)
5262 2a47b1e5 2022-11-01 stsp {
5263 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5264 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5265 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5266 2a47b1e5 2022-11-01 stsp
5267 2a47b1e5 2022-11-01 stsp *fd = -1;
5268 2a47b1e5 2022-11-01 stsp
5269 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5270 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5271 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5272 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5273 2a47b1e5 2022-11-01 stsp } else {
5274 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5275 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5276 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5277 2a47b1e5 2022-11-01 stsp }
5278 2a47b1e5 2022-11-01 stsp
5279 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5280 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5281 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5282 2a47b1e5 2022-11-01 stsp
5283 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5284 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5285 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5286 2a47b1e5 2022-11-01 stsp goto done;
5287 2a47b1e5 2022-11-01 stsp }
5288 2a47b1e5 2022-11-01 stsp
5289 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5290 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5291 2a47b1e5 2022-11-01 stsp goto done;
5292 2a47b1e5 2022-11-01 stsp }
5293 2a47b1e5 2022-11-01 stsp done:
5294 2a47b1e5 2022-11-01 stsp if (err) {
5295 2a47b1e5 2022-11-01 stsp close(*fd);
5296 2a47b1e5 2022-11-01 stsp *fd = -1;
5297 2a47b1e5 2022-11-01 stsp }
5298 2a47b1e5 2022-11-01 stsp return err;
5299 2a47b1e5 2022-11-01 stsp }
5300 2a47b1e5 2022-11-01 stsp
5301 2a47b1e5 2022-11-01 stsp static const struct got_error *
5302 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5303 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5304 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5305 2a47b1e5 2022-11-01 stsp {
5306 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5307 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5308 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5309 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5310 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5311 2a47b1e5 2022-11-01 stsp struct stat sb;
5312 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5313 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5314 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5315 2a47b1e5 2022-11-01 stsp
5316 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5317 4ba5cca9 2022-11-01 stsp
5318 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5319 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5320 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5321 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5322 4ba5cca9 2022-11-01 stsp return NULL;
5323 4ba5cca9 2022-11-01 stsp } else {
5324 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5325 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5326 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5327 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5328 4ba5cca9 2022-11-01 stsp return NULL;
5329 4ba5cca9 2022-11-01 stsp }
5330 2a47b1e5 2022-11-01 stsp
5331 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5332 2a47b1e5 2022-11-01 stsp if (err)
5333 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5334 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5335 2a47b1e5 2022-11-01 stsp if (err)
5336 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5337 2a47b1e5 2022-11-01 stsp
5338 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5339 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5340 2a47b1e5 2022-11-01 stsp if (err)
5341 2a47b1e5 2022-11-01 stsp return err;
5342 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5343 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5344 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5345 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5346 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5347 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5348 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5349 2a47b1e5 2022-11-01 stsp }
5350 2a47b1e5 2022-11-01 stsp
5351 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5352 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5353 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5354 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5355 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5356 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5357 2a47b1e5 2022-11-01 stsp break;
5358 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5359 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5360 2a47b1e5 2022-11-01 stsp break;
5361 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5362 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5363 2a47b1e5 2022-11-01 stsp break;
5364 2a47b1e5 2022-11-01 stsp default:
5365 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5366 2a47b1e5 2022-11-01 stsp }
5367 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5368 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5369 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5370 2a47b1e5 2022-11-01 stsp goto done;
5371 2a47b1e5 2022-11-01 stsp }
5372 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5373 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5374 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5375 2a47b1e5 2022-11-01 stsp goto done;
5376 2a47b1e5 2022-11-01 stsp }
5377 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5378 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5379 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5380 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5381 2a47b1e5 2022-11-01 stsp goto done;
5382 2a47b1e5 2022-11-01 stsp }
5383 2a47b1e5 2022-11-01 stsp
5384 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5385 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5386 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5387 2a47b1e5 2022-11-01 stsp goto done;
5388 2a47b1e5 2022-11-01 stsp }
5389 2a47b1e5 2022-11-01 stsp
5390 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5391 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5392 2a47b1e5 2022-11-01 stsp 8192, fd1);
5393 2a47b1e5 2022-11-01 stsp if (err)
5394 2a47b1e5 2022-11-01 stsp goto done;
5395 2a47b1e5 2022-11-01 stsp }
5396 2a47b1e5 2022-11-01 stsp
5397 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5398 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5399 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5400 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5401 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5402 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5403 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5404 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5405 2a47b1e5 2022-11-01 stsp goto done;
5406 2a47b1e5 2022-11-01 stsp }
5407 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5408 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5409 2a47b1e5 2022-11-01 stsp if (err)
5410 2a47b1e5 2022-11-01 stsp goto done;
5411 2a47b1e5 2022-11-01 stsp }
5412 2a47b1e5 2022-11-01 stsp } else {
5413 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5414 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5415 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5416 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5417 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5418 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5419 2a47b1e5 2022-11-01 stsp goto done;
5420 2a47b1e5 2022-11-01 stsp }
5421 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5422 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5423 2a47b1e5 2022-11-01 stsp if (err)
5424 2a47b1e5 2022-11-01 stsp goto done;
5425 2a47b1e5 2022-11-01 stsp }
5426 2a47b1e5 2022-11-01 stsp }
5427 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5428 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5429 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5430 2a47b1e5 2022-11-01 stsp goto done;
5431 2a47b1e5 2022-11-01 stsp }
5432 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5433 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5434 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5435 2a47b1e5 2022-11-01 stsp goto done;
5436 2a47b1e5 2022-11-01 stsp }
5437 2a47b1e5 2022-11-01 stsp fd = -1;
5438 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5439 2a47b1e5 2022-11-01 stsp }
5440 2a47b1e5 2022-11-01 stsp
5441 2a47b1e5 2022-11-01 stsp if (blob1) {
5442 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5443 2a47b1e5 2022-11-01 stsp f1, blob1);
5444 2a47b1e5 2022-11-01 stsp if (err)
5445 2a47b1e5 2022-11-01 stsp goto done;
5446 2a47b1e5 2022-11-01 stsp }
5447 2a47b1e5 2022-11-01 stsp
5448 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5449 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5450 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5451 2a47b1e5 2022-11-01 stsp done:
5452 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5453 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5454 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5455 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5456 2a47b1e5 2022-11-01 stsp if (blob1)
5457 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5458 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5459 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5460 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5461 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5462 2a47b1e5 2022-11-01 stsp return err;
5463 2a47b1e5 2022-11-01 stsp }
5464 cf066bf8 2019-05-09 stsp
5465 c4296144 2019-05-09 stsp static const struct got_error *
5466 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5467 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5468 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5469 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5470 c4296144 2019-05-09 stsp {
5471 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5472 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5473 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5474 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5475 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5476 768aea60 2019-05-09 stsp struct stat sb;
5477 c4296144 2019-05-09 stsp
5478 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5479 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5480 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5481 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5482 5f8a88c6 2019-08-03 stsp return NULL;
5483 5f8a88c6 2019-08-03 stsp } else {
5484 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5485 12383673 2023-02-18 mark printf("C %s\n", relpath);
5486 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5487 12383673 2023-02-18 mark }
5488 c4296144 2019-05-09 stsp
5489 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5490 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5491 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5492 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5493 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5494 5f8a88c6 2019-08-03 stsp return NULL;
5495 5f8a88c6 2019-08-03 stsp }
5496 0b5cc0d6 2019-05-09 stsp
5497 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5498 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5499 036813ee 2019-05-09 stsp goto done;
5500 036813ee 2019-05-09 stsp }
5501 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5502 036813ee 2019-05-09 stsp parent_path = strdup("");
5503 69960a46 2019-05-09 stsp if (parent_path == NULL)
5504 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5505 69960a46 2019-05-09 stsp } else {
5506 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5507 69960a46 2019-05-09 stsp if (err)
5508 69960a46 2019-05-09 stsp return err;
5509 69960a46 2019-05-09 stsp }
5510 c4296144 2019-05-09 stsp
5511 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5512 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5513 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5514 c4296144 2019-05-09 stsp goto done;
5515 768aea60 2019-05-09 stsp }
5516 768aea60 2019-05-09 stsp
5517 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5518 768aea60 2019-05-09 stsp relpath) == -1) {
5519 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5520 768aea60 2019-05-09 stsp goto done;
5521 768aea60 2019-05-09 stsp }
5522 0aeb8099 2020-07-23 stsp
5523 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5524 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5525 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5526 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5527 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5528 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5529 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5530 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5531 0aeb8099 2020-07-23 stsp break;
5532 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5533 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5534 0aeb8099 2020-07-23 stsp break;
5535 0aeb8099 2020-07-23 stsp default:
5536 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5537 0aeb8099 2020-07-23 stsp goto done;
5538 0aeb8099 2020-07-23 stsp }
5539 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5540 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5541 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5542 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5543 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5544 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5545 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5546 12463d8b 2019-12-13 stsp ct->ondisk_path);
5547 12463d8b 2019-12-13 stsp goto done;
5548 12463d8b 2019-12-13 stsp }
5549 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5550 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5551 768aea60 2019-05-09 stsp goto done;
5552 768aea60 2019-05-09 stsp }
5553 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5554 c4296144 2019-05-09 stsp }
5555 c4296144 2019-05-09 stsp
5556 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5557 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5558 44d03001 2019-05-09 stsp relpath) == -1) {
5559 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5560 44d03001 2019-05-09 stsp goto done;
5561 44d03001 2019-05-09 stsp }
5562 44d03001 2019-05-09 stsp
5563 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5564 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5565 35213c7c 2020-07-23 stsp int is_bad_symlink;
5566 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5567 35213c7c 2020-07-23 stsp ssize_t target_len;
5568 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5569 35213c7c 2020-07-23 stsp sizeof(target_path));
5570 35213c7c 2020-07-23 stsp if (target_len == -1) {
5571 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5572 35213c7c 2020-07-23 stsp ct->ondisk_path);
5573 35213c7c 2020-07-23 stsp goto done;
5574 35213c7c 2020-07-23 stsp }
5575 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5576 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5577 35213c7c 2020-07-23 stsp if (err)
5578 35213c7c 2020-07-23 stsp goto done;
5579 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5580 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5581 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5582 35213c7c 2020-07-23 stsp goto done;
5583 35213c7c 2020-07-23 stsp }
5584 35213c7c 2020-07-23 stsp }
5585 35213c7c 2020-07-23 stsp
5586 35213c7c 2020-07-23 stsp
5587 cf066bf8 2019-05-09 stsp ct->status = status;
5588 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5589 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5590 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5591 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5592 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5593 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5594 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5595 b416585c 2019-05-13 stsp goto done;
5596 b416585c 2019-05-13 stsp }
5597 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5598 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5599 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5600 f0b75401 2019-08-03 stsp goto done;
5601 f0b75401 2019-08-03 stsp }
5602 f0b75401 2019-08-03 stsp }
5603 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5604 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5605 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5606 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5607 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5608 036813ee 2019-05-09 stsp goto done;
5609 036813ee 2019-05-09 stsp }
5610 ca2503ea 2019-05-09 stsp }
5611 24519714 2019-05-09 stsp ct->path = strdup(path);
5612 24519714 2019-05-09 stsp if (ct->path == NULL) {
5613 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5614 24519714 2019-05-09 stsp goto done;
5615 24519714 2019-05-09 stsp }
5616 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5617 2a47b1e5 2022-11-01 stsp if (err)
5618 2a47b1e5 2022-11-01 stsp goto done;
5619 2a47b1e5 2022-11-01 stsp
5620 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5621 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5622 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5623 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5624 2a47b1e5 2022-11-01 stsp if (err)
5625 2a47b1e5 2022-11-01 stsp goto done;
5626 2a47b1e5 2022-11-01 stsp }
5627 c4296144 2019-05-09 stsp done:
5628 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5629 ed175427 2019-05-09 stsp free_commitable(ct);
5630 24519714 2019-05-09 stsp free(parent_path);
5631 036813ee 2019-05-09 stsp free(path);
5632 a129376b 2019-03-28 stsp return err;
5633 a129376b 2019-03-28 stsp }
5634 c4296144 2019-05-09 stsp
5635 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5636 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5637 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5638 036813ee 2019-05-09 stsp struct got_repository *);
5639 ed175427 2019-05-09 stsp
5640 ed175427 2019-05-09 stsp static const struct got_error *
5641 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5642 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5643 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5644 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5645 afa376bf 2019-05-09 stsp struct got_repository *repo)
5646 ed175427 2019-05-09 stsp {
5647 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5648 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5649 036813ee 2019-05-09 stsp char *subpath;
5650 ed175427 2019-05-09 stsp
5651 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5652 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5653 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5654 ed175427 2019-05-09 stsp
5655 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5656 ed175427 2019-05-09 stsp if (err)
5657 ed175427 2019-05-09 stsp return err;
5658 ed175427 2019-05-09 stsp
5659 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5660 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5661 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5662 036813ee 2019-05-09 stsp free(subpath);
5663 036813ee 2019-05-09 stsp return err;
5664 036813ee 2019-05-09 stsp }
5665 ed175427 2019-05-09 stsp
5666 036813ee 2019-05-09 stsp static const struct got_error *
5667 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5668 036813ee 2019-05-09 stsp {
5669 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5670 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5671 ed175427 2019-05-09 stsp
5672 036813ee 2019-05-09 stsp *match = 0;
5673 ed175427 2019-05-09 stsp
5674 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5675 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5676 0f63689d 2019-05-10 stsp return NULL;
5677 036813ee 2019-05-09 stsp }
5678 0b5cc0d6 2019-05-09 stsp
5679 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5680 0f63689d 2019-05-10 stsp if (err)
5681 0f63689d 2019-05-10 stsp return err;
5682 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5683 036813ee 2019-05-09 stsp free(ct_parent_path);
5684 036813ee 2019-05-09 stsp return err;
5685 036813ee 2019-05-09 stsp }
5686 0b5cc0d6 2019-05-09 stsp
5687 768aea60 2019-05-09 stsp static mode_t
5688 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5689 768aea60 2019-05-09 stsp {
5690 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5691 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5692 3d9a4ec4 2020-07-23 stsp
5693 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5694 768aea60 2019-05-09 stsp }
5695 768aea60 2019-05-09 stsp
5696 036813ee 2019-05-09 stsp static const struct got_error *
5697 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5698 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5699 036813ee 2019-05-09 stsp {
5700 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5701 ca2503ea 2019-05-09 stsp
5702 036813ee 2019-05-09 stsp *new_te = NULL;
5703 0b5cc0d6 2019-05-09 stsp
5704 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5705 036813ee 2019-05-09 stsp if (err)
5706 036813ee 2019-05-09 stsp goto done;
5707 0b5cc0d6 2019-05-09 stsp
5708 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5709 036813ee 2019-05-09 stsp
5710 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5711 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5712 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5713 5f8a88c6 2019-08-03 stsp else
5714 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5715 036813ee 2019-05-09 stsp done:
5716 036813ee 2019-05-09 stsp if (err && *new_te) {
5717 56e0773d 2019-11-28 stsp free(*new_te);
5718 036813ee 2019-05-09 stsp *new_te = NULL;
5719 036813ee 2019-05-09 stsp }
5720 036813ee 2019-05-09 stsp return err;
5721 036813ee 2019-05-09 stsp }
5722 036813ee 2019-05-09 stsp
5723 036813ee 2019-05-09 stsp static const struct got_error *
5724 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5725 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5726 036813ee 2019-05-09 stsp {
5727 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5728 102b254e 2020-10-19 stsp char *ct_name = NULL;
5729 036813ee 2019-05-09 stsp
5730 036813ee 2019-05-09 stsp *new_te = NULL;
5731 036813ee 2019-05-09 stsp
5732 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5733 036813ee 2019-05-09 stsp if (*new_te == NULL)
5734 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5735 036813ee 2019-05-09 stsp
5736 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5737 102b254e 2020-10-19 stsp if (err)
5738 036813ee 2019-05-09 stsp goto done;
5739 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5740 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5741 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5742 036813ee 2019-05-09 stsp goto done;
5743 036813ee 2019-05-09 stsp }
5744 036813ee 2019-05-09 stsp
5745 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5746 036813ee 2019-05-09 stsp
5747 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5748 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5749 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5750 5f8a88c6 2019-08-03 stsp else
5751 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5752 036813ee 2019-05-09 stsp done:
5753 102b254e 2020-10-19 stsp free(ct_name);
5754 036813ee 2019-05-09 stsp if (err && *new_te) {
5755 56e0773d 2019-11-28 stsp free(*new_te);
5756 036813ee 2019-05-09 stsp *new_te = NULL;
5757 036813ee 2019-05-09 stsp }
5758 036813ee 2019-05-09 stsp return err;
5759 036813ee 2019-05-09 stsp }
5760 036813ee 2019-05-09 stsp
5761 036813ee 2019-05-09 stsp static const struct got_error *
5762 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5763 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5764 036813ee 2019-05-09 stsp {
5765 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5766 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5767 036813ee 2019-05-09 stsp
5768 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5769 036813ee 2019-05-09 stsp if (err)
5770 036813ee 2019-05-09 stsp return err;
5771 036813ee 2019-05-09 stsp if (new_pe == NULL)
5772 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5773 036813ee 2019-05-09 stsp return NULL;
5774 afa376bf 2019-05-09 stsp }
5775 afa376bf 2019-05-09 stsp
5776 afa376bf 2019-05-09 stsp static const struct got_error *
5777 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5778 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5779 afa376bf 2019-05-09 stsp {
5780 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5781 5f8a88c6 2019-08-03 stsp unsigned char status;
5782 0ff8d236 2021-09-28 stsp
5783 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5784 0ff8d236 2021-09-28 stsp return NULL;
5785 5f8a88c6 2019-08-03 stsp
5786 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5787 afa376bf 2019-05-09 stsp ct_path++;
5788 5f8a88c6 2019-08-03 stsp
5789 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5790 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5791 5f8a88c6 2019-08-03 stsp else
5792 5f8a88c6 2019-08-03 stsp status = ct->status;
5793 5f8a88c6 2019-08-03 stsp
5794 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5795 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5796 036813ee 2019-05-09 stsp }
5797 036813ee 2019-05-09 stsp
5798 036813ee 2019-05-09 stsp static const struct got_error *
5799 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5800 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5801 44d03001 2019-05-09 stsp {
5802 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5803 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5804 44d03001 2019-05-09 stsp char *te_path;
5805 44d03001 2019-05-09 stsp
5806 44d03001 2019-05-09 stsp *modified = 0;
5807 44d03001 2019-05-09 stsp
5808 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5809 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5810 44d03001 2019-05-09 stsp te->name) == -1)
5811 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5812 44d03001 2019-05-09 stsp
5813 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5814 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5815 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5816 44d03001 2019-05-09 stsp strlen(te_path));
5817 62d463ca 2020-10-20 naddy if (*modified)
5818 44d03001 2019-05-09 stsp break;
5819 44d03001 2019-05-09 stsp }
5820 44d03001 2019-05-09 stsp
5821 44d03001 2019-05-09 stsp free(te_path);
5822 44d03001 2019-05-09 stsp return err;
5823 44d03001 2019-05-09 stsp }
5824 44d03001 2019-05-09 stsp
5825 44d03001 2019-05-09 stsp static const struct got_error *
5826 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5827 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5828 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5829 036813ee 2019-05-09 stsp {
5830 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5831 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5832 036813ee 2019-05-09 stsp
5833 036813ee 2019-05-09 stsp *ctp = NULL;
5834 036813ee 2019-05-09 stsp
5835 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5836 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5837 036813ee 2019-05-09 stsp char *ct_name = NULL;
5838 036813ee 2019-05-09 stsp int path_matches;
5839 036813ee 2019-05-09 stsp
5840 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5841 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5842 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5843 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5844 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5845 5f8a88c6 2019-08-03 stsp continue;
5846 5f8a88c6 2019-08-03 stsp } else {
5847 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5848 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5849 5f8a88c6 2019-08-03 stsp continue;
5850 5f8a88c6 2019-08-03 stsp }
5851 036813ee 2019-05-09 stsp
5852 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5853 036813ee 2019-05-09 stsp continue;
5854 036813ee 2019-05-09 stsp
5855 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5856 62d463ca 2020-10-20 naddy if (err)
5857 036813ee 2019-05-09 stsp return err;
5858 036813ee 2019-05-09 stsp if (!path_matches)
5859 036813ee 2019-05-09 stsp continue;
5860 036813ee 2019-05-09 stsp
5861 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5862 d34b633e 2020-10-19 stsp if (err)
5863 d34b633e 2020-10-19 stsp return err;
5864 d34b633e 2020-10-19 stsp
5865 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5866 d34b633e 2020-10-19 stsp free(ct_name);
5867 036813ee 2019-05-09 stsp continue;
5868 d34b633e 2020-10-19 stsp }
5869 d34b633e 2020-10-19 stsp free(ct_name);
5870 036813ee 2019-05-09 stsp
5871 036813ee 2019-05-09 stsp *ctp = ct;
5872 036813ee 2019-05-09 stsp break;
5873 036813ee 2019-05-09 stsp }
5874 2b496619 2019-07-10 stsp
5875 2b496619 2019-07-10 stsp return err;
5876 2b496619 2019-07-10 stsp }
5877 2b496619 2019-07-10 stsp
5878 2b496619 2019-07-10 stsp static const struct got_error *
5879 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5880 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5881 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5882 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5883 2b496619 2019-07-10 stsp struct got_repository *repo)
5884 2b496619 2019-07-10 stsp {
5885 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5886 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5887 2b496619 2019-07-10 stsp char *subtree_path;
5888 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5889 ba580f68 2020-03-22 stsp int nentries;
5890 2b496619 2019-07-10 stsp
5891 2b496619 2019-07-10 stsp *new_tep = NULL;
5892 2b496619 2019-07-10 stsp
5893 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5894 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5895 2b496619 2019-07-10 stsp child_path) == -1)
5896 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5897 036813ee 2019-05-09 stsp
5898 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5899 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5900 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5901 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5902 56e0773d 2019-11-28 stsp
5903 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5904 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5905 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5906 2b496619 2019-07-10 stsp goto done;
5907 2b496619 2019-07-10 stsp }
5908 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5909 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5910 2b496619 2019-07-10 stsp if (err) {
5911 56e0773d 2019-11-28 stsp free(new_te);
5912 2b496619 2019-07-10 stsp goto done;
5913 2b496619 2019-07-10 stsp }
5914 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5915 2b496619 2019-07-10 stsp done:
5916 56e0773d 2019-11-28 stsp free(id);
5917 2b496619 2019-07-10 stsp free(subtree_path);
5918 2b496619 2019-07-10 stsp if (err == NULL)
5919 2b496619 2019-07-10 stsp *new_tep = new_te;
5920 036813ee 2019-05-09 stsp return err;
5921 036813ee 2019-05-09 stsp }
5922 036813ee 2019-05-09 stsp
5923 036813ee 2019-05-09 stsp static const struct got_error *
5924 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5925 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5926 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5927 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5928 036813ee 2019-05-09 stsp struct got_repository *repo)
5929 036813ee 2019-05-09 stsp {
5930 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5931 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5932 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5933 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5934 036813ee 2019-05-09 stsp
5935 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5936 ba580f68 2020-03-22 stsp *nentries = 0;
5937 036813ee 2019-05-09 stsp
5938 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5939 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5940 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5941 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5942 036813ee 2019-05-09 stsp
5943 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5944 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5945 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5946 036813ee 2019-05-09 stsp continue;
5947 036813ee 2019-05-09 stsp
5948 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5949 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5950 036813ee 2019-05-09 stsp continue;
5951 036813ee 2019-05-09 stsp
5952 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5953 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5954 036813ee 2019-05-09 stsp if (err)
5955 036813ee 2019-05-09 stsp goto done;
5956 036813ee 2019-05-09 stsp
5957 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5958 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5959 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5960 036813ee 2019-05-09 stsp if (err)
5961 036813ee 2019-05-09 stsp goto done;
5962 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5963 afa376bf 2019-05-09 stsp if (err)
5964 afa376bf 2019-05-09 stsp goto done;
5965 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5966 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5967 2b496619 2019-07-10 stsp if (err)
5968 2f51b5b3 2019-05-09 stsp goto done;
5969 ba580f68 2020-03-22 stsp (*nentries)++;
5970 2b496619 2019-07-10 stsp } else {
5971 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5972 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5973 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5974 2b496619 2019-07-10 stsp == NULL) {
5975 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5976 2b496619 2019-07-10 stsp child_path, path_base_tree,
5977 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5978 2b496619 2019-07-10 stsp repo);
5979 2b496619 2019-07-10 stsp if (err)
5980 2b496619 2019-07-10 stsp goto done;
5981 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5982 2b496619 2019-07-10 stsp if (err)
5983 2b496619 2019-07-10 stsp goto done;
5984 ba580f68 2020-03-22 stsp (*nentries)++;
5985 9ba0479c 2019-05-10 stsp }
5986 ed175427 2019-05-09 stsp }
5987 2f51b5b3 2019-05-09 stsp }
5988 2f51b5b3 2019-05-09 stsp
5989 2f51b5b3 2019-05-09 stsp if (base_tree) {
5990 56e0773d 2019-11-28 stsp int i, nbase_entries;
5991 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5992 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5993 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5994 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5995 63c5ca5d 2019-08-24 stsp
5996 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5997 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5998 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5999 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6000 63c5ca5d 2019-08-24 stsp if (err)
6001 63c5ca5d 2019-08-24 stsp goto done;
6002 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6003 63c5ca5d 2019-08-24 stsp if (err)
6004 63c5ca5d 2019-08-24 stsp goto done;
6005 ba580f68 2020-03-22 stsp (*nentries)++;
6006 63c5ca5d 2019-08-24 stsp continue;
6007 63c5ca5d 2019-08-24 stsp }
6008 2f51b5b3 2019-05-09 stsp
6009 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6010 44d03001 2019-05-09 stsp int modified;
6011 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6012 036813ee 2019-05-09 stsp if (err)
6013 036813ee 2019-05-09 stsp goto done;
6014 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6015 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6016 2f51b5b3 2019-05-09 stsp if (err)
6017 2f51b5b3 2019-05-09 stsp goto done;
6018 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6019 44d03001 2019-05-09 stsp if (modified) {
6020 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6021 ba580f68 2020-03-22 stsp int nsubentries;
6022 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6023 ba580f68 2020-03-22 stsp &nsubentries, te,
6024 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6025 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6026 44d03001 2019-05-09 stsp if (err)
6027 44d03001 2019-05-09 stsp goto done;
6028 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6029 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6030 ba580f68 2020-03-22 stsp free(new_id);
6031 ba580f68 2020-03-22 stsp continue;
6032 ba580f68 2020-03-22 stsp }
6033 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6034 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6035 56e0773d 2019-11-28 stsp free(new_id);
6036 44d03001 2019-05-09 stsp }
6037 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6038 036813ee 2019-05-09 stsp if (err)
6039 036813ee 2019-05-09 stsp goto done;
6040 ba580f68 2020-03-22 stsp (*nentries)++;
6041 2f51b5b3 2019-05-09 stsp continue;
6042 036813ee 2019-05-09 stsp }
6043 2f51b5b3 2019-05-09 stsp
6044 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6045 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6046 f66c734c 2019-09-22 stsp if (err)
6047 f66c734c 2019-09-22 stsp goto done;
6048 2f51b5b3 2019-05-09 stsp if (ct) {
6049 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6050 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6051 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6052 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6053 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6054 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6055 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6056 2f51b5b3 2019-05-09 stsp if (err)
6057 2f51b5b3 2019-05-09 stsp goto done;
6058 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6059 2f51b5b3 2019-05-09 stsp if (err)
6060 2f51b5b3 2019-05-09 stsp goto done;
6061 ba580f68 2020-03-22 stsp (*nentries)++;
6062 2f51b5b3 2019-05-09 stsp }
6063 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6064 b416585c 2019-05-13 stsp status_arg);
6065 afa376bf 2019-05-09 stsp if (err)
6066 afa376bf 2019-05-09 stsp goto done;
6067 2f51b5b3 2019-05-09 stsp } else {
6068 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6069 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6070 2f51b5b3 2019-05-09 stsp if (err)
6071 2f51b5b3 2019-05-09 stsp goto done;
6072 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6073 2f51b5b3 2019-05-09 stsp if (err)
6074 2f51b5b3 2019-05-09 stsp goto done;
6075 ba580f68 2020-03-22 stsp (*nentries)++;
6076 2f51b5b3 2019-05-09 stsp }
6077 ca2503ea 2019-05-09 stsp }
6078 ed175427 2019-05-09 stsp }
6079 0b5cc0d6 2019-05-09 stsp
6080 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6081 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6082 ed175427 2019-05-09 stsp done:
6083 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6084 2e1fa222 2020-07-23 stsp return err;
6085 2e1fa222 2020-07-23 stsp }
6086 2e1fa222 2020-07-23 stsp
6087 2e1fa222 2020-07-23 stsp static const struct got_error *
6088 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6089 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6090 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6091 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6092 ebf99748 2019-05-09 stsp {
6093 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6094 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6095 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6096 ebf99748 2019-05-09 stsp
6097 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6098 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6099 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6100 ebf99748 2019-05-09 stsp
6101 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6102 437adc9d 2020-12-10 yzhong
6103 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6104 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6105 437adc9d 2020-12-10 yzhong if (err)
6106 437adc9d 2020-12-10 yzhong goto done;
6107 437adc9d 2020-12-10 yzhong
6108 ebf99748 2019-05-09 stsp if (ie) {
6109 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6110 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6111 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6112 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6113 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6114 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6115 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6116 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6117 437adc9d 2020-12-10 yzhong
6118 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6119 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6120 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6121 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6122 72fd46fa 2019-09-06 stsp !have_staged_files);
6123 ebf99748 2019-05-09 stsp } else
6124 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6125 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6126 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6127 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6128 72fd46fa 2019-09-06 stsp !have_staged_files);
6129 ebf99748 2019-05-09 stsp } else {
6130 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6131 ebf99748 2019-05-09 stsp if (err)
6132 437adc9d 2020-12-10 yzhong goto done;
6133 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6134 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6135 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6136 3969253a 2020-03-07 stsp if (err) {
6137 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6138 437adc9d 2020-12-10 yzhong goto done;
6139 3969253a 2020-03-07 stsp }
6140 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6141 3969253a 2020-03-07 stsp if (err) {
6142 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6143 437adc9d 2020-12-10 yzhong goto done;
6144 3969253a 2020-03-07 stsp }
6145 ebf99748 2019-05-09 stsp }
6146 437adc9d 2020-12-10 yzhong free(relpath);
6147 437adc9d 2020-12-10 yzhong relpath = NULL;
6148 ebf99748 2019-05-09 stsp }
6149 437adc9d 2020-12-10 yzhong done:
6150 437adc9d 2020-12-10 yzhong free(relpath);
6151 d56d26ce 2019-05-10 stsp return err;
6152 d56d26ce 2019-05-10 stsp }
6153 735ef5ac 2019-08-03 stsp
6154 d56d26ce 2019-05-10 stsp
6155 d56d26ce 2019-05-10 stsp static const struct got_error *
6156 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6157 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6158 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6159 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6160 735ef5ac 2019-08-03 stsp int ood_errcode)
6161 d56d26ce 2019-05-10 stsp {
6162 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6163 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6164 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6165 1a36436d 2019-06-10 stsp
6166 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6167 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6168 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6169 1a36436d 2019-06-10 stsp return NULL;
6170 9bead371 2019-07-28 stsp /*
6171 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6172 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6173 9bead371 2019-07-28 stsp */
6174 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6175 a44927cc 2022-04-07 stsp if (err)
6176 a44927cc 2022-04-07 stsp goto done;
6177 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6178 9bead371 2019-07-28 stsp if (err) {
6179 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6180 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6181 1a36436d 2019-06-10 stsp goto done;
6182 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6183 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6184 1a36436d 2019-06-10 stsp } else {
6185 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6186 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6187 a44927cc 2022-04-07 stsp if (err)
6188 a44927cc 2022-04-07 stsp goto done;
6189 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6190 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6191 1a36436d 2019-06-10 stsp goto done;
6192 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6193 1a36436d 2019-06-10 stsp }
6194 1a36436d 2019-06-10 stsp done:
6195 1a36436d 2019-06-10 stsp free(id);
6196 a44927cc 2022-04-07 stsp if (commit)
6197 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6198 1a36436d 2019-06-10 stsp return err;
6199 ebf99748 2019-05-09 stsp }
6200 ebf99748 2019-05-09 stsp
6201 336075a4 2022-06-25 op static const struct got_error *
6202 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6203 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6204 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6205 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6206 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6207 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6208 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6209 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6210 afa376bf 2019-05-09 stsp struct got_repository *repo)
6211 c4296144 2019-05-09 stsp {
6212 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6213 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6214 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6215 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6216 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6217 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6218 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6219 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6220 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6221 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6222 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6223 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6224 f8399b8f 2022-07-22 stsp time_t timestamp;
6225 c4296144 2019-05-09 stsp
6226 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6227 c4296144 2019-05-09 stsp
6228 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6229 675c7539 2019-05-09 stsp
6230 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6231 588edf97 2019-05-10 stsp if (err)
6232 588edf97 2019-05-10 stsp goto done;
6233 de18fc63 2019-05-09 stsp
6234 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6235 588edf97 2019-05-10 stsp if (err)
6236 588edf97 2019-05-10 stsp goto done;
6237 588edf97 2019-05-10 stsp
6238 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6239 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6240 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6241 33ad4cbe 2019-05-12 jcs if (err)
6242 33ad4cbe 2019-05-12 jcs goto done;
6243 33ad4cbe 2019-05-12 jcs }
6244 c4296144 2019-05-09 stsp
6245 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6246 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6247 33ad4cbe 2019-05-12 jcs goto done;
6248 33ad4cbe 2019-05-12 jcs }
6249 33ad4cbe 2019-05-12 jcs
6250 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6251 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6252 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6253 cf066bf8 2019-05-09 stsp char *ondisk_path;
6254 cf066bf8 2019-05-09 stsp
6255 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6256 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6257 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6258 5f8a88c6 2019-08-03 stsp continue;
6259 5f8a88c6 2019-08-03 stsp
6260 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6261 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6262 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6263 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6264 cf066bf8 2019-05-09 stsp continue;
6265 cf066bf8 2019-05-09 stsp
6266 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6267 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6268 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6269 cf066bf8 2019-05-09 stsp goto done;
6270 cf066bf8 2019-05-09 stsp }
6271 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6272 2e1fa222 2020-07-23 stsp free(ondisk_path);
6273 2e1fa222 2020-07-23 stsp if (err)
6274 cf066bf8 2019-05-09 stsp goto done;
6275 cf066bf8 2019-05-09 stsp }
6276 036813ee 2019-05-09 stsp
6277 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6278 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6279 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6280 036813ee 2019-05-09 stsp if (err)
6281 036813ee 2019-05-09 stsp goto done;
6282 036813ee 2019-05-09 stsp
6283 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6284 de18fc63 2019-05-09 stsp if (err)
6285 de18fc63 2019-05-09 stsp goto done;
6286 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6287 f259c4c1 2021-09-24 stsp nparents++;
6288 f259c4c1 2021-09-24 stsp if (parent_id2) {
6289 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6290 f259c4c1 2021-09-24 stsp if (err)
6291 f259c4c1 2021-09-24 stsp goto done;
6292 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6293 f259c4c1 2021-09-24 stsp nparents++;
6294 f259c4c1 2021-09-24 stsp }
6295 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6296 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6297 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6298 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6299 33ad4cbe 2019-05-12 jcs free(logmsg);
6300 9d40349a 2019-05-09 stsp if (err)
6301 9d40349a 2019-05-09 stsp goto done;
6302 9d40349a 2019-05-09 stsp
6303 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6304 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6305 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6306 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6307 09f5bd90 2019-05-09 stsp goto done;
6308 09f5bd90 2019-05-09 stsp }
6309 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6310 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6311 09f5bd90 2019-05-09 stsp if (err)
6312 09f5bd90 2019-05-09 stsp goto done;
6313 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6314 ebf99748 2019-05-09 stsp if (err)
6315 ebf99748 2019-05-09 stsp goto done;
6316 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6317 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6318 09f5bd90 2019-05-09 stsp goto done;
6319 09f5bd90 2019-05-09 stsp }
6320 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6321 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6322 f2c16586 2019-05-09 stsp if (err)
6323 f2c16586 2019-05-09 stsp goto done;
6324 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6325 f2c16586 2019-05-09 stsp if (err)
6326 f2c16586 2019-05-09 stsp goto done;
6327 f2c16586 2019-05-09 stsp
6328 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6329 09f5bd90 2019-05-09 stsp if (err)
6330 09f5bd90 2019-05-09 stsp goto done;
6331 09f5bd90 2019-05-09 stsp
6332 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6333 ebf99748 2019-05-09 stsp if (err)
6334 ebf99748 2019-05-09 stsp goto done;
6335 c4296144 2019-05-09 stsp done:
6336 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6337 588edf97 2019-05-10 stsp if (head_tree)
6338 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6339 588edf97 2019-05-10 stsp if (head_commit)
6340 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6341 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6342 2f17228e 2019-05-12 stsp if (head_ref2) {
6343 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6344 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6345 2f17228e 2019-05-12 stsp err = unlockerr;
6346 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6347 39cd0ff6 2019-07-12 stsp }
6348 39cd0ff6 2019-07-12 stsp return err;
6349 39cd0ff6 2019-07-12 stsp }
6350 5c1e53bc 2019-07-28 stsp
6351 5c1e53bc 2019-07-28 stsp static const struct got_error *
6352 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6353 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6354 5c1e53bc 2019-07-28 stsp {
6355 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6356 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6357 39cd0ff6 2019-07-12 stsp
6358 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6359 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6360 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6361 5c1e53bc 2019-07-28 stsp
6362 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6363 5c1e53bc 2019-07-28 stsp ct_path++;
6364 5c1e53bc 2019-07-28 stsp
6365 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6366 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6367 5c1e53bc 2019-07-28 stsp break;
6368 5c1e53bc 2019-07-28 stsp }
6369 5c1e53bc 2019-07-28 stsp
6370 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6371 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6372 5c1e53bc 2019-07-28 stsp
6373 5c1e53bc 2019-07-28 stsp return NULL;
6374 5c1e53bc 2019-07-28 stsp }
6375 5c1e53bc 2019-07-28 stsp
6376 f0b75401 2019-08-03 stsp static const struct got_error *
6377 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6378 f0b75401 2019-08-03 stsp {
6379 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6380 f0b75401 2019-08-03 stsp
6381 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6382 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6383 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6384 f0b75401 2019-08-03 stsp }
6385 f0b75401 2019-08-03 stsp
6386 f0b75401 2019-08-03 stsp return NULL;
6387 f0b75401 2019-08-03 stsp }
6388 f0b75401 2019-08-03 stsp
6389 5f8a88c6 2019-08-03 stsp static const struct got_error *
6390 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6391 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6392 5f8a88c6 2019-08-03 stsp {
6393 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6394 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6395 5f8a88c6 2019-08-03 stsp
6396 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6397 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6398 5f8a88c6 2019-08-03 stsp continue;
6399 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6400 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6401 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6402 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6403 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6404 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6405 5f8a88c6 2019-08-03 stsp }
6406 5f8a88c6 2019-08-03 stsp
6407 5f8a88c6 2019-08-03 stsp return NULL;
6408 5f8a88c6 2019-08-03 stsp }
6409 5f8a88c6 2019-08-03 stsp
6410 39cd0ff6 2019-07-12 stsp const struct got_error *
6411 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6412 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6413 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6414 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6415 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6416 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6417 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6418 39cd0ff6 2019-07-12 stsp {
6419 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6420 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6421 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6422 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6423 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6424 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6425 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6426 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6427 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6428 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6429 39cd0ff6 2019-07-12 stsp
6430 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6431 39cd0ff6 2019-07-12 stsp
6432 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6433 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6434 39cd0ff6 2019-07-12 stsp
6435 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6436 39cd0ff6 2019-07-12 stsp if (err)
6437 39cd0ff6 2019-07-12 stsp goto done;
6438 39cd0ff6 2019-07-12 stsp
6439 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6440 39cd0ff6 2019-07-12 stsp if (err)
6441 39cd0ff6 2019-07-12 stsp goto done;
6442 39cd0ff6 2019-07-12 stsp
6443 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6444 39cd0ff6 2019-07-12 stsp if (err)
6445 39cd0ff6 2019-07-12 stsp goto done;
6446 39cd0ff6 2019-07-12 stsp
6447 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6448 39cd0ff6 2019-07-12 stsp if (err)
6449 39cd0ff6 2019-07-12 stsp goto done;
6450 39cd0ff6 2019-07-12 stsp
6451 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6452 5f8a88c6 2019-08-03 stsp &have_staged_files);
6453 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6454 5f8a88c6 2019-08-03 stsp goto done;
6455 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6456 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6457 5f8a88c6 2019-08-03 stsp if (err)
6458 5f8a88c6 2019-08-03 stsp goto done;
6459 5f8a88c6 2019-08-03 stsp }
6460 5f8a88c6 2019-08-03 stsp
6461 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6462 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6463 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6464 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6465 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6466 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6467 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6468 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6469 2a47b1e5 2022-11-01 stsp if (show_diff) {
6470 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6471 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6472 2a47b1e5 2022-11-01 stsp if (err)
6473 2a47b1e5 2022-11-01 stsp goto done;
6474 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6475 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6476 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6477 2a47b1e5 2022-11-01 stsp goto done;
6478 2a47b1e5 2022-11-01 stsp }
6479 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6480 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6481 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6482 2a47b1e5 2022-11-01 stsp goto done;
6483 2a47b1e5 2022-11-01 stsp }
6484 2a47b1e5 2022-11-01 stsp }
6485 2a47b1e5 2022-11-01 stsp
6486 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6487 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6488 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6489 5c1e53bc 2019-07-28 stsp if (err)
6490 5c1e53bc 2019-07-28 stsp goto done;
6491 5c1e53bc 2019-07-28 stsp }
6492 39cd0ff6 2019-07-12 stsp
6493 2a47b1e5 2022-11-01 stsp if (show_diff) {
6494 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6495 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6496 2a47b1e5 2022-11-01 stsp goto done;
6497 2a47b1e5 2022-11-01 stsp }
6498 2a47b1e5 2022-11-01 stsp }
6499 2a47b1e5 2022-11-01 stsp
6500 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6501 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6502 39cd0ff6 2019-07-12 stsp goto done;
6503 5c1e53bc 2019-07-28 stsp }
6504 5c1e53bc 2019-07-28 stsp
6505 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6506 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6507 5c1e53bc 2019-07-28 stsp if (err)
6508 5c1e53bc 2019-07-28 stsp goto done;
6509 39cd0ff6 2019-07-12 stsp }
6510 f0b75401 2019-08-03 stsp
6511 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6512 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6513 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6514 f0b75401 2019-08-03 stsp
6515 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6516 f0b75401 2019-08-03 stsp ct_path++;
6517 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6518 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6519 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6520 b50cabdf 2019-07-12 stsp if (err)
6521 b50cabdf 2019-07-12 stsp goto done;
6522 f0b75401 2019-08-03 stsp
6523 b50cabdf 2019-07-12 stsp }
6524 b50cabdf 2019-07-12 stsp
6525 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6526 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6527 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6528 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6529 39cd0ff6 2019-07-12 stsp if (err)
6530 39cd0ff6 2019-07-12 stsp goto done;
6531 39cd0ff6 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, have_staged_files);
6534 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6535 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6536 39cd0ff6 2019-07-12 stsp err = sync_err;
6537 39cd0ff6 2019-07-12 stsp done:
6538 39cd0ff6 2019-07-12 stsp if (fileindex)
6539 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6540 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6541 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6542 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6543 39cd0ff6 2019-07-12 stsp err = unlockerr;
6544 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6545 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6546 d8bacb93 2023-01-10 mark
6547 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6548 39cd0ff6 2019-07-12 stsp }
6549 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6550 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6551 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6552 2a47b1e5 2022-11-01 stsp free(diff_path);
6553 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6554 2a47b1e5 2022-11-01 stsp err == NULL)
6555 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6556 c4296144 2019-05-09 stsp return err;
6557 8656d6c4 2019-05-20 stsp }
6558 8656d6c4 2019-05-20 stsp
6559 8656d6c4 2019-05-20 stsp const char *
6560 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6561 8656d6c4 2019-05-20 stsp {
6562 8656d6c4 2019-05-20 stsp return ct->path;
6563 8656d6c4 2019-05-20 stsp }
6564 8656d6c4 2019-05-20 stsp
6565 8656d6c4 2019-05-20 stsp unsigned int
6566 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6567 8656d6c4 2019-05-20 stsp {
6568 8656d6c4 2019-05-20 stsp return ct->status;
6569 818c7501 2019-07-11 stsp }
6570 818c7501 2019-07-11 stsp
6571 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6572 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6573 818c7501 2019-07-11 stsp struct got_repository *repo;
6574 818c7501 2019-07-11 stsp };
6575 818c7501 2019-07-11 stsp
6576 818c7501 2019-07-11 stsp static const struct got_error *
6577 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6578 818c7501 2019-07-11 stsp {
6579 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6580 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6581 818c7501 2019-07-11 stsp unsigned char status;
6582 818c7501 2019-07-11 stsp struct stat sb;
6583 818c7501 2019-07-11 stsp char *ondisk_path;
6584 818c7501 2019-07-11 stsp
6585 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6586 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6587 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6588 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6589 818c7501 2019-07-11 stsp
6590 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6591 818c7501 2019-07-11 stsp == -1)
6592 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6593 818c7501 2019-07-11 stsp
6594 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6595 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6596 818c7501 2019-07-11 stsp free(ondisk_path);
6597 818c7501 2019-07-11 stsp if (err)
6598 818c7501 2019-07-11 stsp return err;
6599 818c7501 2019-07-11 stsp
6600 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6601 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6602 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6603 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6604 818c7501 2019-07-11 stsp
6605 818c7501 2019-07-11 stsp return NULL;
6606 818c7501 2019-07-11 stsp }
6607 818c7501 2019-07-11 stsp
6608 818c7501 2019-07-11 stsp const struct got_error *
6609 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6610 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6611 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6612 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6613 818c7501 2019-07-11 stsp {
6614 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6615 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6616 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6617 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6618 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6619 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6620 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6621 818c7501 2019-07-11 stsp
6622 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6623 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6624 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6625 818c7501 2019-07-11 stsp
6626 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6627 818c7501 2019-07-11 stsp if (err)
6628 818c7501 2019-07-11 stsp return err;
6629 818c7501 2019-07-11 stsp
6630 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6631 818c7501 2019-07-11 stsp if (err)
6632 818c7501 2019-07-11 stsp goto done;
6633 818c7501 2019-07-11 stsp
6634 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6635 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6636 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6637 818c7501 2019-07-11 stsp &ok_arg);
6638 818c7501 2019-07-11 stsp if (err)
6639 818c7501 2019-07-11 stsp goto done;
6640 818c7501 2019-07-11 stsp
6641 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6642 818c7501 2019-07-11 stsp if (err)
6643 818c7501 2019-07-11 stsp goto done;
6644 818c7501 2019-07-11 stsp
6645 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6646 818c7501 2019-07-11 stsp if (err)
6647 818c7501 2019-07-11 stsp goto done;
6648 818c7501 2019-07-11 stsp
6649 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6650 818c7501 2019-07-11 stsp if (err)
6651 818c7501 2019-07-11 stsp goto done;
6652 818c7501 2019-07-11 stsp
6653 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6654 818c7501 2019-07-11 stsp 0);
6655 e51d7b55 2020-01-04 stsp if (err)
6656 e51d7b55 2020-01-04 stsp goto done;
6657 e51d7b55 2020-01-04 stsp
6658 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6659 818c7501 2019-07-11 stsp if (err)
6660 818c7501 2019-07-11 stsp goto done;
6661 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6662 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6663 e51d7b55 2020-01-04 stsp goto done;
6664 e51d7b55 2020-01-04 stsp }
6665 818c7501 2019-07-11 stsp
6666 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6667 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6668 818c7501 2019-07-11 stsp if (err)
6669 818c7501 2019-07-11 stsp goto done;
6670 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6671 818c7501 2019-07-11 stsp if (err)
6672 818c7501 2019-07-11 stsp goto done;
6673 818c7501 2019-07-11 stsp
6674 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6675 818c7501 2019-07-11 stsp
6676 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6677 818c7501 2019-07-11 stsp if (err)
6678 818c7501 2019-07-11 stsp goto done;
6679 818c7501 2019-07-11 stsp
6680 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6681 818c7501 2019-07-11 stsp if (err)
6682 818c7501 2019-07-11 stsp goto done;
6683 818c7501 2019-07-11 stsp
6684 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6685 818c7501 2019-07-11 stsp worktree->base_commit_id);
6686 818c7501 2019-07-11 stsp if (err)
6687 818c7501 2019-07-11 stsp goto done;
6688 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6689 818c7501 2019-07-11 stsp if (err)
6690 818c7501 2019-07-11 stsp goto done;
6691 818c7501 2019-07-11 stsp
6692 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6693 818c7501 2019-07-11 stsp if (err)
6694 818c7501 2019-07-11 stsp goto done;
6695 818c7501 2019-07-11 stsp done:
6696 818c7501 2019-07-11 stsp free(fileindex_path);
6697 818c7501 2019-07-11 stsp free(tmp_branch_name);
6698 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6699 818c7501 2019-07-11 stsp free(branch_ref_name);
6700 818c7501 2019-07-11 stsp if (branch_ref)
6701 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6702 818c7501 2019-07-11 stsp if (wt_branch)
6703 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6704 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6705 818c7501 2019-07-11 stsp if (err) {
6706 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6707 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6708 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6709 818c7501 2019-07-11 stsp }
6710 818c7501 2019-07-11 stsp if (*tmp_branch) {
6711 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6712 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6713 818c7501 2019-07-11 stsp }
6714 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6715 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6716 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6717 3e3a69f1 2019-07-25 stsp }
6718 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6719 818c7501 2019-07-11 stsp }
6720 818c7501 2019-07-11 stsp return err;
6721 818c7501 2019-07-11 stsp }
6722 818c7501 2019-07-11 stsp
6723 818c7501 2019-07-11 stsp const struct got_error *
6724 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6725 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6726 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6727 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6728 818c7501 2019-07-11 stsp {
6729 818c7501 2019-07-11 stsp const struct got_error *err;
6730 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6731 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6732 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6733 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6734 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6735 818c7501 2019-07-11 stsp
6736 818c7501 2019-07-11 stsp *commit_id = NULL;
6737 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6738 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6739 3e3a69f1 2019-07-25 stsp *branch = NULL;
6740 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6741 3e3a69f1 2019-07-25 stsp
6742 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6743 3e3a69f1 2019-07-25 stsp if (err)
6744 3e3a69f1 2019-07-25 stsp return err;
6745 818c7501 2019-07-11 stsp
6746 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6747 3e3a69f1 2019-07-25 stsp if (err)
6748 f032f1f7 2019-08-04 stsp goto done;
6749 f032f1f7 2019-08-04 stsp
6750 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6751 f032f1f7 2019-08-04 stsp &have_staged_files);
6752 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6753 f032f1f7 2019-08-04 stsp goto done;
6754 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6755 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6756 3e3a69f1 2019-07-25 stsp goto done;
6757 f032f1f7 2019-08-04 stsp }
6758 3e3a69f1 2019-07-25 stsp
6759 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6760 818c7501 2019-07-11 stsp if (err)
6761 f032f1f7 2019-08-04 stsp goto done;
6762 818c7501 2019-07-11 stsp
6763 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6764 818c7501 2019-07-11 stsp if (err)
6765 818c7501 2019-07-11 stsp goto done;
6766 818c7501 2019-07-11 stsp
6767 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6768 818c7501 2019-07-11 stsp if (err)
6769 818c7501 2019-07-11 stsp goto done;
6770 818c7501 2019-07-11 stsp
6771 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6772 818c7501 2019-07-11 stsp if (err)
6773 818c7501 2019-07-11 stsp goto done;
6774 818c7501 2019-07-11 stsp
6775 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6776 818c7501 2019-07-11 stsp if (err)
6777 818c7501 2019-07-11 stsp goto done;
6778 818c7501 2019-07-11 stsp
6779 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6780 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6781 818c7501 2019-07-11 stsp if (err)
6782 818c7501 2019-07-11 stsp goto done;
6783 818c7501 2019-07-11 stsp
6784 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6785 818c7501 2019-07-11 stsp if (err)
6786 818c7501 2019-07-11 stsp goto done;
6787 818c7501 2019-07-11 stsp
6788 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6789 818c7501 2019-07-11 stsp if (err)
6790 818c7501 2019-07-11 stsp goto done;
6791 818c7501 2019-07-11 stsp
6792 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6793 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6794 818c7501 2019-07-11 stsp if (err)
6795 818c7501 2019-07-11 stsp goto done;
6796 818c7501 2019-07-11 stsp
6797 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp done:
6801 818c7501 2019-07-11 stsp free(commit_ref_name);
6802 818c7501 2019-07-11 stsp free(branch_ref_name);
6803 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6804 818c7501 2019-07-11 stsp if (commit_ref)
6805 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6806 818c7501 2019-07-11 stsp if (branch_ref)
6807 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6808 818c7501 2019-07-11 stsp if (err) {
6809 818c7501 2019-07-11 stsp free(*commit_id);
6810 818c7501 2019-07-11 stsp *commit_id = NULL;
6811 818c7501 2019-07-11 stsp if (*tmp_branch) {
6812 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6813 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6814 818c7501 2019-07-11 stsp }
6815 818c7501 2019-07-11 stsp if (*new_base_branch) {
6816 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6817 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6818 818c7501 2019-07-11 stsp }
6819 818c7501 2019-07-11 stsp if (*branch) {
6820 818c7501 2019-07-11 stsp got_ref_close(*branch);
6821 818c7501 2019-07-11 stsp *branch = NULL;
6822 818c7501 2019-07-11 stsp }
6823 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6824 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6825 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6826 3e3a69f1 2019-07-25 stsp }
6827 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6828 818c7501 2019-07-11 stsp }
6829 818c7501 2019-07-11 stsp return err;
6830 c4296144 2019-05-09 stsp }
6831 818c7501 2019-07-11 stsp
6832 818c7501 2019-07-11 stsp const struct got_error *
6833 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6834 818c7501 2019-07-11 stsp {
6835 818c7501 2019-07-11 stsp const struct got_error *err;
6836 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6837 818c7501 2019-07-11 stsp
6838 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6839 818c7501 2019-07-11 stsp if (err)
6840 818c7501 2019-07-11 stsp return err;
6841 818c7501 2019-07-11 stsp
6842 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6843 818c7501 2019-07-11 stsp free(tmp_branch_name);
6844 818c7501 2019-07-11 stsp return NULL;
6845 818c7501 2019-07-11 stsp }
6846 818c7501 2019-07-11 stsp
6847 818c7501 2019-07-11 stsp static const struct got_error *
6848 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6849 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6850 818c7501 2019-07-11 stsp {
6851 0ebf8283 2019-07-24 stsp *logmsg = arg;
6852 818c7501 2019-07-11 stsp return NULL;
6853 818c7501 2019-07-11 stsp }
6854 818c7501 2019-07-11 stsp
6855 818c7501 2019-07-11 stsp static const struct got_error *
6856 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6857 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6858 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6859 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6860 818c7501 2019-07-11 stsp {
6861 818c7501 2019-07-11 stsp return NULL;
6862 01757395 2019-07-12 stsp }
6863 01757395 2019-07-12 stsp
6864 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6865 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6866 01757395 2019-07-12 stsp void *progress_arg;
6867 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6868 01757395 2019-07-12 stsp };
6869 01757395 2019-07-12 stsp
6870 01757395 2019-07-12 stsp static const struct got_error *
6871 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6872 01757395 2019-07-12 stsp {
6873 01757395 2019-07-12 stsp const struct got_error *err;
6874 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6875 01757395 2019-07-12 stsp char *p;
6876 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6877 01757395 2019-07-12 stsp
6878 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6879 01757395 2019-07-12 stsp if (err)
6880 01757395 2019-07-12 stsp return err;
6881 01757395 2019-07-12 stsp
6882 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6883 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6884 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6885 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6886 01757395 2019-07-12 stsp return NULL;
6887 01757395 2019-07-12 stsp
6888 01757395 2019-07-12 stsp p = strdup(path);
6889 01757395 2019-07-12 stsp if (p == NULL)
6890 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6891 01757395 2019-07-12 stsp
6892 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6893 01757395 2019-07-12 stsp if (err || new == NULL)
6894 01757395 2019-07-12 stsp free(p);
6895 01757395 2019-07-12 stsp return err;
6896 818c7501 2019-07-11 stsp }
6897 818c7501 2019-07-11 stsp
6898 0ebf8283 2019-07-24 stsp static const struct got_error *
6899 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6900 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6901 818c7501 2019-07-11 stsp {
6902 818c7501 2019-07-11 stsp const struct got_error *err;
6903 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6904 818c7501 2019-07-11 stsp
6905 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6906 818c7501 2019-07-11 stsp if (err) {
6907 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6908 818c7501 2019-07-11 stsp goto done;
6909 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6910 818c7501 2019-07-11 stsp if (err)
6911 818c7501 2019-07-11 stsp goto done;
6912 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6913 818c7501 2019-07-11 stsp if (err)
6914 818c7501 2019-07-11 stsp goto done;
6915 de05890f 2020-03-05 stsp } else if (is_rebase) {
6916 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6917 818c7501 2019-07-11 stsp int cmp;
6918 818c7501 2019-07-11 stsp
6919 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6920 818c7501 2019-07-11 stsp if (err)
6921 818c7501 2019-07-11 stsp goto done;
6922 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6923 818c7501 2019-07-11 stsp free(stored_id);
6924 818c7501 2019-07-11 stsp if (cmp != 0) {
6925 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6926 818c7501 2019-07-11 stsp goto done;
6927 818c7501 2019-07-11 stsp }
6928 818c7501 2019-07-11 stsp }
6929 0ebf8283 2019-07-24 stsp done:
6930 0ebf8283 2019-07-24 stsp if (commit_ref)
6931 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6932 0ebf8283 2019-07-24 stsp return err;
6933 0ebf8283 2019-07-24 stsp }
6934 0ebf8283 2019-07-24 stsp
6935 0ebf8283 2019-07-24 stsp static const struct got_error *
6936 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6937 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6938 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6939 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6940 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6941 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6942 0ebf8283 2019-07-24 stsp {
6943 0ebf8283 2019-07-24 stsp const struct got_error *err;
6944 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6945 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6946 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6947 818c7501 2019-07-11 stsp
6948 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6949 0ebf8283 2019-07-24 stsp
6950 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6951 0ebf8283 2019-07-24 stsp if (err)
6952 0ebf8283 2019-07-24 stsp return err;
6953 0ebf8283 2019-07-24 stsp
6954 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6955 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6956 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6957 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6958 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6959 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6960 818c7501 2019-07-11 stsp if (commit_ref)
6961 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6962 818c7501 2019-07-11 stsp return err;
6963 818c7501 2019-07-11 stsp }
6964 818c7501 2019-07-11 stsp
6965 818c7501 2019-07-11 stsp const struct got_error *
6966 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6967 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6968 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6969 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6970 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6971 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6972 818c7501 2019-07-11 stsp {
6973 0ebf8283 2019-07-24 stsp const struct got_error *err;
6974 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6975 0ebf8283 2019-07-24 stsp
6976 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6977 0ebf8283 2019-07-24 stsp if (err)
6978 0ebf8283 2019-07-24 stsp return err;
6979 0ebf8283 2019-07-24 stsp
6980 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6981 0ebf8283 2019-07-24 stsp if (err)
6982 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp
6984 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6985 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6986 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6987 0ebf8283 2019-07-24 stsp done:
6988 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6989 0ebf8283 2019-07-24 stsp return err;
6990 0ebf8283 2019-07-24 stsp }
6991 0ebf8283 2019-07-24 stsp
6992 0ebf8283 2019-07-24 stsp const struct got_error *
6993 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6994 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6995 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6996 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6997 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6998 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6999 0ebf8283 2019-07-24 stsp {
7000 0ebf8283 2019-07-24 stsp const struct got_error *err;
7001 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7002 0ebf8283 2019-07-24 stsp
7003 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_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 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7008 0ebf8283 2019-07-24 stsp if (err)
7009 0ebf8283 2019-07-24 stsp goto done;
7010 0ebf8283 2019-07-24 stsp
7011 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7012 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7013 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7014 0ebf8283 2019-07-24 stsp done:
7015 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7016 0ebf8283 2019-07-24 stsp return err;
7017 0ebf8283 2019-07-24 stsp }
7018 0ebf8283 2019-07-24 stsp
7019 0ebf8283 2019-07-24 stsp static const struct got_error *
7020 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7021 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7022 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7023 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7024 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7025 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7026 0ebf8283 2019-07-24 stsp {
7027 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7028 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7029 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7030 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7031 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7032 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7033 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7034 818c7501 2019-07-11 stsp
7035 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7036 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7037 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7038 a0e95631 2019-07-12 stsp
7039 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7040 818c7501 2019-07-11 stsp
7041 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7042 a0e95631 2019-07-12 stsp if (err)
7043 3e3a69f1 2019-07-25 stsp return err;
7044 a0e95631 2019-07-12 stsp
7045 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7046 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7047 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7048 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7049 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7050 01757395 2019-07-12 stsp /*
7051 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7052 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7053 6c13b005 2021-09-02 stsp *
7054 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7055 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7056 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7057 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7058 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7059 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7060 01757395 2019-07-12 stsp */
7061 01757395 2019-07-12 stsp if (merged_paths) {
7062 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7063 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7064 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7065 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7066 f2a9dc41 2019-12-13 tracey 0);
7067 01757395 2019-07-12 stsp if (err)
7068 01757395 2019-07-12 stsp goto done;
7069 01757395 2019-07-12 stsp }
7070 01757395 2019-07-12 stsp } else {
7071 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7072 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7073 01757395 2019-07-12 stsp if (err)
7074 01757395 2019-07-12 stsp goto done;
7075 01757395 2019-07-12 stsp }
7076 a0e95631 2019-07-12 stsp
7077 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7078 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7079 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7080 a0e95631 2019-07-12 stsp if (err)
7081 a0e95631 2019-07-12 stsp goto done;
7082 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7083 a0e95631 2019-07-12 stsp goto done;
7084 ff0d2220 2019-07-11 stsp }
7085 818c7501 2019-07-11 stsp
7086 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7087 edd02c5e 2019-07-11 stsp if (err)
7088 edd02c5e 2019-07-11 stsp goto done;
7089 edd02c5e 2019-07-11 stsp
7090 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7091 818c7501 2019-07-11 stsp if (err)
7092 818c7501 2019-07-11 stsp goto done;
7093 818c7501 2019-07-11 stsp
7094 5943eee2 2019-08-13 stsp if (new_logmsg) {
7095 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7096 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7097 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7098 5943eee2 2019-08-13 stsp goto done;
7099 5943eee2 2019-08-13 stsp }
7100 5943eee2 2019-08-13 stsp } else {
7101 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7102 5943eee2 2019-08-13 stsp if (err)
7103 5943eee2 2019-08-13 stsp goto done;
7104 5943eee2 2019-08-13 stsp }
7105 0ebf8283 2019-07-24 stsp
7106 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7107 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7108 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7109 50e7a649 2022-07-22 stsp committer ? committer :
7110 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7111 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7112 a0e95631 2019-07-12 stsp if (err)
7113 a0e95631 2019-07-12 stsp goto done;
7114 a0e95631 2019-07-12 stsp
7115 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7116 a0e95631 2019-07-12 stsp if (err)
7117 a0e95631 2019-07-12 stsp goto done;
7118 a0e95631 2019-07-12 stsp
7119 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7120 a0e95631 2019-07-12 stsp if (err)
7121 a0e95631 2019-07-12 stsp goto done;
7122 a0e95631 2019-07-12 stsp
7123 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7124 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7125 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7126 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7127 a0e95631 2019-07-12 stsp err = sync_err;
7128 818c7501 2019-07-11 stsp done:
7129 a0e95631 2019-07-12 stsp free(fileindex_path);
7130 a0e95631 2019-07-12 stsp free(head_commit_id);
7131 a0e95631 2019-07-12 stsp if (head_ref)
7132 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7133 818c7501 2019-07-11 stsp if (err) {
7134 818c7501 2019-07-11 stsp free(*new_commit_id);
7135 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7136 818c7501 2019-07-11 stsp }
7137 818c7501 2019-07-11 stsp return err;
7138 818c7501 2019-07-11 stsp }
7139 818c7501 2019-07-11 stsp
7140 818c7501 2019-07-11 stsp const struct got_error *
7141 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7142 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7143 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7144 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7145 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7146 12383673 2023-02-18 mark struct got_repository *repo)
7147 0ebf8283 2019-07-24 stsp {
7148 0ebf8283 2019-07-24 stsp const struct got_error *err;
7149 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7150 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7151 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7152 0ebf8283 2019-07-24 stsp
7153 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7154 0ebf8283 2019-07-24 stsp if (err)
7155 0ebf8283 2019-07-24 stsp return err;
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7158 0ebf8283 2019-07-24 stsp if (err)
7159 0ebf8283 2019-07-24 stsp goto done;
7160 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7164 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7165 0ebf8283 2019-07-24 stsp goto done;
7166 0ebf8283 2019-07-24 stsp }
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7169 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7170 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7171 0ebf8283 2019-07-24 stsp done:
7172 0ebf8283 2019-07-24 stsp if (commit_ref)
7173 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7174 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7175 0ebf8283 2019-07-24 stsp free(commit_id);
7176 0ebf8283 2019-07-24 stsp return err;
7177 0ebf8283 2019-07-24 stsp }
7178 0ebf8283 2019-07-24 stsp
7179 0ebf8283 2019-07-24 stsp const struct got_error *
7180 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7181 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7182 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7183 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7184 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7185 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7186 0ebf8283 2019-07-24 stsp {
7187 0ebf8283 2019-07-24 stsp const struct got_error *err;
7188 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7189 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7190 0ebf8283 2019-07-24 stsp
7191 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7192 0ebf8283 2019-07-24 stsp if (err)
7193 0ebf8283 2019-07-24 stsp return err;
7194 0ebf8283 2019-07-24 stsp
7195 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7196 0ebf8283 2019-07-24 stsp if (err)
7197 0ebf8283 2019-07-24 stsp goto done;
7198 0ebf8283 2019-07-24 stsp
7199 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7200 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7201 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7202 0ebf8283 2019-07-24 stsp done:
7203 0ebf8283 2019-07-24 stsp if (commit_ref)
7204 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7205 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7206 0ebf8283 2019-07-24 stsp return err;
7207 0ebf8283 2019-07-24 stsp }
7208 0ebf8283 2019-07-24 stsp
7209 0ebf8283 2019-07-24 stsp const struct got_error *
7210 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7211 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7212 818c7501 2019-07-11 stsp {
7213 3e3a69f1 2019-07-25 stsp if (fileindex)
7214 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7215 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7216 69844fba 2019-07-11 stsp }
7217 69844fba 2019-07-11 stsp
7218 69844fba 2019-07-11 stsp static const struct got_error *
7219 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7220 69844fba 2019-07-11 stsp {
7221 69844fba 2019-07-11 stsp const struct got_error *err;
7222 69844fba 2019-07-11 stsp struct got_reference *ref;
7223 69844fba 2019-07-11 stsp
7224 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7225 69844fba 2019-07-11 stsp if (err) {
7226 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7227 69844fba 2019-07-11 stsp return NULL;
7228 69844fba 2019-07-11 stsp return err;
7229 69844fba 2019-07-11 stsp }
7230 69844fba 2019-07-11 stsp
7231 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7232 69844fba 2019-07-11 stsp got_ref_close(ref);
7233 69844fba 2019-07-11 stsp return err;
7234 818c7501 2019-07-11 stsp }
7235 818c7501 2019-07-11 stsp
7236 69844fba 2019-07-11 stsp static const struct got_error *
7237 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7238 69844fba 2019-07-11 stsp {
7239 69844fba 2019-07-11 stsp const struct got_error *err;
7240 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7241 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7242 69844fba 2019-07-11 stsp
7243 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7244 69844fba 2019-07-11 stsp if (err)
7245 69844fba 2019-07-11 stsp goto done;
7246 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7247 69844fba 2019-07-11 stsp if (err)
7248 69844fba 2019-07-11 stsp goto done;
7249 69844fba 2019-07-11 stsp
7250 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7251 69844fba 2019-07-11 stsp if (err)
7252 69844fba 2019-07-11 stsp goto done;
7253 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7254 69844fba 2019-07-11 stsp if (err)
7255 69844fba 2019-07-11 stsp goto done;
7256 69844fba 2019-07-11 stsp
7257 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7258 69844fba 2019-07-11 stsp if (err)
7259 69844fba 2019-07-11 stsp goto done;
7260 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7261 69844fba 2019-07-11 stsp if (err)
7262 69844fba 2019-07-11 stsp goto done;
7263 69844fba 2019-07-11 stsp
7264 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7265 69844fba 2019-07-11 stsp if (err)
7266 69844fba 2019-07-11 stsp goto done;
7267 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7268 69844fba 2019-07-11 stsp if (err)
7269 69844fba 2019-07-11 stsp goto done;
7270 69844fba 2019-07-11 stsp
7271 69844fba 2019-07-11 stsp done:
7272 69844fba 2019-07-11 stsp free(tmp_branch_name);
7273 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7274 69844fba 2019-07-11 stsp free(branch_ref_name);
7275 69844fba 2019-07-11 stsp free(commit_ref_name);
7276 e600f124 2021-03-21 stsp return err;
7277 e600f124 2021-03-21 stsp }
7278 e600f124 2021-03-21 stsp
7279 336075a4 2022-06-25 op static const struct got_error *
7280 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7281 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7282 e600f124 2021-03-21 stsp {
7283 e600f124 2021-03-21 stsp const struct got_error *err;
7284 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7285 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7286 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7287 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7288 e600f124 2021-03-21 stsp char *refname = NULL;
7289 e600f124 2021-03-21 stsp
7290 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7291 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7292 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7293 e600f124 2021-03-21 stsp branch_name += 11;
7294 e600f124 2021-03-21 stsp
7295 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7296 e600f124 2021-03-21 stsp if (err)
7297 e600f124 2021-03-21 stsp return err;
7298 e600f124 2021-03-21 stsp
7299 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7300 e600f124 2021-03-21 stsp new_id_str) == -1) {
7301 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7302 e600f124 2021-03-21 stsp goto done;
7303 e600f124 2021-03-21 stsp }
7304 e600f124 2021-03-21 stsp
7305 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7306 e600f124 2021-03-21 stsp if (err)
7307 e600f124 2021-03-21 stsp goto done;
7308 e600f124 2021-03-21 stsp
7309 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7310 e600f124 2021-03-21 stsp if (err)
7311 e600f124 2021-03-21 stsp goto done;
7312 e600f124 2021-03-21 stsp
7313 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7314 e600f124 2021-03-21 stsp done:
7315 e600f124 2021-03-21 stsp free(new_id_str);
7316 e600f124 2021-03-21 stsp free(refname);
7317 e600f124 2021-03-21 stsp free(old_commit_id);
7318 e600f124 2021-03-21 stsp if (ref)
7319 e600f124 2021-03-21 stsp got_ref_close(ref);
7320 69844fba 2019-07-11 stsp return err;
7321 69844fba 2019-07-11 stsp }
7322 69844fba 2019-07-11 stsp
7323 818c7501 2019-07-11 stsp const struct got_error *
7324 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7325 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7326 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7327 ef85a376 2023-02-01 mark int create_backup)
7328 818c7501 2019-07-11 stsp {
7329 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7330 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7331 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7332 818c7501 2019-07-11 stsp
7333 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7334 818c7501 2019-07-11 stsp if (err)
7335 818c7501 2019-07-11 stsp return err;
7336 e600f124 2021-03-21 stsp
7337 e600f124 2021-03-21 stsp if (create_backup) {
7338 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7339 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7340 e600f124 2021-03-21 stsp if (err)
7341 e600f124 2021-03-21 stsp goto done;
7342 e600f124 2021-03-21 stsp }
7343 818c7501 2019-07-11 stsp
7344 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7345 818c7501 2019-07-11 stsp if (err)
7346 818c7501 2019-07-11 stsp goto done;
7347 818c7501 2019-07-11 stsp
7348 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7349 818c7501 2019-07-11 stsp if (err)
7350 818c7501 2019-07-11 stsp goto done;
7351 818c7501 2019-07-11 stsp
7352 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7353 818c7501 2019-07-11 stsp if (err)
7354 818c7501 2019-07-11 stsp goto done;
7355 818c7501 2019-07-11 stsp
7356 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7357 a615e0e7 2020-12-16 stsp if (err)
7358 a615e0e7 2020-12-16 stsp goto done;
7359 a615e0e7 2020-12-16 stsp
7360 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7361 a615e0e7 2020-12-16 stsp if (err)
7362 a615e0e7 2020-12-16 stsp goto done;
7363 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7364 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7365 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7366 a615e0e7 2020-12-16 stsp err = sync_err;
7367 818c7501 2019-07-11 stsp done:
7368 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7369 a615e0e7 2020-12-16 stsp free(fileindex_path);
7370 818c7501 2019-07-11 stsp free(new_head_commit_id);
7371 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7372 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7373 818c7501 2019-07-11 stsp err = unlockerr;
7374 818c7501 2019-07-11 stsp return err;
7375 818c7501 2019-07-11 stsp }
7376 af179be7 2023-04-14 stsp
7377 af179be7 2023-04-14 stsp static const struct got_error *
7378 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7379 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7380 af179be7 2023-04-14 stsp struct got_repository *repo)
7381 af179be7 2023-04-14 stsp {
7382 af179be7 2023-04-14 stsp const struct got_error *err;
7383 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7384 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7385 af179be7 2023-04-14 stsp
7386 af179be7 2023-04-14 stsp if (id1) {
7387 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7388 af179be7 2023-04-14 stsp if (err)
7389 af179be7 2023-04-14 stsp goto done;
7390 af179be7 2023-04-14 stsp
7391 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7392 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7393 af179be7 2023-04-14 stsp if (err)
7394 af179be7 2023-04-14 stsp goto done;
7395 af179be7 2023-04-14 stsp }
7396 818c7501 2019-07-11 stsp
7397 af179be7 2023-04-14 stsp if (id2) {
7398 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7399 af179be7 2023-04-14 stsp if (err)
7400 af179be7 2023-04-14 stsp goto done;
7401 af179be7 2023-04-14 stsp
7402 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7403 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7404 af179be7 2023-04-14 stsp if (err)
7405 af179be7 2023-04-14 stsp goto done;
7406 af179be7 2023-04-14 stsp }
7407 af179be7 2023-04-14 stsp
7408 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7409 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7410 af179be7 2023-04-14 stsp if (err)
7411 af179be7 2023-04-14 stsp goto done;
7412 af179be7 2023-04-14 stsp done:
7413 af179be7 2023-04-14 stsp if (commit1)
7414 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7415 af179be7 2023-04-14 stsp if (commit2)
7416 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7417 af179be7 2023-04-14 stsp if (tree1)
7418 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7419 af179be7 2023-04-14 stsp if (tree2)
7420 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7421 af179be7 2023-04-14 stsp return err;
7422 af179be7 2023-04-14 stsp }
7423 af179be7 2023-04-14 stsp
7424 af179be7 2023-04-14 stsp static const struct got_error *
7425 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7426 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7427 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7428 af179be7 2023-04-14 stsp {
7429 af179be7 2023-04-14 stsp const struct got_error *err;
7430 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7431 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7432 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7433 af179be7 2023-04-14 stsp
7434 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7435 af179be7 2023-04-14 stsp
7436 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7437 af179be7 2023-04-14 stsp if (err)
7438 af179be7 2023-04-14 stsp goto done;
7439 af179be7 2023-04-14 stsp
7440 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7441 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7442 af179be7 2023-04-14 stsp
7443 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7444 af179be7 2023-04-14 stsp continue;
7445 af179be7 2023-04-14 stsp
7446 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7447 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7448 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7449 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7450 af179be7 2023-04-14 stsp goto done;
7451 af179be7 2023-04-14 stsp }
7452 af179be7 2023-04-14 stsp } else {
7453 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7454 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7455 af179be7 2023-04-14 stsp goto done;
7456 af179be7 2023-04-14 stsp }
7457 af179be7 2023-04-14 stsp
7458 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7459 af179be7 2023-04-14 stsp path_prefix, abspath);
7460 af179be7 2023-04-14 stsp if (err)
7461 af179be7 2023-04-14 stsp goto done;
7462 af179be7 2023-04-14 stsp free(abspath);
7463 af179be7 2023-04-14 stsp abspath = NULL;
7464 af179be7 2023-04-14 stsp }
7465 af179be7 2023-04-14 stsp
7466 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7467 af179be7 2023-04-14 stsp if (err)
7468 af179be7 2023-04-14 stsp goto done;
7469 af179be7 2023-04-14 stsp wt_path = NULL;
7470 af179be7 2023-04-14 stsp }
7471 af179be7 2023-04-14 stsp
7472 af179be7 2023-04-14 stsp done:
7473 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7474 af179be7 2023-04-14 stsp free(abspath);
7475 af179be7 2023-04-14 stsp free(wt_path);
7476 af179be7 2023-04-14 stsp return err;
7477 af179be7 2023-04-14 stsp }
7478 af179be7 2023-04-14 stsp
7479 af179be7 2023-04-14 stsp static const struct got_error *
7480 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7481 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7482 af179be7 2023-04-14 stsp struct got_repository *repo)
7483 af179be7 2023-04-14 stsp {
7484 af179be7 2023-04-14 stsp const struct got_error *err;
7485 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7486 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7487 af179be7 2023-04-14 stsp
7488 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7489 af179be7 2023-04-14 stsp if (err)
7490 af179be7 2023-04-14 stsp goto done;
7491 af179be7 2023-04-14 stsp
7492 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7493 af179be7 2023-04-14 stsp
7494 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7495 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7496 af179be7 2023-04-14 stsp if (err)
7497 af179be7 2023-04-14 stsp goto done;
7498 af179be7 2023-04-14 stsp done:
7499 af179be7 2023-04-14 stsp if (commit)
7500 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7501 af179be7 2023-04-14 stsp return err;
7502 af179be7 2023-04-14 stsp }
7503 af179be7 2023-04-14 stsp
7504 818c7501 2019-07-11 stsp const struct got_error *
7505 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7506 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7507 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7508 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7509 818c7501 2019-07-11 stsp {
7510 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7511 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7512 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7513 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7514 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7515 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7516 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7517 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7518 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7519 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7520 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7521 818c7501 2019-07-11 stsp
7522 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7523 af179be7 2023-04-14 stsp
7524 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7525 818c7501 2019-07-11 stsp if (err)
7526 818c7501 2019-07-11 stsp return err;
7527 af179be7 2023-04-14 stsp
7528 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7529 af179be7 2023-04-14 stsp if (err)
7530 af179be7 2023-04-14 stsp goto done;
7531 af179be7 2023-04-14 stsp
7532 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7533 af179be7 2023-04-14 stsp if (err)
7534 af179be7 2023-04-14 stsp goto done;
7535 af179be7 2023-04-14 stsp
7536 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7537 af179be7 2023-04-14 stsp if (err)
7538 af179be7 2023-04-14 stsp goto done;
7539 a44927cc 2022-04-07 stsp
7540 af179be7 2023-04-14 stsp /*
7541 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7542 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7543 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7544 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7545 af179be7 2023-04-14 stsp */
7546 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7547 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7548 af179be7 2023-04-14 stsp if (err)
7549 af179be7 2023-04-14 stsp goto done;
7550 af179be7 2023-04-14 stsp
7551 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7552 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7553 818c7501 2019-07-11 stsp if (err)
7554 818c7501 2019-07-11 stsp goto done;
7555 818c7501 2019-07-11 stsp
7556 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7557 818c7501 2019-07-11 stsp if (err)
7558 818c7501 2019-07-11 stsp goto done;
7559 818c7501 2019-07-11 stsp
7560 818c7501 2019-07-11 stsp /*
7561 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7562 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7563 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7564 818c7501 2019-07-11 stsp */
7565 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7566 818c7501 2019-07-11 stsp if (err)
7567 818c7501 2019-07-11 stsp goto done;
7568 818c7501 2019-07-11 stsp
7569 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7570 1b093d84 2023-04-12 stsp if (err)
7571 1b093d84 2023-04-12 stsp goto done;
7572 1b093d84 2023-04-12 stsp
7573 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7574 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7575 818c7501 2019-07-11 stsp if (err)
7576 818c7501 2019-07-11 stsp goto done;
7577 818c7501 2019-07-11 stsp
7578 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7579 a44927cc 2022-04-07 stsp worktree->path_prefix);
7580 ca355955 2019-07-12 stsp if (err)
7581 ca355955 2019-07-12 stsp goto done;
7582 ca355955 2019-07-12 stsp
7583 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7584 ca355955 2019-07-12 stsp if (err)
7585 ca355955 2019-07-12 stsp goto done;
7586 ca355955 2019-07-12 stsp
7587 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7588 818c7501 2019-07-11 stsp if (err)
7589 818c7501 2019-07-11 stsp goto done;
7590 818c7501 2019-07-11 stsp
7591 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7592 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7593 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7594 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7595 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7596 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7597 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7598 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7599 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7600 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7601 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7602 55bd499d 2019-07-12 stsp if (err)
7603 1f1abb7e 2019-08-08 stsp goto sync;
7604 55bd499d 2019-07-12 stsp
7605 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7606 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7607 ca355955 2019-07-12 stsp sync:
7608 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7609 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7610 ca355955 2019-07-12 stsp err = sync_err;
7611 818c7501 2019-07-11 stsp done:
7612 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7613 818c7501 2019-07-11 stsp got_ref_close(resolved);
7614 a3a2faf2 2019-07-12 stsp free(tree_id);
7615 818c7501 2019-07-11 stsp free(commit_id);
7616 af179be7 2023-04-14 stsp free(merged_commit_id);
7617 a44927cc 2022-04-07 stsp if (commit)
7618 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7619 0ebf8283 2019-07-24 stsp if (fileindex)
7620 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7621 0ebf8283 2019-07-24 stsp free(fileindex_path);
7622 af179be7 2023-04-14 stsp free(commit_ref_name);
7623 af179be7 2023-04-14 stsp if (commit_ref)
7624 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7625 0ebf8283 2019-07-24 stsp
7626 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7627 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7628 0ebf8283 2019-07-24 stsp err = unlockerr;
7629 0ebf8283 2019-07-24 stsp return err;
7630 0ebf8283 2019-07-24 stsp }
7631 0ebf8283 2019-07-24 stsp
7632 0ebf8283 2019-07-24 stsp const struct got_error *
7633 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7634 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7635 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7636 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7637 0ebf8283 2019-07-24 stsp {
7638 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7639 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7640 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7641 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7642 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7643 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7644 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7645 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7646 0ebf8283 2019-07-24 stsp
7647 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7648 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7649 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7650 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7651 0ebf8283 2019-07-24 stsp
7652 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7653 0ebf8283 2019-07-24 stsp if (err)
7654 0ebf8283 2019-07-24 stsp return err;
7655 0ebf8283 2019-07-24 stsp
7656 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7657 0ebf8283 2019-07-24 stsp if (err)
7658 0ebf8283 2019-07-24 stsp goto done;
7659 0ebf8283 2019-07-24 stsp
7660 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7661 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7662 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7663 0ebf8283 2019-07-24 stsp &ok_arg);
7664 0ebf8283 2019-07-24 stsp if (err)
7665 0ebf8283 2019-07-24 stsp goto done;
7666 0ebf8283 2019-07-24 stsp
7667 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7668 0ebf8283 2019-07-24 stsp if (err)
7669 0ebf8283 2019-07-24 stsp goto done;
7670 0ebf8283 2019-07-24 stsp
7671 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7672 0ebf8283 2019-07-24 stsp if (err)
7673 0ebf8283 2019-07-24 stsp goto done;
7674 0ebf8283 2019-07-24 stsp
7675 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7676 0ebf8283 2019-07-24 stsp worktree);
7677 0ebf8283 2019-07-24 stsp if (err)
7678 0ebf8283 2019-07-24 stsp goto done;
7679 0ebf8283 2019-07-24 stsp
7680 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7681 0ebf8283 2019-07-24 stsp 0);
7682 0ebf8283 2019-07-24 stsp if (err)
7683 0ebf8283 2019-07-24 stsp goto done;
7684 0ebf8283 2019-07-24 stsp
7685 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7686 0ebf8283 2019-07-24 stsp if (err)
7687 0ebf8283 2019-07-24 stsp goto done;
7688 0ebf8283 2019-07-24 stsp
7689 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7690 0ebf8283 2019-07-24 stsp if (err)
7691 0ebf8283 2019-07-24 stsp goto done;
7692 0ebf8283 2019-07-24 stsp
7693 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7694 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7695 0ebf8283 2019-07-24 stsp if (err)
7696 0ebf8283 2019-07-24 stsp goto done;
7697 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7698 0ebf8283 2019-07-24 stsp if (err)
7699 0ebf8283 2019-07-24 stsp goto done;
7700 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7701 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7702 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7703 0ebf8283 2019-07-24 stsp goto done;
7704 0ebf8283 2019-07-24 stsp }
7705 0ebf8283 2019-07-24 stsp
7706 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7707 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7708 0ebf8283 2019-07-24 stsp if (err)
7709 0ebf8283 2019-07-24 stsp goto done;
7710 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7711 0ebf8283 2019-07-24 stsp if (err)
7712 0ebf8283 2019-07-24 stsp goto done;
7713 0ebf8283 2019-07-24 stsp
7714 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7715 0ebf8283 2019-07-24 stsp if (err)
7716 0ebf8283 2019-07-24 stsp goto done;
7717 0ebf8283 2019-07-24 stsp done:
7718 0ebf8283 2019-07-24 stsp free(fileindex_path);
7719 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7720 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7721 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7722 0ebf8283 2019-07-24 stsp if (wt_branch)
7723 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7724 0ebf8283 2019-07-24 stsp if (err) {
7725 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7726 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7727 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7728 0ebf8283 2019-07-24 stsp }
7729 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7730 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7731 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7732 0ebf8283 2019-07-24 stsp }
7733 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7734 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7735 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7736 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7737 3e3a69f1 2019-07-25 stsp }
7738 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7739 0ebf8283 2019-07-24 stsp }
7740 0ebf8283 2019-07-24 stsp return err;
7741 0ebf8283 2019-07-24 stsp }
7742 0ebf8283 2019-07-24 stsp
7743 0ebf8283 2019-07-24 stsp const struct got_error *
7744 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7745 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7746 0ebf8283 2019-07-24 stsp {
7747 3e3a69f1 2019-07-25 stsp if (fileindex)
7748 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7749 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7750 0ebf8283 2019-07-24 stsp }
7751 0ebf8283 2019-07-24 stsp
7752 0ebf8283 2019-07-24 stsp const struct got_error *
7753 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7754 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7755 0ebf8283 2019-07-24 stsp {
7756 0ebf8283 2019-07-24 stsp const struct got_error *err;
7757 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7758 0ebf8283 2019-07-24 stsp
7759 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7760 0ebf8283 2019-07-24 stsp if (err)
7761 0ebf8283 2019-07-24 stsp return err;
7762 0ebf8283 2019-07-24 stsp
7763 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7764 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7765 0ebf8283 2019-07-24 stsp return NULL;
7766 0ebf8283 2019-07-24 stsp }
7767 0ebf8283 2019-07-24 stsp
7768 0ebf8283 2019-07-24 stsp const struct got_error *
7769 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7770 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7771 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7772 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7773 0ebf8283 2019-07-24 stsp {
7774 0ebf8283 2019-07-24 stsp const struct got_error *err;
7775 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7776 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7777 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7778 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7779 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7780 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7781 0ebf8283 2019-07-24 stsp
7782 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7783 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7784 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7785 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7786 0ebf8283 2019-07-24 stsp
7787 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7788 3e3a69f1 2019-07-25 stsp if (err)
7789 3e3a69f1 2019-07-25 stsp return err;
7790 3e3a69f1 2019-07-25 stsp
7791 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7792 3e3a69f1 2019-07-25 stsp if (err)
7793 f032f1f7 2019-08-04 stsp goto done;
7794 f032f1f7 2019-08-04 stsp
7795 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7796 f032f1f7 2019-08-04 stsp &have_staged_files);
7797 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7798 f032f1f7 2019-08-04 stsp goto done;
7799 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7800 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7801 3e3a69f1 2019-07-25 stsp goto done;
7802 f032f1f7 2019-08-04 stsp }
7803 3e3a69f1 2019-07-25 stsp
7804 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7805 0ebf8283 2019-07-24 stsp if (err)
7806 f032f1f7 2019-08-04 stsp goto done;
7807 0ebf8283 2019-07-24 stsp
7808 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7809 0ebf8283 2019-07-24 stsp if (err)
7810 0ebf8283 2019-07-24 stsp goto done;
7811 0ebf8283 2019-07-24 stsp
7812 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7813 0ebf8283 2019-07-24 stsp if (err)
7814 0ebf8283 2019-07-24 stsp goto done;
7815 0ebf8283 2019-07-24 stsp
7816 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7817 0ebf8283 2019-07-24 stsp worktree);
7818 0ebf8283 2019-07-24 stsp if (err)
7819 0ebf8283 2019-07-24 stsp goto done;
7820 0ebf8283 2019-07-24 stsp
7821 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7822 0ebf8283 2019-07-24 stsp if (err)
7823 0ebf8283 2019-07-24 stsp goto done;
7824 0ebf8283 2019-07-24 stsp
7825 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7826 0ebf8283 2019-07-24 stsp if (err)
7827 0ebf8283 2019-07-24 stsp goto done;
7828 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7829 0ebf8283 2019-07-24 stsp if (err)
7830 0ebf8283 2019-07-24 stsp goto done;
7831 0ebf8283 2019-07-24 stsp
7832 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7833 0ebf8283 2019-07-24 stsp if (err)
7834 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7836 0ebf8283 2019-07-24 stsp if (err)
7837 0ebf8283 2019-07-24 stsp goto done;
7838 0ebf8283 2019-07-24 stsp
7839 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7840 0ebf8283 2019-07-24 stsp if (err)
7841 0ebf8283 2019-07-24 stsp goto done;
7842 0ebf8283 2019-07-24 stsp done:
7843 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7844 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7845 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7846 0ebf8283 2019-07-24 stsp if (commit_ref)
7847 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7848 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7849 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7850 0ebf8283 2019-07-24 stsp if (err) {
7851 0ebf8283 2019-07-24 stsp free(*commit_id);
7852 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7853 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7854 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7855 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7856 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7857 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7858 0ebf8283 2019-07-24 stsp }
7859 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7860 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7861 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7862 3e3a69f1 2019-07-25 stsp }
7863 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7864 0ebf8283 2019-07-24 stsp }
7865 0ebf8283 2019-07-24 stsp return err;
7866 0ebf8283 2019-07-24 stsp }
7867 0ebf8283 2019-07-24 stsp
7868 0ebf8283 2019-07-24 stsp static const struct got_error *
7869 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7870 0ebf8283 2019-07-24 stsp {
7871 0ebf8283 2019-07-24 stsp const struct got_error *err;
7872 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7873 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7874 0ebf8283 2019-07-24 stsp
7875 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7876 0ebf8283 2019-07-24 stsp if (err)
7877 0ebf8283 2019-07-24 stsp goto done;
7878 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7879 0ebf8283 2019-07-24 stsp if (err)
7880 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp
7882 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7883 0ebf8283 2019-07-24 stsp worktree);
7884 0ebf8283 2019-07-24 stsp if (err)
7885 0ebf8283 2019-07-24 stsp goto done;
7886 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7887 0ebf8283 2019-07-24 stsp if (err)
7888 0ebf8283 2019-07-24 stsp goto done;
7889 0ebf8283 2019-07-24 stsp
7890 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7891 0ebf8283 2019-07-24 stsp if (err)
7892 0ebf8283 2019-07-24 stsp goto done;
7893 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7894 0ebf8283 2019-07-24 stsp if (err)
7895 0ebf8283 2019-07-24 stsp goto done;
7896 0ebf8283 2019-07-24 stsp
7897 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7898 0ebf8283 2019-07-24 stsp if (err)
7899 0ebf8283 2019-07-24 stsp goto done;
7900 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7901 0ebf8283 2019-07-24 stsp if (err)
7902 0ebf8283 2019-07-24 stsp goto done;
7903 0ebf8283 2019-07-24 stsp done:
7904 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7905 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7906 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7907 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7908 0ebf8283 2019-07-24 stsp return err;
7909 0ebf8283 2019-07-24 stsp }
7910 0ebf8283 2019-07-24 stsp
7911 0ebf8283 2019-07-24 stsp const struct got_error *
7912 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7913 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7914 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7915 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7916 0ebf8283 2019-07-24 stsp {
7917 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7918 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7919 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7920 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7921 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7922 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7923 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7924 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7925 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7926 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7927 0ebf8283 2019-07-24 stsp
7928 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7929 af179be7 2023-04-14 stsp
7930 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7931 0ebf8283 2019-07-24 stsp if (err)
7932 0ebf8283 2019-07-24 stsp return err;
7933 a44927cc 2022-04-07 stsp
7934 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7935 af179be7 2023-04-14 stsp if (err)
7936 af179be7 2023-04-14 stsp goto done;
7937 af179be7 2023-04-14 stsp
7938 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7939 af179be7 2023-04-14 stsp if (err) {
7940 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
7941 af179be7 2023-04-14 stsp goto done;
7942 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
7943 af179be7 2023-04-14 stsp commit_ref = NULL;
7944 af179be7 2023-04-14 stsp }
7945 af179be7 2023-04-14 stsp
7946 af179be7 2023-04-14 stsp if (commit_ref) {
7947 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7948 af179be7 2023-04-14 stsp if (err)
7949 af179be7 2023-04-14 stsp goto done;
7950 af179be7 2023-04-14 stsp
7951 af179be7 2023-04-14 stsp /*
7952 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7953 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7954 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
7955 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
7956 af179be7 2023-04-14 stsp */
7957 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7958 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7959 af179be7 2023-04-14 stsp if (err)
7960 af179be7 2023-04-14 stsp goto done;
7961 af179be7 2023-04-14 stsp }
7962 af179be7 2023-04-14 stsp
7963 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7964 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7965 0ebf8283 2019-07-24 stsp if (err)
7966 0ebf8283 2019-07-24 stsp goto done;
7967 0ebf8283 2019-07-24 stsp
7968 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7969 0ebf8283 2019-07-24 stsp if (err)
7970 0ebf8283 2019-07-24 stsp goto done;
7971 0ebf8283 2019-07-24 stsp
7972 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7973 0ebf8283 2019-07-24 stsp if (err)
7974 0ebf8283 2019-07-24 stsp goto done;
7975 0ebf8283 2019-07-24 stsp
7976 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7977 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7978 1b093d84 2023-04-12 stsp if (err)
7979 1b093d84 2023-04-12 stsp goto done;
7980 1b093d84 2023-04-12 stsp
7981 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7982 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7983 0ebf8283 2019-07-24 stsp if (err)
7984 0ebf8283 2019-07-24 stsp goto done;
7985 0ebf8283 2019-07-24 stsp
7986 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7987 0ebf8283 2019-07-24 stsp if (err)
7988 0ebf8283 2019-07-24 stsp goto done;
7989 0ebf8283 2019-07-24 stsp
7990 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7991 0ebf8283 2019-07-24 stsp if (err)
7992 0ebf8283 2019-07-24 stsp goto done;
7993 0ebf8283 2019-07-24 stsp
7994 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7995 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7996 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7997 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7998 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7999 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8000 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8001 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8002 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8003 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8004 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8005 0ebf8283 2019-07-24 stsp if (err)
8006 1f1abb7e 2019-08-08 stsp goto sync;
8007 0ebf8283 2019-07-24 stsp
8008 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8009 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8010 0ebf8283 2019-07-24 stsp sync:
8011 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8012 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8013 0ebf8283 2019-07-24 stsp err = sync_err;
8014 0ebf8283 2019-07-24 stsp done:
8015 af179be7 2023-04-14 stsp if (resolved)
8016 af179be7 2023-04-14 stsp got_ref_close(resolved);
8017 af179be7 2023-04-14 stsp if (commit_ref)
8018 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8019 af179be7 2023-04-14 stsp free(merged_commit_id);
8020 0ebf8283 2019-07-24 stsp free(tree_id);
8021 818c7501 2019-07-11 stsp free(fileindex_path);
8022 af179be7 2023-04-14 stsp free(commit_ref_name);
8023 818c7501 2019-07-11 stsp
8024 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8025 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8026 818c7501 2019-07-11 stsp err = unlockerr;
8027 818c7501 2019-07-11 stsp return err;
8028 818c7501 2019-07-11 stsp }
8029 0ebf8283 2019-07-24 stsp
8030 0ebf8283 2019-07-24 stsp const struct got_error *
8031 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8032 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8033 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8034 0ebf8283 2019-07-24 stsp {
8035 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8036 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8037 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8038 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8039 0ebf8283 2019-07-24 stsp
8040 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8041 0ebf8283 2019-07-24 stsp if (err)
8042 0ebf8283 2019-07-24 stsp return err;
8043 0ebf8283 2019-07-24 stsp
8044 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8045 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8046 e600f124 2021-03-21 stsp if (err)
8047 e600f124 2021-03-21 stsp goto done;
8048 e600f124 2021-03-21 stsp
8049 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8050 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8051 0ebf8283 2019-07-24 stsp if (err)
8052 0ebf8283 2019-07-24 stsp goto done;
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8055 0ebf8283 2019-07-24 stsp if (err)
8056 0ebf8283 2019-07-24 stsp goto done;
8057 0ebf8283 2019-07-24 stsp
8058 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8059 0ebf8283 2019-07-24 stsp if (err)
8060 0ebf8283 2019-07-24 stsp goto done;
8061 0ebf8283 2019-07-24 stsp
8062 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8063 0ebf8283 2019-07-24 stsp if (err)
8064 0ebf8283 2019-07-24 stsp goto done;
8065 0ebf8283 2019-07-24 stsp
8066 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8067 a615e0e7 2020-12-16 stsp if (err)
8068 a615e0e7 2020-12-16 stsp goto done;
8069 a615e0e7 2020-12-16 stsp
8070 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8071 a615e0e7 2020-12-16 stsp if (err)
8072 a615e0e7 2020-12-16 stsp goto done;
8073 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8074 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8075 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8076 a615e0e7 2020-12-16 stsp err = sync_err;
8077 0ebf8283 2019-07-24 stsp done:
8078 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8079 a615e0e7 2020-12-16 stsp free(fileindex_path);
8080 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8081 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8082 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8083 0ebf8283 2019-07-24 stsp err = unlockerr;
8084 0ebf8283 2019-07-24 stsp return err;
8085 0ebf8283 2019-07-24 stsp }
8086 0ebf8283 2019-07-24 stsp
8087 0ebf8283 2019-07-24 stsp const struct got_error *
8088 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8089 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8090 0ebf8283 2019-07-24 stsp {
8091 0ebf8283 2019-07-24 stsp const struct got_error *err;
8092 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8093 0ebf8283 2019-07-24 stsp
8094 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8095 0ebf8283 2019-07-24 stsp if (err)
8096 0ebf8283 2019-07-24 stsp return err;
8097 0ebf8283 2019-07-24 stsp
8098 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8099 0ebf8283 2019-07-24 stsp if (err)
8100 0ebf8283 2019-07-24 stsp goto done;
8101 0ebf8283 2019-07-24 stsp
8102 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8103 0ebf8283 2019-07-24 stsp done:
8104 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8105 2822a352 2019-10-15 stsp return err;
8106 2822a352 2019-10-15 stsp }
8107 2822a352 2019-10-15 stsp
8108 2822a352 2019-10-15 stsp const struct got_error *
8109 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8110 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8111 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8112 2822a352 2019-10-15 stsp struct got_repository *repo)
8113 2822a352 2019-10-15 stsp {
8114 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8115 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8116 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8117 2822a352 2019-10-15 stsp
8118 2822a352 2019-10-15 stsp *fileindex = NULL;
8119 2822a352 2019-10-15 stsp *branch_ref = NULL;
8120 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8121 2822a352 2019-10-15 stsp
8122 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8123 2822a352 2019-10-15 stsp if (err)
8124 2822a352 2019-10-15 stsp return err;
8125 2822a352 2019-10-15 stsp
8126 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8127 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8128 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8129 2822a352 2019-10-15 stsp "update -b or different branch name required");
8130 2822a352 2019-10-15 stsp goto done;
8131 2822a352 2019-10-15 stsp }
8132 2822a352 2019-10-15 stsp
8133 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8134 2822a352 2019-10-15 stsp if (err)
8135 2822a352 2019-10-15 stsp goto done;
8136 2822a352 2019-10-15 stsp
8137 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8138 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8139 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8140 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8141 2822a352 2019-10-15 stsp &ok_arg);
8142 2822a352 2019-10-15 stsp if (err)
8143 2822a352 2019-10-15 stsp goto done;
8144 2822a352 2019-10-15 stsp
8145 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8146 2822a352 2019-10-15 stsp if (err)
8147 2822a352 2019-10-15 stsp goto done;
8148 2822a352 2019-10-15 stsp
8149 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8150 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8151 2822a352 2019-10-15 stsp done:
8152 2822a352 2019-10-15 stsp if (err) {
8153 2822a352 2019-10-15 stsp if (*branch_ref) {
8154 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8155 2822a352 2019-10-15 stsp *branch_ref = NULL;
8156 2822a352 2019-10-15 stsp }
8157 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8158 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8159 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8160 2822a352 2019-10-15 stsp }
8161 2822a352 2019-10-15 stsp if (*fileindex) {
8162 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8163 2822a352 2019-10-15 stsp *fileindex = NULL;
8164 2822a352 2019-10-15 stsp }
8165 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8166 2822a352 2019-10-15 stsp }
8167 0cb83759 2019-08-03 stsp return err;
8168 0cb83759 2019-08-03 stsp }
8169 0cb83759 2019-08-03 stsp
8170 2822a352 2019-10-15 stsp const struct got_error *
8171 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8172 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8173 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8174 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8175 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8176 2822a352 2019-10-15 stsp {
8177 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8178 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8179 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8180 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8181 2822a352 2019-10-15 stsp
8182 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8183 2822a352 2019-10-15 stsp if (err)
8184 2822a352 2019-10-15 stsp goto done;
8185 2822a352 2019-10-15 stsp
8186 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8187 2822a352 2019-10-15 stsp if (err)
8188 2822a352 2019-10-15 stsp goto done;
8189 2822a352 2019-10-15 stsp
8190 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8191 a44927cc 2022-04-07 stsp if (err)
8192 a44927cc 2022-04-07 stsp goto done;
8193 a44927cc 2022-04-07 stsp
8194 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8195 2822a352 2019-10-15 stsp worktree->path_prefix);
8196 2822a352 2019-10-15 stsp if (err)
8197 2822a352 2019-10-15 stsp goto done;
8198 2822a352 2019-10-15 stsp
8199 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8200 2822a352 2019-10-15 stsp if (err)
8201 2822a352 2019-10-15 stsp goto done;
8202 2822a352 2019-10-15 stsp
8203 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8204 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8205 2822a352 2019-10-15 stsp if (err)
8206 2822a352 2019-10-15 stsp goto sync;
8207 2822a352 2019-10-15 stsp
8208 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8209 2822a352 2019-10-15 stsp if (err)
8210 2822a352 2019-10-15 stsp goto sync;
8211 2822a352 2019-10-15 stsp
8212 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8213 6e210706 2021-01-22 stsp if (err)
8214 6e210706 2021-01-22 stsp goto sync;
8215 6e210706 2021-01-22 stsp
8216 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8217 2822a352 2019-10-15 stsp sync:
8218 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8219 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8220 2822a352 2019-10-15 stsp err = sync_err;
8221 2822a352 2019-10-15 stsp
8222 2822a352 2019-10-15 stsp done:
8223 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8224 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8225 2822a352 2019-10-15 stsp err = unlockerr;
8226 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8227 2822a352 2019-10-15 stsp
8228 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8229 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8230 2822a352 2019-10-15 stsp err = unlockerr;
8231 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8232 2822a352 2019-10-15 stsp
8233 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8234 2822a352 2019-10-15 stsp free(fileindex_path);
8235 2822a352 2019-10-15 stsp free(tree_id);
8236 a44927cc 2022-04-07 stsp if (commit)
8237 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8238 2822a352 2019-10-15 stsp
8239 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8240 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8241 2822a352 2019-10-15 stsp err = unlockerr;
8242 2822a352 2019-10-15 stsp return err;
8243 2822a352 2019-10-15 stsp }
8244 2822a352 2019-10-15 stsp
8245 2822a352 2019-10-15 stsp const struct got_error *
8246 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8247 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8248 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8249 2822a352 2019-10-15 stsp {
8250 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8251 8b692cd0 2019-10-21 stsp
8252 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8253 8b692cd0 2019-10-21 stsp
8254 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8255 8b692cd0 2019-10-21 stsp
8256 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8257 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8258 8b692cd0 2019-10-21 stsp err = unlockerr;
8259 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8260 8b692cd0 2019-10-21 stsp
8261 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8262 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8263 8b692cd0 2019-10-21 stsp err = unlockerr;
8264 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8265 f259c4c1 2021-09-24 stsp
8266 f259c4c1 2021-09-24 stsp return err;
8267 f259c4c1 2021-09-24 stsp }
8268 f259c4c1 2021-09-24 stsp
8269 f259c4c1 2021-09-24 stsp const struct got_error *
8270 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8271 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8272 f259c4c1 2021-09-24 stsp {
8273 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8274 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8275 f259c4c1 2021-09-24 stsp
8276 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8277 f259c4c1 2021-09-24 stsp if (err)
8278 f259c4c1 2021-09-24 stsp goto done;
8279 8b692cd0 2019-10-21 stsp
8280 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8281 f259c4c1 2021-09-24 stsp
8282 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8283 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8284 f259c4c1 2021-09-24 stsp err = sync_err;
8285 f259c4c1 2021-09-24 stsp done:
8286 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8287 f259c4c1 2021-09-24 stsp free(fileindex_path);
8288 8b692cd0 2019-10-21 stsp return err;
8289 2822a352 2019-10-15 stsp }
8290 2822a352 2019-10-15 stsp
8291 f259c4c1 2021-09-24 stsp static const struct got_error *
8292 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8293 f259c4c1 2021-09-24 stsp {
8294 f259c4c1 2021-09-24 stsp const struct got_error *err;
8295 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8296 f259c4c1 2021-09-24 stsp
8297 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8298 f259c4c1 2021-09-24 stsp if (err)
8299 f259c4c1 2021-09-24 stsp goto done;
8300 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8301 f259c4c1 2021-09-24 stsp if (err)
8302 f259c4c1 2021-09-24 stsp goto done;
8303 f259c4c1 2021-09-24 stsp
8304 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8305 f259c4c1 2021-09-24 stsp if (err)
8306 f259c4c1 2021-09-24 stsp goto done;
8307 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8308 f259c4c1 2021-09-24 stsp if (err)
8309 f259c4c1 2021-09-24 stsp goto done;
8310 f259c4c1 2021-09-24 stsp
8311 f259c4c1 2021-09-24 stsp done:
8312 f259c4c1 2021-09-24 stsp free(branch_refname);
8313 f259c4c1 2021-09-24 stsp free(commit_refname);
8314 f259c4c1 2021-09-24 stsp return err;
8315 f259c4c1 2021-09-24 stsp }
8316 f259c4c1 2021-09-24 stsp
8317 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8318 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8319 f259c4c1 2021-09-24 stsp const char *branch_name;
8320 f259c4c1 2021-09-24 stsp };
8321 f259c4c1 2021-09-24 stsp
8322 f259c4c1 2021-09-24 stsp static const struct got_error *
8323 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8324 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8325 f259c4c1 2021-09-24 stsp {
8326 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8327 f259c4c1 2021-09-24 stsp
8328 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8329 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8330 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8331 f259c4c1 2021-09-24 stsp
8332 f259c4c1 2021-09-24 stsp return NULL;
8333 f259c4c1 2021-09-24 stsp }
8334 f259c4c1 2021-09-24 stsp
8335 f259c4c1 2021-09-24 stsp
8336 f259c4c1 2021-09-24 stsp const struct got_error *
8337 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8338 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8339 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8340 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8341 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8342 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8343 f259c4c1 2021-09-24 stsp {
8344 f259c4c1 2021-09-24 stsp const struct got_error *err;
8345 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8346 f259c4c1 2021-09-24 stsp
8347 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8348 f259c4c1 2021-09-24 stsp if (err)
8349 f259c4c1 2021-09-24 stsp goto done;
8350 f259c4c1 2021-09-24 stsp
8351 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8352 f259c4c1 2021-09-24 stsp worktree);
8353 f259c4c1 2021-09-24 stsp if (err)
8354 f259c4c1 2021-09-24 stsp goto done;
8355 f259c4c1 2021-09-24 stsp
8356 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8357 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8358 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8359 f259c4c1 2021-09-24 stsp done:
8360 f259c4c1 2021-09-24 stsp free(fileindex_path);
8361 f259c4c1 2021-09-24 stsp return err;
8362 f259c4c1 2021-09-24 stsp }
8363 f259c4c1 2021-09-24 stsp
8364 f259c4c1 2021-09-24 stsp const struct got_error *
8365 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8366 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8367 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8368 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8369 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8370 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8371 0ff8d236 2021-09-28 stsp
8372 f259c4c1 2021-09-24 stsp {
8373 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8374 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8375 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8376 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8377 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8378 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8379 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8380 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8381 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8382 f259c4c1 2021-09-24 stsp
8383 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8384 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8385 f259c4c1 2021-09-24 stsp
8386 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8387 f259c4c1 2021-09-24 stsp
8388 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8389 f259c4c1 2021-09-24 stsp if (err)
8390 f259c4c1 2021-09-24 stsp goto done;
8391 f259c4c1 2021-09-24 stsp
8392 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8393 f259c4c1 2021-09-24 stsp if (err)
8394 f259c4c1 2021-09-24 stsp goto done;
8395 f259c4c1 2021-09-24 stsp
8396 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8397 f259c4c1 2021-09-24 stsp if (err)
8398 f259c4c1 2021-09-24 stsp goto done;
8399 f259c4c1 2021-09-24 stsp
8400 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8401 f259c4c1 2021-09-24 stsp &have_staged_files);
8402 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8403 f259c4c1 2021-09-24 stsp goto done;
8404 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8405 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8406 f259c4c1 2021-09-24 stsp goto done;
8407 f259c4c1 2021-09-24 stsp }
8408 f259c4c1 2021-09-24 stsp
8409 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8410 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8411 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8412 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8413 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8414 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8415 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8416 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8417 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8418 f259c4c1 2021-09-24 stsp if (err)
8419 f259c4c1 2021-09-24 stsp goto done;
8420 f259c4c1 2021-09-24 stsp
8421 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8422 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8423 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8424 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8425 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8426 f259c4c1 2021-09-24 stsp if (err)
8427 f259c4c1 2021-09-24 stsp goto done;
8428 f259c4c1 2021-09-24 stsp
8429 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8430 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8431 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8432 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8433 f259c4c1 2021-09-24 stsp err = sync_err;
8434 f259c4c1 2021-09-24 stsp done:
8435 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8436 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8437 d8bacb93 2023-01-10 mark
8438 f259c4c1 2021-09-24 stsp free_commitable(ct);
8439 f259c4c1 2021-09-24 stsp }
8440 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8441 f259c4c1 2021-09-24 stsp free(fileindex_path);
8442 f259c4c1 2021-09-24 stsp return err;
8443 f259c4c1 2021-09-24 stsp }
8444 f259c4c1 2021-09-24 stsp
8445 f259c4c1 2021-09-24 stsp const struct got_error *
8446 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8447 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8448 f259c4c1 2021-09-24 stsp {
8449 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8450 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8451 f259c4c1 2021-09-24 stsp
8452 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8453 f259c4c1 2021-09-24 stsp if (err)
8454 f259c4c1 2021-09-24 stsp goto done;
8455 f259c4c1 2021-09-24 stsp
8456 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8457 f259c4c1 2021-09-24 stsp if (err)
8458 f259c4c1 2021-09-24 stsp goto done;
8459 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8460 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8461 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8462 f259c4c1 2021-09-24 stsp err = sync_err;
8463 f259c4c1 2021-09-24 stsp done:
8464 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8465 f259c4c1 2021-09-24 stsp free(fileindex_path);
8466 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8467 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8468 f259c4c1 2021-09-24 stsp err = unlockerr;
8469 f259c4c1 2021-09-24 stsp return err;
8470 f259c4c1 2021-09-24 stsp }
8471 f259c4c1 2021-09-24 stsp
8472 f259c4c1 2021-09-24 stsp const struct got_error *
8473 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8474 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8475 f259c4c1 2021-09-24 stsp {
8476 f259c4c1 2021-09-24 stsp const struct got_error *err;
8477 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8478 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8479 f259c4c1 2021-09-24 stsp
8480 f259c4c1 2021-09-24 stsp *in_progress = 0;
8481 f259c4c1 2021-09-24 stsp
8482 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8483 f259c4c1 2021-09-24 stsp if (err)
8484 f259c4c1 2021-09-24 stsp return err;
8485 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8486 793fcac3 2021-09-24 stsp free(branch_refname);
8487 f259c4c1 2021-09-24 stsp if (err) {
8488 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8489 f259c4c1 2021-09-24 stsp return err;
8490 f259c4c1 2021-09-24 stsp } else
8491 f259c4c1 2021-09-24 stsp *in_progress = 1;
8492 f259c4c1 2021-09-24 stsp
8493 f259c4c1 2021-09-24 stsp return NULL;
8494 f259c4c1 2021-09-24 stsp }
8495 f259c4c1 2021-09-24 stsp
8496 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8497 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8498 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8499 f259c4c1 2021-09-24 stsp {
8500 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8501 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8502 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8503 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8504 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8505 f259c4c1 2021-09-24 stsp
8506 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8507 f259c4c1 2021-09-24 stsp
8508 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8509 f259c4c1 2021-09-24 stsp if (err)
8510 f259c4c1 2021-09-24 stsp return err;
8511 f259c4c1 2021-09-24 stsp
8512 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8513 f259c4c1 2021-09-24 stsp if (err)
8514 f259c4c1 2021-09-24 stsp goto done;
8515 f259c4c1 2021-09-24 stsp
8516 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8517 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8518 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8519 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8520 f259c4c1 2021-09-24 stsp &ok_arg);
8521 f259c4c1 2021-09-24 stsp if (err)
8522 f259c4c1 2021-09-24 stsp goto done;
8523 f259c4c1 2021-09-24 stsp
8524 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8525 f259c4c1 2021-09-24 stsp 0);
8526 f259c4c1 2021-09-24 stsp if (err)
8527 f259c4c1 2021-09-24 stsp goto done;
8528 f259c4c1 2021-09-24 stsp
8529 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8530 f259c4c1 2021-09-24 stsp if (err)
8531 f259c4c1 2021-09-24 stsp goto done;
8532 f259c4c1 2021-09-24 stsp
8533 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8534 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8535 f259c4c1 2021-09-24 stsp goto done;
8536 f259c4c1 2021-09-24 stsp }
8537 179f9db0 2023-06-20 falsifian
8538 179f9db0 2023-06-20 falsifian done:
8539 179f9db0 2023-06-20 falsifian free(fileindex_path);
8540 179f9db0 2023-06-20 falsifian if (wt_branch)
8541 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8542 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8543 179f9db0 2023-06-20 falsifian if (err) {
8544 179f9db0 2023-06-20 falsifian if (*fileindex) {
8545 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8546 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8547 179f9db0 2023-06-20 falsifian }
8548 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8549 179f9db0 2023-06-20 falsifian }
8550 179f9db0 2023-06-20 falsifian return err;
8551 179f9db0 2023-06-20 falsifian }
8552 f259c4c1 2021-09-24 stsp
8553 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8554 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8555 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8556 179f9db0 2023-06-20 falsifian {
8557 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8558 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8559 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8560 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8561 179f9db0 2023-06-20 falsifian
8562 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8563 179f9db0 2023-06-20 falsifian if (err)
8564 179f9db0 2023-06-20 falsifian return err;
8565 179f9db0 2023-06-20 falsifian
8566 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8567 179f9db0 2023-06-20 falsifian if (err)
8568 179f9db0 2023-06-20 falsifian return err;
8569 179f9db0 2023-06-20 falsifian
8570 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8571 f259c4c1 2021-09-24 stsp if (err)
8572 f259c4c1 2021-09-24 stsp goto done;
8573 f259c4c1 2021-09-24 stsp
8574 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8575 f259c4c1 2021-09-24 stsp if (err)
8576 f259c4c1 2021-09-24 stsp goto done;
8577 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8578 f259c4c1 2021-09-24 stsp if (err)
8579 f259c4c1 2021-09-24 stsp goto done;
8580 f259c4c1 2021-09-24 stsp
8581 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8582 f259c4c1 2021-09-24 stsp if (err)
8583 f259c4c1 2021-09-24 stsp goto done;
8584 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8585 f259c4c1 2021-09-24 stsp if (err)
8586 f259c4c1 2021-09-24 stsp goto done;
8587 f259c4c1 2021-09-24 stsp
8588 f259c4c1 2021-09-24 stsp done:
8589 f259c4c1 2021-09-24 stsp free(branch_refname);
8590 f259c4c1 2021-09-24 stsp free(commit_refname);
8591 f259c4c1 2021-09-24 stsp if (branch_ref)
8592 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8593 f259c4c1 2021-09-24 stsp if (commit_ref)
8594 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8595 179f9db0 2023-06-20 falsifian free(branch_tip);
8596 f259c4c1 2021-09-24 stsp return err;
8597 f259c4c1 2021-09-24 stsp }
8598 f259c4c1 2021-09-24 stsp
8599 f259c4c1 2021-09-24 stsp const struct got_error *
8600 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8601 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8602 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8603 f259c4c1 2021-09-24 stsp {
8604 f259c4c1 2021-09-24 stsp const struct got_error *err;
8605 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8606 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8607 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8608 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8609 f259c4c1 2021-09-24 stsp
8610 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8611 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8612 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8613 f259c4c1 2021-09-24 stsp
8614 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8615 f259c4c1 2021-09-24 stsp if (err)
8616 f259c4c1 2021-09-24 stsp return err;
8617 f259c4c1 2021-09-24 stsp
8618 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8619 f259c4c1 2021-09-24 stsp if (err)
8620 f259c4c1 2021-09-24 stsp goto done;
8621 f259c4c1 2021-09-24 stsp
8622 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8623 f259c4c1 2021-09-24 stsp &have_staged_files);
8624 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8625 f259c4c1 2021-09-24 stsp goto done;
8626 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8627 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8628 f259c4c1 2021-09-24 stsp goto done;
8629 f259c4c1 2021-09-24 stsp }
8630 f259c4c1 2021-09-24 stsp
8631 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8632 f259c4c1 2021-09-24 stsp if (err)
8633 f259c4c1 2021-09-24 stsp goto done;
8634 f259c4c1 2021-09-24 stsp
8635 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8636 f259c4c1 2021-09-24 stsp if (err)
8637 f259c4c1 2021-09-24 stsp goto done;
8638 f259c4c1 2021-09-24 stsp
8639 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8640 f259c4c1 2021-09-24 stsp if (err)
8641 f259c4c1 2021-09-24 stsp goto done;
8642 f259c4c1 2021-09-24 stsp
8643 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8644 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8645 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8646 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8647 f259c4c1 2021-09-24 stsp goto done;
8648 f259c4c1 2021-09-24 stsp }
8649 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8650 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8651 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8652 f259c4c1 2021-09-24 stsp goto done;
8653 f259c4c1 2021-09-24 stsp }
8654 f259c4c1 2021-09-24 stsp
8655 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8656 f259c4c1 2021-09-24 stsp if (err)
8657 f259c4c1 2021-09-24 stsp goto done;
8658 f259c4c1 2021-09-24 stsp
8659 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8660 f259c4c1 2021-09-24 stsp if (err)
8661 f259c4c1 2021-09-24 stsp goto done;
8662 f259c4c1 2021-09-24 stsp done:
8663 f259c4c1 2021-09-24 stsp free(commit_refname);
8664 f259c4c1 2021-09-24 stsp free(branch_refname);
8665 f259c4c1 2021-09-24 stsp free(fileindex_path);
8666 f259c4c1 2021-09-24 stsp if (commit_ref)
8667 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8668 f259c4c1 2021-09-24 stsp if (branch_ref)
8669 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8670 f259c4c1 2021-09-24 stsp if (err) {
8671 f259c4c1 2021-09-24 stsp if (*branch_name) {
8672 f259c4c1 2021-09-24 stsp free(*branch_name);
8673 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8674 f259c4c1 2021-09-24 stsp }
8675 f259c4c1 2021-09-24 stsp free(*branch_tip);
8676 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8677 f259c4c1 2021-09-24 stsp if (*fileindex) {
8678 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8679 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8680 f259c4c1 2021-09-24 stsp }
8681 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8682 f259c4c1 2021-09-24 stsp }
8683 f259c4c1 2021-09-24 stsp return err;
8684 f259c4c1 2021-09-24 stsp }
8685 f259c4c1 2021-09-24 stsp
8686 f259c4c1 2021-09-24 stsp const struct got_error *
8687 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8688 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8689 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8690 f259c4c1 2021-09-24 stsp {
8691 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8692 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8693 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8694 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8695 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8696 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8697 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8698 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8699 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8700 af179be7 2023-04-14 stsp
8701 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8702 af179be7 2023-04-14 stsp
8703 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8704 af179be7 2023-04-14 stsp if (err)
8705 af179be7 2023-04-14 stsp goto done;
8706 af179be7 2023-04-14 stsp
8707 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8708 af179be7 2023-04-14 stsp if (err)
8709 af179be7 2023-04-14 stsp goto done;
8710 af179be7 2023-04-14 stsp
8711 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8712 af179be7 2023-04-14 stsp if (err)
8713 af179be7 2023-04-14 stsp goto done;
8714 af179be7 2023-04-14 stsp
8715 af179be7 2023-04-14 stsp /*
8716 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8717 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8718 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8719 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8720 af179be7 2023-04-14 stsp */
8721 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8722 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8723 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8724 af179be7 2023-04-14 stsp if (err)
8725 af179be7 2023-04-14 stsp goto done;
8726 f259c4c1 2021-09-24 stsp
8727 af179be7 2023-04-14 stsp
8728 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8729 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8730 a44927cc 2022-04-07 stsp if (err)
8731 a44927cc 2022-04-07 stsp goto done;
8732 a44927cc 2022-04-07 stsp
8733 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8734 a44927cc 2022-04-07 stsp worktree->path_prefix);
8735 f259c4c1 2021-09-24 stsp if (err)
8736 f259c4c1 2021-09-24 stsp goto done;
8737 f259c4c1 2021-09-24 stsp
8738 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8739 f259c4c1 2021-09-24 stsp if (err)
8740 f259c4c1 2021-09-24 stsp goto done;
8741 f259c4c1 2021-09-24 stsp
8742 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8743 f259c4c1 2021-09-24 stsp if (err)
8744 f259c4c1 2021-09-24 stsp goto done;
8745 f259c4c1 2021-09-24 stsp
8746 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8747 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8748 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8749 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8750 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8751 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8752 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8753 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8754 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8755 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8756 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8757 f259c4c1 2021-09-24 stsp if (err)
8758 f259c4c1 2021-09-24 stsp goto sync;
8759 f259c4c1 2021-09-24 stsp
8760 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8761 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8762 f259c4c1 2021-09-24 stsp sync:
8763 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8764 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8765 f259c4c1 2021-09-24 stsp err = sync_err;
8766 f259c4c1 2021-09-24 stsp done:
8767 f259c4c1 2021-09-24 stsp free(tree_id);
8768 af179be7 2023-04-14 stsp free(merged_commit_id);
8769 a44927cc 2022-04-07 stsp if (commit)
8770 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8771 f259c4c1 2021-09-24 stsp if (fileindex)
8772 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8773 f259c4c1 2021-09-24 stsp free(fileindex_path);
8774 af179be7 2023-04-14 stsp if (commit_ref)
8775 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8776 af179be7 2023-04-14 stsp free(commit_ref_name);
8777 f259c4c1 2021-09-24 stsp
8778 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8779 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8780 f259c4c1 2021-09-24 stsp err = unlockerr;
8781 f259c4c1 2021-09-24 stsp return err;
8782 f259c4c1 2021-09-24 stsp }
8783 f259c4c1 2021-09-24 stsp
8784 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8785 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8786 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8787 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8788 2db2652d 2019-08-07 stsp struct got_repository *repo;
8789 2db2652d 2019-08-07 stsp int have_changes;
8790 2db2652d 2019-08-07 stsp };
8791 2db2652d 2019-08-07 stsp
8792 336075a4 2022-06-25 op static const struct got_error *
8793 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8794 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8795 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8796 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8797 735ef5ac 2019-08-03 stsp {
8798 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8799 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8800 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8801 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8802 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8803 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8804 8b13ce36 2019-08-08 stsp
8805 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8806 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8807 8b13ce36 2019-08-08 stsp return NULL;
8808 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8809 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8810 735ef5ac 2019-08-03 stsp
8811 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8812 735ef5ac 2019-08-03 stsp if (ie == NULL)
8813 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8814 735ef5ac 2019-08-03 stsp
8815 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8816 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8817 735ef5ac 2019-08-03 stsp relpath) == -1)
8818 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8819 735ef5ac 2019-08-03 stsp
8820 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8821 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8822 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8823 735ef5ac 2019-08-03 stsp }
8824 735ef5ac 2019-08-03 stsp
8825 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8826 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8827 3aa5969e 2019-08-06 stsp goto done;
8828 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8829 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8830 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8831 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8832 735ef5ac 2019-08-03 stsp goto done;
8833 3aa5969e 2019-08-06 stsp }
8834 735ef5ac 2019-08-03 stsp
8835 2db2652d 2019-08-07 stsp a->have_changes = 1;
8836 2db2652d 2019-08-07 stsp
8837 735ef5ac 2019-08-03 stsp p = in_repo_path;
8838 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8839 735ef5ac 2019-08-03 stsp p++;
8840 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8841 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8842 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8843 735ef5ac 2019-08-03 stsp done:
8844 735ef5ac 2019-08-03 stsp free(in_repo_path);
8845 dc424a06 2019-08-07 stsp return err;
8846 dc424a06 2019-08-07 stsp }
8847 dc424a06 2019-08-07 stsp
8848 2db2652d 2019-08-07 stsp struct stage_path_arg {
8849 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8850 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8851 2db2652d 2019-08-07 stsp struct got_repository *repo;
8852 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8853 2db2652d 2019-08-07 stsp void *status_arg;
8854 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8855 2db2652d 2019-08-07 stsp void *patch_arg;
8856 7b5dc508 2019-10-28 stsp int staged_something;
8857 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8858 2db2652d 2019-08-07 stsp };
8859 2db2652d 2019-08-07 stsp
8860 2db2652d 2019-08-07 stsp static const struct got_error *
8861 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8862 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8863 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8864 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8865 0cb83759 2019-08-03 stsp {
8866 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8867 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8868 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8869 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8870 0cb83759 2019-08-03 stsp uint32_t stage;
8871 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8872 0aeb8099 2020-07-23 stsp struct stat sb;
8873 8b13ce36 2019-08-08 stsp
8874 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8875 8b13ce36 2019-08-08 stsp return NULL;
8876 0cb83759 2019-08-03 stsp
8877 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8878 d3e7c587 2019-08-03 stsp if (ie == NULL)
8879 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8880 0cb83759 2019-08-03 stsp
8881 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8882 2db2652d 2019-08-07 stsp relpath)== -1)
8883 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8884 0cb83759 2019-08-03 stsp
8885 0cb83759 2019-08-03 stsp switch (status) {
8886 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8887 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8888 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8889 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8890 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8891 0aeb8099 2020-07-23 stsp break;
8892 0aeb8099 2020-07-23 stsp }
8893 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8894 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8895 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8896 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8897 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8898 dc424a06 2019-08-07 stsp if (err)
8899 dc424a06 2019-08-07 stsp break;
8900 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8901 dc424a06 2019-08-07 stsp break;
8902 dc424a06 2019-08-07 stsp } else {
8903 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8904 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8905 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8906 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8907 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8908 dc424a06 2019-08-07 stsp break;
8909 dc424a06 2019-08-07 stsp }
8910 dc424a06 2019-08-07 stsp }
8911 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8912 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8913 0cb83759 2019-08-03 stsp if (err)
8914 dc424a06 2019-08-07 stsp break;
8915 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8916 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8917 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8918 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8919 0cb83759 2019-08-03 stsp else
8920 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8921 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8922 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8923 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8924 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8925 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8926 35213c7c 2020-07-23 stsp ssize_t target_len;
8927 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8928 35213c7c 2020-07-23 stsp sizeof(target_path));
8929 35213c7c 2020-07-23 stsp if (target_len == -1) {
8930 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8931 35213c7c 2020-07-23 stsp ondisk_path);
8932 35213c7c 2020-07-23 stsp break;
8933 35213c7c 2020-07-23 stsp }
8934 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8935 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8936 35213c7c 2020-07-23 stsp a->worktree->root_path);
8937 35213c7c 2020-07-23 stsp if (err)
8938 35213c7c 2020-07-23 stsp break;
8939 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8940 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8941 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8942 35213c7c 2020-07-23 stsp break;
8943 35213c7c 2020-07-23 stsp }
8944 35213c7c 2020-07-23 stsp }
8945 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8946 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8947 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8948 35213c7c 2020-07-23 stsp else
8949 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8950 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8951 0aeb8099 2020-07-23 stsp } else {
8952 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8953 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8954 0aeb8099 2020-07-23 stsp }
8955 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8956 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8957 dc424a06 2019-08-07 stsp break;
8958 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8959 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8960 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8961 9fdde394 2022-06-04 op if (err)
8962 9fdde394 2022-06-04 op break;
8963 9fdde394 2022-06-04 op /*
8964 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8965 9fdde394 2022-06-04 op * implicitly unstage the file.
8966 9fdde394 2022-06-04 op */
8967 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8968 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8969 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8970 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8971 9fdde394 2022-06-04 op }
8972 0cb83759 2019-08-03 stsp break;
8973 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8974 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8975 d3e7c587 2019-08-03 stsp break;
8976 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8977 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8978 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8979 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8980 dc424a06 2019-08-07 stsp if (err)
8981 dc424a06 2019-08-07 stsp break;
8982 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8983 88f33a19 2019-08-08 stsp break;
8984 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8985 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8986 dc424a06 2019-08-07 stsp break;
8987 88f33a19 2019-08-08 stsp }
8988 dc424a06 2019-08-07 stsp }
8989 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8990 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8991 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8992 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8993 dc424a06 2019-08-07 stsp break;
8994 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8995 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8996 12463d8b 2019-12-13 stsp de_name);
8997 0cb83759 2019-08-03 stsp break;
8998 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8999 d3e7c587 2019-08-03 stsp break;
9000 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9001 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9002 ebf48fd5 2019-08-03 stsp break;
9003 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9004 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9005 2a06fe5f 2019-08-24 stsp break;
9006 0cb83759 2019-08-03 stsp default:
9007 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9008 537ac44b 2019-08-03 stsp break;
9009 0cb83759 2019-08-03 stsp }
9010 dc424a06 2019-08-07 stsp
9011 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9012 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9013 dc424a06 2019-08-07 stsp free(path_content);
9014 2db2652d 2019-08-07 stsp free(ondisk_path);
9015 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9016 0ebf8283 2019-07-24 stsp return err;
9017 0ebf8283 2019-07-24 stsp }
9018 0cb83759 2019-08-03 stsp
9019 0cb83759 2019-08-03 stsp const struct got_error *
9020 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9021 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9022 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9023 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9024 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9025 0cb83759 2019-08-03 stsp {
9026 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9027 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9028 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9029 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9030 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9031 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9032 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9033 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9034 0cb83759 2019-08-03 stsp
9035 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9036 0cb83759 2019-08-03 stsp if (err)
9037 0cb83759 2019-08-03 stsp return err;
9038 0cb83759 2019-08-03 stsp
9039 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9040 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9041 735ef5ac 2019-08-03 stsp if (err)
9042 735ef5ac 2019-08-03 stsp goto done;
9043 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9044 735ef5ac 2019-08-03 stsp if (err)
9045 735ef5ac 2019-08-03 stsp goto done;
9046 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9047 0cb83759 2019-08-03 stsp if (err)
9048 0cb83759 2019-08-03 stsp goto done;
9049 0cb83759 2019-08-03 stsp
9050 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9051 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9052 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9053 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9054 2db2652d 2019-08-07 stsp oka.repo = repo;
9055 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9056 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9057 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9058 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9059 735ef5ac 2019-08-03 stsp if (err)
9060 735ef5ac 2019-08-03 stsp goto done;
9061 735ef5ac 2019-08-03 stsp }
9062 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9063 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9064 2db2652d 2019-08-07 stsp goto done;
9065 2db2652d 2019-08-07 stsp }
9066 735ef5ac 2019-08-03 stsp
9067 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9068 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9069 2db2652d 2019-08-07 stsp spa.repo = repo;
9070 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9071 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9072 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9073 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9074 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9075 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9076 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9077 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9078 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9079 42005733 2019-08-03 stsp if (err)
9080 2db2652d 2019-08-07 stsp goto done;
9081 7b5dc508 2019-10-28 stsp }
9082 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9083 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9084 7b5dc508 2019-10-28 stsp goto done;
9085 0cb83759 2019-08-03 stsp }
9086 0cb83759 2019-08-03 stsp
9087 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9088 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9089 0cb83759 2019-08-03 stsp err = sync_err;
9090 0cb83759 2019-08-03 stsp done:
9091 735ef5ac 2019-08-03 stsp if (head_ref)
9092 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9093 735ef5ac 2019-08-03 stsp free(head_commit_id);
9094 0cb83759 2019-08-03 stsp free(fileindex_path);
9095 0cb83759 2019-08-03 stsp if (fileindex)
9096 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9097 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9098 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9099 0cb83759 2019-08-03 stsp err = unlockerr;
9100 0cb83759 2019-08-03 stsp return err;
9101 0cb83759 2019-08-03 stsp }
9102 ad493afc 2019-08-03 stsp
9103 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9104 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9105 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9106 ad493afc 2019-08-03 stsp struct got_repository *repo;
9107 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9108 ad493afc 2019-08-03 stsp void *progress_arg;
9109 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9110 2e1f37b0 2019-08-08 stsp void *patch_arg;
9111 ad493afc 2019-08-03 stsp };
9112 2e1f37b0 2019-08-08 stsp
9113 2e1f37b0 2019-08-08 stsp static const struct got_error *
9114 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9115 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9116 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9117 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9118 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9119 2e1f37b0 2019-08-08 stsp {
9120 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9121 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9122 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9123 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9124 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9125 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9126 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9127 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9128 2e1f37b0 2019-08-08 stsp
9129 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9130 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9131 2e1f37b0 2019-08-08 stsp
9132 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9133 2e1f37b0 2019-08-08 stsp if (err)
9134 2e1f37b0 2019-08-08 stsp return err;
9135 eb81bc23 2022-06-28 tracey
9136 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9137 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9138 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9139 eb81bc23 2022-06-28 tracey goto done;
9140 eb81bc23 2022-06-28 tracey }
9141 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9142 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9143 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9144 eb81bc23 2022-06-28 tracey goto done;
9145 eb81bc23 2022-06-28 tracey }
9146 eb81bc23 2022-06-28 tracey
9147 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9148 2e1f37b0 2019-08-08 stsp if (err)
9149 2e1f37b0 2019-08-08 stsp goto done;
9150 2e1f37b0 2019-08-08 stsp
9151 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9152 2e1f37b0 2019-08-08 stsp if (err)
9153 2e1f37b0 2019-08-08 stsp goto done;
9154 2e1f37b0 2019-08-08 stsp
9155 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9156 2e1f37b0 2019-08-08 stsp if (err)
9157 2e1f37b0 2019-08-08 stsp goto done;
9158 2e1f37b0 2019-08-08 stsp
9159 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9160 eb81bc23 2022-06-28 tracey fd2);
9161 2e1f37b0 2019-08-08 stsp if (err)
9162 2e1f37b0 2019-08-08 stsp goto done;
9163 2e1f37b0 2019-08-08 stsp
9164 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9165 2e1f37b0 2019-08-08 stsp if (err)
9166 2e1f37b0 2019-08-08 stsp goto done;
9167 ad493afc 2019-08-03 stsp
9168 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9169 2e1f37b0 2019-08-08 stsp if (err)
9170 2e1f37b0 2019-08-08 stsp goto done;
9171 2e1f37b0 2019-08-08 stsp
9172 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9173 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9174 2e1f37b0 2019-08-08 stsp if (err)
9175 2e1f37b0 2019-08-08 stsp goto done;
9176 2e1f37b0 2019-08-08 stsp
9177 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9178 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9179 2e1f37b0 2019-08-08 stsp if (err)
9180 2e1f37b0 2019-08-08 stsp goto done;
9181 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9182 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9183 2e1f37b0 2019-08-08 stsp if (err)
9184 2e1f37b0 2019-08-08 stsp goto done;
9185 2e1f37b0 2019-08-08 stsp
9186 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9187 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9188 2e1f37b0 2019-08-08 stsp goto done;
9189 2e1f37b0 2019-08-08 stsp }
9190 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9191 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9192 2e1f37b0 2019-08-08 stsp goto done;
9193 2e1f37b0 2019-08-08 stsp }
9194 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9195 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9196 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9197 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9198 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9199 fe621944 2020-11-10 stsp nchanges++;
9200 fe621944 2020-11-10 stsp }
9201 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9202 2e1f37b0 2019-08-08 stsp int choice;
9203 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9204 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9205 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9206 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9207 2e1f37b0 2019-08-08 stsp if (err)
9208 2e1f37b0 2019-08-08 stsp goto done;
9209 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9210 2e1f37b0 2019-08-08 stsp have_content = 1;
9211 19e4b907 2019-08-08 stsp else
9212 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9213 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9214 2e1f37b0 2019-08-08 stsp break;
9215 2e1f37b0 2019-08-08 stsp }
9216 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9217 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9218 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9219 2e1f37b0 2019-08-08 stsp done:
9220 2e1f37b0 2019-08-08 stsp free(label1);
9221 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9222 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9223 2e1f37b0 2019-08-08 stsp if (blob)
9224 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9225 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9226 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9227 2e1f37b0 2019-08-08 stsp if (staged_blob)
9228 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9229 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9230 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9231 fe621944 2020-11-10 stsp err = free_err;
9232 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9233 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9234 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9235 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9236 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9237 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9238 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9239 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9240 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9241 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9242 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9243 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9244 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9245 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9246 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9247 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9248 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9249 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9250 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9251 2e1f37b0 2019-08-08 stsp }
9252 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9253 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9254 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9255 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9256 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9257 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9258 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9259 2e1f37b0 2019-08-08 stsp }
9260 2e1f37b0 2019-08-08 stsp free(path1);
9261 2e1f37b0 2019-08-08 stsp free(path2);
9262 fda8017d 2020-07-23 stsp return err;
9263 fda8017d 2020-07-23 stsp }
9264 fda8017d 2020-07-23 stsp
9265 fda8017d 2020-07-23 stsp static const struct got_error *
9266 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9267 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9268 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9269 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9270 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9271 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9272 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9273 fda8017d 2020-07-23 stsp {
9274 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9275 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9276 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9277 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9278 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9279 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9280 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9281 fda8017d 2020-07-23 stsp struct stat sb;
9282 fda8017d 2020-07-23 stsp
9283 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9284 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9285 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9286 fda8017d 2020-07-23 stsp if (err)
9287 fda8017d 2020-07-23 stsp return err;
9288 fda8017d 2020-07-23 stsp
9289 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9290 fda8017d 2020-07-23 stsp return NULL;
9291 fda8017d 2020-07-23 stsp
9292 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9293 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9294 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9295 fda8017d 2020-07-23 stsp if (err)
9296 fda8017d 2020-07-23 stsp goto done;
9297 fda8017d 2020-07-23 stsp }
9298 fda8017d 2020-07-23 stsp
9299 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9300 fda8017d 2020-07-23 stsp if (f == NULL) {
9301 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9302 fda8017d 2020-07-23 stsp path_unstaged_content);
9303 fda8017d 2020-07-23 stsp goto done;
9304 fda8017d 2020-07-23 stsp }
9305 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9306 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9307 fda8017d 2020-07-23 stsp goto done;
9308 fda8017d 2020-07-23 stsp }
9309 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9310 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9311 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9312 fda8017d 2020-07-23 stsp size_t r;
9313 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9314 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9315 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9316 fda8017d 2020-07-23 stsp goto done;
9317 fda8017d 2020-07-23 stsp }
9318 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9319 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9320 fda8017d 2020-07-23 stsp goto done;
9321 fda8017d 2020-07-23 stsp }
9322 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9323 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9324 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9325 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9326 fda8017d 2020-07-23 stsp progress_arg);
9327 fda8017d 2020-07-23 stsp } else {
9328 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9329 67a66647 2021-05-31 stsp
9330 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9331 67a66647 2021-05-31 stsp if (err)
9332 67a66647 2021-05-31 stsp return err;
9333 67a66647 2021-05-31 stsp
9334 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9335 67a66647 2021-05-31 stsp parent) == -1) {
9336 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9337 67a66647 2021-05-31 stsp base_path = NULL;
9338 67a66647 2021-05-31 stsp goto done;
9339 67a66647 2021-05-31 stsp }
9340 67a66647 2021-05-31 stsp
9341 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9342 b90054ed 2022-11-01 stsp base_path, "");
9343 67a66647 2021-05-31 stsp if (err)
9344 67a66647 2021-05-31 stsp goto done;
9345 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9346 67a66647 2021-05-31 stsp blob_base);
9347 67a66647 2021-05-31 stsp if (err)
9348 67a66647 2021-05-31 stsp goto done;
9349 67a66647 2021-05-31 stsp
9350 5e91dae4 2022-08-30 stsp /*
9351 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9352 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9353 eec2f5a9 2021-06-03 stsp */
9354 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9355 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9356 eec2f5a9 2021-06-03 stsp ondisk_path);
9357 eec2f5a9 2021-06-03 stsp if (err)
9358 eec2f5a9 2021-06-03 stsp goto done;
9359 eec2f5a9 2021-06-03 stsp } else {
9360 eec2f5a9 2021-06-03 stsp int fd;
9361 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9362 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9363 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9364 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9365 eec2f5a9 2021-06-03 stsp goto done;
9366 eec2f5a9 2021-06-03 stsp }
9367 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9368 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9369 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9370 eec2f5a9 2021-06-03 stsp close(fd);
9371 eec2f5a9 2021-06-03 stsp goto done;
9372 eec2f5a9 2021-06-03 stsp }
9373 eec2f5a9 2021-06-03 stsp }
9374 eec2f5a9 2021-06-03 stsp
9375 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9376 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9377 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9378 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9379 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9380 fda8017d 2020-07-23 stsp }
9381 fda8017d 2020-07-23 stsp if (err)
9382 fda8017d 2020-07-23 stsp goto done;
9383 fda8017d 2020-07-23 stsp
9384 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9385 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9386 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9387 2ac8aa02 2020-11-09 stsp } else {
9388 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9389 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9390 2ac8aa02 2020-11-09 stsp }
9391 fda8017d 2020-07-23 stsp done:
9392 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9393 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9394 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9395 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9396 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9397 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9398 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9399 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9400 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9401 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9402 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9403 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9404 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9405 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9406 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9407 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9408 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9409 67a66647 2021-05-31 stsp free(blob_base_path);
9410 67a66647 2021-05-31 stsp free(parent);
9411 67a66647 2021-05-31 stsp free(base_path);
9412 2e1f37b0 2019-08-08 stsp return err;
9413 2e1f37b0 2019-08-08 stsp }
9414 2e1f37b0 2019-08-08 stsp
9415 ad493afc 2019-08-03 stsp static const struct got_error *
9416 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9417 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9418 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9419 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9420 ad493afc 2019-08-03 stsp {
9421 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9422 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9423 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9424 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9425 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9426 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9427 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9428 9bc94a15 2019-08-03 stsp struct stat sb;
9429 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9430 ad493afc 2019-08-03 stsp
9431 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9432 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9433 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9434 2e1f37b0 2019-08-08 stsp return NULL;
9435 2e1f37b0 2019-08-08 stsp
9436 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9437 ad493afc 2019-08-03 stsp if (ie == NULL)
9438 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9439 9bc94a15 2019-08-03 stsp
9440 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9441 9bc94a15 2019-08-03 stsp == -1)
9442 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9443 ad493afc 2019-08-03 stsp
9444 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9445 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9446 f69721c3 2019-10-21 stsp if (err)
9447 f69721c3 2019-10-21 stsp goto done;
9448 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9449 f69721c3 2019-10-21 stsp id_str) == -1) {
9450 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9451 eb81bc23 2022-06-28 tracey goto done;
9452 eb81bc23 2022-06-28 tracey }
9453 eb81bc23 2022-06-28 tracey
9454 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9455 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9456 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9457 eb81bc23 2022-06-28 tracey goto done;
9458 eb81bc23 2022-06-28 tracey }
9459 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9460 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9461 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9462 f69721c3 2019-10-21 stsp goto done;
9463 f69721c3 2019-10-21 stsp }
9464 f69721c3 2019-10-21 stsp
9465 ad493afc 2019-08-03 stsp switch (staged_status) {
9466 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9467 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9468 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9469 ad493afc 2019-08-03 stsp if (err)
9470 ad493afc 2019-08-03 stsp break;
9471 ad493afc 2019-08-03 stsp /* fall through */
9472 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9473 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9474 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9475 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9476 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9477 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9478 2e1f37b0 2019-08-08 stsp if (err)
9479 2e1f37b0 2019-08-08 stsp break;
9480 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9481 2e1f37b0 2019-08-08 stsp break;
9482 2e1f37b0 2019-08-08 stsp } else {
9483 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9484 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9485 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9486 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9487 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9488 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9489 2e1f37b0 2019-08-08 stsp }
9490 2e1f37b0 2019-08-08 stsp }
9491 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9492 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9493 ad493afc 2019-08-03 stsp if (err)
9494 ad493afc 2019-08-03 stsp break;
9495 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9496 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9497 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9498 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9499 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9500 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9501 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9502 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9503 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9504 ea7786be 2020-07-23 stsp break;
9505 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9506 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9507 36bf999c 2020-07-23 stsp char *staged_target;
9508 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9509 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9510 36bf999c 2020-07-23 stsp if (err)
9511 36bf999c 2020-07-23 stsp goto done;
9512 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9513 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9514 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9515 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9516 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9517 36bf999c 2020-07-23 stsp free(staged_target);
9518 dfe9fba0 2020-07-23 stsp } else {
9519 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9520 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9521 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9522 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9523 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9524 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9525 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9526 dfe9fba0 2020-07-23 stsp }
9527 ea7786be 2020-07-23 stsp break;
9528 ea7786be 2020-07-23 stsp default:
9529 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9530 ea7786be 2020-07-23 stsp break;
9531 ea7786be 2020-07-23 stsp }
9532 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9533 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9534 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9535 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9536 2ac8aa02 2020-11-09 stsp }
9537 ad493afc 2019-08-03 stsp break;
9538 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9539 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9540 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9541 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9542 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9543 2e1f37b0 2019-08-08 stsp if (err)
9544 2e1f37b0 2019-08-08 stsp break;
9545 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9546 2e1f37b0 2019-08-08 stsp break;
9547 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9548 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9549 2e1f37b0 2019-08-08 stsp break;
9550 2e1f37b0 2019-08-08 stsp }
9551 2e1f37b0 2019-08-08 stsp }
9552 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9553 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9554 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9555 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9556 9bc94a15 2019-08-03 stsp if (err)
9557 9bc94a15 2019-08-03 stsp break;
9558 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9559 ad493afc 2019-08-03 stsp break;
9560 ad493afc 2019-08-03 stsp }
9561 f69721c3 2019-10-21 stsp done:
9562 ad493afc 2019-08-03 stsp free(ondisk_path);
9563 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9564 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9565 ad493afc 2019-08-03 stsp if (blob_base)
9566 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9567 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9568 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9569 ad493afc 2019-08-03 stsp if (blob_staged)
9570 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9571 f69721c3 2019-10-21 stsp free(id_str);
9572 f69721c3 2019-10-21 stsp free(label_orig);
9573 ad493afc 2019-08-03 stsp return err;
9574 ad493afc 2019-08-03 stsp }
9575 ad493afc 2019-08-03 stsp
9576 ad493afc 2019-08-03 stsp const struct got_error *
9577 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9578 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9579 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9580 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9581 ad493afc 2019-08-03 stsp struct got_repository *repo)
9582 ad493afc 2019-08-03 stsp {
9583 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9584 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9585 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9586 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9587 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9588 ad493afc 2019-08-03 stsp
9589 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9590 ad493afc 2019-08-03 stsp if (err)
9591 ad493afc 2019-08-03 stsp return err;
9592 ad493afc 2019-08-03 stsp
9593 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9594 ad493afc 2019-08-03 stsp if (err)
9595 ad493afc 2019-08-03 stsp goto done;
9596 ad493afc 2019-08-03 stsp
9597 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9598 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9599 ad493afc 2019-08-03 stsp upa.repo = repo;
9600 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9601 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9602 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9603 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9604 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9605 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9606 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9607 ad493afc 2019-08-03 stsp if (err)
9608 ad493afc 2019-08-03 stsp goto done;
9609 ad493afc 2019-08-03 stsp }
9610 ad493afc 2019-08-03 stsp
9611 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9612 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9613 ad493afc 2019-08-03 stsp err = sync_err;
9614 ad493afc 2019-08-03 stsp done:
9615 ad493afc 2019-08-03 stsp free(fileindex_path);
9616 ad493afc 2019-08-03 stsp if (fileindex)
9617 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9618 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9619 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9620 ad493afc 2019-08-03 stsp err = unlockerr;
9621 ad493afc 2019-08-03 stsp return err;
9622 ad493afc 2019-08-03 stsp }
9623 b2118c49 2020-07-28 stsp
9624 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9625 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9626 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9627 b2118c49 2020-07-28 stsp void *info_arg;
9628 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9629 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9630 b2118c49 2020-07-28 stsp void *cancel_arg;
9631 b2118c49 2020-07-28 stsp };
9632 b2118c49 2020-07-28 stsp
9633 b2118c49 2020-07-28 stsp static const struct got_error *
9634 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9635 b2118c49 2020-07-28 stsp {
9636 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9637 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9638 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9639 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9640 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9641 b2118c49 2020-07-28 stsp int stage;
9642 b2118c49 2020-07-28 stsp
9643 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9644 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9645 b2118c49 2020-07-28 stsp
9646 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9647 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9648 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9649 b2118c49 2020-07-28 stsp break;
9650 b2118c49 2020-07-28 stsp }
9651 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9652 b2118c49 2020-07-28 stsp return NULL;
9653 b2118c49 2020-07-28 stsp
9654 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9655 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9656 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9657 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9658 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9659 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9660 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9661 b2118c49 2020-07-28 stsp }
9662 b2118c49 2020-07-28 stsp
9663 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9664 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9665 b2118c49 2020-07-28 stsp
9666 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9667 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9668 b2118c49 2020-07-28 stsp }
9669 b2118c49 2020-07-28 stsp
9670 b2118c49 2020-07-28 stsp const struct got_error *
9671 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9672 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9673 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9674 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9675 b2118c49 2020-07-28 stsp
9676 b2118c49 2020-07-28 stsp {
9677 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9678 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9679 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9680 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9681 b2118c49 2020-07-28 stsp
9682 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9683 b2118c49 2020-07-28 stsp if (err)
9684 b2118c49 2020-07-28 stsp return err;
9685 b2118c49 2020-07-28 stsp
9686 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9687 b2118c49 2020-07-28 stsp if (err)
9688 b2118c49 2020-07-28 stsp goto done;
9689 b2118c49 2020-07-28 stsp
9690 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9691 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9692 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9693 b2118c49 2020-07-28 stsp arg.paths = paths;
9694 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9695 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9696 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9697 b2118c49 2020-07-28 stsp &arg);
9698 b2118c49 2020-07-28 stsp done:
9699 b2118c49 2020-07-28 stsp free(fileindex_path);
9700 b2118c49 2020-07-28 stsp if (fileindex)
9701 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9702 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9703 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9704 b2118c49 2020-07-28 stsp err = unlockerr;
9705 b2118c49 2020-07-28 stsp return err;
9706 b2118c49 2020-07-28 stsp }
9707 78f5ac24 2022-03-19 op
9708 78f5ac24 2022-03-19 op static const struct got_error *
9709 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9710 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9711 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9712 78f5ac24 2022-03-19 op {
9713 78f5ac24 2022-03-19 op const struct got_error *err;
9714 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9715 78f5ac24 2022-03-19 op struct stat sb;
9716 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9717 78f5ac24 2022-03-19 op
9718 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9719 78f5ac24 2022-03-19 op if (err)
9720 78f5ac24 2022-03-19 op return err;
9721 78f5ac24 2022-03-19 op
9722 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9723 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9724 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9725 78f5ac24 2022-03-19 op
9726 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9727 78f5ac24 2022-03-19 op if (ie) {
9728 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9729 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9730 a05fb460 2022-04-23 op repo);
9731 78f5ac24 2022-03-19 op if (err)
9732 78f5ac24 2022-03-19 op goto done;
9733 78f5ac24 2022-03-19 op } else {
9734 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9735 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9736 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9737 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9738 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9739 78f5ac24 2022-03-19 op ondisk_path);
9740 78f5ac24 2022-03-19 op goto done;
9741 78f5ac24 2022-03-19 op }
9742 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9743 78f5ac24 2022-03-19 op }
9744 78f5ac24 2022-03-19 op }
9745 78f5ac24 2022-03-19 op
9746 78f5ac24 2022-03-19 op done:
9747 78f5ac24 2022-03-19 op free(ondisk_path);
9748 78f5ac24 2022-03-19 op return err;
9749 78f5ac24 2022-03-19 op }
9750 78f5ac24 2022-03-19 op
9751 78f5ac24 2022-03-19 op static const struct got_error *
9752 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9753 78f5ac24 2022-03-19 op unsigned char staged_status)
9754 78f5ac24 2022-03-19 op {
9755 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9756 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9757 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9758 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9759 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9760 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9761 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9762 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9763 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9764 78f5ac24 2022-03-19 op return NULL;
9765 78f5ac24 2022-03-19 op }
9766 78f5ac24 2022-03-19 op
9767 78f5ac24 2022-03-19 op static const struct got_error *
9768 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9769 78f5ac24 2022-03-19 op {
9770 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9771 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9772 78f5ac24 2022-03-19 op return NULL;
9773 78f5ac24 2022-03-19 op }
9774 78f5ac24 2022-03-19 op
9775 78f5ac24 2022-03-19 op static const struct got_error *
9776 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9777 78f5ac24 2022-03-19 op unsigned char staged_status)
9778 78f5ac24 2022-03-19 op {
9779 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9780 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9781 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9782 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9783 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9784 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9785 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9786 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9787 78f5ac24 2022-03-19 op return NULL;
9788 78f5ac24 2022-03-19 op }
9789 78f5ac24 2022-03-19 op
9790 78f5ac24 2022-03-19 op const struct got_error *
9791 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9792 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9793 78f5ac24 2022-03-19 op {
9794 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9795 78f5ac24 2022-03-19 op }
9796 78f5ac24 2022-03-19 op
9797 78f5ac24 2022-03-19 op const struct got_error *
9798 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9799 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9800 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9801 78f5ac24 2022-03-19 op {
9802 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9803 78f5ac24 2022-03-19 op int file_renamed = 0;
9804 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9805 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9806 78f5ac24 2022-03-19 op
9807 78f5ac24 2022-03-19 op *oldpath = NULL;
9808 78f5ac24 2022-03-19 op *newpath = NULL;
9809 78f5ac24 2022-03-19 op
9810 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9811 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9812 78f5ac24 2022-03-19 op if (err)
9813 78f5ac24 2022-03-19 op goto done;
9814 78f5ac24 2022-03-19 op
9815 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9816 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9817 78f5ac24 2022-03-19 op if (err)
9818 78f5ac24 2022-03-19 op goto done;
9819 78f5ac24 2022-03-19 op
9820 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9821 78f5ac24 2022-03-19 op file_renamed = 1;
9822 78f5ac24 2022-03-19 op
9823 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9824 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9825 78f5ac24 2022-03-19 op else if (file_renamed) {
9826 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9827 78f5ac24 2022-03-19 op if (err == NULL)
9828 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9829 78f5ac24 2022-03-19 op } else if (old == NULL)
9830 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9831 78f5ac24 2022-03-19 op else
9832 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9833 78f5ac24 2022-03-19 op
9834 78f5ac24 2022-03-19 op done:
9835 78f5ac24 2022-03-19 op if (err) {
9836 78f5ac24 2022-03-19 op free(*oldpath);
9837 78f5ac24 2022-03-19 op *oldpath = NULL;
9838 78f5ac24 2022-03-19 op free(*newpath);
9839 78f5ac24 2022-03-19 op *newpath = NULL;
9840 78f5ac24 2022-03-19 op }
9841 78f5ac24 2022-03-19 op return err;
9842 78f5ac24 2022-03-19 op }
9843 78f5ac24 2022-03-19 op
9844 78f5ac24 2022-03-19 op const struct got_error *
9845 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9846 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9847 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9848 78f5ac24 2022-03-19 op {
9849 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9850 f2dd7807 2022-05-17 op
9851 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9852 f2dd7807 2022-05-17 op saa.worktree = worktree;
9853 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9854 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9855 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9856 f2dd7807 2022-05-17 op saa.repo = repo;
9857 f2dd7807 2022-05-17 op
9858 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9859 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9860 78f5ac24 2022-03-19 op }
9861 f2dd7807 2022-05-17 op
9862 f2dd7807 2022-05-17 op const struct got_error *
9863 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9864 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9865 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9866 f2dd7807 2022-05-17 op {
9867 7f4e5320 2023-05-29 stsp const struct got_error *err;
9868 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9869 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
9870 f2dd7807 2022-05-17 op
9871 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9872 f2dd7807 2022-05-17 op sda.worktree = worktree;
9873 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9874 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9875 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9876 f2dd7807 2022-05-17 op sda.repo = repo;
9877 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9878 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9879 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9880 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9881 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
9882 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
9883 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
9884 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
9885 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
9886 7f4e5320 2023-05-29 stsp
9887 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
9888 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9889 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
9890 7f4e5320 2023-05-29 stsp return err;
9891 f2dd7807 2022-05-17 op }
9892 f2dd7807 2022-05-17 op
9893 f2dd7807 2022-05-17 op const struct got_error *
9894 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9895 4bcdc895 2022-05-17 op const char *fileindex_path)
9896 f2dd7807 2022-05-17 op {
9897 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9898 f2dd7807 2022-05-17 op
9899 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9900 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9901 f2dd7807 2022-05-17 op
9902 f2dd7807 2022-05-17 op return err;
9903 f2dd7807 2022-05-17 op }