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 df6221c7 2023-07-19 stsp const char *prefix, const char *meta_dir, 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 df6221c7 2023-07-19 stsp /* Create .got/.cvg directory (may already exist). */
192 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", path, meta_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 df6221c7 2023-07-19 stsp worktree->meta_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 df6221c7 2023-07-19 stsp worktree->meta_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 6a390967 2023-07-14 stsp
400 6a390967 2023-07-14 stsp /* We only accept worktree-relative paths here. */
401 6a390967 2023-07-14 stsp if (got_path_is_absolute(path)) {
402 6a390967 2023-07-14 stsp return got_error_fmt(GOT_ERR_BAD_PATH,
403 6a390967 2023-07-14 stsp "%s does not accept absolute paths: %s",
404 6a390967 2023-07-14 stsp __func__, path);
405 6a390967 2023-07-14 stsp }
406 4a1ddfc2 2019-01-12 stsp
407 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
409 4a1ddfc2 2019-01-12 stsp
410 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
411 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 ddcd8544 2019-03-11 stsp struct stat sb;
413 ddcd8544 2019-03-11 stsp err = NULL;
414 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
415 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
416 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
417 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
418 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
419 ddcd8544 2019-03-11 stsp }
420 ddcd8544 2019-03-11 stsp }
421 4a1ddfc2 2019-01-12 stsp free(abspath);
422 68c76935 2019-02-19 stsp return err;
423 68c76935 2019-02-19 stsp }
424 68c76935 2019-02-19 stsp
425 68c76935 2019-02-19 stsp static const struct got_error *
426 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
427 68c76935 2019-02-19 stsp {
428 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
429 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
430 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
431 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
432 68c76935 2019-02-19 stsp
433 68c76935 2019-02-19 stsp *same = 1;
434 68c76935 2019-02-19 stsp
435 230a42bd 2019-05-11 jcs for (;;) {
436 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
438 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
443 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
444 68c76935 2019-02-19 stsp break;
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp if (flen1 == 0) {
447 68c76935 2019-02-19 stsp if (flen2 != 0)
448 68c76935 2019-02-19 stsp *same = 0;
449 68c76935 2019-02-19 stsp break;
450 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
451 68c76935 2019-02-19 stsp if (flen1 != 0)
452 68c76935 2019-02-19 stsp *same = 0;
453 68c76935 2019-02-19 stsp break;
454 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
455 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 68c76935 2019-02-19 stsp *same = 0;
457 68c76935 2019-02-19 stsp break;
458 68c76935 2019-02-19 stsp }
459 68c76935 2019-02-19 stsp } else {
460 68c76935 2019-02-19 stsp *same = 0;
461 68c76935 2019-02-19 stsp break;
462 68c76935 2019-02-19 stsp }
463 68c76935 2019-02-19 stsp }
464 68c76935 2019-02-19 stsp
465 68c76935 2019-02-19 stsp return err;
466 68c76935 2019-02-19 stsp }
467 68c76935 2019-02-19 stsp
468 68c76935 2019-02-19 stsp static const struct got_error *
469 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
470 68c76935 2019-02-19 stsp {
471 68c76935 2019-02-19 stsp struct stat sb;
472 68c76935 2019-02-19 stsp size_t size1, size2;
473 68c76935 2019-02-19 stsp
474 68c76935 2019-02-19 stsp *same = 1;
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
477 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
478 68c76935 2019-02-19 stsp size1 = sb.st_size;
479 68c76935 2019-02-19 stsp
480 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
481 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
482 68c76935 2019-02-19 stsp size2 = sb.st_size;
483 68c76935 2019-02-19 stsp
484 68c76935 2019-02-19 stsp if (size1 != size2) {
485 68c76935 2019-02-19 stsp *same = 0;
486 68c76935 2019-02-19 stsp return NULL;
487 68c76935 2019-02-19 stsp }
488 68c76935 2019-02-19 stsp
489 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
490 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
491 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
492 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
493 68c76935 2019-02-19 stsp
494 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
495 0e039681 2021-11-15 stsp }
496 0e039681 2021-11-15 stsp
497 0e039681 2021-11-15 stsp static const struct got_error *
498 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
499 0e039681 2021-11-15 stsp {
500 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
501 0e039681 2021-11-15 stsp size_t flen;
502 0e039681 2021-11-15 stsp ssize_t outlen;
503 0e039681 2021-11-15 stsp
504 0e039681 2021-11-15 stsp *outsize = 0;
505 0e039681 2021-11-15 stsp
506 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
507 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
508 0e039681 2021-11-15 stsp
509 0e039681 2021-11-15 stsp for (;;) {
510 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
511 0e039681 2021-11-15 stsp if (flen == 0) {
512 0e039681 2021-11-15 stsp if (ferror(f))
513 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
514 0e039681 2021-11-15 stsp if (feof(f))
515 0e039681 2021-11-15 stsp break;
516 0e039681 2021-11-15 stsp }
517 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
518 0e039681 2021-11-15 stsp if (outlen == -1)
519 0e039681 2021-11-15 stsp return got_error_from_errno("write");
520 0e039681 2021-11-15 stsp if (outlen != flen)
521 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
522 0e039681 2021-11-15 stsp *outsize += outlen;
523 0e039681 2021-11-15 stsp }
524 0e039681 2021-11-15 stsp
525 0e039681 2021-11-15 stsp return NULL;
526 0e039681 2021-11-15 stsp }
527 0e039681 2021-11-15 stsp
528 0e039681 2021-11-15 stsp static const struct got_error *
529 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
530 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 0e039681 2021-11-15 stsp const char *ondisk_path)
533 0e039681 2021-11-15 stsp {
534 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
535 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
536 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
540 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
541 0e039681 2021-11-15 stsp
542 0e039681 2021-11-15 stsp *overlapcnt = 0;
543 0e039681 2021-11-15 stsp
544 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 0e039681 2021-11-15 stsp if (err)
546 0e039681 2021-11-15 stsp return err;
547 0e039681 2021-11-15 stsp
548 0e039681 2021-11-15 stsp if (same_content)
549 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
550 0e039681 2021-11-15 stsp
551 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
552 0e039681 2021-11-15 stsp if (err)
553 0e039681 2021-11-15 stsp return err;
554 0e039681 2021-11-15 stsp changed_deriv = !same_content;
555 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
556 0e039681 2021-11-15 stsp if (err)
557 0e039681 2021-11-15 stsp return err;
558 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
559 0e039681 2021-11-15 stsp
560 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
561 0e039681 2021-11-15 stsp *overlapcnt = 1;
562 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
564 0e039681 2021-11-15 stsp goto done;
565 0e039681 2021-11-15 stsp }
566 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
568 0e039681 2021-11-15 stsp goto done;
569 0e039681 2021-11-15 stsp }
570 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
572 0e039681 2021-11-15 stsp goto done;
573 0e039681 2021-11-15 stsp }
574 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 b90054ed 2022-11-01 stsp base_path_orig, "");
576 0e039681 2021-11-15 stsp if (err)
577 0e039681 2021-11-15 stsp goto done;
578 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 b90054ed 2022-11-01 stsp base_path_deriv, "");
580 0e039681 2021-11-15 stsp if (err)
581 0e039681 2021-11-15 stsp goto done;
582 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 b90054ed 2022-11-01 stsp base_path_deriv2, "");
584 0e039681 2021-11-15 stsp if (err)
585 0e039681 2021-11-15 stsp goto done;
586 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 0e039681 2021-11-15 stsp if (err)
588 0e039681 2021-11-15 stsp goto done;
589 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 0e039681 2021-11-15 stsp if (err)
591 0e039681 2021-11-15 stsp goto done;
592 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 0e039681 2021-11-15 stsp if (err)
594 0e039681 2021-11-15 stsp goto done;
595 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
596 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
597 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
598 0e039681 2021-11-15 stsp goto done;
599 0e039681 2021-11-15 stsp }
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 0e039681 2021-11-15 stsp label_deriv ? " " : "",
603 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
604 0e039681 2021-11-15 stsp path_deriv) < 0) {
605 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
606 0e039681 2021-11-15 stsp goto done;
607 0e039681 2021-11-15 stsp }
608 0e039681 2021-11-15 stsp if (size_orig > 0) {
609 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
611 0e039681 2021-11-15 stsp label_orig ? " " : "",
612 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
613 0e039681 2021-11-15 stsp path_orig) < 0) {
614 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
615 0e039681 2021-11-15 stsp goto done;
616 0e039681 2021-11-15 stsp }
617 0e039681 2021-11-15 stsp }
618 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
620 0e039681 2021-11-15 stsp path_deriv2,
621 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
622 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
623 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
624 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
625 0e039681 2021-11-15 stsp goto done;
626 0e039681 2021-11-15 stsp }
627 0e039681 2021-11-15 stsp } else if (changed_deriv)
628 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 0e039681 2021-11-15 stsp else if (changed_deriv2)
630 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 0e039681 2021-11-15 stsp done:
632 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 0e039681 2021-11-15 stsp err == NULL)
634 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
635 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
637 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
639 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
641 0e039681 2021-11-15 stsp free(path_orig);
642 0e039681 2021-11-15 stsp free(path_deriv);
643 0e039681 2021-11-15 stsp free(path_deriv2);
644 0e039681 2021-11-15 stsp free(base_path_orig);
645 0e039681 2021-11-15 stsp free(base_path_deriv);
646 0e039681 2021-11-15 stsp free(base_path_deriv2);
647 0e039681 2021-11-15 stsp return err;
648 6353ad76 2019-02-08 stsp }
649 6353ad76 2019-02-08 stsp
650 6353ad76 2019-02-08 stsp /*
651 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
652 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
653 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
654 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
655 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
656 6353ad76 2019-02-08 stsp */
657 6353ad76 2019-02-08 stsp static const struct got_error *
658 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
661 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
664 6353ad76 2019-02-08 stsp {
665 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
666 6353ad76 2019-02-08 stsp int merged_fd = -1;
667 db590691 2021-06-02 stsp FILE *f_merged = NULL;
668 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
669 234035bc 2019-06-01 stsp int overlapcnt = 0;
670 3524bbf9 2020-10-20 stsp char *parent = NULL;
671 6353ad76 2019-02-08 stsp
672 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
673 234035bc 2019-06-01 stsp
674 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
675 3524bbf9 2020-10-20 stsp if (err)
676 3524bbf9 2020-10-20 stsp return err;
677 af54ae4a 2019-02-19 stsp
678 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
680 3524bbf9 2020-10-20 stsp goto done;
681 3524bbf9 2020-10-20 stsp }
682 af54ae4a 2019-02-19 stsp
683 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 6353ad76 2019-02-08 stsp if (err)
685 6353ad76 2019-02-08 stsp goto done;
686 3b9f0f87 2020-07-23 stsp
687 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 0e039681 2021-11-15 stsp if (err) {
690 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
691 0e039681 2021-11-15 stsp goto done;
692 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 0e039681 2021-11-15 stsp ondisk_path);
695 0e039681 2021-11-15 stsp if (err)
696 0e039681 2021-11-15 stsp goto done;
697 0e039681 2021-11-15 stsp }
698 6353ad76 2019-02-08 stsp
699 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
700 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 1ee397ad 2019-07-12 stsp if (err)
702 1ee397ad 2019-07-12 stsp goto done;
703 6353ad76 2019-02-08 stsp
704 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
706 816dc654 2019-02-16 stsp goto done;
707 68c76935 2019-02-19 stsp }
708 68c76935 2019-02-19 stsp
709 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
710 db590691 2021-06-02 stsp if (f_merged == NULL) {
711 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
712 db590691 2021-06-02 stsp goto done;
713 db590691 2021-06-02 stsp }
714 db590691 2021-06-02 stsp merged_fd = -1;
715 db590691 2021-06-02 stsp
716 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
717 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
718 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
719 db590691 2021-06-02 stsp f_merged);
720 68c76935 2019-02-19 stsp if (err)
721 68c76935 2019-02-19 stsp goto done;
722 816dc654 2019-02-16 stsp }
723 6353ad76 2019-02-08 stsp
724 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
726 70a0c8ec 2019-02-20 stsp goto done;
727 70a0c8ec 2019-02-20 stsp }
728 70a0c8ec 2019-02-20 stsp
729 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
731 230a42bd 2019-05-11 jcs ondisk_path);
732 6353ad76 2019-02-08 stsp goto done;
733 6353ad76 2019-02-08 stsp }
734 6353ad76 2019-02-08 stsp done:
735 fdcb7daf 2019-12-15 stsp if (err) {
736 fdcb7daf 2019-12-15 stsp if (merged_path)
737 fdcb7daf 2019-12-15 stsp unlink(merged_path);
738 3b9f0f87 2020-07-23 stsp }
739 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
741 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
743 6353ad76 2019-02-08 stsp free(merged_path);
744 af54ae4a 2019-02-19 stsp free(base_path);
745 3524bbf9 2020-10-20 stsp free(parent);
746 af57b12a 2020-07-23 stsp return err;
747 af57b12a 2020-07-23 stsp }
748 af57b12a 2020-07-23 stsp
749 af57b12a 2020-07-23 stsp static const struct got_error *
750 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
751 af57b12a 2020-07-23 stsp size_t target_len)
752 af57b12a 2020-07-23 stsp {
753 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
754 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
755 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
756 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
757 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
758 af57b12a 2020-07-23 stsp ondisk_path);
759 af57b12a 2020-07-23 stsp return NULL;
760 af57b12a 2020-07-23 stsp }
761 af57b12a 2020-07-23 stsp
762 af57b12a 2020-07-23 stsp /*
763 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
765 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
766 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
767 993e2a1b 2020-07-23 stsp *
768 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
769 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
770 993e2a1b 2020-07-23 stsp *
771 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
772 993e2a1b 2020-07-23 stsp * has deleted this symlink.
773 11cc08c1 2020-07-23 stsp */
774 11cc08c1 2020-07-23 stsp static const struct got_error *
775 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
776 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
778 11cc08c1 2020-07-23 stsp {
779 11cc08c1 2020-07-23 stsp const struct got_error *err;
780 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 11cc08c1 2020-07-23 stsp FILE *f = NULL;
782 11cc08c1 2020-07-23 stsp
783 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
784 11cc08c1 2020-07-23 stsp if (err)
785 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
786 11cc08c1 2020-07-23 stsp
787 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
788 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
790 11cc08c1 2020-07-23 stsp goto done;
791 11cc08c1 2020-07-23 stsp }
792 11cc08c1 2020-07-23 stsp
793 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 11cc08c1 2020-07-23 stsp if (err)
795 3818e3c4 2020-11-01 naddy goto done;
796 3818e3c4 2020-11-01 naddy
797 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
799 11cc08c1 2020-07-23 stsp goto done;
800 3818e3c4 2020-11-01 naddy }
801 11cc08c1 2020-07-23 stsp
802 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
805 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
806 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
807 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
808 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
809 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
810 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
812 11cc08c1 2020-07-23 stsp goto done;
813 11cc08c1 2020-07-23 stsp }
814 11cc08c1 2020-07-23 stsp
815 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
816 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
817 11cc08c1 2020-07-23 stsp goto done;
818 11cc08c1 2020-07-23 stsp }
819 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
820 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
821 11cc08c1 2020-07-23 stsp goto done;
822 11cc08c1 2020-07-23 stsp }
823 11cc08c1 2020-07-23 stsp done:
824 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
825 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
826 11cc08c1 2020-07-23 stsp free(path);
827 11cc08c1 2020-07-23 stsp free(id_str);
828 11cc08c1 2020-07-23 stsp free(label_deriv);
829 11cc08c1 2020-07-23 stsp return err;
830 11cc08c1 2020-07-23 stsp }
831 d219f183 2020-07-23 stsp
832 d219f183 2020-07-23 stsp /* forward declaration */
833 d219f183 2020-07-23 stsp static const struct got_error *
834 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
836 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
837 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
838 11cc08c1 2020-07-23 stsp
839 11cc08c1 2020-07-23 stsp /*
840 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
841 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
842 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
843 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
844 af57b12a 2020-07-23 stsp */
845 af57b12a 2020-07-23 stsp static const struct got_error *
846 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
847 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
848 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
849 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
851 af57b12a 2020-07-23 stsp {
852 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
853 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
854 af57b12a 2020-07-23 stsp struct stat sb;
855 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
856 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
857 11cc08c1 2020-07-23 stsp int have_local_change = 0;
858 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
859 af57b12a 2020-07-23 stsp
860 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
861 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
862 af57b12a 2020-07-23 stsp
863 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
864 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
865 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
866 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
867 af57b12a 2020-07-23 stsp ondisk_path);
868 af57b12a 2020-07-23 stsp goto done;
869 af57b12a 2020-07-23 stsp }
870 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
871 af57b12a 2020-07-23 stsp
872 526a746f 2020-07-23 stsp if (blob_orig) {
873 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 526a746f 2020-07-23 stsp if (err)
875 526a746f 2020-07-23 stsp goto done;
876 526a746f 2020-07-23 stsp }
877 af57b12a 2020-07-23 stsp
878 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
879 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
880 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 11cc08c1 2020-07-23 stsp have_local_change = 1;
882 11cc08c1 2020-07-23 stsp
883 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
884 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
885 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
886 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
888 11cc08c1 2020-07-23 stsp
889 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
890 11cc08c1 2020-07-23 stsp if (ancestor_target) {
891 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
892 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 11cc08c1 2020-07-23 stsp path);
894 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
895 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
897 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 11cc08c1 2020-07-23 stsp path);
899 11cc08c1 2020-07-23 stsp } else {
900 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
901 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
902 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
903 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
904 11cc08c1 2020-07-23 stsp if (err)
905 11cc08c1 2020-07-23 stsp goto done;
906 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 11cc08c1 2020-07-23 stsp path);
908 11cc08c1 2020-07-23 stsp }
909 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
910 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
911 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
912 af57b12a 2020-07-23 stsp strlen(deriv_target));
913 af57b12a 2020-07-23 stsp if (err)
914 af57b12a 2020-07-23 stsp goto done;
915 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
917 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
918 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
920 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 ea7786be 2020-07-23 stsp path);
922 ea7786be 2020-07-23 stsp } else {
923 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
924 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
925 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
926 ea7786be 2020-07-23 stsp if (err)
927 ea7786be 2020-07-23 stsp goto done;
928 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 ea7786be 2020-07-23 stsp path);
930 ea7786be 2020-07-23 stsp }
931 6353ad76 2019-02-08 stsp }
932 11cc08c1 2020-07-23 stsp
933 af57b12a 2020-07-23 stsp done:
934 af57b12a 2020-07-23 stsp free(ancestor_target);
935 eec2f5a9 2021-06-03 stsp return err;
936 eec2f5a9 2021-06-03 stsp }
937 eec2f5a9 2021-06-03 stsp
938 eec2f5a9 2021-06-03 stsp static const struct got_error *
939 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
940 eec2f5a9 2021-06-03 stsp {
941 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
942 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
943 eec2f5a9 2021-06-03 stsp ssize_t target_len;
944 eec2f5a9 2021-06-03 stsp size_t n;
945 eec2f5a9 2021-06-03 stsp FILE *f;
946 eec2f5a9 2021-06-03 stsp
947 eec2f5a9 2021-06-03 stsp *outfile = NULL;
948 eec2f5a9 2021-06-03 stsp
949 eec2f5a9 2021-06-03 stsp f = got_opentemp();
950 eec2f5a9 2021-06-03 stsp if (f == NULL)
951 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
952 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
954 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
955 eec2f5a9 2021-06-03 stsp goto done;
956 eec2f5a9 2021-06-03 stsp }
957 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
958 eec2f5a9 2021-06-03 stsp if (n != target_len) {
959 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
960 eec2f5a9 2021-06-03 stsp goto done;
961 eec2f5a9 2021-06-03 stsp }
962 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
963 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
964 eec2f5a9 2021-06-03 stsp goto done;
965 eec2f5a9 2021-06-03 stsp }
966 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
967 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
968 eec2f5a9 2021-06-03 stsp goto done;
969 eec2f5a9 2021-06-03 stsp }
970 eec2f5a9 2021-06-03 stsp done:
971 eec2f5a9 2021-06-03 stsp if (err)
972 eec2f5a9 2021-06-03 stsp fclose(f);
973 eec2f5a9 2021-06-03 stsp else
974 eec2f5a9 2021-06-03 stsp *outfile = f;
975 14c901f1 2019-08-08 stsp return err;
976 14c901f1 2019-08-08 stsp }
977 14c901f1 2019-08-08 stsp
978 14c901f1 2019-08-08 stsp /*
979 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
981 14c901f1 2019-08-08 stsp * acts as the second derived version.
982 14c901f1 2019-08-08 stsp */
983 14c901f1 2019-08-08 stsp static const struct got_error *
984 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
986 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
987 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
988 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
990 14c901f1 2019-08-08 stsp {
991 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
992 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
994 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
996 14c901f1 2019-08-08 stsp
997 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
998 14c901f1 2019-08-08 stsp
999 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1000 ed6b5030 2020-10-20 stsp if (err)
1001 ed6b5030 2020-10-20 stsp return err;
1002 14c901f1 2019-08-08 stsp
1003 67a66647 2021-05-31 stsp if (blob_orig) {
1004 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 67a66647 2021-05-31 stsp parent) == -1) {
1006 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1007 67a66647 2021-05-31 stsp base_path = NULL;
1008 67a66647 2021-05-31 stsp goto done;
1009 67a66647 2021-05-31 stsp }
1010 67a66647 2021-05-31 stsp
1011 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 b90054ed 2022-11-01 stsp base_path, "");
1013 67a66647 2021-05-31 stsp if (err)
1014 67a66647 2021-05-31 stsp goto done;
1015 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 67a66647 2021-05-31 stsp blob_orig);
1017 67a66647 2021-05-31 stsp if (err)
1018 67a66647 2021-05-31 stsp goto done;
1019 67a66647 2021-05-31 stsp free(base_path);
1020 db590691 2021-06-02 stsp } else {
1021 db590691 2021-06-02 stsp /*
1022 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1023 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1024 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1025 db590691 2021-06-02 stsp */
1026 db590691 2021-06-02 stsp f_orig = got_opentemp();
1027 db590691 2021-06-02 stsp if (f_orig == NULL) {
1028 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1029 db590691 2021-06-02 stsp goto done;
1030 db590691 2021-06-02 stsp }
1031 67a66647 2021-05-31 stsp }
1032 67a66647 2021-05-31 stsp
1033 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp base_path = NULL;
1036 14c901f1 2019-08-08 stsp goto done;
1037 14c901f1 2019-08-08 stsp }
1038 14c901f1 2019-08-08 stsp
1039 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 14c901f1 2019-08-08 stsp if (err)
1041 14c901f1 2019-08-08 stsp goto done;
1042 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 14c901f1 2019-08-08 stsp blob_deriv);
1044 14c901f1 2019-08-08 stsp if (err)
1045 14c901f1 2019-08-08 stsp goto done;
1046 14c901f1 2019-08-08 stsp
1047 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 14c901f1 2019-08-08 stsp if (err)
1049 14c901f1 2019-08-08 stsp goto done;
1050 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1051 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1053 14c901f1 2019-08-08 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp }
1055 eec2f5a9 2021-06-03 stsp
1056 5e91dae4 2022-08-30 stsp /*
1057 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1058 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1059 eec2f5a9 2021-06-03 stsp */
1060 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1061 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 eec2f5a9 2021-06-03 stsp if (err)
1063 eec2f5a9 2021-06-03 stsp goto done;
1064 eec2f5a9 2021-06-03 stsp } else {
1065 eec2f5a9 2021-06-03 stsp int fd;
1066 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1069 eec2f5a9 2021-06-03 stsp goto done;
1070 eec2f5a9 2021-06-03 stsp }
1071 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1072 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1073 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1074 eec2f5a9 2021-06-03 stsp close(fd);
1075 eec2f5a9 2021-06-03 stsp goto done;
1076 eec2f5a9 2021-06-03 stsp }
1077 14c901f1 2019-08-08 stsp }
1078 14c901f1 2019-08-08 stsp
1079 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 14c901f1 2019-08-08 stsp done:
1083 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1085 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1087 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1089 14c901f1 2019-08-08 stsp free(base_path);
1090 67a66647 2021-05-31 stsp if (blob_orig_path) {
1091 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1092 67a66647 2021-05-31 stsp free(blob_orig_path);
1093 67a66647 2021-05-31 stsp }
1094 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1095 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1096 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1097 14c901f1 2019-08-08 stsp }
1098 6353ad76 2019-02-08 stsp free(id_str);
1099 818c7501 2019-07-11 stsp free(label_deriv);
1100 ed6b5030 2020-10-20 stsp free(parent);
1101 4a1ddfc2 2019-01-12 stsp return err;
1102 4a1ddfc2 2019-01-12 stsp }
1103 4a1ddfc2 2019-01-12 stsp
1104 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1105 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 ef623445 2023-09-16 op int wt_fd, const char *path, struct got_object_id *blob_id,
1108 ef623445 2023-09-16 op int update_timestamps)
1109 13d9040b 2019-03-27 stsp {
1110 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1111 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1112 13d9040b 2019-03-27 stsp
1113 65b05cec 2020-07-23 stsp *new_iep = NULL;
1114 65b05cec 2020-07-23 stsp
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1116 054041d0 2020-06-24 stsp if (err)
1117 054041d0 2020-06-24 stsp return err;
1118 054041d0 2020-06-24 stsp
1119 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 ef623445 2023-09-16 op blob_id->sha1, base_commit_id->sha1, update_timestamps);
1121 054041d0 2020-06-24 stsp if (err)
1122 054041d0 2020-06-24 stsp goto done;
1123 054041d0 2020-06-24 stsp
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1125 054041d0 2020-06-24 stsp done:
1126 054041d0 2020-06-24 stsp if (err)
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1128 65b05cec 2020-07-23 stsp else
1129 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1130 13d9040b 2019-03-27 stsp return err;
1131 1ebedb77 2019-10-19 stsp }
1132 1ebedb77 2019-10-19 stsp
1133 1ebedb77 2019-10-19 stsp static mode_t
1134 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1135 1ebedb77 2019-10-19 stsp {
1136 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1137 1ebedb77 2019-10-19 stsp
1138 1ebedb77 2019-10-19 stsp if (executable) {
1139 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1142 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1143 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1144 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1145 1ebedb77 2019-10-19 stsp }
1146 1ebedb77 2019-10-19 stsp
1147 b2b3fce1 2022-10-29 op return st_mode;
1148 13d9040b 2019-03-27 stsp }
1149 13d9040b 2019-03-27 stsp
1150 8ba819a3 2020-07-23 stsp /* forward declaration */
1151 13d9040b 2019-03-27 stsp static const struct got_error *
1152 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1154 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1155 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1156 ef623445 2023-09-16 op int path_is_unversioned, int *update_timestamps,
1157 ef623445 2023-09-16 op struct got_repository *repo,
1158 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 8ba819a3 2020-07-23 stsp
1160 5a1fbc73 2020-07-23 stsp /*
1161 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1162 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1163 5a1fbc73 2020-07-23 stsp */
1164 8ba819a3 2020-07-23 stsp static const struct got_error *
1165 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1167 5a1fbc73 2020-07-23 stsp {
1168 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1169 5a1fbc73 2020-07-23 stsp ssize_t elen;
1170 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1171 5a1fbc73 2020-07-23 stsp int fd;
1172 5a1fbc73 2020-07-23 stsp
1173 c6e8a826 2021-04-05 stsp *did_something = 0;
1174 c6e8a826 2021-04-05 stsp
1175 5a1fbc73 2020-07-23 stsp /*
1176 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1177 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1178 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1179 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1180 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1181 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1182 5a1fbc73 2020-07-23 stsp */
1183 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1184 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1185 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1189 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 5a1fbc73 2020-07-23 stsp if (elen == -1)
1191 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp
1193 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1194 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1195 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1196 5a1fbc73 2020-07-23 stsp }
1197 5a1fbc73 2020-07-23 stsp
1198 c6e8a826 2021-04-05 stsp *did_something = 1;
1199 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1200 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1201 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1202 5a1fbc73 2020-07-23 stsp return err;
1203 3c1ec782 2020-07-23 stsp }
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp static const struct got_error *
1206 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 df6221c7 2023-07-19 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 df6221c7 2023-07-19 stsp const char *meta_dir)
1209 3c1ec782 2020-07-23 stsp {
1210 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1211 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1212 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1213 3c1ec782 2020-07-23 stsp
1214 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1215 3c1ec782 2020-07-23 stsp
1216 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1218 3c1ec782 2020-07-23 stsp return NULL;
1219 3c1ec782 2020-07-23 stsp }
1220 3c1ec782 2020-07-23 stsp
1221 3c1ec782 2020-07-23 stsp /*
1222 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1223 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1224 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1225 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1226 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1227 3c1ec782 2020-07-23 stsp */
1228 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1229 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1230 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1231 ce031e9e 2020-10-20 stsp if (err)
1232 ce031e9e 2020-10-20 stsp return err;
1233 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1234 ce031e9e 2020-10-20 stsp free(parent);
1235 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1236 ce031e9e 2020-10-20 stsp }
1237 ce031e9e 2020-10-20 stsp free(parent);
1238 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1239 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 3c1ec782 2020-07-23 stsp free(abspath);
1241 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp }
1243 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1244 3c1ec782 2020-07-23 stsp free(abspath);
1245 3c1ec782 2020-07-23 stsp if (err)
1246 3c1ec782 2020-07-23 stsp return err;
1247 3c1ec782 2020-07-23 stsp } else {
1248 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1249 3c1ec782 2020-07-23 stsp if (err)
1250 3c1ec782 2020-07-23 stsp return err;
1251 3c1ec782 2020-07-23 stsp }
1252 3c1ec782 2020-07-23 stsp
1253 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1254 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 3c1ec782 2020-07-23 stsp }
1258 3c1ec782 2020-07-23 stsp
1259 df6221c7 2023-07-19 stsp /* Do not allow symlinks pointing into the meta directory. */
1260 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1262 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1264 3c1ec782 2020-07-23 stsp
1265 3c1ec782 2020-07-23 stsp free(path_got);
1266 3c1ec782 2020-07-23 stsp return NULL;
1267 5a1fbc73 2020-07-23 stsp }
1268 5a1fbc73 2020-07-23 stsp
1269 5a1fbc73 2020-07-23 stsp static const struct got_error *
1270 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1273 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1274 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1275 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1276 9d31a1d8 2018-03-11 stsp {
1277 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1278 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1279 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1280 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1282 8ba819a3 2020-07-23 stsp
1283 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1284 2e1fa222 2020-07-23 stsp
1285 3c29341b 2022-07-21 florian /*
1286 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1287 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1288 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1289 8ba819a3 2020-07-23 stsp * in the blob object.
1290 8ba819a3 2020-07-23 stsp */
1291 8ba819a3 2020-07-23 stsp do {
1292 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1293 538b6881 2022-07-21 florian if (err)
1294 538b6881 2022-07-21 florian return err;
1295 538b6881 2022-07-21 florian
1296 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1297 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1298 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1299 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1300 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1301 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1303 ef623445 2023-09-16 op 1, path_is_unversioned, NULL, repo, progress_cb,
1304 3b9f0f87 2020-07-23 stsp progress_arg);
1305 8ba819a3 2020-07-23 stsp }
1306 8ba819a3 2020-07-23 stsp if (len > 0) {
1307 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1308 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1309 8ba819a3 2020-07-23 stsp len - hdrlen);
1310 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1311 8ba819a3 2020-07-23 stsp hdrlen = 0;
1312 8ba819a3 2020-07-23 stsp }
1313 8ba819a3 2020-07-23 stsp } while (len != 0);
1314 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1315 8ba819a3 2020-07-23 stsp
1316 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 df6221c7 2023-07-19 stsp ondisk_path, worktree->root_path, worktree->meta_dir);
1318 3c1ec782 2020-07-23 stsp if (err)
1319 3c1ec782 2020-07-23 stsp return err;
1320 8ba819a3 2020-07-23 stsp
1321 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1322 906c123b 2020-07-23 stsp /* install as a regular file */
1323 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1324 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1325 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1327 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
1328 ef623445 2023-09-16 op progress_cb, progress_arg);
1329 3c29341b 2022-07-21 florian return err;
1330 906c123b 2020-07-23 stsp }
1331 906c123b 2020-07-23 stsp
1332 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1333 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1334 c6e8a826 2021-04-05 stsp int symlink_replaced;
1335 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1336 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1337 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1338 3c29341b 2022-07-21 florian return err;
1339 c90c8ce3 2020-07-23 stsp }
1340 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1341 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1342 5a1fbc73 2020-07-23 stsp if (err)
1343 3c29341b 2022-07-21 florian return err;
1344 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1345 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1346 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1347 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1348 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1349 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1350 c6e8a826 2021-04-05 stsp } else {
1351 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1352 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1353 c6e8a826 2021-04-05 stsp }
1354 f35fa46a 2020-07-23 stsp }
1355 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1356 f35fa46a 2020-07-23 stsp }
1357 f35fa46a 2020-07-23 stsp
1358 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1359 f4994adc 2020-10-20 stsp char *parent;
1360 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1361 f4994adc 2020-10-20 stsp if (err)
1362 3c29341b 2022-07-21 florian return err;
1363 6a390967 2023-07-14 stsp err = got_path_mkdir(parent);
1364 f4994adc 2020-10-20 stsp free(parent);
1365 f35fa46a 2020-07-23 stsp if (err)
1366 3c29341b 2022-07-21 florian return err;
1367 f35fa46a 2020-07-23 stsp /*
1368 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1369 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1370 f35fa46a 2020-07-23 stsp */
1371 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1372 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1373 6a390967 2023-07-14 stsp if (progress_cb) {
1374 6a390967 2023-07-14 stsp err = (*progress_cb)(progress_arg,
1375 6a390967 2023-07-14 stsp reverting_versioned_file ?
1376 6a390967 2023-07-14 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD,
1377 6a390967 2023-07-14 stsp path);
1378 6a390967 2023-07-14 stsp }
1379 3c29341b 2022-07-21 florian return err;
1380 f35fa46a 2020-07-23 stsp }
1381 f35fa46a 2020-07-23 stsp }
1382 f35fa46a 2020-07-23 stsp
1383 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1384 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1385 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1386 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1387 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1388 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1389 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1391 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
1392 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1393 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1394 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1395 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1396 8ba819a3 2020-07-23 stsp } else {
1397 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1398 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1399 8ba819a3 2020-07-23 stsp }
1400 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1401 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 8ba819a3 2020-07-23 stsp return err;
1404 8ba819a3 2020-07-23 stsp }
1405 8ba819a3 2020-07-23 stsp
1406 8ba819a3 2020-07-23 stsp static const struct got_error *
1407 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1409 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1410 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1411 ef623445 2023-09-16 op int path_is_unversioned, int *update_timestamps,
1412 ef623445 2023-09-16 op struct got_repository *repo,
1413 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1414 8ba819a3 2020-07-23 stsp {
1415 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1416 507dc3bb 2018-12-29 stsp int fd = -1;
1417 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1418 507dc3bb 2018-12-29 stsp int update = 0;
1419 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1420 b2b3fce1 2022-10-29 op mode_t mode;
1421 ef623445 2023-09-16 op
1422 ef623445 2023-09-16 op if (update_timestamps)
1423 ef623445 2023-09-16 op *update_timestamps = 1;
1424 8ba819a3 2020-07-23 stsp
1425 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1427 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1428 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1429 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1430 f5375317 2020-10-20 stsp char *parent;
1431 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1432 f5375317 2020-10-20 stsp if (err)
1433 f5375317 2020-10-20 stsp return err;
1434 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1435 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1437 f5375317 2020-10-20 stsp free(parent);
1438 21908da4 2019-01-13 stsp if (err)
1439 21908da4 2019-01-13 stsp return err;
1440 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1441 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1442 b2b3fce1 2022-10-29 op mode);
1443 21908da4 2019-01-13 stsp if (fd == -1)
1444 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1445 230a42bd 2019-05-11 jcs ondisk_path);
1446 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1447 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1448 ef623445 2023-09-16 op if (update_timestamps)
1449 ef623445 2023-09-16 op *update_timestamps = 0;
1450 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1451 ef623445 2023-09-16 op GOT_STATUS_EXISTS, path);
1452 3b9f0f87 2020-07-23 stsp goto done;
1453 3b9f0f87 2020-07-23 stsp }
1454 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1457 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1458 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1459 507dc3bb 2018-12-29 stsp goto done;
1460 d70b8e30 2018-12-27 stsp } else {
1461 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1462 b90054ed 2022-11-01 stsp ondisk_path, "");
1463 507dc3bb 2018-12-29 stsp if (err)
1464 507dc3bb 2018-12-29 stsp goto done;
1465 507dc3bb 2018-12-29 stsp update = 1;
1466 b2b3fce1 2022-10-29 op
1467 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1468 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1469 b2b3fce1 2022-10-29 op tmppath);
1470 b2b3fce1 2022-10-29 op goto done;
1471 b2b3fce1 2022-10-29 op }
1472 9d31a1d8 2018-03-11 stsp }
1473 507dc3bb 2018-12-29 stsp } else
1474 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1475 3818e3c4 2020-11-01 naddy }
1476 3818e3c4 2020-11-01 naddy
1477 bd6aa359 2020-07-23 stsp if (progress_cb) {
1478 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 bd6aa359 2020-07-23 stsp path);
1481 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1483 bd6aa359 2020-07-23 stsp path);
1484 bd6aa359 2020-07-23 stsp else
1485 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1486 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1487 bd6aa359 2020-07-23 stsp if (err)
1488 bd6aa359 2020-07-23 stsp goto done;
1489 bd6aa359 2020-07-23 stsp }
1490 d7b62c98 2018-12-27 stsp
1491 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1492 9d31a1d8 2018-03-11 stsp do {
1493 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1495 9d31a1d8 2018-03-11 stsp if (err)
1496 9d31a1d8 2018-03-11 stsp break;
1497 9d31a1d8 2018-03-11 stsp if (len > 0) {
1498 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1499 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1501 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1502 b87c6f83 2018-12-24 stsp goto done;
1503 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1504 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1505 b87c6f83 2018-12-24 stsp goto done;
1506 9d31a1d8 2018-03-11 stsp }
1507 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1508 9d31a1d8 2018-03-11 stsp }
1509 9d31a1d8 2018-03-11 stsp } while (len != 0);
1510 9d31a1d8 2018-03-11 stsp
1511 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1513 816dc654 2019-02-16 stsp goto done;
1514 816dc654 2019-02-16 stsp }
1515 9d31a1d8 2018-03-11 stsp
1516 507dc3bb 2018-12-29 stsp if (update) {
1517 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1519 f6d8c0ac 2020-11-09 stsp goto done;
1520 f6d8c0ac 2020-11-09 stsp }
1521 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1522 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1523 230a42bd 2019-05-11 jcs ondisk_path);
1524 68ed9ba5 2019-02-10 stsp goto done;
1525 68ed9ba5 2019-02-10 stsp }
1526 63df146d 2020-11-09 stsp free(tmppath);
1527 63df146d 2020-11-09 stsp tmppath = NULL;
1528 507dc3bb 2018-12-29 stsp }
1529 507dc3bb 2018-12-29 stsp
1530 9d31a1d8 2018-03-11 stsp done:
1531 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1532 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1533 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1535 507dc3bb 2018-12-29 stsp free(tmppath);
1536 6353ad76 2019-02-08 stsp return err;
1537 6353ad76 2019-02-08 stsp }
1538 6353ad76 2019-02-08 stsp
1539 12383673 2023-02-18 mark /*
1540 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1542 12383673 2023-02-18 mark */
1543 6353ad76 2019-02-08 stsp static const struct got_error *
1544 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1545 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1546 12383673 2023-02-18 mark FILE *ondisk_file)
1547 7154f6ce 2019-03-27 stsp {
1548 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1549 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1550 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1552 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1553 7154f6ce 2019-03-27 stsp };
1554 d952957d 2023-02-19 mark FILE *f1 = NULL;
1555 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1556 d952957d 2023-02-19 mark struct diff_result *r;
1557 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1558 9bdd68dd 2020-01-02 naddy char *line = NULL;
1559 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1560 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1561 7154f6ce 2019-03-27 stsp
1562 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1563 d952957d 2023-02-19 mark return NULL;
1564 12383673 2023-02-18 mark
1565 d952957d 2023-02-19 mark f1 = got_opentemp();
1566 d952957d 2023-02-19 mark if (f1 == NULL)
1567 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1568 12383673 2023-02-18 mark
1569 d952957d 2023-02-19 mark if (blob) {
1570 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1571 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1572 12383673 2023-02-18 mark if (err)
1573 12383673 2023-02-18 mark goto done;
1574 d952957d 2023-02-19 mark }
1575 12383673 2023-02-18 mark
1576 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1578 d952957d 2023-02-19 mark if (err)
1579 d952957d 2023-02-19 mark goto done;
1580 d952957d 2023-02-19 mark
1581 d952957d 2023-02-19 mark r = diffreg_result->result;
1582 d952957d 2023-02-19 mark
1583 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 d952957d 2023-02-19 mark struct diff_chunk *c;
1585 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1586 7783f73c 2023-02-20 mark off_t pos;
1587 d952957d 2023-02-19 mark
1588 d952957d 2023-02-19 mark /*
1589 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1590 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1591 d952957d 2023-02-19 mark */
1592 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1593 d952957d 2023-02-19 mark
1594 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1595 d952957d 2023-02-19 mark nchunks_parsed = 1;
1596 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1597 7154f6ce 2019-03-27 stsp }
1598 7154f6ce 2019-03-27 stsp
1599 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1600 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 7783f73c 2023-02-20 mark goto done;
1603 7154f6ce 2019-03-27 stsp }
1604 d952957d 2023-02-19 mark
1605 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 7783f73c 2023-02-20 mark ln = cc.right.start;
1607 7783f73c 2023-02-20 mark
1608 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1609 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1610 d952957d 2023-02-19 mark if (linelen == -1) {
1611 d952957d 2023-02-19 mark if (feof(ondisk_file))
1612 d952957d 2023-02-19 mark break;
1613 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 d952957d 2023-02-19 mark break;
1615 d952957d 2023-02-19 mark }
1616 d952957d 2023-02-19 mark
1617 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1618 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1619 d952957d 2023-02-19 mark if (strcmp(markers[i],
1620 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1622 d952957d 2023-02-19 mark goto done;
1623 d952957d 2023-02-19 mark } else
1624 d952957d 2023-02-19 mark i++;
1625 d952957d 2023-02-19 mark }
1626 d952957d 2023-02-19 mark ++ln;
1627 d952957d 2023-02-19 mark }
1628 7154f6ce 2019-03-27 stsp }
1629 12383673 2023-02-18 mark
1630 12383673 2023-02-18 mark done:
1631 9bdd68dd 2020-01-02 naddy free(line);
1632 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1634 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1635 d952957d 2023-02-19 mark if (err == NULL)
1636 d952957d 2023-02-19 mark err = free_err;
1637 7154f6ce 2019-03-27 stsp
1638 7154f6ce 2019-03-27 stsp return err;
1639 e2b1e152 2019-07-13 stsp }
1640 e2b1e152 2019-07-13 stsp
1641 e2b1e152 2019-07-13 stsp static int
1642 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1643 1ebedb77 2019-10-19 stsp {
1644 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1646 1ebedb77 2019-10-19 stsp }
1647 1ebedb77 2019-10-19 stsp
1648 1ebedb77 2019-10-19 stsp static int
1649 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1650 e2b1e152 2019-07-13 stsp {
1651 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1656 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1657 c363b2c1 2019-08-03 stsp }
1658 c363b2c1 2019-08-03 stsp
1659 c363b2c1 2019-08-03 stsp static unsigned char
1660 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1661 c363b2c1 2019-08-03 stsp {
1662 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1663 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1664 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1665 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1666 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1667 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1668 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1669 c363b2c1 2019-08-03 stsp default:
1670 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1671 a919d5c4 2020-07-23 stsp }
1672 a919d5c4 2020-07-23 stsp }
1673 a919d5c4 2020-07-23 stsp
1674 a919d5c4 2020-07-23 stsp static const struct got_error *
1675 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1676 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1677 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1678 a919d5c4 2020-07-23 stsp {
1679 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1680 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1681 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1682 a919d5c4 2020-07-23 stsp ssize_t elen;
1683 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1684 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1686 a919d5c4 2020-07-23 stsp
1687 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1688 a919d5c4 2020-07-23 stsp
1689 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1690 a919d5c4 2020-07-23 stsp do {
1691 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1692 a919d5c4 2020-07-23 stsp if (err)
1693 a919d5c4 2020-07-23 stsp return err;
1694 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1695 a919d5c4 2020-07-23 stsp /*
1696 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1697 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1698 a919d5c4 2020-07-23 stsp */
1699 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1700 a919d5c4 2020-07-23 stsp }
1701 a919d5c4 2020-07-23 stsp if (len > 0) {
1702 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1703 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1704 a919d5c4 2020-07-23 stsp len - hdrlen);
1705 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1706 a919d5c4 2020-07-23 stsp hdrlen = 0;
1707 a919d5c4 2020-07-23 stsp }
1708 a919d5c4 2020-07-23 stsp } while (len != 0);
1709 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1710 a919d5c4 2020-07-23 stsp
1711 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1712 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1713 a919d5c4 2020-07-23 stsp if (elen == -1)
1714 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1715 a919d5c4 2020-07-23 stsp } else {
1716 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1717 a919d5c4 2020-07-23 stsp if (elen == -1)
1718 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1719 c363b2c1 2019-08-03 stsp }
1720 a919d5c4 2020-07-23 stsp
1721 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1723 a919d5c4 2020-07-23 stsp
1724 a919d5c4 2020-07-23 stsp return NULL;
1725 7154f6ce 2019-03-27 stsp }
1726 7154f6ce 2019-03-27 stsp
1727 7154f6ce 2019-03-27 stsp static const struct got_error *
1728 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1729 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1730 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1731 6353ad76 2019-02-08 stsp {
1732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1733 6353ad76 2019-02-08 stsp struct got_object_id id;
1734 6353ad76 2019-02-08 stsp size_t hdrlen;
1735 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1736 6353ad76 2019-02-08 stsp FILE *f = NULL;
1737 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1738 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1739 6353ad76 2019-02-08 stsp size_t flen, blen;
1740 2c4740ad 2023-02-12 mark unsigned char staged_status;
1741 6353ad76 2019-02-08 stsp
1742 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1743 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1744 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1745 6353ad76 2019-02-08 stsp
1746 7f91a133 2019-12-13 stsp /*
1747 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1748 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1749 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1750 7f91a133 2019-12-13 stsp */
1751 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1752 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1754 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1755 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1756 882ef1b9 2019-12-13 stsp else
1757 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1758 882ef1b9 2019-12-13 stsp goto done;
1759 882ef1b9 2019-12-13 stsp }
1760 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1761 3d35a492 2019-12-13 stsp goto done;
1762 3d35a492 2019-12-13 stsp }
1763 7f91a133 2019-12-13 stsp } else {
1764 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1766 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1767 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1768 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1770 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1771 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1773 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1774 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1775 3d35a492 2019-12-13 stsp else
1776 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1777 3d35a492 2019-12-13 stsp goto done;
1778 3d35a492 2019-12-13 stsp }
1779 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1780 1338848f 2019-12-13 stsp goto done;
1781 a378724f 2019-02-10 stsp }
1782 a378724f 2019-02-10 stsp }
1783 6353ad76 2019-02-08 stsp
1784 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1786 1338848f 2019-12-13 stsp goto done;
1787 3f148bc6 2019-07-27 stsp }
1788 efdd40df 2019-07-27 stsp
1789 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1791 1338848f 2019-12-13 stsp goto done;
1792 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1793 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1794 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1795 1338848f 2019-12-13 stsp goto done;
1796 d00136be 2019-03-26 stsp }
1797 b8f41171 2019-02-10 stsp
1798 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1799 f179e66d 2020-07-23 stsp goto done;
1800 f179e66d 2020-07-23 stsp
1801 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1802 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1804 1338848f 2019-12-13 stsp goto done;
1805 f179e66d 2020-07-23 stsp }
1806 6353ad76 2019-02-08 stsp
1807 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1808 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1809 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1810 c363b2c1 2019-08-03 stsp else
1811 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1812 c363b2c1 2019-08-03 stsp
1813 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1814 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1815 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1816 eb81bc23 2022-06-28 tracey goto done;
1817 eb81bc23 2022-06-28 tracey }
1818 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1819 6353ad76 2019-02-08 stsp if (err)
1820 1338848f 2019-12-13 stsp goto done;
1821 6353ad76 2019-02-08 stsp
1822 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1823 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1824 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1825 a919d5c4 2020-07-23 stsp goto done;
1826 a919d5c4 2020-07-23 stsp }
1827 a919d5c4 2020-07-23 stsp
1828 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1829 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1830 ab0d4361 2019-12-13 stsp if (fd == -1) {
1831 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1832 ab0d4361 2019-12-13 stsp goto done;
1833 ab0d4361 2019-12-13 stsp }
1834 3d35a492 2019-12-13 stsp }
1835 3d35a492 2019-12-13 stsp
1836 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1837 6353ad76 2019-02-08 stsp if (f == NULL) {
1838 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1839 6353ad76 2019-02-08 stsp goto done;
1840 6353ad76 2019-02-08 stsp }
1841 1338848f 2019-12-13 stsp fd = -1;
1842 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1843 656b1f76 2019-05-11 jcs for (;;) {
1844 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1846 6353ad76 2019-02-08 stsp if (err)
1847 7154f6ce 2019-03-27 stsp goto done;
1848 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1849 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1851 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1852 7154f6ce 2019-03-27 stsp goto done;
1853 80c5c120 2019-02-19 stsp }
1854 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1855 6353ad76 2019-02-08 stsp if (flen != 0)
1856 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1857 6353ad76 2019-02-08 stsp break;
1858 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1859 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1860 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1861 6353ad76 2019-02-08 stsp break;
1862 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1863 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1864 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1866 6353ad76 2019-02-08 stsp break;
1867 6353ad76 2019-02-08 stsp }
1868 6353ad76 2019-02-08 stsp } else {
1869 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1870 6353ad76 2019-02-08 stsp break;
1871 6353ad76 2019-02-08 stsp }
1872 6353ad76 2019-02-08 stsp hdrlen = 0;
1873 6353ad76 2019-02-08 stsp }
1874 7154f6ce 2019-03-27 stsp
1875 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1876 7154f6ce 2019-03-27 stsp rewind(f);
1877 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1878 12383673 2023-02-18 mark sb, f);
1879 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1880 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1881 6353ad76 2019-02-08 stsp done:
1882 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1884 6353ad76 2019-02-08 stsp if (blob)
1885 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1886 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1887 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1888 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1889 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1890 9d31a1d8 2018-03-11 stsp return err;
1891 e2b1e152 2019-07-13 stsp }
1892 e2b1e152 2019-07-13 stsp
1893 e2b1e152 2019-07-13 stsp /*
1894 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1895 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1896 e2b1e152 2019-07-13 stsp */
1897 e2b1e152 2019-07-13 stsp static const struct got_error *
1898 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1900 e2b1e152 2019-07-13 stsp {
1901 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1903 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1904 e2b1e152 2019-07-13 stsp
1905 e2b1e152 2019-07-13 stsp return NULL;
1906 9d31a1d8 2018-03-11 stsp }
1907 9d31a1d8 2018-03-11 stsp
1908 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1909 07fa9365 2023-03-10 stsp
1910 9d31a1d8 2018-03-11 stsp static const struct got_error *
1911 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1912 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1914 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1915 0584f854 2019-04-06 stsp void *progress_arg)
1916 9d31a1d8 2018-03-11 stsp {
1917 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1918 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1919 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1920 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1921 b8f41171 2019-02-10 stsp struct stat sb;
1922 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1923 8c8d22ba 2024-02-13 op int update_timestamps = 0;
1924 9d31a1d8 2018-03-11 stsp
1925 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1927 6353ad76 2019-02-08 stsp
1928 abb4604f 2019-07-27 stsp if (ie) {
1929 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1931 a76c42e6 2019-08-03 stsp goto done;
1932 a76c42e6 2019-08-03 stsp }
1933 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1934 7f91a133 2019-12-13 stsp repo);
1935 abb4604f 2019-07-27 stsp if (err)
1936 abb4604f 2019-07-27 stsp goto done;
1937 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1939 c90c8ce3 2020-07-23 stsp } else {
1940 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1941 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1942 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1943 f6764181 2021-09-24 stsp ondisk_path);
1944 f6764181 2021-09-24 stsp goto done;
1945 f6764181 2021-09-24 stsp }
1946 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1948 f6764181 2021-09-24 stsp } else {
1949 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1951 f6764181 2021-09-24 stsp else
1952 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1953 f6764181 2021-09-24 stsp }
1954 c90c8ce3 2020-07-23 stsp }
1955 6353ad76 2019-02-08 stsp
1956 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1957 2c41dce7 2021-06-27 stsp if (ie)
1958 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1959 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1960 b8f41171 2019-02-10 stsp goto done;
1961 b8f41171 2019-02-10 stsp }
1962 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1963 a769b60b 2021-06-27 stsp if (ie)
1964 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1965 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1966 5036ab18 2020-04-18 stsp path);
1967 5036ab18 2020-04-18 stsp goto done;
1968 07fa9365 2023-03-10 stsp }
1969 07fa9365 2023-03-10 stsp
1970 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1972 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1973 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1974 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1975 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1976 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1977 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1978 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1979 07fa9365 2023-03-10 stsp } else if (ie) {
1980 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1981 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1982 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1983 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1984 07fa9365 2023-03-10 stsp ie->path);
1985 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1986 07fa9365 2023-03-10 stsp errno == ENOENT))
1987 07fa9365 2023-03-10 stsp goto done;
1988 07fa9365 2023-03-10 stsp }
1989 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1990 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1991 07fa9365 2023-03-10 stsp ie->path);
1992 07fa9365 2023-03-10 stsp }
1993 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1994 5036ab18 2020-04-18 stsp }
1995 b8f41171 2019-02-10 stsp
1996 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1998 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1999 c6e8a826 2021-04-05 stsp /*
2000 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
2001 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
2002 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
2003 c6e8a826 2021-04-05 stsp * updating contents of this file.
2004 c6e8a826 2021-04-05 stsp */
2005 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
2006 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
2008 c6e8a826 2021-04-05 stsp /* Same commit. */
2009 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2010 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2011 e2b1e152 2019-07-13 stsp if (err)
2012 e2b1e152 2019-07-13 stsp goto done;
2013 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2014 b8f41171 2019-02-10 stsp path);
2015 6353ad76 2019-02-08 stsp goto done;
2016 a378724f 2019-02-10 stsp }
2017 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2018 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2019 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2020 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2021 fd785a9a 2023-04-12 stsp if (got_fileindex_entry_has_commit(ie)) {
2022 fd785a9a 2023-04-12 stsp /* Update the base commit ID of this file. */
2023 fd785a9a 2023-04-12 stsp memcpy(ie->commit_sha1,
2024 fd785a9a 2023-04-12 stsp worktree->base_commit_id->sha1,
2025 fd785a9a 2023-04-12 stsp sizeof(ie->commit_sha1));
2026 fd785a9a 2023-04-12 stsp }
2027 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2028 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2029 0f58026f 2021-04-05 stsp if (err)
2030 0f58026f 2021-04-05 stsp goto done;
2031 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2032 0f58026f 2021-04-05 stsp path);
2033 b8f41171 2019-02-10 stsp goto done;
2034 e2b1e152 2019-07-13 stsp }
2035 9d31a1d8 2018-03-11 stsp }
2036 9d31a1d8 2018-03-11 stsp
2037 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2038 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2039 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2040 eb81bc23 2022-06-28 tracey goto done;
2041 eb81bc23 2022-06-28 tracey }
2042 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2043 8da9e5f4 2019-01-12 stsp if (err)
2044 6353ad76 2019-02-08 stsp goto done;
2045 512f0d0e 2019-01-02 stsp
2046 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2048 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2049 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2050 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2051 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2052 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2053 eb81bc23 2022-06-28 tracey goto done;
2054 eb81bc23 2022-06-28 tracey }
2055 234035bc 2019-06-01 stsp struct got_object_id id2;
2056 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2057 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2058 eb81bc23 2022-06-28 tracey fd2);
2059 234035bc 2019-06-01 stsp if (err)
2060 f69721c3 2019-10-21 stsp goto done;
2061 f69721c3 2019-10-21 stsp }
2062 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2063 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2064 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2065 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2066 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2067 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2068 f69721c3 2019-10-21 stsp goto done;
2069 f69721c3 2019-10-21 stsp }
2070 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2071 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2072 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2073 234035bc 2019-06-01 stsp goto done;
2074 f69721c3 2019-10-21 stsp }
2075 234035bc 2019-06-01 stsp }
2076 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2077 36bf999c 2020-07-23 stsp char *link_target;
2078 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2079 36bf999c 2020-07-23 stsp if (err)
2080 36bf999c 2020-07-23 stsp goto done;
2081 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2082 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2083 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2084 36bf999c 2020-07-23 stsp free(link_target);
2085 993e2a1b 2020-07-23 stsp } else {
2086 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2087 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2088 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2089 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2090 993e2a1b 2020-07-23 stsp }
2091 f69721c3 2019-10-21 stsp free(label_orig);
2092 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2093 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2094 eb81bc23 2022-06-28 tracey goto done;
2095 eb81bc23 2022-06-28 tracey }
2096 234035bc 2019-06-01 stsp if (blob2)
2097 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2098 909d120e 2019-09-22 stsp if (err)
2099 909d120e 2019-09-22 stsp goto done;
2100 234035bc 2019-06-01 stsp /*
2101 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2102 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2103 234035bc 2019-06-01 stsp * unmodified files again.
2104 234035bc 2019-06-01 stsp */
2105 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2106 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2107 234035bc 2019-06-01 stsp update_timestamps);
2108 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2109 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2110 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2111 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2112 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2113 1ee397ad 2019-07-12 stsp if (err)
2114 1ee397ad 2019-07-12 stsp goto done;
2115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2116 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2117 13d9040b 2019-03-27 stsp if (err)
2118 13d9040b 2019-03-27 stsp goto done;
2119 13d9040b 2019-03-27 stsp } else {
2120 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2121 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2122 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2123 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2124 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2125 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2126 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2127 2e1fa222 2020-07-23 stsp } else {
2128 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2129 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2130 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2131 ef623445 2023-09-16 op status == GOT_STATUS_UNVERSIONED,
2132 ef623445 2023-09-16 op &update_timestamps,
2133 ef623445 2023-09-16 op repo, progress_cb, progress_arg);
2134 2e1fa222 2020-07-23 stsp }
2135 13d9040b 2019-03-27 stsp if (err)
2136 13d9040b 2019-03-27 stsp goto done;
2137 65b05cec 2020-07-23 stsp
2138 054041d0 2020-06-24 stsp if (ie) {
2139 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2140 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2141 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2142 054041d0 2020-06-24 stsp } else {
2143 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2144 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2145 ef623445 2023-09-16 op &blob->id, update_timestamps);
2146 054041d0 2020-06-24 stsp }
2147 13d9040b 2019-03-27 stsp if (err)
2148 13d9040b 2019-03-27 stsp goto done;
2149 65b05cec 2020-07-23 stsp
2150 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2151 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2152 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2153 2e1fa222 2020-07-23 stsp }
2154 13d9040b 2019-03-27 stsp }
2155 eb81bc23 2022-06-28 tracey
2156 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2157 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2158 eb81bc23 2022-06-28 tracey goto done;
2159 eb81bc23 2022-06-28 tracey }
2160 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2161 6353ad76 2019-02-08 stsp done:
2162 6353ad76 2019-02-08 stsp free(ondisk_path);
2163 8da9e5f4 2019-01-12 stsp return err;
2164 90285c3b 2019-01-08 stsp }
2165 90285c3b 2019-01-08 stsp
2166 90285c3b 2019-01-08 stsp static const struct got_error *
2167 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2168 90285c3b 2019-01-08 stsp {
2169 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2170 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2171 90285c3b 2019-01-08 stsp
2172 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2174 90285c3b 2019-01-08 stsp
2175 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2176 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2178 c97665ae 2019-01-13 stsp } else {
2179 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2180 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2181 1c4cdd89 2021-06-20 stsp if (err)
2182 1c4cdd89 2021-06-20 stsp goto done;
2183 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2184 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2185 fddefe3b 2020-10-20 stsp free(ondisk_path);
2186 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2187 1c4cdd89 2021-06-20 stsp parent = NULL;
2188 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2189 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2190 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2191 fddefe3b 2020-10-20 stsp ondisk_path);
2192 90285c3b 2019-01-08 stsp break;
2193 90285c3b 2019-01-08 stsp }
2194 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2195 1c4cdd89 2021-06-20 stsp if (err)
2196 1c4cdd89 2021-06-20 stsp break;
2197 1c4cdd89 2021-06-20 stsp }
2198 90285c3b 2019-01-08 stsp }
2199 1c4cdd89 2021-06-20 stsp done:
2200 90285c3b 2019-01-08 stsp free(ondisk_path);
2201 1c4cdd89 2021-06-20 stsp free(parent);
2202 90285c3b 2019-01-08 stsp return err;
2203 512f0d0e 2019-01-02 stsp }
2204 512f0d0e 2019-01-02 stsp
2205 708d8e67 2019-03-27 stsp static const struct got_error *
2206 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2207 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2208 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2209 708d8e67 2019-03-27 stsp {
2210 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2211 708d8e67 2019-03-27 stsp unsigned char status;
2212 708d8e67 2019-03-27 stsp struct stat sb;
2213 708d8e67 2019-03-27 stsp char *ondisk_path;
2214 708d8e67 2019-03-27 stsp
2215 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2216 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2217 a76c42e6 2019-08-03 stsp
2218 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2219 708d8e67 2019-03-27 stsp == -1)
2220 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2221 708d8e67 2019-03-27 stsp
2222 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2223 708d8e67 2019-03-27 stsp if (err)
2224 5a58a424 2020-06-23 stsp goto done;
2225 708d8e67 2019-03-27 stsp
2226 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2227 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2228 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2229 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2230 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2231 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2232 993e2a1b 2020-07-23 stsp goto done;
2233 993e2a1b 2020-07-23 stsp }
2234 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2235 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2236 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2237 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2238 993e2a1b 2020-07-23 stsp if (err)
2239 993e2a1b 2020-07-23 stsp goto done;
2240 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2241 993e2a1b 2020-07-23 stsp ie->path);
2242 993e2a1b 2020-07-23 stsp goto done;
2243 993e2a1b 2020-07-23 stsp }
2244 993e2a1b 2020-07-23 stsp
2245 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2246 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2247 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2248 1ee397ad 2019-07-12 stsp if (err)
2249 5a58a424 2020-06-23 stsp goto done;
2250 fc6346c4 2019-03-27 stsp /*
2251 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2252 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2253 fc6346c4 2019-03-27 stsp */
2254 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2255 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2256 fc6346c4 2019-03-27 stsp } else {
2257 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2258 1ee397ad 2019-07-12 stsp if (err)
2259 5a58a424 2020-06-23 stsp goto done;
2260 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2261 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2262 fc6346c4 2019-03-27 stsp if (err)
2263 5a58a424 2020-06-23 stsp goto done;
2264 fc6346c4 2019-03-27 stsp }
2265 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2266 708d8e67 2019-03-27 stsp }
2267 5a58a424 2020-06-23 stsp done:
2268 5a58a424 2020-06-23 stsp free(ondisk_path);
2269 fc6346c4 2019-03-27 stsp return err;
2270 708d8e67 2019-03-27 stsp }
2271 708d8e67 2019-03-27 stsp
2272 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2273 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2274 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2275 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2276 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2277 8da9e5f4 2019-01-12 stsp void *progress_arg;
2278 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2279 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2280 8da9e5f4 2019-01-12 stsp };
2281 8da9e5f4 2019-01-12 stsp
2282 512f0d0e 2019-01-02 stsp static const struct got_error *
2283 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2284 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2285 512f0d0e 2019-01-02 stsp {
2286 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2287 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2288 0584f854 2019-04-06 stsp
2289 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2290 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2291 f6b8c3c2 2023-07-24 stsp if (err)
2292 f6b8c3c2 2023-07-24 stsp return err;
2293 f6b8c3c2 2023-07-24 stsp }
2294 512f0d0e 2019-01-02 stsp
2295 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2296 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2297 8da9e5f4 2019-01-12 stsp }
2298 512f0d0e 2019-01-02 stsp
2299 8da9e5f4 2019-01-12 stsp static const struct got_error *
2300 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2301 8da9e5f4 2019-01-12 stsp {
2302 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2303 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2304 7a9df742 2019-01-08 stsp
2305 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2306 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2307 f6b8c3c2 2023-07-24 stsp if (err)
2308 f6b8c3c2 2023-07-24 stsp return err;
2309 f6b8c3c2 2023-07-24 stsp }
2310 0584f854 2019-04-06 stsp
2311 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2312 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2313 9d31a1d8 2018-03-11 stsp }
2314 9d31a1d8 2018-03-11 stsp
2315 9d31a1d8 2018-03-11 stsp static const struct got_error *
2316 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2317 9d31a1d8 2018-03-11 stsp {
2318 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2319 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2320 8da9e5f4 2019-01-12 stsp char *path;
2321 9d31a1d8 2018-03-11 stsp
2322 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2323 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2324 f6b8c3c2 2023-07-24 stsp if (err)
2325 f6b8c3c2 2023-07-24 stsp return err;
2326 f6b8c3c2 2023-07-24 stsp }
2327 0584f854 2019-04-06 stsp
2328 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2329 6a390967 2023-07-14 stsp return NULL;
2330 6a390967 2023-07-14 stsp
2331 6a390967 2023-07-14 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2332 63c5ca5d 2019-08-24 stsp return NULL;
2333 63c5ca5d 2019-08-24 stsp
2334 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2335 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2336 8da9e5f4 2019-01-12 stsp == -1)
2337 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2338 9d31a1d8 2018-03-11 stsp
2339 6a390967 2023-07-14 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2340 6a390967 2023-07-14 stsp a->repo, a->progress_cb, a->progress_arg);
2341 9d31a1d8 2018-03-11 stsp
2342 8da9e5f4 2019-01-12 stsp free(path);
2343 0cd1c46a 2019-03-11 stsp return err;
2344 0cd1c46a 2019-03-11 stsp }
2345 0cd1c46a 2019-03-11 stsp
2346 b2118c49 2020-07-28 stsp const struct got_error *
2347 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2348 b2118c49 2020-07-28 stsp {
2349 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2350 b2118c49 2020-07-28 stsp
2351 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2352 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2353 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2354 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2355 b2118c49 2020-07-28 stsp }
2356 b2118c49 2020-07-28 stsp
2357 b2118c49 2020-07-28 stsp return NULL;
2358 b2118c49 2020-07-28 stsp }
2359 b2118c49 2020-07-28 stsp
2360 818c7501 2019-07-11 stsp static const struct got_error *
2361 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2362 0cd1c46a 2019-03-11 stsp {
2363 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2364 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2365 0cd1c46a 2019-03-11 stsp
2366 517bab73 2019-03-11 stsp *refname = NULL;
2367 517bab73 2019-03-11 stsp
2368 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2369 b2118c49 2020-07-28 stsp if (err)
2370 b2118c49 2020-07-28 stsp return err;
2371 0cd1c46a 2019-03-11 stsp
2372 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2373 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2374 0647c563 2019-03-11 stsp *refname = NULL;
2375 0cd1c46a 2019-03-11 stsp }
2376 517bab73 2019-03-11 stsp free(uuidstr);
2377 517bab73 2019-03-11 stsp return err;
2378 517bab73 2019-03-11 stsp }
2379 517bab73 2019-03-11 stsp
2380 818c7501 2019-07-11 stsp const struct got_error *
2381 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2382 9587e6cc 2023-01-28 mark const char *prefix)
2383 9587e6cc 2023-01-28 mark {
2384 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2385 9587e6cc 2023-01-28 mark }
2386 9587e6cc 2023-01-28 mark
2387 5662d61d 2023-07-03 jrick static const struct got_error *
2388 5662d61d 2023-07-03 jrick get_base_ref_name(char **refname, struct got_worktree *worktree)
2389 818c7501 2019-07-11 stsp {
2390 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2391 818c7501 2019-07-11 stsp }
2392 818c7501 2019-07-11 stsp
2393 818c7501 2019-07-11 stsp static const struct got_error *
2394 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2395 818c7501 2019-07-11 stsp {
2396 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2397 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2398 818c7501 2019-07-11 stsp }
2399 818c7501 2019-07-11 stsp
2400 818c7501 2019-07-11 stsp static const struct got_error *
2401 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2402 818c7501 2019-07-11 stsp {
2403 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2404 818c7501 2019-07-11 stsp }
2405 818c7501 2019-07-11 stsp
2406 818c7501 2019-07-11 stsp static const struct got_error *
2407 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2408 818c7501 2019-07-11 stsp {
2409 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2410 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2411 818c7501 2019-07-11 stsp }
2412 818c7501 2019-07-11 stsp
2413 818c7501 2019-07-11 stsp static const struct got_error *
2414 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2415 818c7501 2019-07-11 stsp {
2416 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2417 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2418 0ebf8283 2019-07-24 stsp }
2419 0ebf8283 2019-07-24 stsp
2420 0ebf8283 2019-07-24 stsp static const struct got_error *
2421 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2422 0ebf8283 2019-07-24 stsp {
2423 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2424 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2425 0ebf8283 2019-07-24 stsp }
2426 0ebf8283 2019-07-24 stsp
2427 0ebf8283 2019-07-24 stsp static const struct got_error *
2428 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2429 0ebf8283 2019-07-24 stsp {
2430 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2431 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2432 0ebf8283 2019-07-24 stsp }
2433 0ebf8283 2019-07-24 stsp
2434 0ebf8283 2019-07-24 stsp static const struct got_error *
2435 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2436 0ebf8283 2019-07-24 stsp {
2437 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2438 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2439 818c7501 2019-07-11 stsp }
2440 818c7501 2019-07-11 stsp
2441 0ebf8283 2019-07-24 stsp static const struct got_error *
2442 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2443 0ebf8283 2019-07-24 stsp {
2444 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2445 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2446 0ebf8283 2019-07-24 stsp }
2447 818c7501 2019-07-11 stsp
2448 0ebf8283 2019-07-24 stsp const struct got_error *
2449 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2450 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2451 0ebf8283 2019-07-24 stsp {
2452 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2453 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2454 0ebf8283 2019-07-24 stsp *path = NULL;
2455 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2456 0ebf8283 2019-07-24 stsp }
2457 0ebf8283 2019-07-24 stsp return NULL;
2458 f259c4c1 2021-09-24 stsp }
2459 f259c4c1 2021-09-24 stsp
2460 f259c4c1 2021-09-24 stsp static const struct got_error *
2461 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2462 f259c4c1 2021-09-24 stsp {
2463 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2464 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2465 0ebf8283 2019-07-24 stsp }
2466 0ebf8283 2019-07-24 stsp
2467 f259c4c1 2021-09-24 stsp static const struct got_error *
2468 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2469 f259c4c1 2021-09-24 stsp {
2470 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2471 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2472 f259c4c1 2021-09-24 stsp }
2473 f259c4c1 2021-09-24 stsp
2474 517bab73 2019-03-11 stsp /*
2475 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2476 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2477 517bab73 2019-03-11 stsp */
2478 517bab73 2019-03-11 stsp static const struct got_error *
2479 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2480 517bab73 2019-03-11 stsp {
2481 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2482 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2483 517bab73 2019-03-11 stsp char *refname;
2484 517bab73 2019-03-11 stsp
2485 5662d61d 2023-07-03 jrick err = get_base_ref_name(&refname, worktree);
2486 517bab73 2019-03-11 stsp if (err)
2487 517bab73 2019-03-11 stsp return err;
2488 0cd1c46a 2019-03-11 stsp
2489 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2490 0cd1c46a 2019-03-11 stsp if (err)
2491 0cd1c46a 2019-03-11 stsp goto done;
2492 0cd1c46a 2019-03-11 stsp
2493 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2494 0cd1c46a 2019-03-11 stsp done:
2495 0cd1c46a 2019-03-11 stsp free(refname);
2496 0cd1c46a 2019-03-11 stsp if (ref)
2497 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2498 3e3a69f1 2019-07-25 stsp return err;
2499 3e3a69f1 2019-07-25 stsp }
2500 3e3a69f1 2019-07-25 stsp
2501 3e3a69f1 2019-07-25 stsp static const struct got_error *
2502 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2503 3e3a69f1 2019-07-25 stsp {
2504 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2505 3e3a69f1 2019-07-25 stsp
2506 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2507 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2508 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2509 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2510 3e3a69f1 2019-07-25 stsp }
2511 9d31a1d8 2018-03-11 stsp return err;
2512 9d31a1d8 2018-03-11 stsp }
2513 9d31a1d8 2018-03-11 stsp
2514 3e3a69f1 2019-07-25 stsp
2515 ebf99748 2019-05-09 stsp static const struct got_error *
2516 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2517 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2518 ebf99748 2019-05-09 stsp {
2519 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2520 ebf99748 2019-05-09 stsp FILE *index = NULL;
2521 0cd1c46a 2019-03-11 stsp
2522 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2523 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2524 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2525 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2526 ebf99748 2019-05-09 stsp
2527 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2528 3e3a69f1 2019-07-25 stsp if (err)
2529 ebf99748 2019-05-09 stsp goto done;
2530 ebf99748 2019-05-09 stsp
2531 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2532 ebf99748 2019-05-09 stsp if (index == NULL) {
2533 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2534 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2535 ebf99748 2019-05-09 stsp } else {
2536 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2537 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2538 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2539 ebf99748 2019-05-09 stsp }
2540 ebf99748 2019-05-09 stsp done:
2541 ebf99748 2019-05-09 stsp if (err) {
2542 ebf99748 2019-05-09 stsp free(*fileindex_path);
2543 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2544 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2545 ebf99748 2019-05-09 stsp *fileindex = NULL;
2546 ebf99748 2019-05-09 stsp }
2547 ebf99748 2019-05-09 stsp return err;
2548 ebf99748 2019-05-09 stsp }
2549 c932eeeb 2019-05-22 stsp
2550 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2551 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2552 c932eeeb 2019-05-22 stsp const char *path;
2553 c932eeeb 2019-05-22 stsp size_t path_len;
2554 c932eeeb 2019-05-22 stsp const char *entry_name;
2555 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2556 a484d721 2019-06-10 stsp void *progress_arg;
2557 c932eeeb 2019-05-22 stsp };
2558 ebf99748 2019-05-09 stsp
2559 c932eeeb 2019-05-22 stsp static const struct got_error *
2560 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2561 c932eeeb 2019-05-22 stsp {
2562 1ee397ad 2019-07-12 stsp const struct got_error *err;
2563 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2564 c932eeeb 2019-05-22 stsp
2565 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2566 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2567 c932eeeb 2019-05-22 stsp return NULL;
2568 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2569 102ff934 2019-06-11 stsp return NULL;
2570 102ff934 2019-06-11 stsp
2571 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2572 a769b60b 2021-06-27 stsp return NULL;
2573 a769b60b 2021-06-27 stsp
2574 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2575 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2576 c932eeeb 2019-05-22 stsp return NULL;
2577 c932eeeb 2019-05-22 stsp
2578 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2579 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2580 edd02c5e 2019-07-11 stsp ie->path);
2581 1ee397ad 2019-07-12 stsp if (err)
2582 1ee397ad 2019-07-12 stsp return err;
2583 1ee397ad 2019-07-12 stsp }
2584 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2585 c932eeeb 2019-05-22 stsp return NULL;
2586 a615e0e7 2020-12-16 stsp }
2587 a615e0e7 2020-12-16 stsp
2588 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2589 a615e0e7 2020-12-16 stsp static const struct got_error *
2590 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2591 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2592 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2593 a615e0e7 2020-12-16 stsp {
2594 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2595 a615e0e7 2020-12-16 stsp
2596 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2597 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2598 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2599 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2600 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2601 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2602 a615e0e7 2020-12-16 stsp
2603 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2604 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2605 9c6338c4 2019-06-02 stsp }
2606 9c6338c4 2019-06-02 stsp
2607 9c6338c4 2019-06-02 stsp static const struct got_error *
2608 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2609 9c6338c4 2019-06-02 stsp {
2610 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2611 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2612 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2613 867630bb 2020-01-17 stsp struct timespec timeout;
2614 9c6338c4 2019-06-02 stsp
2615 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2616 b90054ed 2022-11-01 stsp fileindex_path, "");
2617 9c6338c4 2019-06-02 stsp if (err)
2618 9c6338c4 2019-06-02 stsp goto done;
2619 9c6338c4 2019-06-02 stsp
2620 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2621 9c6338c4 2019-06-02 stsp if (err)
2622 9c6338c4 2019-06-02 stsp goto done;
2623 9c6338c4 2019-06-02 stsp
2624 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2625 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2626 9c6338c4 2019-06-02 stsp fileindex_path);
2627 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2628 9c6338c4 2019-06-02 stsp }
2629 867630bb 2020-01-17 stsp
2630 867630bb 2020-01-17 stsp /*
2631 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2632 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2633 867630bb 2020-01-17 stsp * was recorded in the file index.
2634 867630bb 2020-01-17 stsp */
2635 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2636 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2637 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2638 9c6338c4 2019-06-02 stsp done:
2639 9c6338c4 2019-06-02 stsp if (new_index)
2640 9c6338c4 2019-06-02 stsp fclose(new_index);
2641 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2642 9c6338c4 2019-06-02 stsp return err;
2643 c932eeeb 2019-05-22 stsp }
2644 6ced4176 2019-07-12 stsp
2645 6ced4176 2019-07-12 stsp static const struct got_error *
2646 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2647 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2648 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2649 a44927cc 2022-04-07 stsp struct got_repository *repo)
2650 6ced4176 2019-07-12 stsp {
2651 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2652 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2653 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2654 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2655 6ced4176 2019-07-12 stsp
2656 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2657 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2658 6ced4176 2019-07-12 stsp *tree_id = NULL;
2659 6ced4176 2019-07-12 stsp
2660 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2661 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2662 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2663 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2664 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2665 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2666 6ced4176 2019-07-12 stsp goto done;
2667 6ced4176 2019-07-12 stsp }
2668 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2669 a44927cc 2022-04-07 stsp worktree->path_prefix);
2670 6ced4176 2019-07-12 stsp if (err)
2671 6ced4176 2019-07-12 stsp goto done;
2672 6ced4176 2019-07-12 stsp return NULL;
2673 6ced4176 2019-07-12 stsp }
2674 6ced4176 2019-07-12 stsp
2675 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2676 6ced4176 2019-07-12 stsp
2677 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2678 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2679 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2680 6ced4176 2019-07-12 stsp goto done;
2681 6ced4176 2019-07-12 stsp }
2682 6ced4176 2019-07-12 stsp
2683 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2684 6ced4176 2019-07-12 stsp if (err)
2685 6ced4176 2019-07-12 stsp goto done;
2686 6ced4176 2019-07-12 stsp
2687 6ced4176 2019-07-12 stsp free(in_repo_path);
2688 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2689 6ced4176 2019-07-12 stsp
2690 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2691 6ced4176 2019-07-12 stsp if (err)
2692 6ced4176 2019-07-12 stsp goto done;
2693 c932eeeb 2019-05-22 stsp
2694 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2695 6ced4176 2019-07-12 stsp /* Check out a single file. */
2696 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2697 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2698 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2699 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2700 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2701 6ced4176 2019-07-12 stsp goto done;
2702 6ced4176 2019-07-12 stsp }
2703 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2704 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2705 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2706 6ced4176 2019-07-12 stsp goto done;
2707 6ced4176 2019-07-12 stsp }
2708 6ced4176 2019-07-12 stsp } else {
2709 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2710 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2711 6ced4176 2019-07-12 stsp if (err)
2712 6ced4176 2019-07-12 stsp return err;
2713 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2714 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2715 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2716 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2717 6ced4176 2019-07-12 stsp goto done;
2718 6ced4176 2019-07-12 stsp }
2719 6ced4176 2019-07-12 stsp }
2720 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2721 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2722 6ced4176 2019-07-12 stsp } else {
2723 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2724 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2725 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2726 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2727 6ced4176 2019-07-12 stsp goto done;
2728 6ced4176 2019-07-12 stsp }
2729 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2730 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2731 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2732 6ced4176 2019-07-12 stsp goto done;
2733 6ced4176 2019-07-12 stsp }
2734 6ced4176 2019-07-12 stsp }
2735 6ced4176 2019-07-12 stsp done:
2736 6ced4176 2019-07-12 stsp free(id);
2737 6ced4176 2019-07-12 stsp free(in_repo_path);
2738 6ced4176 2019-07-12 stsp if (err) {
2739 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2740 6ced4176 2019-07-12 stsp free(*tree_relpath);
2741 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2742 6ced4176 2019-07-12 stsp free(*tree_id);
2743 6ced4176 2019-07-12 stsp *tree_id = NULL;
2744 6ced4176 2019-07-12 stsp }
2745 07ed472d 2019-07-12 stsp return err;
2746 07ed472d 2019-07-12 stsp }
2747 07ed472d 2019-07-12 stsp
2748 07ed472d 2019-07-12 stsp static const struct got_error *
2749 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2750 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2751 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2752 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2753 07ed472d 2019-07-12 stsp {
2754 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2755 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2756 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2757 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2758 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2759 07ed472d 2019-07-12 stsp
2760 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2761 7f47418f 2019-12-20 stsp if (err) {
2762 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2763 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2764 7f47418f 2019-12-20 stsp goto done;
2765 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2766 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2767 7f47418f 2019-12-20 stsp if (err)
2768 7f47418f 2019-12-20 stsp return err;
2769 7f47418f 2019-12-20 stsp }
2770 07ed472d 2019-07-12 stsp
2771 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2772 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2773 07ed472d 2019-07-12 stsp if (err)
2774 07ed472d 2019-07-12 stsp goto done;
2775 07ed472d 2019-07-12 stsp
2776 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2777 07ed472d 2019-07-12 stsp if (err)
2778 07ed472d 2019-07-12 stsp goto done;
2779 07ed472d 2019-07-12 stsp
2780 07ed472d 2019-07-12 stsp if (entry_name &&
2781 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2782 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2783 07ed472d 2019-07-12 stsp goto done;
2784 07ed472d 2019-07-12 stsp }
2785 07ed472d 2019-07-12 stsp
2786 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2787 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2788 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2789 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2790 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2791 07ed472d 2019-07-12 stsp arg.repo = repo;
2792 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2793 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2794 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2795 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2796 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2797 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2798 07ed472d 2019-07-12 stsp done:
2799 07ed472d 2019-07-12 stsp if (tree)
2800 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2801 07ed472d 2019-07-12 stsp if (commit)
2802 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2803 6ced4176 2019-07-12 stsp return err;
2804 6ced4176 2019-07-12 stsp }
2805 6ced4176 2019-07-12 stsp
2806 9d31a1d8 2018-03-11 stsp const struct got_error *
2807 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2808 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2809 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2811 9d31a1d8 2018-03-11 stsp {
2812 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2813 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2814 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2815 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2816 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2817 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2818 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2819 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2820 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2821 f2ea84fa 2019-07-27 stsp int entry_type;
2822 f2ea84fa 2019-07-27 stsp char *relpath;
2823 f2ea84fa 2019-07-27 stsp char *entry_name;
2824 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2825 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2826 9d31a1d8 2018-03-11 stsp
2827 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2828 f2ea84fa 2019-07-27 stsp
2829 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2830 9d31a1d8 2018-03-11 stsp if (err)
2831 9d31a1d8 2018-03-11 stsp return err;
2832 a44927cc 2022-04-07 stsp
2833 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2834 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2835 a44927cc 2022-04-07 stsp if (err)
2836 a44927cc 2022-04-07 stsp goto done;
2837 93a30277 2018-12-24 stsp
2838 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2839 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2840 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2841 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2842 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2843 f2ea84fa 2019-07-27 stsp goto done;
2844 f2ea84fa 2019-07-27 stsp }
2845 6ced4176 2019-07-12 stsp
2846 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2847 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2848 a44927cc 2022-04-07 stsp worktree, repo);
2849 f2ea84fa 2019-07-27 stsp if (err) {
2850 f2ea84fa 2019-07-27 stsp free(tpd);
2851 6ced4176 2019-07-12 stsp goto done;
2852 6ced4176 2019-07-12 stsp }
2853 f2ea84fa 2019-07-27 stsp
2854 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2855 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2856 f2ea84fa 2019-07-27 stsp if (err) {
2857 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2858 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2859 f2ea84fa 2019-07-27 stsp free(tpd);
2860 f2ea84fa 2019-07-27 stsp goto done;
2861 f2ea84fa 2019-07-27 stsp }
2862 f2ea84fa 2019-07-27 stsp } else
2863 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2864 f2ea84fa 2019-07-27 stsp
2865 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2866 6ced4176 2019-07-12 stsp }
2867 6ced4176 2019-07-12 stsp
2868 51514078 2018-12-25 stsp /*
2869 51514078 2018-12-25 stsp * Read the file index.
2870 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2871 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2872 51514078 2018-12-25 stsp */
2873 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2874 8da9e5f4 2019-01-12 stsp if (err)
2875 c4cdcb68 2019-04-03 stsp goto done;
2876 c4cdcb68 2019-04-03 stsp
2877 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2878 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2879 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2880 f2ea84fa 2019-07-27 stsp
2881 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2882 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2883 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2884 f2ea84fa 2019-07-27 stsp if (err)
2885 f2ea84fa 2019-07-27 stsp break;
2886 f2ea84fa 2019-07-27 stsp
2887 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2888 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2889 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2890 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2891 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2892 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2893 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2894 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2895 f2ea84fa 2019-07-27 stsp if (err)
2896 f2ea84fa 2019-07-27 stsp break;
2897 f2ea84fa 2019-07-27 stsp
2898 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2899 711d9cd9 2019-07-12 stsp }
2900 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2901 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2902 af12c6b9 2019-06-04 stsp err = sync_err;
2903 9d31a1d8 2018-03-11 stsp done:
2904 9c6338c4 2019-06-02 stsp free(fileindex_path);
2905 edfa7d7f 2018-09-11 stsp if (tree)
2906 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2907 9d31a1d8 2018-03-11 stsp if (commit)
2908 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2909 5ade8233 2019-07-12 stsp if (fileindex)
2910 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2911 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2912 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2913 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2914 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2915 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2916 f2ea84fa 2019-07-27 stsp free(tpd);
2917 f2ea84fa 2019-07-27 stsp }
2918 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2919 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2920 9d31a1d8 2018-03-11 stsp err = unlockerr;
2921 f8d1f275 2019-02-04 stsp return err;
2922 f8d1f275 2019-02-04 stsp }
2923 f8d1f275 2019-02-04 stsp
2924 9a298e5c 2023-03-10 stsp static const struct got_error *
2925 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2926 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2927 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2928 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2929 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2930 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2931 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2932 9a298e5c 2023-03-10 stsp {
2933 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2934 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2935 9a298e5c 2023-03-10 stsp
2936 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2937 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2938 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2939 9a298e5c 2023-03-10 stsp restoring_missing_file,
2940 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2941 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2942 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2943 9a298e5c 2023-03-10 stsp } else {
2944 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2945 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2946 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2947 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
2948 ef623445 2023-09-16 op progress_cb, progress_arg);
2949 9a298e5c 2023-03-10 stsp }
2950 9a298e5c 2023-03-10 stsp if (err)
2951 9a298e5c 2023-03-10 stsp return err;
2952 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2953 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2954 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2955 9a298e5c 2023-03-10 stsp if (err)
2956 9a298e5c 2023-03-10 stsp return err;
2957 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2958 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2959 9a298e5c 2023-03-10 stsp if (err) {
2960 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2961 9a298e5c 2023-03-10 stsp return err;
2962 9a298e5c 2023-03-10 stsp }
2963 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2964 9a298e5c 2023-03-10 stsp if (err) {
2965 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2966 9a298e5c 2023-03-10 stsp return err;
2967 9a298e5c 2023-03-10 stsp }
2968 9a298e5c 2023-03-10 stsp } else {
2969 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2970 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2971 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2972 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2973 9a298e5c 2023-03-10 stsp if (err)
2974 9a298e5c 2023-03-10 stsp return err;
2975 9a298e5c 2023-03-10 stsp }
2976 9a298e5c 2023-03-10 stsp
2977 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2978 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2979 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2980 9a298e5c 2023-03-10 stsp }
2981 9a298e5c 2023-03-10 stsp
2982 9a298e5c 2023-03-10 stsp return NULL;
2983 9a298e5c 2023-03-10 stsp }
2984 9a298e5c 2023-03-10 stsp
2985 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2986 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2987 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2988 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2989 234035bc 2019-06-01 stsp void *progress_arg;
2990 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2991 234035bc 2019-06-01 stsp void *cancel_arg;
2992 f69721c3 2019-10-21 stsp const char *label_orig;
2993 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2994 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2995 234035bc 2019-06-01 stsp };
2996 234035bc 2019-06-01 stsp
2997 234035bc 2019-06-01 stsp static const struct got_error *
2998 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2999 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3000 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3001 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3002 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3003 234035bc 2019-06-01 stsp {
3004 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3005 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3006 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3007 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3008 234035bc 2019-06-01 stsp struct stat sb;
3009 234035bc 2019-06-01 stsp unsigned char status;
3010 234035bc 2019-06-01 stsp int local_changes_subsumed;
3011 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3012 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3013 c2ba7aa6 2023-07-26 stsp struct got_object_id *id = NULL;
3014 234035bc 2019-06-01 stsp
3015 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3016 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3017 d6c87207 2019-08-02 stsp strlen(path2));
3018 1ee397ad 2019-07-12 stsp if (ie == NULL)
3019 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3020 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3021 234035bc 2019-06-01 stsp
3022 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3023 234035bc 2019-06-01 stsp path2) == -1)
3024 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3025 234035bc 2019-06-01 stsp
3026 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3027 7f91a133 2019-12-13 stsp repo);
3028 234035bc 2019-06-01 stsp if (err)
3029 234035bc 2019-06-01 stsp goto done;
3030 234035bc 2019-06-01 stsp
3031 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3032 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3033 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3034 234035bc 2019-06-01 stsp goto done;
3035 234035bc 2019-06-01 stsp }
3036 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3037 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3038 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3039 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3040 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3041 234035bc 2019-06-01 stsp goto done;
3042 234035bc 2019-06-01 stsp }
3043 234035bc 2019-06-01 stsp
3044 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3045 36bf999c 2020-07-23 stsp char *link_target2;
3046 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3047 36bf999c 2020-07-23 stsp if (err)
3048 36bf999c 2020-07-23 stsp goto done;
3049 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3050 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3051 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3052 36bf999c 2020-07-23 stsp free(link_target2);
3053 af57b12a 2020-07-23 stsp } else {
3054 1af628f4 2021-06-03 stsp int fd;
3055 1af628f4 2021-06-03 stsp
3056 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3057 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3058 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3059 1af628f4 2021-06-03 stsp goto done;
3060 1af628f4 2021-06-03 stsp }
3061 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3062 1af628f4 2021-06-03 stsp f_orig, blob1);
3063 1af628f4 2021-06-03 stsp if (err)
3064 1af628f4 2021-06-03 stsp goto done;
3065 1af628f4 2021-06-03 stsp
3066 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3067 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3068 1af628f4 2021-06-03 stsp goto done;
3069 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3070 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3071 1af628f4 2021-06-03 stsp if (err)
3072 1af628f4 2021-06-03 stsp goto done;
3073 1af628f4 2021-06-03 stsp
3074 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3075 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3076 1af628f4 2021-06-03 stsp if (fd == -1) {
3077 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3078 1af628f4 2021-06-03 stsp ondisk_path);
3079 1af628f4 2021-06-03 stsp goto done;
3080 1af628f4 2021-06-03 stsp }
3081 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3082 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3083 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3084 1af628f4 2021-06-03 stsp ondisk_path);
3085 1af628f4 2021-06-03 stsp close(fd);
3086 1af628f4 2021-06-03 stsp goto done;
3087 1af628f4 2021-06-03 stsp }
3088 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3089 1af628f4 2021-06-03 stsp if (err)
3090 1af628f4 2021-06-03 stsp goto done;
3091 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3092 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3093 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3094 1af628f4 2021-06-03 stsp goto done;
3095 1af628f4 2021-06-03 stsp }
3096 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3097 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3098 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3099 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3100 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3101 af57b12a 2020-07-23 stsp }
3102 234035bc 2019-06-01 stsp } else if (blob1) {
3103 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3104 d6c87207 2019-08-02 stsp strlen(path1));
3105 1ee397ad 2019-07-12 stsp if (ie == NULL)
3106 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3107 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3108 2b92fad7 2019-06-02 stsp
3109 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3110 2b92fad7 2019-06-02 stsp path1) == -1)
3111 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3112 2b92fad7 2019-06-02 stsp
3113 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3114 7f91a133 2019-12-13 stsp repo);
3115 2b92fad7 2019-06-02 stsp if (err)
3116 2b92fad7 2019-06-02 stsp goto done;
3117 2b92fad7 2019-06-02 stsp
3118 2b92fad7 2019-06-02 stsp switch (status) {
3119 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3120 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3121 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3122 1ee397ad 2019-07-12 stsp if (err)
3123 1ee397ad 2019-07-12 stsp goto done;
3124 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3125 2b92fad7 2019-06-02 stsp if (err)
3126 2b92fad7 2019-06-02 stsp goto done;
3127 2b92fad7 2019-06-02 stsp if (ie)
3128 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3129 2b92fad7 2019-06-02 stsp break;
3130 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3131 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3132 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3133 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3134 1ee397ad 2019-07-12 stsp if (err)
3135 1ee397ad 2019-07-12 stsp goto done;
3136 2b92fad7 2019-06-02 stsp if (ie)
3137 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3138 2b92fad7 2019-06-02 stsp break;
3139 c2ba7aa6 2023-07-26 stsp case GOT_STATUS_MODIFY: {
3140 c2ba7aa6 2023-07-26 stsp FILE *blob1_f;
3141 c2ba7aa6 2023-07-26 stsp off_t blob1_size;
3142 c2ba7aa6 2023-07-26 stsp int obj_type;
3143 c2ba7aa6 2023-07-26 stsp /*
3144 c2ba7aa6 2023-07-26 stsp * Delete the file only if its content already
3145 c2ba7aa6 2023-07-26 stsp * exists in the repository.
3146 c2ba7aa6 2023-07-26 stsp */
3147 c2ba7aa6 2023-07-26 stsp err = got_object_blob_file_create(&id, &blob1_f,
3148 c2ba7aa6 2023-07-26 stsp &blob1_size, path1);
3149 c2ba7aa6 2023-07-26 stsp if (err)
3150 c2ba7aa6 2023-07-26 stsp goto done;
3151 c2ba7aa6 2023-07-26 stsp if (fclose(blob1_f) == EOF) {
3152 c2ba7aa6 2023-07-26 stsp err = got_error_from_errno("fclose");
3153 c2ba7aa6 2023-07-26 stsp goto done;
3154 c2ba7aa6 2023-07-26 stsp }
3155 c2ba7aa6 2023-07-26 stsp
3156 c2ba7aa6 2023-07-26 stsp /* Implied existence check. */
3157 c2ba7aa6 2023-07-26 stsp err = got_object_get_type(&obj_type, repo, id);
3158 c2ba7aa6 2023-07-26 stsp if (err) {
3159 c2ba7aa6 2023-07-26 stsp if (err->code != GOT_ERR_NO_OBJ)
3160 c2ba7aa6 2023-07-26 stsp goto done;
3161 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3162 c2ba7aa6 2023-07-26 stsp GOT_STATUS_CANNOT_DELETE, path1);
3163 c2ba7aa6 2023-07-26 stsp goto done;
3164 c2ba7aa6 2023-07-26 stsp } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3165 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3166 c2ba7aa6 2023-07-26 stsp GOT_STATUS_CANNOT_DELETE, path1);
3167 c2ba7aa6 2023-07-26 stsp goto done;
3168 c2ba7aa6 2023-07-26 stsp }
3169 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3170 c2ba7aa6 2023-07-26 stsp GOT_STATUS_DELETE, path1);
3171 c2ba7aa6 2023-07-26 stsp if (err)
3172 c2ba7aa6 2023-07-26 stsp goto done;
3173 c2ba7aa6 2023-07-26 stsp err = remove_ondisk_file(a->worktree->root_path,
3174 c2ba7aa6 2023-07-26 stsp path1);
3175 c2ba7aa6 2023-07-26 stsp if (err)
3176 c2ba7aa6 2023-07-26 stsp goto done;
3177 c2ba7aa6 2023-07-26 stsp if (ie)
3178 c2ba7aa6 2023-07-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3179 c2ba7aa6 2023-07-26 stsp break;
3180 c2ba7aa6 2023-07-26 stsp }
3181 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3182 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3183 72840534 2022-01-19 stsp off_t blob1_size;
3184 0a22ca1a 2020-09-23 stsp /*
3185 c2ba7aa6 2023-07-26 stsp * Delete the file only if its content already
3186 0a22ca1a 2020-09-23 stsp * exists in the repository.
3187 0a22ca1a 2020-09-23 stsp */
3188 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3189 72840534 2022-01-19 stsp &blob1_size, path1);
3190 0a22ca1a 2020-09-23 stsp if (err)
3191 0a22ca1a 2020-09-23 stsp goto done;
3192 c2ba7aa6 2023-07-26 stsp if (fclose(blob1_f) == EOF) {
3193 c2ba7aa6 2023-07-26 stsp err = got_error_from_errno("fclose");
3194 c2ba7aa6 2023-07-26 stsp goto done;
3195 c2ba7aa6 2023-07-26 stsp }
3196 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3197 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3198 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3199 0a22ca1a 2020-09-23 stsp if (err)
3200 0a22ca1a 2020-09-23 stsp goto done;
3201 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3202 0a22ca1a 2020-09-23 stsp path1);
3203 0a22ca1a 2020-09-23 stsp if (err)
3204 0a22ca1a 2020-09-23 stsp goto done;
3205 0a22ca1a 2020-09-23 stsp if (ie)
3206 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3207 0a22ca1a 2020-09-23 stsp ie);
3208 0a22ca1a 2020-09-23 stsp } else {
3209 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3210 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3211 0a22ca1a 2020-09-23 stsp }
3212 0a22ca1a 2020-09-23 stsp if (err)
3213 0a22ca1a 2020-09-23 stsp goto done;
3214 0a22ca1a 2020-09-23 stsp break;
3215 0a22ca1a 2020-09-23 stsp }
3216 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3217 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3218 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3219 1ee397ad 2019-07-12 stsp if (err)
3220 1ee397ad 2019-07-12 stsp goto done;
3221 2b92fad7 2019-06-02 stsp break;
3222 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3223 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3224 1ee397ad 2019-07-12 stsp if (err)
3225 1ee397ad 2019-07-12 stsp goto done;
3226 2b92fad7 2019-06-02 stsp break;
3227 2b92fad7 2019-06-02 stsp default:
3228 2b92fad7 2019-06-02 stsp break;
3229 2b92fad7 2019-06-02 stsp }
3230 234035bc 2019-06-01 stsp } else if (blob2) {
3231 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3232 234035bc 2019-06-01 stsp path2) == -1)
3233 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3234 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3235 d6c87207 2019-08-02 stsp strlen(path2));
3236 234035bc 2019-06-01 stsp if (ie) {
3237 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3238 7f91a133 2019-12-13 stsp -1, NULL, repo);
3239 234035bc 2019-06-01 stsp if (err)
3240 234035bc 2019-06-01 stsp goto done;
3241 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3242 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3243 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3244 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3245 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3246 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3247 1ee397ad 2019-07-12 stsp status, path2);
3248 234035bc 2019-06-01 stsp goto done;
3249 234035bc 2019-06-01 stsp }
3250 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3251 36bf999c 2020-07-23 stsp char *link_target2;
3252 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3253 36bf999c 2020-07-23 stsp blob2);
3254 36bf999c 2020-07-23 stsp if (err)
3255 36bf999c 2020-07-23 stsp goto done;
3256 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3257 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3258 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3259 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3260 36bf999c 2020-07-23 stsp free(link_target2);
3261 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3262 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3263 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3264 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3265 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3266 526a746f 2020-07-23 stsp a->progress_arg);
3267 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3268 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3269 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3270 526a746f 2020-07-23 stsp }
3271 dfe9fba0 2020-07-23 stsp if (err)
3272 dfe9fba0 2020-07-23 stsp goto done;
3273 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3274 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3275 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3276 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3277 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3278 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3279 234035bc 2019-06-01 stsp if (err)
3280 234035bc 2019-06-01 stsp goto done;
3281 234035bc 2019-06-01 stsp }
3282 234035bc 2019-06-01 stsp } else {
3283 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3284 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3285 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3286 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3287 234035bc 2019-06-01 stsp if (err)
3288 2b92fad7 2019-06-02 stsp goto done;
3289 234035bc 2019-06-01 stsp }
3290 234035bc 2019-06-01 stsp }
3291 234035bc 2019-06-01 stsp done:
3292 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3293 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3294 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3295 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3296 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3297 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3298 1af628f4 2021-06-03 stsp free(id_str);
3299 c2ba7aa6 2023-07-26 stsp free(id);
3300 54d5be07 2021-06-03 stsp free(label_deriv2);
3301 234035bc 2019-06-01 stsp free(ondisk_path);
3302 234035bc 2019-06-01 stsp return err;
3303 234035bc 2019-06-01 stsp }
3304 cdbfe5d2 2023-07-24 stsp
3305 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args {
3306 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree;
3307 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb;
3308 cdbfe5d2 2023-07-24 stsp void *cancel_arg;
3309 cdbfe5d2 2023-07-24 stsp };
3310 234035bc 2019-06-01 stsp
3311 69de9dd4 2021-09-03 stsp static const struct got_error *
3312 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3313 69de9dd4 2021-09-03 stsp {
3314 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
3315 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args *a = arg;
3316 cdbfe5d2 2023-07-24 stsp
3317 cdbfe5d2 2023-07-24 stsp if (a->cancel_cb) {
3318 cdbfe5d2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3319 cdbfe5d2 2023-07-24 stsp if (err)
3320 cdbfe5d2 2023-07-24 stsp return err;
3321 cdbfe5d2 2023-07-24 stsp }
3322 69de9dd4 2021-09-03 stsp
3323 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3324 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3325 cdbfe5d2 2023-07-24 stsp memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3326 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3327 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3328 69de9dd4 2021-09-03 stsp
3329 69de9dd4 2021-09-03 stsp return NULL;
3330 69de9dd4 2021-09-03 stsp }
3331 69de9dd4 2021-09-03 stsp
3332 c935fd51 2023-07-23 mark const struct got_error *
3333 c935fd51 2023-07-23 mark got_worktree_get_state(char *state, struct got_repository *repo,
3334 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree,
3335 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3336 c935fd51 2023-07-23 mark {
3337 c935fd51 2023-07-23 mark const struct got_error *err;
3338 c935fd51 2023-07-23 mark struct got_object_id *base_id, *head_id = NULL;
3339 c935fd51 2023-07-23 mark struct got_reference *head_ref;
3340 c935fd51 2023-07-23 mark struct got_fileindex *fileindex = NULL;
3341 c935fd51 2023-07-23 mark char *fileindex_path = NULL;
3342 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3343 c935fd51 2023-07-23 mark
3344 c935fd51 2023-07-23 mark if (worktree == NULL)
3345 c935fd51 2023-07-23 mark return got_error(GOT_ERR_NOT_WORKTREE);
3346 c935fd51 2023-07-23 mark
3347 c935fd51 2023-07-23 mark err = got_ref_open(&head_ref, repo,
3348 c935fd51 2023-07-23 mark got_worktree_get_head_ref_name(worktree), 0);
3349 c935fd51 2023-07-23 mark if (err)
3350 c935fd51 2023-07-23 mark return err;
3351 c935fd51 2023-07-23 mark
3352 c935fd51 2023-07-23 mark err = got_ref_resolve(&head_id, repo, head_ref);
3353 c935fd51 2023-07-23 mark if (err)
3354 c935fd51 2023-07-23 mark goto done;
3355 c935fd51 2023-07-23 mark
3356 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UNKNOWN;
3357 c935fd51 2023-07-23 mark base_id = got_worktree_get_base_commit_id(worktree);
3358 c935fd51 2023-07-23 mark
3359 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3360 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3361 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3362 cdbfe5d2 2023-07-24 stsp
3363 c935fd51 2023-07-23 mark if (got_object_id_cmp(base_id, head_id) == 0) {
3364 c935fd51 2023-07-23 mark err = open_fileindex(&fileindex, &fileindex_path, worktree);
3365 c935fd51 2023-07-23 mark if (err)
3366 c935fd51 2023-07-23 mark goto done;
3367 c935fd51 2023-07-23 mark
3368 c935fd51 2023-07-23 mark err = got_fileindex_for_each_entry_safe(fileindex,
3369 cdbfe5d2 2023-07-24 stsp check_mixed_commits, &cma);
3370 c935fd51 2023-07-23 mark if (err == NULL)
3371 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UPTODATE;
3372 99301cec 2023-07-24 stsp else if (err->code == GOT_ERR_MIXED_COMMITS) {
3373 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3374 c935fd51 2023-07-23 mark err = NULL;
3375 99301cec 2023-07-24 stsp }
3376 99301cec 2023-07-24 stsp } else
3377 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3378 c935fd51 2023-07-23 mark
3379 c935fd51 2023-07-23 mark done:
3380 c935fd51 2023-07-23 mark free(head_id);
3381 c935fd51 2023-07-23 mark free(fileindex_path);
3382 c935fd51 2023-07-23 mark got_ref_close(head_ref);
3383 c935fd51 2023-07-23 mark if (fileindex != NULL)
3384 c935fd51 2023-07-23 mark got_fileindex_free(fileindex);
3385 c935fd51 2023-07-23 mark return err;
3386 c935fd51 2023-07-23 mark }
3387 c935fd51 2023-07-23 mark
3388 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3389 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3390 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3391 234035bc 2019-06-01 stsp struct got_repository *repo;
3392 234035bc 2019-06-01 stsp };
3393 234035bc 2019-06-01 stsp
3394 234035bc 2019-06-01 stsp static const struct got_error *
3395 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3396 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3397 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3398 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3399 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3400 234035bc 2019-06-01 stsp {
3401 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3402 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3403 234035bc 2019-06-01 stsp unsigned char status;
3404 234035bc 2019-06-01 stsp struct stat sb;
3405 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3406 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3407 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3408 234035bc 2019-06-01 stsp char *ondisk_path;
3409 234035bc 2019-06-01 stsp
3410 69de9dd4 2021-09-03 stsp if (id == NULL)
3411 69de9dd4 2021-09-03 stsp return NULL;
3412 234035bc 2019-06-01 stsp
3413 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3414 69de9dd4 2021-09-03 stsp if (ie == NULL)
3415 69de9dd4 2021-09-03 stsp return NULL;
3416 69de9dd4 2021-09-03 stsp
3417 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3418 234035bc 2019-06-01 stsp == -1)
3419 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3420 234035bc 2019-06-01 stsp
3421 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3422 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3423 5546d466 2021-09-02 stsp free(ondisk_path);
3424 234035bc 2019-06-01 stsp if (err)
3425 234035bc 2019-06-01 stsp return err;
3426 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3427 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3428 234035bc 2019-06-01 stsp
3429 234035bc 2019-06-01 stsp return NULL;
3430 234035bc 2019-06-01 stsp }
3431 234035bc 2019-06-01 stsp
3432 818c7501 2019-07-11 stsp static const struct got_error *
3433 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3434 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3435 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3436 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3437 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3438 234035bc 2019-06-01 stsp {
3439 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3440 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3441 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3442 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3443 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3444 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3445 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3446 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3447 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3448 234035bc 2019-06-01 stsp
3449 03415a1a 2019-06-02 stsp if (commit_id1) {
3450 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3451 a44927cc 2022-04-07 stsp if (err)
3452 a44927cc 2022-04-07 stsp goto done;
3453 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3454 03415a1a 2019-06-02 stsp worktree->path_prefix);
3455 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3456 03415a1a 2019-06-02 stsp goto done;
3457 69d57f3d 2020-07-31 stsp }
3458 69d57f3d 2020-07-31 stsp if (tree_id1) {
3459 69d57f3d 2020-07-31 stsp char *id_str;
3460 03415a1a 2019-06-02 stsp
3461 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3462 03415a1a 2019-06-02 stsp if (err)
3463 03415a1a 2019-06-02 stsp goto done;
3464 f69721c3 2019-10-21 stsp
3465 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3466 f69721c3 2019-10-21 stsp if (err)
3467 f69721c3 2019-10-21 stsp goto done;
3468 f69721c3 2019-10-21 stsp
3469 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3470 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3471 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3472 f69721c3 2019-10-21 stsp free(id_str);
3473 f69721c3 2019-10-21 stsp goto done;
3474 f69721c3 2019-10-21 stsp }
3475 f69721c3 2019-10-21 stsp free(id_str);
3476 b72706c3 2022-06-01 stsp
3477 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3478 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3479 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3480 b72706c3 2022-06-01 stsp goto done;
3481 b72706c3 2022-06-01 stsp }
3482 03415a1a 2019-06-02 stsp }
3483 234035bc 2019-06-01 stsp
3484 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3485 a44927cc 2022-04-07 stsp if (err)
3486 a44927cc 2022-04-07 stsp goto done;
3487 a44927cc 2022-04-07 stsp
3488 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3489 234035bc 2019-06-01 stsp worktree->path_prefix);
3490 234035bc 2019-06-01 stsp if (err)
3491 234035bc 2019-06-01 stsp goto done;
3492 234035bc 2019-06-01 stsp
3493 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3494 234035bc 2019-06-01 stsp if (err)
3495 234035bc 2019-06-01 stsp goto done;
3496 234035bc 2019-06-01 stsp
3497 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3498 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3499 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3500 f9d37699 2022-06-28 stsp goto done;
3501 f9d37699 2022-06-28 stsp }
3502 f9d37699 2022-06-28 stsp
3503 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3504 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3505 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3506 b72706c3 2022-06-01 stsp goto done;
3507 b72706c3 2022-06-01 stsp }
3508 b72706c3 2022-06-01 stsp
3509 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3510 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3511 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3512 f9d37699 2022-06-28 stsp goto done;
3513 f9d37699 2022-06-28 stsp }
3514 f9d37699 2022-06-28 stsp
3515 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3516 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3517 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3518 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3519 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3520 69de9dd4 2021-09-03 stsp if (err)
3521 69de9dd4 2021-09-03 stsp goto done;
3522 69de9dd4 2021-09-03 stsp
3523 234035bc 2019-06-01 stsp arg.worktree = worktree;
3524 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3525 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3526 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3527 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3528 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3529 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3530 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3531 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3532 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3533 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3534 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3535 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3536 af12c6b9 2019-06-04 stsp err = sync_err;
3537 234035bc 2019-06-01 stsp done:
3538 a44927cc 2022-04-07 stsp if (commit1)
3539 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3540 a44927cc 2022-04-07 stsp if (commit2)
3541 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3542 234035bc 2019-06-01 stsp if (tree1)
3543 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3544 234035bc 2019-06-01 stsp if (tree2)
3545 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3546 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3547 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3548 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3549 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3550 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3551 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3552 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3553 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3554 f69721c3 2019-10-21 stsp free(label_orig);
3555 818c7501 2019-07-11 stsp return err;
3556 818c7501 2019-07-11 stsp }
3557 234035bc 2019-06-01 stsp
3558 818c7501 2019-07-11 stsp const struct got_error *
3559 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3560 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3561 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3562 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3563 818c7501 2019-07-11 stsp {
3564 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3565 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3566 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3567 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3568 818c7501 2019-07-11 stsp
3569 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3570 818c7501 2019-07-11 stsp if (err)
3571 818c7501 2019-07-11 stsp return err;
3572 818c7501 2019-07-11 stsp
3573 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3574 818c7501 2019-07-11 stsp if (err)
3575 818c7501 2019-07-11 stsp goto done;
3576 818c7501 2019-07-11 stsp
3577 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3578 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3579 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3580 cdbfe5d2 2023-07-24 stsp
3581 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3582 cdbfe5d2 2023-07-24 stsp &cma);
3583 818c7501 2019-07-11 stsp if (err)
3584 818c7501 2019-07-11 stsp goto done;
3585 818c7501 2019-07-11 stsp
3586 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3587 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3588 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3589 818c7501 2019-07-11 stsp done:
3590 818c7501 2019-07-11 stsp if (fileindex)
3591 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3592 818c7501 2019-07-11 stsp free(fileindex_path);
3593 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3594 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3595 234035bc 2019-06-01 stsp err = unlockerr;
3596 234035bc 2019-06-01 stsp return err;
3597 234035bc 2019-06-01 stsp }
3598 234035bc 2019-06-01 stsp
3599 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3600 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3601 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3602 927df6b7 2019-02-10 stsp const char *status_path;
3603 927df6b7 2019-02-10 stsp size_t status_path_len;
3604 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3605 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3606 f8d1f275 2019-02-04 stsp void *status_arg;
3607 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3608 f8d1f275 2019-02-04 stsp void *cancel_arg;
3609 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3610 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3611 f2a9dc41 2019-12-13 tracey int report_unchanged;
3612 3143d852 2020-06-25 stsp int no_ignores;
3613 f8d1f275 2019-02-04 stsp };
3614 88d0e355 2019-08-03 stsp
3615 f8d1f275 2019-02-04 stsp static const struct got_error *
3616 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3617 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3618 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3619 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3620 927df6b7 2019-02-10 stsp {
3621 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3622 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3623 2c4740ad 2023-02-12 mark unsigned char staged_status;
3624 927df6b7 2019-02-10 stsp struct stat sb;
3625 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3626 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3627 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3628 927df6b7 2019-02-10 stsp
3629 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3630 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3631 98eaaa12 2019-08-03 stsp if (err)
3632 98eaaa12 2019-08-03 stsp return err;
3633 98eaaa12 2019-08-03 stsp
3634 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3635 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3636 98eaaa12 2019-08-03 stsp return NULL;
3637 98eaaa12 2019-08-03 stsp
3638 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3639 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3640 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3641 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3642 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3643 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3644 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3645 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3646 98eaaa12 2019-08-03 stsp }
3647 98eaaa12 2019-08-03 stsp
3648 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3649 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3650 927df6b7 2019-02-10 stsp }
3651 927df6b7 2019-02-10 stsp
3652 927df6b7 2019-02-10 stsp static const struct got_error *
3653 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3654 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3655 f8d1f275 2019-02-04 stsp {
3656 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3657 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3658 f8d1f275 2019-02-04 stsp char *abspath;
3659 f8d1f275 2019-02-04 stsp
3660 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3661 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3662 f6b8c3c2 2023-07-24 stsp if (err)
3663 f6b8c3c2 2023-07-24 stsp return err;
3664 f6b8c3c2 2023-07-24 stsp }
3665 0584f854 2019-04-06 stsp
3666 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3667 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3668 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3669 927df6b7 2019-02-10 stsp return NULL;
3670 927df6b7 2019-02-10 stsp
3671 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3672 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3673 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3674 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3675 f8d1f275 2019-02-04 stsp } else {
3676 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3677 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3678 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3679 f8d1f275 2019-02-04 stsp }
3680 f8d1f275 2019-02-04 stsp
3681 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3682 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3683 f8d1f275 2019-02-04 stsp free(abspath);
3684 9d31a1d8 2018-03-11 stsp return err;
3685 9d31a1d8 2018-03-11 stsp }
3686 f8d1f275 2019-02-04 stsp
3687 f8d1f275 2019-02-04 stsp static const struct got_error *
3688 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3689 f8d1f275 2019-02-04 stsp {
3690 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
3691 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3692 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3693 2ec1f75b 2019-03-26 stsp unsigned char status;
3694 927df6b7 2019-02-10 stsp
3695 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3696 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3697 f6b8c3c2 2023-07-24 stsp if (err)
3698 f6b8c3c2 2023-07-24 stsp return err;
3699 f6b8c3c2 2023-07-24 stsp }
3700 0584f854 2019-04-06 stsp
3701 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3702 927df6b7 2019-02-10 stsp return NULL;
3703 927df6b7 2019-02-10 stsp
3704 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3705 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3706 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3707 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3708 2ec1f75b 2019-03-26 stsp else
3709 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3710 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3711 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3712 6841da00 2019-08-08 stsp }
3713 6841da00 2019-08-08 stsp
3714 336075a4 2022-06-25 op static void
3715 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3716 6841da00 2019-08-08 stsp {
3717 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3718 6841da00 2019-08-08 stsp
3719 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3720 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3721 d8bacb93 2023-01-10 mark
3722 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3723 6841da00 2019-08-08 stsp }
3724 8fd0d196 2023-12-08 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3725 6841da00 2019-08-08 stsp }
3726 6841da00 2019-08-08 stsp
3727 6841da00 2019-08-08 stsp static const struct got_error *
3728 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3729 6841da00 2019-08-08 stsp {
3730 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3731 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3732 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3733 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3734 6841da00 2019-08-08 stsp size_t linesize = 0;
3735 6841da00 2019-08-08 stsp ssize_t linelen;
3736 6841da00 2019-08-08 stsp
3737 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3738 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3739 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3740 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3741 6841da00 2019-08-08 stsp
3742 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3743 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3744 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3745 bd8de430 2019-10-04 stsp
3746 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3747 bd8de430 2019-10-04 stsp if (line[0] == '#')
3748 bd8de430 2019-10-04 stsp continue;
3749 bd8de430 2019-10-04 stsp
3750 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3751 bd8de430 2019-10-04 stsp if (line[0] == '!')
3752 bd8de430 2019-10-04 stsp continue;
3753 bd8de430 2019-10-04 stsp
3754 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3755 6841da00 2019-08-08 stsp line) == -1) {
3756 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3757 6841da00 2019-08-08 stsp goto done;
3758 6841da00 2019-08-08 stsp }
3759 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3760 6841da00 2019-08-08 stsp if (err)
3761 6841da00 2019-08-08 stsp goto done;
3762 6841da00 2019-08-08 stsp }
3763 6841da00 2019-08-08 stsp if (ferror(f)) {
3764 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3765 6841da00 2019-08-08 stsp goto done;
3766 6841da00 2019-08-08 stsp }
3767 6841da00 2019-08-08 stsp
3768 6841da00 2019-08-08 stsp dirpath = strdup(path);
3769 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3770 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3771 6841da00 2019-08-08 stsp goto done;
3772 6841da00 2019-08-08 stsp }
3773 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3774 6841da00 2019-08-08 stsp done:
3775 6841da00 2019-08-08 stsp free(line);
3776 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3777 6841da00 2019-08-08 stsp free(dirpath);
3778 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3779 8fd0d196 2023-12-08 mark free(ignorelist);
3780 6841da00 2019-08-08 stsp }
3781 6841da00 2019-08-08 stsp return err;
3782 249b637c 2023-02-20 stsp }
3783 249b637c 2023-02-20 stsp
3784 249b637c 2023-02-20 stsp static int
3785 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3786 249b637c 2023-02-20 stsp int flags)
3787 249b637c 2023-02-20 stsp {
3788 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3789 249b637c 2023-02-20 stsp
3790 249b637c 2023-02-20 stsp /*
3791 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3792 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3793 249b637c 2023-02-20 stsp */
3794 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3795 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3796 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3797 249b637c 2023-02-20 stsp
3798 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3799 249b637c 2023-02-20 stsp }
3800 249b637c 2023-02-20 stsp
3801 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3802 6841da00 2019-08-08 stsp }
3803 6841da00 2019-08-08 stsp
3804 336075a4 2022-06-25 op static int
3805 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3806 6841da00 2019-08-08 stsp {
3807 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3808 bd8de430 2019-10-04 stsp
3809 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3810 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3811 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3812 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3813 bd8de430 2019-10-04 stsp
3814 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3815 249b637c 2023-02-20 stsp const char *p;
3816 6841da00 2019-08-08 stsp
3817 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3818 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3819 bd8de430 2019-10-04 stsp continue;
3820 bd8de430 2019-10-04 stsp p = path;
3821 bd8de430 2019-10-04 stsp while (*p) {
3822 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3823 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3824 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3825 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3826 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3827 bd8de430 2019-10-04 stsp p++;
3828 bd8de430 2019-10-04 stsp while (*p == '/')
3829 bd8de430 2019-10-04 stsp p++;
3830 bd8de430 2019-10-04 stsp continue;
3831 bd8de430 2019-10-04 stsp }
3832 bd8de430 2019-10-04 stsp return 1;
3833 bd8de430 2019-10-04 stsp }
3834 bd8de430 2019-10-04 stsp }
3835 bd8de430 2019-10-04 stsp }
3836 bd8de430 2019-10-04 stsp
3837 6841da00 2019-08-08 stsp /*
3838 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3839 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3840 6841da00 2019-08-08 stsp * ignores backwards.
3841 6841da00 2019-08-08 stsp */
3842 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3843 6841da00 2019-08-08 stsp while (pe) {
3844 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3845 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3846 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3847 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3848 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3849 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3850 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3851 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3852 249b637c 2023-02-20 stsp path, flags))
3853 6841da00 2019-08-08 stsp continue;
3854 6841da00 2019-08-08 stsp return 1;
3855 6841da00 2019-08-08 stsp }
3856 6841da00 2019-08-08 stsp }
3857 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3858 6841da00 2019-08-08 stsp }
3859 6841da00 2019-08-08 stsp
3860 6841da00 2019-08-08 stsp return 0;
3861 6841da00 2019-08-08 stsp }
3862 6841da00 2019-08-08 stsp
3863 6841da00 2019-08-08 stsp static const struct got_error *
3864 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3865 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3866 6841da00 2019-08-08 stsp {
3867 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3868 6841da00 2019-08-08 stsp char *ignorespath;
3869 886cec17 2019-12-15 stsp int fd = -1;
3870 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3871 6841da00 2019-08-08 stsp
3872 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3873 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3874 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3875 6841da00 2019-08-08 stsp
3876 886cec17 2019-12-15 stsp if (dirfd != -1) {
3877 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3878 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3879 886cec17 2019-12-15 stsp if (fd == -1) {
3880 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3881 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3882 886cec17 2019-12-15 stsp ignorespath);
3883 886cec17 2019-12-15 stsp } else {
3884 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3885 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3886 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3887 886cec17 2019-12-15 stsp ignorespath);
3888 886cec17 2019-12-15 stsp else {
3889 886cec17 2019-12-15 stsp fd = -1;
3890 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3891 886cec17 2019-12-15 stsp }
3892 886cec17 2019-12-15 stsp }
3893 886cec17 2019-12-15 stsp } else {
3894 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3895 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3896 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3897 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3898 886cec17 2019-12-15 stsp ignorespath);
3899 886cec17 2019-12-15 stsp } else
3900 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3901 886cec17 2019-12-15 stsp }
3902 6841da00 2019-08-08 stsp
3903 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3904 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3905 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3906 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3907 6841da00 2019-08-08 stsp free(ignorespath);
3908 6841da00 2019-08-08 stsp return err;
3909 f8d1f275 2019-02-04 stsp }
3910 f8d1f275 2019-02-04 stsp
3911 f8d1f275 2019-02-04 stsp static const struct got_error *
3912 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3913 62da3196 2021-10-01 stsp int dirfd)
3914 f8d1f275 2019-02-04 stsp {
3915 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3916 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3917 f8d1f275 2019-02-04 stsp char *path = NULL;
3918 62da3196 2021-10-01 stsp
3919 62da3196 2021-10-01 stsp if (ignore != NULL)
3920 62da3196 2021-10-01 stsp *ignore = 0;
3921 f8d1f275 2019-02-04 stsp
3922 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3923 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3924 f6b8c3c2 2023-07-24 stsp if (err)
3925 f6b8c3c2 2023-07-24 stsp return err;
3926 f6b8c3c2 2023-07-24 stsp }
3927 0584f854 2019-04-06 stsp
3928 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3929 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3930 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3931 f8d1f275 2019-02-04 stsp } else {
3932 f8d1f275 2019-02-04 stsp path = de->d_name;
3933 f8d1f275 2019-02-04 stsp }
3934 f8d1f275 2019-02-04 stsp
3935 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3936 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3937 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3938 62da3196 2021-10-01 stsp *ignore = 1;
3939 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3940 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3941 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3942 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3943 f8d1f275 2019-02-04 stsp if (parent_path[0])
3944 f8d1f275 2019-02-04 stsp free(path);
3945 b72f483a 2019-02-05 stsp return err;
3946 f8d1f275 2019-02-04 stsp }
3947 f8d1f275 2019-02-04 stsp
3948 347d1d3e 2019-07-12 stsp static const struct got_error *
3949 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3950 3143d852 2020-06-25 stsp {
3951 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3952 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3953 3143d852 2020-06-25 stsp
3954 3143d852 2020-06-25 stsp if (a->no_ignores)
3955 3143d852 2020-06-25 stsp return NULL;
3956 3143d852 2020-06-25 stsp
3957 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3958 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3959 3143d852 2020-06-25 stsp if (err)
3960 3143d852 2020-06-25 stsp return err;
3961 3143d852 2020-06-25 stsp
3962 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3963 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3964 3143d852 2020-06-25 stsp
3965 3143d852 2020-06-25 stsp return err;
3966 3143d852 2020-06-25 stsp }
3967 3143d852 2020-06-25 stsp
3968 3143d852 2020-06-25 stsp static const struct got_error *
3969 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3970 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3971 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3972 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3973 abb4604f 2019-07-27 stsp {
3974 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3975 abb4604f 2019-07-27 stsp struct stat sb;
3976 ff56836b 2021-07-08 stsp
3977 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3978 abb4604f 2019-07-27 stsp if (ie)
3979 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3980 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3981 abb4604f 2019-07-27 stsp
3982 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3983 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3984 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3985 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3986 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3987 abb4604f 2019-07-27 stsp }
3988 abb4604f 2019-07-27 stsp
3989 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3990 916237f3 2022-02-11 stsp return NULL;
3991 916237f3 2022-02-11 stsp
3992 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3993 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3994 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3995 abb4604f 2019-07-27 stsp
3996 abb4604f 2019-07-27 stsp return NULL;
3997 3143d852 2020-06-25 stsp }
3998 3143d852 2020-06-25 stsp
3999 3143d852 2020-06-25 stsp static const struct got_error *
4000 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4001 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4002 3143d852 2020-06-25 stsp {
4003 3143d852 2020-06-25 stsp const struct got_error *err;
4004 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4005 3143d852 2020-06-25 stsp
4006 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4007 3143d852 2020-06-25 stsp ".cvsignore");
4008 3143d852 2020-06-25 stsp if (err)
4009 3143d852 2020-06-25 stsp return err;
4010 3143d852 2020-06-25 stsp
4011 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4012 3143d852 2020-06-25 stsp ".gitignore");
4013 3143d852 2020-06-25 stsp if (err)
4014 3143d852 2020-06-25 stsp return err;
4015 3143d852 2020-06-25 stsp
4016 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4017 3143d852 2020-06-25 stsp if (err) {
4018 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4019 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4020 3143d852 2020-06-25 stsp return err;
4021 3143d852 2020-06-25 stsp }
4022 3143d852 2020-06-25 stsp for (;;) {
4023 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4024 3143d852 2020-06-25 stsp ".cvsignore");
4025 3143d852 2020-06-25 stsp if (err)
4026 3143d852 2020-06-25 stsp break;
4027 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4028 3143d852 2020-06-25 stsp ".gitignore");
4029 3143d852 2020-06-25 stsp if (err)
4030 3143d852 2020-06-25 stsp break;
4031 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4032 3143d852 2020-06-25 stsp if (err) {
4033 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4034 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4035 3143d852 2020-06-25 stsp break;
4036 3143d852 2020-06-25 stsp }
4037 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4038 ff56836b 2021-07-08 stsp break;
4039 b737c85a 2020-06-26 stsp free(parent_path);
4040 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4041 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4042 3143d852 2020-06-25 stsp }
4043 3143d852 2020-06-25 stsp
4044 b737c85a 2020-06-26 stsp free(parent_path);
4045 b737c85a 2020-06-26 stsp free(next_parent_path);
4046 3143d852 2020-06-25 stsp return err;
4047 abb4604f 2019-07-27 stsp }
4048 31cf15ec 2023-04-12 stsp
4049 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
4050 31cf15ec 2023-04-12 stsp const char *parent_path;
4051 31cf15ec 2023-04-12 stsp size_t parent_len;
4052 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
4053 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
4054 31cf15ec 2023-04-12 stsp void *cancel_arg;
4055 31cf15ec 2023-04-12 stsp };
4056 31cf15ec 2023-04-12 stsp
4057 abb4604f 2019-07-27 stsp static const struct got_error *
4058 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
4059 31cf15ec 2023-04-12 stsp {
4060 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4061 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
4062 31cf15ec 2023-04-12 stsp
4063 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
4064 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
4065 31cf15ec 2023-04-12 stsp if (err)
4066 31cf15ec 2023-04-12 stsp return err;
4067 31cf15ec 2023-04-12 stsp }
4068 31cf15ec 2023-04-12 stsp
4069 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4070 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
4071 31cf15ec 2023-04-12 stsp
4072 31cf15ec 2023-04-12 stsp return err;
4073 31cf15ec 2023-04-12 stsp }
4074 31cf15ec 2023-04-12 stsp
4075 31cf15ec 2023-04-12 stsp static const struct got_error *
4076 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
4077 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4078 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
4079 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
4080 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4081 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4082 31cf15ec 2023-04-12 stsp {
4083 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4084 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
4085 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
4086 31cf15ec 2023-04-12 stsp
4087 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
4088 31cf15ec 2023-04-12 stsp if (cancel_cb) {
4089 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
4090 31cf15ec 2023-04-12 stsp if (err)
4091 31cf15ec 2023-04-12 stsp break;
4092 31cf15ec 2023-04-12 stsp }
4093 31cf15ec 2023-04-12 stsp
4094 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4095 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
4096 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
4097 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4098 31cf15ec 2023-04-12 stsp break;
4099 31cf15ec 2023-04-12 stsp }
4100 31cf15ec 2023-04-12 stsp
4101 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
4102 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
4103 31cf15ec 2023-04-12 stsp ignores, no_ignores);
4104 31cf15ec 2023-04-12 stsp if (err)
4105 31cf15ec 2023-04-12 stsp break;
4106 31cf15ec 2023-04-12 stsp
4107 31cf15ec 2023-04-12 stsp free(ondisk_path);
4108 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4109 31cf15ec 2023-04-12 stsp }
4110 31cf15ec 2023-04-12 stsp
4111 31cf15ec 2023-04-12 stsp free(ondisk_path);
4112 31cf15ec 2023-04-12 stsp return err;
4113 31cf15ec 2023-04-12 stsp }
4114 31cf15ec 2023-04-12 stsp
4115 31cf15ec 2023-04-12 stsp static const struct got_error *
4116 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4117 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4118 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4119 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4120 f2a9dc41 2019-12-13 tracey int report_unchanged)
4121 f8d1f275 2019-02-04 stsp {
4122 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4123 6fc93f37 2019-12-13 stsp int fd = -1;
4124 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4125 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4126 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4127 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
4128 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
4129 3143d852 2020-06-25 stsp
4130 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4131 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
4132 f8d1f275 2019-02-04 stsp
4133 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4134 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4135 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4136 8dc303cc 2019-07-27 stsp
4137 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4138 a84c0d30 2022-03-12 stsp if (ie) {
4139 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
4140 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
4141 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
4142 a84c0d30 2022-03-12 stsp goto done;
4143 31cf15ec 2023-04-12 stsp } else {
4144 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
4145 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
4146 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
4147 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
4148 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
4149 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
4150 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
4151 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
4152 31cf15ec 2023-04-12 stsp if (err)
4153 31cf15ec 2023-04-12 stsp goto done;
4154 a84c0d30 2022-03-12 stsp }
4155 a84c0d30 2022-03-12 stsp
4156 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4157 6fc93f37 2019-12-13 stsp if (fd == -1) {
4158 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4159 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
4160 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4161 ff56836b 2021-07-08 stsp else {
4162 ff56836b 2021-07-08 stsp if (!no_ignores) {
4163 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4164 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4165 ff56836b 2021-07-08 stsp if (err)
4166 ff56836b 2021-07-08 stsp goto done;
4167 ff56836b 2021-07-08 stsp }
4168 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4169 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4170 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4171 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4172 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4173 31cf15ec 2023-04-12 stsp if (err)
4174 31cf15ec 2023-04-12 stsp goto done;
4175 31cf15ec 2023-04-12 stsp } else {
4176 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4177 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4178 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4179 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4180 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4181 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4182 46108e23 2023-04-12 stsp if (err)
4183 46108e23 2023-04-12 stsp goto done;
4184 31cf15ec 2023-04-12 stsp }
4185 ff56836b 2021-07-08 stsp }
4186 abb4604f 2019-07-27 stsp } else {
4187 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4188 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4189 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4190 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4191 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4192 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4193 abb4604f 2019-07-27 stsp arg.status_path = path;
4194 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4195 abb4604f 2019-07-27 stsp arg.repo = repo;
4196 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4197 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4198 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4199 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4200 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4201 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4202 022fae89 2019-12-06 tracey if (!no_ignores) {
4203 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4204 3143d852 2020-06-25 stsp worktree->root_path, path);
4205 3143d852 2020-06-25 stsp if (err)
4206 3143d852 2020-06-25 stsp goto done;
4207 022fae89 2019-12-06 tracey }
4208 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4209 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4210 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4211 927df6b7 2019-02-10 stsp }
4212 3143d852 2020-06-25 stsp done:
4213 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4214 9e83097f 2023-12-05 mark got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4215 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4216 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4217 927df6b7 2019-02-10 stsp free(ondisk_path);
4218 347d1d3e 2019-07-12 stsp return err;
4219 347d1d3e 2019-07-12 stsp }
4220 347d1d3e 2019-07-12 stsp
4221 347d1d3e 2019-07-12 stsp const struct got_error *
4222 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4223 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4224 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4225 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4226 347d1d3e 2019-07-12 stsp {
4227 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4228 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4229 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4230 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4231 347d1d3e 2019-07-12 stsp
4232 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4233 347d1d3e 2019-07-12 stsp if (err)
4234 347d1d3e 2019-07-12 stsp return err;
4235 347d1d3e 2019-07-12 stsp
4236 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4237 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4238 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4239 f6343036 2021-06-22 stsp no_ignores, 0);
4240 72ea6654 2019-07-27 stsp if (err)
4241 72ea6654 2019-07-27 stsp break;
4242 72ea6654 2019-07-27 stsp }
4243 f8d1f275 2019-02-04 stsp free(fileindex_path);
4244 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4245 f8d1f275 2019-02-04 stsp return err;
4246 f8d1f275 2019-02-04 stsp }
4247 6c7ab921 2019-03-18 stsp
4248 6c7ab921 2019-03-18 stsp const struct got_error *
4249 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4250 6c7ab921 2019-03-18 stsp const char *arg)
4251 6c7ab921 2019-03-18 stsp {
4252 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4253 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4254 6c7ab921 2019-03-18 stsp size_t len;
4255 00bb5ea0 2020-07-23 stsp struct stat sb;
4256 01740607 2020-11-04 stsp char *abspath = NULL;
4257 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4258 6c7ab921 2019-03-18 stsp
4259 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4260 6c7ab921 2019-03-18 stsp
4261 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4262 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4263 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4264 00bb5ea0 2020-07-23 stsp
4265 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4266 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4267 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4268 00bb5ea0 2020-07-23 stsp goto done;
4269 00bb5ea0 2020-07-23 stsp }
4270 727173c3 2020-11-06 stsp sb.st_mode = 0;
4271 00bb5ea0 2020-07-23 stsp }
4272 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4273 00bb5ea0 2020-07-23 stsp /*
4274 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4275 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4276 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4277 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4278 00bb5ea0 2020-07-23 stsp */
4279 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4280 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4281 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4282 00bb5ea0 2020-07-23 stsp goto done;
4283 00bb5ea0 2020-07-23 stsp }
4284 00bb5ea0 2020-07-23 stsp
4285 00bb5ea0 2020-07-23 stsp }
4286 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4287 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4288 00bb5ea0 2020-07-23 stsp if (err)
4289 d0710d08 2019-07-22 stsp goto done;
4290 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4291 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4292 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4293 00bb5ea0 2020-07-23 stsp goto done;
4294 00bb5ea0 2020-07-23 stsp }
4295 00bb5ea0 2020-07-23 stsp } else {
4296 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4297 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4298 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4299 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4300 00bb5ea0 2020-07-23 stsp goto done;
4301 00bb5ea0 2020-07-23 stsp }
4302 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4303 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4304 01740607 2020-11-04 stsp goto done;
4305 01740607 2020-11-04 stsp }
4306 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4307 01740607 2020-11-04 stsp sizeof(canonpath));
4308 01740607 2020-11-04 stsp if (err)
4309 00bb5ea0 2020-07-23 stsp goto done;
4310 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4311 01740607 2020-11-04 stsp if (resolved == NULL) {
4312 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4313 01740607 2020-11-04 stsp goto done;
4314 00bb5ea0 2020-07-23 stsp }
4315 d0710d08 2019-07-22 stsp }
4316 d0710d08 2019-07-22 stsp }
4317 6c7ab921 2019-03-18 stsp
4318 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4319 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4320 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4321 6c7ab921 2019-03-18 stsp goto done;
4322 6c7ab921 2019-03-18 stsp }
4323 6c7ab921 2019-03-18 stsp
4324 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4325 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4326 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4327 f6d88e1a 2019-05-29 stsp if (err)
4328 f6d88e1a 2019-05-29 stsp goto done;
4329 f6d88e1a 2019-05-29 stsp } else {
4330 f6d88e1a 2019-05-29 stsp path = strdup("");
4331 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4332 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4333 f6d88e1a 2019-05-29 stsp goto done;
4334 f6d88e1a 2019-05-29 stsp }
4335 6c7ab921 2019-03-18 stsp }
4336 6c7ab921 2019-03-18 stsp
4337 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4338 6c7ab921 2019-03-18 stsp len = strlen(path);
4339 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4340 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4341 6c7ab921 2019-03-18 stsp len--;
4342 6c7ab921 2019-03-18 stsp }
4343 6c7ab921 2019-03-18 stsp done:
4344 01740607 2020-11-04 stsp free(abspath);
4345 6c7ab921 2019-03-18 stsp free(resolved);
4346 d0710d08 2019-07-22 stsp free(cwd);
4347 6c7ab921 2019-03-18 stsp if (err == NULL)
4348 6c7ab921 2019-03-18 stsp *wt_path = path;
4349 6c7ab921 2019-03-18 stsp else
4350 6c7ab921 2019-03-18 stsp free(path);
4351 d00136be 2019-03-26 stsp return err;
4352 d00136be 2019-03-26 stsp }
4353 d00136be 2019-03-26 stsp
4354 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4355 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4356 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4357 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4358 4e68cba3 2019-11-23 stsp void *progress_arg;
4359 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4360 4e68cba3 2019-11-23 stsp };
4361 4e68cba3 2019-11-23 stsp
4362 b88936d3 2023-06-21 stsp static int
4363 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4364 b88936d3 2023-06-21 stsp {
4365 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4366 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4367 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4368 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4369 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4370 b88936d3 2023-06-21 stsp }
4371 b88936d3 2023-06-21 stsp
4372 4e68cba3 2019-11-23 stsp static const struct got_error *
4373 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4374 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4375 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4376 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4377 07f5b47a 2019-06-02 stsp {
4378 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4379 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4380 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4381 6d022e97 2019-08-04 stsp struct stat sb;
4382 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4383 07f5b47a 2019-06-02 stsp
4384 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4385 dbb83fbd 2019-12-12 stsp relpath) == -1)
4386 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4387 dbb83fbd 2019-12-12 stsp
4388 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4389 6d022e97 2019-08-04 stsp if (ie) {
4390 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4391 12463d8b 2019-12-13 stsp de_name, a->repo);
4392 6d022e97 2019-08-04 stsp if (err)
4393 dbb83fbd 2019-12-12 stsp goto done;
4394 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4395 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4396 b88936d3 2023-06-21 stsp add_noop_status(status))
4397 dbb83fbd 2019-12-12 stsp goto done;
4398 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4399 dbb83fbd 2019-12-12 stsp if (err)
4400 dbb83fbd 2019-12-12 stsp goto done;
4401 6d022e97 2019-08-04 stsp }
4402 07f5b47a 2019-06-02 stsp
4403 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4404 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4405 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4406 9b4603c0 2022-01-31 stsp else
4407 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4408 dbb83fbd 2019-12-12 stsp goto done;
4409 4e68cba3 2019-11-23 stsp }
4410 07f5b47a 2019-06-02 stsp
4411 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4412 4e68cba3 2019-11-23 stsp if (err)
4413 4e68cba3 2019-11-23 stsp goto done;
4414 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4415 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4416 3969253a 2020-03-07 stsp if (err) {
4417 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4418 3969253a 2020-03-07 stsp goto done;
4419 3969253a 2020-03-07 stsp }
4420 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4421 07f5b47a 2019-06-02 stsp if (err) {
4422 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4423 4e68cba3 2019-11-23 stsp goto done;
4424 07f5b47a 2019-06-02 stsp }
4425 4e68cba3 2019-11-23 stsp done:
4426 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4427 dbb83fbd 2019-12-12 stsp if (err)
4428 dbb83fbd 2019-12-12 stsp return err;
4429 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4430 dbb83fbd 2019-12-12 stsp return NULL;
4431 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4432 07f5b47a 2019-06-02 stsp }
4433 07f5b47a 2019-06-02 stsp
4434 d00136be 2019-03-26 stsp const struct got_error *
4435 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4436 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4437 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4438 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4439 d00136be 2019-03-26 stsp {
4440 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4441 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4442 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4443 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4444 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4445 d00136be 2019-03-26 stsp
4446 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4447 d00136be 2019-03-26 stsp if (err)
4448 d00136be 2019-03-26 stsp return err;
4449 d00136be 2019-03-26 stsp
4450 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4451 d00136be 2019-03-26 stsp if (err)
4452 d00136be 2019-03-26 stsp goto done;
4453 d00136be 2019-03-26 stsp
4454 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4455 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4456 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4457 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4458 4e68cba3 2019-11-23 stsp saa.repo = repo;
4459 4e68cba3 2019-11-23 stsp
4460 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4461 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4462 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4463 1dd54920 2019-05-11 stsp if (err)
4464 af12c6b9 2019-06-04 stsp break;
4465 1dd54920 2019-05-11 stsp }
4466 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4467 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4468 af12c6b9 2019-06-04 stsp err = sync_err;
4469 d00136be 2019-03-26 stsp done:
4470 fb399478 2019-07-12 stsp free(fileindex_path);
4471 d00136be 2019-03-26 stsp if (fileindex)
4472 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4473 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4474 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4475 d00136be 2019-03-26 stsp err = unlockerr;
4476 6c7ab921 2019-03-18 stsp return err;
4477 6c7ab921 2019-03-18 stsp }
4478 17ed4618 2019-06-02 stsp
4479 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4480 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4481 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4482 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4483 f2a9dc41 2019-12-13 tracey void *progress_arg;
4484 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4485 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4486 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4487 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4488 7f4e5320 2023-05-29 stsp const char *status_path;
4489 7f4e5320 2023-05-29 stsp size_t status_path_len;
4490 766841c2 2020-08-13 stsp const char *status_codes;
4491 f2a9dc41 2019-12-13 tracey };
4492 f2a9dc41 2019-12-13 tracey
4493 f2a9dc41 2019-12-13 tracey static const struct got_error *
4494 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4495 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4496 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4497 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4498 17ed4618 2019-06-02 stsp {
4499 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4500 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4501 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4502 17ed4618 2019-06-02 stsp struct stat sb;
4503 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4504 4e12cd97 2022-01-25 stsp
4505 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4506 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4507 4e12cd97 2022-01-25 stsp return NULL;
4508 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4509 4e12cd97 2022-01-25 stsp }
4510 17ed4618 2019-06-02 stsp
4511 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4512 17ed4618 2019-06-02 stsp if (ie == NULL)
4513 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4514 2ec1f75b 2019-03-26 stsp
4515 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4516 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4517 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4518 9acbc4fa 2019-08-03 stsp return NULL;
4519 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4520 9acbc4fa 2019-08-03 stsp }
4521 9acbc4fa 2019-08-03 stsp
4522 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4523 f2a9dc41 2019-12-13 tracey relpath) == -1)
4524 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4525 f2a9dc41 2019-12-13 tracey
4526 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4527 12463d8b 2019-12-13 stsp a->repo);
4528 17ed4618 2019-06-02 stsp if (err)
4529 f2a9dc41 2019-12-13 tracey goto done;
4530 17ed4618 2019-06-02 stsp
4531 766841c2 2020-08-13 stsp if (a->status_codes) {
4532 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4533 766841c2 2020-08-13 stsp int i;
4534 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4535 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4536 766841c2 2020-08-13 stsp break;
4537 766841c2 2020-08-13 stsp }
4538 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4539 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4540 766841c2 2020-08-13 stsp free(ondisk_path);
4541 766841c2 2020-08-13 stsp return NULL;
4542 766841c2 2020-08-13 stsp }
4543 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4544 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4545 766841c2 2020-08-13 stsp static char msg[64];
4546 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4547 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4548 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4549 766841c2 2020-08-13 stsp goto done;
4550 766841c2 2020-08-13 stsp }
4551 766841c2 2020-08-13 stsp }
4552 766841c2 2020-08-13 stsp
4553 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4554 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4555 f2a9dc41 2019-12-13 tracey goto done;
4556 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4557 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4558 f2a9dc41 2019-12-13 tracey goto done;
4559 f2a9dc41 2019-12-13 tracey }
4560 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4561 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4562 4e12cd97 2022-01-25 stsp goto done;
4563 4e12cd97 2022-01-25 stsp }
4564 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4565 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4566 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4567 f2a9dc41 2019-12-13 tracey goto done;
4568 f2a9dc41 2019-12-13 tracey }
4569 17ed4618 2019-06-02 stsp }
4570 17ed4618 2019-06-02 stsp
4571 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4572 20a2ad1c 2020-10-20 stsp size_t root_len;
4573 20a2ad1c 2020-10-20 stsp
4574 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4575 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4576 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4577 12463d8b 2019-12-13 stsp ondisk_path);
4578 12463d8b 2019-12-13 stsp goto done;
4579 12463d8b 2019-12-13 stsp }
4580 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4581 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4582 12463d8b 2019-12-13 stsp goto done;
4583 15341bfd 2020-03-05 tracey }
4584 15341bfd 2020-03-05 tracey
4585 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4586 20a2ad1c 2020-10-20 stsp do {
4587 20a2ad1c 2020-10-20 stsp char *parent;
4588 7f4e5320 2023-05-29 stsp
4589 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4590 20a2ad1c 2020-10-20 stsp if (err)
4591 2513f20a 2020-10-20 stsp goto done;
4592 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4593 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4594 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4595 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4596 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4597 7f4e5320 2023-05-29 stsp a->status_path_len))
4598 7f4e5320 2023-05-29 stsp break;
4599 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4600 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4601 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4602 20a2ad1c 2020-10-20 stsp ondisk_path);
4603 15341bfd 2020-03-05 tracey break;
4604 15341bfd 2020-03-05 tracey }
4605 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4606 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4607 f2a9dc41 2019-12-13 tracey }
4608 17ed4618 2019-06-02 stsp
4609 f6635657 2023-08-25 stsp if (got_fileindex_entry_has_blob(ie))
4610 f6635657 2023-08-25 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4611 f6635657 2023-08-25 stsp else
4612 f6635657 2023-08-25 stsp got_fileindex_entry_remove(a->fileindex, ie);
4613 f2a9dc41 2019-12-13 tracey done:
4614 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4615 f2a9dc41 2019-12-13 tracey if (err)
4616 f2a9dc41 2019-12-13 tracey return err;
4617 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4618 f2a9dc41 2019-12-13 tracey return NULL;
4619 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4620 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4621 17ed4618 2019-06-02 stsp }
4622 17ed4618 2019-06-02 stsp
4623 2ec1f75b 2019-03-26 stsp const struct got_error *
4624 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4625 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4626 766841c2 2020-08-13 stsp const char *status_codes,
4627 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4628 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4629 2ec1f75b 2019-03-26 stsp {
4630 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4631 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4632 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4633 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4634 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4635 2ec1f75b 2019-03-26 stsp
4636 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4637 2ec1f75b 2019-03-26 stsp if (err)
4638 2ec1f75b 2019-03-26 stsp return err;
4639 2ec1f75b 2019-03-26 stsp
4640 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4641 2ec1f75b 2019-03-26 stsp if (err)
4642 2ec1f75b 2019-03-26 stsp goto done;
4643 f2a9dc41 2019-12-13 tracey
4644 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4645 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4646 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4647 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4648 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4649 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4650 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4651 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4652 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4653 2ec1f75b 2019-03-26 stsp
4654 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4655 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4656 7f4e5320 2023-05-29 stsp
4657 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4658 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4659 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4660 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4661 7f4e5320 2023-05-29 stsp goto done;
4662 7f4e5320 2023-05-29 stsp }
4663 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4664 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4665 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4666 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4667 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4668 17ed4618 2019-06-02 stsp if (err)
4669 af12c6b9 2019-06-04 stsp break;
4670 2ec1f75b 2019-03-26 stsp }
4671 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4672 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4673 af12c6b9 2019-06-04 stsp err = sync_err;
4674 2ec1f75b 2019-03-26 stsp done:
4675 fb399478 2019-07-12 stsp free(fileindex_path);
4676 2ec1f75b 2019-03-26 stsp if (fileindex)
4677 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4678 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4679 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4680 2ec1f75b 2019-03-26 stsp err = unlockerr;
4681 33aa809d 2019-08-08 stsp return err;
4682 33aa809d 2019-08-08 stsp }
4683 33aa809d 2019-08-08 stsp
4684 33aa809d 2019-08-08 stsp static const struct got_error *
4685 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4686 33aa809d 2019-08-08 stsp {
4687 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4688 33aa809d 2019-08-08 stsp char *line = NULL;
4689 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4690 33aa809d 2019-08-08 stsp ssize_t linelen;
4691 33aa809d 2019-08-08 stsp
4692 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4693 33aa809d 2019-08-08 stsp if (linelen == -1) {
4694 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4695 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4696 33aa809d 2019-08-08 stsp goto done;
4697 33aa809d 2019-08-08 stsp }
4698 33aa809d 2019-08-08 stsp return NULL;
4699 33aa809d 2019-08-08 stsp }
4700 33aa809d 2019-08-08 stsp if (outfile) {
4701 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4702 33aa809d 2019-08-08 stsp if (n != linelen) {
4703 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4704 33aa809d 2019-08-08 stsp goto done;
4705 33aa809d 2019-08-08 stsp }
4706 33aa809d 2019-08-08 stsp }
4707 33aa809d 2019-08-08 stsp if (rejectfile) {
4708 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4709 33aa809d 2019-08-08 stsp if (n != linelen)
4710 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4711 33aa809d 2019-08-08 stsp }
4712 33aa809d 2019-08-08 stsp done:
4713 33aa809d 2019-08-08 stsp free(line);
4714 2ec1f75b 2019-03-26 stsp return err;
4715 2ec1f75b 2019-03-26 stsp }
4716 1f1abb7e 2019-08-08 stsp
4717 33aa809d 2019-08-08 stsp static const struct got_error *
4718 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4719 33aa809d 2019-08-08 stsp {
4720 33aa809d 2019-08-08 stsp char *line = NULL;
4721 33aa809d 2019-08-08 stsp size_t linesize = 0;
4722 33aa809d 2019-08-08 stsp ssize_t linelen;
4723 33aa809d 2019-08-08 stsp
4724 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4725 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4726 500467ff 2019-09-25 hiltjo if (ferror(f))
4727 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4728 500467ff 2019-09-25 hiltjo return NULL;
4729 500467ff 2019-09-25 hiltjo }
4730 33aa809d 2019-08-08 stsp free(line);
4731 33aa809d 2019-08-08 stsp return NULL;
4732 33aa809d 2019-08-08 stsp }
4733 33aa809d 2019-08-08 stsp
4734 33aa809d 2019-08-08 stsp static const struct got_error *
4735 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4736 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4737 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4738 abc59930 2021-09-05 naddy {
4739 33aa809d 2019-08-08 stsp const struct got_error *err;
4740 33aa809d 2019-08-08 stsp
4741 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4742 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4743 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4744 33aa809d 2019-08-08 stsp if (err)
4745 33aa809d 2019-08-08 stsp return err;
4746 33aa809d 2019-08-08 stsp (*line_cur1)++;
4747 33aa809d 2019-08-08 stsp }
4748 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4749 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4750 33aa809d 2019-08-08 stsp if (rejectfile)
4751 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4752 33aa809d 2019-08-08 stsp else
4753 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4754 33aa809d 2019-08-08 stsp if (err)
4755 33aa809d 2019-08-08 stsp return err;
4756 33aa809d 2019-08-08 stsp (*line_cur2)++;
4757 33aa809d 2019-08-08 stsp }
4758 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4759 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4760 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4761 33aa809d 2019-08-08 stsp if (err)
4762 33aa809d 2019-08-08 stsp return err;
4763 33aa809d 2019-08-08 stsp (*line_cur2)++;
4764 33aa809d 2019-08-08 stsp }
4765 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4766 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4767 33aa809d 2019-08-08 stsp if (rejectfile)
4768 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4769 33aa809d 2019-08-08 stsp else
4770 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4771 33aa809d 2019-08-08 stsp if (err)
4772 33aa809d 2019-08-08 stsp return err;
4773 33aa809d 2019-08-08 stsp (*line_cur1)++;
4774 33aa809d 2019-08-08 stsp }
4775 f1e81a05 2019-08-10 stsp
4776 f1e81a05 2019-08-10 stsp return NULL;
4777 f1e81a05 2019-08-10 stsp }
4778 f1e81a05 2019-08-10 stsp
4779 f1e81a05 2019-08-10 stsp static const struct got_error *
4780 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4781 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4782 f1e81a05 2019-08-10 stsp {
4783 f1e81a05 2019-08-10 stsp const struct got_error *err;
4784 f1e81a05 2019-08-10 stsp
4785 f1e81a05 2019-08-10 stsp if (outfile) {
4786 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4787 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4788 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4789 f1e81a05 2019-08-10 stsp if (err)
4790 f1e81a05 2019-08-10 stsp return err;
4791 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4792 f1e81a05 2019-08-10 stsp }
4793 f1e81a05 2019-08-10 stsp }
4794 f1e81a05 2019-08-10 stsp if (rejectfile) {
4795 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4796 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4797 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4798 f1e81a05 2019-08-10 stsp if (err)
4799 f1e81a05 2019-08-10 stsp return err;
4800 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4801 f1e81a05 2019-08-10 stsp }
4802 33aa809d 2019-08-08 stsp }
4803 33aa809d 2019-08-08 stsp
4804 33aa809d 2019-08-08 stsp return NULL;
4805 33aa809d 2019-08-08 stsp }
4806 33aa809d 2019-08-08 stsp
4807 33aa809d 2019-08-08 stsp static const struct got_error *
4808 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4809 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4810 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4811 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4812 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4813 33aa809d 2019-08-08 stsp {
4814 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4815 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4816 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4817 33aa809d 2019-08-08 stsp FILE *hunkfile;
4818 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4819 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4820 fe621944 2020-11-10 stsp int rc;
4821 33aa809d 2019-08-08 stsp
4822 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4823 33aa809d 2019-08-08 stsp
4824 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4825 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4826 fe621944 2020-11-10 stsp start_old = cc.left.start;
4827 fe621944 2020-11-10 stsp end_old = cc.left.end;
4828 fe621944 2020-11-10 stsp start_new = cc.right.start;
4829 fe621944 2020-11-10 stsp end_new = cc.right.end;
4830 33aa809d 2019-08-08 stsp
4831 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4832 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4833 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4834 33aa809d 2019-08-08 stsp
4835 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4836 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4837 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4838 33aa809d 2019-08-08 stsp
4839 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4840 fe621944 2020-11-10 stsp if (diff_state == NULL)
4841 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4842 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4843 fe621944 2020-11-10 stsp
4844 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4845 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4846 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4847 33aa809d 2019-08-08 stsp goto done;
4848 33aa809d 2019-08-08 stsp }
4849 fe621944 2020-11-10 stsp
4850 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4851 fe621944 2020-11-10 stsp diff_result, &cc);
4852 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4853 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4854 33aa809d 2019-08-08 stsp goto done;
4855 33aa809d 2019-08-08 stsp }
4856 fe621944 2020-11-10 stsp
4857 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4858 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4859 33aa809d 2019-08-08 stsp goto done;
4860 33aa809d 2019-08-08 stsp }
4861 33aa809d 2019-08-08 stsp
4862 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4863 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4864 33aa809d 2019-08-08 stsp if (err)
4865 33aa809d 2019-08-08 stsp goto done;
4866 33aa809d 2019-08-08 stsp
4867 33aa809d 2019-08-08 stsp switch (*choice) {
4868 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4869 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4870 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4871 33aa809d 2019-08-08 stsp break;
4872 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4873 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4874 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4875 33aa809d 2019-08-08 stsp break;
4876 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4877 33aa809d 2019-08-08 stsp break;
4878 33aa809d 2019-08-08 stsp default:
4879 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4880 33aa809d 2019-08-08 stsp break;
4881 33aa809d 2019-08-08 stsp }
4882 33aa809d 2019-08-08 stsp done:
4883 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4884 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4885 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4886 33aa809d 2019-08-08 stsp return err;
4887 33aa809d 2019-08-08 stsp }
4888 33aa809d 2019-08-08 stsp
4889 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4890 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4891 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4892 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4893 1f1abb7e 2019-08-08 stsp void *progress_arg;
4894 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4895 33aa809d 2019-08-08 stsp void *patch_arg;
4896 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4897 f259c4c1 2021-09-24 stsp int unlink_added_files;
4898 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4899 1f1abb7e 2019-08-08 stsp };
4900 a129376b 2019-03-28 stsp
4901 e20a8b6f 2019-06-04 stsp static const struct got_error *
4902 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4903 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4904 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4905 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4906 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4907 33aa809d 2019-08-08 stsp {
4908 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4909 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4910 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4911 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4912 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4913 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4914 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4915 fe621944 2020-11-10 stsp struct stat sb2;
4916 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4917 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4918 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4919 33aa809d 2019-08-08 stsp
4920 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4921 33aa809d 2019-08-08 stsp
4922 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4923 33aa809d 2019-08-08 stsp if (err)
4924 33aa809d 2019-08-08 stsp return err;
4925 33aa809d 2019-08-08 stsp
4926 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4927 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4928 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4929 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4930 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4931 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4932 fa3cef63 2020-07-23 stsp goto done;
4933 fa3cef63 2020-07-23 stsp }
4934 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4935 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4936 c0df5966 2021-12-31 stsp if (link_len == -1) {
4937 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4938 c0df5966 2021-12-31 stsp path2);
4939 c0df5966 2021-12-31 stsp }
4940 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4941 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4942 12463d8b 2019-12-13 stsp }
4943 12463d8b 2019-12-13 stsp } else {
4944 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4945 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4946 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4947 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4948 fa3cef63 2020-07-23 stsp goto done;
4949 fa3cef63 2020-07-23 stsp }
4950 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4951 fa3cef63 2020-07-23 stsp sizeof(link_target));
4952 fa3cef63 2020-07-23 stsp if (link_len == -1)
4953 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4954 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4955 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4956 12463d8b 2019-12-13 stsp }
4957 1ebedb77 2019-10-19 stsp }
4958 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4959 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4960 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4961 fa3cef63 2020-07-23 stsp goto done;
4962 fa3cef63 2020-07-23 stsp }
4963 1ebedb77 2019-10-19 stsp
4964 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4965 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4966 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4967 fa3cef63 2020-07-23 stsp goto done;
4968 fa3cef63 2020-07-23 stsp }
4969 fa3cef63 2020-07-23 stsp fd2 = -1;
4970 fa3cef63 2020-07-23 stsp } else {
4971 fa3cef63 2020-07-23 stsp size_t n;
4972 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4973 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4974 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4975 fa3cef63 2020-07-23 stsp goto done;
4976 fa3cef63 2020-07-23 stsp }
4977 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4978 fa3cef63 2020-07-23 stsp if (n != link_len) {
4979 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4980 fa3cef63 2020-07-23 stsp goto done;
4981 fa3cef63 2020-07-23 stsp }
4982 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4983 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4984 fa3cef63 2020-07-23 stsp goto done;
4985 fa3cef63 2020-07-23 stsp }
4986 fa3cef63 2020-07-23 stsp rewind(f2);
4987 33aa809d 2019-08-08 stsp }
4988 33aa809d 2019-08-08 stsp
4989 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4990 eb81bc23 2022-06-28 tracey if (fd == -1) {
4991 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4992 eb81bc23 2022-06-28 tracey goto done;
4993 eb81bc23 2022-06-28 tracey }
4994 eb81bc23 2022-06-28 tracey
4995 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4996 33aa809d 2019-08-08 stsp if (err)
4997 33aa809d 2019-08-08 stsp goto done;
4998 33aa809d 2019-08-08 stsp
4999 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5000 33aa809d 2019-08-08 stsp if (err)
5001 33aa809d 2019-08-08 stsp goto done;
5002 33aa809d 2019-08-08 stsp
5003 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5004 33aa809d 2019-08-08 stsp if (err)
5005 33aa809d 2019-08-08 stsp goto done;
5006 33aa809d 2019-08-08 stsp
5007 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5008 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5009 33aa809d 2019-08-08 stsp if (err)
5010 33aa809d 2019-08-08 stsp goto done;
5011 33aa809d 2019-08-08 stsp
5012 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5013 b90054ed 2022-11-01 stsp "");
5014 33aa809d 2019-08-08 stsp if (err)
5015 33aa809d 2019-08-08 stsp goto done;
5016 33aa809d 2019-08-08 stsp
5017 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5018 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5019 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5020 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5021 fe621944 2020-11-10 stsp
5022 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5023 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5024 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
5025 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5026 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5027 fe621944 2020-11-10 stsp nchanges++;
5028 fe621944 2020-11-10 stsp }
5029 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5030 33aa809d 2019-08-08 stsp int choice;
5031 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5032 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5033 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5034 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5035 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5036 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5037 33aa809d 2019-08-08 stsp if (err)
5038 33aa809d 2019-08-08 stsp goto done;
5039 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5040 33aa809d 2019-08-08 stsp have_content = 1;
5041 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5042 33aa809d 2019-08-08 stsp break;
5043 33aa809d 2019-08-08 stsp }
5044 1ebedb77 2019-10-19 stsp if (have_content) {
5045 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5046 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5047 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5048 1ebedb77 2019-10-19 stsp if (err)
5049 1ebedb77 2019-10-19 stsp goto done;
5050 1ebedb77 2019-10-19 stsp
5051 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5052 b2b3fce1 2022-10-29 op mode_t mode;
5053 b2b3fce1 2022-10-29 op
5054 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
5055 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
5056 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5057 fa3cef63 2020-07-23 stsp goto done;
5058 fa3cef63 2020-07-23 stsp }
5059 1ebedb77 2019-10-19 stsp }
5060 1ebedb77 2019-10-19 stsp }
5061 33aa809d 2019-08-08 stsp done:
5062 33aa809d 2019-08-08 stsp free(id_str);
5063 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5064 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5065 33aa809d 2019-08-08 stsp if (blob)
5066 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5067 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5068 fe621944 2020-11-10 stsp if (err == NULL)
5069 fe621944 2020-11-10 stsp err = free_err;
5070 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5071 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5072 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5073 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5074 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5075 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5076 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5077 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5078 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5079 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5080 33aa809d 2019-08-08 stsp if (err || !have_content) {
5081 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5082 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5083 33aa809d 2019-08-08 stsp free(*path_outfile);
5084 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5085 33aa809d 2019-08-08 stsp }
5086 33aa809d 2019-08-08 stsp free(path1);
5087 33aa809d 2019-08-08 stsp return err;
5088 33aa809d 2019-08-08 stsp }
5089 33aa809d 2019-08-08 stsp
5090 33aa809d 2019-08-08 stsp static const struct got_error *
5091 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5092 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5093 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5094 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5095 a129376b 2019-03-28 stsp {
5096 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5097 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5098 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5099 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5100 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
5101 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5102 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5103 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5104 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5105 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5106 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5107 eb81bc23 2022-06-28 tracey int fd = -1;
5108 a129376b 2019-03-28 stsp
5109 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5110 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5111 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5112 d3bcc3d1 2019-08-08 stsp return NULL;
5113 3d69ad8d 2019-08-17 semarie
5114 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5115 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5116 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5117 d3bcc3d1 2019-08-08 stsp
5118 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5119 65084dad 2019-08-08 stsp if (ie == NULL)
5120 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5121 a129376b 2019-03-28 stsp
5122 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5123 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5124 a129376b 2019-03-28 stsp if (err) {
5125 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5126 a129376b 2019-03-28 stsp goto done;
5127 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5128 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5129 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5130 e20a8b6f 2019-06-04 stsp goto done;
5131 e20a8b6f 2019-06-04 stsp }
5132 a129376b 2019-03-28 stsp }
5133 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5134 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5135 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5136 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5137 a129376b 2019-03-28 stsp goto done;
5138 a129376b 2019-03-28 stsp }
5139 a129376b 2019-03-28 stsp } else {
5140 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5141 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5142 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5143 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5144 a129376b 2019-03-28 stsp goto done;
5145 a129376b 2019-03-28 stsp }
5146 a129376b 2019-03-28 stsp } else {
5147 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5148 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5149 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5150 a129376b 2019-03-28 stsp goto done;
5151 a129376b 2019-03-28 stsp }
5152 a129376b 2019-03-28 stsp }
5153 a129376b 2019-03-28 stsp }
5154 a129376b 2019-03-28 stsp
5155 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
5156 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
5157 a44927cc 2022-04-07 stsp if (err)
5158 a44927cc 2022-04-07 stsp goto done;
5159 a44927cc 2022-04-07 stsp
5160 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5161 a9fa2909 2019-07-27 stsp if (err) {
5162 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5163 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5164 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5165 a9fa2909 2019-07-27 stsp goto done;
5166 a9fa2909 2019-07-27 stsp } else {
5167 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5168 a9fa2909 2019-07-27 stsp if (err)
5169 a9fa2909 2019-07-27 stsp goto done;
5170 a9fa2909 2019-07-27 stsp
5171 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5172 1233e6b6 2020-10-19 stsp if (err)
5173 a9fa2909 2019-07-27 stsp goto done;
5174 a9fa2909 2019-07-27 stsp
5175 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5176 1233e6b6 2020-10-19 stsp free(te_name);
5177 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5178 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5179 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5180 a9fa2909 2019-07-27 stsp goto done;
5181 a9fa2909 2019-07-27 stsp }
5182 a129376b 2019-03-28 stsp }
5183 a129376b 2019-03-28 stsp
5184 a129376b 2019-03-28 stsp switch (status) {
5185 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5186 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5187 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5188 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5189 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5190 33aa809d 2019-08-08 stsp if (err)
5191 33aa809d 2019-08-08 stsp goto done;
5192 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5193 33aa809d 2019-08-08 stsp break;
5194 33aa809d 2019-08-08 stsp }
5195 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5196 1f1abb7e 2019-08-08 stsp ie->path);
5197 1ee397ad 2019-07-12 stsp if (err)
5198 1ee397ad 2019-07-12 stsp goto done;
5199 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5200 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5201 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5202 af179be7 2023-04-14 stsp
5203 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5204 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5205 af179be7 2023-04-14 stsp
5206 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5207 af179be7 2023-04-14 stsp entry) {
5208 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5209 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5210 af179be7 2023-04-14 stsp continue;
5211 af179be7 2023-04-14 stsp do_unlink = 1;
5212 af179be7 2023-04-14 stsp break;
5213 af179be7 2023-04-14 stsp }
5214 f259c4c1 2021-09-24 stsp }
5215 af179be7 2023-04-14 stsp
5216 af179be7 2023-04-14 stsp if (do_unlink) {
5217 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5218 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5219 af179be7 2023-04-14 stsp relpath) == -1) {
5220 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5221 af179be7 2023-04-14 stsp goto done;
5222 af179be7 2023-04-14 stsp }
5223 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5224 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5225 af179be7 2023-04-14 stsp ondisk_path);
5226 af179be7 2023-04-14 stsp break;
5227 af179be7 2023-04-14 stsp }
5228 f259c4c1 2021-09-24 stsp }
5229 f259c4c1 2021-09-24 stsp }
5230 a129376b 2019-03-28 stsp break;
5231 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5232 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5233 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5234 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5235 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5236 33aa809d 2019-08-08 stsp if (err)
5237 33aa809d 2019-08-08 stsp goto done;
5238 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5239 33aa809d 2019-08-08 stsp break;
5240 33aa809d 2019-08-08 stsp }
5241 33aa809d 2019-08-08 stsp /* fall through */
5242 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5243 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5244 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5245 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5246 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5247 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5248 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5249 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5250 b4b2adf5 2023-02-09 op else
5251 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5252 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5253 eb81bc23 2022-06-28 tracey if (fd == -1) {
5254 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5255 eb81bc23 2022-06-28 tracey goto done;
5256 eb81bc23 2022-06-28 tracey }
5257 eb81bc23 2022-06-28 tracey
5258 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5259 a129376b 2019-03-28 stsp if (err)
5260 65084dad 2019-08-08 stsp goto done;
5261 65084dad 2019-08-08 stsp
5262 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5263 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5264 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5265 a129376b 2019-03-28 stsp goto done;
5266 65084dad 2019-08-08 stsp }
5267 65084dad 2019-08-08 stsp
5268 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5269 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5270 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5271 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5272 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5273 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5274 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5275 33aa809d 2019-08-08 stsp break;
5276 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5277 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5278 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5279 369fd7e5 2020-07-23 stsp path_content);
5280 369fd7e5 2020-07-23 stsp break;
5281 369fd7e5 2020-07-23 stsp }
5282 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5283 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5284 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5285 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5286 369fd7e5 2020-07-23 stsp } else {
5287 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5288 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5289 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5290 369fd7e5 2020-07-23 stsp goto done;
5291 369fd7e5 2020-07-23 stsp }
5292 33aa809d 2019-08-08 stsp }
5293 33aa809d 2019-08-08 stsp } else {
5294 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5295 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5296 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5297 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5298 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5299 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5300 2e1fa222 2020-07-23 stsp } else {
5301 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5302 2e1fa222 2020-07-23 stsp ie->path,
5303 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5304 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5305 ef623445 2023-09-16 op 0, 1, 0, 0, NULL, a->repo,
5306 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5307 2e1fa222 2020-07-23 stsp }
5308 a129376b 2019-03-28 stsp if (err)
5309 a129376b 2019-03-28 stsp goto done;
5310 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5311 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5312 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5313 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5314 437adc9d 2020-12-10 yzhong blob->id.sha1,
5315 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5316 2e1fa222 2020-07-23 stsp if (err)
5317 2e1fa222 2020-07-23 stsp goto done;
5318 2e1fa222 2020-07-23 stsp }
5319 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5320 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5321 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5322 33aa809d 2019-08-08 stsp }
5323 a129376b 2019-03-28 stsp }
5324 a129376b 2019-03-28 stsp break;
5325 e20a8b6f 2019-06-04 stsp }
5326 a129376b 2019-03-28 stsp default:
5327 1f1abb7e 2019-08-08 stsp break;
5328 a129376b 2019-03-28 stsp }
5329 a129376b 2019-03-28 stsp done:
5330 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5331 33aa809d 2019-08-08 stsp free(path_content);
5332 e20a8b6f 2019-06-04 stsp free(parent_path);
5333 a129376b 2019-03-28 stsp free(tree_path);
5334 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5335 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5336 a129376b 2019-03-28 stsp if (blob)
5337 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5338 a129376b 2019-03-28 stsp if (tree)
5339 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5340 a129376b 2019-03-28 stsp free(tree_id);
5341 a44927cc 2022-04-07 stsp if (base_commit)
5342 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5343 e20a8b6f 2019-06-04 stsp return err;
5344 e20a8b6f 2019-06-04 stsp }
5345 e20a8b6f 2019-06-04 stsp
5346 e20a8b6f 2019-06-04 stsp const struct got_error *
5347 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5348 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5349 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5350 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5351 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5352 e20a8b6f 2019-06-04 stsp {
5353 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5354 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5355 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5356 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5357 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5358 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5359 e20a8b6f 2019-06-04 stsp
5360 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5361 e20a8b6f 2019-06-04 stsp if (err)
5362 e20a8b6f 2019-06-04 stsp return err;
5363 e20a8b6f 2019-06-04 stsp
5364 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5365 e20a8b6f 2019-06-04 stsp if (err)
5366 e20a8b6f 2019-06-04 stsp goto done;
5367 e20a8b6f 2019-06-04 stsp
5368 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5369 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5370 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5371 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5372 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5373 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5374 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5375 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5376 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5377 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5378 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5379 e20a8b6f 2019-06-04 stsp if (err)
5380 af12c6b9 2019-06-04 stsp break;
5381 e20a8b6f 2019-06-04 stsp }
5382 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5383 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5384 e20a8b6f 2019-06-04 stsp err = sync_err;
5385 af12c6b9 2019-06-04 stsp done:
5386 fb399478 2019-07-12 stsp free(fileindex_path);
5387 a129376b 2019-03-28 stsp if (fileindex)
5388 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5389 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5390 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5391 a129376b 2019-03-28 stsp err = unlockerr;
5392 c4296144 2019-05-09 stsp return err;
5393 c4296144 2019-05-09 stsp }
5394 c4296144 2019-05-09 stsp
5395 cf066bf8 2019-05-09 stsp static void
5396 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5397 cf066bf8 2019-05-09 stsp {
5398 24519714 2019-05-09 stsp free(ct->path);
5399 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5400 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5401 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5402 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5403 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5404 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5405 cf066bf8 2019-05-09 stsp free(ct);
5406 cf066bf8 2019-05-09 stsp }
5407 24519714 2019-05-09 stsp
5408 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5409 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5410 24519714 2019-05-09 stsp struct got_repository *repo;
5411 24519714 2019-05-09 stsp struct got_worktree *worktree;
5412 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5413 5f8a88c6 2019-08-03 stsp int have_staged_files;
5414 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5415 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5416 12383673 2023-02-18 mark int commit_conflicts;
5417 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5418 2a47b1e5 2022-11-01 stsp FILE *f1;
5419 2a47b1e5 2022-11-01 stsp FILE *f2;
5420 24519714 2019-05-09 stsp };
5421 2a47b1e5 2022-11-01 stsp
5422 2a47b1e5 2022-11-01 stsp /*
5423 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5424 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5425 2a47b1e5 2022-11-01 stsp */
5426 2a47b1e5 2022-11-01 stsp static const struct got_error *
5427 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5428 2a47b1e5 2022-11-01 stsp const char *abspath)
5429 2a47b1e5 2022-11-01 stsp {
5430 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5431 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5432 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5433 2a47b1e5 2022-11-01 stsp
5434 2a47b1e5 2022-11-01 stsp *fd = -1;
5435 2a47b1e5 2022-11-01 stsp
5436 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5437 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5438 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5439 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5440 2a47b1e5 2022-11-01 stsp } else {
5441 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5442 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5443 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5444 2a47b1e5 2022-11-01 stsp }
5445 2a47b1e5 2022-11-01 stsp
5446 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5447 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5448 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5449 2a47b1e5 2022-11-01 stsp
5450 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5451 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5452 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5453 2a47b1e5 2022-11-01 stsp goto done;
5454 2a47b1e5 2022-11-01 stsp }
5455 2a47b1e5 2022-11-01 stsp
5456 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5457 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5458 2a47b1e5 2022-11-01 stsp goto done;
5459 2a47b1e5 2022-11-01 stsp }
5460 2a47b1e5 2022-11-01 stsp done:
5461 2a47b1e5 2022-11-01 stsp if (err) {
5462 2a47b1e5 2022-11-01 stsp close(*fd);
5463 2a47b1e5 2022-11-01 stsp *fd = -1;
5464 2a47b1e5 2022-11-01 stsp }
5465 2a47b1e5 2022-11-01 stsp return err;
5466 2a47b1e5 2022-11-01 stsp }
5467 2a47b1e5 2022-11-01 stsp
5468 2a47b1e5 2022-11-01 stsp static const struct got_error *
5469 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5470 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5471 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5472 2a47b1e5 2022-11-01 stsp {
5473 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5474 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5475 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5476 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5477 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5478 2a47b1e5 2022-11-01 stsp struct stat sb;
5479 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5480 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5481 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5482 2a47b1e5 2022-11-01 stsp
5483 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5484 4ba5cca9 2022-11-01 stsp
5485 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5486 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5487 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5488 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5489 4ba5cca9 2022-11-01 stsp return NULL;
5490 4ba5cca9 2022-11-01 stsp } else {
5491 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5492 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5493 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5494 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5495 4ba5cca9 2022-11-01 stsp return NULL;
5496 4ba5cca9 2022-11-01 stsp }
5497 2a47b1e5 2022-11-01 stsp
5498 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5499 2a47b1e5 2022-11-01 stsp if (err)
5500 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5501 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5502 2a47b1e5 2022-11-01 stsp if (err)
5503 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5504 2a47b1e5 2022-11-01 stsp
5505 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5506 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5507 2a47b1e5 2022-11-01 stsp if (err)
5508 2a47b1e5 2022-11-01 stsp return err;
5509 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5510 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5511 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5512 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5513 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5514 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5515 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5516 2a47b1e5 2022-11-01 stsp }
5517 2a47b1e5 2022-11-01 stsp
5518 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5519 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5520 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5521 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5522 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5523 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5524 2a47b1e5 2022-11-01 stsp break;
5525 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5526 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5527 2a47b1e5 2022-11-01 stsp break;
5528 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5529 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5530 2a47b1e5 2022-11-01 stsp break;
5531 2a47b1e5 2022-11-01 stsp default:
5532 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5533 2a47b1e5 2022-11-01 stsp }
5534 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5535 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5536 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5537 2a47b1e5 2022-11-01 stsp goto done;
5538 2a47b1e5 2022-11-01 stsp }
5539 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5540 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5541 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5542 2a47b1e5 2022-11-01 stsp goto done;
5543 2a47b1e5 2022-11-01 stsp }
5544 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5545 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5546 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5547 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5548 2a47b1e5 2022-11-01 stsp goto done;
5549 2a47b1e5 2022-11-01 stsp }
5550 2a47b1e5 2022-11-01 stsp
5551 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5552 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5553 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5554 2a47b1e5 2022-11-01 stsp goto done;
5555 2a47b1e5 2022-11-01 stsp }
5556 2a47b1e5 2022-11-01 stsp
5557 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5558 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5559 2a47b1e5 2022-11-01 stsp 8192, fd1);
5560 2a47b1e5 2022-11-01 stsp if (err)
5561 2a47b1e5 2022-11-01 stsp goto done;
5562 2a47b1e5 2022-11-01 stsp }
5563 2a47b1e5 2022-11-01 stsp
5564 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5565 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5566 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5567 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5568 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5569 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5570 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5571 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5572 2a47b1e5 2022-11-01 stsp goto done;
5573 2a47b1e5 2022-11-01 stsp }
5574 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5575 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5576 2a47b1e5 2022-11-01 stsp if (err)
5577 2a47b1e5 2022-11-01 stsp goto done;
5578 2a47b1e5 2022-11-01 stsp }
5579 2a47b1e5 2022-11-01 stsp } else {
5580 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5581 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5582 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5583 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5584 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5585 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5586 2a47b1e5 2022-11-01 stsp goto done;
5587 2a47b1e5 2022-11-01 stsp }
5588 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5589 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5590 2a47b1e5 2022-11-01 stsp if (err)
5591 2a47b1e5 2022-11-01 stsp goto done;
5592 2a47b1e5 2022-11-01 stsp }
5593 2a47b1e5 2022-11-01 stsp }
5594 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5595 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5596 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5597 2a47b1e5 2022-11-01 stsp goto done;
5598 2a47b1e5 2022-11-01 stsp }
5599 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5600 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5601 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5602 2a47b1e5 2022-11-01 stsp goto done;
5603 2a47b1e5 2022-11-01 stsp }
5604 2a47b1e5 2022-11-01 stsp fd = -1;
5605 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5606 2a47b1e5 2022-11-01 stsp }
5607 2a47b1e5 2022-11-01 stsp
5608 2a47b1e5 2022-11-01 stsp if (blob1) {
5609 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5610 2a47b1e5 2022-11-01 stsp f1, blob1);
5611 2a47b1e5 2022-11-01 stsp if (err)
5612 2a47b1e5 2022-11-01 stsp goto done;
5613 2a47b1e5 2022-11-01 stsp }
5614 2a47b1e5 2022-11-01 stsp
5615 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5616 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5617 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5618 2a47b1e5 2022-11-01 stsp done:
5619 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5620 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5621 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5622 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5623 2a47b1e5 2022-11-01 stsp if (blob1)
5624 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5625 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5626 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5627 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5628 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5629 2a47b1e5 2022-11-01 stsp return err;
5630 2a47b1e5 2022-11-01 stsp }
5631 cf066bf8 2019-05-09 stsp
5632 c4296144 2019-05-09 stsp static const struct got_error *
5633 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5634 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5635 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5636 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5637 c4296144 2019-05-09 stsp {
5638 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5639 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5640 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5641 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5642 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5643 768aea60 2019-05-09 stsp struct stat sb;
5644 c4296144 2019-05-09 stsp
5645 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5646 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5647 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5648 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5649 5f8a88c6 2019-08-03 stsp return NULL;
5650 5f8a88c6 2019-08-03 stsp } else {
5651 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5652 12383673 2023-02-18 mark printf("C %s\n", relpath);
5653 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5654 12383673 2023-02-18 mark }
5655 c4296144 2019-05-09 stsp
5656 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5657 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5658 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5659 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5660 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5661 5f8a88c6 2019-08-03 stsp return NULL;
5662 5f8a88c6 2019-08-03 stsp }
5663 0b5cc0d6 2019-05-09 stsp
5664 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5665 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5666 036813ee 2019-05-09 stsp goto done;
5667 036813ee 2019-05-09 stsp }
5668 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5669 036813ee 2019-05-09 stsp parent_path = strdup("");
5670 69960a46 2019-05-09 stsp if (parent_path == NULL)
5671 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5672 69960a46 2019-05-09 stsp } else {
5673 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5674 69960a46 2019-05-09 stsp if (err)
5675 69960a46 2019-05-09 stsp return err;
5676 69960a46 2019-05-09 stsp }
5677 c4296144 2019-05-09 stsp
5678 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5679 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5680 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5681 c4296144 2019-05-09 stsp goto done;
5682 768aea60 2019-05-09 stsp }
5683 768aea60 2019-05-09 stsp
5684 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5685 768aea60 2019-05-09 stsp relpath) == -1) {
5686 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5687 768aea60 2019-05-09 stsp goto done;
5688 768aea60 2019-05-09 stsp }
5689 0aeb8099 2020-07-23 stsp
5690 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5691 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5692 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5693 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5694 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5695 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5696 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5697 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5698 0aeb8099 2020-07-23 stsp break;
5699 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5700 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5701 0aeb8099 2020-07-23 stsp break;
5702 0aeb8099 2020-07-23 stsp default:
5703 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5704 0aeb8099 2020-07-23 stsp goto done;
5705 0aeb8099 2020-07-23 stsp }
5706 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5707 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5708 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5709 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5710 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5711 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5712 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5713 12463d8b 2019-12-13 stsp ct->ondisk_path);
5714 12463d8b 2019-12-13 stsp goto done;
5715 12463d8b 2019-12-13 stsp }
5716 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5717 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5718 768aea60 2019-05-09 stsp goto done;
5719 768aea60 2019-05-09 stsp }
5720 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5721 c4296144 2019-05-09 stsp }
5722 c4296144 2019-05-09 stsp
5723 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5724 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5725 44d03001 2019-05-09 stsp relpath) == -1) {
5726 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5727 44d03001 2019-05-09 stsp goto done;
5728 44d03001 2019-05-09 stsp }
5729 44d03001 2019-05-09 stsp
5730 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5731 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5732 35213c7c 2020-07-23 stsp int is_bad_symlink;
5733 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5734 35213c7c 2020-07-23 stsp ssize_t target_len;
5735 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5736 35213c7c 2020-07-23 stsp sizeof(target_path));
5737 35213c7c 2020-07-23 stsp if (target_len == -1) {
5738 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5739 35213c7c 2020-07-23 stsp ct->ondisk_path);
5740 35213c7c 2020-07-23 stsp goto done;
5741 35213c7c 2020-07-23 stsp }
5742 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5743 df6221c7 2023-07-19 stsp target_len, ct->ondisk_path, a->worktree->root_path,
5744 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
5745 35213c7c 2020-07-23 stsp if (err)
5746 35213c7c 2020-07-23 stsp goto done;
5747 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5748 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5749 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5750 35213c7c 2020-07-23 stsp goto done;
5751 35213c7c 2020-07-23 stsp }
5752 35213c7c 2020-07-23 stsp }
5753 35213c7c 2020-07-23 stsp
5754 35213c7c 2020-07-23 stsp
5755 cf066bf8 2019-05-09 stsp ct->status = status;
5756 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5757 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5758 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5759 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5760 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5761 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5762 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5763 b416585c 2019-05-13 stsp goto done;
5764 b416585c 2019-05-13 stsp }
5765 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5766 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5767 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5768 f0b75401 2019-08-03 stsp goto done;
5769 f0b75401 2019-08-03 stsp }
5770 f0b75401 2019-08-03 stsp }
5771 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5772 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5773 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5774 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5775 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5776 036813ee 2019-05-09 stsp goto done;
5777 036813ee 2019-05-09 stsp }
5778 ca2503ea 2019-05-09 stsp }
5779 24519714 2019-05-09 stsp ct->path = strdup(path);
5780 24519714 2019-05-09 stsp if (ct->path == NULL) {
5781 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5782 24519714 2019-05-09 stsp goto done;
5783 24519714 2019-05-09 stsp }
5784 2a47b1e5 2022-11-01 stsp
5785 c78dbc03 2023-08-25 stsp if (a->diff_outfile) {
5786 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5787 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5788 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5789 2a47b1e5 2022-11-01 stsp if (err)
5790 2a47b1e5 2022-11-01 stsp goto done;
5791 2a47b1e5 2022-11-01 stsp }
5792 c78dbc03 2023-08-25 stsp
5793 c78dbc03 2023-08-25 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5794 c4296144 2019-05-09 stsp done:
5795 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5796 ed175427 2019-05-09 stsp free_commitable(ct);
5797 24519714 2019-05-09 stsp free(parent_path);
5798 036813ee 2019-05-09 stsp free(path);
5799 a129376b 2019-03-28 stsp return err;
5800 a129376b 2019-03-28 stsp }
5801 c4296144 2019-05-09 stsp
5802 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5803 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5804 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5805 036813ee 2019-05-09 stsp struct got_repository *);
5806 ed175427 2019-05-09 stsp
5807 ed175427 2019-05-09 stsp static const struct got_error *
5808 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5809 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5810 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5811 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5812 afa376bf 2019-05-09 stsp struct got_repository *repo)
5813 ed175427 2019-05-09 stsp {
5814 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5815 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5816 036813ee 2019-05-09 stsp char *subpath;
5817 ed175427 2019-05-09 stsp
5818 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5819 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5820 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5821 ed175427 2019-05-09 stsp
5822 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5823 ed175427 2019-05-09 stsp if (err)
5824 ed175427 2019-05-09 stsp return err;
5825 ed175427 2019-05-09 stsp
5826 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5827 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5828 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5829 036813ee 2019-05-09 stsp free(subpath);
5830 036813ee 2019-05-09 stsp return err;
5831 036813ee 2019-05-09 stsp }
5832 ed175427 2019-05-09 stsp
5833 036813ee 2019-05-09 stsp static const struct got_error *
5834 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5835 036813ee 2019-05-09 stsp {
5836 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5837 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5838 ed175427 2019-05-09 stsp
5839 036813ee 2019-05-09 stsp *match = 0;
5840 ed175427 2019-05-09 stsp
5841 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5842 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5843 0f63689d 2019-05-10 stsp return NULL;
5844 036813ee 2019-05-09 stsp }
5845 0b5cc0d6 2019-05-09 stsp
5846 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5847 0f63689d 2019-05-10 stsp if (err)
5848 0f63689d 2019-05-10 stsp return err;
5849 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5850 036813ee 2019-05-09 stsp free(ct_parent_path);
5851 036813ee 2019-05-09 stsp return err;
5852 036813ee 2019-05-09 stsp }
5853 0b5cc0d6 2019-05-09 stsp
5854 768aea60 2019-05-09 stsp static mode_t
5855 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5856 768aea60 2019-05-09 stsp {
5857 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5858 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5859 3d9a4ec4 2020-07-23 stsp
5860 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5861 768aea60 2019-05-09 stsp }
5862 768aea60 2019-05-09 stsp
5863 036813ee 2019-05-09 stsp static const struct got_error *
5864 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5865 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5866 036813ee 2019-05-09 stsp {
5867 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5868 ca2503ea 2019-05-09 stsp
5869 036813ee 2019-05-09 stsp *new_te = NULL;
5870 0b5cc0d6 2019-05-09 stsp
5871 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5872 036813ee 2019-05-09 stsp if (err)
5873 036813ee 2019-05-09 stsp goto done;
5874 0b5cc0d6 2019-05-09 stsp
5875 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5876 036813ee 2019-05-09 stsp
5877 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5878 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5879 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5880 5f8a88c6 2019-08-03 stsp else
5881 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5882 036813ee 2019-05-09 stsp done:
5883 036813ee 2019-05-09 stsp if (err && *new_te) {
5884 56e0773d 2019-11-28 stsp free(*new_te);
5885 036813ee 2019-05-09 stsp *new_te = NULL;
5886 036813ee 2019-05-09 stsp }
5887 036813ee 2019-05-09 stsp return err;
5888 036813ee 2019-05-09 stsp }
5889 036813ee 2019-05-09 stsp
5890 036813ee 2019-05-09 stsp static const struct got_error *
5891 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5892 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5893 036813ee 2019-05-09 stsp {
5894 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5895 102b254e 2020-10-19 stsp char *ct_name = NULL;
5896 036813ee 2019-05-09 stsp
5897 06bd8ee4 2024-02-13 op *new_te = NULL;
5898 036813ee 2019-05-09 stsp
5899 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5900 036813ee 2019-05-09 stsp if (*new_te == NULL)
5901 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5902 036813ee 2019-05-09 stsp
5903 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5904 102b254e 2020-10-19 stsp if (err)
5905 036813ee 2019-05-09 stsp goto done;
5906 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5907 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5908 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5909 036813ee 2019-05-09 stsp goto done;
5910 036813ee 2019-05-09 stsp }
5911 036813ee 2019-05-09 stsp
5912 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5913 036813ee 2019-05-09 stsp
5914 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5915 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5916 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5917 5f8a88c6 2019-08-03 stsp else
5918 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5919 036813ee 2019-05-09 stsp done:
5920 102b254e 2020-10-19 stsp free(ct_name);
5921 036813ee 2019-05-09 stsp if (err && *new_te) {
5922 56e0773d 2019-11-28 stsp free(*new_te);
5923 036813ee 2019-05-09 stsp *new_te = NULL;
5924 036813ee 2019-05-09 stsp }
5925 036813ee 2019-05-09 stsp return err;
5926 036813ee 2019-05-09 stsp }
5927 036813ee 2019-05-09 stsp
5928 036813ee 2019-05-09 stsp static const struct got_error *
5929 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5930 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5931 036813ee 2019-05-09 stsp {
5932 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5933 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5934 036813ee 2019-05-09 stsp
5935 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5936 036813ee 2019-05-09 stsp if (err)
5937 036813ee 2019-05-09 stsp return err;
5938 036813ee 2019-05-09 stsp if (new_pe == NULL)
5939 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5940 036813ee 2019-05-09 stsp return NULL;
5941 afa376bf 2019-05-09 stsp }
5942 afa376bf 2019-05-09 stsp
5943 afa376bf 2019-05-09 stsp static const struct got_error *
5944 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5945 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5946 afa376bf 2019-05-09 stsp {
5947 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5948 5f8a88c6 2019-08-03 stsp unsigned char status;
5949 0ff8d236 2021-09-28 stsp
5950 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5951 0ff8d236 2021-09-28 stsp return NULL;
5952 5f8a88c6 2019-08-03 stsp
5953 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5954 afa376bf 2019-05-09 stsp ct_path++;
5955 5f8a88c6 2019-08-03 stsp
5956 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5957 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5958 5f8a88c6 2019-08-03 stsp else
5959 5f8a88c6 2019-08-03 stsp status = ct->status;
5960 5f8a88c6 2019-08-03 stsp
5961 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5962 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5963 036813ee 2019-05-09 stsp }
5964 036813ee 2019-05-09 stsp
5965 036813ee 2019-05-09 stsp static const struct got_error *
5966 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5967 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5968 44d03001 2019-05-09 stsp {
5969 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5970 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5971 44d03001 2019-05-09 stsp char *te_path;
5972 44d03001 2019-05-09 stsp
5973 44d03001 2019-05-09 stsp *modified = 0;
5974 44d03001 2019-05-09 stsp
5975 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5976 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5977 44d03001 2019-05-09 stsp te->name) == -1)
5978 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5979 44d03001 2019-05-09 stsp
5980 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5981 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5982 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5983 44d03001 2019-05-09 stsp strlen(te_path));
5984 62d463ca 2020-10-20 naddy if (*modified)
5985 44d03001 2019-05-09 stsp break;
5986 44d03001 2019-05-09 stsp }
5987 44d03001 2019-05-09 stsp
5988 44d03001 2019-05-09 stsp free(te_path);
5989 44d03001 2019-05-09 stsp return err;
5990 44d03001 2019-05-09 stsp }
5991 44d03001 2019-05-09 stsp
5992 44d03001 2019-05-09 stsp static const struct got_error *
5993 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5994 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5995 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5996 036813ee 2019-05-09 stsp {
5997 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5998 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5999 036813ee 2019-05-09 stsp
6000 036813ee 2019-05-09 stsp *ctp = NULL;
6001 036813ee 2019-05-09 stsp
6002 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6003 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6004 036813ee 2019-05-09 stsp char *ct_name = NULL;
6005 036813ee 2019-05-09 stsp int path_matches;
6006 036813ee 2019-05-09 stsp
6007 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6008 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6009 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6010 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
6011 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6012 5f8a88c6 2019-08-03 stsp continue;
6013 5f8a88c6 2019-08-03 stsp } else {
6014 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6015 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6016 5f8a88c6 2019-08-03 stsp continue;
6017 5f8a88c6 2019-08-03 stsp }
6018 036813ee 2019-05-09 stsp
6019 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6020 036813ee 2019-05-09 stsp continue;
6021 036813ee 2019-05-09 stsp
6022 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6023 62d463ca 2020-10-20 naddy if (err)
6024 036813ee 2019-05-09 stsp return err;
6025 036813ee 2019-05-09 stsp if (!path_matches)
6026 036813ee 2019-05-09 stsp continue;
6027 036813ee 2019-05-09 stsp
6028 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6029 d34b633e 2020-10-19 stsp if (err)
6030 d34b633e 2020-10-19 stsp return err;
6031 d34b633e 2020-10-19 stsp
6032 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6033 d34b633e 2020-10-19 stsp free(ct_name);
6034 036813ee 2019-05-09 stsp continue;
6035 d34b633e 2020-10-19 stsp }
6036 d34b633e 2020-10-19 stsp free(ct_name);
6037 036813ee 2019-05-09 stsp
6038 036813ee 2019-05-09 stsp *ctp = ct;
6039 036813ee 2019-05-09 stsp break;
6040 036813ee 2019-05-09 stsp }
6041 2b496619 2019-07-10 stsp
6042 2b496619 2019-07-10 stsp return err;
6043 2b496619 2019-07-10 stsp }
6044 2b496619 2019-07-10 stsp
6045 2b496619 2019-07-10 stsp static const struct got_error *
6046 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6047 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6048 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6049 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6050 2b496619 2019-07-10 stsp struct got_repository *repo)
6051 2b496619 2019-07-10 stsp {
6052 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6053 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6054 2b496619 2019-07-10 stsp char *subtree_path;
6055 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6056 ba580f68 2020-03-22 stsp int nentries;
6057 2b496619 2019-07-10 stsp
6058 2b496619 2019-07-10 stsp *new_tep = NULL;
6059 2b496619 2019-07-10 stsp
6060 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6061 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6062 2b496619 2019-07-10 stsp child_path) == -1)
6063 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6064 036813ee 2019-05-09 stsp
6065 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6066 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6067 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6068 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6069 56e0773d 2019-11-28 stsp
6070 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6071 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6072 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6073 2b496619 2019-07-10 stsp goto done;
6074 2b496619 2019-07-10 stsp }
6075 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6076 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6077 2b496619 2019-07-10 stsp if (err) {
6078 56e0773d 2019-11-28 stsp free(new_te);
6079 2b496619 2019-07-10 stsp goto done;
6080 2b496619 2019-07-10 stsp }
6081 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6082 2b496619 2019-07-10 stsp done:
6083 56e0773d 2019-11-28 stsp free(id);
6084 2b496619 2019-07-10 stsp free(subtree_path);
6085 2b496619 2019-07-10 stsp if (err == NULL)
6086 2b496619 2019-07-10 stsp *new_tep = new_te;
6087 036813ee 2019-05-09 stsp return err;
6088 036813ee 2019-05-09 stsp }
6089 036813ee 2019-05-09 stsp
6090 036813ee 2019-05-09 stsp static const struct got_error *
6091 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6092 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6093 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6094 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6095 036813ee 2019-05-09 stsp struct got_repository *repo)
6096 036813ee 2019-05-09 stsp {
6097 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6098 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6099 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6100 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6101 036813ee 2019-05-09 stsp
6102 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6103 ba580f68 2020-03-22 stsp *nentries = 0;
6104 036813ee 2019-05-09 stsp
6105 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6106 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6107 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6108 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6109 036813ee 2019-05-09 stsp
6110 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6111 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6112 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6113 036813ee 2019-05-09 stsp continue;
6114 036813ee 2019-05-09 stsp
6115 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6116 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6117 036813ee 2019-05-09 stsp continue;
6118 036813ee 2019-05-09 stsp
6119 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6120 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6121 036813ee 2019-05-09 stsp if (err)
6122 036813ee 2019-05-09 stsp goto done;
6123 036813ee 2019-05-09 stsp
6124 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6125 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6126 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6127 036813ee 2019-05-09 stsp if (err)
6128 036813ee 2019-05-09 stsp goto done;
6129 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6130 afa376bf 2019-05-09 stsp if (err)
6131 afa376bf 2019-05-09 stsp goto done;
6132 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6133 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6134 2b496619 2019-07-10 stsp if (err)
6135 2f51b5b3 2019-05-09 stsp goto done;
6136 ba580f68 2020-03-22 stsp (*nentries)++;
6137 2b496619 2019-07-10 stsp } else {
6138 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6139 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6140 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6141 2b496619 2019-07-10 stsp == NULL) {
6142 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6143 2b496619 2019-07-10 stsp child_path, path_base_tree,
6144 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6145 2b496619 2019-07-10 stsp repo);
6146 2b496619 2019-07-10 stsp if (err)
6147 2b496619 2019-07-10 stsp goto done;
6148 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6149 2b496619 2019-07-10 stsp if (err)
6150 2b496619 2019-07-10 stsp goto done;
6151 ba580f68 2020-03-22 stsp (*nentries)++;
6152 9ba0479c 2019-05-10 stsp }
6153 ed175427 2019-05-09 stsp }
6154 2f51b5b3 2019-05-09 stsp }
6155 2f51b5b3 2019-05-09 stsp
6156 2f51b5b3 2019-05-09 stsp if (base_tree) {
6157 56e0773d 2019-11-28 stsp int i, nbase_entries;
6158 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6159 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6160 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6161 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6162 63c5ca5d 2019-08-24 stsp
6163 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6164 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6165 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6166 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6167 63c5ca5d 2019-08-24 stsp if (err)
6168 63c5ca5d 2019-08-24 stsp goto done;
6169 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6170 63c5ca5d 2019-08-24 stsp if (err)
6171 63c5ca5d 2019-08-24 stsp goto done;
6172 ba580f68 2020-03-22 stsp (*nentries)++;
6173 63c5ca5d 2019-08-24 stsp continue;
6174 63c5ca5d 2019-08-24 stsp }
6175 2f51b5b3 2019-05-09 stsp
6176 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6177 44d03001 2019-05-09 stsp int modified;
6178 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6179 036813ee 2019-05-09 stsp if (err)
6180 036813ee 2019-05-09 stsp goto done;
6181 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6182 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6183 2f51b5b3 2019-05-09 stsp if (err)
6184 2f51b5b3 2019-05-09 stsp goto done;
6185 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6186 44d03001 2019-05-09 stsp if (modified) {
6187 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6188 ba580f68 2020-03-22 stsp int nsubentries;
6189 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6190 ba580f68 2020-03-22 stsp &nsubentries, te,
6191 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6192 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6193 44d03001 2019-05-09 stsp if (err)
6194 44d03001 2019-05-09 stsp goto done;
6195 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6196 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6197 ba580f68 2020-03-22 stsp free(new_id);
6198 ba580f68 2020-03-22 stsp continue;
6199 ba580f68 2020-03-22 stsp }
6200 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6201 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6202 56e0773d 2019-11-28 stsp free(new_id);
6203 44d03001 2019-05-09 stsp }
6204 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6205 036813ee 2019-05-09 stsp if (err)
6206 036813ee 2019-05-09 stsp goto done;
6207 ba580f68 2020-03-22 stsp (*nentries)++;
6208 2f51b5b3 2019-05-09 stsp continue;
6209 036813ee 2019-05-09 stsp }
6210 2f51b5b3 2019-05-09 stsp
6211 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6212 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6213 f66c734c 2019-09-22 stsp if (err)
6214 f66c734c 2019-09-22 stsp goto done;
6215 2f51b5b3 2019-05-09 stsp if (ct) {
6216 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6217 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6218 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6219 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6220 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6221 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6222 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6223 2f51b5b3 2019-05-09 stsp if (err)
6224 2f51b5b3 2019-05-09 stsp goto done;
6225 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6226 2f51b5b3 2019-05-09 stsp if (err)
6227 2f51b5b3 2019-05-09 stsp goto done;
6228 ba580f68 2020-03-22 stsp (*nentries)++;
6229 2f51b5b3 2019-05-09 stsp }
6230 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6231 b416585c 2019-05-13 stsp status_arg);
6232 afa376bf 2019-05-09 stsp if (err)
6233 afa376bf 2019-05-09 stsp goto done;
6234 2f51b5b3 2019-05-09 stsp } else {
6235 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6236 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6237 2f51b5b3 2019-05-09 stsp if (err)
6238 2f51b5b3 2019-05-09 stsp goto done;
6239 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6240 2f51b5b3 2019-05-09 stsp if (err)
6241 2f51b5b3 2019-05-09 stsp goto done;
6242 ba580f68 2020-03-22 stsp (*nentries)++;
6243 2f51b5b3 2019-05-09 stsp }
6244 ca2503ea 2019-05-09 stsp }
6245 ed175427 2019-05-09 stsp }
6246 0b5cc0d6 2019-05-09 stsp
6247 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6248 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6249 ed175427 2019-05-09 stsp done:
6250 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6251 2e1fa222 2020-07-23 stsp return err;
6252 2e1fa222 2020-07-23 stsp }
6253 2e1fa222 2020-07-23 stsp
6254 2e1fa222 2020-07-23 stsp static const struct got_error *
6255 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6256 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6257 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6258 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6259 ebf99748 2019-05-09 stsp {
6260 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6261 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6262 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6263 ebf99748 2019-05-09 stsp
6264 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6265 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6266 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6267 ebf99748 2019-05-09 stsp
6268 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6269 437adc9d 2020-12-10 yzhong
6270 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6271 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6272 437adc9d 2020-12-10 yzhong if (err)
6273 437adc9d 2020-12-10 yzhong goto done;
6274 437adc9d 2020-12-10 yzhong
6275 ebf99748 2019-05-09 stsp if (ie) {
6276 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6277 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6278 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6279 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6280 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6281 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6282 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6283 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6284 437adc9d 2020-12-10 yzhong
6285 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6286 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6287 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6288 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6289 72fd46fa 2019-09-06 stsp !have_staged_files);
6290 ebf99748 2019-05-09 stsp } else
6291 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6292 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6293 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6294 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6295 72fd46fa 2019-09-06 stsp !have_staged_files);
6296 ebf99748 2019-05-09 stsp } else {
6297 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6298 ebf99748 2019-05-09 stsp if (err)
6299 437adc9d 2020-12-10 yzhong goto done;
6300 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6301 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6302 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6303 3969253a 2020-03-07 stsp if (err) {
6304 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6305 437adc9d 2020-12-10 yzhong goto done;
6306 3969253a 2020-03-07 stsp }
6307 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6308 3969253a 2020-03-07 stsp if (err) {
6309 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6310 437adc9d 2020-12-10 yzhong goto done;
6311 3969253a 2020-03-07 stsp }
6312 ebf99748 2019-05-09 stsp }
6313 437adc9d 2020-12-10 yzhong free(relpath);
6314 437adc9d 2020-12-10 yzhong relpath = NULL;
6315 ebf99748 2019-05-09 stsp }
6316 437adc9d 2020-12-10 yzhong done:
6317 437adc9d 2020-12-10 yzhong free(relpath);
6318 d56d26ce 2019-05-10 stsp return err;
6319 d56d26ce 2019-05-10 stsp }
6320 735ef5ac 2019-08-03 stsp
6321 d56d26ce 2019-05-10 stsp
6322 d56d26ce 2019-05-10 stsp static const struct got_error *
6323 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6324 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6325 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6326 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6327 735ef5ac 2019-08-03 stsp int ood_errcode)
6328 d56d26ce 2019-05-10 stsp {
6329 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6330 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6331 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6332 1a36436d 2019-06-10 stsp
6333 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6334 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6335 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6336 1a36436d 2019-06-10 stsp return NULL;
6337 9bead371 2019-07-28 stsp /*
6338 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6339 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6340 9bead371 2019-07-28 stsp */
6341 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6342 a44927cc 2022-04-07 stsp if (err)
6343 a44927cc 2022-04-07 stsp goto done;
6344 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6345 9bead371 2019-07-28 stsp if (err) {
6346 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6347 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6348 1a36436d 2019-06-10 stsp goto done;
6349 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6350 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6351 1a36436d 2019-06-10 stsp } else {
6352 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6353 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6354 a44927cc 2022-04-07 stsp if (err)
6355 a44927cc 2022-04-07 stsp goto done;
6356 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6357 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6358 1a36436d 2019-06-10 stsp goto done;
6359 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6360 1a36436d 2019-06-10 stsp }
6361 1a36436d 2019-06-10 stsp done:
6362 1a36436d 2019-06-10 stsp free(id);
6363 a44927cc 2022-04-07 stsp if (commit)
6364 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6365 1a36436d 2019-06-10 stsp return err;
6366 ebf99748 2019-05-09 stsp }
6367 ebf99748 2019-05-09 stsp
6368 336075a4 2022-06-25 op static const struct got_error *
6369 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6370 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6371 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6372 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6373 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6374 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6375 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6376 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6377 afa376bf 2019-05-09 stsp struct got_repository *repo)
6378 c4296144 2019-05-09 stsp {
6379 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6380 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6381 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6382 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6383 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6384 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6385 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6386 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6387 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6388 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6389 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6390 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6391 f8399b8f 2022-07-22 stsp time_t timestamp;
6392 c4296144 2019-05-09 stsp
6393 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6394 c4296144 2019-05-09 stsp
6395 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6396 675c7539 2019-05-09 stsp
6397 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6398 588edf97 2019-05-10 stsp if (err)
6399 588edf97 2019-05-10 stsp goto done;
6400 de18fc63 2019-05-09 stsp
6401 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6402 588edf97 2019-05-10 stsp if (err)
6403 588edf97 2019-05-10 stsp goto done;
6404 588edf97 2019-05-10 stsp
6405 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6406 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6407 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6408 33ad4cbe 2019-05-12 jcs if (err)
6409 33ad4cbe 2019-05-12 jcs goto done;
6410 33ad4cbe 2019-05-12 jcs }
6411 c4296144 2019-05-09 stsp
6412 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6413 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6414 33ad4cbe 2019-05-12 jcs goto done;
6415 33ad4cbe 2019-05-12 jcs }
6416 33ad4cbe 2019-05-12 jcs
6417 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6418 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6419 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6420 cf066bf8 2019-05-09 stsp char *ondisk_path;
6421 cf066bf8 2019-05-09 stsp
6422 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6423 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6424 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6425 5f8a88c6 2019-08-03 stsp continue;
6426 5f8a88c6 2019-08-03 stsp
6427 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6428 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6429 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6430 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6431 cf066bf8 2019-05-09 stsp continue;
6432 cf066bf8 2019-05-09 stsp
6433 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6434 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6435 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6436 cf066bf8 2019-05-09 stsp goto done;
6437 cf066bf8 2019-05-09 stsp }
6438 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6439 2e1fa222 2020-07-23 stsp free(ondisk_path);
6440 2e1fa222 2020-07-23 stsp if (err)
6441 cf066bf8 2019-05-09 stsp goto done;
6442 cf066bf8 2019-05-09 stsp }
6443 036813ee 2019-05-09 stsp
6444 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6445 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6446 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6447 036813ee 2019-05-09 stsp if (err)
6448 036813ee 2019-05-09 stsp goto done;
6449 036813ee 2019-05-09 stsp
6450 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6451 de18fc63 2019-05-09 stsp if (err)
6452 de18fc63 2019-05-09 stsp goto done;
6453 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6454 f259c4c1 2021-09-24 stsp nparents++;
6455 f259c4c1 2021-09-24 stsp if (parent_id2) {
6456 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6457 f259c4c1 2021-09-24 stsp if (err)
6458 f259c4c1 2021-09-24 stsp goto done;
6459 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6460 f259c4c1 2021-09-24 stsp nparents++;
6461 f259c4c1 2021-09-24 stsp }
6462 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6463 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6464 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6465 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6466 33ad4cbe 2019-05-12 jcs free(logmsg);
6467 9d40349a 2019-05-09 stsp if (err)
6468 9d40349a 2019-05-09 stsp goto done;
6469 9d40349a 2019-05-09 stsp
6470 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6471 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6472 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6473 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6474 09f5bd90 2019-05-09 stsp goto done;
6475 09f5bd90 2019-05-09 stsp }
6476 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6477 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6478 09f5bd90 2019-05-09 stsp if (err)
6479 09f5bd90 2019-05-09 stsp goto done;
6480 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6481 ebf99748 2019-05-09 stsp if (err)
6482 ebf99748 2019-05-09 stsp goto done;
6483 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6484 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6485 09f5bd90 2019-05-09 stsp goto done;
6486 09f5bd90 2019-05-09 stsp }
6487 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6488 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6489 f2c16586 2019-05-09 stsp if (err)
6490 f2c16586 2019-05-09 stsp goto done;
6491 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6492 f2c16586 2019-05-09 stsp if (err)
6493 f2c16586 2019-05-09 stsp goto done;
6494 f2c16586 2019-05-09 stsp
6495 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6496 09f5bd90 2019-05-09 stsp if (err)
6497 09f5bd90 2019-05-09 stsp goto done;
6498 09f5bd90 2019-05-09 stsp
6499 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6500 ebf99748 2019-05-09 stsp if (err)
6501 ebf99748 2019-05-09 stsp goto done;
6502 c4296144 2019-05-09 stsp done:
6503 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6504 588edf97 2019-05-10 stsp if (head_tree)
6505 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6506 588edf97 2019-05-10 stsp if (head_commit)
6507 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6508 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6509 2f17228e 2019-05-12 stsp if (head_ref2) {
6510 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6511 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6512 2f17228e 2019-05-12 stsp err = unlockerr;
6513 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6514 39cd0ff6 2019-07-12 stsp }
6515 39cd0ff6 2019-07-12 stsp return err;
6516 39cd0ff6 2019-07-12 stsp }
6517 5c1e53bc 2019-07-28 stsp
6518 5c1e53bc 2019-07-28 stsp static const struct got_error *
6519 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6520 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6521 5c1e53bc 2019-07-28 stsp {
6522 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6523 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6524 39cd0ff6 2019-07-12 stsp
6525 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6526 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6527 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6528 5c1e53bc 2019-07-28 stsp
6529 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6530 5c1e53bc 2019-07-28 stsp ct_path++;
6531 5c1e53bc 2019-07-28 stsp
6532 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6533 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6534 5c1e53bc 2019-07-28 stsp break;
6535 5c1e53bc 2019-07-28 stsp }
6536 5c1e53bc 2019-07-28 stsp
6537 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6538 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6539 5c1e53bc 2019-07-28 stsp
6540 5c1e53bc 2019-07-28 stsp return NULL;
6541 5c1e53bc 2019-07-28 stsp }
6542 5c1e53bc 2019-07-28 stsp
6543 f0b75401 2019-08-03 stsp static const struct got_error *
6544 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6545 f0b75401 2019-08-03 stsp {
6546 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6547 f0b75401 2019-08-03 stsp
6548 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6549 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6550 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6551 f0b75401 2019-08-03 stsp }
6552 f0b75401 2019-08-03 stsp
6553 f0b75401 2019-08-03 stsp return NULL;
6554 f0b75401 2019-08-03 stsp }
6555 f0b75401 2019-08-03 stsp
6556 5f8a88c6 2019-08-03 stsp static const struct got_error *
6557 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6558 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6559 5f8a88c6 2019-08-03 stsp {
6560 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6561 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6562 5f8a88c6 2019-08-03 stsp
6563 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6564 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6565 5f8a88c6 2019-08-03 stsp continue;
6566 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6567 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6568 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6569 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6570 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6571 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6572 5f8a88c6 2019-08-03 stsp }
6573 5f8a88c6 2019-08-03 stsp
6574 5f8a88c6 2019-08-03 stsp return NULL;
6575 5f8a88c6 2019-08-03 stsp }
6576 5f8a88c6 2019-08-03 stsp
6577 39cd0ff6 2019-07-12 stsp const struct got_error *
6578 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6579 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6580 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6581 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6582 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6583 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6584 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6585 39cd0ff6 2019-07-12 stsp {
6586 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6587 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6588 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6589 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6590 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6591 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6592 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6593 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6594 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6595 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6596 39cd0ff6 2019-07-12 stsp
6597 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6598 39cd0ff6 2019-07-12 stsp
6599 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6600 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6601 39cd0ff6 2019-07-12 stsp
6602 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6603 39cd0ff6 2019-07-12 stsp if (err)
6604 39cd0ff6 2019-07-12 stsp goto done;
6605 39cd0ff6 2019-07-12 stsp
6606 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6607 39cd0ff6 2019-07-12 stsp if (err)
6608 39cd0ff6 2019-07-12 stsp goto done;
6609 39cd0ff6 2019-07-12 stsp
6610 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6611 39cd0ff6 2019-07-12 stsp if (err)
6612 39cd0ff6 2019-07-12 stsp goto done;
6613 39cd0ff6 2019-07-12 stsp
6614 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6615 39cd0ff6 2019-07-12 stsp if (err)
6616 39cd0ff6 2019-07-12 stsp goto done;
6617 39cd0ff6 2019-07-12 stsp
6618 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6619 5f8a88c6 2019-08-03 stsp &have_staged_files);
6620 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6621 5f8a88c6 2019-08-03 stsp goto done;
6622 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6623 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6624 5f8a88c6 2019-08-03 stsp if (err)
6625 5f8a88c6 2019-08-03 stsp goto done;
6626 5f8a88c6 2019-08-03 stsp }
6627 5f8a88c6 2019-08-03 stsp
6628 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6629 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6630 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6631 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6632 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6633 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6634 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6635 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6636 2a47b1e5 2022-11-01 stsp if (show_diff) {
6637 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6638 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6639 2a47b1e5 2022-11-01 stsp if (err)
6640 2a47b1e5 2022-11-01 stsp goto done;
6641 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6642 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6643 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6644 2a47b1e5 2022-11-01 stsp goto done;
6645 2a47b1e5 2022-11-01 stsp }
6646 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6647 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6648 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6649 2a47b1e5 2022-11-01 stsp goto done;
6650 2a47b1e5 2022-11-01 stsp }
6651 2a47b1e5 2022-11-01 stsp }
6652 2a47b1e5 2022-11-01 stsp
6653 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6654 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6655 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6656 5c1e53bc 2019-07-28 stsp if (err)
6657 5c1e53bc 2019-07-28 stsp goto done;
6658 5c1e53bc 2019-07-28 stsp }
6659 39cd0ff6 2019-07-12 stsp
6660 2a47b1e5 2022-11-01 stsp if (show_diff) {
6661 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6662 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6663 2a47b1e5 2022-11-01 stsp goto done;
6664 2a47b1e5 2022-11-01 stsp }
6665 2a47b1e5 2022-11-01 stsp }
6666 2a47b1e5 2022-11-01 stsp
6667 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6668 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6669 39cd0ff6 2019-07-12 stsp goto done;
6670 5c1e53bc 2019-07-28 stsp }
6671 5c1e53bc 2019-07-28 stsp
6672 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6673 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6674 5c1e53bc 2019-07-28 stsp if (err)
6675 5c1e53bc 2019-07-28 stsp goto done;
6676 39cd0ff6 2019-07-12 stsp }
6677 f0b75401 2019-08-03 stsp
6678 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6679 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6680 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6681 f0b75401 2019-08-03 stsp
6682 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6683 f0b75401 2019-08-03 stsp ct_path++;
6684 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6685 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6686 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6687 b50cabdf 2019-07-12 stsp if (err)
6688 b50cabdf 2019-07-12 stsp goto done;
6689 f0b75401 2019-08-03 stsp
6690 b50cabdf 2019-07-12 stsp }
6691 b50cabdf 2019-07-12 stsp
6692 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6693 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6694 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6695 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6696 39cd0ff6 2019-07-12 stsp if (err)
6697 39cd0ff6 2019-07-12 stsp goto done;
6698 39cd0ff6 2019-07-12 stsp
6699 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6700 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6701 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6702 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6703 39cd0ff6 2019-07-12 stsp err = sync_err;
6704 39cd0ff6 2019-07-12 stsp done:
6705 39cd0ff6 2019-07-12 stsp if (fileindex)
6706 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6707 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6708 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6709 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6710 39cd0ff6 2019-07-12 stsp err = unlockerr;
6711 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6712 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6713 d8bacb93 2023-01-10 mark
6714 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6715 39cd0ff6 2019-07-12 stsp }
6716 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6717 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6718 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6719 2a47b1e5 2022-11-01 stsp free(diff_path);
6720 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6721 2a47b1e5 2022-11-01 stsp err == NULL)
6722 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6723 c4296144 2019-05-09 stsp return err;
6724 8656d6c4 2019-05-20 stsp }
6725 8656d6c4 2019-05-20 stsp
6726 8656d6c4 2019-05-20 stsp const char *
6727 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6728 8656d6c4 2019-05-20 stsp {
6729 8656d6c4 2019-05-20 stsp return ct->path;
6730 8656d6c4 2019-05-20 stsp }
6731 8656d6c4 2019-05-20 stsp
6732 8656d6c4 2019-05-20 stsp unsigned int
6733 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6734 8656d6c4 2019-05-20 stsp {
6735 8656d6c4 2019-05-20 stsp return ct->status;
6736 818c7501 2019-07-11 stsp }
6737 818c7501 2019-07-11 stsp
6738 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6739 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6740 818c7501 2019-07-11 stsp struct got_repository *repo;
6741 818c7501 2019-07-11 stsp };
6742 818c7501 2019-07-11 stsp
6743 818c7501 2019-07-11 stsp static const struct got_error *
6744 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6745 818c7501 2019-07-11 stsp {
6746 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6747 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6748 818c7501 2019-07-11 stsp unsigned char status;
6749 818c7501 2019-07-11 stsp struct stat sb;
6750 818c7501 2019-07-11 stsp char *ondisk_path;
6751 818c7501 2019-07-11 stsp
6752 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6753 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6754 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6755 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6756 818c7501 2019-07-11 stsp
6757 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6758 818c7501 2019-07-11 stsp == -1)
6759 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6760 818c7501 2019-07-11 stsp
6761 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6762 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6763 818c7501 2019-07-11 stsp free(ondisk_path);
6764 818c7501 2019-07-11 stsp if (err)
6765 818c7501 2019-07-11 stsp return err;
6766 818c7501 2019-07-11 stsp
6767 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6768 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6769 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6770 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6771 818c7501 2019-07-11 stsp
6772 818c7501 2019-07-11 stsp return NULL;
6773 818c7501 2019-07-11 stsp }
6774 818c7501 2019-07-11 stsp
6775 818c7501 2019-07-11 stsp const struct got_error *
6776 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6777 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6778 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6779 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6780 818c7501 2019-07-11 stsp {
6781 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6782 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6783 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6784 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6785 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6786 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6787 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6788 818c7501 2019-07-11 stsp
6789 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6790 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6791 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6792 818c7501 2019-07-11 stsp
6793 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6794 818c7501 2019-07-11 stsp if (err)
6795 818c7501 2019-07-11 stsp return err;
6796 818c7501 2019-07-11 stsp
6797 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp
6801 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6802 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6803 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6804 818c7501 2019-07-11 stsp &ok_arg);
6805 818c7501 2019-07-11 stsp if (err)
6806 818c7501 2019-07-11 stsp goto done;
6807 818c7501 2019-07-11 stsp
6808 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6809 818c7501 2019-07-11 stsp if (err)
6810 818c7501 2019-07-11 stsp goto done;
6811 818c7501 2019-07-11 stsp
6812 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6813 818c7501 2019-07-11 stsp if (err)
6814 818c7501 2019-07-11 stsp goto done;
6815 818c7501 2019-07-11 stsp
6816 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6817 818c7501 2019-07-11 stsp if (err)
6818 818c7501 2019-07-11 stsp goto done;
6819 818c7501 2019-07-11 stsp
6820 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6821 818c7501 2019-07-11 stsp 0);
6822 e51d7b55 2020-01-04 stsp if (err)
6823 e51d7b55 2020-01-04 stsp goto done;
6824 e51d7b55 2020-01-04 stsp
6825 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6826 818c7501 2019-07-11 stsp if (err)
6827 818c7501 2019-07-11 stsp goto done;
6828 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6829 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6830 e51d7b55 2020-01-04 stsp goto done;
6831 e51d7b55 2020-01-04 stsp }
6832 818c7501 2019-07-11 stsp
6833 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6834 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6835 818c7501 2019-07-11 stsp if (err)
6836 818c7501 2019-07-11 stsp goto done;
6837 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6838 818c7501 2019-07-11 stsp if (err)
6839 818c7501 2019-07-11 stsp goto done;
6840 818c7501 2019-07-11 stsp
6841 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6842 818c7501 2019-07-11 stsp
6843 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6844 818c7501 2019-07-11 stsp if (err)
6845 818c7501 2019-07-11 stsp goto done;
6846 818c7501 2019-07-11 stsp
6847 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6848 818c7501 2019-07-11 stsp if (err)
6849 818c7501 2019-07-11 stsp goto done;
6850 818c7501 2019-07-11 stsp
6851 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6852 818c7501 2019-07-11 stsp worktree->base_commit_id);
6853 818c7501 2019-07-11 stsp if (err)
6854 818c7501 2019-07-11 stsp goto done;
6855 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6856 818c7501 2019-07-11 stsp if (err)
6857 818c7501 2019-07-11 stsp goto done;
6858 818c7501 2019-07-11 stsp
6859 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6860 818c7501 2019-07-11 stsp if (err)
6861 818c7501 2019-07-11 stsp goto done;
6862 818c7501 2019-07-11 stsp done:
6863 818c7501 2019-07-11 stsp free(fileindex_path);
6864 818c7501 2019-07-11 stsp free(tmp_branch_name);
6865 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6866 818c7501 2019-07-11 stsp free(branch_ref_name);
6867 818c7501 2019-07-11 stsp if (branch_ref)
6868 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6869 818c7501 2019-07-11 stsp if (wt_branch)
6870 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6871 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6872 818c7501 2019-07-11 stsp if (err) {
6873 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6874 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6875 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6876 818c7501 2019-07-11 stsp }
6877 818c7501 2019-07-11 stsp if (*tmp_branch) {
6878 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6879 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6880 818c7501 2019-07-11 stsp }
6881 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6882 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6883 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6884 3e3a69f1 2019-07-25 stsp }
6885 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6886 818c7501 2019-07-11 stsp }
6887 818c7501 2019-07-11 stsp return err;
6888 818c7501 2019-07-11 stsp }
6889 818c7501 2019-07-11 stsp
6890 818c7501 2019-07-11 stsp const struct got_error *
6891 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6892 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6893 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6894 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6895 818c7501 2019-07-11 stsp {
6896 818c7501 2019-07-11 stsp const struct got_error *err;
6897 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6898 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6899 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6900 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6901 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6902 818c7501 2019-07-11 stsp
6903 818c7501 2019-07-11 stsp *commit_id = NULL;
6904 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6905 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6906 3e3a69f1 2019-07-25 stsp *branch = NULL;
6907 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6908 3e3a69f1 2019-07-25 stsp
6909 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6910 3e3a69f1 2019-07-25 stsp if (err)
6911 3e3a69f1 2019-07-25 stsp return err;
6912 818c7501 2019-07-11 stsp
6913 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6914 3e3a69f1 2019-07-25 stsp if (err)
6915 f032f1f7 2019-08-04 stsp goto done;
6916 f032f1f7 2019-08-04 stsp
6917 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6918 f032f1f7 2019-08-04 stsp &have_staged_files);
6919 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6920 f032f1f7 2019-08-04 stsp goto done;
6921 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6922 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6923 3e3a69f1 2019-07-25 stsp goto done;
6924 f032f1f7 2019-08-04 stsp }
6925 3e3a69f1 2019-07-25 stsp
6926 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6927 818c7501 2019-07-11 stsp if (err)
6928 f032f1f7 2019-08-04 stsp goto done;
6929 818c7501 2019-07-11 stsp
6930 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6931 818c7501 2019-07-11 stsp if (err)
6932 818c7501 2019-07-11 stsp goto done;
6933 818c7501 2019-07-11 stsp
6934 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6935 818c7501 2019-07-11 stsp if (err)
6936 818c7501 2019-07-11 stsp goto done;
6937 818c7501 2019-07-11 stsp
6938 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6939 818c7501 2019-07-11 stsp if (err)
6940 818c7501 2019-07-11 stsp goto done;
6941 818c7501 2019-07-11 stsp
6942 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6943 818c7501 2019-07-11 stsp if (err)
6944 818c7501 2019-07-11 stsp goto done;
6945 818c7501 2019-07-11 stsp
6946 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6947 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6948 818c7501 2019-07-11 stsp if (err)
6949 818c7501 2019-07-11 stsp goto done;
6950 818c7501 2019-07-11 stsp
6951 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6952 818c7501 2019-07-11 stsp if (err)
6953 818c7501 2019-07-11 stsp goto done;
6954 818c7501 2019-07-11 stsp
6955 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6956 818c7501 2019-07-11 stsp if (err)
6957 818c7501 2019-07-11 stsp goto done;
6958 818c7501 2019-07-11 stsp
6959 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6960 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6961 818c7501 2019-07-11 stsp if (err)
6962 818c7501 2019-07-11 stsp goto done;
6963 818c7501 2019-07-11 stsp
6964 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6965 818c7501 2019-07-11 stsp if (err)
6966 818c7501 2019-07-11 stsp goto done;
6967 818c7501 2019-07-11 stsp done:
6968 818c7501 2019-07-11 stsp free(commit_ref_name);
6969 818c7501 2019-07-11 stsp free(branch_ref_name);
6970 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6971 818c7501 2019-07-11 stsp if (commit_ref)
6972 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6973 818c7501 2019-07-11 stsp if (branch_ref)
6974 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6975 818c7501 2019-07-11 stsp if (err) {
6976 818c7501 2019-07-11 stsp free(*commit_id);
6977 818c7501 2019-07-11 stsp *commit_id = NULL;
6978 818c7501 2019-07-11 stsp if (*tmp_branch) {
6979 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6980 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6981 818c7501 2019-07-11 stsp }
6982 818c7501 2019-07-11 stsp if (*new_base_branch) {
6983 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6984 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6985 818c7501 2019-07-11 stsp }
6986 818c7501 2019-07-11 stsp if (*branch) {
6987 818c7501 2019-07-11 stsp got_ref_close(*branch);
6988 818c7501 2019-07-11 stsp *branch = NULL;
6989 818c7501 2019-07-11 stsp }
6990 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6991 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6992 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6993 3e3a69f1 2019-07-25 stsp }
6994 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6995 818c7501 2019-07-11 stsp }
6996 818c7501 2019-07-11 stsp return err;
6997 c4296144 2019-05-09 stsp }
6998 818c7501 2019-07-11 stsp
6999 818c7501 2019-07-11 stsp const struct got_error *
7000 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7001 818c7501 2019-07-11 stsp {
7002 818c7501 2019-07-11 stsp const struct got_error *err;
7003 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7004 818c7501 2019-07-11 stsp
7005 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7006 818c7501 2019-07-11 stsp if (err)
7007 818c7501 2019-07-11 stsp return err;
7008 818c7501 2019-07-11 stsp
7009 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7010 818c7501 2019-07-11 stsp free(tmp_branch_name);
7011 818c7501 2019-07-11 stsp return NULL;
7012 f4ab0e57 2024-03-28 stsp }
7013 f4ab0e57 2024-03-28 stsp
7014 f4ab0e57 2024-03-28 stsp const struct got_error *
7015 f4ab0e57 2024-03-28 stsp got_worktree_rebase_info(char **new_base_branch_name, char **branch_name,
7016 f4ab0e57 2024-03-28 stsp struct got_worktree *worktree, struct got_repository *repo)
7017 f4ab0e57 2024-03-28 stsp {
7018 f4ab0e57 2024-03-28 stsp const struct got_error *err;
7019 f4ab0e57 2024-03-28 stsp char *new_base_branch_ref_name = NULL;
7020 f4ab0e57 2024-03-28 stsp char *branch_ref_name = NULL;
7021 f4ab0e57 2024-03-28 stsp struct got_reference *branch_ref = NULL, *branch = NULL;
7022 f4ab0e57 2024-03-28 stsp struct got_reference *new_base_branch = NULL;
7023 f4ab0e57 2024-03-28 stsp
7024 f4ab0e57 2024-03-28 stsp *new_base_branch_name = NULL;
7025 f4ab0e57 2024-03-28 stsp *branch_name = NULL;
7026 f4ab0e57 2024-03-28 stsp
7027 f4ab0e57 2024-03-28 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7028 f4ab0e57 2024-03-28 stsp if (err)
7029 f4ab0e57 2024-03-28 stsp goto done;
7030 f4ab0e57 2024-03-28 stsp
7031 f4ab0e57 2024-03-28 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7032 f4ab0e57 2024-03-28 stsp if (err)
7033 f4ab0e57 2024-03-28 stsp goto done;
7034 f4ab0e57 2024-03-28 stsp
7035 f4ab0e57 2024-03-28 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
7036 f4ab0e57 2024-03-28 stsp if (err)
7037 f4ab0e57 2024-03-28 stsp goto done;
7038 f4ab0e57 2024-03-28 stsp
7039 f4ab0e57 2024-03-28 stsp err = got_ref_open(&branch, repo,
7040 f4ab0e57 2024-03-28 stsp got_ref_get_symref_target(branch_ref), 0);
7041 f4ab0e57 2024-03-28 stsp if (err)
7042 f4ab0e57 2024-03-28 stsp goto done;
7043 f4ab0e57 2024-03-28 stsp
7044 f4ab0e57 2024-03-28 stsp err = got_ref_open(&new_base_branch, repo,
7045 f4ab0e57 2024-03-28 stsp new_base_branch_ref_name, 0);
7046 f4ab0e57 2024-03-28 stsp if (err)
7047 f4ab0e57 2024-03-28 stsp goto done;
7048 f4ab0e57 2024-03-28 stsp
7049 f4ab0e57 2024-03-28 stsp if (!got_ref_is_symbolic(new_base_branch)) {
7050 f4ab0e57 2024-03-28 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7051 f4ab0e57 2024-03-28 stsp "%s is not a symbolic reference",
7052 f4ab0e57 2024-03-28 stsp got_ref_get_name(branch_ref));
7053 f4ab0e57 2024-03-28 stsp goto done;
7054 f4ab0e57 2024-03-28 stsp }
7055 f4ab0e57 2024-03-28 stsp
7056 f4ab0e57 2024-03-28 stsp *new_base_branch_name = strdup(got_ref_get_symref_target(
7057 f4ab0e57 2024-03-28 stsp new_base_branch));
7058 f4ab0e57 2024-03-28 stsp if (*new_base_branch_name == NULL) {
7059 f4ab0e57 2024-03-28 stsp err = got_error_from_errno("strdup");
7060 f4ab0e57 2024-03-28 stsp goto done;
7061 f4ab0e57 2024-03-28 stsp }
7062 f4ab0e57 2024-03-28 stsp *branch_name = strdup(got_ref_get_name(branch));
7063 f4ab0e57 2024-03-28 stsp if (*branch_name == NULL) {
7064 f4ab0e57 2024-03-28 stsp err = got_error_from_errno("strdup");
7065 f4ab0e57 2024-03-28 stsp goto done;
7066 f4ab0e57 2024-03-28 stsp }
7067 f4ab0e57 2024-03-28 stsp done:
7068 f4ab0e57 2024-03-28 stsp free(branch_ref_name);
7069 f4ab0e57 2024-03-28 stsp if (branch_ref)
7070 f4ab0e57 2024-03-28 stsp got_ref_close(branch_ref);
7071 f4ab0e57 2024-03-28 stsp if (new_base_branch)
7072 f4ab0e57 2024-03-28 stsp got_ref_close(new_base_branch);
7073 f4ab0e57 2024-03-28 stsp if (branch)
7074 f4ab0e57 2024-03-28 stsp got_ref_close(branch);
7075 f4ab0e57 2024-03-28 stsp return err;
7076 818c7501 2019-07-11 stsp }
7077 818c7501 2019-07-11 stsp
7078 818c7501 2019-07-11 stsp static const struct got_error *
7079 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7080 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7081 818c7501 2019-07-11 stsp {
7082 0ebf8283 2019-07-24 stsp *logmsg = arg;
7083 818c7501 2019-07-11 stsp return NULL;
7084 818c7501 2019-07-11 stsp }
7085 818c7501 2019-07-11 stsp
7086 818c7501 2019-07-11 stsp static const struct got_error *
7087 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7088 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7089 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7090 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7091 818c7501 2019-07-11 stsp {
7092 818c7501 2019-07-11 stsp return NULL;
7093 01757395 2019-07-12 stsp }
7094 01757395 2019-07-12 stsp
7095 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7096 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7097 01757395 2019-07-12 stsp void *progress_arg;
7098 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7099 01757395 2019-07-12 stsp };
7100 01757395 2019-07-12 stsp
7101 01757395 2019-07-12 stsp static const struct got_error *
7102 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7103 01757395 2019-07-12 stsp {
7104 01757395 2019-07-12 stsp const struct got_error *err;
7105 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7106 01757395 2019-07-12 stsp char *p;
7107 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7108 01757395 2019-07-12 stsp
7109 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7110 01757395 2019-07-12 stsp if (err)
7111 01757395 2019-07-12 stsp return err;
7112 01757395 2019-07-12 stsp
7113 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7114 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7115 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7116 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7117 01757395 2019-07-12 stsp return NULL;
7118 01757395 2019-07-12 stsp
7119 01757395 2019-07-12 stsp p = strdup(path);
7120 01757395 2019-07-12 stsp if (p == NULL)
7121 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7122 01757395 2019-07-12 stsp
7123 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7124 01757395 2019-07-12 stsp if (err || new == NULL)
7125 01757395 2019-07-12 stsp free(p);
7126 01757395 2019-07-12 stsp return err;
7127 818c7501 2019-07-11 stsp }
7128 818c7501 2019-07-11 stsp
7129 0ebf8283 2019-07-24 stsp static const struct got_error *
7130 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7131 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7132 818c7501 2019-07-11 stsp {
7133 818c7501 2019-07-11 stsp const struct got_error *err;
7134 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7135 818c7501 2019-07-11 stsp
7136 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7137 818c7501 2019-07-11 stsp if (err) {
7138 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7139 818c7501 2019-07-11 stsp goto done;
7140 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7141 818c7501 2019-07-11 stsp if (err)
7142 818c7501 2019-07-11 stsp goto done;
7143 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7144 818c7501 2019-07-11 stsp if (err)
7145 818c7501 2019-07-11 stsp goto done;
7146 de05890f 2020-03-05 stsp } else if (is_rebase) {
7147 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7148 818c7501 2019-07-11 stsp int cmp;
7149 818c7501 2019-07-11 stsp
7150 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7151 818c7501 2019-07-11 stsp if (err)
7152 818c7501 2019-07-11 stsp goto done;
7153 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7154 818c7501 2019-07-11 stsp free(stored_id);
7155 818c7501 2019-07-11 stsp if (cmp != 0) {
7156 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7157 818c7501 2019-07-11 stsp goto done;
7158 818c7501 2019-07-11 stsp }
7159 818c7501 2019-07-11 stsp }
7160 0ebf8283 2019-07-24 stsp done:
7161 0ebf8283 2019-07-24 stsp if (commit_ref)
7162 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7163 0ebf8283 2019-07-24 stsp return err;
7164 0ebf8283 2019-07-24 stsp }
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp static const struct got_error *
7167 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7168 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7169 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7170 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7171 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7172 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7173 0ebf8283 2019-07-24 stsp {
7174 0ebf8283 2019-07-24 stsp const struct got_error *err;
7175 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7176 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7177 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7178 818c7501 2019-07-11 stsp
7179 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7180 0ebf8283 2019-07-24 stsp
7181 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7182 0ebf8283 2019-07-24 stsp if (err)
7183 0ebf8283 2019-07-24 stsp return err;
7184 0ebf8283 2019-07-24 stsp
7185 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7186 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7187 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7188 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7189 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7190 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7191 818c7501 2019-07-11 stsp if (commit_ref)
7192 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7193 818c7501 2019-07-11 stsp return err;
7194 818c7501 2019-07-11 stsp }
7195 818c7501 2019-07-11 stsp
7196 818c7501 2019-07-11 stsp const struct got_error *
7197 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7198 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7199 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7200 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7201 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7202 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7203 818c7501 2019-07-11 stsp {
7204 0ebf8283 2019-07-24 stsp const struct got_error *err;
7205 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7206 0ebf8283 2019-07-24 stsp
7207 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7208 0ebf8283 2019-07-24 stsp if (err)
7209 0ebf8283 2019-07-24 stsp return err;
7210 0ebf8283 2019-07-24 stsp
7211 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7212 0ebf8283 2019-07-24 stsp if (err)
7213 0ebf8283 2019-07-24 stsp goto done;
7214 0ebf8283 2019-07-24 stsp
7215 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7216 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7217 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7218 0ebf8283 2019-07-24 stsp done:
7219 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7220 0ebf8283 2019-07-24 stsp return err;
7221 0ebf8283 2019-07-24 stsp }
7222 0ebf8283 2019-07-24 stsp
7223 0ebf8283 2019-07-24 stsp const struct got_error *
7224 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7225 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7226 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7227 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7228 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7229 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7230 0ebf8283 2019-07-24 stsp {
7231 0ebf8283 2019-07-24 stsp const struct got_error *err;
7232 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7233 0ebf8283 2019-07-24 stsp
7234 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7235 0ebf8283 2019-07-24 stsp if (err)
7236 0ebf8283 2019-07-24 stsp return err;
7237 0ebf8283 2019-07-24 stsp
7238 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7239 0ebf8283 2019-07-24 stsp if (err)
7240 0ebf8283 2019-07-24 stsp goto done;
7241 0ebf8283 2019-07-24 stsp
7242 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7243 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7244 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7245 0ebf8283 2019-07-24 stsp done:
7246 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7247 0ebf8283 2019-07-24 stsp return err;
7248 0ebf8283 2019-07-24 stsp }
7249 0ebf8283 2019-07-24 stsp
7250 0ebf8283 2019-07-24 stsp static const struct got_error *
7251 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7252 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7253 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7254 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7255 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7256 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7257 0ebf8283 2019-07-24 stsp {
7258 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7259 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7260 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7261 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7262 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7263 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7264 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7265 818c7501 2019-07-11 stsp
7266 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7267 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7268 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7269 a0e95631 2019-07-12 stsp
7270 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7271 818c7501 2019-07-11 stsp
7272 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7273 a0e95631 2019-07-12 stsp if (err)
7274 3e3a69f1 2019-07-25 stsp return err;
7275 a0e95631 2019-07-12 stsp
7276 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7277 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7278 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7279 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7280 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7281 01757395 2019-07-12 stsp /*
7282 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7283 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7284 6c13b005 2021-09-02 stsp *
7285 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7286 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7287 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7288 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7289 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7290 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7291 01757395 2019-07-12 stsp */
7292 01757395 2019-07-12 stsp if (merged_paths) {
7293 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7294 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7295 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7296 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7297 f2a9dc41 2019-12-13 tracey 0);
7298 01757395 2019-07-12 stsp if (err)
7299 01757395 2019-07-12 stsp goto done;
7300 01757395 2019-07-12 stsp }
7301 01757395 2019-07-12 stsp } else {
7302 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7303 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7304 01757395 2019-07-12 stsp if (err)
7305 01757395 2019-07-12 stsp goto done;
7306 01757395 2019-07-12 stsp }
7307 a0e95631 2019-07-12 stsp
7308 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7309 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7310 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7311 a0e95631 2019-07-12 stsp if (err)
7312 a0e95631 2019-07-12 stsp goto done;
7313 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7314 a0e95631 2019-07-12 stsp goto done;
7315 ff0d2220 2019-07-11 stsp }
7316 818c7501 2019-07-11 stsp
7317 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7318 edd02c5e 2019-07-11 stsp if (err)
7319 edd02c5e 2019-07-11 stsp goto done;
7320 edd02c5e 2019-07-11 stsp
7321 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7322 818c7501 2019-07-11 stsp if (err)
7323 818c7501 2019-07-11 stsp goto done;
7324 818c7501 2019-07-11 stsp
7325 5943eee2 2019-08-13 stsp if (new_logmsg) {
7326 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7327 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7328 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7329 5943eee2 2019-08-13 stsp goto done;
7330 5943eee2 2019-08-13 stsp }
7331 5943eee2 2019-08-13 stsp } else {
7332 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7333 5943eee2 2019-08-13 stsp if (err)
7334 5943eee2 2019-08-13 stsp goto done;
7335 5943eee2 2019-08-13 stsp }
7336 0ebf8283 2019-07-24 stsp
7337 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7338 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7339 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7340 50e7a649 2022-07-22 stsp committer ? committer :
7341 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7342 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7343 a0e95631 2019-07-12 stsp if (err)
7344 a0e95631 2019-07-12 stsp goto done;
7345 a0e95631 2019-07-12 stsp
7346 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7347 a0e95631 2019-07-12 stsp if (err)
7348 a0e95631 2019-07-12 stsp goto done;
7349 a0e95631 2019-07-12 stsp
7350 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7351 a0e95631 2019-07-12 stsp if (err)
7352 a0e95631 2019-07-12 stsp goto done;
7353 a0e95631 2019-07-12 stsp
7354 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7355 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7356 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7357 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7358 a0e95631 2019-07-12 stsp err = sync_err;
7359 818c7501 2019-07-11 stsp done:
7360 a0e95631 2019-07-12 stsp free(fileindex_path);
7361 a0e95631 2019-07-12 stsp free(head_commit_id);
7362 a0e95631 2019-07-12 stsp if (head_ref)
7363 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7364 818c7501 2019-07-11 stsp if (err) {
7365 818c7501 2019-07-11 stsp free(*new_commit_id);
7366 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7367 818c7501 2019-07-11 stsp }
7368 818c7501 2019-07-11 stsp return err;
7369 818c7501 2019-07-11 stsp }
7370 818c7501 2019-07-11 stsp
7371 818c7501 2019-07-11 stsp const struct got_error *
7372 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7373 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7374 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7375 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7376 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7377 12383673 2023-02-18 mark struct got_repository *repo)
7378 0ebf8283 2019-07-24 stsp {
7379 0ebf8283 2019-07-24 stsp const struct got_error *err;
7380 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7381 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7382 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7385 0ebf8283 2019-07-24 stsp if (err)
7386 0ebf8283 2019-07-24 stsp return err;
7387 0ebf8283 2019-07-24 stsp
7388 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7389 0ebf8283 2019-07-24 stsp if (err)
7390 0ebf8283 2019-07-24 stsp goto done;
7391 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7392 0ebf8283 2019-07-24 stsp if (err)
7393 0ebf8283 2019-07-24 stsp goto done;
7394 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7395 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7396 0ebf8283 2019-07-24 stsp goto done;
7397 0ebf8283 2019-07-24 stsp }
7398 0ebf8283 2019-07-24 stsp
7399 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7400 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7401 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7402 0ebf8283 2019-07-24 stsp done:
7403 0ebf8283 2019-07-24 stsp if (commit_ref)
7404 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7405 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7406 0ebf8283 2019-07-24 stsp free(commit_id);
7407 0ebf8283 2019-07-24 stsp return err;
7408 0ebf8283 2019-07-24 stsp }
7409 0ebf8283 2019-07-24 stsp
7410 0ebf8283 2019-07-24 stsp const struct got_error *
7411 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7412 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7413 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7414 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7415 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7416 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7417 0ebf8283 2019-07-24 stsp {
7418 0ebf8283 2019-07-24 stsp const struct got_error *err;
7419 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7420 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7421 0ebf8283 2019-07-24 stsp
7422 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7423 0ebf8283 2019-07-24 stsp if (err)
7424 0ebf8283 2019-07-24 stsp return err;
7425 0ebf8283 2019-07-24 stsp
7426 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7427 0ebf8283 2019-07-24 stsp if (err)
7428 0ebf8283 2019-07-24 stsp goto done;
7429 0ebf8283 2019-07-24 stsp
7430 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7431 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7432 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7433 0ebf8283 2019-07-24 stsp done:
7434 0ebf8283 2019-07-24 stsp if (commit_ref)
7435 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7436 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7437 0ebf8283 2019-07-24 stsp return err;
7438 0ebf8283 2019-07-24 stsp }
7439 0ebf8283 2019-07-24 stsp
7440 0ebf8283 2019-07-24 stsp const struct got_error *
7441 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7442 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7443 818c7501 2019-07-11 stsp {
7444 3e3a69f1 2019-07-25 stsp if (fileindex)
7445 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7446 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7447 69844fba 2019-07-11 stsp }
7448 69844fba 2019-07-11 stsp
7449 69844fba 2019-07-11 stsp static const struct got_error *
7450 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7451 69844fba 2019-07-11 stsp {
7452 69844fba 2019-07-11 stsp const struct got_error *err;
7453 69844fba 2019-07-11 stsp struct got_reference *ref;
7454 69844fba 2019-07-11 stsp
7455 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7456 69844fba 2019-07-11 stsp if (err) {
7457 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7458 69844fba 2019-07-11 stsp return NULL;
7459 69844fba 2019-07-11 stsp return err;
7460 69844fba 2019-07-11 stsp }
7461 69844fba 2019-07-11 stsp
7462 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7463 69844fba 2019-07-11 stsp got_ref_close(ref);
7464 69844fba 2019-07-11 stsp return err;
7465 818c7501 2019-07-11 stsp }
7466 818c7501 2019-07-11 stsp
7467 69844fba 2019-07-11 stsp static const struct got_error *
7468 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7469 69844fba 2019-07-11 stsp {
7470 69844fba 2019-07-11 stsp const struct got_error *err;
7471 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7472 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7473 69844fba 2019-07-11 stsp
7474 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7475 69844fba 2019-07-11 stsp if (err)
7476 69844fba 2019-07-11 stsp goto done;
7477 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7478 69844fba 2019-07-11 stsp if (err)
7479 69844fba 2019-07-11 stsp goto done;
7480 69844fba 2019-07-11 stsp
7481 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7482 69844fba 2019-07-11 stsp if (err)
7483 69844fba 2019-07-11 stsp goto done;
7484 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7485 69844fba 2019-07-11 stsp if (err)
7486 69844fba 2019-07-11 stsp goto done;
7487 69844fba 2019-07-11 stsp
7488 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7489 69844fba 2019-07-11 stsp if (err)
7490 69844fba 2019-07-11 stsp goto done;
7491 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7492 69844fba 2019-07-11 stsp if (err)
7493 69844fba 2019-07-11 stsp goto done;
7494 69844fba 2019-07-11 stsp
7495 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7496 69844fba 2019-07-11 stsp if (err)
7497 69844fba 2019-07-11 stsp goto done;
7498 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7499 69844fba 2019-07-11 stsp if (err)
7500 69844fba 2019-07-11 stsp goto done;
7501 69844fba 2019-07-11 stsp
7502 69844fba 2019-07-11 stsp done:
7503 69844fba 2019-07-11 stsp free(tmp_branch_name);
7504 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7505 69844fba 2019-07-11 stsp free(branch_ref_name);
7506 69844fba 2019-07-11 stsp free(commit_ref_name);
7507 e600f124 2021-03-21 stsp return err;
7508 e600f124 2021-03-21 stsp }
7509 e600f124 2021-03-21 stsp
7510 336075a4 2022-06-25 op static const struct got_error *
7511 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7512 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7513 e600f124 2021-03-21 stsp {
7514 e600f124 2021-03-21 stsp const struct got_error *err;
7515 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7516 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7517 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7518 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7519 e600f124 2021-03-21 stsp char *refname = NULL;
7520 e600f124 2021-03-21 stsp
7521 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7522 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7523 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7524 e600f124 2021-03-21 stsp branch_name += 11;
7525 e600f124 2021-03-21 stsp
7526 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7527 e600f124 2021-03-21 stsp if (err)
7528 e600f124 2021-03-21 stsp return err;
7529 e600f124 2021-03-21 stsp
7530 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7531 e600f124 2021-03-21 stsp new_id_str) == -1) {
7532 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7533 e600f124 2021-03-21 stsp goto done;
7534 e600f124 2021-03-21 stsp }
7535 e600f124 2021-03-21 stsp
7536 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7537 e600f124 2021-03-21 stsp if (err)
7538 e600f124 2021-03-21 stsp goto done;
7539 e600f124 2021-03-21 stsp
7540 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7541 e600f124 2021-03-21 stsp if (err)
7542 e600f124 2021-03-21 stsp goto done;
7543 e600f124 2021-03-21 stsp
7544 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7545 e600f124 2021-03-21 stsp done:
7546 e600f124 2021-03-21 stsp free(new_id_str);
7547 e600f124 2021-03-21 stsp free(refname);
7548 e600f124 2021-03-21 stsp free(old_commit_id);
7549 e600f124 2021-03-21 stsp if (ref)
7550 e600f124 2021-03-21 stsp got_ref_close(ref);
7551 69844fba 2019-07-11 stsp return err;
7552 69844fba 2019-07-11 stsp }
7553 69844fba 2019-07-11 stsp
7554 818c7501 2019-07-11 stsp const struct got_error *
7555 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7556 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7557 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7558 ef85a376 2023-02-01 mark int create_backup)
7559 818c7501 2019-07-11 stsp {
7560 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7561 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7562 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7563 818c7501 2019-07-11 stsp
7564 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7565 818c7501 2019-07-11 stsp if (err)
7566 818c7501 2019-07-11 stsp return err;
7567 e600f124 2021-03-21 stsp
7568 e600f124 2021-03-21 stsp if (create_backup) {
7569 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7570 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7571 e600f124 2021-03-21 stsp if (err)
7572 e600f124 2021-03-21 stsp goto done;
7573 e600f124 2021-03-21 stsp }
7574 818c7501 2019-07-11 stsp
7575 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7576 818c7501 2019-07-11 stsp if (err)
7577 818c7501 2019-07-11 stsp goto done;
7578 818c7501 2019-07-11 stsp
7579 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7580 818c7501 2019-07-11 stsp if (err)
7581 818c7501 2019-07-11 stsp goto done;
7582 818c7501 2019-07-11 stsp
7583 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7584 818c7501 2019-07-11 stsp if (err)
7585 818c7501 2019-07-11 stsp goto done;
7586 818c7501 2019-07-11 stsp
7587 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7588 a615e0e7 2020-12-16 stsp if (err)
7589 a615e0e7 2020-12-16 stsp goto done;
7590 a615e0e7 2020-12-16 stsp
7591 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7592 a615e0e7 2020-12-16 stsp if (err)
7593 a615e0e7 2020-12-16 stsp goto done;
7594 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7595 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7596 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7597 a615e0e7 2020-12-16 stsp err = sync_err;
7598 818c7501 2019-07-11 stsp done:
7599 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7600 a615e0e7 2020-12-16 stsp free(fileindex_path);
7601 818c7501 2019-07-11 stsp free(new_head_commit_id);
7602 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7603 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7604 818c7501 2019-07-11 stsp err = unlockerr;
7605 818c7501 2019-07-11 stsp return err;
7606 818c7501 2019-07-11 stsp }
7607 af179be7 2023-04-14 stsp
7608 af179be7 2023-04-14 stsp static const struct got_error *
7609 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7610 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7611 af179be7 2023-04-14 stsp struct got_repository *repo)
7612 af179be7 2023-04-14 stsp {
7613 af179be7 2023-04-14 stsp const struct got_error *err;
7614 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7615 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7616 af179be7 2023-04-14 stsp
7617 af179be7 2023-04-14 stsp if (id1) {
7618 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7619 af179be7 2023-04-14 stsp if (err)
7620 af179be7 2023-04-14 stsp goto done;
7621 af179be7 2023-04-14 stsp
7622 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7623 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7624 af179be7 2023-04-14 stsp if (err)
7625 af179be7 2023-04-14 stsp goto done;
7626 af179be7 2023-04-14 stsp }
7627 818c7501 2019-07-11 stsp
7628 af179be7 2023-04-14 stsp if (id2) {
7629 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7630 af179be7 2023-04-14 stsp if (err)
7631 af179be7 2023-04-14 stsp goto done;
7632 af179be7 2023-04-14 stsp
7633 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7634 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7635 af179be7 2023-04-14 stsp if (err)
7636 af179be7 2023-04-14 stsp goto done;
7637 af179be7 2023-04-14 stsp }
7638 af179be7 2023-04-14 stsp
7639 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7640 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7641 af179be7 2023-04-14 stsp if (err)
7642 af179be7 2023-04-14 stsp goto done;
7643 af179be7 2023-04-14 stsp done:
7644 af179be7 2023-04-14 stsp if (commit1)
7645 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7646 af179be7 2023-04-14 stsp if (commit2)
7647 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7648 af179be7 2023-04-14 stsp if (tree1)
7649 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7650 af179be7 2023-04-14 stsp if (tree2)
7651 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7652 af179be7 2023-04-14 stsp return err;
7653 af179be7 2023-04-14 stsp }
7654 af179be7 2023-04-14 stsp
7655 af179be7 2023-04-14 stsp static const struct got_error *
7656 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7657 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7658 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7659 af179be7 2023-04-14 stsp {
7660 af179be7 2023-04-14 stsp const struct got_error *err;
7661 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7662 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7663 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7664 af179be7 2023-04-14 stsp
7665 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7666 af179be7 2023-04-14 stsp
7667 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7668 af179be7 2023-04-14 stsp if (err)
7669 af179be7 2023-04-14 stsp goto done;
7670 af179be7 2023-04-14 stsp
7671 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7672 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7673 af179be7 2023-04-14 stsp
7674 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7675 af179be7 2023-04-14 stsp continue;
7676 af179be7 2023-04-14 stsp
7677 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7678 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7679 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7680 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7681 af179be7 2023-04-14 stsp goto done;
7682 af179be7 2023-04-14 stsp }
7683 af179be7 2023-04-14 stsp } else {
7684 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7685 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7686 af179be7 2023-04-14 stsp goto done;
7687 af179be7 2023-04-14 stsp }
7688 af179be7 2023-04-14 stsp
7689 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7690 af179be7 2023-04-14 stsp path_prefix, abspath);
7691 af179be7 2023-04-14 stsp if (err)
7692 af179be7 2023-04-14 stsp goto done;
7693 af179be7 2023-04-14 stsp free(abspath);
7694 af179be7 2023-04-14 stsp abspath = NULL;
7695 af179be7 2023-04-14 stsp }
7696 af179be7 2023-04-14 stsp
7697 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7698 af179be7 2023-04-14 stsp if (err)
7699 af179be7 2023-04-14 stsp goto done;
7700 af179be7 2023-04-14 stsp wt_path = NULL;
7701 af179be7 2023-04-14 stsp }
7702 af179be7 2023-04-14 stsp
7703 af179be7 2023-04-14 stsp done:
7704 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7705 af179be7 2023-04-14 stsp free(abspath);
7706 af179be7 2023-04-14 stsp free(wt_path);
7707 af179be7 2023-04-14 stsp return err;
7708 af179be7 2023-04-14 stsp }
7709 af179be7 2023-04-14 stsp
7710 af179be7 2023-04-14 stsp static const struct got_error *
7711 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7712 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7713 af179be7 2023-04-14 stsp struct got_repository *repo)
7714 af179be7 2023-04-14 stsp {
7715 af179be7 2023-04-14 stsp const struct got_error *err;
7716 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7717 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7718 af179be7 2023-04-14 stsp
7719 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7720 af179be7 2023-04-14 stsp if (err)
7721 af179be7 2023-04-14 stsp goto done;
7722 af179be7 2023-04-14 stsp
7723 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7724 af179be7 2023-04-14 stsp
7725 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7726 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7727 af179be7 2023-04-14 stsp if (err)
7728 af179be7 2023-04-14 stsp goto done;
7729 af179be7 2023-04-14 stsp done:
7730 af179be7 2023-04-14 stsp if (commit)
7731 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7732 af179be7 2023-04-14 stsp return err;
7733 af179be7 2023-04-14 stsp }
7734 af179be7 2023-04-14 stsp
7735 818c7501 2019-07-11 stsp const struct got_error *
7736 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7737 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7738 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7739 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7740 818c7501 2019-07-11 stsp {
7741 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7742 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7743 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7744 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7745 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7746 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7747 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7748 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7749 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7750 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7751 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7752 818c7501 2019-07-11 stsp
7753 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7754 af179be7 2023-04-14 stsp
7755 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7756 818c7501 2019-07-11 stsp if (err)
7757 818c7501 2019-07-11 stsp return err;
7758 af179be7 2023-04-14 stsp
7759 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7760 af179be7 2023-04-14 stsp if (err)
7761 af179be7 2023-04-14 stsp goto done;
7762 af179be7 2023-04-14 stsp
7763 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7764 af179be7 2023-04-14 stsp if (err)
7765 af179be7 2023-04-14 stsp goto done;
7766 af179be7 2023-04-14 stsp
7767 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7768 af179be7 2023-04-14 stsp if (err)
7769 af179be7 2023-04-14 stsp goto done;
7770 a44927cc 2022-04-07 stsp
7771 af179be7 2023-04-14 stsp /*
7772 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7773 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7774 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7775 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7776 af179be7 2023-04-14 stsp */
7777 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7778 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7779 af179be7 2023-04-14 stsp if (err)
7780 af179be7 2023-04-14 stsp goto done;
7781 af179be7 2023-04-14 stsp
7782 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7783 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7784 818c7501 2019-07-11 stsp if (err)
7785 818c7501 2019-07-11 stsp goto done;
7786 818c7501 2019-07-11 stsp
7787 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7788 818c7501 2019-07-11 stsp if (err)
7789 818c7501 2019-07-11 stsp goto done;
7790 818c7501 2019-07-11 stsp
7791 818c7501 2019-07-11 stsp /*
7792 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7793 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7794 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7795 818c7501 2019-07-11 stsp */
7796 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7797 818c7501 2019-07-11 stsp if (err)
7798 818c7501 2019-07-11 stsp goto done;
7799 818c7501 2019-07-11 stsp
7800 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7801 1b093d84 2023-04-12 stsp if (err)
7802 1b093d84 2023-04-12 stsp goto done;
7803 1b093d84 2023-04-12 stsp
7804 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7805 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7806 818c7501 2019-07-11 stsp if (err)
7807 818c7501 2019-07-11 stsp goto done;
7808 818c7501 2019-07-11 stsp
7809 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7810 a44927cc 2022-04-07 stsp worktree->path_prefix);
7811 ca355955 2019-07-12 stsp if (err)
7812 ca355955 2019-07-12 stsp goto done;
7813 ca355955 2019-07-12 stsp
7814 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7815 ca355955 2019-07-12 stsp if (err)
7816 ca355955 2019-07-12 stsp goto done;
7817 ca355955 2019-07-12 stsp
7818 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7819 818c7501 2019-07-11 stsp if (err)
7820 818c7501 2019-07-11 stsp goto done;
7821 818c7501 2019-07-11 stsp
7822 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7823 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7824 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7825 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7826 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7827 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7828 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7829 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7830 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7831 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7832 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7833 55bd499d 2019-07-12 stsp if (err)
7834 1f1abb7e 2019-08-08 stsp goto sync;
7835 55bd499d 2019-07-12 stsp
7836 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7837 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7838 ca355955 2019-07-12 stsp sync:
7839 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7840 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7841 ca355955 2019-07-12 stsp err = sync_err;
7842 818c7501 2019-07-11 stsp done:
7843 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7844 818c7501 2019-07-11 stsp got_ref_close(resolved);
7845 a3a2faf2 2019-07-12 stsp free(tree_id);
7846 818c7501 2019-07-11 stsp free(commit_id);
7847 af179be7 2023-04-14 stsp free(merged_commit_id);
7848 a44927cc 2022-04-07 stsp if (commit)
7849 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7850 0ebf8283 2019-07-24 stsp if (fileindex)
7851 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7852 0ebf8283 2019-07-24 stsp free(fileindex_path);
7853 af179be7 2023-04-14 stsp free(commit_ref_name);
7854 af179be7 2023-04-14 stsp if (commit_ref)
7855 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7856 0ebf8283 2019-07-24 stsp
7857 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7858 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7859 0ebf8283 2019-07-24 stsp err = unlockerr;
7860 0ebf8283 2019-07-24 stsp return err;
7861 0ebf8283 2019-07-24 stsp }
7862 0ebf8283 2019-07-24 stsp
7863 0ebf8283 2019-07-24 stsp const struct got_error *
7864 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7865 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7866 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7867 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7868 0ebf8283 2019-07-24 stsp {
7869 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7870 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7871 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7872 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7873 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7874 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7875 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7876 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7877 0ebf8283 2019-07-24 stsp
7878 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7879 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7880 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7881 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7882 0ebf8283 2019-07-24 stsp
7883 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7884 0ebf8283 2019-07-24 stsp if (err)
7885 0ebf8283 2019-07-24 stsp return err;
7886 0ebf8283 2019-07-24 stsp
7887 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7888 0ebf8283 2019-07-24 stsp if (err)
7889 0ebf8283 2019-07-24 stsp goto done;
7890 0ebf8283 2019-07-24 stsp
7891 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7892 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7893 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7894 0ebf8283 2019-07-24 stsp &ok_arg);
7895 0ebf8283 2019-07-24 stsp if (err)
7896 0ebf8283 2019-07-24 stsp goto done;
7897 0ebf8283 2019-07-24 stsp
7898 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7899 0ebf8283 2019-07-24 stsp if (err)
7900 0ebf8283 2019-07-24 stsp goto done;
7901 0ebf8283 2019-07-24 stsp
7902 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7903 0ebf8283 2019-07-24 stsp if (err)
7904 0ebf8283 2019-07-24 stsp goto done;
7905 0ebf8283 2019-07-24 stsp
7906 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7907 0ebf8283 2019-07-24 stsp worktree);
7908 0ebf8283 2019-07-24 stsp if (err)
7909 0ebf8283 2019-07-24 stsp goto done;
7910 0ebf8283 2019-07-24 stsp
7911 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7912 0ebf8283 2019-07-24 stsp 0);
7913 0ebf8283 2019-07-24 stsp if (err)
7914 0ebf8283 2019-07-24 stsp goto done;
7915 0ebf8283 2019-07-24 stsp
7916 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7917 0ebf8283 2019-07-24 stsp if (err)
7918 0ebf8283 2019-07-24 stsp goto done;
7919 0ebf8283 2019-07-24 stsp
7920 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7921 0ebf8283 2019-07-24 stsp if (err)
7922 0ebf8283 2019-07-24 stsp goto done;
7923 0ebf8283 2019-07-24 stsp
7924 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7925 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7926 0ebf8283 2019-07-24 stsp if (err)
7927 0ebf8283 2019-07-24 stsp goto done;
7928 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7929 0ebf8283 2019-07-24 stsp if (err)
7930 0ebf8283 2019-07-24 stsp goto done;
7931 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7932 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7933 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7934 0ebf8283 2019-07-24 stsp goto done;
7935 0ebf8283 2019-07-24 stsp }
7936 0ebf8283 2019-07-24 stsp
7937 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7938 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7939 0ebf8283 2019-07-24 stsp if (err)
7940 0ebf8283 2019-07-24 stsp goto done;
7941 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7942 0ebf8283 2019-07-24 stsp if (err)
7943 0ebf8283 2019-07-24 stsp goto done;
7944 0ebf8283 2019-07-24 stsp
7945 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7946 0ebf8283 2019-07-24 stsp if (err)
7947 0ebf8283 2019-07-24 stsp goto done;
7948 0ebf8283 2019-07-24 stsp done:
7949 0ebf8283 2019-07-24 stsp free(fileindex_path);
7950 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7951 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7952 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7953 0ebf8283 2019-07-24 stsp if (wt_branch)
7954 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7955 0ebf8283 2019-07-24 stsp if (err) {
7956 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7957 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7958 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7959 0ebf8283 2019-07-24 stsp }
7960 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7961 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7962 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7963 0ebf8283 2019-07-24 stsp }
7964 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7965 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7966 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7967 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7968 3e3a69f1 2019-07-25 stsp }
7969 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7970 0ebf8283 2019-07-24 stsp }
7971 0ebf8283 2019-07-24 stsp return err;
7972 0ebf8283 2019-07-24 stsp }
7973 0ebf8283 2019-07-24 stsp
7974 0ebf8283 2019-07-24 stsp const struct got_error *
7975 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7976 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7977 0ebf8283 2019-07-24 stsp {
7978 3e3a69f1 2019-07-25 stsp if (fileindex)
7979 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7980 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7981 0ebf8283 2019-07-24 stsp }
7982 0ebf8283 2019-07-24 stsp
7983 0ebf8283 2019-07-24 stsp const struct got_error *
7984 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7985 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7986 0ebf8283 2019-07-24 stsp {
7987 0ebf8283 2019-07-24 stsp const struct got_error *err;
7988 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7989 0ebf8283 2019-07-24 stsp
7990 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7991 0ebf8283 2019-07-24 stsp if (err)
7992 0ebf8283 2019-07-24 stsp return err;
7993 0ebf8283 2019-07-24 stsp
7994 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7995 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7996 0ebf8283 2019-07-24 stsp return NULL;
7997 0ebf8283 2019-07-24 stsp }
7998 0ebf8283 2019-07-24 stsp
7999 0ebf8283 2019-07-24 stsp const struct got_error *
8000 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
8001 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
8002 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
8003 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8004 0ebf8283 2019-07-24 stsp {
8005 0ebf8283 2019-07-24 stsp const struct got_error *err;
8006 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
8007 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
8008 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
8009 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
8010 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
8011 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
8012 0ebf8283 2019-07-24 stsp
8013 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8014 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8015 a854b8c9 2024-03-28 stsp *branch_ref = NULL;
8016 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8017 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8018 0ebf8283 2019-07-24 stsp
8019 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
8020 3e3a69f1 2019-07-25 stsp if (err)
8021 3e3a69f1 2019-07-25 stsp return err;
8022 3e3a69f1 2019-07-25 stsp
8023 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8024 3e3a69f1 2019-07-25 stsp if (err)
8025 f032f1f7 2019-08-04 stsp goto done;
8026 f032f1f7 2019-08-04 stsp
8027 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8028 f032f1f7 2019-08-04 stsp &have_staged_files);
8029 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
8030 f032f1f7 2019-08-04 stsp goto done;
8031 f032f1f7 2019-08-04 stsp if (have_staged_files) {
8032 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8033 3e3a69f1 2019-07-25 stsp goto done;
8034 f032f1f7 2019-08-04 stsp }
8035 3e3a69f1 2019-07-25 stsp
8036 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8037 0ebf8283 2019-07-24 stsp if (err)
8038 f032f1f7 2019-08-04 stsp goto done;
8039 0ebf8283 2019-07-24 stsp
8040 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8041 0ebf8283 2019-07-24 stsp if (err)
8042 0ebf8283 2019-07-24 stsp goto done;
8043 0ebf8283 2019-07-24 stsp
8044 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8045 0ebf8283 2019-07-24 stsp if (err)
8046 0ebf8283 2019-07-24 stsp goto done;
8047 0ebf8283 2019-07-24 stsp
8048 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8049 0ebf8283 2019-07-24 stsp worktree);
8050 0ebf8283 2019-07-24 stsp if (err)
8051 0ebf8283 2019-07-24 stsp goto done;
8052 0ebf8283 2019-07-24 stsp
8053 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
8054 0ebf8283 2019-07-24 stsp if (err)
8055 0ebf8283 2019-07-24 stsp goto done;
8056 0ebf8283 2019-07-24 stsp
8057 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8058 0ebf8283 2019-07-24 stsp if (err)
8059 0ebf8283 2019-07-24 stsp goto done;
8060 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
8061 0ebf8283 2019-07-24 stsp if (err)
8062 0ebf8283 2019-07-24 stsp goto done;
8063 0ebf8283 2019-07-24 stsp
8064 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8065 0ebf8283 2019-07-24 stsp if (err)
8066 0ebf8283 2019-07-24 stsp goto done;
8067 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8068 0ebf8283 2019-07-24 stsp if (err)
8069 0ebf8283 2019-07-24 stsp goto done;
8070 0ebf8283 2019-07-24 stsp
8071 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8072 0ebf8283 2019-07-24 stsp if (err)
8073 0ebf8283 2019-07-24 stsp goto done;
8074 0ebf8283 2019-07-24 stsp done:
8075 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8076 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8077 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8078 0ebf8283 2019-07-24 stsp if (commit_ref)
8079 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8080 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8081 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8082 0ebf8283 2019-07-24 stsp if (err) {
8083 0ebf8283 2019-07-24 stsp free(*commit_id);
8084 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8085 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8086 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8087 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8088 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8089 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8090 0ebf8283 2019-07-24 stsp }
8091 a854b8c9 2024-03-28 stsp if (*branch_ref) {
8092 a854b8c9 2024-03-28 stsp got_ref_close(*branch_ref);
8093 a854b8c9 2024-03-28 stsp *branch_ref = NULL;
8094 a854b8c9 2024-03-28 stsp }
8095 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8096 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8097 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8098 3e3a69f1 2019-07-25 stsp }
8099 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8100 f4ab0e57 2024-03-28 stsp }
8101 f4ab0e57 2024-03-28 stsp return err;
8102 f4ab0e57 2024-03-28 stsp }
8103 f4ab0e57 2024-03-28 stsp
8104 f4ab0e57 2024-03-28 stsp const struct got_error *
8105 f4ab0e57 2024-03-28 stsp got_worktree_histedit_info(char **branch_name,
8106 f4ab0e57 2024-03-28 stsp struct got_worktree *worktree, struct got_repository *repo)
8107 f4ab0e57 2024-03-28 stsp {
8108 f4ab0e57 2024-03-28 stsp const struct got_error *err;
8109 f4ab0e57 2024-03-28 stsp struct got_reference *branch_ref = NULL;
8110 f4ab0e57 2024-03-28 stsp char *branch_ref_name = NULL;
8111 f4ab0e57 2024-03-28 stsp
8112 f4ab0e57 2024-03-28 stsp *branch_name = NULL;
8113 f4ab0e57 2024-03-28 stsp
8114 f4ab0e57 2024-03-28 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8115 f4ab0e57 2024-03-28 stsp if (err)
8116 f4ab0e57 2024-03-28 stsp goto done;
8117 f4ab0e57 2024-03-28 stsp
8118 f4ab0e57 2024-03-28 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
8119 f4ab0e57 2024-03-28 stsp if (err)
8120 f4ab0e57 2024-03-28 stsp goto done;
8121 f4ab0e57 2024-03-28 stsp
8122 f4ab0e57 2024-03-28 stsp if (!got_ref_is_symbolic(branch_ref)) {
8123 f4ab0e57 2024-03-28 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8124 f4ab0e57 2024-03-28 stsp "%s is not a symbolic reference",
8125 f4ab0e57 2024-03-28 stsp got_ref_get_name(branch_ref));
8126 f4ab0e57 2024-03-28 stsp goto done;
8127 f4ab0e57 2024-03-28 stsp }
8128 f4ab0e57 2024-03-28 stsp
8129 f4ab0e57 2024-03-28 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8130 f4ab0e57 2024-03-28 stsp if (*branch_name == NULL) {
8131 f4ab0e57 2024-03-28 stsp err = got_error_from_errno("strdup");
8132 f4ab0e57 2024-03-28 stsp goto done;
8133 0ebf8283 2019-07-24 stsp }
8134 f4ab0e57 2024-03-28 stsp done:
8135 f4ab0e57 2024-03-28 stsp free(branch_ref_name);
8136 f4ab0e57 2024-03-28 stsp if (branch_ref)
8137 f4ab0e57 2024-03-28 stsp got_ref_close(branch_ref);
8138 f4ab0e57 2024-03-28 stsp if (err) {
8139 f4ab0e57 2024-03-28 stsp free(*branch_name);
8140 f4ab0e57 2024-03-28 stsp *branch_name = NULL;
8141 f4ab0e57 2024-03-28 stsp }
8142 0ebf8283 2019-07-24 stsp return err;
8143 0ebf8283 2019-07-24 stsp }
8144 0ebf8283 2019-07-24 stsp
8145 0ebf8283 2019-07-24 stsp static const struct got_error *
8146 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8147 0ebf8283 2019-07-24 stsp {
8148 0ebf8283 2019-07-24 stsp const struct got_error *err;
8149 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8150 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8151 0ebf8283 2019-07-24 stsp
8152 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8153 0ebf8283 2019-07-24 stsp if (err)
8154 0ebf8283 2019-07-24 stsp goto done;
8155 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8156 0ebf8283 2019-07-24 stsp if (err)
8157 0ebf8283 2019-07-24 stsp goto done;
8158 0ebf8283 2019-07-24 stsp
8159 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8160 0ebf8283 2019-07-24 stsp worktree);
8161 0ebf8283 2019-07-24 stsp if (err)
8162 0ebf8283 2019-07-24 stsp goto done;
8163 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8164 0ebf8283 2019-07-24 stsp if (err)
8165 0ebf8283 2019-07-24 stsp goto done;
8166 0ebf8283 2019-07-24 stsp
8167 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8168 0ebf8283 2019-07-24 stsp if (err)
8169 0ebf8283 2019-07-24 stsp goto done;
8170 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8171 0ebf8283 2019-07-24 stsp if (err)
8172 0ebf8283 2019-07-24 stsp goto done;
8173 0ebf8283 2019-07-24 stsp
8174 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8175 0ebf8283 2019-07-24 stsp if (err)
8176 0ebf8283 2019-07-24 stsp goto done;
8177 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8178 0ebf8283 2019-07-24 stsp if (err)
8179 0ebf8283 2019-07-24 stsp goto done;
8180 0ebf8283 2019-07-24 stsp done:
8181 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8182 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8183 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8184 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8185 0ebf8283 2019-07-24 stsp return err;
8186 0ebf8283 2019-07-24 stsp }
8187 0ebf8283 2019-07-24 stsp
8188 0ebf8283 2019-07-24 stsp const struct got_error *
8189 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8190 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8191 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8192 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8193 0ebf8283 2019-07-24 stsp {
8194 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8195 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8196 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8197 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8198 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8199 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8200 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8201 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8202 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8203 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8204 0ebf8283 2019-07-24 stsp
8205 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8206 af179be7 2023-04-14 stsp
8207 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8208 0ebf8283 2019-07-24 stsp if (err)
8209 0ebf8283 2019-07-24 stsp return err;
8210 a44927cc 2022-04-07 stsp
8211 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8212 af179be7 2023-04-14 stsp if (err)
8213 af179be7 2023-04-14 stsp goto done;
8214 af179be7 2023-04-14 stsp
8215 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8216 af179be7 2023-04-14 stsp if (err) {
8217 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
8218 af179be7 2023-04-14 stsp goto done;
8219 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
8220 af179be7 2023-04-14 stsp commit_ref = NULL;
8221 af179be7 2023-04-14 stsp }
8222 af179be7 2023-04-14 stsp
8223 af179be7 2023-04-14 stsp if (commit_ref) {
8224 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8225 af179be7 2023-04-14 stsp if (err)
8226 af179be7 2023-04-14 stsp goto done;
8227 af179be7 2023-04-14 stsp
8228 af179be7 2023-04-14 stsp /*
8229 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8230 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8231 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
8232 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
8233 af179be7 2023-04-14 stsp */
8234 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8235 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8236 af179be7 2023-04-14 stsp if (err)
8237 af179be7 2023-04-14 stsp goto done;
8238 af179be7 2023-04-14 stsp }
8239 af179be7 2023-04-14 stsp
8240 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8241 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8242 0ebf8283 2019-07-24 stsp if (err)
8243 0ebf8283 2019-07-24 stsp goto done;
8244 0ebf8283 2019-07-24 stsp
8245 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8246 0ebf8283 2019-07-24 stsp if (err)
8247 0ebf8283 2019-07-24 stsp goto done;
8248 0ebf8283 2019-07-24 stsp
8249 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8250 0ebf8283 2019-07-24 stsp if (err)
8251 0ebf8283 2019-07-24 stsp goto done;
8252 0ebf8283 2019-07-24 stsp
8253 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
8254 1b093d84 2023-04-12 stsp worktree->base_commit_id);
8255 1b093d84 2023-04-12 stsp if (err)
8256 1b093d84 2023-04-12 stsp goto done;
8257 1b093d84 2023-04-12 stsp
8258 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8259 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8260 0ebf8283 2019-07-24 stsp if (err)
8261 0ebf8283 2019-07-24 stsp goto done;
8262 0ebf8283 2019-07-24 stsp
8263 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8264 0ebf8283 2019-07-24 stsp if (err)
8265 0ebf8283 2019-07-24 stsp goto done;
8266 0ebf8283 2019-07-24 stsp
8267 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8268 0ebf8283 2019-07-24 stsp if (err)
8269 0ebf8283 2019-07-24 stsp goto done;
8270 0ebf8283 2019-07-24 stsp
8271 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8272 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8273 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8274 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8275 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8276 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8277 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8278 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8279 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8280 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8281 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8282 0ebf8283 2019-07-24 stsp if (err)
8283 1f1abb7e 2019-08-08 stsp goto sync;
8284 0ebf8283 2019-07-24 stsp
8285 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8286 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8287 0ebf8283 2019-07-24 stsp sync:
8288 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8289 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8290 0ebf8283 2019-07-24 stsp err = sync_err;
8291 0ebf8283 2019-07-24 stsp done:
8292 af179be7 2023-04-14 stsp if (resolved)
8293 af179be7 2023-04-14 stsp got_ref_close(resolved);
8294 af179be7 2023-04-14 stsp if (commit_ref)
8295 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8296 af179be7 2023-04-14 stsp free(merged_commit_id);
8297 0ebf8283 2019-07-24 stsp free(tree_id);
8298 818c7501 2019-07-11 stsp free(fileindex_path);
8299 af179be7 2023-04-14 stsp free(commit_ref_name);
8300 818c7501 2019-07-11 stsp
8301 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8302 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8303 818c7501 2019-07-11 stsp err = unlockerr;
8304 818c7501 2019-07-11 stsp return err;
8305 818c7501 2019-07-11 stsp }
8306 0ebf8283 2019-07-24 stsp
8307 0ebf8283 2019-07-24 stsp const struct got_error *
8308 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8309 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8310 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8311 0ebf8283 2019-07-24 stsp {
8312 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8313 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8314 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8315 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8316 0ebf8283 2019-07-24 stsp
8317 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8318 0ebf8283 2019-07-24 stsp if (err)
8319 0ebf8283 2019-07-24 stsp return err;
8320 0ebf8283 2019-07-24 stsp
8321 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8322 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8323 e600f124 2021-03-21 stsp if (err)
8324 e600f124 2021-03-21 stsp goto done;
8325 e600f124 2021-03-21 stsp
8326 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8327 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8328 0ebf8283 2019-07-24 stsp if (err)
8329 0ebf8283 2019-07-24 stsp goto done;
8330 0ebf8283 2019-07-24 stsp
8331 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8332 0ebf8283 2019-07-24 stsp if (err)
8333 0ebf8283 2019-07-24 stsp goto done;
8334 0ebf8283 2019-07-24 stsp
8335 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8336 0ebf8283 2019-07-24 stsp if (err)
8337 0ebf8283 2019-07-24 stsp goto done;
8338 0ebf8283 2019-07-24 stsp
8339 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8340 0ebf8283 2019-07-24 stsp if (err)
8341 0ebf8283 2019-07-24 stsp goto done;
8342 0ebf8283 2019-07-24 stsp
8343 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8344 a615e0e7 2020-12-16 stsp if (err)
8345 a615e0e7 2020-12-16 stsp goto done;
8346 a615e0e7 2020-12-16 stsp
8347 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8348 a615e0e7 2020-12-16 stsp if (err)
8349 a615e0e7 2020-12-16 stsp goto done;
8350 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8351 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8352 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8353 a615e0e7 2020-12-16 stsp err = sync_err;
8354 0ebf8283 2019-07-24 stsp done:
8355 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8356 a615e0e7 2020-12-16 stsp free(fileindex_path);
8357 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8358 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8359 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8360 0ebf8283 2019-07-24 stsp err = unlockerr;
8361 0ebf8283 2019-07-24 stsp return err;
8362 0ebf8283 2019-07-24 stsp }
8363 0ebf8283 2019-07-24 stsp
8364 0ebf8283 2019-07-24 stsp const struct got_error *
8365 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8366 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8367 0ebf8283 2019-07-24 stsp {
8368 0ebf8283 2019-07-24 stsp const struct got_error *err;
8369 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8370 0ebf8283 2019-07-24 stsp
8371 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8372 0ebf8283 2019-07-24 stsp if (err)
8373 0ebf8283 2019-07-24 stsp return err;
8374 0ebf8283 2019-07-24 stsp
8375 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8376 0ebf8283 2019-07-24 stsp if (err)
8377 0ebf8283 2019-07-24 stsp goto done;
8378 0ebf8283 2019-07-24 stsp
8379 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8380 0ebf8283 2019-07-24 stsp done:
8381 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8382 2822a352 2019-10-15 stsp return err;
8383 2822a352 2019-10-15 stsp }
8384 2822a352 2019-10-15 stsp
8385 2822a352 2019-10-15 stsp const struct got_error *
8386 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8387 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8388 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8389 2822a352 2019-10-15 stsp struct got_repository *repo)
8390 2822a352 2019-10-15 stsp {
8391 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8392 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8393 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8394 2822a352 2019-10-15 stsp
8395 2822a352 2019-10-15 stsp *fileindex = NULL;
8396 2822a352 2019-10-15 stsp *branch_ref = NULL;
8397 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8398 2822a352 2019-10-15 stsp
8399 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8400 2822a352 2019-10-15 stsp if (err)
8401 2822a352 2019-10-15 stsp return err;
8402 2822a352 2019-10-15 stsp
8403 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8404 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8405 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8406 2822a352 2019-10-15 stsp "update -b or different branch name required");
8407 2822a352 2019-10-15 stsp goto done;
8408 2822a352 2019-10-15 stsp }
8409 2822a352 2019-10-15 stsp
8410 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8411 2822a352 2019-10-15 stsp if (err)
8412 2822a352 2019-10-15 stsp goto done;
8413 2822a352 2019-10-15 stsp
8414 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8415 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8416 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8417 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8418 2822a352 2019-10-15 stsp &ok_arg);
8419 2822a352 2019-10-15 stsp if (err)
8420 2822a352 2019-10-15 stsp goto done;
8421 2822a352 2019-10-15 stsp
8422 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8423 2822a352 2019-10-15 stsp if (err)
8424 2822a352 2019-10-15 stsp goto done;
8425 2822a352 2019-10-15 stsp
8426 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8427 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8428 2822a352 2019-10-15 stsp done:
8429 2822a352 2019-10-15 stsp if (err) {
8430 2822a352 2019-10-15 stsp if (*branch_ref) {
8431 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8432 2822a352 2019-10-15 stsp *branch_ref = NULL;
8433 2822a352 2019-10-15 stsp }
8434 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8435 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8436 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8437 2822a352 2019-10-15 stsp }
8438 2822a352 2019-10-15 stsp if (*fileindex) {
8439 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8440 2822a352 2019-10-15 stsp *fileindex = NULL;
8441 2822a352 2019-10-15 stsp }
8442 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8443 2822a352 2019-10-15 stsp }
8444 0cb83759 2019-08-03 stsp return err;
8445 0cb83759 2019-08-03 stsp }
8446 0cb83759 2019-08-03 stsp
8447 2822a352 2019-10-15 stsp const struct got_error *
8448 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8449 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8450 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8451 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8452 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8453 2822a352 2019-10-15 stsp {
8454 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8455 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8456 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8457 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8458 2822a352 2019-10-15 stsp
8459 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8460 2822a352 2019-10-15 stsp if (err)
8461 2822a352 2019-10-15 stsp goto done;
8462 2822a352 2019-10-15 stsp
8463 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8464 2822a352 2019-10-15 stsp if (err)
8465 2822a352 2019-10-15 stsp goto done;
8466 2822a352 2019-10-15 stsp
8467 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8468 a44927cc 2022-04-07 stsp if (err)
8469 a44927cc 2022-04-07 stsp goto done;
8470 a44927cc 2022-04-07 stsp
8471 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8472 2822a352 2019-10-15 stsp worktree->path_prefix);
8473 2822a352 2019-10-15 stsp if (err)
8474 2822a352 2019-10-15 stsp goto done;
8475 2822a352 2019-10-15 stsp
8476 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8477 2822a352 2019-10-15 stsp if (err)
8478 2822a352 2019-10-15 stsp goto done;
8479 2822a352 2019-10-15 stsp
8480 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8481 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8482 2822a352 2019-10-15 stsp if (err)
8483 2822a352 2019-10-15 stsp goto sync;
8484 2822a352 2019-10-15 stsp
8485 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8486 2822a352 2019-10-15 stsp if (err)
8487 2822a352 2019-10-15 stsp goto sync;
8488 2822a352 2019-10-15 stsp
8489 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8490 6e210706 2021-01-22 stsp if (err)
8491 6e210706 2021-01-22 stsp goto sync;
8492 6e210706 2021-01-22 stsp
8493 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8494 2822a352 2019-10-15 stsp sync:
8495 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8496 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8497 2822a352 2019-10-15 stsp err = sync_err;
8498 2822a352 2019-10-15 stsp
8499 2822a352 2019-10-15 stsp done:
8500 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8501 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8502 2822a352 2019-10-15 stsp err = unlockerr;
8503 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8504 2822a352 2019-10-15 stsp
8505 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8506 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8507 2822a352 2019-10-15 stsp err = unlockerr;
8508 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8509 2822a352 2019-10-15 stsp
8510 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8511 2822a352 2019-10-15 stsp free(fileindex_path);
8512 2822a352 2019-10-15 stsp free(tree_id);
8513 a44927cc 2022-04-07 stsp if (commit)
8514 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8515 2822a352 2019-10-15 stsp
8516 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8517 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8518 2822a352 2019-10-15 stsp err = unlockerr;
8519 2822a352 2019-10-15 stsp return err;
8520 2822a352 2019-10-15 stsp }
8521 2822a352 2019-10-15 stsp
8522 2822a352 2019-10-15 stsp const struct got_error *
8523 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8524 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8525 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8526 2822a352 2019-10-15 stsp {
8527 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8528 8b692cd0 2019-10-21 stsp
8529 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8530 8b692cd0 2019-10-21 stsp
8531 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8532 8b692cd0 2019-10-21 stsp
8533 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8534 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8535 8b692cd0 2019-10-21 stsp err = unlockerr;
8536 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8537 8b692cd0 2019-10-21 stsp
8538 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8539 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8540 8b692cd0 2019-10-21 stsp err = unlockerr;
8541 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8542 f259c4c1 2021-09-24 stsp
8543 f259c4c1 2021-09-24 stsp return err;
8544 f259c4c1 2021-09-24 stsp }
8545 f259c4c1 2021-09-24 stsp
8546 f259c4c1 2021-09-24 stsp const struct got_error *
8547 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8548 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8549 f259c4c1 2021-09-24 stsp {
8550 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8551 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8552 f259c4c1 2021-09-24 stsp
8553 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8554 f259c4c1 2021-09-24 stsp if (err)
8555 f259c4c1 2021-09-24 stsp goto done;
8556 8b692cd0 2019-10-21 stsp
8557 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8558 f259c4c1 2021-09-24 stsp
8559 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8560 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8561 f259c4c1 2021-09-24 stsp err = sync_err;
8562 f259c4c1 2021-09-24 stsp done:
8563 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8564 f259c4c1 2021-09-24 stsp free(fileindex_path);
8565 8b692cd0 2019-10-21 stsp return err;
8566 2822a352 2019-10-15 stsp }
8567 2822a352 2019-10-15 stsp
8568 f259c4c1 2021-09-24 stsp static const struct got_error *
8569 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8570 f259c4c1 2021-09-24 stsp {
8571 f259c4c1 2021-09-24 stsp const struct got_error *err;
8572 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8573 f259c4c1 2021-09-24 stsp
8574 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8575 f259c4c1 2021-09-24 stsp if (err)
8576 f259c4c1 2021-09-24 stsp goto done;
8577 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, 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 = get_merge_commit_ref_name(&commit_refname, worktree);
8582 f259c4c1 2021-09-24 stsp if (err)
8583 f259c4c1 2021-09-24 stsp goto done;
8584 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, 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 return err;
8592 f259c4c1 2021-09-24 stsp }
8593 f259c4c1 2021-09-24 stsp
8594 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8595 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8596 f259c4c1 2021-09-24 stsp const char *branch_name;
8597 f259c4c1 2021-09-24 stsp };
8598 f259c4c1 2021-09-24 stsp
8599 f259c4c1 2021-09-24 stsp static const struct got_error *
8600 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8601 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8602 f259c4c1 2021-09-24 stsp {
8603 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8604 f259c4c1 2021-09-24 stsp
8605 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8606 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8607 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8608 f259c4c1 2021-09-24 stsp
8609 f259c4c1 2021-09-24 stsp return NULL;
8610 f259c4c1 2021-09-24 stsp }
8611 f259c4c1 2021-09-24 stsp
8612 f259c4c1 2021-09-24 stsp
8613 f259c4c1 2021-09-24 stsp const struct got_error *
8614 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8615 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8616 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8617 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8618 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8619 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8620 f259c4c1 2021-09-24 stsp {
8621 f259c4c1 2021-09-24 stsp const struct got_error *err;
8622 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8623 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
8624 f259c4c1 2021-09-24 stsp
8625 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8626 f259c4c1 2021-09-24 stsp if (err)
8627 f259c4c1 2021-09-24 stsp goto done;
8628 f259c4c1 2021-09-24 stsp
8629 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
8630 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
8631 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
8632 cdbfe5d2 2023-07-24 stsp
8633 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8634 e07c1782 2023-07-24 op &cma);
8635 f259c4c1 2021-09-24 stsp if (err)
8636 f259c4c1 2021-09-24 stsp goto done;
8637 f259c4c1 2021-09-24 stsp
8638 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8639 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8640 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8641 f259c4c1 2021-09-24 stsp done:
8642 f259c4c1 2021-09-24 stsp free(fileindex_path);
8643 f259c4c1 2021-09-24 stsp return err;
8644 f259c4c1 2021-09-24 stsp }
8645 f259c4c1 2021-09-24 stsp
8646 f259c4c1 2021-09-24 stsp const struct got_error *
8647 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8648 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8649 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8650 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8651 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8652 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8653 0ff8d236 2021-09-28 stsp
8654 f259c4c1 2021-09-24 stsp {
8655 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8656 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8657 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8658 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8659 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8660 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8661 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8662 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8663 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8664 f259c4c1 2021-09-24 stsp
8665 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8666 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8667 f259c4c1 2021-09-24 stsp
8668 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8669 f259c4c1 2021-09-24 stsp
8670 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8671 f259c4c1 2021-09-24 stsp if (err)
8672 f259c4c1 2021-09-24 stsp goto done;
8673 f259c4c1 2021-09-24 stsp
8674 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8675 f259c4c1 2021-09-24 stsp if (err)
8676 f259c4c1 2021-09-24 stsp goto done;
8677 f259c4c1 2021-09-24 stsp
8678 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8679 f259c4c1 2021-09-24 stsp if (err)
8680 f259c4c1 2021-09-24 stsp goto done;
8681 f259c4c1 2021-09-24 stsp
8682 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8683 f259c4c1 2021-09-24 stsp &have_staged_files);
8684 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8685 f259c4c1 2021-09-24 stsp goto done;
8686 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8687 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8688 f259c4c1 2021-09-24 stsp goto done;
8689 f259c4c1 2021-09-24 stsp }
8690 f259c4c1 2021-09-24 stsp
8691 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8692 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8693 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8694 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8695 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8696 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8697 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8698 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8699 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8700 f259c4c1 2021-09-24 stsp if (err)
8701 f259c4c1 2021-09-24 stsp goto done;
8702 f259c4c1 2021-09-24 stsp
8703 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8704 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8705 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8706 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8707 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8708 f259c4c1 2021-09-24 stsp if (err)
8709 f259c4c1 2021-09-24 stsp goto done;
8710 f259c4c1 2021-09-24 stsp
8711 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8712 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8713 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8714 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8715 f259c4c1 2021-09-24 stsp err = sync_err;
8716 f259c4c1 2021-09-24 stsp done:
8717 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8718 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8719 d8bacb93 2023-01-10 mark
8720 f259c4c1 2021-09-24 stsp free_commitable(ct);
8721 f259c4c1 2021-09-24 stsp }
8722 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8723 f259c4c1 2021-09-24 stsp free(fileindex_path);
8724 f259c4c1 2021-09-24 stsp return err;
8725 f259c4c1 2021-09-24 stsp }
8726 f259c4c1 2021-09-24 stsp
8727 f259c4c1 2021-09-24 stsp const struct got_error *
8728 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8729 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8730 f259c4c1 2021-09-24 stsp {
8731 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8732 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8733 f259c4c1 2021-09-24 stsp
8734 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
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 = get_fileindex_path(&fileindex_path, worktree);
8739 f259c4c1 2021-09-24 stsp if (err)
8740 f259c4c1 2021-09-24 stsp goto done;
8741 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8742 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8743 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8744 f259c4c1 2021-09-24 stsp err = sync_err;
8745 f259c4c1 2021-09-24 stsp done:
8746 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8747 f259c4c1 2021-09-24 stsp free(fileindex_path);
8748 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8749 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8750 f259c4c1 2021-09-24 stsp err = unlockerr;
8751 f259c4c1 2021-09-24 stsp return err;
8752 f259c4c1 2021-09-24 stsp }
8753 f259c4c1 2021-09-24 stsp
8754 f259c4c1 2021-09-24 stsp const struct got_error *
8755 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8756 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8757 f259c4c1 2021-09-24 stsp {
8758 f259c4c1 2021-09-24 stsp const struct got_error *err;
8759 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8760 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8761 f259c4c1 2021-09-24 stsp
8762 f259c4c1 2021-09-24 stsp *in_progress = 0;
8763 f259c4c1 2021-09-24 stsp
8764 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8765 f259c4c1 2021-09-24 stsp if (err)
8766 f259c4c1 2021-09-24 stsp return err;
8767 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8768 793fcac3 2021-09-24 stsp free(branch_refname);
8769 f259c4c1 2021-09-24 stsp if (err) {
8770 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8771 f259c4c1 2021-09-24 stsp return err;
8772 f259c4c1 2021-09-24 stsp } else
8773 f259c4c1 2021-09-24 stsp *in_progress = 1;
8774 f259c4c1 2021-09-24 stsp
8775 f259c4c1 2021-09-24 stsp return NULL;
8776 f259c4c1 2021-09-24 stsp }
8777 f259c4c1 2021-09-24 stsp
8778 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8779 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8780 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8781 f259c4c1 2021-09-24 stsp {
8782 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8783 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8784 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8785 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8786 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8787 f259c4c1 2021-09-24 stsp
8788 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8789 f259c4c1 2021-09-24 stsp
8790 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8791 f259c4c1 2021-09-24 stsp if (err)
8792 f259c4c1 2021-09-24 stsp return err;
8793 f259c4c1 2021-09-24 stsp
8794 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8795 f259c4c1 2021-09-24 stsp if (err)
8796 f259c4c1 2021-09-24 stsp goto done;
8797 f259c4c1 2021-09-24 stsp
8798 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8799 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8800 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8801 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8802 f259c4c1 2021-09-24 stsp &ok_arg);
8803 f259c4c1 2021-09-24 stsp if (err)
8804 f259c4c1 2021-09-24 stsp goto done;
8805 f259c4c1 2021-09-24 stsp
8806 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8807 f259c4c1 2021-09-24 stsp 0);
8808 f259c4c1 2021-09-24 stsp if (err)
8809 f259c4c1 2021-09-24 stsp goto done;
8810 f259c4c1 2021-09-24 stsp
8811 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8812 f259c4c1 2021-09-24 stsp if (err)
8813 f259c4c1 2021-09-24 stsp goto done;
8814 f259c4c1 2021-09-24 stsp
8815 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8816 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8817 f259c4c1 2021-09-24 stsp goto done;
8818 f259c4c1 2021-09-24 stsp }
8819 179f9db0 2023-06-20 falsifian
8820 179f9db0 2023-06-20 falsifian done:
8821 179f9db0 2023-06-20 falsifian free(fileindex_path);
8822 179f9db0 2023-06-20 falsifian if (wt_branch)
8823 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8824 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8825 179f9db0 2023-06-20 falsifian if (err) {
8826 179f9db0 2023-06-20 falsifian if (*fileindex) {
8827 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8828 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8829 179f9db0 2023-06-20 falsifian }
8830 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8831 179f9db0 2023-06-20 falsifian }
8832 179f9db0 2023-06-20 falsifian return err;
8833 179f9db0 2023-06-20 falsifian }
8834 f259c4c1 2021-09-24 stsp
8835 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8836 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8837 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8838 179f9db0 2023-06-20 falsifian {
8839 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8840 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8841 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8842 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8843 179f9db0 2023-06-20 falsifian
8844 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8845 179f9db0 2023-06-20 falsifian if (err)
8846 179f9db0 2023-06-20 falsifian return err;
8847 179f9db0 2023-06-20 falsifian
8848 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8849 179f9db0 2023-06-20 falsifian if (err)
8850 179f9db0 2023-06-20 falsifian return err;
8851 179f9db0 2023-06-20 falsifian
8852 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8853 f259c4c1 2021-09-24 stsp if (err)
8854 f259c4c1 2021-09-24 stsp goto done;
8855 f259c4c1 2021-09-24 stsp
8856 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8857 f259c4c1 2021-09-24 stsp if (err)
8858 f259c4c1 2021-09-24 stsp goto done;
8859 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8860 f259c4c1 2021-09-24 stsp if (err)
8861 f259c4c1 2021-09-24 stsp goto done;
8862 f259c4c1 2021-09-24 stsp
8863 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8864 f259c4c1 2021-09-24 stsp if (err)
8865 f259c4c1 2021-09-24 stsp goto done;
8866 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8867 f259c4c1 2021-09-24 stsp if (err)
8868 f259c4c1 2021-09-24 stsp goto done;
8869 f259c4c1 2021-09-24 stsp
8870 f259c4c1 2021-09-24 stsp done:
8871 f259c4c1 2021-09-24 stsp free(branch_refname);
8872 f259c4c1 2021-09-24 stsp free(commit_refname);
8873 f259c4c1 2021-09-24 stsp if (branch_ref)
8874 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8875 f259c4c1 2021-09-24 stsp if (commit_ref)
8876 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8877 179f9db0 2023-06-20 falsifian free(branch_tip);
8878 f259c4c1 2021-09-24 stsp return err;
8879 f259c4c1 2021-09-24 stsp }
8880 f259c4c1 2021-09-24 stsp
8881 f259c4c1 2021-09-24 stsp const struct got_error *
8882 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8883 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8884 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8885 f259c4c1 2021-09-24 stsp {
8886 f259c4c1 2021-09-24 stsp const struct got_error *err;
8887 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8888 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8889 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8890 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8891 f259c4c1 2021-09-24 stsp
8892 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8893 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8894 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8895 f259c4c1 2021-09-24 stsp
8896 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8897 f259c4c1 2021-09-24 stsp if (err)
8898 f259c4c1 2021-09-24 stsp return err;
8899 f259c4c1 2021-09-24 stsp
8900 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8901 f259c4c1 2021-09-24 stsp if (err)
8902 f259c4c1 2021-09-24 stsp goto done;
8903 f259c4c1 2021-09-24 stsp
8904 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8905 f259c4c1 2021-09-24 stsp &have_staged_files);
8906 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8907 f259c4c1 2021-09-24 stsp goto done;
8908 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8909 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8910 f259c4c1 2021-09-24 stsp goto done;
8911 f259c4c1 2021-09-24 stsp }
8912 f259c4c1 2021-09-24 stsp
8913 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8914 f259c4c1 2021-09-24 stsp if (err)
8915 f259c4c1 2021-09-24 stsp goto done;
8916 f259c4c1 2021-09-24 stsp
8917 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8918 f259c4c1 2021-09-24 stsp if (err)
8919 f259c4c1 2021-09-24 stsp goto done;
8920 f259c4c1 2021-09-24 stsp
8921 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8922 f259c4c1 2021-09-24 stsp if (err)
8923 f259c4c1 2021-09-24 stsp goto done;
8924 f259c4c1 2021-09-24 stsp
8925 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8926 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8927 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8928 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8929 f259c4c1 2021-09-24 stsp goto done;
8930 f259c4c1 2021-09-24 stsp }
8931 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8932 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8933 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8934 f259c4c1 2021-09-24 stsp goto done;
8935 f259c4c1 2021-09-24 stsp }
8936 f259c4c1 2021-09-24 stsp
8937 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8938 f259c4c1 2021-09-24 stsp if (err)
8939 f259c4c1 2021-09-24 stsp goto done;
8940 f259c4c1 2021-09-24 stsp
8941 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8942 f259c4c1 2021-09-24 stsp if (err)
8943 f259c4c1 2021-09-24 stsp goto done;
8944 f259c4c1 2021-09-24 stsp done:
8945 f259c4c1 2021-09-24 stsp free(commit_refname);
8946 f259c4c1 2021-09-24 stsp free(branch_refname);
8947 f259c4c1 2021-09-24 stsp free(fileindex_path);
8948 f259c4c1 2021-09-24 stsp if (commit_ref)
8949 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8950 f259c4c1 2021-09-24 stsp if (branch_ref)
8951 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8952 f259c4c1 2021-09-24 stsp if (err) {
8953 f259c4c1 2021-09-24 stsp if (*branch_name) {
8954 f259c4c1 2021-09-24 stsp free(*branch_name);
8955 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8956 f259c4c1 2021-09-24 stsp }
8957 f259c4c1 2021-09-24 stsp free(*branch_tip);
8958 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8959 f259c4c1 2021-09-24 stsp if (*fileindex) {
8960 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8961 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8962 f259c4c1 2021-09-24 stsp }
8963 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8964 f259c4c1 2021-09-24 stsp }
8965 f259c4c1 2021-09-24 stsp return err;
8966 f259c4c1 2021-09-24 stsp }
8967 f259c4c1 2021-09-24 stsp
8968 f259c4c1 2021-09-24 stsp const struct got_error *
8969 f4ab0e57 2024-03-28 stsp got_worktree_merge_info(char **branch_name, struct got_worktree *worktree,
8970 f4ab0e57 2024-03-28 stsp struct got_repository *repo)
8971 f4ab0e57 2024-03-28 stsp {
8972 f4ab0e57 2024-03-28 stsp const struct got_error *err;
8973 f4ab0e57 2024-03-28 stsp char *branch_refname = NULL;
8974 f4ab0e57 2024-03-28 stsp struct got_reference *branch_ref = NULL;
8975 f4ab0e57 2024-03-28 stsp
8976 f4ab0e57 2024-03-28 stsp *branch_name = NULL;
8977 f4ab0e57 2024-03-28 stsp
8978 f4ab0e57 2024-03-28 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8979 f4ab0e57 2024-03-28 stsp if (err)
8980 f4ab0e57 2024-03-28 stsp goto done;
8981 f4ab0e57 2024-03-28 stsp
8982 f4ab0e57 2024-03-28 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8983 f4ab0e57 2024-03-28 stsp if (err)
8984 f4ab0e57 2024-03-28 stsp goto done;
8985 f4ab0e57 2024-03-28 stsp
8986 f4ab0e57 2024-03-28 stsp if (!got_ref_is_symbolic(branch_ref)) {
8987 f4ab0e57 2024-03-28 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8988 f4ab0e57 2024-03-28 stsp "%s is not a symbolic reference",
8989 f4ab0e57 2024-03-28 stsp got_ref_get_name(branch_ref));
8990 f4ab0e57 2024-03-28 stsp goto done;
8991 f4ab0e57 2024-03-28 stsp }
8992 f4ab0e57 2024-03-28 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8993 f4ab0e57 2024-03-28 stsp if (*branch_name == NULL) {
8994 f4ab0e57 2024-03-28 stsp err = got_error_from_errno("strdup");
8995 f4ab0e57 2024-03-28 stsp goto done;
8996 f4ab0e57 2024-03-28 stsp }
8997 f4ab0e57 2024-03-28 stsp
8998 f4ab0e57 2024-03-28 stsp done:
8999 f4ab0e57 2024-03-28 stsp free(branch_refname);
9000 f4ab0e57 2024-03-28 stsp if (branch_ref)
9001 f4ab0e57 2024-03-28 stsp got_ref_close(branch_ref);
9002 f4ab0e57 2024-03-28 stsp if (err) {
9003 f4ab0e57 2024-03-28 stsp if (*branch_name) {
9004 f4ab0e57 2024-03-28 stsp free(*branch_name);
9005 f4ab0e57 2024-03-28 stsp *branch_name = NULL;
9006 f4ab0e57 2024-03-28 stsp }
9007 f4ab0e57 2024-03-28 stsp }
9008 f4ab0e57 2024-03-28 stsp return err;
9009 f4ab0e57 2024-03-28 stsp }
9010 f4ab0e57 2024-03-28 stsp
9011 f4ab0e57 2024-03-28 stsp const struct got_error *
9012 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
9013 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
9014 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9015 f259c4c1 2021-09-24 stsp {
9016 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
9017 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9018 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
9019 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
9020 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
9021 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
9022 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
9023 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
9024 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
9025 af179be7 2023-04-14 stsp
9026 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
9027 af179be7 2023-04-14 stsp
9028 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
9029 af179be7 2023-04-14 stsp if (err)
9030 af179be7 2023-04-14 stsp goto done;
9031 af179be7 2023-04-14 stsp
9032 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
9033 af179be7 2023-04-14 stsp if (err)
9034 af179be7 2023-04-14 stsp goto done;
9035 af179be7 2023-04-14 stsp
9036 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
9037 af179be7 2023-04-14 stsp if (err)
9038 af179be7 2023-04-14 stsp goto done;
9039 af179be7 2023-04-14 stsp
9040 af179be7 2023-04-14 stsp /*
9041 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
9042 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
9043 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
9044 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
9045 af179be7 2023-04-14 stsp */
9046 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
9047 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
9048 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
9049 af179be7 2023-04-14 stsp if (err)
9050 af179be7 2023-04-14 stsp goto done;
9051 f259c4c1 2021-09-24 stsp
9052 af179be7 2023-04-14 stsp
9053 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
9054 a44927cc 2022-04-07 stsp worktree->base_commit_id);
9055 a44927cc 2022-04-07 stsp if (err)
9056 a44927cc 2022-04-07 stsp goto done;
9057 a44927cc 2022-04-07 stsp
9058 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
9059 a44927cc 2022-04-07 stsp worktree->path_prefix);
9060 f259c4c1 2021-09-24 stsp if (err)
9061 f259c4c1 2021-09-24 stsp goto done;
9062 f259c4c1 2021-09-24 stsp
9063 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
9064 f259c4c1 2021-09-24 stsp if (err)
9065 f259c4c1 2021-09-24 stsp goto done;
9066 f259c4c1 2021-09-24 stsp
9067 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
9068 f259c4c1 2021-09-24 stsp if (err)
9069 f259c4c1 2021-09-24 stsp goto done;
9070 f259c4c1 2021-09-24 stsp
9071 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
9072 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
9073 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
9074 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
9075 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
9076 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
9077 f259c4c1 2021-09-24 stsp rfa.repo = repo;
9078 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
9079 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
9080 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
9081 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
9082 f259c4c1 2021-09-24 stsp if (err)
9083 f259c4c1 2021-09-24 stsp goto sync;
9084 f259c4c1 2021-09-24 stsp
9085 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
9086 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
9087 f259c4c1 2021-09-24 stsp sync:
9088 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9089 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
9090 f259c4c1 2021-09-24 stsp err = sync_err;
9091 f259c4c1 2021-09-24 stsp done:
9092 f259c4c1 2021-09-24 stsp free(tree_id);
9093 af179be7 2023-04-14 stsp free(merged_commit_id);
9094 a44927cc 2022-04-07 stsp if (commit)
9095 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9096 f259c4c1 2021-09-24 stsp if (fileindex)
9097 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
9098 f259c4c1 2021-09-24 stsp free(fileindex_path);
9099 af179be7 2023-04-14 stsp if (commit_ref)
9100 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
9101 af179be7 2023-04-14 stsp free(commit_ref_name);
9102 f259c4c1 2021-09-24 stsp
9103 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9104 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
9105 f259c4c1 2021-09-24 stsp err = unlockerr;
9106 f259c4c1 2021-09-24 stsp return err;
9107 f259c4c1 2021-09-24 stsp }
9108 f259c4c1 2021-09-24 stsp
9109 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
9110 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
9111 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9112 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9113 2db2652d 2019-08-07 stsp struct got_repository *repo;
9114 2db2652d 2019-08-07 stsp int have_changes;
9115 2db2652d 2019-08-07 stsp };
9116 2db2652d 2019-08-07 stsp
9117 336075a4 2022-06-25 op static const struct got_error *
9118 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
9119 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9120 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9121 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9122 735ef5ac 2019-08-03 stsp {
9123 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
9124 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
9125 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
9126 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
9127 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
9128 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
9129 8b13ce36 2019-08-08 stsp
9130 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
9131 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
9132 8b13ce36 2019-08-08 stsp return NULL;
9133 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
9134 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
9135 735ef5ac 2019-08-03 stsp
9136 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9137 735ef5ac 2019-08-03 stsp if (ie == NULL)
9138 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9139 735ef5ac 2019-08-03 stsp
9140 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
9141 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
9142 735ef5ac 2019-08-03 stsp relpath) == -1)
9143 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
9144 735ef5ac 2019-08-03 stsp
9145 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
9146 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
9147 b4b2adf5 2023-02-09 op &base_commit_id, ie);
9148 735ef5ac 2019-08-03 stsp }
9149 735ef5ac 2019-08-03 stsp
9150 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
9151 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9152 3aa5969e 2019-08-06 stsp goto done;
9153 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9154 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9155 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9156 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9157 735ef5ac 2019-08-03 stsp goto done;
9158 3aa5969e 2019-08-06 stsp }
9159 735ef5ac 2019-08-03 stsp
9160 2db2652d 2019-08-07 stsp a->have_changes = 1;
9161 2db2652d 2019-08-07 stsp
9162 735ef5ac 2019-08-03 stsp p = in_repo_path;
9163 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9164 735ef5ac 2019-08-03 stsp p++;
9165 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9166 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9167 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9168 735ef5ac 2019-08-03 stsp done:
9169 735ef5ac 2019-08-03 stsp free(in_repo_path);
9170 dc424a06 2019-08-07 stsp return err;
9171 dc424a06 2019-08-07 stsp }
9172 dc424a06 2019-08-07 stsp
9173 2db2652d 2019-08-07 stsp struct stage_path_arg {
9174 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9175 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9176 2db2652d 2019-08-07 stsp struct got_repository *repo;
9177 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9178 2db2652d 2019-08-07 stsp void *status_arg;
9179 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9180 2db2652d 2019-08-07 stsp void *patch_arg;
9181 7b5dc508 2019-10-28 stsp int staged_something;
9182 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9183 2db2652d 2019-08-07 stsp };
9184 2db2652d 2019-08-07 stsp
9185 2db2652d 2019-08-07 stsp static const struct got_error *
9186 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9187 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9188 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9189 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9190 0cb83759 2019-08-03 stsp {
9191 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9192 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9193 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9194 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9195 0cb83759 2019-08-03 stsp uint32_t stage;
9196 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9197 0aeb8099 2020-07-23 stsp struct stat sb;
9198 8b13ce36 2019-08-08 stsp
9199 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9200 8b13ce36 2019-08-08 stsp return NULL;
9201 0cb83759 2019-08-03 stsp
9202 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9203 d3e7c587 2019-08-03 stsp if (ie == NULL)
9204 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9205 0cb83759 2019-08-03 stsp
9206 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9207 2db2652d 2019-08-07 stsp relpath)== -1)
9208 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9209 0cb83759 2019-08-03 stsp
9210 0cb83759 2019-08-03 stsp switch (status) {
9211 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9212 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9213 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9214 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9215 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9216 0aeb8099 2020-07-23 stsp break;
9217 0aeb8099 2020-07-23 stsp }
9218 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9219 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9220 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9221 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9222 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9223 dc424a06 2019-08-07 stsp if (err)
9224 dc424a06 2019-08-07 stsp break;
9225 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9226 dc424a06 2019-08-07 stsp break;
9227 dc424a06 2019-08-07 stsp } else {
9228 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9229 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9230 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9231 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9232 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9233 dc424a06 2019-08-07 stsp break;
9234 dc424a06 2019-08-07 stsp }
9235 dc424a06 2019-08-07 stsp }
9236 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9237 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9238 0cb83759 2019-08-03 stsp if (err)
9239 dc424a06 2019-08-07 stsp break;
9240 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9241 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9242 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9243 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9244 0cb83759 2019-08-03 stsp else
9245 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9246 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9247 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9248 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9249 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9250 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9251 35213c7c 2020-07-23 stsp ssize_t target_len;
9252 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9253 35213c7c 2020-07-23 stsp sizeof(target_path));
9254 35213c7c 2020-07-23 stsp if (target_len == -1) {
9255 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9256 35213c7c 2020-07-23 stsp ondisk_path);
9257 35213c7c 2020-07-23 stsp break;
9258 35213c7c 2020-07-23 stsp }
9259 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9260 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9261 df6221c7 2023-07-19 stsp a->worktree->root_path,
9262 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
9263 35213c7c 2020-07-23 stsp if (err)
9264 35213c7c 2020-07-23 stsp break;
9265 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9266 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9267 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9268 35213c7c 2020-07-23 stsp break;
9269 35213c7c 2020-07-23 stsp }
9270 35213c7c 2020-07-23 stsp }
9271 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9272 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9273 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9274 35213c7c 2020-07-23 stsp else
9275 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9276 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9277 0aeb8099 2020-07-23 stsp } else {
9278 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9279 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9280 0aeb8099 2020-07-23 stsp }
9281 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9282 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9283 dc424a06 2019-08-07 stsp break;
9284 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9285 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9286 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9287 9fdde394 2022-06-04 op if (err)
9288 9fdde394 2022-06-04 op break;
9289 9fdde394 2022-06-04 op /*
9290 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
9291 9fdde394 2022-06-04 op * implicitly unstage the file.
9292 9fdde394 2022-06-04 op */
9293 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9294 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
9295 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
9296 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
9297 9fdde394 2022-06-04 op }
9298 0cb83759 2019-08-03 stsp break;
9299 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9300 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9301 d3e7c587 2019-08-03 stsp break;
9302 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9303 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9304 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9305 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9306 dc424a06 2019-08-07 stsp if (err)
9307 dc424a06 2019-08-07 stsp break;
9308 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9309 88f33a19 2019-08-08 stsp break;
9310 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9311 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9312 dc424a06 2019-08-07 stsp break;
9313 88f33a19 2019-08-08 stsp }
9314 dc424a06 2019-08-07 stsp }
9315 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9316 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9317 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9318 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9319 dc424a06 2019-08-07 stsp break;
9320 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9321 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9322 12463d8b 2019-12-13 stsp de_name);
9323 0cb83759 2019-08-03 stsp break;
9324 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9325 d3e7c587 2019-08-03 stsp break;
9326 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9327 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9328 ebf48fd5 2019-08-03 stsp break;
9329 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9330 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9331 2a06fe5f 2019-08-24 stsp break;
9332 0cb83759 2019-08-03 stsp default:
9333 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9334 537ac44b 2019-08-03 stsp break;
9335 0cb83759 2019-08-03 stsp }
9336 dc424a06 2019-08-07 stsp
9337 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9338 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9339 dc424a06 2019-08-07 stsp free(path_content);
9340 2db2652d 2019-08-07 stsp free(ondisk_path);
9341 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9342 0ebf8283 2019-07-24 stsp return err;
9343 0ebf8283 2019-07-24 stsp }
9344 0cb83759 2019-08-03 stsp
9345 0cb83759 2019-08-03 stsp const struct got_error *
9346 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9347 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9348 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9349 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9350 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9351 0cb83759 2019-08-03 stsp {
9352 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9353 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9354 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9355 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9356 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9357 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9358 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9359 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9360 0cb83759 2019-08-03 stsp
9361 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9362 0cb83759 2019-08-03 stsp if (err)
9363 0cb83759 2019-08-03 stsp return err;
9364 0cb83759 2019-08-03 stsp
9365 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9366 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9367 735ef5ac 2019-08-03 stsp if (err)
9368 735ef5ac 2019-08-03 stsp goto done;
9369 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9370 735ef5ac 2019-08-03 stsp if (err)
9371 735ef5ac 2019-08-03 stsp goto done;
9372 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9373 0cb83759 2019-08-03 stsp if (err)
9374 0cb83759 2019-08-03 stsp goto done;
9375 0cb83759 2019-08-03 stsp
9376 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9377 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9378 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9379 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9380 2db2652d 2019-08-07 stsp oka.repo = repo;
9381 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9382 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9383 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9384 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9385 735ef5ac 2019-08-03 stsp if (err)
9386 735ef5ac 2019-08-03 stsp goto done;
9387 735ef5ac 2019-08-03 stsp }
9388 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9389 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9390 2db2652d 2019-08-07 stsp goto done;
9391 2db2652d 2019-08-07 stsp }
9392 735ef5ac 2019-08-03 stsp
9393 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9394 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9395 2db2652d 2019-08-07 stsp spa.repo = repo;
9396 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9397 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9398 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9399 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9400 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9401 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9402 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9403 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9404 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9405 42005733 2019-08-03 stsp if (err)
9406 2db2652d 2019-08-07 stsp goto done;
9407 7b5dc508 2019-10-28 stsp }
9408 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9409 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9410 7b5dc508 2019-10-28 stsp goto done;
9411 0cb83759 2019-08-03 stsp }
9412 0cb83759 2019-08-03 stsp
9413 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9414 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9415 0cb83759 2019-08-03 stsp err = sync_err;
9416 0cb83759 2019-08-03 stsp done:
9417 735ef5ac 2019-08-03 stsp if (head_ref)
9418 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9419 735ef5ac 2019-08-03 stsp free(head_commit_id);
9420 0cb83759 2019-08-03 stsp free(fileindex_path);
9421 0cb83759 2019-08-03 stsp if (fileindex)
9422 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9423 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9424 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9425 0cb83759 2019-08-03 stsp err = unlockerr;
9426 0cb83759 2019-08-03 stsp return err;
9427 0cb83759 2019-08-03 stsp }
9428 ad493afc 2019-08-03 stsp
9429 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9430 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9431 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9432 ad493afc 2019-08-03 stsp struct got_repository *repo;
9433 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9434 ad493afc 2019-08-03 stsp void *progress_arg;
9435 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9436 2e1f37b0 2019-08-08 stsp void *patch_arg;
9437 ad493afc 2019-08-03 stsp };
9438 2e1f37b0 2019-08-08 stsp
9439 2e1f37b0 2019-08-08 stsp static const struct got_error *
9440 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9441 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9442 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9443 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9444 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9445 2e1f37b0 2019-08-08 stsp {
9446 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9447 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9448 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9449 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9450 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9451 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9452 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9453 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9454 2e1f37b0 2019-08-08 stsp
9455 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9456 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9457 2e1f37b0 2019-08-08 stsp
9458 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9459 2e1f37b0 2019-08-08 stsp if (err)
9460 2e1f37b0 2019-08-08 stsp return err;
9461 eb81bc23 2022-06-28 tracey
9462 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9463 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9464 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9465 eb81bc23 2022-06-28 tracey goto done;
9466 eb81bc23 2022-06-28 tracey }
9467 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9468 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9469 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9470 eb81bc23 2022-06-28 tracey goto done;
9471 eb81bc23 2022-06-28 tracey }
9472 eb81bc23 2022-06-28 tracey
9473 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9474 2e1f37b0 2019-08-08 stsp if (err)
9475 2e1f37b0 2019-08-08 stsp goto done;
9476 2e1f37b0 2019-08-08 stsp
9477 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9478 2e1f37b0 2019-08-08 stsp if (err)
9479 2e1f37b0 2019-08-08 stsp goto done;
9480 2e1f37b0 2019-08-08 stsp
9481 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9482 2e1f37b0 2019-08-08 stsp if (err)
9483 2e1f37b0 2019-08-08 stsp goto done;
9484 2e1f37b0 2019-08-08 stsp
9485 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9486 eb81bc23 2022-06-28 tracey fd2);
9487 2e1f37b0 2019-08-08 stsp if (err)
9488 2e1f37b0 2019-08-08 stsp goto done;
9489 2e1f37b0 2019-08-08 stsp
9490 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9491 2e1f37b0 2019-08-08 stsp if (err)
9492 2e1f37b0 2019-08-08 stsp goto done;
9493 ad493afc 2019-08-03 stsp
9494 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9495 2e1f37b0 2019-08-08 stsp if (err)
9496 2e1f37b0 2019-08-08 stsp goto done;
9497 2e1f37b0 2019-08-08 stsp
9498 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9499 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9500 2e1f37b0 2019-08-08 stsp if (err)
9501 2e1f37b0 2019-08-08 stsp goto done;
9502 2e1f37b0 2019-08-08 stsp
9503 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9504 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9505 2e1f37b0 2019-08-08 stsp if (err)
9506 2e1f37b0 2019-08-08 stsp goto done;
9507 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9508 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9509 2e1f37b0 2019-08-08 stsp if (err)
9510 2e1f37b0 2019-08-08 stsp goto done;
9511 2e1f37b0 2019-08-08 stsp
9512 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9513 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9514 2e1f37b0 2019-08-08 stsp goto done;
9515 2e1f37b0 2019-08-08 stsp }
9516 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9517 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9518 2e1f37b0 2019-08-08 stsp goto done;
9519 2e1f37b0 2019-08-08 stsp }
9520 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9521 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9522 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9523 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9524 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9525 fe621944 2020-11-10 stsp nchanges++;
9526 fe621944 2020-11-10 stsp }
9527 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9528 2e1f37b0 2019-08-08 stsp int choice;
9529 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9530 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9531 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9532 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9533 2e1f37b0 2019-08-08 stsp if (err)
9534 2e1f37b0 2019-08-08 stsp goto done;
9535 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9536 2e1f37b0 2019-08-08 stsp have_content = 1;
9537 19e4b907 2019-08-08 stsp else
9538 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9539 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9540 2e1f37b0 2019-08-08 stsp break;
9541 2e1f37b0 2019-08-08 stsp }
9542 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9543 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9544 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9545 2e1f37b0 2019-08-08 stsp done:
9546 2e1f37b0 2019-08-08 stsp free(label1);
9547 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9548 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9549 2e1f37b0 2019-08-08 stsp if (blob)
9550 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9551 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9552 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9553 2e1f37b0 2019-08-08 stsp if (staged_blob)
9554 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9555 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9556 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9557 fe621944 2020-11-10 stsp err = free_err;
9558 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9559 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9560 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9561 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9562 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9563 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9564 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9565 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9566 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9567 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9568 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9569 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9570 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9571 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9572 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9573 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9574 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9575 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9576 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9577 2e1f37b0 2019-08-08 stsp }
9578 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9579 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9580 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9581 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9582 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9583 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9584 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9585 2e1f37b0 2019-08-08 stsp }
9586 2e1f37b0 2019-08-08 stsp free(path1);
9587 2e1f37b0 2019-08-08 stsp free(path2);
9588 fda8017d 2020-07-23 stsp return err;
9589 fda8017d 2020-07-23 stsp }
9590 fda8017d 2020-07-23 stsp
9591 fda8017d 2020-07-23 stsp static const struct got_error *
9592 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9593 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9594 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9595 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9596 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9597 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9598 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9599 fda8017d 2020-07-23 stsp {
9600 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9601 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9602 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9603 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9604 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9605 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9606 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9607 fda8017d 2020-07-23 stsp struct stat sb;
9608 fda8017d 2020-07-23 stsp
9609 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9610 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9611 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9612 fda8017d 2020-07-23 stsp if (err)
9613 fda8017d 2020-07-23 stsp return err;
9614 fda8017d 2020-07-23 stsp
9615 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9616 fda8017d 2020-07-23 stsp return NULL;
9617 fda8017d 2020-07-23 stsp
9618 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9619 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9620 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9621 fda8017d 2020-07-23 stsp if (err)
9622 fda8017d 2020-07-23 stsp goto done;
9623 fda8017d 2020-07-23 stsp }
9624 fda8017d 2020-07-23 stsp
9625 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9626 fda8017d 2020-07-23 stsp if (f == NULL) {
9627 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9628 fda8017d 2020-07-23 stsp path_unstaged_content);
9629 fda8017d 2020-07-23 stsp goto done;
9630 fda8017d 2020-07-23 stsp }
9631 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9632 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9633 fda8017d 2020-07-23 stsp goto done;
9634 fda8017d 2020-07-23 stsp }
9635 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9636 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9637 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9638 fda8017d 2020-07-23 stsp size_t r;
9639 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9640 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9641 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9642 fda8017d 2020-07-23 stsp goto done;
9643 fda8017d 2020-07-23 stsp }
9644 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9645 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9646 fda8017d 2020-07-23 stsp goto done;
9647 fda8017d 2020-07-23 stsp }
9648 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9649 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9650 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9651 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9652 fda8017d 2020-07-23 stsp progress_arg);
9653 fda8017d 2020-07-23 stsp } else {
9654 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9655 67a66647 2021-05-31 stsp
9656 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9657 67a66647 2021-05-31 stsp if (err)
9658 67a66647 2021-05-31 stsp return err;
9659 67a66647 2021-05-31 stsp
9660 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9661 67a66647 2021-05-31 stsp parent) == -1) {
9662 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9663 67a66647 2021-05-31 stsp base_path = NULL;
9664 67a66647 2021-05-31 stsp goto done;
9665 67a66647 2021-05-31 stsp }
9666 67a66647 2021-05-31 stsp
9667 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9668 b90054ed 2022-11-01 stsp base_path, "");
9669 67a66647 2021-05-31 stsp if (err)
9670 67a66647 2021-05-31 stsp goto done;
9671 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9672 67a66647 2021-05-31 stsp blob_base);
9673 67a66647 2021-05-31 stsp if (err)
9674 67a66647 2021-05-31 stsp goto done;
9675 67a66647 2021-05-31 stsp
9676 5e91dae4 2022-08-30 stsp /*
9677 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9678 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9679 eec2f5a9 2021-06-03 stsp */
9680 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9681 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9682 eec2f5a9 2021-06-03 stsp ondisk_path);
9683 eec2f5a9 2021-06-03 stsp if (err)
9684 eec2f5a9 2021-06-03 stsp goto done;
9685 eec2f5a9 2021-06-03 stsp } else {
9686 eec2f5a9 2021-06-03 stsp int fd;
9687 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9688 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9689 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9690 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9691 eec2f5a9 2021-06-03 stsp goto done;
9692 eec2f5a9 2021-06-03 stsp }
9693 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9694 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9695 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9696 eec2f5a9 2021-06-03 stsp close(fd);
9697 eec2f5a9 2021-06-03 stsp goto done;
9698 eec2f5a9 2021-06-03 stsp }
9699 eec2f5a9 2021-06-03 stsp }
9700 eec2f5a9 2021-06-03 stsp
9701 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9702 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9703 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9704 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9705 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9706 fda8017d 2020-07-23 stsp }
9707 fda8017d 2020-07-23 stsp if (err)
9708 fda8017d 2020-07-23 stsp goto done;
9709 fda8017d 2020-07-23 stsp
9710 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9711 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9712 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9713 2ac8aa02 2020-11-09 stsp } else {
9714 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9715 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9716 2ac8aa02 2020-11-09 stsp }
9717 fda8017d 2020-07-23 stsp done:
9718 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9719 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9720 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9721 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9722 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9723 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9724 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9725 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9726 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9727 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9728 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9729 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9730 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9731 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9732 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9733 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9734 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9735 67a66647 2021-05-31 stsp free(blob_base_path);
9736 67a66647 2021-05-31 stsp free(parent);
9737 67a66647 2021-05-31 stsp free(base_path);
9738 2e1f37b0 2019-08-08 stsp return err;
9739 2e1f37b0 2019-08-08 stsp }
9740 2e1f37b0 2019-08-08 stsp
9741 ad493afc 2019-08-03 stsp static const struct got_error *
9742 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9743 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9744 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9745 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9746 ad493afc 2019-08-03 stsp {
9747 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9748 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9749 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9750 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9751 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9752 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9753 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9754 9bc94a15 2019-08-03 stsp struct stat sb;
9755 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9756 ad493afc 2019-08-03 stsp
9757 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9758 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9759 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9760 2e1f37b0 2019-08-08 stsp return NULL;
9761 2e1f37b0 2019-08-08 stsp
9762 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9763 ad493afc 2019-08-03 stsp if (ie == NULL)
9764 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9765 9bc94a15 2019-08-03 stsp
9766 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9767 9bc94a15 2019-08-03 stsp == -1)
9768 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9769 ad493afc 2019-08-03 stsp
9770 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9771 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9772 f69721c3 2019-10-21 stsp if (err)
9773 f69721c3 2019-10-21 stsp goto done;
9774 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9775 f69721c3 2019-10-21 stsp id_str) == -1) {
9776 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9777 eb81bc23 2022-06-28 tracey goto done;
9778 eb81bc23 2022-06-28 tracey }
9779 eb81bc23 2022-06-28 tracey
9780 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9781 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9782 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9783 eb81bc23 2022-06-28 tracey goto done;
9784 eb81bc23 2022-06-28 tracey }
9785 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9786 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9787 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9788 f69721c3 2019-10-21 stsp goto done;
9789 f69721c3 2019-10-21 stsp }
9790 f69721c3 2019-10-21 stsp
9791 ad493afc 2019-08-03 stsp switch (staged_status) {
9792 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9793 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9794 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9795 ad493afc 2019-08-03 stsp if (err)
9796 ad493afc 2019-08-03 stsp break;
9797 ad493afc 2019-08-03 stsp /* fall through */
9798 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9799 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9800 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9801 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9802 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9803 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9804 2e1f37b0 2019-08-08 stsp if (err)
9805 2e1f37b0 2019-08-08 stsp break;
9806 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9807 2e1f37b0 2019-08-08 stsp break;
9808 2e1f37b0 2019-08-08 stsp } else {
9809 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9810 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9811 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9812 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9813 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9814 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9815 2e1f37b0 2019-08-08 stsp }
9816 2e1f37b0 2019-08-08 stsp }
9817 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9818 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9819 ad493afc 2019-08-03 stsp if (err)
9820 ad493afc 2019-08-03 stsp break;
9821 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9822 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9823 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9824 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9825 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9826 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9827 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9828 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9829 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9830 ea7786be 2020-07-23 stsp break;
9831 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9832 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9833 36bf999c 2020-07-23 stsp char *staged_target;
9834 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9835 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9836 36bf999c 2020-07-23 stsp if (err)
9837 36bf999c 2020-07-23 stsp goto done;
9838 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9839 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9840 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9841 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9842 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9843 36bf999c 2020-07-23 stsp free(staged_target);
9844 dfe9fba0 2020-07-23 stsp } else {
9845 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9846 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9847 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9848 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9849 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9850 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9851 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9852 dfe9fba0 2020-07-23 stsp }
9853 ea7786be 2020-07-23 stsp break;
9854 ea7786be 2020-07-23 stsp default:
9855 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9856 ea7786be 2020-07-23 stsp break;
9857 ea7786be 2020-07-23 stsp }
9858 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9859 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9860 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9861 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9862 2ac8aa02 2020-11-09 stsp }
9863 ad493afc 2019-08-03 stsp break;
9864 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9865 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9866 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9867 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9868 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9869 2e1f37b0 2019-08-08 stsp if (err)
9870 2e1f37b0 2019-08-08 stsp break;
9871 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9872 2e1f37b0 2019-08-08 stsp break;
9873 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9874 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9875 2e1f37b0 2019-08-08 stsp break;
9876 2e1f37b0 2019-08-08 stsp }
9877 2e1f37b0 2019-08-08 stsp }
9878 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9879 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9880 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9881 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9882 9bc94a15 2019-08-03 stsp if (err)
9883 9bc94a15 2019-08-03 stsp break;
9884 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9885 ad493afc 2019-08-03 stsp break;
9886 ad493afc 2019-08-03 stsp }
9887 f69721c3 2019-10-21 stsp done:
9888 ad493afc 2019-08-03 stsp free(ondisk_path);
9889 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9890 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9891 ad493afc 2019-08-03 stsp if (blob_base)
9892 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9893 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9894 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9895 ad493afc 2019-08-03 stsp if (blob_staged)
9896 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9897 f69721c3 2019-10-21 stsp free(id_str);
9898 f69721c3 2019-10-21 stsp free(label_orig);
9899 ad493afc 2019-08-03 stsp return err;
9900 ad493afc 2019-08-03 stsp }
9901 ad493afc 2019-08-03 stsp
9902 ad493afc 2019-08-03 stsp const struct got_error *
9903 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9904 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9905 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9906 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9907 ad493afc 2019-08-03 stsp struct got_repository *repo)
9908 ad493afc 2019-08-03 stsp {
9909 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9910 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9911 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9912 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9913 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9914 ad493afc 2019-08-03 stsp
9915 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9916 ad493afc 2019-08-03 stsp if (err)
9917 ad493afc 2019-08-03 stsp return err;
9918 ad493afc 2019-08-03 stsp
9919 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9920 ad493afc 2019-08-03 stsp if (err)
9921 ad493afc 2019-08-03 stsp goto done;
9922 ad493afc 2019-08-03 stsp
9923 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9924 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9925 ad493afc 2019-08-03 stsp upa.repo = repo;
9926 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9927 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9928 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9929 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9930 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9931 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9932 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9933 ad493afc 2019-08-03 stsp if (err)
9934 ad493afc 2019-08-03 stsp goto done;
9935 ad493afc 2019-08-03 stsp }
9936 ad493afc 2019-08-03 stsp
9937 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9938 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9939 ad493afc 2019-08-03 stsp err = sync_err;
9940 ad493afc 2019-08-03 stsp done:
9941 ad493afc 2019-08-03 stsp free(fileindex_path);
9942 ad493afc 2019-08-03 stsp if (fileindex)
9943 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9944 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9945 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9946 ad493afc 2019-08-03 stsp err = unlockerr;
9947 ad493afc 2019-08-03 stsp return err;
9948 ad493afc 2019-08-03 stsp }
9949 b2118c49 2020-07-28 stsp
9950 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9951 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9952 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9953 b2118c49 2020-07-28 stsp void *info_arg;
9954 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9955 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9956 b2118c49 2020-07-28 stsp void *cancel_arg;
9957 b2118c49 2020-07-28 stsp };
9958 b2118c49 2020-07-28 stsp
9959 b2118c49 2020-07-28 stsp static const struct got_error *
9960 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9961 b2118c49 2020-07-28 stsp {
9962 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
9963 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9964 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9965 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9966 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9967 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9968 b2118c49 2020-07-28 stsp int stage;
9969 b2118c49 2020-07-28 stsp
9970 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
9971 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
9972 f6b8c3c2 2023-07-24 stsp if (err)
9973 f6b8c3c2 2023-07-24 stsp return err;
9974 f6b8c3c2 2023-07-24 stsp }
9975 b2118c49 2020-07-28 stsp
9976 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9977 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9978 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9979 b2118c49 2020-07-28 stsp break;
9980 b2118c49 2020-07-28 stsp }
9981 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9982 b2118c49 2020-07-28 stsp return NULL;
9983 b2118c49 2020-07-28 stsp
9984 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9985 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9986 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9987 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9988 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9989 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9990 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9991 b2118c49 2020-07-28 stsp }
9992 b2118c49 2020-07-28 stsp
9993 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9994 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9995 b2118c49 2020-07-28 stsp
9996 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9997 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9998 b2118c49 2020-07-28 stsp }
9999 b2118c49 2020-07-28 stsp
10000 b2118c49 2020-07-28 stsp const struct got_error *
10001 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
10002 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
10003 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
10004 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
10005 b2118c49 2020-07-28 stsp
10006 b2118c49 2020-07-28 stsp {
10007 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
10008 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
10009 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
10010 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
10011 b2118c49 2020-07-28 stsp
10012 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
10013 b2118c49 2020-07-28 stsp if (err)
10014 b2118c49 2020-07-28 stsp return err;
10015 b2118c49 2020-07-28 stsp
10016 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
10017 b2118c49 2020-07-28 stsp if (err)
10018 b2118c49 2020-07-28 stsp goto done;
10019 b2118c49 2020-07-28 stsp
10020 b2118c49 2020-07-28 stsp arg.worktree = worktree;
10021 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
10022 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
10023 b2118c49 2020-07-28 stsp arg.paths = paths;
10024 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
10025 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
10026 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
10027 b2118c49 2020-07-28 stsp &arg);
10028 b2118c49 2020-07-28 stsp done:
10029 b2118c49 2020-07-28 stsp free(fileindex_path);
10030 b2118c49 2020-07-28 stsp if (fileindex)
10031 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
10032 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
10033 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
10034 b2118c49 2020-07-28 stsp err = unlockerr;
10035 b2118c49 2020-07-28 stsp return err;
10036 b2118c49 2020-07-28 stsp }
10037 78f5ac24 2022-03-19 op
10038 78f5ac24 2022-03-19 op static const struct got_error *
10039 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
10040 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
10041 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
10042 78f5ac24 2022-03-19 op {
10043 78f5ac24 2022-03-19 op const struct got_error *err;
10044 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
10045 78f5ac24 2022-03-19 op struct stat sb;
10046 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
10047 78f5ac24 2022-03-19 op
10048 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
10049 78f5ac24 2022-03-19 op if (err)
10050 78f5ac24 2022-03-19 op return err;
10051 78f5ac24 2022-03-19 op
10052 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
10053 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
10054 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
10055 78f5ac24 2022-03-19 op
10056 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
10057 78f5ac24 2022-03-19 op if (ie) {
10058 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
10059 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
10060 a05fb460 2022-04-23 op repo);
10061 78f5ac24 2022-03-19 op if (err)
10062 78f5ac24 2022-03-19 op goto done;
10063 78f5ac24 2022-03-19 op } else {
10064 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
10065 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
10066 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
10067 78f5ac24 2022-03-19 op if (errno != ENOENT) {
10068 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
10069 78f5ac24 2022-03-19 op ondisk_path);
10070 78f5ac24 2022-03-19 op goto done;
10071 78f5ac24 2022-03-19 op }
10072 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
10073 78f5ac24 2022-03-19 op }
10074 78f5ac24 2022-03-19 op }
10075 78f5ac24 2022-03-19 op
10076 78f5ac24 2022-03-19 op done:
10077 78f5ac24 2022-03-19 op free(ondisk_path);
10078 78f5ac24 2022-03-19 op return err;
10079 78f5ac24 2022-03-19 op }
10080 78f5ac24 2022-03-19 op
10081 78f5ac24 2022-03-19 op static const struct got_error *
10082 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
10083 78f5ac24 2022-03-19 op unsigned char staged_status)
10084 78f5ac24 2022-03-19 op {
10085 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
10086 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
10087 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
10088 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
10089 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
10090 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
10091 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
10092 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
10093 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
10094 78f5ac24 2022-03-19 op return NULL;
10095 78f5ac24 2022-03-19 op }
10096 78f5ac24 2022-03-19 op
10097 78f5ac24 2022-03-19 op static const struct got_error *
10098 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
10099 78f5ac24 2022-03-19 op {
10100 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
10101 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
10102 78f5ac24 2022-03-19 op return NULL;
10103 78f5ac24 2022-03-19 op }
10104 78f5ac24 2022-03-19 op
10105 78f5ac24 2022-03-19 op static const struct got_error *
10106 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
10107 78f5ac24 2022-03-19 op unsigned char staged_status)
10108 78f5ac24 2022-03-19 op {
10109 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
10110 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
10111 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
10112 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
10113 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
10114 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
10115 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
10116 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
10117 78f5ac24 2022-03-19 op return NULL;
10118 78f5ac24 2022-03-19 op }
10119 78f5ac24 2022-03-19 op
10120 78f5ac24 2022-03-19 op const struct got_error *
10121 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
10122 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
10123 78f5ac24 2022-03-19 op {
10124 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
10125 78f5ac24 2022-03-19 op }
10126 78f5ac24 2022-03-19 op
10127 78f5ac24 2022-03-19 op const struct got_error *
10128 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
10129 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
10130 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
10131 78f5ac24 2022-03-19 op {
10132 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
10133 78f5ac24 2022-03-19 op int file_renamed = 0;
10134 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
10135 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
10136 78f5ac24 2022-03-19 op
10137 78f5ac24 2022-03-19 op *oldpath = NULL;
10138 78f5ac24 2022-03-19 op *newpath = NULL;
10139 78f5ac24 2022-03-19 op
10140 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
10141 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
10142 78f5ac24 2022-03-19 op if (err)
10143 78f5ac24 2022-03-19 op goto done;
10144 78f5ac24 2022-03-19 op
10145 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
10146 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
10147 78f5ac24 2022-03-19 op if (err)
10148 78f5ac24 2022-03-19 op goto done;
10149 78f5ac24 2022-03-19 op
10150 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
10151 78f5ac24 2022-03-19 op file_renamed = 1;
10152 78f5ac24 2022-03-19 op
10153 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
10154 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10155 78f5ac24 2022-03-19 op else if (file_renamed) {
10156 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10157 78f5ac24 2022-03-19 op if (err == NULL)
10158 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10159 78f5ac24 2022-03-19 op } else if (old == NULL)
10160 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10161 78f5ac24 2022-03-19 op else
10162 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
10163 78f5ac24 2022-03-19 op
10164 78f5ac24 2022-03-19 op done:
10165 78f5ac24 2022-03-19 op if (err) {
10166 78f5ac24 2022-03-19 op free(*oldpath);
10167 78f5ac24 2022-03-19 op *oldpath = NULL;
10168 78f5ac24 2022-03-19 op free(*newpath);
10169 78f5ac24 2022-03-19 op *newpath = NULL;
10170 78f5ac24 2022-03-19 op }
10171 78f5ac24 2022-03-19 op return err;
10172 78f5ac24 2022-03-19 op }
10173 78f5ac24 2022-03-19 op
10174 78f5ac24 2022-03-19 op const struct got_error *
10175 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10176 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10177 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
10178 78f5ac24 2022-03-19 op {
10179 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
10180 f2dd7807 2022-05-17 op
10181 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
10182 f2dd7807 2022-05-17 op saa.worktree = worktree;
10183 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
10184 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
10185 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
10186 f2dd7807 2022-05-17 op saa.repo = repo;
10187 f2dd7807 2022-05-17 op
10188 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
10189 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
10190 78f5ac24 2022-03-19 op }
10191 f2dd7807 2022-05-17 op
10192 f2dd7807 2022-05-17 op const struct got_error *
10193 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10194 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10195 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
10196 f2dd7807 2022-05-17 op {
10197 7f4e5320 2023-05-29 stsp const struct got_error *err;
10198 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
10199 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
10200 f2dd7807 2022-05-17 op
10201 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
10202 f2dd7807 2022-05-17 op sda.worktree = worktree;
10203 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
10204 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
10205 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
10206 f2dd7807 2022-05-17 op sda.repo = repo;
10207 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
10208 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
10209 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
10210 f2dd7807 2022-05-17 op sda.status_codes = NULL;
10211 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
10212 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
10213 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
10214 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
10215 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
10216 7f4e5320 2023-05-29 stsp
10217 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
10218 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10219 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
10220 7f4e5320 2023-05-29 stsp return err;
10221 f2dd7807 2022-05-17 op }
10222 f2dd7807 2022-05-17 op
10223 f2dd7807 2022-05-17 op const struct got_error *
10224 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
10225 4bcdc895 2022-05-17 op const char *fileindex_path)
10226 f2dd7807 2022-05-17 op {
10227 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
10228 f2dd7807 2022-05-17 op
10229 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
10230 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
10231 f2dd7807 2022-05-17 op
10232 f2dd7807 2022-05-17 op return err;
10233 f2dd7807 2022-05-17 op }