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 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
999 67a66647 2021-05-31 stsp if (err)
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1002 db590691 2021-06-02 stsp } else {
1003 db590691 2021-06-02 stsp /*
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp */
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1012 db590691 2021-06-02 stsp }
1013 67a66647 2021-05-31 stsp }
1014 67a66647 2021-05-31 stsp
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1019 14c901f1 2019-08-08 stsp }
1020 14c901f1 2019-08-08 stsp
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp }
1037 eec2f5a9 2021-06-03 stsp
1038 b6b86fd1 2022-08-30 thomas /*
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp */
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp if (err)
1045 eec2f5a9 2021-06-03 stsp goto done;
1046 eec2f5a9 2021-06-03 stsp } else {
1047 eec2f5a9 2021-06-03 stsp int fd;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp }
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1058 eec2f5a9 2021-06-03 stsp }
1059 14c901f1 2019-08-08 stsp }
1060 14c901f1 2019-08-08 stsp
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 14c901f1 2019-08-08 stsp done:
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 67a66647 2021-05-31 stsp }
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp }
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1084 4a1ddfc2 2019-01-12 stsp }
1085 4a1ddfc2 2019-01-12 stsp
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp {
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 13d9040b 2019-03-27 stsp
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 65b05cec 2020-07-23 stsp
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp if (err)
1098 054041d0 2020-06-24 stsp return err;
1099 054041d0 2020-06-24 stsp
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1106 054041d0 2020-06-24 stsp done:
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp else
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1112 1ebedb77 2019-10-19 stsp }
1113 1ebedb77 2019-10-19 stsp
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp {
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1126 1ebedb77 2019-10-19 stsp }
1127 1ebedb77 2019-10-19 stsp
1128 a2c162eb 2022-10-30 thomas return st_mode;
1129 a2c162eb 2022-10-30 thomas }
1130 a2c162eb 2022-10-30 thomas
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1133 a2c162eb 2022-10-30 thomas {
1134 a2c162eb 2022-10-30 thomas mode_t um;
1135 a2c162eb 2022-10-30 thomas
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1139 13d9040b 2019-03-27 stsp }
1140 13d9040b 2019-03-27 stsp
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 8ba819a3 2020-07-23 stsp
1150 5a1fbc73 2020-07-23 stsp /*
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1153 5a1fbc73 2020-07-23 stsp */
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1157 5a1fbc73 2020-07-23 stsp {
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 5a1fbc73 2020-07-23 stsp int fd;
1162 5a1fbc73 2020-07-23 stsp
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 c6e8a826 2021-04-05 stsp
1165 5a1fbc73 2020-07-23 stsp /*
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1172 5a1fbc73 2020-07-23 stsp */
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1177 5a1fbc73 2020-07-23 stsp
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 5a1fbc73 2020-07-23 stsp }
1187 5a1fbc73 2020-07-23 stsp
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp }
1194 3c1ec782 2020-07-23 stsp
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 3c1ec782 2020-07-23 stsp {
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1208 3c1ec782 2020-07-23 stsp }
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp /*
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1216 3c1ec782 2020-07-23 stsp */
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1220 ce031e9e 2020-10-20 stsp if (err)
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1225 ce031e9e 2020-10-20 stsp }
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp }
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1234 3c1ec782 2020-07-23 stsp if (err)
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp } else {
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp if (err)
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp }
1258 5a1fbc73 2020-07-23 stsp
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 9d31a1d8 2018-03-11 stsp {
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 8ba819a3 2020-07-23 stsp
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 2e1fa222 2020-07-23 stsp
1275 9e39ca77 2022-07-21 thomas /*
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp */
1281 8ba819a3 2020-07-23 stsp do {
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1285 3521b466 2022-07-21 thomas
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1295 8ba819a3 2020-07-23 stsp }
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1305 8ba819a3 2020-07-23 stsp
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1308 3c1ec782 2020-07-23 stsp if (err)
1309 3c1ec782 2020-07-23 stsp return err;
1310 8ba819a3 2020-07-23 stsp
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1319 906c123b 2020-07-23 stsp }
1320 906c123b 2020-07-23 stsp
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1328 c90c8ce3 2020-07-23 stsp }
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1331 5a1fbc73 2020-07-23 stsp if (err)
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1339 c6e8a826 2021-04-05 stsp } else {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 c6e8a826 2021-04-05 stsp }
1343 f35fa46a 2020-07-23 stsp }
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp }
1346 f35fa46a 2020-07-23 stsp
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1350 f4994adc 2020-10-20 stsp if (err)
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1354 f35fa46a 2020-07-23 stsp if (err)
1355 9e39ca77 2022-07-21 thomas return err;
1356 f35fa46a 2020-07-23 stsp /*
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1359 f35fa46a 2020-07-23 stsp */
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1379 8ba819a3 2020-07-23 stsp } else {
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1382 8ba819a3 2020-07-23 stsp }
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp }
1388 8ba819a3 2020-07-23 stsp
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 8ba819a3 2020-07-23 stsp {
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1403 8ba819a3 2020-07-23 stsp
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1411 f5375317 2020-10-20 stsp if (err)
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1416 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp if (err)
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 a2c162eb 2022-10-30 thomas mode);
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1430 3b9f0f87 2020-07-23 stsp }
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1437 d70b8e30 2018-12-27 stsp } else {
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp if (err)
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1443 a2c162eb 2022-10-30 thomas
1444 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1445 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1446 a2c162eb 2022-10-30 thomas tmppath);
1447 a2c162eb 2022-10-30 thomas goto done;
1448 a2c162eb 2022-10-30 thomas }
1449 9d31a1d8 2018-03-11 stsp }
1450 507dc3bb 2018-12-29 stsp } else
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 3818e3c4 2020-11-01 naddy }
1453 3818e3c4 2020-11-01 naddy
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp if (err)
1465 bd6aa359 2020-07-23 stsp goto done;
1466 bd6aa359 2020-07-23 stsp }
1467 d7b62c98 2018-12-27 stsp
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp do {
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (err)
1473 9d31a1d8 2018-03-11 stsp break;
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1483 9d31a1d8 2018-03-11 stsp }
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp }
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 9d31a1d8 2018-03-11 stsp
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1491 816dc654 2019-02-16 stsp }
1492 9d31a1d8 2018-03-11 stsp
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1497 f6d8c0ac 2020-11-09 stsp }
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1502 68ed9ba5 2019-02-10 stsp }
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1505 507dc3bb 2018-12-29 stsp }
1506 507dc3bb 2018-12-29 stsp
1507 9d31a1d8 2018-03-11 stsp done:
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1514 6353ad76 2019-02-08 stsp }
1515 6353ad76 2019-02-08 stsp
1516 be94c032 2023-02-20 thomas /*
1517 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1519 be94c032 2023-02-20 thomas */
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1522 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1523 be94c032 2023-02-20 thomas FILE *ondisk_file)
1524 7154f6ce 2019-03-27 stsp {
1525 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1526 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1530 7154f6ce 2019-03-27 stsp };
1531 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1532 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1533 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1534 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1535 9bdd68dd 2020-01-02 naddy char *line = NULL;
1536 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1537 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1538 7154f6ce 2019-03-27 stsp
1539 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1540 8de9d8ad 2023-02-20 thomas return NULL;
1541 be94c032 2023-02-20 thomas
1542 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1543 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1544 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1545 be94c032 2023-02-20 thomas
1546 8de9d8ad 2023-02-20 thomas if (blob) {
1547 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1548 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 be94c032 2023-02-20 thomas if (err)
1550 be94c032 2023-02-20 thomas goto done;
1551 8de9d8ad 2023-02-20 thomas }
1552 be94c032 2023-02-20 thomas
1553 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 8de9d8ad 2023-02-20 thomas if (err)
1556 8de9d8ad 2023-02-20 thomas goto done;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1559 8de9d8ad 2023-02-20 thomas
1560 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1562 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1563 450d9f6b 2023-02-20 thomas off_t pos;
1564 8de9d8ad 2023-02-20 thomas
1565 8de9d8ad 2023-02-20 thomas /*
1566 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1567 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1568 8de9d8ad 2023-02-20 thomas */
1569 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1570 8de9d8ad 2023-02-20 thomas
1571 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1573 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1574 7154f6ce 2019-03-27 stsp }
1575 7154f6ce 2019-03-27 stsp
1576 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1577 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 450d9f6b 2023-02-20 thomas goto done;
1580 7154f6ce 2019-03-27 stsp }
1581 8de9d8ad 2023-02-20 thomas
1582 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1584 450d9f6b 2023-02-20 thomas
1585 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1586 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1587 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1588 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 8de9d8ad 2023-02-20 thomas break;
1592 8de9d8ad 2023-02-20 thomas }
1593 8de9d8ad 2023-02-20 thomas
1594 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1595 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1596 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1597 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1599 8de9d8ad 2023-02-20 thomas goto done;
1600 8de9d8ad 2023-02-20 thomas } else
1601 8de9d8ad 2023-02-20 thomas i++;
1602 8de9d8ad 2023-02-20 thomas }
1603 8de9d8ad 2023-02-20 thomas ++ln;
1604 8de9d8ad 2023-02-20 thomas }
1605 7154f6ce 2019-03-27 stsp }
1606 be94c032 2023-02-20 thomas
1607 be94c032 2023-02-20 thomas done:
1608 9bdd68dd 2020-01-02 naddy free(line);
1609 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1611 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1612 8de9d8ad 2023-02-20 thomas if (err == NULL)
1613 8de9d8ad 2023-02-20 thomas err = free_err;
1614 7154f6ce 2019-03-27 stsp
1615 7154f6ce 2019-03-27 stsp return err;
1616 e2b1e152 2019-07-13 stsp }
1617 e2b1e152 2019-07-13 stsp
1618 e2b1e152 2019-07-13 stsp static int
1619 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 1ebedb77 2019-10-19 stsp {
1621 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 1ebedb77 2019-10-19 stsp }
1624 1ebedb77 2019-10-19 stsp
1625 1ebedb77 2019-10-19 stsp static int
1626 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 e2b1e152 2019-07-13 stsp {
1628 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1633 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1634 c363b2c1 2019-08-03 stsp }
1635 c363b2c1 2019-08-03 stsp
1636 c363b2c1 2019-08-03 stsp static unsigned char
1637 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1638 c363b2c1 2019-08-03 stsp {
1639 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1644 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1646 c363b2c1 2019-08-03 stsp default:
1647 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp }
1650 a919d5c4 2020-07-23 stsp
1651 a919d5c4 2020-07-23 stsp static const struct got_error *
1652 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1653 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1654 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1655 a919d5c4 2020-07-23 stsp {
1656 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1657 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1658 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1659 a919d5c4 2020-07-23 stsp ssize_t elen;
1660 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1661 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1665 a919d5c4 2020-07-23 stsp
1666 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1667 a919d5c4 2020-07-23 stsp do {
1668 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1669 a919d5c4 2020-07-23 stsp if (err)
1670 a919d5c4 2020-07-23 stsp return err;
1671 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1672 a919d5c4 2020-07-23 stsp /*
1673 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1674 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1675 a919d5c4 2020-07-23 stsp */
1676 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1677 a919d5c4 2020-07-23 stsp }
1678 a919d5c4 2020-07-23 stsp if (len > 0) {
1679 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1680 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1681 a919d5c4 2020-07-23 stsp len - hdrlen);
1682 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1683 a919d5c4 2020-07-23 stsp hdrlen = 0;
1684 a919d5c4 2020-07-23 stsp }
1685 a919d5c4 2020-07-23 stsp } while (len != 0);
1686 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1687 a919d5c4 2020-07-23 stsp
1688 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1689 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 a919d5c4 2020-07-23 stsp if (elen == -1)
1691 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1692 a919d5c4 2020-07-23 stsp } else {
1693 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1694 a919d5c4 2020-07-23 stsp if (elen == -1)
1695 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1696 c363b2c1 2019-08-03 stsp }
1697 a919d5c4 2020-07-23 stsp
1698 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 a919d5c4 2020-07-23 stsp
1701 a919d5c4 2020-07-23 stsp return NULL;
1702 7154f6ce 2019-03-27 stsp }
1703 7154f6ce 2019-03-27 stsp
1704 7154f6ce 2019-03-27 stsp static const struct got_error *
1705 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1706 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1707 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1708 6353ad76 2019-02-08 stsp {
1709 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1710 6353ad76 2019-02-08 stsp struct got_object_id id;
1711 6353ad76 2019-02-08 stsp size_t hdrlen;
1712 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1713 6353ad76 2019-02-08 stsp FILE *f = NULL;
1714 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1715 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1716 6353ad76 2019-02-08 stsp size_t flen, blen;
1717 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1718 6353ad76 2019-02-08 stsp
1719 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1720 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1721 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1722 6353ad76 2019-02-08 stsp
1723 7f91a133 2019-12-13 stsp /*
1724 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1725 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1726 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1727 7f91a133 2019-12-13 stsp */
1728 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1729 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1731 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 882ef1b9 2019-12-13 stsp else
1734 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1735 882ef1b9 2019-12-13 stsp goto done;
1736 882ef1b9 2019-12-13 stsp }
1737 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1738 3d35a492 2019-12-13 stsp goto done;
1739 3d35a492 2019-12-13 stsp }
1740 7f91a133 2019-12-13 stsp } else {
1741 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1743 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1744 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1745 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1747 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1748 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1750 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1752 3d35a492 2019-12-13 stsp else
1753 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1754 3d35a492 2019-12-13 stsp goto done;
1755 3d35a492 2019-12-13 stsp }
1756 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1757 1338848f 2019-12-13 stsp goto done;
1758 a378724f 2019-02-10 stsp }
1759 a378724f 2019-02-10 stsp }
1760 6353ad76 2019-02-08 stsp
1761 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1763 1338848f 2019-12-13 stsp goto done;
1764 3f148bc6 2019-07-27 stsp }
1765 efdd40df 2019-07-27 stsp
1766 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1768 1338848f 2019-12-13 stsp goto done;
1769 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1770 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1771 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1772 1338848f 2019-12-13 stsp goto done;
1773 d00136be 2019-03-26 stsp }
1774 b8f41171 2019-02-10 stsp
1775 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1776 f179e66d 2020-07-23 stsp goto done;
1777 f179e66d 2020-07-23 stsp
1778 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1779 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1781 1338848f 2019-12-13 stsp goto done;
1782 f179e66d 2020-07-23 stsp }
1783 6353ad76 2019-02-08 stsp
1784 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1785 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp else
1788 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1789 c363b2c1 2019-08-03 stsp
1790 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1791 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1792 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1793 f4ae6ddb 2022-07-01 thomas goto done;
1794 f4ae6ddb 2022-07-01 thomas }
1795 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 6353ad76 2019-02-08 stsp if (err)
1797 1338848f 2019-12-13 stsp goto done;
1798 6353ad76 2019-02-08 stsp
1799 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1800 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1801 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1802 a919d5c4 2020-07-23 stsp goto done;
1803 a919d5c4 2020-07-23 stsp }
1804 a919d5c4 2020-07-23 stsp
1805 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1806 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 ab0d4361 2019-12-13 stsp if (fd == -1) {
1808 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1809 ab0d4361 2019-12-13 stsp goto done;
1810 ab0d4361 2019-12-13 stsp }
1811 3d35a492 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp
1813 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1814 6353ad76 2019-02-08 stsp if (f == NULL) {
1815 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1816 6353ad76 2019-02-08 stsp goto done;
1817 6353ad76 2019-02-08 stsp }
1818 1338848f 2019-12-13 stsp fd = -1;
1819 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1820 656b1f76 2019-05-11 jcs for (;;) {
1821 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1823 6353ad76 2019-02-08 stsp if (err)
1824 7154f6ce 2019-03-27 stsp goto done;
1825 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1826 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1828 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1829 7154f6ce 2019-03-27 stsp goto done;
1830 80c5c120 2019-02-19 stsp }
1831 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1832 6353ad76 2019-02-08 stsp if (flen != 0)
1833 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1834 6353ad76 2019-02-08 stsp break;
1835 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1836 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1837 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp break;
1839 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1840 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1841 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1843 6353ad76 2019-02-08 stsp break;
1844 6353ad76 2019-02-08 stsp }
1845 6353ad76 2019-02-08 stsp } else {
1846 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1847 6353ad76 2019-02-08 stsp break;
1848 6353ad76 2019-02-08 stsp }
1849 6353ad76 2019-02-08 stsp hdrlen = 0;
1850 6353ad76 2019-02-08 stsp }
1851 7154f6ce 2019-03-27 stsp
1852 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1853 7154f6ce 2019-03-27 stsp rewind(f);
1854 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1855 be94c032 2023-02-20 thomas sb, f);
1856 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1857 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1858 6353ad76 2019-02-08 stsp done:
1859 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1861 6353ad76 2019-02-08 stsp if (blob)
1862 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1863 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1864 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1865 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1866 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1867 9d31a1d8 2018-03-11 stsp return err;
1868 e2b1e152 2019-07-13 stsp }
1869 e2b1e152 2019-07-13 stsp
1870 e2b1e152 2019-07-13 stsp /*
1871 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1872 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1873 e2b1e152 2019-07-13 stsp */
1874 e2b1e152 2019-07-13 stsp static const struct got_error *
1875 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1877 e2b1e152 2019-07-13 stsp {
1878 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1880 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1881 e2b1e152 2019-07-13 stsp
1882 e2b1e152 2019-07-13 stsp return NULL;
1883 9d31a1d8 2018-03-11 stsp }
1884 9d31a1d8 2018-03-11 stsp
1885 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1886 9f323212 2023-03-10 thomas
1887 9d31a1d8 2018-03-11 stsp static const struct got_error *
1888 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1889 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1891 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 0584f854 2019-04-06 stsp void *progress_arg)
1893 9d31a1d8 2018-03-11 stsp {
1894 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1895 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1896 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1897 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1898 b8f41171 2019-02-10 stsp struct stat sb;
1899 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1900 9d31a1d8 2018-03-11 stsp
1901 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1903 6353ad76 2019-02-08 stsp
1904 abb4604f 2019-07-27 stsp if (ie) {
1905 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 a76c42e6 2019-08-03 stsp goto done;
1908 a76c42e6 2019-08-03 stsp }
1909 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 7f91a133 2019-12-13 stsp repo);
1911 abb4604f 2019-07-27 stsp if (err)
1912 abb4604f 2019-07-27 stsp goto done;
1913 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1915 c90c8ce3 2020-07-23 stsp } else {
1916 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1917 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1918 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1919 e6f4ba31 2021-09-24 thomas ondisk_path);
1920 e6f4ba31 2021-09-24 thomas goto done;
1921 e6f4ba31 2021-09-24 thomas }
1922 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1924 e6f4ba31 2021-09-24 thomas } else {
1925 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1927 e6f4ba31 2021-09-24 thomas else
1928 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1929 e6f4ba31 2021-09-24 thomas }
1930 c90c8ce3 2020-07-23 stsp }
1931 6353ad76 2019-02-08 stsp
1932 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1933 2c41dce7 2021-06-27 stsp if (ie)
1934 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1935 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1936 b8f41171 2019-02-10 stsp goto done;
1937 b8f41171 2019-02-10 stsp }
1938 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1939 a769b60b 2021-06-27 stsp if (ie)
1940 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1941 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 5036ab18 2020-04-18 stsp path);
1943 5036ab18 2020-04-18 stsp goto done;
1944 9f323212 2023-03-10 thomas }
1945 9f323212 2023-03-10 thomas
1946 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1948 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1949 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1950 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1951 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1952 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1953 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1954 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1955 9f323212 2023-03-10 thomas } else if (ie) {
1956 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1957 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1958 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1959 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1960 9f323212 2023-03-10 thomas ie->path);
1961 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1962 9f323212 2023-03-10 thomas errno == ENOENT))
1963 9f323212 2023-03-10 thomas goto done;
1964 9f323212 2023-03-10 thomas }
1965 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1966 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 9f323212 2023-03-10 thomas ie->path);
1968 9f323212 2023-03-10 thomas }
1969 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1970 5036ab18 2020-04-18 stsp }
1971 b8f41171 2019-02-10 stsp
1972 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1974 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1975 c6e8a826 2021-04-05 stsp /*
1976 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1977 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1978 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1979 c6e8a826 2021-04-05 stsp * updating contents of this file.
1980 c6e8a826 2021-04-05 stsp */
1981 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1982 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1984 c6e8a826 2021-04-05 stsp /* Same commit. */
1985 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1986 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1987 e2b1e152 2019-07-13 stsp if (err)
1988 e2b1e152 2019-07-13 stsp goto done;
1989 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 b8f41171 2019-02-10 stsp path);
1991 6353ad76 2019-02-08 stsp goto done;
1992 a378724f 2019-02-10 stsp }
1993 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1994 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1995 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1996 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1997 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
1998 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
1999 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2000 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2001 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2002 b36a9774 2023-04-14 thomas }
2003 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2004 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2005 0f58026f 2021-04-05 stsp if (err)
2006 0f58026f 2021-04-05 stsp goto done;
2007 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2008 0f58026f 2021-04-05 stsp path);
2009 b8f41171 2019-02-10 stsp goto done;
2010 e2b1e152 2019-07-13 stsp }
2011 9d31a1d8 2018-03-11 stsp }
2012 9d31a1d8 2018-03-11 stsp
2013 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2014 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2015 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2016 f4ae6ddb 2022-07-01 thomas goto done;
2017 f4ae6ddb 2022-07-01 thomas }
2018 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2019 8da9e5f4 2019-01-12 stsp if (err)
2020 6353ad76 2019-02-08 stsp goto done;
2021 512f0d0e 2019-01-02 stsp
2022 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2023 234035bc 2019-06-01 stsp int update_timestamps;
2024 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2025 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2026 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2027 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2028 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2029 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2030 f4ae6ddb 2022-07-01 thomas goto done;
2031 f4ae6ddb 2022-07-01 thomas }
2032 234035bc 2019-06-01 stsp struct got_object_id id2;
2033 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2034 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2035 f4ae6ddb 2022-07-01 thomas fd2);
2036 234035bc 2019-06-01 stsp if (err)
2037 f69721c3 2019-10-21 stsp goto done;
2038 f69721c3 2019-10-21 stsp }
2039 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2040 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2041 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2042 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2043 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2044 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2045 f69721c3 2019-10-21 stsp goto done;
2046 f69721c3 2019-10-21 stsp }
2047 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2049 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2050 234035bc 2019-06-01 stsp goto done;
2051 f69721c3 2019-10-21 stsp }
2052 234035bc 2019-06-01 stsp }
2053 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2054 36bf999c 2020-07-23 stsp char *link_target;
2055 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2056 36bf999c 2020-07-23 stsp if (err)
2057 36bf999c 2020-07-23 stsp goto done;
2058 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2059 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2060 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2061 36bf999c 2020-07-23 stsp free(link_target);
2062 993e2a1b 2020-07-23 stsp } else {
2063 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2064 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2065 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2066 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2067 993e2a1b 2020-07-23 stsp }
2068 f69721c3 2019-10-21 stsp free(label_orig);
2069 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2070 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2071 f4ae6ddb 2022-07-01 thomas goto done;
2072 f4ae6ddb 2022-07-01 thomas }
2073 234035bc 2019-06-01 stsp if (blob2)
2074 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2075 909d120e 2019-09-22 stsp if (err)
2076 909d120e 2019-09-22 stsp goto done;
2077 234035bc 2019-06-01 stsp /*
2078 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2079 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2080 234035bc 2019-06-01 stsp * unmodified files again.
2081 234035bc 2019-06-01 stsp */
2082 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2083 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2084 234035bc 2019-06-01 stsp update_timestamps);
2085 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2089 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2090 1ee397ad 2019-07-12 stsp if (err)
2091 1ee397ad 2019-07-12 stsp goto done;
2092 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2093 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2094 13d9040b 2019-03-27 stsp if (err)
2095 13d9040b 2019-03-27 stsp goto done;
2096 13d9040b 2019-03-27 stsp } else {
2097 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2098 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2099 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2100 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2101 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2102 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2103 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2104 2e1fa222 2020-07-23 stsp } else {
2105 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2106 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2107 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2108 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2109 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2110 2e1fa222 2020-07-23 stsp }
2111 13d9040b 2019-03-27 stsp if (err)
2112 13d9040b 2019-03-27 stsp goto done;
2113 65b05cec 2020-07-23 stsp
2114 054041d0 2020-06-24 stsp if (ie) {
2115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2116 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2117 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2118 054041d0 2020-06-24 stsp } else {
2119 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2120 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2121 054041d0 2020-06-24 stsp &blob->id);
2122 054041d0 2020-06-24 stsp }
2123 13d9040b 2019-03-27 stsp if (err)
2124 13d9040b 2019-03-27 stsp goto done;
2125 65b05cec 2020-07-23 stsp
2126 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2127 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2128 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2129 2e1fa222 2020-07-23 stsp }
2130 13d9040b 2019-03-27 stsp }
2131 f4ae6ddb 2022-07-01 thomas
2132 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2133 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2134 f4ae6ddb 2022-07-01 thomas goto done;
2135 f4ae6ddb 2022-07-01 thomas }
2136 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2137 6353ad76 2019-02-08 stsp done:
2138 6353ad76 2019-02-08 stsp free(ondisk_path);
2139 8da9e5f4 2019-01-12 stsp return err;
2140 90285c3b 2019-01-08 stsp }
2141 90285c3b 2019-01-08 stsp
2142 90285c3b 2019-01-08 stsp static const struct got_error *
2143 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2144 90285c3b 2019-01-08 stsp {
2145 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2146 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2147 90285c3b 2019-01-08 stsp
2148 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2149 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2150 90285c3b 2019-01-08 stsp
2151 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2152 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2153 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2154 c97665ae 2019-01-13 stsp } else {
2155 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2156 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2157 1c4cdd89 2021-06-20 stsp if (err)
2158 1c4cdd89 2021-06-20 stsp goto done;
2159 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2160 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2161 fddefe3b 2020-10-20 stsp free(ondisk_path);
2162 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2163 1c4cdd89 2021-06-20 stsp parent = NULL;
2164 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2165 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2166 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2167 fddefe3b 2020-10-20 stsp ondisk_path);
2168 90285c3b 2019-01-08 stsp break;
2169 90285c3b 2019-01-08 stsp }
2170 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2171 1c4cdd89 2021-06-20 stsp if (err)
2172 1c4cdd89 2021-06-20 stsp break;
2173 1c4cdd89 2021-06-20 stsp }
2174 90285c3b 2019-01-08 stsp }
2175 1c4cdd89 2021-06-20 stsp done:
2176 90285c3b 2019-01-08 stsp free(ondisk_path);
2177 1c4cdd89 2021-06-20 stsp free(parent);
2178 90285c3b 2019-01-08 stsp return err;
2179 512f0d0e 2019-01-02 stsp }
2180 512f0d0e 2019-01-02 stsp
2181 708d8e67 2019-03-27 stsp static const struct got_error *
2182 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2183 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2184 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2185 708d8e67 2019-03-27 stsp {
2186 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2187 708d8e67 2019-03-27 stsp unsigned char status;
2188 708d8e67 2019-03-27 stsp struct stat sb;
2189 708d8e67 2019-03-27 stsp char *ondisk_path;
2190 708d8e67 2019-03-27 stsp
2191 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2192 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2193 a76c42e6 2019-08-03 stsp
2194 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2195 708d8e67 2019-03-27 stsp == -1)
2196 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2197 708d8e67 2019-03-27 stsp
2198 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2199 708d8e67 2019-03-27 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 708d8e67 2019-03-27 stsp
2202 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2203 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2204 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2205 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2206 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2207 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2208 993e2a1b 2020-07-23 stsp goto done;
2209 993e2a1b 2020-07-23 stsp }
2210 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2211 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2212 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2213 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2214 993e2a1b 2020-07-23 stsp if (err)
2215 993e2a1b 2020-07-23 stsp goto done;
2216 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2217 993e2a1b 2020-07-23 stsp ie->path);
2218 993e2a1b 2020-07-23 stsp goto done;
2219 993e2a1b 2020-07-23 stsp }
2220 993e2a1b 2020-07-23 stsp
2221 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2222 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2223 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2224 1ee397ad 2019-07-12 stsp if (err)
2225 5a58a424 2020-06-23 stsp goto done;
2226 fc6346c4 2019-03-27 stsp /*
2227 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2228 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2229 fc6346c4 2019-03-27 stsp */
2230 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2231 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2232 fc6346c4 2019-03-27 stsp } else {
2233 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2234 1ee397ad 2019-07-12 stsp if (err)
2235 5a58a424 2020-06-23 stsp goto done;
2236 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2237 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2238 fc6346c4 2019-03-27 stsp if (err)
2239 5a58a424 2020-06-23 stsp goto done;
2240 fc6346c4 2019-03-27 stsp }
2241 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2242 708d8e67 2019-03-27 stsp }
2243 5a58a424 2020-06-23 stsp done:
2244 5a58a424 2020-06-23 stsp free(ondisk_path);
2245 fc6346c4 2019-03-27 stsp return err;
2246 708d8e67 2019-03-27 stsp }
2247 708d8e67 2019-03-27 stsp
2248 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2249 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2250 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2251 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2252 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2253 8da9e5f4 2019-01-12 stsp void *progress_arg;
2254 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2255 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2256 8da9e5f4 2019-01-12 stsp };
2257 8da9e5f4 2019-01-12 stsp
2258 512f0d0e 2019-01-02 stsp static const struct got_error *
2259 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2260 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2261 512f0d0e 2019-01-02 stsp {
2262 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2263 0584f854 2019-04-06 stsp
2264 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2266 512f0d0e 2019-01-02 stsp
2267 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2268 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2269 8da9e5f4 2019-01-12 stsp }
2270 512f0d0e 2019-01-02 stsp
2271 8da9e5f4 2019-01-12 stsp static const struct got_error *
2272 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2273 8da9e5f4 2019-01-12 stsp {
2274 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2275 7a9df742 2019-01-08 stsp
2276 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2277 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2278 0584f854 2019-04-06 stsp
2279 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2280 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2281 9d31a1d8 2018-03-11 stsp }
2282 9d31a1d8 2018-03-11 stsp
2283 9d31a1d8 2018-03-11 stsp static const struct got_error *
2284 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2285 9d31a1d8 2018-03-11 stsp {
2286 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2287 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2288 8da9e5f4 2019-01-12 stsp char *path;
2289 9d31a1d8 2018-03-11 stsp
2290 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2292 0584f854 2019-04-06 stsp
2293 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2294 63c5ca5d 2019-08-24 stsp return NULL;
2295 63c5ca5d 2019-08-24 stsp
2296 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2297 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2298 8da9e5f4 2019-01-12 stsp == -1)
2299 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2300 9d31a1d8 2018-03-11 stsp
2301 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2302 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2303 8da9e5f4 2019-01-12 stsp else
2304 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2305 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2306 9d31a1d8 2018-03-11 stsp
2307 8da9e5f4 2019-01-12 stsp free(path);
2308 0cd1c46a 2019-03-11 stsp return err;
2309 0cd1c46a 2019-03-11 stsp }
2310 0cd1c46a 2019-03-11 stsp
2311 b2118c49 2020-07-28 stsp const struct got_error *
2312 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2313 b2118c49 2020-07-28 stsp {
2314 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2315 b2118c49 2020-07-28 stsp
2316 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2317 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2318 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2319 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2320 b2118c49 2020-07-28 stsp }
2321 b2118c49 2020-07-28 stsp
2322 b2118c49 2020-07-28 stsp return NULL;
2323 b2118c49 2020-07-28 stsp }
2324 b2118c49 2020-07-28 stsp
2325 818c7501 2019-07-11 stsp static const struct got_error *
2326 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2327 0cd1c46a 2019-03-11 stsp {
2328 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2329 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2330 0cd1c46a 2019-03-11 stsp
2331 517bab73 2019-03-11 stsp *refname = NULL;
2332 517bab73 2019-03-11 stsp
2333 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2334 b2118c49 2020-07-28 stsp if (err)
2335 b2118c49 2020-07-28 stsp return err;
2336 0cd1c46a 2019-03-11 stsp
2337 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2338 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2339 0647c563 2019-03-11 stsp *refname = NULL;
2340 0cd1c46a 2019-03-11 stsp }
2341 517bab73 2019-03-11 stsp free(uuidstr);
2342 517bab73 2019-03-11 stsp return err;
2343 517bab73 2019-03-11 stsp }
2344 517bab73 2019-03-11 stsp
2345 818c7501 2019-07-11 stsp const struct got_error *
2346 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2347 f6cd0243 2023-01-28 thomas const char *prefix)
2348 f6cd0243 2023-01-28 thomas {
2349 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2350 f6cd0243 2023-01-28 thomas }
2351 f6cd0243 2023-01-28 thomas
2352 f6cd0243 2023-01-28 thomas const struct got_error *
2353 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2354 818c7501 2019-07-11 stsp {
2355 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2356 818c7501 2019-07-11 stsp }
2357 818c7501 2019-07-11 stsp
2358 818c7501 2019-07-11 stsp static const struct got_error *
2359 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2360 818c7501 2019-07-11 stsp {
2361 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2362 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2363 818c7501 2019-07-11 stsp }
2364 818c7501 2019-07-11 stsp
2365 818c7501 2019-07-11 stsp static const struct got_error *
2366 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2367 818c7501 2019-07-11 stsp {
2368 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2369 818c7501 2019-07-11 stsp }
2370 818c7501 2019-07-11 stsp
2371 818c7501 2019-07-11 stsp static const struct got_error *
2372 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2373 818c7501 2019-07-11 stsp {
2374 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2375 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2376 818c7501 2019-07-11 stsp }
2377 818c7501 2019-07-11 stsp
2378 818c7501 2019-07-11 stsp static const struct got_error *
2379 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 818c7501 2019-07-11 stsp {
2381 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2382 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2383 0ebf8283 2019-07-24 stsp }
2384 0ebf8283 2019-07-24 stsp
2385 0ebf8283 2019-07-24 stsp static const struct got_error *
2386 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2387 0ebf8283 2019-07-24 stsp {
2388 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2389 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2390 0ebf8283 2019-07-24 stsp }
2391 0ebf8283 2019-07-24 stsp
2392 0ebf8283 2019-07-24 stsp static const struct got_error *
2393 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2394 0ebf8283 2019-07-24 stsp {
2395 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2396 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2397 0ebf8283 2019-07-24 stsp }
2398 0ebf8283 2019-07-24 stsp
2399 0ebf8283 2019-07-24 stsp static const struct got_error *
2400 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2401 0ebf8283 2019-07-24 stsp {
2402 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2403 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2404 818c7501 2019-07-11 stsp }
2405 818c7501 2019-07-11 stsp
2406 0ebf8283 2019-07-24 stsp static const struct got_error *
2407 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2408 0ebf8283 2019-07-24 stsp {
2409 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2410 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2411 0ebf8283 2019-07-24 stsp }
2412 818c7501 2019-07-11 stsp
2413 0ebf8283 2019-07-24 stsp const struct got_error *
2414 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2415 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2416 0ebf8283 2019-07-24 stsp {
2417 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2418 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2419 0ebf8283 2019-07-24 stsp *path = NULL;
2420 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2421 0ebf8283 2019-07-24 stsp }
2422 0ebf8283 2019-07-24 stsp return NULL;
2423 10604dce 2021-09-24 thomas }
2424 10604dce 2021-09-24 thomas
2425 10604dce 2021-09-24 thomas static const struct got_error *
2426 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2427 10604dce 2021-09-24 thomas {
2428 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2429 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2430 0ebf8283 2019-07-24 stsp }
2431 0ebf8283 2019-07-24 stsp
2432 10604dce 2021-09-24 thomas static const struct got_error *
2433 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 10604dce 2021-09-24 thomas {
2435 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2436 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2437 10604dce 2021-09-24 thomas }
2438 10604dce 2021-09-24 thomas
2439 517bab73 2019-03-11 stsp /*
2440 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2441 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2442 517bab73 2019-03-11 stsp */
2443 517bab73 2019-03-11 stsp static const struct got_error *
2444 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2445 517bab73 2019-03-11 stsp {
2446 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2447 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2448 517bab73 2019-03-11 stsp char *refname;
2449 517bab73 2019-03-11 stsp
2450 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2451 517bab73 2019-03-11 stsp if (err)
2452 517bab73 2019-03-11 stsp return err;
2453 0cd1c46a 2019-03-11 stsp
2454 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2455 0cd1c46a 2019-03-11 stsp if (err)
2456 0cd1c46a 2019-03-11 stsp goto done;
2457 0cd1c46a 2019-03-11 stsp
2458 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2459 0cd1c46a 2019-03-11 stsp done:
2460 0cd1c46a 2019-03-11 stsp free(refname);
2461 0cd1c46a 2019-03-11 stsp if (ref)
2462 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2463 3e3a69f1 2019-07-25 stsp return err;
2464 3e3a69f1 2019-07-25 stsp }
2465 3e3a69f1 2019-07-25 stsp
2466 3e3a69f1 2019-07-25 stsp static const struct got_error *
2467 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2468 3e3a69f1 2019-07-25 stsp {
2469 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2470 3e3a69f1 2019-07-25 stsp
2471 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2472 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2473 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2474 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2475 3e3a69f1 2019-07-25 stsp }
2476 9d31a1d8 2018-03-11 stsp return err;
2477 9d31a1d8 2018-03-11 stsp }
2478 9d31a1d8 2018-03-11 stsp
2479 3e3a69f1 2019-07-25 stsp
2480 ebf99748 2019-05-09 stsp static const struct got_error *
2481 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2482 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2483 ebf99748 2019-05-09 stsp {
2484 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2485 ebf99748 2019-05-09 stsp FILE *index = NULL;
2486 0cd1c46a 2019-03-11 stsp
2487 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2488 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2489 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2490 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2491 ebf99748 2019-05-09 stsp
2492 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2493 3e3a69f1 2019-07-25 stsp if (err)
2494 ebf99748 2019-05-09 stsp goto done;
2495 ebf99748 2019-05-09 stsp
2496 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2497 ebf99748 2019-05-09 stsp if (index == NULL) {
2498 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2499 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2500 ebf99748 2019-05-09 stsp } else {
2501 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2502 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2503 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2504 ebf99748 2019-05-09 stsp }
2505 ebf99748 2019-05-09 stsp done:
2506 ebf99748 2019-05-09 stsp if (err) {
2507 ebf99748 2019-05-09 stsp free(*fileindex_path);
2508 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2509 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2510 ebf99748 2019-05-09 stsp *fileindex = NULL;
2511 ebf99748 2019-05-09 stsp }
2512 ebf99748 2019-05-09 stsp return err;
2513 ebf99748 2019-05-09 stsp }
2514 c932eeeb 2019-05-22 stsp
2515 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2516 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2517 c932eeeb 2019-05-22 stsp const char *path;
2518 c932eeeb 2019-05-22 stsp size_t path_len;
2519 c932eeeb 2019-05-22 stsp const char *entry_name;
2520 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2521 a484d721 2019-06-10 stsp void *progress_arg;
2522 c932eeeb 2019-05-22 stsp };
2523 ebf99748 2019-05-09 stsp
2524 c932eeeb 2019-05-22 stsp static const struct got_error *
2525 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2526 c932eeeb 2019-05-22 stsp {
2527 1ee397ad 2019-07-12 stsp const struct got_error *err;
2528 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2529 c932eeeb 2019-05-22 stsp
2530 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2531 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2532 c932eeeb 2019-05-22 stsp return NULL;
2533 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2534 102ff934 2019-06-11 stsp return NULL;
2535 102ff934 2019-06-11 stsp
2536 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2537 a769b60b 2021-06-27 stsp return NULL;
2538 a769b60b 2021-06-27 stsp
2539 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2540 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2541 c932eeeb 2019-05-22 stsp return NULL;
2542 c932eeeb 2019-05-22 stsp
2543 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2544 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2545 edd02c5e 2019-07-11 stsp ie->path);
2546 1ee397ad 2019-07-12 stsp if (err)
2547 1ee397ad 2019-07-12 stsp return err;
2548 1ee397ad 2019-07-12 stsp }
2549 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2550 c932eeeb 2019-05-22 stsp return NULL;
2551 a615e0e7 2020-12-16 stsp }
2552 a615e0e7 2020-12-16 stsp
2553 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2554 a615e0e7 2020-12-16 stsp static const struct got_error *
2555 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2556 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2557 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2558 a615e0e7 2020-12-16 stsp {
2559 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2560 a615e0e7 2020-12-16 stsp
2561 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2562 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2563 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2564 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2565 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2566 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2567 a615e0e7 2020-12-16 stsp
2568 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2569 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2570 9c6338c4 2019-06-02 stsp }
2571 9c6338c4 2019-06-02 stsp
2572 9c6338c4 2019-06-02 stsp static const struct got_error *
2573 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2574 9c6338c4 2019-06-02 stsp {
2575 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2576 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2577 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2578 867630bb 2020-01-17 stsp struct timespec timeout;
2579 9c6338c4 2019-06-02 stsp
2580 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2581 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2582 9c6338c4 2019-06-02 stsp if (err)
2583 9c6338c4 2019-06-02 stsp goto done;
2584 9c6338c4 2019-06-02 stsp
2585 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2586 9c6338c4 2019-06-02 stsp if (err)
2587 9c6338c4 2019-06-02 stsp goto done;
2588 9c6338c4 2019-06-02 stsp
2589 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2590 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2591 9c6338c4 2019-06-02 stsp fileindex_path);
2592 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2593 9c6338c4 2019-06-02 stsp }
2594 867630bb 2020-01-17 stsp
2595 867630bb 2020-01-17 stsp /*
2596 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2597 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2598 867630bb 2020-01-17 stsp * was recorded in the file index.
2599 867630bb 2020-01-17 stsp */
2600 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2601 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2602 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2603 9c6338c4 2019-06-02 stsp done:
2604 9c6338c4 2019-06-02 stsp if (new_index)
2605 9c6338c4 2019-06-02 stsp fclose(new_index);
2606 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2607 9c6338c4 2019-06-02 stsp return err;
2608 c932eeeb 2019-05-22 stsp }
2609 6ced4176 2019-07-12 stsp
2610 6ced4176 2019-07-12 stsp static const struct got_error *
2611 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2612 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2613 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2614 945f9229 2022-04-16 thomas struct got_repository *repo)
2615 6ced4176 2019-07-12 stsp {
2616 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2617 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2618 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2619 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2620 6ced4176 2019-07-12 stsp
2621 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2622 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2623 6ced4176 2019-07-12 stsp *tree_id = NULL;
2624 6ced4176 2019-07-12 stsp
2625 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2626 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2627 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2628 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2629 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2630 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2631 6ced4176 2019-07-12 stsp goto done;
2632 6ced4176 2019-07-12 stsp }
2633 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2634 945f9229 2022-04-16 thomas worktree->path_prefix);
2635 6ced4176 2019-07-12 stsp if (err)
2636 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp return NULL;
2638 6ced4176 2019-07-12 stsp }
2639 6ced4176 2019-07-12 stsp
2640 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2641 6ced4176 2019-07-12 stsp
2642 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2643 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2644 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2645 6ced4176 2019-07-12 stsp goto done;
2646 6ced4176 2019-07-12 stsp }
2647 6ced4176 2019-07-12 stsp
2648 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2649 6ced4176 2019-07-12 stsp if (err)
2650 6ced4176 2019-07-12 stsp goto done;
2651 6ced4176 2019-07-12 stsp
2652 6ced4176 2019-07-12 stsp free(in_repo_path);
2653 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2654 6ced4176 2019-07-12 stsp
2655 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2656 6ced4176 2019-07-12 stsp if (err)
2657 6ced4176 2019-07-12 stsp goto done;
2658 c932eeeb 2019-05-22 stsp
2659 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2660 6ced4176 2019-07-12 stsp /* Check out a single file. */
2661 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2662 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2663 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2664 6ced4176 2019-07-12 stsp if (in_repo_path == 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 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2669 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2670 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2671 6ced4176 2019-07-12 stsp goto done;
2672 6ced4176 2019-07-12 stsp }
2673 6ced4176 2019-07-12 stsp } else {
2674 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2675 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2676 6ced4176 2019-07-12 stsp if (err)
2677 6ced4176 2019-07-12 stsp return err;
2678 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2679 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2680 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2681 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2682 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp }
2684 6ced4176 2019-07-12 stsp }
2685 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2686 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2687 6ced4176 2019-07-12 stsp } else {
2688 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2689 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2690 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2691 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2692 6ced4176 2019-07-12 stsp goto done;
2693 6ced4176 2019-07-12 stsp }
2694 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2695 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2696 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2697 6ced4176 2019-07-12 stsp goto done;
2698 6ced4176 2019-07-12 stsp }
2699 6ced4176 2019-07-12 stsp }
2700 6ced4176 2019-07-12 stsp done:
2701 6ced4176 2019-07-12 stsp free(id);
2702 6ced4176 2019-07-12 stsp free(in_repo_path);
2703 6ced4176 2019-07-12 stsp if (err) {
2704 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2705 6ced4176 2019-07-12 stsp free(*tree_relpath);
2706 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2707 6ced4176 2019-07-12 stsp free(*tree_id);
2708 6ced4176 2019-07-12 stsp *tree_id = NULL;
2709 6ced4176 2019-07-12 stsp }
2710 07ed472d 2019-07-12 stsp return err;
2711 07ed472d 2019-07-12 stsp }
2712 07ed472d 2019-07-12 stsp
2713 07ed472d 2019-07-12 stsp static const struct got_error *
2714 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2715 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2716 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2717 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2718 07ed472d 2019-07-12 stsp {
2719 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2720 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2721 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2722 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2723 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2724 07ed472d 2019-07-12 stsp
2725 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2726 7f47418f 2019-12-20 stsp if (err) {
2727 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2728 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2729 7f47418f 2019-12-20 stsp goto done;
2730 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2731 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2732 7f47418f 2019-12-20 stsp if (err)
2733 7f47418f 2019-12-20 stsp return err;
2734 7f47418f 2019-12-20 stsp }
2735 07ed472d 2019-07-12 stsp
2736 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2737 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2738 07ed472d 2019-07-12 stsp if (err)
2739 07ed472d 2019-07-12 stsp goto done;
2740 07ed472d 2019-07-12 stsp
2741 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2742 07ed472d 2019-07-12 stsp if (err)
2743 07ed472d 2019-07-12 stsp goto done;
2744 07ed472d 2019-07-12 stsp
2745 07ed472d 2019-07-12 stsp if (entry_name &&
2746 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2747 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2748 07ed472d 2019-07-12 stsp goto done;
2749 07ed472d 2019-07-12 stsp }
2750 07ed472d 2019-07-12 stsp
2751 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2752 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2753 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2754 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2755 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2756 07ed472d 2019-07-12 stsp arg.repo = repo;
2757 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2758 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2759 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2760 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2761 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2762 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2763 07ed472d 2019-07-12 stsp done:
2764 07ed472d 2019-07-12 stsp if (tree)
2765 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2766 07ed472d 2019-07-12 stsp if (commit)
2767 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2768 6ced4176 2019-07-12 stsp return err;
2769 6ced4176 2019-07-12 stsp }
2770 6ced4176 2019-07-12 stsp
2771 9d31a1d8 2018-03-11 stsp const struct got_error *
2772 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2773 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2774 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2775 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2776 9d31a1d8 2018-03-11 stsp {
2777 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2778 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2779 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2780 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2781 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2782 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2783 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2784 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2785 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2786 f2ea84fa 2019-07-27 stsp int entry_type;
2787 f2ea84fa 2019-07-27 stsp char *relpath;
2788 f2ea84fa 2019-07-27 stsp char *entry_name;
2789 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2790 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2791 9d31a1d8 2018-03-11 stsp
2792 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2793 f2ea84fa 2019-07-27 stsp
2794 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2795 9d31a1d8 2018-03-11 stsp if (err)
2796 9d31a1d8 2018-03-11 stsp return err;
2797 945f9229 2022-04-16 thomas
2798 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2799 945f9229 2022-04-16 thomas worktree->base_commit_id);
2800 945f9229 2022-04-16 thomas if (err)
2801 945f9229 2022-04-16 thomas goto done;
2802 93a30277 2018-12-24 stsp
2803 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2804 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2805 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2806 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2807 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2808 f2ea84fa 2019-07-27 stsp goto done;
2809 f2ea84fa 2019-07-27 stsp }
2810 6ced4176 2019-07-12 stsp
2811 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2812 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2813 945f9229 2022-04-16 thomas worktree, repo);
2814 f2ea84fa 2019-07-27 stsp if (err) {
2815 f2ea84fa 2019-07-27 stsp free(tpd);
2816 6ced4176 2019-07-12 stsp goto done;
2817 6ced4176 2019-07-12 stsp }
2818 f2ea84fa 2019-07-27 stsp
2819 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2820 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2821 f2ea84fa 2019-07-27 stsp if (err) {
2822 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2823 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2824 f2ea84fa 2019-07-27 stsp free(tpd);
2825 f2ea84fa 2019-07-27 stsp goto done;
2826 f2ea84fa 2019-07-27 stsp }
2827 f2ea84fa 2019-07-27 stsp } else
2828 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2829 f2ea84fa 2019-07-27 stsp
2830 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2831 6ced4176 2019-07-12 stsp }
2832 6ced4176 2019-07-12 stsp
2833 51514078 2018-12-25 stsp /*
2834 51514078 2018-12-25 stsp * Read the file index.
2835 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2836 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2837 51514078 2018-12-25 stsp */
2838 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2839 8da9e5f4 2019-01-12 stsp if (err)
2840 c4cdcb68 2019-04-03 stsp goto done;
2841 c4cdcb68 2019-04-03 stsp
2842 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2843 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2844 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2845 f2ea84fa 2019-07-27 stsp
2846 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2847 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2848 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 f2ea84fa 2019-07-27 stsp if (err)
2850 f2ea84fa 2019-07-27 stsp break;
2851 f2ea84fa 2019-07-27 stsp
2852 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2853 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2854 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2855 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2856 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2857 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2858 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2859 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2860 f2ea84fa 2019-07-27 stsp if (err)
2861 f2ea84fa 2019-07-27 stsp break;
2862 f2ea84fa 2019-07-27 stsp
2863 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2864 711d9cd9 2019-07-12 stsp }
2865 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2866 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2867 af12c6b9 2019-06-04 stsp err = sync_err;
2868 9d31a1d8 2018-03-11 stsp done:
2869 9c6338c4 2019-06-02 stsp free(fileindex_path);
2870 edfa7d7f 2018-09-11 stsp if (tree)
2871 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2872 9d31a1d8 2018-03-11 stsp if (commit)
2873 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2874 5ade8233 2019-07-12 stsp if (fileindex)
2875 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2876 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2877 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2878 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2879 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2880 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2881 f2ea84fa 2019-07-27 stsp free(tpd);
2882 f2ea84fa 2019-07-27 stsp }
2883 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2884 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2885 9d31a1d8 2018-03-11 stsp err = unlockerr;
2886 f8d1f275 2019-02-04 stsp return err;
2887 f8d1f275 2019-02-04 stsp }
2888 f8d1f275 2019-02-04 stsp
2889 0529f8df 2023-03-10 thomas static const struct got_error *
2890 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2891 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2892 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2893 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2894 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2895 0529f8df 2023-03-10 thomas struct got_repository *repo,
2896 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2897 0529f8df 2023-03-10 thomas {
2898 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2899 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2900 0529f8df 2023-03-10 thomas
2901 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2902 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2903 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2904 0529f8df 2023-03-10 thomas restoring_missing_file,
2905 0529f8df 2023-03-10 thomas reverting_versioned_file,
2906 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2907 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2908 0529f8df 2023-03-10 thomas } else {
2909 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2910 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2911 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2912 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2913 0529f8df 2023-03-10 thomas }
2914 0529f8df 2023-03-10 thomas if (err)
2915 0529f8df 2023-03-10 thomas return err;
2916 0529f8df 2023-03-10 thomas if (ie == NULL) {
2917 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2918 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2919 0529f8df 2023-03-10 thomas if (err)
2920 0529f8df 2023-03-10 thomas return err;
2921 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2922 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2923 0529f8df 2023-03-10 thomas if (err) {
2924 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2925 0529f8df 2023-03-10 thomas return err;
2926 0529f8df 2023-03-10 thomas }
2927 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2928 0529f8df 2023-03-10 thomas if (err) {
2929 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2930 0529f8df 2023-03-10 thomas return err;
2931 0529f8df 2023-03-10 thomas }
2932 0529f8df 2023-03-10 thomas } else {
2933 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2934 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2935 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2936 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2937 0529f8df 2023-03-10 thomas if (err)
2938 0529f8df 2023-03-10 thomas return err;
2939 0529f8df 2023-03-10 thomas }
2940 0529f8df 2023-03-10 thomas
2941 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2942 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2943 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2944 0529f8df 2023-03-10 thomas }
2945 0529f8df 2023-03-10 thomas
2946 0529f8df 2023-03-10 thomas return NULL;
2947 0529f8df 2023-03-10 thomas }
2948 0529f8df 2023-03-10 thomas
2949 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2950 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2951 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2952 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2953 234035bc 2019-06-01 stsp void *progress_arg;
2954 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2955 234035bc 2019-06-01 stsp void *cancel_arg;
2956 f69721c3 2019-10-21 stsp const char *label_orig;
2957 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2958 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2959 234035bc 2019-06-01 stsp };
2960 234035bc 2019-06-01 stsp
2961 234035bc 2019-06-01 stsp static const struct got_error *
2962 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2963 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2964 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2965 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2966 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2967 234035bc 2019-06-01 stsp {
2968 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2969 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2970 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2971 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2972 234035bc 2019-06-01 stsp struct stat sb;
2973 234035bc 2019-06-01 stsp unsigned char status;
2974 234035bc 2019-06-01 stsp int local_changes_subsumed;
2975 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2976 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2977 234035bc 2019-06-01 stsp
2978 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2979 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2980 d6c87207 2019-08-02 stsp strlen(path2));
2981 1ee397ad 2019-07-12 stsp if (ie == NULL)
2982 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2983 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2984 234035bc 2019-06-01 stsp
2985 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 234035bc 2019-06-01 stsp path2) == -1)
2987 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2988 234035bc 2019-06-01 stsp
2989 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2990 7f91a133 2019-12-13 stsp repo);
2991 234035bc 2019-06-01 stsp if (err)
2992 234035bc 2019-06-01 stsp goto done;
2993 234035bc 2019-06-01 stsp
2994 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2995 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2996 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2997 234035bc 2019-06-01 stsp goto done;
2998 234035bc 2019-06-01 stsp }
2999 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3000 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3001 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3002 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3003 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3004 234035bc 2019-06-01 stsp goto done;
3005 234035bc 2019-06-01 stsp }
3006 234035bc 2019-06-01 stsp
3007 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3008 36bf999c 2020-07-23 stsp char *link_target2;
3009 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3010 36bf999c 2020-07-23 stsp if (err)
3011 36bf999c 2020-07-23 stsp goto done;
3012 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3013 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3014 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3015 36bf999c 2020-07-23 stsp free(link_target2);
3016 af57b12a 2020-07-23 stsp } else {
3017 1af628f4 2021-06-03 stsp int fd;
3018 1af628f4 2021-06-03 stsp
3019 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3020 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3021 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3022 1af628f4 2021-06-03 stsp goto done;
3023 1af628f4 2021-06-03 stsp }
3024 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3025 1af628f4 2021-06-03 stsp f_orig, blob1);
3026 1af628f4 2021-06-03 stsp if (err)
3027 1af628f4 2021-06-03 stsp goto done;
3028 1af628f4 2021-06-03 stsp
3029 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3030 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3031 1af628f4 2021-06-03 stsp goto done;
3032 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3033 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3034 1af628f4 2021-06-03 stsp if (err)
3035 1af628f4 2021-06-03 stsp goto done;
3036 1af628f4 2021-06-03 stsp
3037 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3038 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3039 1af628f4 2021-06-03 stsp if (fd == -1) {
3040 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3041 1af628f4 2021-06-03 stsp ondisk_path);
3042 1af628f4 2021-06-03 stsp goto done;
3043 1af628f4 2021-06-03 stsp }
3044 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3045 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3046 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3047 1af628f4 2021-06-03 stsp ondisk_path);
3048 1af628f4 2021-06-03 stsp close(fd);
3049 1af628f4 2021-06-03 stsp goto done;
3050 1af628f4 2021-06-03 stsp }
3051 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3052 1af628f4 2021-06-03 stsp if (err)
3053 1af628f4 2021-06-03 stsp goto done;
3054 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3055 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3056 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3057 1af628f4 2021-06-03 stsp goto done;
3058 1af628f4 2021-06-03 stsp }
3059 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3060 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3061 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3062 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3063 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3064 af57b12a 2020-07-23 stsp }
3065 234035bc 2019-06-01 stsp } else if (blob1) {
3066 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3067 d6c87207 2019-08-02 stsp strlen(path1));
3068 1ee397ad 2019-07-12 stsp if (ie == NULL)
3069 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3070 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3071 2b92fad7 2019-06-02 stsp
3072 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3073 2b92fad7 2019-06-02 stsp path1) == -1)
3074 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3075 2b92fad7 2019-06-02 stsp
3076 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3077 7f91a133 2019-12-13 stsp repo);
3078 2b92fad7 2019-06-02 stsp if (err)
3079 2b92fad7 2019-06-02 stsp goto done;
3080 2b92fad7 2019-06-02 stsp
3081 2b92fad7 2019-06-02 stsp switch (status) {
3082 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3083 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3084 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3085 1ee397ad 2019-07-12 stsp if (err)
3086 1ee397ad 2019-07-12 stsp goto done;
3087 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3088 2b92fad7 2019-06-02 stsp if (err)
3089 2b92fad7 2019-06-02 stsp goto done;
3090 2b92fad7 2019-06-02 stsp if (ie)
3091 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3092 2b92fad7 2019-06-02 stsp break;
3093 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3094 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3095 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3096 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3097 1ee397ad 2019-07-12 stsp if (err)
3098 1ee397ad 2019-07-12 stsp goto done;
3099 2b92fad7 2019-06-02 stsp if (ie)
3100 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3101 2b92fad7 2019-06-02 stsp break;
3102 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3103 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3104 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3105 e8f02263 2022-01-23 thomas off_t blob1_size;
3106 0a22ca1a 2020-09-23 stsp /*
3107 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3108 0a22ca1a 2020-09-23 stsp * exists in the repository.
3109 0a22ca1a 2020-09-23 stsp */
3110 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3111 e8f02263 2022-01-23 thomas &blob1_size, path1);
3112 0a22ca1a 2020-09-23 stsp if (err)
3113 0a22ca1a 2020-09-23 stsp goto done;
3114 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3115 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3116 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3117 0a22ca1a 2020-09-23 stsp if (err)
3118 0a22ca1a 2020-09-23 stsp goto done;
3119 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3120 0a22ca1a 2020-09-23 stsp path1);
3121 0a22ca1a 2020-09-23 stsp if (err)
3122 0a22ca1a 2020-09-23 stsp goto done;
3123 0a22ca1a 2020-09-23 stsp if (ie)
3124 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3125 0a22ca1a 2020-09-23 stsp ie);
3126 0a22ca1a 2020-09-23 stsp } else {
3127 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3128 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3129 0a22ca1a 2020-09-23 stsp }
3130 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3131 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3132 0a22ca1a 2020-09-23 stsp free(id);
3133 0a22ca1a 2020-09-23 stsp if (err)
3134 0a22ca1a 2020-09-23 stsp goto done;
3135 0a22ca1a 2020-09-23 stsp break;
3136 0a22ca1a 2020-09-23 stsp }
3137 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3138 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3139 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3140 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3141 1ee397ad 2019-07-12 stsp if (err)
3142 1ee397ad 2019-07-12 stsp goto done;
3143 2b92fad7 2019-06-02 stsp break;
3144 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3145 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3146 1ee397ad 2019-07-12 stsp if (err)
3147 1ee397ad 2019-07-12 stsp goto done;
3148 2b92fad7 2019-06-02 stsp break;
3149 2b92fad7 2019-06-02 stsp default:
3150 2b92fad7 2019-06-02 stsp break;
3151 2b92fad7 2019-06-02 stsp }
3152 234035bc 2019-06-01 stsp } else if (blob2) {
3153 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3154 234035bc 2019-06-01 stsp path2) == -1)
3155 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3156 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3157 d6c87207 2019-08-02 stsp strlen(path2));
3158 234035bc 2019-06-01 stsp if (ie) {
3159 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3160 7f91a133 2019-12-13 stsp -1, NULL, repo);
3161 234035bc 2019-06-01 stsp if (err)
3162 234035bc 2019-06-01 stsp goto done;
3163 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3164 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3165 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3166 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3167 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3168 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3169 1ee397ad 2019-07-12 stsp status, path2);
3170 234035bc 2019-06-01 stsp goto done;
3171 234035bc 2019-06-01 stsp }
3172 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3173 36bf999c 2020-07-23 stsp char *link_target2;
3174 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3175 36bf999c 2020-07-23 stsp blob2);
3176 36bf999c 2020-07-23 stsp if (err)
3177 36bf999c 2020-07-23 stsp goto done;
3178 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3179 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3180 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3181 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3182 36bf999c 2020-07-23 stsp free(link_target2);
3183 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3184 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3185 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3186 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3187 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3188 526a746f 2020-07-23 stsp a->progress_arg);
3189 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3190 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3191 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3192 526a746f 2020-07-23 stsp }
3193 dfe9fba0 2020-07-23 stsp if (err)
3194 dfe9fba0 2020-07-23 stsp goto done;
3195 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3196 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3197 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3198 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3199 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3200 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3201 234035bc 2019-06-01 stsp if (err)
3202 234035bc 2019-06-01 stsp goto done;
3203 234035bc 2019-06-01 stsp }
3204 234035bc 2019-06-01 stsp } else {
3205 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3206 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3207 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3208 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3209 234035bc 2019-06-01 stsp if (err)
3210 2b92fad7 2019-06-02 stsp goto done;
3211 234035bc 2019-06-01 stsp }
3212 234035bc 2019-06-01 stsp }
3213 234035bc 2019-06-01 stsp done:
3214 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3215 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3216 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3217 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3218 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3219 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3220 1af628f4 2021-06-03 stsp free(id_str);
3221 54d5be07 2021-06-03 stsp free(label_deriv2);
3222 234035bc 2019-06-01 stsp free(ondisk_path);
3223 234035bc 2019-06-01 stsp return err;
3224 234035bc 2019-06-01 stsp }
3225 234035bc 2019-06-01 stsp
3226 69de9dd4 2021-09-03 stsp static const struct got_error *
3227 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3228 69de9dd4 2021-09-03 stsp {
3229 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3230 69de9dd4 2021-09-03 stsp
3231 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3232 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3233 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3234 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3235 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3236 69de9dd4 2021-09-03 stsp
3237 69de9dd4 2021-09-03 stsp return NULL;
3238 69de9dd4 2021-09-03 stsp }
3239 69de9dd4 2021-09-03 stsp
3240 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3241 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3242 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3243 234035bc 2019-06-01 stsp struct got_repository *repo;
3244 234035bc 2019-06-01 stsp };
3245 234035bc 2019-06-01 stsp
3246 234035bc 2019-06-01 stsp static const struct got_error *
3247 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3248 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3249 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3250 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3251 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3252 234035bc 2019-06-01 stsp {
3253 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3254 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3255 234035bc 2019-06-01 stsp unsigned char status;
3256 234035bc 2019-06-01 stsp struct stat sb;
3257 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3258 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3259 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3260 234035bc 2019-06-01 stsp char *ondisk_path;
3261 234035bc 2019-06-01 stsp
3262 69de9dd4 2021-09-03 stsp if (id == NULL)
3263 69de9dd4 2021-09-03 stsp return NULL;
3264 234035bc 2019-06-01 stsp
3265 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3266 69de9dd4 2021-09-03 stsp if (ie == NULL)
3267 69de9dd4 2021-09-03 stsp return NULL;
3268 69de9dd4 2021-09-03 stsp
3269 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3270 234035bc 2019-06-01 stsp == -1)
3271 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3272 234035bc 2019-06-01 stsp
3273 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3274 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3275 5546d466 2021-09-02 stsp free(ondisk_path);
3276 234035bc 2019-06-01 stsp if (err)
3277 234035bc 2019-06-01 stsp return err;
3278 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3279 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3280 234035bc 2019-06-01 stsp
3281 234035bc 2019-06-01 stsp return NULL;
3282 234035bc 2019-06-01 stsp }
3283 234035bc 2019-06-01 stsp
3284 818c7501 2019-07-11 stsp static const struct got_error *
3285 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3286 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3287 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3288 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3290 234035bc 2019-06-01 stsp {
3291 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3292 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3293 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3294 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3295 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3296 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3297 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3298 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3299 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3300 234035bc 2019-06-01 stsp
3301 03415a1a 2019-06-02 stsp if (commit_id1) {
3302 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3303 945f9229 2022-04-16 thomas if (err)
3304 945f9229 2022-04-16 thomas goto done;
3305 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3306 03415a1a 2019-06-02 stsp worktree->path_prefix);
3307 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3308 03415a1a 2019-06-02 stsp goto done;
3309 69d57f3d 2020-07-31 stsp }
3310 69d57f3d 2020-07-31 stsp if (tree_id1) {
3311 69d57f3d 2020-07-31 stsp char *id_str;
3312 03415a1a 2019-06-02 stsp
3313 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3314 03415a1a 2019-06-02 stsp if (err)
3315 03415a1a 2019-06-02 stsp goto done;
3316 f69721c3 2019-10-21 stsp
3317 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3318 f69721c3 2019-10-21 stsp if (err)
3319 f69721c3 2019-10-21 stsp goto done;
3320 f69721c3 2019-10-21 stsp
3321 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3322 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3323 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3324 f69721c3 2019-10-21 stsp free(id_str);
3325 f69721c3 2019-10-21 stsp goto done;
3326 f69721c3 2019-10-21 stsp }
3327 f69721c3 2019-10-21 stsp free(id_str);
3328 a0f32f33 2022-06-13 thomas
3329 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3330 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3331 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3332 a0f32f33 2022-06-13 thomas goto done;
3333 a0f32f33 2022-06-13 thomas }
3334 03415a1a 2019-06-02 stsp }
3335 234035bc 2019-06-01 stsp
3336 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3337 945f9229 2022-04-16 thomas if (err)
3338 945f9229 2022-04-16 thomas goto done;
3339 945f9229 2022-04-16 thomas
3340 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3341 234035bc 2019-06-01 stsp worktree->path_prefix);
3342 234035bc 2019-06-01 stsp if (err)
3343 234035bc 2019-06-01 stsp goto done;
3344 234035bc 2019-06-01 stsp
3345 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3346 234035bc 2019-06-01 stsp if (err)
3347 234035bc 2019-06-01 stsp goto done;
3348 234035bc 2019-06-01 stsp
3349 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3350 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3351 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3352 19a6a6b5 2022-07-01 thomas goto done;
3353 19a6a6b5 2022-07-01 thomas }
3354 19a6a6b5 2022-07-01 thomas
3355 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3356 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3357 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3358 a0f32f33 2022-06-13 thomas goto done;
3359 a0f32f33 2022-06-13 thomas }
3360 a0f32f33 2022-06-13 thomas
3361 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3362 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3363 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3364 19a6a6b5 2022-07-01 thomas goto done;
3365 19a6a6b5 2022-07-01 thomas }
3366 19a6a6b5 2022-07-01 thomas
3367 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3368 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3369 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3370 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3371 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3372 69de9dd4 2021-09-03 stsp if (err)
3373 69de9dd4 2021-09-03 stsp goto done;
3374 69de9dd4 2021-09-03 stsp
3375 234035bc 2019-06-01 stsp arg.worktree = worktree;
3376 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3377 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3378 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3379 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3380 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3381 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3382 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3383 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3384 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3385 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3386 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3387 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3388 af12c6b9 2019-06-04 stsp err = sync_err;
3389 234035bc 2019-06-01 stsp done:
3390 945f9229 2022-04-16 thomas if (commit1)
3391 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3392 945f9229 2022-04-16 thomas if (commit2)
3393 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3394 234035bc 2019-06-01 stsp if (tree1)
3395 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3396 234035bc 2019-06-01 stsp if (tree2)
3397 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3398 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3399 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3400 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3401 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3402 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3403 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3404 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3405 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3406 f69721c3 2019-10-21 stsp free(label_orig);
3407 818c7501 2019-07-11 stsp return err;
3408 818c7501 2019-07-11 stsp }
3409 234035bc 2019-06-01 stsp
3410 818c7501 2019-07-11 stsp const struct got_error *
3411 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3412 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3413 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3414 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3415 818c7501 2019-07-11 stsp {
3416 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3417 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3418 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3419 818c7501 2019-07-11 stsp
3420 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3421 818c7501 2019-07-11 stsp if (err)
3422 818c7501 2019-07-11 stsp return err;
3423 818c7501 2019-07-11 stsp
3424 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3425 818c7501 2019-07-11 stsp if (err)
3426 818c7501 2019-07-11 stsp goto done;
3427 818c7501 2019-07-11 stsp
3428 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3429 69de9dd4 2021-09-03 stsp worktree);
3430 818c7501 2019-07-11 stsp if (err)
3431 818c7501 2019-07-11 stsp goto done;
3432 818c7501 2019-07-11 stsp
3433 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3434 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3435 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3436 818c7501 2019-07-11 stsp done:
3437 818c7501 2019-07-11 stsp if (fileindex)
3438 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3439 818c7501 2019-07-11 stsp free(fileindex_path);
3440 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3441 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3442 234035bc 2019-06-01 stsp err = unlockerr;
3443 234035bc 2019-06-01 stsp return err;
3444 234035bc 2019-06-01 stsp }
3445 234035bc 2019-06-01 stsp
3446 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3447 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3448 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3449 927df6b7 2019-02-10 stsp const char *status_path;
3450 927df6b7 2019-02-10 stsp size_t status_path_len;
3451 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3452 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3453 f8d1f275 2019-02-04 stsp void *status_arg;
3454 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3455 f8d1f275 2019-02-04 stsp void *cancel_arg;
3456 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3457 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3458 f2a9dc41 2019-12-13 tracey int report_unchanged;
3459 3143d852 2020-06-25 stsp int no_ignores;
3460 f8d1f275 2019-02-04 stsp };
3461 88d0e355 2019-08-03 stsp
3462 f8d1f275 2019-02-04 stsp static const struct got_error *
3463 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3464 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3465 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3466 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3467 927df6b7 2019-02-10 stsp {
3468 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3469 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3470 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3471 927df6b7 2019-02-10 stsp struct stat sb;
3472 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3473 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3474 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3475 927df6b7 2019-02-10 stsp
3476 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3477 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3478 98eaaa12 2019-08-03 stsp if (err)
3479 98eaaa12 2019-08-03 stsp return err;
3480 98eaaa12 2019-08-03 stsp
3481 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3482 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3483 98eaaa12 2019-08-03 stsp return NULL;
3484 98eaaa12 2019-08-03 stsp
3485 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3486 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3487 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3488 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3489 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3490 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3491 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3492 43010591 2023-02-17 thomas &staged_blob_id, ie);
3493 98eaaa12 2019-08-03 stsp }
3494 98eaaa12 2019-08-03 stsp
3495 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3496 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3497 927df6b7 2019-02-10 stsp }
3498 927df6b7 2019-02-10 stsp
3499 927df6b7 2019-02-10 stsp static const struct got_error *
3500 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3501 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3502 f8d1f275 2019-02-04 stsp {
3503 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3504 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3505 f8d1f275 2019-02-04 stsp char *abspath;
3506 f8d1f275 2019-02-04 stsp
3507 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3509 0584f854 2019-04-06 stsp
3510 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3511 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3512 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3513 927df6b7 2019-02-10 stsp return NULL;
3514 927df6b7 2019-02-10 stsp
3515 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3516 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3517 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3518 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3519 f8d1f275 2019-02-04 stsp } else {
3520 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3521 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3522 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3523 f8d1f275 2019-02-04 stsp }
3524 f8d1f275 2019-02-04 stsp
3525 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3526 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3527 f8d1f275 2019-02-04 stsp free(abspath);
3528 9d31a1d8 2018-03-11 stsp return err;
3529 9d31a1d8 2018-03-11 stsp }
3530 f8d1f275 2019-02-04 stsp
3531 f8d1f275 2019-02-04 stsp static const struct got_error *
3532 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3533 f8d1f275 2019-02-04 stsp {
3534 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3535 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3536 2ec1f75b 2019-03-26 stsp unsigned char status;
3537 927df6b7 2019-02-10 stsp
3538 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3539 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3540 0584f854 2019-04-06 stsp
3541 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3542 927df6b7 2019-02-10 stsp return NULL;
3543 927df6b7 2019-02-10 stsp
3544 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3545 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3546 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3547 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3548 2ec1f75b 2019-03-26 stsp else
3549 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3550 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3551 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 6841da00 2019-08-08 stsp }
3553 6841da00 2019-08-08 stsp
3554 ef20f542 2022-06-26 thomas static void
3555 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3556 6841da00 2019-08-08 stsp {
3557 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3558 6841da00 2019-08-08 stsp
3559 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3560 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3561 21c2d8be 2023-01-10 thomas
3562 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3563 6841da00 2019-08-08 stsp }
3564 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp
3567 6841da00 2019-08-08 stsp static const struct got_error *
3568 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3569 6841da00 2019-08-08 stsp {
3570 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3571 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3572 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3573 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3574 6841da00 2019-08-08 stsp size_t linesize = 0;
3575 6841da00 2019-08-08 stsp ssize_t linelen;
3576 6841da00 2019-08-08 stsp
3577 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3578 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3579 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3580 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3581 6841da00 2019-08-08 stsp
3582 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3583 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3584 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3585 bd8de430 2019-10-04 stsp
3586 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3587 bd8de430 2019-10-04 stsp if (line[0] == '#')
3588 bd8de430 2019-10-04 stsp continue;
3589 bd8de430 2019-10-04 stsp
3590 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3591 bd8de430 2019-10-04 stsp if (line[0] == '!')
3592 bd8de430 2019-10-04 stsp continue;
3593 bd8de430 2019-10-04 stsp
3594 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3595 6841da00 2019-08-08 stsp line) == -1) {
3596 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3597 6841da00 2019-08-08 stsp goto done;
3598 6841da00 2019-08-08 stsp }
3599 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3600 6841da00 2019-08-08 stsp if (err)
3601 6841da00 2019-08-08 stsp goto done;
3602 6841da00 2019-08-08 stsp }
3603 6841da00 2019-08-08 stsp if (ferror(f)) {
3604 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3605 6841da00 2019-08-08 stsp goto done;
3606 6841da00 2019-08-08 stsp }
3607 6841da00 2019-08-08 stsp
3608 6841da00 2019-08-08 stsp dirpath = strdup(path);
3609 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3610 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3611 6841da00 2019-08-08 stsp goto done;
3612 6841da00 2019-08-08 stsp }
3613 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3614 6841da00 2019-08-08 stsp done:
3615 6841da00 2019-08-08 stsp free(line);
3616 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3617 6841da00 2019-08-08 stsp free(dirpath);
3618 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3619 6841da00 2019-08-08 stsp }
3620 6841da00 2019-08-08 stsp return err;
3621 1b5d300f 2023-02-20 thomas }
3622 1b5d300f 2023-02-20 thomas
3623 1b5d300f 2023-02-20 thomas static int
3624 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3625 1b5d300f 2023-02-20 thomas int flags)
3626 1b5d300f 2023-02-20 thomas {
3627 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3628 1b5d300f 2023-02-20 thomas
3629 1b5d300f 2023-02-20 thomas /*
3630 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3631 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3632 1b5d300f 2023-02-20 thomas */
3633 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3634 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3635 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3636 1b5d300f 2023-02-20 thomas
3637 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3638 1b5d300f 2023-02-20 thomas }
3639 1b5d300f 2023-02-20 thomas
3640 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3641 6841da00 2019-08-08 stsp }
3642 6841da00 2019-08-08 stsp
3643 ef20f542 2022-06-26 thomas static int
3644 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3645 6841da00 2019-08-08 stsp {
3646 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3647 bd8de430 2019-10-04 stsp
3648 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3649 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3650 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3651 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3652 bd8de430 2019-10-04 stsp
3653 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3654 1b5d300f 2023-02-20 thomas const char *p;
3655 6841da00 2019-08-08 stsp
3656 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3657 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3658 bd8de430 2019-10-04 stsp continue;
3659 bd8de430 2019-10-04 stsp p = path;
3660 bd8de430 2019-10-04 stsp while (*p) {
3661 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3662 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3663 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3664 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3665 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3666 bd8de430 2019-10-04 stsp p++;
3667 bd8de430 2019-10-04 stsp while (*p == '/')
3668 bd8de430 2019-10-04 stsp p++;
3669 bd8de430 2019-10-04 stsp continue;
3670 bd8de430 2019-10-04 stsp }
3671 bd8de430 2019-10-04 stsp return 1;
3672 bd8de430 2019-10-04 stsp }
3673 bd8de430 2019-10-04 stsp }
3674 bd8de430 2019-10-04 stsp }
3675 bd8de430 2019-10-04 stsp
3676 6841da00 2019-08-08 stsp /*
3677 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3678 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3679 6841da00 2019-08-08 stsp * ignores backwards.
3680 6841da00 2019-08-08 stsp */
3681 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3682 6841da00 2019-08-08 stsp while (pe) {
3683 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3684 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3685 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3686 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3687 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3688 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3689 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3690 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3691 1b5d300f 2023-02-20 thomas path, flags))
3692 6841da00 2019-08-08 stsp continue;
3693 6841da00 2019-08-08 stsp return 1;
3694 6841da00 2019-08-08 stsp }
3695 6841da00 2019-08-08 stsp }
3696 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3697 6841da00 2019-08-08 stsp }
3698 6841da00 2019-08-08 stsp
3699 6841da00 2019-08-08 stsp return 0;
3700 6841da00 2019-08-08 stsp }
3701 6841da00 2019-08-08 stsp
3702 6841da00 2019-08-08 stsp static const struct got_error *
3703 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3704 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3705 6841da00 2019-08-08 stsp {
3706 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3707 6841da00 2019-08-08 stsp char *ignorespath;
3708 886cec17 2019-12-15 stsp int fd = -1;
3709 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3710 6841da00 2019-08-08 stsp
3711 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3712 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3713 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3714 6841da00 2019-08-08 stsp
3715 886cec17 2019-12-15 stsp if (dirfd != -1) {
3716 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3717 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3718 886cec17 2019-12-15 stsp if (fd == -1) {
3719 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3720 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3721 886cec17 2019-12-15 stsp ignorespath);
3722 886cec17 2019-12-15 stsp } else {
3723 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3724 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3725 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3726 886cec17 2019-12-15 stsp ignorespath);
3727 886cec17 2019-12-15 stsp else {
3728 886cec17 2019-12-15 stsp fd = -1;
3729 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3730 886cec17 2019-12-15 stsp }
3731 886cec17 2019-12-15 stsp }
3732 886cec17 2019-12-15 stsp } else {
3733 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3734 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3735 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3736 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3737 886cec17 2019-12-15 stsp ignorespath);
3738 886cec17 2019-12-15 stsp } else
3739 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3740 886cec17 2019-12-15 stsp }
3741 6841da00 2019-08-08 stsp
3742 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3743 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3744 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3745 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3746 6841da00 2019-08-08 stsp free(ignorespath);
3747 6841da00 2019-08-08 stsp return err;
3748 f8d1f275 2019-02-04 stsp }
3749 f8d1f275 2019-02-04 stsp
3750 f8d1f275 2019-02-04 stsp static const struct got_error *
3751 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3752 6092c299 2021-10-04 thomas int dirfd)
3753 f8d1f275 2019-02-04 stsp {
3754 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3755 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3756 f8d1f275 2019-02-04 stsp char *path = NULL;
3757 6092c299 2021-10-04 thomas
3758 6092c299 2021-10-04 thomas if (ignore != NULL)
3759 6092c299 2021-10-04 thomas *ignore = 0;
3760 f8d1f275 2019-02-04 stsp
3761 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3762 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3763 0584f854 2019-04-06 stsp
3764 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3765 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3766 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3767 f8d1f275 2019-02-04 stsp } else {
3768 f8d1f275 2019-02-04 stsp path = de->d_name;
3769 f8d1f275 2019-02-04 stsp }
3770 f8d1f275 2019-02-04 stsp
3771 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3772 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3773 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3774 6092c299 2021-10-04 thomas *ignore = 1;
3775 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3776 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3777 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3778 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3779 f8d1f275 2019-02-04 stsp if (parent_path[0])
3780 f8d1f275 2019-02-04 stsp free(path);
3781 b72f483a 2019-02-05 stsp return err;
3782 f8d1f275 2019-02-04 stsp }
3783 f8d1f275 2019-02-04 stsp
3784 347d1d3e 2019-07-12 stsp static const struct got_error *
3785 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3786 3143d852 2020-06-25 stsp {
3787 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3788 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3789 3143d852 2020-06-25 stsp
3790 3143d852 2020-06-25 stsp if (a->no_ignores)
3791 3143d852 2020-06-25 stsp return NULL;
3792 3143d852 2020-06-25 stsp
3793 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3794 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3795 3143d852 2020-06-25 stsp if (err)
3796 3143d852 2020-06-25 stsp return err;
3797 3143d852 2020-06-25 stsp
3798 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3799 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3800 3143d852 2020-06-25 stsp
3801 3143d852 2020-06-25 stsp return err;
3802 3143d852 2020-06-25 stsp }
3803 3143d852 2020-06-25 stsp
3804 3143d852 2020-06-25 stsp static const struct got_error *
3805 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3806 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3807 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3808 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3809 abb4604f 2019-07-27 stsp {
3810 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3811 abb4604f 2019-07-27 stsp struct stat sb;
3812 ff56836b 2021-07-08 stsp
3813 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3814 abb4604f 2019-07-27 stsp if (ie)
3815 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3816 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3817 abb4604f 2019-07-27 stsp
3818 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3819 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3820 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3821 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3822 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3823 abb4604f 2019-07-27 stsp }
3824 abb4604f 2019-07-27 stsp
3825 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3826 fe1d8685 2022-02-12 thomas return NULL;
3827 fe1d8685 2022-02-12 thomas
3828 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3829 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3830 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3831 abb4604f 2019-07-27 stsp
3832 abb4604f 2019-07-27 stsp return NULL;
3833 3143d852 2020-06-25 stsp }
3834 3143d852 2020-06-25 stsp
3835 3143d852 2020-06-25 stsp static const struct got_error *
3836 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3837 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3838 3143d852 2020-06-25 stsp {
3839 3143d852 2020-06-25 stsp const struct got_error *err;
3840 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3841 3143d852 2020-06-25 stsp
3842 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3843 3143d852 2020-06-25 stsp ".cvsignore");
3844 3143d852 2020-06-25 stsp if (err)
3845 3143d852 2020-06-25 stsp return err;
3846 3143d852 2020-06-25 stsp
3847 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3848 3143d852 2020-06-25 stsp ".gitignore");
3849 3143d852 2020-06-25 stsp if (err)
3850 3143d852 2020-06-25 stsp return err;
3851 3143d852 2020-06-25 stsp
3852 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3853 3143d852 2020-06-25 stsp if (err) {
3854 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3855 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3856 3143d852 2020-06-25 stsp return err;
3857 3143d852 2020-06-25 stsp }
3858 3143d852 2020-06-25 stsp for (;;) {
3859 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3860 3143d852 2020-06-25 stsp ".cvsignore");
3861 3143d852 2020-06-25 stsp if (err)
3862 3143d852 2020-06-25 stsp break;
3863 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3864 3143d852 2020-06-25 stsp ".gitignore");
3865 3143d852 2020-06-25 stsp if (err)
3866 3143d852 2020-06-25 stsp break;
3867 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3868 3143d852 2020-06-25 stsp if (err) {
3869 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3870 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3871 3143d852 2020-06-25 stsp break;
3872 3143d852 2020-06-25 stsp }
3873 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3874 ff56836b 2021-07-08 stsp break;
3875 b737c85a 2020-06-26 stsp free(parent_path);
3876 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3877 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3878 3143d852 2020-06-25 stsp }
3879 3143d852 2020-06-25 stsp
3880 b737c85a 2020-06-26 stsp free(parent_path);
3881 b737c85a 2020-06-26 stsp free(next_parent_path);
3882 3143d852 2020-06-25 stsp return err;
3883 abb4604f 2019-07-27 stsp }
3884 f8836425 2023-04-14 thomas
3885 f8836425 2023-04-14 thomas struct find_missing_children_args {
3886 f8836425 2023-04-14 thomas const char *parent_path;
3887 f8836425 2023-04-14 thomas size_t parent_len;
3888 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3889 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3890 f8836425 2023-04-14 thomas void *cancel_arg;
3891 f8836425 2023-04-14 thomas };
3892 f8836425 2023-04-14 thomas
3893 abb4604f 2019-07-27 stsp static const struct got_error *
3894 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3895 f8836425 2023-04-14 thomas {
3896 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3897 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3898 f8836425 2023-04-14 thomas
3899 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3900 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3901 f8836425 2023-04-14 thomas if (err)
3902 f8836425 2023-04-14 thomas return err;
3903 f8836425 2023-04-14 thomas }
3904 f8836425 2023-04-14 thomas
3905 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3906 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3907 f8836425 2023-04-14 thomas
3908 f8836425 2023-04-14 thomas return err;
3909 f8836425 2023-04-14 thomas }
3910 f8836425 2023-04-14 thomas
3911 f8836425 2023-04-14 thomas static const struct got_error *
3912 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3913 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3914 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3915 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3916 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3917 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3918 f8836425 2023-04-14 thomas {
3919 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3920 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3921 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3922 f8836425 2023-04-14 thomas
3923 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3924 f8836425 2023-04-14 thomas if (cancel_cb) {
3925 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3926 f8836425 2023-04-14 thomas if (err)
3927 f8836425 2023-04-14 thomas break;
3928 f8836425 2023-04-14 thomas }
3929 f8836425 2023-04-14 thomas
3930 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3931 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3932 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3933 f8836425 2023-04-14 thomas ondisk_path = NULL;
3934 f8836425 2023-04-14 thomas break;
3935 f8836425 2023-04-14 thomas }
3936 f8836425 2023-04-14 thomas
3937 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3938 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3939 f8836425 2023-04-14 thomas ignores, no_ignores);
3940 f8836425 2023-04-14 thomas if (err)
3941 f8836425 2023-04-14 thomas break;
3942 f8836425 2023-04-14 thomas
3943 f8836425 2023-04-14 thomas free(ondisk_path);
3944 f8836425 2023-04-14 thomas ondisk_path = NULL;
3945 f8836425 2023-04-14 thomas }
3946 f8836425 2023-04-14 thomas
3947 f8836425 2023-04-14 thomas free(ondisk_path);
3948 f8836425 2023-04-14 thomas return err;
3949 f8836425 2023-04-14 thomas }
3950 f8836425 2023-04-14 thomas
3951 f8836425 2023-04-14 thomas static const struct got_error *
3952 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3953 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3954 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3955 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3956 f2a9dc41 2019-12-13 tracey int report_unchanged)
3957 f8d1f275 2019-02-04 stsp {
3958 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3959 6fc93f37 2019-12-13 stsp int fd = -1;
3960 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3961 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3962 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3963 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3964 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3965 3143d852 2020-06-25 stsp
3966 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3967 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3968 f8d1f275 2019-02-04 stsp
3969 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3970 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3971 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3972 8dc303cc 2019-07-27 stsp
3973 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3974 a78810f8 2022-03-13 thomas if (ie) {
3975 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3976 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3977 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3978 a78810f8 2022-03-13 thomas goto done;
3979 f8836425 2023-04-14 thomas } else {
3980 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3981 f8836425 2023-04-14 thomas fmca.parent_path = path;
3982 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3983 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3984 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3985 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3986 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3987 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3988 f8836425 2023-04-14 thomas if (err)
3989 f8836425 2023-04-14 thomas goto done;
3990 a78810f8 2022-03-13 thomas }
3991 a78810f8 2022-03-13 thomas
3992 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3993 6fc93f37 2019-12-13 stsp if (fd == -1) {
3994 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3995 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3996 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3997 ff56836b 2021-07-08 stsp else {
3998 ff56836b 2021-07-08 stsp if (!no_ignores) {
3999 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4000 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4001 ff56836b 2021-07-08 stsp if (err)
4002 ff56836b 2021-07-08 stsp goto done;
4003 ff56836b 2021-07-08 stsp }
4004 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4005 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4006 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4007 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4008 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4009 f8836425 2023-04-14 thomas if (err)
4010 f8836425 2023-04-14 thomas goto done;
4011 f8836425 2023-04-14 thomas } else {
4012 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4013 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4014 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4015 f8836425 2023-04-14 thomas &ignores, no_ignores,
4016 f8836425 2023-04-14 thomas status_cb, status_arg,
4017 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4018 6f97aa83 2023-04-14 thomas if (err)
4019 6f97aa83 2023-04-14 thomas goto done;
4020 f8836425 2023-04-14 thomas }
4021 ff56836b 2021-07-08 stsp }
4022 abb4604f 2019-07-27 stsp } else {
4023 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4024 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4025 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4026 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4027 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4028 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4029 abb4604f 2019-07-27 stsp arg.status_path = path;
4030 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4031 abb4604f 2019-07-27 stsp arg.repo = repo;
4032 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4033 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4034 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4035 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4036 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4037 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4038 022fae89 2019-12-06 tracey if (!no_ignores) {
4039 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4040 3143d852 2020-06-25 stsp worktree->root_path, path);
4041 3143d852 2020-06-25 stsp if (err)
4042 3143d852 2020-06-25 stsp goto done;
4043 022fae89 2019-12-06 tracey }
4044 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4045 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4046 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4047 927df6b7 2019-02-10 stsp }
4048 3143d852 2020-06-25 stsp done:
4049 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4050 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4051 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4052 927df6b7 2019-02-10 stsp free(ondisk_path);
4053 347d1d3e 2019-07-12 stsp return err;
4054 347d1d3e 2019-07-12 stsp }
4055 347d1d3e 2019-07-12 stsp
4056 347d1d3e 2019-07-12 stsp const struct got_error *
4057 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4058 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4059 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4060 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4061 347d1d3e 2019-07-12 stsp {
4062 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4063 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4064 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4065 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4066 347d1d3e 2019-07-12 stsp
4067 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4068 347d1d3e 2019-07-12 stsp if (err)
4069 347d1d3e 2019-07-12 stsp return err;
4070 347d1d3e 2019-07-12 stsp
4071 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4072 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4073 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4074 f6343036 2021-06-22 stsp no_ignores, 0);
4075 72ea6654 2019-07-27 stsp if (err)
4076 72ea6654 2019-07-27 stsp break;
4077 72ea6654 2019-07-27 stsp }
4078 f8d1f275 2019-02-04 stsp free(fileindex_path);
4079 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4080 f8d1f275 2019-02-04 stsp return err;
4081 f8d1f275 2019-02-04 stsp }
4082 6c7ab921 2019-03-18 stsp
4083 6c7ab921 2019-03-18 stsp const struct got_error *
4084 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4085 6c7ab921 2019-03-18 stsp const char *arg)
4086 6c7ab921 2019-03-18 stsp {
4087 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4088 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4089 6c7ab921 2019-03-18 stsp size_t len;
4090 00bb5ea0 2020-07-23 stsp struct stat sb;
4091 01740607 2020-11-04 stsp char *abspath = NULL;
4092 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4093 6c7ab921 2019-03-18 stsp
4094 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4095 6c7ab921 2019-03-18 stsp
4096 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4097 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4098 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4099 00bb5ea0 2020-07-23 stsp
4100 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4101 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4102 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4103 00bb5ea0 2020-07-23 stsp goto done;
4104 00bb5ea0 2020-07-23 stsp }
4105 727173c3 2020-11-06 stsp sb.st_mode = 0;
4106 00bb5ea0 2020-07-23 stsp }
4107 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4108 00bb5ea0 2020-07-23 stsp /*
4109 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4110 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4111 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4112 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4113 00bb5ea0 2020-07-23 stsp */
4114 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4115 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4116 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4117 00bb5ea0 2020-07-23 stsp goto done;
4118 00bb5ea0 2020-07-23 stsp }
4119 00bb5ea0 2020-07-23 stsp
4120 00bb5ea0 2020-07-23 stsp }
4121 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4122 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4123 00bb5ea0 2020-07-23 stsp if (err)
4124 d0710d08 2019-07-22 stsp goto done;
4125 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4126 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4127 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4128 00bb5ea0 2020-07-23 stsp goto done;
4129 00bb5ea0 2020-07-23 stsp }
4130 00bb5ea0 2020-07-23 stsp } else {
4131 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4132 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4133 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4134 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4135 00bb5ea0 2020-07-23 stsp goto done;
4136 00bb5ea0 2020-07-23 stsp }
4137 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4138 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4139 01740607 2020-11-04 stsp goto done;
4140 01740607 2020-11-04 stsp }
4141 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4142 01740607 2020-11-04 stsp sizeof(canonpath));
4143 01740607 2020-11-04 stsp if (err)
4144 00bb5ea0 2020-07-23 stsp goto done;
4145 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4146 01740607 2020-11-04 stsp if (resolved == NULL) {
4147 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4148 01740607 2020-11-04 stsp goto done;
4149 00bb5ea0 2020-07-23 stsp }
4150 d0710d08 2019-07-22 stsp }
4151 d0710d08 2019-07-22 stsp }
4152 6c7ab921 2019-03-18 stsp
4153 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4154 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4155 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4156 6c7ab921 2019-03-18 stsp goto done;
4157 6c7ab921 2019-03-18 stsp }
4158 6c7ab921 2019-03-18 stsp
4159 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4160 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4161 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4162 f6d88e1a 2019-05-29 stsp if (err)
4163 f6d88e1a 2019-05-29 stsp goto done;
4164 f6d88e1a 2019-05-29 stsp } else {
4165 f6d88e1a 2019-05-29 stsp path = strdup("");
4166 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4167 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4168 f6d88e1a 2019-05-29 stsp goto done;
4169 f6d88e1a 2019-05-29 stsp }
4170 6c7ab921 2019-03-18 stsp }
4171 6c7ab921 2019-03-18 stsp
4172 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4173 6c7ab921 2019-03-18 stsp len = strlen(path);
4174 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4175 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4176 6c7ab921 2019-03-18 stsp len--;
4177 6c7ab921 2019-03-18 stsp }
4178 6c7ab921 2019-03-18 stsp done:
4179 01740607 2020-11-04 stsp free(abspath);
4180 6c7ab921 2019-03-18 stsp free(resolved);
4181 d0710d08 2019-07-22 stsp free(cwd);
4182 6c7ab921 2019-03-18 stsp if (err == NULL)
4183 6c7ab921 2019-03-18 stsp *wt_path = path;
4184 6c7ab921 2019-03-18 stsp else
4185 6c7ab921 2019-03-18 stsp free(path);
4186 d00136be 2019-03-26 stsp return err;
4187 d00136be 2019-03-26 stsp }
4188 d00136be 2019-03-26 stsp
4189 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4190 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4191 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4192 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4193 4e68cba3 2019-11-23 stsp void *progress_arg;
4194 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4195 4e68cba3 2019-11-23 stsp };
4196 4e68cba3 2019-11-23 stsp
4197 b21ebdb0 2023-06-22 thomas static int
4198 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4199 b21ebdb0 2023-06-22 thomas {
4200 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4201 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4202 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4203 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4204 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4205 b21ebdb0 2023-06-22 thomas }
4206 b21ebdb0 2023-06-22 thomas
4207 4e68cba3 2019-11-23 stsp static const struct got_error *
4208 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4209 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4210 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4211 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4212 07f5b47a 2019-06-02 stsp {
4213 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4214 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4215 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4216 6d022e97 2019-08-04 stsp struct stat sb;
4217 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4218 07f5b47a 2019-06-02 stsp
4219 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4220 dbb83fbd 2019-12-12 stsp relpath) == -1)
4221 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4222 dbb83fbd 2019-12-12 stsp
4223 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4224 6d022e97 2019-08-04 stsp if (ie) {
4225 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4226 12463d8b 2019-12-13 stsp de_name, a->repo);
4227 6d022e97 2019-08-04 stsp if (err)
4228 dbb83fbd 2019-12-12 stsp goto done;
4229 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4230 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4231 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4232 dbb83fbd 2019-12-12 stsp goto done;
4233 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4234 dbb83fbd 2019-12-12 stsp if (err)
4235 dbb83fbd 2019-12-12 stsp goto done;
4236 6d022e97 2019-08-04 stsp }
4237 07f5b47a 2019-06-02 stsp
4238 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4239 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4240 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4241 84bf00a6 2022-02-12 thomas else
4242 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4243 dbb83fbd 2019-12-12 stsp goto done;
4244 4e68cba3 2019-11-23 stsp }
4245 07f5b47a 2019-06-02 stsp
4246 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4247 4e68cba3 2019-11-23 stsp if (err)
4248 4e68cba3 2019-11-23 stsp goto done;
4249 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4250 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4251 3969253a 2020-03-07 stsp if (err) {
4252 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4253 3969253a 2020-03-07 stsp goto done;
4254 3969253a 2020-03-07 stsp }
4255 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4256 07f5b47a 2019-06-02 stsp if (err) {
4257 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4258 4e68cba3 2019-11-23 stsp goto done;
4259 07f5b47a 2019-06-02 stsp }
4260 4e68cba3 2019-11-23 stsp done:
4261 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4262 dbb83fbd 2019-12-12 stsp if (err)
4263 dbb83fbd 2019-12-12 stsp return err;
4264 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4265 dbb83fbd 2019-12-12 stsp return NULL;
4266 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4267 07f5b47a 2019-06-02 stsp }
4268 07f5b47a 2019-06-02 stsp
4269 d00136be 2019-03-26 stsp const struct got_error *
4270 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4271 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4272 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4273 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4274 d00136be 2019-03-26 stsp {
4275 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4276 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4277 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4278 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4279 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4280 d00136be 2019-03-26 stsp
4281 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4282 d00136be 2019-03-26 stsp if (err)
4283 d00136be 2019-03-26 stsp return err;
4284 d00136be 2019-03-26 stsp
4285 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4286 d00136be 2019-03-26 stsp if (err)
4287 d00136be 2019-03-26 stsp goto done;
4288 d00136be 2019-03-26 stsp
4289 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4290 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4291 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4292 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4293 4e68cba3 2019-11-23 stsp saa.repo = repo;
4294 4e68cba3 2019-11-23 stsp
4295 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4296 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4297 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4298 1dd54920 2019-05-11 stsp if (err)
4299 af12c6b9 2019-06-04 stsp break;
4300 1dd54920 2019-05-11 stsp }
4301 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4302 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4303 af12c6b9 2019-06-04 stsp err = sync_err;
4304 d00136be 2019-03-26 stsp done:
4305 fb399478 2019-07-12 stsp free(fileindex_path);
4306 d00136be 2019-03-26 stsp if (fileindex)
4307 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4308 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4309 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4310 d00136be 2019-03-26 stsp err = unlockerr;
4311 6c7ab921 2019-03-18 stsp return err;
4312 6c7ab921 2019-03-18 stsp }
4313 17ed4618 2019-06-02 stsp
4314 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4315 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4316 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4317 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4318 f2a9dc41 2019-12-13 tracey void *progress_arg;
4319 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4320 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4321 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4322 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4323 8ec7352a 2023-06-01 thomas const char *status_path;
4324 8ec7352a 2023-06-01 thomas size_t status_path_len;
4325 766841c2 2020-08-13 stsp const char *status_codes;
4326 f2a9dc41 2019-12-13 tracey };
4327 f2a9dc41 2019-12-13 tracey
4328 f2a9dc41 2019-12-13 tracey static const struct got_error *
4329 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4330 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4331 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4332 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4333 17ed4618 2019-06-02 stsp {
4334 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4335 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4336 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4337 17ed4618 2019-06-02 stsp struct stat sb;
4338 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4339 d64cc78a 2022-01-25 thomas
4340 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4341 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4342 d64cc78a 2022-01-25 thomas return NULL;
4343 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4344 d64cc78a 2022-01-25 thomas }
4345 17ed4618 2019-06-02 stsp
4346 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4347 17ed4618 2019-06-02 stsp if (ie == NULL)
4348 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4349 2ec1f75b 2019-03-26 stsp
4350 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4351 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4352 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4353 9acbc4fa 2019-08-03 stsp return NULL;
4354 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4355 9acbc4fa 2019-08-03 stsp }
4356 9acbc4fa 2019-08-03 stsp
4357 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4358 f2a9dc41 2019-12-13 tracey relpath) == -1)
4359 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4360 f2a9dc41 2019-12-13 tracey
4361 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4362 12463d8b 2019-12-13 stsp a->repo);
4363 17ed4618 2019-06-02 stsp if (err)
4364 f2a9dc41 2019-12-13 tracey goto done;
4365 17ed4618 2019-06-02 stsp
4366 766841c2 2020-08-13 stsp if (a->status_codes) {
4367 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4368 766841c2 2020-08-13 stsp int i;
4369 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4370 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4371 766841c2 2020-08-13 stsp break;
4372 766841c2 2020-08-13 stsp }
4373 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4374 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4375 766841c2 2020-08-13 stsp free(ondisk_path);
4376 766841c2 2020-08-13 stsp return NULL;
4377 766841c2 2020-08-13 stsp }
4378 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4379 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4380 766841c2 2020-08-13 stsp static char msg[64];
4381 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4382 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4383 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4384 766841c2 2020-08-13 stsp goto done;
4385 766841c2 2020-08-13 stsp }
4386 766841c2 2020-08-13 stsp }
4387 766841c2 2020-08-13 stsp
4388 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4389 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4390 f2a9dc41 2019-12-13 tracey goto done;
4391 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4392 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4393 f2a9dc41 2019-12-13 tracey goto done;
4394 f2a9dc41 2019-12-13 tracey }
4395 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4396 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4397 d64cc78a 2022-01-25 thomas goto done;
4398 d64cc78a 2022-01-25 thomas }
4399 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4400 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4401 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4402 f2a9dc41 2019-12-13 tracey goto done;
4403 f2a9dc41 2019-12-13 tracey }
4404 17ed4618 2019-06-02 stsp }
4405 17ed4618 2019-06-02 stsp
4406 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4407 20a2ad1c 2020-10-20 stsp size_t root_len;
4408 20a2ad1c 2020-10-20 stsp
4409 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4410 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4411 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4412 12463d8b 2019-12-13 stsp ondisk_path);
4413 12463d8b 2019-12-13 stsp goto done;
4414 12463d8b 2019-12-13 stsp }
4415 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4416 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4417 12463d8b 2019-12-13 stsp goto done;
4418 15341bfd 2020-03-05 tracey }
4419 15341bfd 2020-03-05 tracey
4420 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4421 20a2ad1c 2020-10-20 stsp do {
4422 20a2ad1c 2020-10-20 stsp char *parent;
4423 8ec7352a 2023-06-01 thomas
4424 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4425 20a2ad1c 2020-10-20 stsp if (err)
4426 2513f20a 2020-10-20 stsp goto done;
4427 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4428 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4429 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4430 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4431 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4432 8ec7352a 2023-06-01 thomas a->status_path_len))
4433 8ec7352a 2023-06-01 thomas break;
4434 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4435 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4436 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4437 20a2ad1c 2020-10-20 stsp ondisk_path);
4438 15341bfd 2020-03-05 tracey break;
4439 15341bfd 2020-03-05 tracey }
4440 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4441 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4442 f2a9dc41 2019-12-13 tracey }
4443 17ed4618 2019-06-02 stsp
4444 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4445 f2a9dc41 2019-12-13 tracey done:
4446 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4447 f2a9dc41 2019-12-13 tracey if (err)
4448 f2a9dc41 2019-12-13 tracey return err;
4449 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4450 f2a9dc41 2019-12-13 tracey return NULL;
4451 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4452 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4453 17ed4618 2019-06-02 stsp }
4454 17ed4618 2019-06-02 stsp
4455 2ec1f75b 2019-03-26 stsp const struct got_error *
4456 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4457 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4458 766841c2 2020-08-13 stsp const char *status_codes,
4459 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4460 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4461 2ec1f75b 2019-03-26 stsp {
4462 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4463 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4464 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4465 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4466 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4467 2ec1f75b 2019-03-26 stsp
4468 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4469 2ec1f75b 2019-03-26 stsp if (err)
4470 2ec1f75b 2019-03-26 stsp return err;
4471 2ec1f75b 2019-03-26 stsp
4472 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4473 2ec1f75b 2019-03-26 stsp if (err)
4474 2ec1f75b 2019-03-26 stsp goto done;
4475 f2a9dc41 2019-12-13 tracey
4476 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4477 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4478 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4479 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4480 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4481 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4482 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4483 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4484 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4485 2ec1f75b 2019-03-26 stsp
4486 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4487 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4488 8ec7352a 2023-06-01 thomas
4489 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4490 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4491 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4492 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4493 8ec7352a 2023-06-01 thomas goto done;
4494 8ec7352a 2023-06-01 thomas }
4495 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4496 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4497 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4498 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4499 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4500 17ed4618 2019-06-02 stsp if (err)
4501 af12c6b9 2019-06-04 stsp break;
4502 2ec1f75b 2019-03-26 stsp }
4503 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4504 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4505 af12c6b9 2019-06-04 stsp err = sync_err;
4506 2ec1f75b 2019-03-26 stsp done:
4507 fb399478 2019-07-12 stsp free(fileindex_path);
4508 2ec1f75b 2019-03-26 stsp if (fileindex)
4509 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4510 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4511 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4512 2ec1f75b 2019-03-26 stsp err = unlockerr;
4513 33aa809d 2019-08-08 stsp return err;
4514 33aa809d 2019-08-08 stsp }
4515 33aa809d 2019-08-08 stsp
4516 33aa809d 2019-08-08 stsp static const struct got_error *
4517 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4518 33aa809d 2019-08-08 stsp {
4519 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4520 33aa809d 2019-08-08 stsp char *line = NULL;
4521 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4522 33aa809d 2019-08-08 stsp ssize_t linelen;
4523 33aa809d 2019-08-08 stsp
4524 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4525 33aa809d 2019-08-08 stsp if (linelen == -1) {
4526 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4527 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4528 33aa809d 2019-08-08 stsp goto done;
4529 33aa809d 2019-08-08 stsp }
4530 33aa809d 2019-08-08 stsp return NULL;
4531 33aa809d 2019-08-08 stsp }
4532 33aa809d 2019-08-08 stsp if (outfile) {
4533 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4534 33aa809d 2019-08-08 stsp if (n != linelen) {
4535 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4536 33aa809d 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp }
4538 33aa809d 2019-08-08 stsp }
4539 33aa809d 2019-08-08 stsp if (rejectfile) {
4540 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4541 33aa809d 2019-08-08 stsp if (n != linelen)
4542 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4543 33aa809d 2019-08-08 stsp }
4544 33aa809d 2019-08-08 stsp done:
4545 33aa809d 2019-08-08 stsp free(line);
4546 2ec1f75b 2019-03-26 stsp return err;
4547 2ec1f75b 2019-03-26 stsp }
4548 1f1abb7e 2019-08-08 stsp
4549 33aa809d 2019-08-08 stsp static const struct got_error *
4550 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4551 33aa809d 2019-08-08 stsp {
4552 33aa809d 2019-08-08 stsp char *line = NULL;
4553 33aa809d 2019-08-08 stsp size_t linesize = 0;
4554 33aa809d 2019-08-08 stsp ssize_t linelen;
4555 33aa809d 2019-08-08 stsp
4556 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4557 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4558 500467ff 2019-09-25 hiltjo if (ferror(f))
4559 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4560 500467ff 2019-09-25 hiltjo return NULL;
4561 500467ff 2019-09-25 hiltjo }
4562 33aa809d 2019-08-08 stsp free(line);
4563 33aa809d 2019-08-08 stsp return NULL;
4564 33aa809d 2019-08-08 stsp }
4565 33aa809d 2019-08-08 stsp
4566 33aa809d 2019-08-08 stsp static const struct got_error *
4567 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4568 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4569 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4570 abc59930 2021-09-05 naddy {
4571 33aa809d 2019-08-08 stsp const struct got_error *err;
4572 33aa809d 2019-08-08 stsp
4573 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4574 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4575 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4576 33aa809d 2019-08-08 stsp if (err)
4577 33aa809d 2019-08-08 stsp return err;
4578 33aa809d 2019-08-08 stsp (*line_cur1)++;
4579 33aa809d 2019-08-08 stsp }
4580 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4581 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4582 33aa809d 2019-08-08 stsp if (rejectfile)
4583 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4584 33aa809d 2019-08-08 stsp else
4585 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4586 33aa809d 2019-08-08 stsp if (err)
4587 33aa809d 2019-08-08 stsp return err;
4588 33aa809d 2019-08-08 stsp (*line_cur2)++;
4589 33aa809d 2019-08-08 stsp }
4590 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4591 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4592 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4593 33aa809d 2019-08-08 stsp if (err)
4594 33aa809d 2019-08-08 stsp return err;
4595 33aa809d 2019-08-08 stsp (*line_cur2)++;
4596 33aa809d 2019-08-08 stsp }
4597 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4598 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4599 33aa809d 2019-08-08 stsp if (rejectfile)
4600 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4601 33aa809d 2019-08-08 stsp else
4602 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4603 33aa809d 2019-08-08 stsp if (err)
4604 33aa809d 2019-08-08 stsp return err;
4605 33aa809d 2019-08-08 stsp (*line_cur1)++;
4606 33aa809d 2019-08-08 stsp }
4607 f1e81a05 2019-08-10 stsp
4608 f1e81a05 2019-08-10 stsp return NULL;
4609 f1e81a05 2019-08-10 stsp }
4610 f1e81a05 2019-08-10 stsp
4611 f1e81a05 2019-08-10 stsp static const struct got_error *
4612 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4613 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4614 f1e81a05 2019-08-10 stsp {
4615 f1e81a05 2019-08-10 stsp const struct got_error *err;
4616 f1e81a05 2019-08-10 stsp
4617 f1e81a05 2019-08-10 stsp if (outfile) {
4618 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4619 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4620 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4621 f1e81a05 2019-08-10 stsp if (err)
4622 f1e81a05 2019-08-10 stsp return err;
4623 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4624 f1e81a05 2019-08-10 stsp }
4625 f1e81a05 2019-08-10 stsp }
4626 f1e81a05 2019-08-10 stsp if (rejectfile) {
4627 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4628 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4629 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4630 f1e81a05 2019-08-10 stsp if (err)
4631 f1e81a05 2019-08-10 stsp return err;
4632 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4633 f1e81a05 2019-08-10 stsp }
4634 33aa809d 2019-08-08 stsp }
4635 33aa809d 2019-08-08 stsp
4636 33aa809d 2019-08-08 stsp return NULL;
4637 33aa809d 2019-08-08 stsp }
4638 33aa809d 2019-08-08 stsp
4639 33aa809d 2019-08-08 stsp static const struct got_error *
4640 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4641 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4642 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4643 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4644 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4645 33aa809d 2019-08-08 stsp {
4646 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4647 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4648 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4649 33aa809d 2019-08-08 stsp FILE *hunkfile;
4650 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4651 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4652 fe621944 2020-11-10 stsp int rc;
4653 33aa809d 2019-08-08 stsp
4654 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4655 33aa809d 2019-08-08 stsp
4656 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4657 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4658 fe621944 2020-11-10 stsp start_old = cc.left.start;
4659 fe621944 2020-11-10 stsp end_old = cc.left.end;
4660 fe621944 2020-11-10 stsp start_new = cc.right.start;
4661 fe621944 2020-11-10 stsp end_new = cc.right.end;
4662 33aa809d 2019-08-08 stsp
4663 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4664 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4665 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4666 33aa809d 2019-08-08 stsp
4667 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4668 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4669 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4670 33aa809d 2019-08-08 stsp
4671 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4672 fe621944 2020-11-10 stsp if (diff_state == NULL)
4673 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4674 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4675 fe621944 2020-11-10 stsp
4676 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4677 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4678 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4679 33aa809d 2019-08-08 stsp goto done;
4680 33aa809d 2019-08-08 stsp }
4681 fe621944 2020-11-10 stsp
4682 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4683 fe621944 2020-11-10 stsp diff_result, &cc);
4684 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4685 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4686 33aa809d 2019-08-08 stsp goto done;
4687 33aa809d 2019-08-08 stsp }
4688 fe621944 2020-11-10 stsp
4689 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4690 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4691 33aa809d 2019-08-08 stsp goto done;
4692 33aa809d 2019-08-08 stsp }
4693 33aa809d 2019-08-08 stsp
4694 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4695 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4696 33aa809d 2019-08-08 stsp if (err)
4697 33aa809d 2019-08-08 stsp goto done;
4698 33aa809d 2019-08-08 stsp
4699 33aa809d 2019-08-08 stsp switch (*choice) {
4700 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4701 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4702 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4703 33aa809d 2019-08-08 stsp break;
4704 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4705 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4706 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4707 33aa809d 2019-08-08 stsp break;
4708 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4709 33aa809d 2019-08-08 stsp break;
4710 33aa809d 2019-08-08 stsp default:
4711 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4712 33aa809d 2019-08-08 stsp break;
4713 33aa809d 2019-08-08 stsp }
4714 33aa809d 2019-08-08 stsp done:
4715 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4716 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4717 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4718 33aa809d 2019-08-08 stsp return err;
4719 33aa809d 2019-08-08 stsp }
4720 33aa809d 2019-08-08 stsp
4721 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4722 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4723 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4724 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4725 1f1abb7e 2019-08-08 stsp void *progress_arg;
4726 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4727 33aa809d 2019-08-08 stsp void *patch_arg;
4728 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4729 10604dce 2021-09-24 thomas int unlink_added_files;
4730 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4731 1f1abb7e 2019-08-08 stsp };
4732 a129376b 2019-03-28 stsp
4733 e20a8b6f 2019-06-04 stsp static const struct got_error *
4734 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4735 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4736 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4737 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4738 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4739 33aa809d 2019-08-08 stsp {
4740 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4741 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4742 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4743 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4744 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4745 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4746 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4747 fe621944 2020-11-10 stsp struct stat sb2;
4748 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4749 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4750 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4751 33aa809d 2019-08-08 stsp
4752 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4753 33aa809d 2019-08-08 stsp
4754 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4755 33aa809d 2019-08-08 stsp if (err)
4756 33aa809d 2019-08-08 stsp return err;
4757 33aa809d 2019-08-08 stsp
4758 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4759 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4760 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4761 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4762 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4763 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4764 fa3cef63 2020-07-23 stsp goto done;
4765 fa3cef63 2020-07-23 stsp }
4766 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4767 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4768 48b4f239 2021-12-31 thomas if (link_len == -1) {
4769 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4770 48b4f239 2021-12-31 thomas path2);
4771 48b4f239 2021-12-31 thomas }
4772 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4773 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4774 12463d8b 2019-12-13 stsp }
4775 12463d8b 2019-12-13 stsp } else {
4776 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4777 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4778 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4779 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4780 fa3cef63 2020-07-23 stsp goto done;
4781 fa3cef63 2020-07-23 stsp }
4782 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4783 fa3cef63 2020-07-23 stsp sizeof(link_target));
4784 fa3cef63 2020-07-23 stsp if (link_len == -1)
4785 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4786 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4787 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4788 12463d8b 2019-12-13 stsp }
4789 1ebedb77 2019-10-19 stsp }
4790 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4791 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4792 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4793 fa3cef63 2020-07-23 stsp goto done;
4794 fa3cef63 2020-07-23 stsp }
4795 1ebedb77 2019-10-19 stsp
4796 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4797 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4798 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4799 fa3cef63 2020-07-23 stsp goto done;
4800 fa3cef63 2020-07-23 stsp }
4801 fa3cef63 2020-07-23 stsp fd2 = -1;
4802 fa3cef63 2020-07-23 stsp } else {
4803 fa3cef63 2020-07-23 stsp size_t n;
4804 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4805 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4806 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4807 fa3cef63 2020-07-23 stsp goto done;
4808 fa3cef63 2020-07-23 stsp }
4809 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4810 fa3cef63 2020-07-23 stsp if (n != link_len) {
4811 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4812 fa3cef63 2020-07-23 stsp goto done;
4813 fa3cef63 2020-07-23 stsp }
4814 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4815 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4816 fa3cef63 2020-07-23 stsp goto done;
4817 fa3cef63 2020-07-23 stsp }
4818 fa3cef63 2020-07-23 stsp rewind(f2);
4819 33aa809d 2019-08-08 stsp }
4820 33aa809d 2019-08-08 stsp
4821 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4822 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4823 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4824 f4ae6ddb 2022-07-01 thomas goto done;
4825 f4ae6ddb 2022-07-01 thomas }
4826 f4ae6ddb 2022-07-01 thomas
4827 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4828 33aa809d 2019-08-08 stsp if (err)
4829 33aa809d 2019-08-08 stsp goto done;
4830 33aa809d 2019-08-08 stsp
4831 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4832 33aa809d 2019-08-08 stsp if (err)
4833 33aa809d 2019-08-08 stsp goto done;
4834 33aa809d 2019-08-08 stsp
4835 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4836 33aa809d 2019-08-08 stsp if (err)
4837 33aa809d 2019-08-08 stsp goto done;
4838 33aa809d 2019-08-08 stsp
4839 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4840 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4841 33aa809d 2019-08-08 stsp if (err)
4842 33aa809d 2019-08-08 stsp goto done;
4843 33aa809d 2019-08-08 stsp
4844 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4845 fc2a50f2 2022-11-01 thomas "");
4846 33aa809d 2019-08-08 stsp if (err)
4847 33aa809d 2019-08-08 stsp goto done;
4848 33aa809d 2019-08-08 stsp
4849 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4850 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4851 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4852 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4853 fe621944 2020-11-10 stsp
4854 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4855 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4856 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4857 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4858 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4859 fe621944 2020-11-10 stsp nchanges++;
4860 fe621944 2020-11-10 stsp }
4861 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4862 33aa809d 2019-08-08 stsp int choice;
4863 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4864 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4865 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4866 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4867 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4868 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4869 33aa809d 2019-08-08 stsp if (err)
4870 33aa809d 2019-08-08 stsp goto done;
4871 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4872 33aa809d 2019-08-08 stsp have_content = 1;
4873 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4874 33aa809d 2019-08-08 stsp break;
4875 33aa809d 2019-08-08 stsp }
4876 1ebedb77 2019-10-19 stsp if (have_content) {
4877 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4878 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4879 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4880 1ebedb77 2019-10-19 stsp if (err)
4881 1ebedb77 2019-10-19 stsp goto done;
4882 1ebedb77 2019-10-19 stsp
4883 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4884 a2c162eb 2022-10-30 thomas mode_t mode;
4885 a2c162eb 2022-10-30 thomas
4886 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4887 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4888 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4889 fa3cef63 2020-07-23 stsp goto done;
4890 fa3cef63 2020-07-23 stsp }
4891 1ebedb77 2019-10-19 stsp }
4892 1ebedb77 2019-10-19 stsp }
4893 33aa809d 2019-08-08 stsp done:
4894 33aa809d 2019-08-08 stsp free(id_str);
4895 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4896 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4897 33aa809d 2019-08-08 stsp if (blob)
4898 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4899 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4900 fe621944 2020-11-10 stsp if (err == NULL)
4901 fe621944 2020-11-10 stsp err = free_err;
4902 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4903 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4904 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4905 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4906 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4907 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4908 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4909 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4910 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4911 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4912 33aa809d 2019-08-08 stsp if (err || !have_content) {
4913 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4914 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4915 33aa809d 2019-08-08 stsp free(*path_outfile);
4916 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4917 33aa809d 2019-08-08 stsp }
4918 33aa809d 2019-08-08 stsp free(path1);
4919 33aa809d 2019-08-08 stsp return err;
4920 33aa809d 2019-08-08 stsp }
4921 33aa809d 2019-08-08 stsp
4922 33aa809d 2019-08-08 stsp static const struct got_error *
4923 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4924 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4925 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4926 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4927 a129376b 2019-03-28 stsp {
4928 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4929 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4930 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4931 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4932 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4933 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4934 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4935 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4936 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4937 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4938 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4939 f4ae6ddb 2022-07-01 thomas int fd = -1;
4940 a129376b 2019-03-28 stsp
4941 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4942 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4943 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4944 d3bcc3d1 2019-08-08 stsp return NULL;
4945 3d69ad8d 2019-08-17 semarie
4946 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4947 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4948 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4949 d3bcc3d1 2019-08-08 stsp
4950 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4951 65084dad 2019-08-08 stsp if (ie == NULL)
4952 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4953 a129376b 2019-03-28 stsp
4954 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4955 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4956 a129376b 2019-03-28 stsp if (err) {
4957 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4958 a129376b 2019-03-28 stsp goto done;
4959 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4960 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4961 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4962 e20a8b6f 2019-06-04 stsp goto done;
4963 e20a8b6f 2019-06-04 stsp }
4964 a129376b 2019-03-28 stsp }
4965 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4966 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4967 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4968 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4969 a129376b 2019-03-28 stsp goto done;
4970 a129376b 2019-03-28 stsp }
4971 a129376b 2019-03-28 stsp } else {
4972 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4973 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4974 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4975 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4976 a129376b 2019-03-28 stsp goto done;
4977 a129376b 2019-03-28 stsp }
4978 a129376b 2019-03-28 stsp } else {
4979 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4980 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4981 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4982 a129376b 2019-03-28 stsp goto done;
4983 a129376b 2019-03-28 stsp }
4984 a129376b 2019-03-28 stsp }
4985 a129376b 2019-03-28 stsp }
4986 a129376b 2019-03-28 stsp
4987 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4988 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4989 945f9229 2022-04-16 thomas if (err)
4990 945f9229 2022-04-16 thomas goto done;
4991 945f9229 2022-04-16 thomas
4992 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4993 a9fa2909 2019-07-27 stsp if (err) {
4994 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4995 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4996 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4997 a9fa2909 2019-07-27 stsp goto done;
4998 a9fa2909 2019-07-27 stsp } else {
4999 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5000 a9fa2909 2019-07-27 stsp if (err)
5001 a9fa2909 2019-07-27 stsp goto done;
5002 a9fa2909 2019-07-27 stsp
5003 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5004 1233e6b6 2020-10-19 stsp if (err)
5005 a9fa2909 2019-07-27 stsp goto done;
5006 a9fa2909 2019-07-27 stsp
5007 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5008 1233e6b6 2020-10-19 stsp free(te_name);
5009 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5010 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5011 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5012 a9fa2909 2019-07-27 stsp goto done;
5013 a9fa2909 2019-07-27 stsp }
5014 a129376b 2019-03-28 stsp }
5015 a129376b 2019-03-28 stsp
5016 a129376b 2019-03-28 stsp switch (status) {
5017 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5018 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5019 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5020 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5021 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5022 33aa809d 2019-08-08 stsp if (err)
5023 33aa809d 2019-08-08 stsp goto done;
5024 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5025 33aa809d 2019-08-08 stsp break;
5026 33aa809d 2019-08-08 stsp }
5027 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5028 1f1abb7e 2019-08-08 stsp ie->path);
5029 1ee397ad 2019-07-12 stsp if (err)
5030 1ee397ad 2019-07-12 stsp goto done;
5031 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5032 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5033 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5034 8641a332 2023-04-14 thomas
5035 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5036 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5037 8641a332 2023-04-14 thomas
5038 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5039 8641a332 2023-04-14 thomas entry) {
5040 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5041 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5042 8641a332 2023-04-14 thomas continue;
5043 8641a332 2023-04-14 thomas do_unlink = 1;
5044 8641a332 2023-04-14 thomas break;
5045 8641a332 2023-04-14 thomas }
5046 10604dce 2021-09-24 thomas }
5047 8641a332 2023-04-14 thomas
5048 8641a332 2023-04-14 thomas if (do_unlink) {
5049 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5050 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5051 8641a332 2023-04-14 thomas relpath) == -1) {
5052 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5053 8641a332 2023-04-14 thomas goto done;
5054 8641a332 2023-04-14 thomas }
5055 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5056 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5057 8641a332 2023-04-14 thomas ondisk_path);
5058 8641a332 2023-04-14 thomas break;
5059 8641a332 2023-04-14 thomas }
5060 10604dce 2021-09-24 thomas }
5061 10604dce 2021-09-24 thomas }
5062 a129376b 2019-03-28 stsp break;
5063 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5064 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5065 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5066 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5067 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5068 33aa809d 2019-08-08 stsp if (err)
5069 33aa809d 2019-08-08 stsp goto done;
5070 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5071 33aa809d 2019-08-08 stsp break;
5072 33aa809d 2019-08-08 stsp }
5073 33aa809d 2019-08-08 stsp /* fall through */
5074 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5075 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5076 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5077 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5078 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5079 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5080 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5081 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5082 43010591 2023-02-17 thomas else
5083 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5084 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5085 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5086 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5087 f4ae6ddb 2022-07-01 thomas goto done;
5088 f4ae6ddb 2022-07-01 thomas }
5089 f4ae6ddb 2022-07-01 thomas
5090 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5091 a129376b 2019-03-28 stsp if (err)
5092 65084dad 2019-08-08 stsp goto done;
5093 65084dad 2019-08-08 stsp
5094 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5095 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5096 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5097 a129376b 2019-03-28 stsp goto done;
5098 65084dad 2019-08-08 stsp }
5099 65084dad 2019-08-08 stsp
5100 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5101 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5102 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5103 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5104 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5105 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5106 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5107 33aa809d 2019-08-08 stsp break;
5108 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5109 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5110 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5111 369fd7e5 2020-07-23 stsp path_content);
5112 369fd7e5 2020-07-23 stsp break;
5113 369fd7e5 2020-07-23 stsp }
5114 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5115 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5116 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5117 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5118 369fd7e5 2020-07-23 stsp } else {
5119 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5120 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5121 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5122 369fd7e5 2020-07-23 stsp goto done;
5123 369fd7e5 2020-07-23 stsp }
5124 33aa809d 2019-08-08 stsp }
5125 33aa809d 2019-08-08 stsp } else {
5126 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5127 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5128 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5129 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5130 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5131 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5132 2e1fa222 2020-07-23 stsp } else {
5133 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5134 2e1fa222 2020-07-23 stsp ie->path,
5135 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5136 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5137 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5138 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5139 2e1fa222 2020-07-23 stsp }
5140 a129376b 2019-03-28 stsp if (err)
5141 a129376b 2019-03-28 stsp goto done;
5142 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5143 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5144 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5145 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5146 437adc9d 2020-12-10 yzhong blob->id.sha1,
5147 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5148 2e1fa222 2020-07-23 stsp if (err)
5149 2e1fa222 2020-07-23 stsp goto done;
5150 2e1fa222 2020-07-23 stsp }
5151 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5152 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5153 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5154 33aa809d 2019-08-08 stsp }
5155 a129376b 2019-03-28 stsp }
5156 a129376b 2019-03-28 stsp break;
5157 e20a8b6f 2019-06-04 stsp }
5158 a129376b 2019-03-28 stsp default:
5159 1f1abb7e 2019-08-08 stsp break;
5160 a129376b 2019-03-28 stsp }
5161 a129376b 2019-03-28 stsp done:
5162 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5163 33aa809d 2019-08-08 stsp free(path_content);
5164 e20a8b6f 2019-06-04 stsp free(parent_path);
5165 a129376b 2019-03-28 stsp free(tree_path);
5166 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5167 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5168 a129376b 2019-03-28 stsp if (blob)
5169 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5170 a129376b 2019-03-28 stsp if (tree)
5171 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5172 a129376b 2019-03-28 stsp free(tree_id);
5173 945f9229 2022-04-16 thomas if (base_commit)
5174 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5175 e20a8b6f 2019-06-04 stsp return err;
5176 e20a8b6f 2019-06-04 stsp }
5177 e20a8b6f 2019-06-04 stsp
5178 e20a8b6f 2019-06-04 stsp const struct got_error *
5179 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5180 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5181 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5182 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5183 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5184 e20a8b6f 2019-06-04 stsp {
5185 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5186 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5187 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5188 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5189 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5190 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5191 e20a8b6f 2019-06-04 stsp
5192 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5193 e20a8b6f 2019-06-04 stsp if (err)
5194 e20a8b6f 2019-06-04 stsp return err;
5195 e20a8b6f 2019-06-04 stsp
5196 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5197 e20a8b6f 2019-06-04 stsp if (err)
5198 e20a8b6f 2019-06-04 stsp goto done;
5199 e20a8b6f 2019-06-04 stsp
5200 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5201 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5202 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5203 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5204 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5205 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5206 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5207 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5208 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5209 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5210 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5211 e20a8b6f 2019-06-04 stsp if (err)
5212 af12c6b9 2019-06-04 stsp break;
5213 e20a8b6f 2019-06-04 stsp }
5214 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5215 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5216 e20a8b6f 2019-06-04 stsp err = sync_err;
5217 af12c6b9 2019-06-04 stsp done:
5218 fb399478 2019-07-12 stsp free(fileindex_path);
5219 a129376b 2019-03-28 stsp if (fileindex)
5220 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5221 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5222 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5223 a129376b 2019-03-28 stsp err = unlockerr;
5224 c4296144 2019-05-09 stsp return err;
5225 c4296144 2019-05-09 stsp }
5226 c4296144 2019-05-09 stsp
5227 cf066bf8 2019-05-09 stsp static void
5228 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5229 cf066bf8 2019-05-09 stsp {
5230 24519714 2019-05-09 stsp free(ct->path);
5231 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5232 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5233 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5234 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5235 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5236 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5237 cf066bf8 2019-05-09 stsp free(ct);
5238 cf066bf8 2019-05-09 stsp }
5239 24519714 2019-05-09 stsp
5240 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5241 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5242 24519714 2019-05-09 stsp struct got_repository *repo;
5243 24519714 2019-05-09 stsp struct got_worktree *worktree;
5244 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5245 5f8a88c6 2019-08-03 stsp int have_staged_files;
5246 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5247 ef899790 2022-11-01 thomas int diff_header_shown;
5248 be94c032 2023-02-20 thomas int commit_conflicts;
5249 ef899790 2022-11-01 thomas FILE *diff_outfile;
5250 ef899790 2022-11-01 thomas FILE *f1;
5251 ef899790 2022-11-01 thomas FILE *f2;
5252 24519714 2019-05-09 stsp };
5253 ef899790 2022-11-01 thomas
5254 ef899790 2022-11-01 thomas /*
5255 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5256 ef899790 2022-11-01 thomas * it as content to the diff engine.
5257 ef899790 2022-11-01 thomas */
5258 ef899790 2022-11-01 thomas static const struct got_error *
5259 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5260 ef899790 2022-11-01 thomas const char *abspath)
5261 ef899790 2022-11-01 thomas {
5262 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5263 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5264 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5265 ef899790 2022-11-01 thomas
5266 ef899790 2022-11-01 thomas *fd = -1;
5267 ef899790 2022-11-01 thomas
5268 ef899790 2022-11-01 thomas if (dirfd != -1) {
5269 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5270 ef899790 2022-11-01 thomas if (target_len == -1)
5271 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5272 ef899790 2022-11-01 thomas } else {
5273 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5274 ef899790 2022-11-01 thomas if (target_len == -1)
5275 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5276 ef899790 2022-11-01 thomas }
5277 ef899790 2022-11-01 thomas
5278 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5279 ef899790 2022-11-01 thomas if (*fd == -1)
5280 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5281 ef899790 2022-11-01 thomas
5282 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5283 ef899790 2022-11-01 thomas if (outlen == -1) {
5284 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5285 ef899790 2022-11-01 thomas goto done;
5286 ef899790 2022-11-01 thomas }
5287 ef899790 2022-11-01 thomas
5288 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5289 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5290 ef899790 2022-11-01 thomas goto done;
5291 ef899790 2022-11-01 thomas }
5292 ef899790 2022-11-01 thomas done:
5293 ef899790 2022-11-01 thomas if (err) {
5294 ef899790 2022-11-01 thomas close(*fd);
5295 ef899790 2022-11-01 thomas *fd = -1;
5296 ef899790 2022-11-01 thomas }
5297 ef899790 2022-11-01 thomas return err;
5298 ef899790 2022-11-01 thomas }
5299 ef899790 2022-11-01 thomas
5300 ef899790 2022-11-01 thomas static const struct got_error *
5301 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5302 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5303 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5304 ef899790 2022-11-01 thomas {
5305 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5306 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5307 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5308 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5309 ef899790 2022-11-01 thomas char *label1 = NULL;
5310 ef899790 2022-11-01 thomas struct stat sb;
5311 ef899790 2022-11-01 thomas off_t size1 = 0;
5312 ef899790 2022-11-01 thomas int f2_exists = 0;
5313 ef899790 2022-11-01 thomas char *id_str = NULL;
5314 ef899790 2022-11-01 thomas
5315 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5316 d259e491 2022-11-01 thomas
5317 d259e491 2022-11-01 thomas if (diff_staged) {
5318 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5319 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5320 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5321 d259e491 2022-11-01 thomas return NULL;
5322 d259e491 2022-11-01 thomas } else {
5323 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5324 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5325 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5326 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5327 d259e491 2022-11-01 thomas return NULL;
5328 d259e491 2022-11-01 thomas }
5329 ef899790 2022-11-01 thomas
5330 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5331 ef899790 2022-11-01 thomas if (err)
5332 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5333 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5334 ef899790 2022-11-01 thomas if (err)
5335 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5336 ef899790 2022-11-01 thomas
5337 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5338 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5339 ef899790 2022-11-01 thomas if (err)
5340 ef899790 2022-11-01 thomas return err;
5341 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5342 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5343 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5344 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5345 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5346 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5347 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5348 ef899790 2022-11-01 thomas }
5349 ef899790 2022-11-01 thomas
5350 ef899790 2022-11-01 thomas if (diff_staged) {
5351 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5352 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5353 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5354 ef899790 2022-11-01 thomas label1 = ct->path;
5355 ef899790 2022-11-01 thomas label2 = ct->path;
5356 ef899790 2022-11-01 thomas break;
5357 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5358 ef899790 2022-11-01 thomas label2 = ct->path;
5359 ef899790 2022-11-01 thomas break;
5360 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5361 ef899790 2022-11-01 thomas label1 = ct->path;
5362 ef899790 2022-11-01 thomas break;
5363 ef899790 2022-11-01 thomas default:
5364 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5365 ef899790 2022-11-01 thomas }
5366 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5367 ef899790 2022-11-01 thomas if (fd1 == -1) {
5368 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5369 ef899790 2022-11-01 thomas goto done;
5370 ef899790 2022-11-01 thomas }
5371 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5372 ef899790 2022-11-01 thomas if (fd2 == -1) {
5373 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5374 ef899790 2022-11-01 thomas goto done;
5375 ef899790 2022-11-01 thomas }
5376 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5377 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5378 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5379 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5380 ef899790 2022-11-01 thomas goto done;
5381 ef899790 2022-11-01 thomas }
5382 ef899790 2022-11-01 thomas
5383 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5384 ef899790 2022-11-01 thomas if (fd1 == -1) {
5385 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5386 ef899790 2022-11-01 thomas goto done;
5387 ef899790 2022-11-01 thomas }
5388 ef899790 2022-11-01 thomas
5389 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5390 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5391 ef899790 2022-11-01 thomas 8192, fd1);
5392 ef899790 2022-11-01 thomas if (err)
5393 ef899790 2022-11-01 thomas goto done;
5394 ef899790 2022-11-01 thomas }
5395 ef899790 2022-11-01 thomas
5396 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5397 ef899790 2022-11-01 thomas if (dirfd != -1) {
5398 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5399 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5400 ef899790 2022-11-01 thomas if (fd == -1) {
5401 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5402 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5403 ef899790 2022-11-01 thomas ct->ondisk_path);
5404 ef899790 2022-11-01 thomas goto done;
5405 ef899790 2022-11-01 thomas }
5406 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5407 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5408 ef899790 2022-11-01 thomas if (err)
5409 ef899790 2022-11-01 thomas goto done;
5410 ef899790 2022-11-01 thomas }
5411 ef899790 2022-11-01 thomas } else {
5412 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5413 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5414 ef899790 2022-11-01 thomas if (fd == -1) {
5415 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5416 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5417 ef899790 2022-11-01 thomas ct->ondisk_path);
5418 ef899790 2022-11-01 thomas goto done;
5419 ef899790 2022-11-01 thomas }
5420 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5421 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5422 ef899790 2022-11-01 thomas if (err)
5423 ef899790 2022-11-01 thomas goto done;
5424 ef899790 2022-11-01 thomas }
5425 ef899790 2022-11-01 thomas }
5426 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5427 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5428 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5429 ef899790 2022-11-01 thomas goto done;
5430 ef899790 2022-11-01 thomas }
5431 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5432 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5433 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5434 ef899790 2022-11-01 thomas goto done;
5435 ef899790 2022-11-01 thomas }
5436 ef899790 2022-11-01 thomas fd = -1;
5437 ef899790 2022-11-01 thomas f2_exists = 1;
5438 ef899790 2022-11-01 thomas }
5439 ef899790 2022-11-01 thomas
5440 ef899790 2022-11-01 thomas if (blob1) {
5441 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5442 ef899790 2022-11-01 thomas f1, blob1);
5443 ef899790 2022-11-01 thomas if (err)
5444 ef899790 2022-11-01 thomas goto done;
5445 ef899790 2022-11-01 thomas }
5446 ef899790 2022-11-01 thomas
5447 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5448 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5449 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5450 ef899790 2022-11-01 thomas done:
5451 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5452 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5453 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5454 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5455 ef899790 2022-11-01 thomas if (blob1)
5456 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5457 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5458 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5459 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5460 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5461 ef899790 2022-11-01 thomas return err;
5462 ef899790 2022-11-01 thomas }
5463 cf066bf8 2019-05-09 stsp
5464 c4296144 2019-05-09 stsp static const struct got_error *
5465 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5466 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5467 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5468 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5469 c4296144 2019-05-09 stsp {
5470 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5471 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5472 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5473 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5474 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5475 768aea60 2019-05-09 stsp struct stat sb;
5476 c4296144 2019-05-09 stsp
5477 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5478 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5479 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5480 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5481 5f8a88c6 2019-08-03 stsp return NULL;
5482 5f8a88c6 2019-08-03 stsp } else {
5483 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5484 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5485 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5486 be94c032 2023-02-20 thomas }
5487 c4296144 2019-05-09 stsp
5488 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5489 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5490 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5491 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5492 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5493 5f8a88c6 2019-08-03 stsp return NULL;
5494 5f8a88c6 2019-08-03 stsp }
5495 0b5cc0d6 2019-05-09 stsp
5496 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5497 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5498 036813ee 2019-05-09 stsp goto done;
5499 036813ee 2019-05-09 stsp }
5500 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5501 036813ee 2019-05-09 stsp parent_path = strdup("");
5502 69960a46 2019-05-09 stsp if (parent_path == NULL)
5503 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5504 69960a46 2019-05-09 stsp } else {
5505 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5506 69960a46 2019-05-09 stsp if (err)
5507 69960a46 2019-05-09 stsp return err;
5508 69960a46 2019-05-09 stsp }
5509 c4296144 2019-05-09 stsp
5510 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5511 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5512 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5513 c4296144 2019-05-09 stsp goto done;
5514 768aea60 2019-05-09 stsp }
5515 768aea60 2019-05-09 stsp
5516 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5517 768aea60 2019-05-09 stsp relpath) == -1) {
5518 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5519 768aea60 2019-05-09 stsp goto done;
5520 768aea60 2019-05-09 stsp }
5521 0aeb8099 2020-07-23 stsp
5522 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5523 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5524 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5525 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5526 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5527 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5528 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5529 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5530 0aeb8099 2020-07-23 stsp break;
5531 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5532 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5533 0aeb8099 2020-07-23 stsp break;
5534 0aeb8099 2020-07-23 stsp default:
5535 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5536 0aeb8099 2020-07-23 stsp goto done;
5537 0aeb8099 2020-07-23 stsp }
5538 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5539 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5540 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5541 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5542 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5543 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5544 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5545 12463d8b 2019-12-13 stsp ct->ondisk_path);
5546 12463d8b 2019-12-13 stsp goto done;
5547 12463d8b 2019-12-13 stsp }
5548 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5549 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5550 768aea60 2019-05-09 stsp goto done;
5551 768aea60 2019-05-09 stsp }
5552 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5553 c4296144 2019-05-09 stsp }
5554 c4296144 2019-05-09 stsp
5555 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5556 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5557 44d03001 2019-05-09 stsp relpath) == -1) {
5558 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5559 44d03001 2019-05-09 stsp goto done;
5560 44d03001 2019-05-09 stsp }
5561 44d03001 2019-05-09 stsp
5562 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5563 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5564 35213c7c 2020-07-23 stsp int is_bad_symlink;
5565 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5566 35213c7c 2020-07-23 stsp ssize_t target_len;
5567 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5568 35213c7c 2020-07-23 stsp sizeof(target_path));
5569 35213c7c 2020-07-23 stsp if (target_len == -1) {
5570 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5571 35213c7c 2020-07-23 stsp ct->ondisk_path);
5572 35213c7c 2020-07-23 stsp goto done;
5573 35213c7c 2020-07-23 stsp }
5574 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5575 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5576 35213c7c 2020-07-23 stsp if (err)
5577 35213c7c 2020-07-23 stsp goto done;
5578 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5579 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5580 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5581 35213c7c 2020-07-23 stsp goto done;
5582 35213c7c 2020-07-23 stsp }
5583 35213c7c 2020-07-23 stsp }
5584 35213c7c 2020-07-23 stsp
5585 35213c7c 2020-07-23 stsp
5586 cf066bf8 2019-05-09 stsp ct->status = status;
5587 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5588 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5589 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5590 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5591 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5592 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5593 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5594 b416585c 2019-05-13 stsp goto done;
5595 b416585c 2019-05-13 stsp }
5596 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5597 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5598 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5599 f0b75401 2019-08-03 stsp goto done;
5600 f0b75401 2019-08-03 stsp }
5601 f0b75401 2019-08-03 stsp }
5602 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5603 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5604 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5605 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5606 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5607 036813ee 2019-05-09 stsp goto done;
5608 036813ee 2019-05-09 stsp }
5609 ca2503ea 2019-05-09 stsp }
5610 24519714 2019-05-09 stsp ct->path = strdup(path);
5611 24519714 2019-05-09 stsp if (ct->path == NULL) {
5612 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5613 24519714 2019-05-09 stsp goto done;
5614 24519714 2019-05-09 stsp }
5615 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5616 ef899790 2022-11-01 thomas if (err)
5617 ef899790 2022-11-01 thomas goto done;
5618 ef899790 2022-11-01 thomas
5619 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5620 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5621 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5622 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5623 ef899790 2022-11-01 thomas if (err)
5624 ef899790 2022-11-01 thomas goto done;
5625 ef899790 2022-11-01 thomas }
5626 c4296144 2019-05-09 stsp done:
5627 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5628 ed175427 2019-05-09 stsp free_commitable(ct);
5629 24519714 2019-05-09 stsp free(parent_path);
5630 036813ee 2019-05-09 stsp free(path);
5631 a129376b 2019-03-28 stsp return err;
5632 a129376b 2019-03-28 stsp }
5633 c4296144 2019-05-09 stsp
5634 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5635 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5636 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5637 036813ee 2019-05-09 stsp struct got_repository *);
5638 ed175427 2019-05-09 stsp
5639 ed175427 2019-05-09 stsp static const struct got_error *
5640 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5641 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5642 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5643 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5644 afa376bf 2019-05-09 stsp struct got_repository *repo)
5645 ed175427 2019-05-09 stsp {
5646 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5647 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5648 036813ee 2019-05-09 stsp char *subpath;
5649 ed175427 2019-05-09 stsp
5650 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5651 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5652 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5653 ed175427 2019-05-09 stsp
5654 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5655 ed175427 2019-05-09 stsp if (err)
5656 ed175427 2019-05-09 stsp return err;
5657 ed175427 2019-05-09 stsp
5658 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5659 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5660 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5661 036813ee 2019-05-09 stsp free(subpath);
5662 036813ee 2019-05-09 stsp return err;
5663 036813ee 2019-05-09 stsp }
5664 ed175427 2019-05-09 stsp
5665 036813ee 2019-05-09 stsp static const struct got_error *
5666 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5667 036813ee 2019-05-09 stsp {
5668 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5669 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5670 ed175427 2019-05-09 stsp
5671 036813ee 2019-05-09 stsp *match = 0;
5672 ed175427 2019-05-09 stsp
5673 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5674 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5675 0f63689d 2019-05-10 stsp return NULL;
5676 036813ee 2019-05-09 stsp }
5677 0b5cc0d6 2019-05-09 stsp
5678 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5679 0f63689d 2019-05-10 stsp if (err)
5680 0f63689d 2019-05-10 stsp return err;
5681 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5682 036813ee 2019-05-09 stsp free(ct_parent_path);
5683 036813ee 2019-05-09 stsp return err;
5684 036813ee 2019-05-09 stsp }
5685 0b5cc0d6 2019-05-09 stsp
5686 768aea60 2019-05-09 stsp static mode_t
5687 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5688 768aea60 2019-05-09 stsp {
5689 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5690 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5691 3d9a4ec4 2020-07-23 stsp
5692 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5693 768aea60 2019-05-09 stsp }
5694 768aea60 2019-05-09 stsp
5695 036813ee 2019-05-09 stsp static const struct got_error *
5696 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5697 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5698 036813ee 2019-05-09 stsp {
5699 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5700 ca2503ea 2019-05-09 stsp
5701 036813ee 2019-05-09 stsp *new_te = NULL;
5702 0b5cc0d6 2019-05-09 stsp
5703 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5704 036813ee 2019-05-09 stsp if (err)
5705 036813ee 2019-05-09 stsp goto done;
5706 0b5cc0d6 2019-05-09 stsp
5707 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5708 036813ee 2019-05-09 stsp
5709 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5710 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5711 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5712 5f8a88c6 2019-08-03 stsp else
5713 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5714 036813ee 2019-05-09 stsp done:
5715 036813ee 2019-05-09 stsp if (err && *new_te) {
5716 56e0773d 2019-11-28 stsp free(*new_te);
5717 036813ee 2019-05-09 stsp *new_te = NULL;
5718 036813ee 2019-05-09 stsp }
5719 036813ee 2019-05-09 stsp return err;
5720 036813ee 2019-05-09 stsp }
5721 036813ee 2019-05-09 stsp
5722 036813ee 2019-05-09 stsp static const struct got_error *
5723 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5724 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5725 036813ee 2019-05-09 stsp {
5726 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5727 102b254e 2020-10-19 stsp char *ct_name = NULL;
5728 036813ee 2019-05-09 stsp
5729 036813ee 2019-05-09 stsp *new_te = NULL;
5730 036813ee 2019-05-09 stsp
5731 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5732 036813ee 2019-05-09 stsp if (*new_te == NULL)
5733 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5734 036813ee 2019-05-09 stsp
5735 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5736 102b254e 2020-10-19 stsp if (err)
5737 036813ee 2019-05-09 stsp goto done;
5738 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5739 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5740 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5741 036813ee 2019-05-09 stsp goto done;
5742 036813ee 2019-05-09 stsp }
5743 036813ee 2019-05-09 stsp
5744 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5745 036813ee 2019-05-09 stsp
5746 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5747 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5748 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5749 5f8a88c6 2019-08-03 stsp else
5750 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5751 036813ee 2019-05-09 stsp done:
5752 102b254e 2020-10-19 stsp free(ct_name);
5753 036813ee 2019-05-09 stsp if (err && *new_te) {
5754 56e0773d 2019-11-28 stsp free(*new_te);
5755 036813ee 2019-05-09 stsp *new_te = NULL;
5756 036813ee 2019-05-09 stsp }
5757 036813ee 2019-05-09 stsp return err;
5758 036813ee 2019-05-09 stsp }
5759 036813ee 2019-05-09 stsp
5760 036813ee 2019-05-09 stsp static const struct got_error *
5761 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5762 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5763 036813ee 2019-05-09 stsp {
5764 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5765 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5766 036813ee 2019-05-09 stsp
5767 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5768 036813ee 2019-05-09 stsp if (err)
5769 036813ee 2019-05-09 stsp return err;
5770 036813ee 2019-05-09 stsp if (new_pe == NULL)
5771 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5772 036813ee 2019-05-09 stsp return NULL;
5773 afa376bf 2019-05-09 stsp }
5774 afa376bf 2019-05-09 stsp
5775 afa376bf 2019-05-09 stsp static const struct got_error *
5776 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5777 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5778 afa376bf 2019-05-09 stsp {
5779 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5780 5f8a88c6 2019-08-03 stsp unsigned char status;
5781 ae1e948a 2021-09-28 thomas
5782 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5783 ae1e948a 2021-09-28 thomas return NULL;
5784 5f8a88c6 2019-08-03 stsp
5785 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5786 afa376bf 2019-05-09 stsp ct_path++;
5787 5f8a88c6 2019-08-03 stsp
5788 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5789 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5790 5f8a88c6 2019-08-03 stsp else
5791 5f8a88c6 2019-08-03 stsp status = ct->status;
5792 5f8a88c6 2019-08-03 stsp
5793 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5794 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5795 036813ee 2019-05-09 stsp }
5796 036813ee 2019-05-09 stsp
5797 036813ee 2019-05-09 stsp static const struct got_error *
5798 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5799 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5800 44d03001 2019-05-09 stsp {
5801 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5802 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5803 44d03001 2019-05-09 stsp char *te_path;
5804 44d03001 2019-05-09 stsp
5805 44d03001 2019-05-09 stsp *modified = 0;
5806 44d03001 2019-05-09 stsp
5807 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5808 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5809 44d03001 2019-05-09 stsp te->name) == -1)
5810 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5811 44d03001 2019-05-09 stsp
5812 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5813 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5814 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5815 44d03001 2019-05-09 stsp strlen(te_path));
5816 62d463ca 2020-10-20 naddy if (*modified)
5817 44d03001 2019-05-09 stsp break;
5818 44d03001 2019-05-09 stsp }
5819 44d03001 2019-05-09 stsp
5820 44d03001 2019-05-09 stsp free(te_path);
5821 44d03001 2019-05-09 stsp return err;
5822 44d03001 2019-05-09 stsp }
5823 44d03001 2019-05-09 stsp
5824 44d03001 2019-05-09 stsp static const struct got_error *
5825 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5826 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5827 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5828 036813ee 2019-05-09 stsp {
5829 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5830 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5831 036813ee 2019-05-09 stsp
5832 036813ee 2019-05-09 stsp *ctp = NULL;
5833 036813ee 2019-05-09 stsp
5834 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5835 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5836 036813ee 2019-05-09 stsp char *ct_name = NULL;
5837 036813ee 2019-05-09 stsp int path_matches;
5838 036813ee 2019-05-09 stsp
5839 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5840 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5841 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5842 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5843 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5844 5f8a88c6 2019-08-03 stsp continue;
5845 5f8a88c6 2019-08-03 stsp } else {
5846 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5847 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5848 5f8a88c6 2019-08-03 stsp continue;
5849 5f8a88c6 2019-08-03 stsp }
5850 036813ee 2019-05-09 stsp
5851 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5852 036813ee 2019-05-09 stsp continue;
5853 036813ee 2019-05-09 stsp
5854 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5855 62d463ca 2020-10-20 naddy if (err)
5856 036813ee 2019-05-09 stsp return err;
5857 036813ee 2019-05-09 stsp if (!path_matches)
5858 036813ee 2019-05-09 stsp continue;
5859 036813ee 2019-05-09 stsp
5860 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5861 d34b633e 2020-10-19 stsp if (err)
5862 d34b633e 2020-10-19 stsp return err;
5863 d34b633e 2020-10-19 stsp
5864 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5865 d34b633e 2020-10-19 stsp free(ct_name);
5866 036813ee 2019-05-09 stsp continue;
5867 d34b633e 2020-10-19 stsp }
5868 d34b633e 2020-10-19 stsp free(ct_name);
5869 036813ee 2019-05-09 stsp
5870 036813ee 2019-05-09 stsp *ctp = ct;
5871 036813ee 2019-05-09 stsp break;
5872 036813ee 2019-05-09 stsp }
5873 2b496619 2019-07-10 stsp
5874 2b496619 2019-07-10 stsp return err;
5875 2b496619 2019-07-10 stsp }
5876 2b496619 2019-07-10 stsp
5877 2b496619 2019-07-10 stsp static const struct got_error *
5878 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5879 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5880 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5881 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5882 2b496619 2019-07-10 stsp struct got_repository *repo)
5883 2b496619 2019-07-10 stsp {
5884 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5885 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5886 2b496619 2019-07-10 stsp char *subtree_path;
5887 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5888 ba580f68 2020-03-22 stsp int nentries;
5889 2b496619 2019-07-10 stsp
5890 2b496619 2019-07-10 stsp *new_tep = NULL;
5891 2b496619 2019-07-10 stsp
5892 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5893 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5894 2b496619 2019-07-10 stsp child_path) == -1)
5895 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5896 036813ee 2019-05-09 stsp
5897 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5898 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5899 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5900 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5901 56e0773d 2019-11-28 stsp
5902 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5903 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5904 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5905 2b496619 2019-07-10 stsp goto done;
5906 2b496619 2019-07-10 stsp }
5907 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5908 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5909 2b496619 2019-07-10 stsp if (err) {
5910 56e0773d 2019-11-28 stsp free(new_te);
5911 2b496619 2019-07-10 stsp goto done;
5912 2b496619 2019-07-10 stsp }
5913 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5914 2b496619 2019-07-10 stsp done:
5915 56e0773d 2019-11-28 stsp free(id);
5916 2b496619 2019-07-10 stsp free(subtree_path);
5917 2b496619 2019-07-10 stsp if (err == NULL)
5918 2b496619 2019-07-10 stsp *new_tep = new_te;
5919 036813ee 2019-05-09 stsp return err;
5920 036813ee 2019-05-09 stsp }
5921 036813ee 2019-05-09 stsp
5922 036813ee 2019-05-09 stsp static const struct got_error *
5923 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5924 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5925 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5926 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5927 036813ee 2019-05-09 stsp struct got_repository *repo)
5928 036813ee 2019-05-09 stsp {
5929 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5930 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5931 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5932 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5933 036813ee 2019-05-09 stsp
5934 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5935 ba580f68 2020-03-22 stsp *nentries = 0;
5936 036813ee 2019-05-09 stsp
5937 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5938 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5939 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5940 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5941 036813ee 2019-05-09 stsp
5942 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5943 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5944 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5945 036813ee 2019-05-09 stsp continue;
5946 036813ee 2019-05-09 stsp
5947 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5948 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5949 036813ee 2019-05-09 stsp continue;
5950 036813ee 2019-05-09 stsp
5951 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5952 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5953 036813ee 2019-05-09 stsp if (err)
5954 036813ee 2019-05-09 stsp goto done;
5955 036813ee 2019-05-09 stsp
5956 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5957 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5958 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5959 036813ee 2019-05-09 stsp if (err)
5960 036813ee 2019-05-09 stsp goto done;
5961 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5962 afa376bf 2019-05-09 stsp if (err)
5963 afa376bf 2019-05-09 stsp goto done;
5964 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5965 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5966 2b496619 2019-07-10 stsp if (err)
5967 2f51b5b3 2019-05-09 stsp goto done;
5968 ba580f68 2020-03-22 stsp (*nentries)++;
5969 2b496619 2019-07-10 stsp } else {
5970 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5971 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5972 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5973 2b496619 2019-07-10 stsp == NULL) {
5974 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5975 2b496619 2019-07-10 stsp child_path, path_base_tree,
5976 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5977 2b496619 2019-07-10 stsp repo);
5978 2b496619 2019-07-10 stsp if (err)
5979 2b496619 2019-07-10 stsp goto done;
5980 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5981 2b496619 2019-07-10 stsp if (err)
5982 2b496619 2019-07-10 stsp goto done;
5983 ba580f68 2020-03-22 stsp (*nentries)++;
5984 9ba0479c 2019-05-10 stsp }
5985 ed175427 2019-05-09 stsp }
5986 2f51b5b3 2019-05-09 stsp }
5987 2f51b5b3 2019-05-09 stsp
5988 2f51b5b3 2019-05-09 stsp if (base_tree) {
5989 56e0773d 2019-11-28 stsp int i, nbase_entries;
5990 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5991 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5992 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5993 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5994 63c5ca5d 2019-08-24 stsp
5995 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5996 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5997 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5998 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5999 63c5ca5d 2019-08-24 stsp if (err)
6000 63c5ca5d 2019-08-24 stsp goto done;
6001 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6002 63c5ca5d 2019-08-24 stsp if (err)
6003 63c5ca5d 2019-08-24 stsp goto done;
6004 ba580f68 2020-03-22 stsp (*nentries)++;
6005 63c5ca5d 2019-08-24 stsp continue;
6006 63c5ca5d 2019-08-24 stsp }
6007 2f51b5b3 2019-05-09 stsp
6008 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6009 44d03001 2019-05-09 stsp int modified;
6010 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6011 036813ee 2019-05-09 stsp if (err)
6012 036813ee 2019-05-09 stsp goto done;
6013 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6014 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6015 2f51b5b3 2019-05-09 stsp if (err)
6016 2f51b5b3 2019-05-09 stsp goto done;
6017 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6018 44d03001 2019-05-09 stsp if (modified) {
6019 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6020 ba580f68 2020-03-22 stsp int nsubentries;
6021 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6022 ba580f68 2020-03-22 stsp &nsubentries, te,
6023 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6024 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6025 44d03001 2019-05-09 stsp if (err)
6026 44d03001 2019-05-09 stsp goto done;
6027 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6028 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6029 ba580f68 2020-03-22 stsp free(new_id);
6030 ba580f68 2020-03-22 stsp continue;
6031 ba580f68 2020-03-22 stsp }
6032 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6033 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6034 56e0773d 2019-11-28 stsp free(new_id);
6035 44d03001 2019-05-09 stsp }
6036 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6037 036813ee 2019-05-09 stsp if (err)
6038 036813ee 2019-05-09 stsp goto done;
6039 ba580f68 2020-03-22 stsp (*nentries)++;
6040 2f51b5b3 2019-05-09 stsp continue;
6041 036813ee 2019-05-09 stsp }
6042 2f51b5b3 2019-05-09 stsp
6043 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6044 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6045 f66c734c 2019-09-22 stsp if (err)
6046 f66c734c 2019-09-22 stsp goto done;
6047 2f51b5b3 2019-05-09 stsp if (ct) {
6048 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6049 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6050 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6051 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6052 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6053 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6054 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6055 2f51b5b3 2019-05-09 stsp if (err)
6056 2f51b5b3 2019-05-09 stsp goto done;
6057 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6058 2f51b5b3 2019-05-09 stsp if (err)
6059 2f51b5b3 2019-05-09 stsp goto done;
6060 ba580f68 2020-03-22 stsp (*nentries)++;
6061 2f51b5b3 2019-05-09 stsp }
6062 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6063 b416585c 2019-05-13 stsp status_arg);
6064 afa376bf 2019-05-09 stsp if (err)
6065 afa376bf 2019-05-09 stsp goto done;
6066 2f51b5b3 2019-05-09 stsp } else {
6067 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6068 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6069 2f51b5b3 2019-05-09 stsp if (err)
6070 2f51b5b3 2019-05-09 stsp goto done;
6071 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6072 2f51b5b3 2019-05-09 stsp if (err)
6073 2f51b5b3 2019-05-09 stsp goto done;
6074 ba580f68 2020-03-22 stsp (*nentries)++;
6075 2f51b5b3 2019-05-09 stsp }
6076 ca2503ea 2019-05-09 stsp }
6077 ed175427 2019-05-09 stsp }
6078 0b5cc0d6 2019-05-09 stsp
6079 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6080 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6081 ed175427 2019-05-09 stsp done:
6082 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6083 2e1fa222 2020-07-23 stsp return err;
6084 2e1fa222 2020-07-23 stsp }
6085 2e1fa222 2020-07-23 stsp
6086 2e1fa222 2020-07-23 stsp static const struct got_error *
6087 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6088 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6089 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6090 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6091 ebf99748 2019-05-09 stsp {
6092 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6093 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6094 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6095 ebf99748 2019-05-09 stsp
6096 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6097 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6098 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6099 ebf99748 2019-05-09 stsp
6100 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6101 437adc9d 2020-12-10 yzhong
6102 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6103 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6104 437adc9d 2020-12-10 yzhong if (err)
6105 437adc9d 2020-12-10 yzhong goto done;
6106 437adc9d 2020-12-10 yzhong
6107 ebf99748 2019-05-09 stsp if (ie) {
6108 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6109 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6110 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6111 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6112 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6113 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6114 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6115 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6116 437adc9d 2020-12-10 yzhong
6117 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6118 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6119 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6120 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6121 72fd46fa 2019-09-06 stsp !have_staged_files);
6122 ebf99748 2019-05-09 stsp } else
6123 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6124 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6125 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6126 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6127 72fd46fa 2019-09-06 stsp !have_staged_files);
6128 ebf99748 2019-05-09 stsp } else {
6129 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6130 ebf99748 2019-05-09 stsp if (err)
6131 437adc9d 2020-12-10 yzhong goto done;
6132 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6133 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6134 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6135 3969253a 2020-03-07 stsp if (err) {
6136 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6137 437adc9d 2020-12-10 yzhong goto done;
6138 3969253a 2020-03-07 stsp }
6139 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6140 3969253a 2020-03-07 stsp if (err) {
6141 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6142 437adc9d 2020-12-10 yzhong goto done;
6143 3969253a 2020-03-07 stsp }
6144 ebf99748 2019-05-09 stsp }
6145 437adc9d 2020-12-10 yzhong free(relpath);
6146 437adc9d 2020-12-10 yzhong relpath = NULL;
6147 ebf99748 2019-05-09 stsp }
6148 437adc9d 2020-12-10 yzhong done:
6149 437adc9d 2020-12-10 yzhong free(relpath);
6150 d56d26ce 2019-05-10 stsp return err;
6151 d56d26ce 2019-05-10 stsp }
6152 735ef5ac 2019-08-03 stsp
6153 d56d26ce 2019-05-10 stsp
6154 d56d26ce 2019-05-10 stsp static const struct got_error *
6155 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6156 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6157 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6158 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6159 735ef5ac 2019-08-03 stsp int ood_errcode)
6160 d56d26ce 2019-05-10 stsp {
6161 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6162 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6163 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6164 1a36436d 2019-06-10 stsp
6165 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6166 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6167 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6168 1a36436d 2019-06-10 stsp return NULL;
6169 9bead371 2019-07-28 stsp /*
6170 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6171 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6172 9bead371 2019-07-28 stsp */
6173 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6174 945f9229 2022-04-16 thomas if (err)
6175 945f9229 2022-04-16 thomas goto done;
6176 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6177 9bead371 2019-07-28 stsp if (err) {
6178 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6179 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6180 1a36436d 2019-06-10 stsp goto done;
6181 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6182 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6183 1a36436d 2019-06-10 stsp } else {
6184 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6185 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6186 945f9229 2022-04-16 thomas if (err)
6187 945f9229 2022-04-16 thomas goto done;
6188 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6189 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6190 1a36436d 2019-06-10 stsp goto done;
6191 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6192 1a36436d 2019-06-10 stsp }
6193 1a36436d 2019-06-10 stsp done:
6194 1a36436d 2019-06-10 stsp free(id);
6195 945f9229 2022-04-16 thomas if (commit)
6196 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6197 1a36436d 2019-06-10 stsp return err;
6198 ebf99748 2019-05-09 stsp }
6199 ebf99748 2019-05-09 stsp
6200 ef20f542 2022-06-26 thomas static const struct got_error *
6201 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6202 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6203 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6204 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6205 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6206 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6207 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6208 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6209 afa376bf 2019-05-09 stsp struct got_repository *repo)
6210 c4296144 2019-05-09 stsp {
6211 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6212 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6213 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6214 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6215 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6216 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6217 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6218 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6219 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6220 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6221 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6222 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6223 892eab0a 2022-07-22 thomas time_t timestamp;
6224 c4296144 2019-05-09 stsp
6225 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6226 c4296144 2019-05-09 stsp
6227 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6228 675c7539 2019-05-09 stsp
6229 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6230 588edf97 2019-05-10 stsp if (err)
6231 588edf97 2019-05-10 stsp goto done;
6232 de18fc63 2019-05-09 stsp
6233 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6234 588edf97 2019-05-10 stsp if (err)
6235 588edf97 2019-05-10 stsp goto done;
6236 588edf97 2019-05-10 stsp
6237 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6238 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6239 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6240 33ad4cbe 2019-05-12 jcs if (err)
6241 33ad4cbe 2019-05-12 jcs goto done;
6242 33ad4cbe 2019-05-12 jcs }
6243 c4296144 2019-05-09 stsp
6244 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6245 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6246 33ad4cbe 2019-05-12 jcs goto done;
6247 33ad4cbe 2019-05-12 jcs }
6248 33ad4cbe 2019-05-12 jcs
6249 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6250 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6251 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6252 cf066bf8 2019-05-09 stsp char *ondisk_path;
6253 cf066bf8 2019-05-09 stsp
6254 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6255 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6256 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6257 5f8a88c6 2019-08-03 stsp continue;
6258 5f8a88c6 2019-08-03 stsp
6259 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6260 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6261 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6262 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6263 cf066bf8 2019-05-09 stsp continue;
6264 cf066bf8 2019-05-09 stsp
6265 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6266 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6267 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6268 cf066bf8 2019-05-09 stsp goto done;
6269 cf066bf8 2019-05-09 stsp }
6270 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6271 2e1fa222 2020-07-23 stsp free(ondisk_path);
6272 2e1fa222 2020-07-23 stsp if (err)
6273 cf066bf8 2019-05-09 stsp goto done;
6274 cf066bf8 2019-05-09 stsp }
6275 036813ee 2019-05-09 stsp
6276 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6277 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6278 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6279 036813ee 2019-05-09 stsp if (err)
6280 036813ee 2019-05-09 stsp goto done;
6281 036813ee 2019-05-09 stsp
6282 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6283 de18fc63 2019-05-09 stsp if (err)
6284 de18fc63 2019-05-09 stsp goto done;
6285 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6286 10604dce 2021-09-24 thomas nparents++;
6287 10604dce 2021-09-24 thomas if (parent_id2) {
6288 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6289 10604dce 2021-09-24 thomas if (err)
6290 10604dce 2021-09-24 thomas goto done;
6291 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6292 10604dce 2021-09-24 thomas nparents++;
6293 10604dce 2021-09-24 thomas }
6294 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6295 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6296 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6297 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6298 33ad4cbe 2019-05-12 jcs free(logmsg);
6299 9d40349a 2019-05-09 stsp if (err)
6300 9d40349a 2019-05-09 stsp goto done;
6301 9d40349a 2019-05-09 stsp
6302 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6303 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6304 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6305 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6306 09f5bd90 2019-05-09 stsp goto done;
6307 09f5bd90 2019-05-09 stsp }
6308 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6309 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6310 09f5bd90 2019-05-09 stsp if (err)
6311 09f5bd90 2019-05-09 stsp goto done;
6312 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6313 ebf99748 2019-05-09 stsp if (err)
6314 ebf99748 2019-05-09 stsp goto done;
6315 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6316 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6317 09f5bd90 2019-05-09 stsp goto done;
6318 09f5bd90 2019-05-09 stsp }
6319 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6320 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6321 f2c16586 2019-05-09 stsp if (err)
6322 f2c16586 2019-05-09 stsp goto done;
6323 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6324 f2c16586 2019-05-09 stsp if (err)
6325 f2c16586 2019-05-09 stsp goto done;
6326 f2c16586 2019-05-09 stsp
6327 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6328 09f5bd90 2019-05-09 stsp if (err)
6329 09f5bd90 2019-05-09 stsp goto done;
6330 09f5bd90 2019-05-09 stsp
6331 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6332 ebf99748 2019-05-09 stsp if (err)
6333 ebf99748 2019-05-09 stsp goto done;
6334 c4296144 2019-05-09 stsp done:
6335 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6336 588edf97 2019-05-10 stsp if (head_tree)
6337 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6338 588edf97 2019-05-10 stsp if (head_commit)
6339 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6340 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6341 2f17228e 2019-05-12 stsp if (head_ref2) {
6342 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6343 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6344 2f17228e 2019-05-12 stsp err = unlockerr;
6345 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6346 39cd0ff6 2019-07-12 stsp }
6347 39cd0ff6 2019-07-12 stsp return err;
6348 39cd0ff6 2019-07-12 stsp }
6349 5c1e53bc 2019-07-28 stsp
6350 5c1e53bc 2019-07-28 stsp static const struct got_error *
6351 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6352 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6353 5c1e53bc 2019-07-28 stsp {
6354 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6355 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6356 39cd0ff6 2019-07-12 stsp
6357 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6358 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6359 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6360 5c1e53bc 2019-07-28 stsp
6361 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6362 5c1e53bc 2019-07-28 stsp ct_path++;
6363 5c1e53bc 2019-07-28 stsp
6364 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6365 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6366 5c1e53bc 2019-07-28 stsp break;
6367 5c1e53bc 2019-07-28 stsp }
6368 5c1e53bc 2019-07-28 stsp
6369 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6370 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6371 5c1e53bc 2019-07-28 stsp
6372 5c1e53bc 2019-07-28 stsp return NULL;
6373 5c1e53bc 2019-07-28 stsp }
6374 5c1e53bc 2019-07-28 stsp
6375 f0b75401 2019-08-03 stsp static const struct got_error *
6376 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6377 f0b75401 2019-08-03 stsp {
6378 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6379 f0b75401 2019-08-03 stsp
6380 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6381 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6382 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6383 f0b75401 2019-08-03 stsp }
6384 f0b75401 2019-08-03 stsp
6385 f0b75401 2019-08-03 stsp return NULL;
6386 f0b75401 2019-08-03 stsp }
6387 f0b75401 2019-08-03 stsp
6388 5f8a88c6 2019-08-03 stsp static const struct got_error *
6389 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6390 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6391 5f8a88c6 2019-08-03 stsp {
6392 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6393 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6394 5f8a88c6 2019-08-03 stsp
6395 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6396 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6397 5f8a88c6 2019-08-03 stsp continue;
6398 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6399 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6400 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6401 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6402 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6403 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6404 5f8a88c6 2019-08-03 stsp }
6405 5f8a88c6 2019-08-03 stsp
6406 5f8a88c6 2019-08-03 stsp return NULL;
6407 5f8a88c6 2019-08-03 stsp }
6408 5f8a88c6 2019-08-03 stsp
6409 39cd0ff6 2019-07-12 stsp const struct got_error *
6410 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6411 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6412 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6413 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6414 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6415 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6416 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6417 39cd0ff6 2019-07-12 stsp {
6418 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6419 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6420 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6421 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6422 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6423 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6424 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6425 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6426 ef899790 2022-11-01 thomas char *diff_path = NULL;
6427 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6428 39cd0ff6 2019-07-12 stsp
6429 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6430 39cd0ff6 2019-07-12 stsp
6431 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6432 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6433 39cd0ff6 2019-07-12 stsp
6434 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6435 39cd0ff6 2019-07-12 stsp if (err)
6436 39cd0ff6 2019-07-12 stsp goto done;
6437 39cd0ff6 2019-07-12 stsp
6438 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6439 39cd0ff6 2019-07-12 stsp if (err)
6440 39cd0ff6 2019-07-12 stsp goto done;
6441 39cd0ff6 2019-07-12 stsp
6442 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6443 39cd0ff6 2019-07-12 stsp if (err)
6444 39cd0ff6 2019-07-12 stsp goto done;
6445 39cd0ff6 2019-07-12 stsp
6446 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6447 39cd0ff6 2019-07-12 stsp if (err)
6448 39cd0ff6 2019-07-12 stsp goto done;
6449 39cd0ff6 2019-07-12 stsp
6450 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6451 5f8a88c6 2019-08-03 stsp &have_staged_files);
6452 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6453 5f8a88c6 2019-08-03 stsp goto done;
6454 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6455 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6456 5f8a88c6 2019-08-03 stsp if (err)
6457 5f8a88c6 2019-08-03 stsp goto done;
6458 5f8a88c6 2019-08-03 stsp }
6459 5f8a88c6 2019-08-03 stsp
6460 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6461 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6462 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6463 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6464 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6465 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6466 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6467 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6468 ef899790 2022-11-01 thomas if (show_diff) {
6469 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6470 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6471 ef899790 2022-11-01 thomas if (err)
6472 ef899790 2022-11-01 thomas goto done;
6473 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6474 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6475 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6476 ef899790 2022-11-01 thomas goto done;
6477 ef899790 2022-11-01 thomas }
6478 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6479 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6480 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6481 ef899790 2022-11-01 thomas goto done;
6482 ef899790 2022-11-01 thomas }
6483 ef899790 2022-11-01 thomas }
6484 ef899790 2022-11-01 thomas
6485 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6486 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6487 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6488 5c1e53bc 2019-07-28 stsp if (err)
6489 5c1e53bc 2019-07-28 stsp goto done;
6490 5c1e53bc 2019-07-28 stsp }
6491 39cd0ff6 2019-07-12 stsp
6492 ef899790 2022-11-01 thomas if (show_diff) {
6493 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6494 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6495 ef899790 2022-11-01 thomas goto done;
6496 ef899790 2022-11-01 thomas }
6497 ef899790 2022-11-01 thomas }
6498 ef899790 2022-11-01 thomas
6499 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6500 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6501 39cd0ff6 2019-07-12 stsp goto done;
6502 5c1e53bc 2019-07-28 stsp }
6503 5c1e53bc 2019-07-28 stsp
6504 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6505 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6506 5c1e53bc 2019-07-28 stsp if (err)
6507 5c1e53bc 2019-07-28 stsp goto done;
6508 39cd0ff6 2019-07-12 stsp }
6509 f0b75401 2019-08-03 stsp
6510 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6511 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6512 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6513 f0b75401 2019-08-03 stsp
6514 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6515 f0b75401 2019-08-03 stsp ct_path++;
6516 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6517 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6518 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6519 b50cabdf 2019-07-12 stsp if (err)
6520 b50cabdf 2019-07-12 stsp goto done;
6521 f0b75401 2019-08-03 stsp
6522 b50cabdf 2019-07-12 stsp }
6523 b50cabdf 2019-07-12 stsp
6524 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6525 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6526 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6527 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6528 39cd0ff6 2019-07-12 stsp if (err)
6529 39cd0ff6 2019-07-12 stsp goto done;
6530 39cd0ff6 2019-07-12 stsp
6531 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6532 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6533 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6534 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6535 39cd0ff6 2019-07-12 stsp err = sync_err;
6536 39cd0ff6 2019-07-12 stsp done:
6537 39cd0ff6 2019-07-12 stsp if (fileindex)
6538 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6539 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6540 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6541 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6542 39cd0ff6 2019-07-12 stsp err = unlockerr;
6543 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6544 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6545 21c2d8be 2023-01-10 thomas
6546 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6547 39cd0ff6 2019-07-12 stsp }
6548 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6549 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6550 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6551 ef899790 2022-11-01 thomas free(diff_path);
6552 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6553 ef899790 2022-11-01 thomas err == NULL)
6554 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6555 c4296144 2019-05-09 stsp return err;
6556 8656d6c4 2019-05-20 stsp }
6557 8656d6c4 2019-05-20 stsp
6558 8656d6c4 2019-05-20 stsp const char *
6559 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6560 8656d6c4 2019-05-20 stsp {
6561 8656d6c4 2019-05-20 stsp return ct->path;
6562 8656d6c4 2019-05-20 stsp }
6563 8656d6c4 2019-05-20 stsp
6564 8656d6c4 2019-05-20 stsp unsigned int
6565 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6566 8656d6c4 2019-05-20 stsp {
6567 8656d6c4 2019-05-20 stsp return ct->status;
6568 818c7501 2019-07-11 stsp }
6569 818c7501 2019-07-11 stsp
6570 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6571 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6572 818c7501 2019-07-11 stsp struct got_repository *repo;
6573 818c7501 2019-07-11 stsp };
6574 818c7501 2019-07-11 stsp
6575 818c7501 2019-07-11 stsp static const struct got_error *
6576 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6577 818c7501 2019-07-11 stsp {
6578 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6579 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6580 818c7501 2019-07-11 stsp unsigned char status;
6581 818c7501 2019-07-11 stsp struct stat sb;
6582 818c7501 2019-07-11 stsp char *ondisk_path;
6583 818c7501 2019-07-11 stsp
6584 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6585 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6586 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6587 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6588 818c7501 2019-07-11 stsp
6589 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6590 818c7501 2019-07-11 stsp == -1)
6591 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6592 818c7501 2019-07-11 stsp
6593 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6594 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6595 818c7501 2019-07-11 stsp free(ondisk_path);
6596 818c7501 2019-07-11 stsp if (err)
6597 818c7501 2019-07-11 stsp return err;
6598 818c7501 2019-07-11 stsp
6599 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6600 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6601 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6602 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6603 818c7501 2019-07-11 stsp
6604 818c7501 2019-07-11 stsp return NULL;
6605 818c7501 2019-07-11 stsp }
6606 818c7501 2019-07-11 stsp
6607 818c7501 2019-07-11 stsp const struct got_error *
6608 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6609 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6610 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6611 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6612 818c7501 2019-07-11 stsp {
6613 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6614 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6615 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6616 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6617 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6618 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6619 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6620 818c7501 2019-07-11 stsp
6621 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6622 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6623 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6624 818c7501 2019-07-11 stsp
6625 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6626 818c7501 2019-07-11 stsp if (err)
6627 818c7501 2019-07-11 stsp return err;
6628 818c7501 2019-07-11 stsp
6629 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6630 818c7501 2019-07-11 stsp if (err)
6631 818c7501 2019-07-11 stsp goto done;
6632 818c7501 2019-07-11 stsp
6633 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6634 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6635 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6636 818c7501 2019-07-11 stsp &ok_arg);
6637 818c7501 2019-07-11 stsp if (err)
6638 818c7501 2019-07-11 stsp goto done;
6639 818c7501 2019-07-11 stsp
6640 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6641 818c7501 2019-07-11 stsp if (err)
6642 818c7501 2019-07-11 stsp goto done;
6643 818c7501 2019-07-11 stsp
6644 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6645 818c7501 2019-07-11 stsp if (err)
6646 818c7501 2019-07-11 stsp goto done;
6647 818c7501 2019-07-11 stsp
6648 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6649 818c7501 2019-07-11 stsp if (err)
6650 818c7501 2019-07-11 stsp goto done;
6651 818c7501 2019-07-11 stsp
6652 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6653 818c7501 2019-07-11 stsp 0);
6654 e51d7b55 2020-01-04 stsp if (err)
6655 e51d7b55 2020-01-04 stsp goto done;
6656 e51d7b55 2020-01-04 stsp
6657 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6658 818c7501 2019-07-11 stsp if (err)
6659 818c7501 2019-07-11 stsp goto done;
6660 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6661 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6662 e51d7b55 2020-01-04 stsp goto done;
6663 e51d7b55 2020-01-04 stsp }
6664 818c7501 2019-07-11 stsp
6665 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6666 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6667 818c7501 2019-07-11 stsp if (err)
6668 818c7501 2019-07-11 stsp goto done;
6669 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6670 818c7501 2019-07-11 stsp if (err)
6671 818c7501 2019-07-11 stsp goto done;
6672 818c7501 2019-07-11 stsp
6673 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6674 818c7501 2019-07-11 stsp
6675 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6676 818c7501 2019-07-11 stsp if (err)
6677 818c7501 2019-07-11 stsp goto done;
6678 818c7501 2019-07-11 stsp
6679 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6680 818c7501 2019-07-11 stsp if (err)
6681 818c7501 2019-07-11 stsp goto done;
6682 818c7501 2019-07-11 stsp
6683 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6684 818c7501 2019-07-11 stsp worktree->base_commit_id);
6685 818c7501 2019-07-11 stsp if (err)
6686 818c7501 2019-07-11 stsp goto done;
6687 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6688 818c7501 2019-07-11 stsp if (err)
6689 818c7501 2019-07-11 stsp goto done;
6690 818c7501 2019-07-11 stsp
6691 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6692 818c7501 2019-07-11 stsp if (err)
6693 818c7501 2019-07-11 stsp goto done;
6694 818c7501 2019-07-11 stsp done:
6695 818c7501 2019-07-11 stsp free(fileindex_path);
6696 818c7501 2019-07-11 stsp free(tmp_branch_name);
6697 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6698 818c7501 2019-07-11 stsp free(branch_ref_name);
6699 818c7501 2019-07-11 stsp if (branch_ref)
6700 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6701 818c7501 2019-07-11 stsp if (wt_branch)
6702 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6703 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6704 818c7501 2019-07-11 stsp if (err) {
6705 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6706 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6707 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6708 818c7501 2019-07-11 stsp }
6709 818c7501 2019-07-11 stsp if (*tmp_branch) {
6710 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6711 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6712 818c7501 2019-07-11 stsp }
6713 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6714 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6715 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6716 3e3a69f1 2019-07-25 stsp }
6717 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6718 818c7501 2019-07-11 stsp }
6719 818c7501 2019-07-11 stsp return err;
6720 818c7501 2019-07-11 stsp }
6721 818c7501 2019-07-11 stsp
6722 818c7501 2019-07-11 stsp const struct got_error *
6723 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6724 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6725 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6726 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6727 818c7501 2019-07-11 stsp {
6728 818c7501 2019-07-11 stsp const struct got_error *err;
6729 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6730 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6731 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6732 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6733 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp *commit_id = NULL;
6736 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6737 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6738 3e3a69f1 2019-07-25 stsp *branch = NULL;
6739 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6740 3e3a69f1 2019-07-25 stsp
6741 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6742 3e3a69f1 2019-07-25 stsp if (err)
6743 3e3a69f1 2019-07-25 stsp return err;
6744 818c7501 2019-07-11 stsp
6745 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6746 3e3a69f1 2019-07-25 stsp if (err)
6747 f032f1f7 2019-08-04 stsp goto done;
6748 f032f1f7 2019-08-04 stsp
6749 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6750 f032f1f7 2019-08-04 stsp &have_staged_files);
6751 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6752 f032f1f7 2019-08-04 stsp goto done;
6753 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6754 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6755 3e3a69f1 2019-07-25 stsp goto done;
6756 f032f1f7 2019-08-04 stsp }
6757 3e3a69f1 2019-07-25 stsp
6758 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6759 818c7501 2019-07-11 stsp if (err)
6760 f032f1f7 2019-08-04 stsp goto done;
6761 818c7501 2019-07-11 stsp
6762 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6763 818c7501 2019-07-11 stsp if (err)
6764 818c7501 2019-07-11 stsp goto done;
6765 818c7501 2019-07-11 stsp
6766 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6767 818c7501 2019-07-11 stsp if (err)
6768 818c7501 2019-07-11 stsp goto done;
6769 818c7501 2019-07-11 stsp
6770 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6771 818c7501 2019-07-11 stsp if (err)
6772 818c7501 2019-07-11 stsp goto done;
6773 818c7501 2019-07-11 stsp
6774 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6775 818c7501 2019-07-11 stsp if (err)
6776 818c7501 2019-07-11 stsp goto done;
6777 818c7501 2019-07-11 stsp
6778 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6779 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6780 818c7501 2019-07-11 stsp if (err)
6781 818c7501 2019-07-11 stsp goto done;
6782 818c7501 2019-07-11 stsp
6783 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6784 818c7501 2019-07-11 stsp if (err)
6785 818c7501 2019-07-11 stsp goto done;
6786 818c7501 2019-07-11 stsp
6787 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6788 818c7501 2019-07-11 stsp if (err)
6789 818c7501 2019-07-11 stsp goto done;
6790 818c7501 2019-07-11 stsp
6791 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6792 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6793 818c7501 2019-07-11 stsp if (err)
6794 818c7501 2019-07-11 stsp goto done;
6795 818c7501 2019-07-11 stsp
6796 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6797 818c7501 2019-07-11 stsp if (err)
6798 818c7501 2019-07-11 stsp goto done;
6799 818c7501 2019-07-11 stsp done:
6800 818c7501 2019-07-11 stsp free(commit_ref_name);
6801 818c7501 2019-07-11 stsp free(branch_ref_name);
6802 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6803 818c7501 2019-07-11 stsp if (commit_ref)
6804 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6805 818c7501 2019-07-11 stsp if (branch_ref)
6806 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6807 818c7501 2019-07-11 stsp if (err) {
6808 818c7501 2019-07-11 stsp free(*commit_id);
6809 818c7501 2019-07-11 stsp *commit_id = NULL;
6810 818c7501 2019-07-11 stsp if (*tmp_branch) {
6811 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6812 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6813 818c7501 2019-07-11 stsp }
6814 818c7501 2019-07-11 stsp if (*new_base_branch) {
6815 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6816 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6817 818c7501 2019-07-11 stsp }
6818 818c7501 2019-07-11 stsp if (*branch) {
6819 818c7501 2019-07-11 stsp got_ref_close(*branch);
6820 818c7501 2019-07-11 stsp *branch = NULL;
6821 818c7501 2019-07-11 stsp }
6822 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6823 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6824 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6825 3e3a69f1 2019-07-25 stsp }
6826 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6827 818c7501 2019-07-11 stsp }
6828 818c7501 2019-07-11 stsp return err;
6829 c4296144 2019-05-09 stsp }
6830 818c7501 2019-07-11 stsp
6831 818c7501 2019-07-11 stsp const struct got_error *
6832 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6833 818c7501 2019-07-11 stsp {
6834 818c7501 2019-07-11 stsp const struct got_error *err;
6835 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6836 818c7501 2019-07-11 stsp
6837 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6838 818c7501 2019-07-11 stsp if (err)
6839 818c7501 2019-07-11 stsp return err;
6840 818c7501 2019-07-11 stsp
6841 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6842 818c7501 2019-07-11 stsp free(tmp_branch_name);
6843 818c7501 2019-07-11 stsp return NULL;
6844 818c7501 2019-07-11 stsp }
6845 818c7501 2019-07-11 stsp
6846 818c7501 2019-07-11 stsp static const struct got_error *
6847 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6848 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6849 818c7501 2019-07-11 stsp {
6850 0ebf8283 2019-07-24 stsp *logmsg = arg;
6851 818c7501 2019-07-11 stsp return NULL;
6852 818c7501 2019-07-11 stsp }
6853 818c7501 2019-07-11 stsp
6854 818c7501 2019-07-11 stsp static const struct got_error *
6855 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6856 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6857 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6858 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6859 818c7501 2019-07-11 stsp {
6860 818c7501 2019-07-11 stsp return NULL;
6861 01757395 2019-07-12 stsp }
6862 01757395 2019-07-12 stsp
6863 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6864 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6865 01757395 2019-07-12 stsp void *progress_arg;
6866 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6867 01757395 2019-07-12 stsp };
6868 01757395 2019-07-12 stsp
6869 01757395 2019-07-12 stsp static const struct got_error *
6870 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6871 01757395 2019-07-12 stsp {
6872 01757395 2019-07-12 stsp const struct got_error *err;
6873 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6874 01757395 2019-07-12 stsp char *p;
6875 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6876 01757395 2019-07-12 stsp
6877 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6878 01757395 2019-07-12 stsp if (err)
6879 01757395 2019-07-12 stsp return err;
6880 01757395 2019-07-12 stsp
6881 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6882 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6883 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6884 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6885 01757395 2019-07-12 stsp return NULL;
6886 01757395 2019-07-12 stsp
6887 01757395 2019-07-12 stsp p = strdup(path);
6888 01757395 2019-07-12 stsp if (p == NULL)
6889 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6890 01757395 2019-07-12 stsp
6891 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6892 01757395 2019-07-12 stsp if (err || new == NULL)
6893 01757395 2019-07-12 stsp free(p);
6894 01757395 2019-07-12 stsp return err;
6895 818c7501 2019-07-11 stsp }
6896 818c7501 2019-07-11 stsp
6897 0ebf8283 2019-07-24 stsp static const struct got_error *
6898 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6899 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6900 818c7501 2019-07-11 stsp {
6901 818c7501 2019-07-11 stsp const struct got_error *err;
6902 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6903 818c7501 2019-07-11 stsp
6904 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6905 818c7501 2019-07-11 stsp if (err) {
6906 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6907 818c7501 2019-07-11 stsp goto done;
6908 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6909 818c7501 2019-07-11 stsp if (err)
6910 818c7501 2019-07-11 stsp goto done;
6911 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6912 818c7501 2019-07-11 stsp if (err)
6913 818c7501 2019-07-11 stsp goto done;
6914 de05890f 2020-03-05 stsp } else if (is_rebase) {
6915 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6916 818c7501 2019-07-11 stsp int cmp;
6917 818c7501 2019-07-11 stsp
6918 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6919 818c7501 2019-07-11 stsp if (err)
6920 818c7501 2019-07-11 stsp goto done;
6921 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6922 818c7501 2019-07-11 stsp free(stored_id);
6923 818c7501 2019-07-11 stsp if (cmp != 0) {
6924 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6925 818c7501 2019-07-11 stsp goto done;
6926 818c7501 2019-07-11 stsp }
6927 818c7501 2019-07-11 stsp }
6928 0ebf8283 2019-07-24 stsp done:
6929 0ebf8283 2019-07-24 stsp if (commit_ref)
6930 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6931 0ebf8283 2019-07-24 stsp return err;
6932 0ebf8283 2019-07-24 stsp }
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp static const struct got_error *
6935 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6936 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6937 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6938 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6939 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6940 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6941 0ebf8283 2019-07-24 stsp {
6942 0ebf8283 2019-07-24 stsp const struct got_error *err;
6943 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6944 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6945 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6946 818c7501 2019-07-11 stsp
6947 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6948 0ebf8283 2019-07-24 stsp
6949 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6950 0ebf8283 2019-07-24 stsp if (err)
6951 0ebf8283 2019-07-24 stsp return err;
6952 0ebf8283 2019-07-24 stsp
6953 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6954 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6955 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6956 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6957 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6958 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6959 818c7501 2019-07-11 stsp if (commit_ref)
6960 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6961 818c7501 2019-07-11 stsp return err;
6962 818c7501 2019-07-11 stsp }
6963 818c7501 2019-07-11 stsp
6964 818c7501 2019-07-11 stsp const struct got_error *
6965 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6966 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6967 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6968 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6969 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6970 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6971 818c7501 2019-07-11 stsp {
6972 0ebf8283 2019-07-24 stsp const struct got_error *err;
6973 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6976 0ebf8283 2019-07-24 stsp if (err)
6977 0ebf8283 2019-07-24 stsp return err;
6978 0ebf8283 2019-07-24 stsp
6979 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp goto done;
6982 0ebf8283 2019-07-24 stsp
6983 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6984 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6985 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6986 0ebf8283 2019-07-24 stsp done:
6987 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6988 0ebf8283 2019-07-24 stsp return err;
6989 0ebf8283 2019-07-24 stsp }
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp const struct got_error *
6992 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6993 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6994 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6995 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6996 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6997 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6998 0ebf8283 2019-07-24 stsp {
6999 0ebf8283 2019-07-24 stsp const struct got_error *err;
7000 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7001 0ebf8283 2019-07-24 stsp
7002 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7003 0ebf8283 2019-07-24 stsp if (err)
7004 0ebf8283 2019-07-24 stsp return err;
7005 0ebf8283 2019-07-24 stsp
7006 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7007 0ebf8283 2019-07-24 stsp if (err)
7008 0ebf8283 2019-07-24 stsp goto done;
7009 0ebf8283 2019-07-24 stsp
7010 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7011 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7012 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7013 0ebf8283 2019-07-24 stsp done:
7014 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7015 0ebf8283 2019-07-24 stsp return err;
7016 0ebf8283 2019-07-24 stsp }
7017 0ebf8283 2019-07-24 stsp
7018 0ebf8283 2019-07-24 stsp static const struct got_error *
7019 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7020 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7021 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7022 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7023 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7024 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7025 0ebf8283 2019-07-24 stsp {
7026 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7027 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7028 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7029 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7030 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7031 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7032 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7033 818c7501 2019-07-11 stsp
7034 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7035 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7036 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7037 a0e95631 2019-07-12 stsp
7038 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7039 818c7501 2019-07-11 stsp
7040 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7041 a0e95631 2019-07-12 stsp if (err)
7042 3e3a69f1 2019-07-25 stsp return err;
7043 a0e95631 2019-07-12 stsp
7044 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7045 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7046 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7047 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7048 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7049 01757395 2019-07-12 stsp /*
7050 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7051 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7052 6c13b005 2021-09-02 stsp *
7053 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7054 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7055 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7056 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7057 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7058 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7059 01757395 2019-07-12 stsp */
7060 01757395 2019-07-12 stsp if (merged_paths) {
7061 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7062 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7063 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7064 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7065 f2a9dc41 2019-12-13 tracey 0);
7066 01757395 2019-07-12 stsp if (err)
7067 01757395 2019-07-12 stsp goto done;
7068 01757395 2019-07-12 stsp }
7069 01757395 2019-07-12 stsp } else {
7070 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7071 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7072 01757395 2019-07-12 stsp if (err)
7073 01757395 2019-07-12 stsp goto done;
7074 01757395 2019-07-12 stsp }
7075 a0e95631 2019-07-12 stsp
7076 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7077 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7078 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7079 a0e95631 2019-07-12 stsp if (err)
7080 a0e95631 2019-07-12 stsp goto done;
7081 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7082 a0e95631 2019-07-12 stsp goto done;
7083 ff0d2220 2019-07-11 stsp }
7084 818c7501 2019-07-11 stsp
7085 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7086 edd02c5e 2019-07-11 stsp if (err)
7087 edd02c5e 2019-07-11 stsp goto done;
7088 edd02c5e 2019-07-11 stsp
7089 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7090 818c7501 2019-07-11 stsp if (err)
7091 818c7501 2019-07-11 stsp goto done;
7092 818c7501 2019-07-11 stsp
7093 5943eee2 2019-08-13 stsp if (new_logmsg) {
7094 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7095 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7096 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7097 5943eee2 2019-08-13 stsp goto done;
7098 5943eee2 2019-08-13 stsp }
7099 5943eee2 2019-08-13 stsp } else {
7100 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7101 5943eee2 2019-08-13 stsp if (err)
7102 5943eee2 2019-08-13 stsp goto done;
7103 5943eee2 2019-08-13 stsp }
7104 0ebf8283 2019-07-24 stsp
7105 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7106 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7107 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7108 8db00f97 2022-07-22 thomas committer ? committer :
7109 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7110 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7111 a0e95631 2019-07-12 stsp if (err)
7112 a0e95631 2019-07-12 stsp goto done;
7113 a0e95631 2019-07-12 stsp
7114 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7115 a0e95631 2019-07-12 stsp if (err)
7116 a0e95631 2019-07-12 stsp goto done;
7117 a0e95631 2019-07-12 stsp
7118 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7119 a0e95631 2019-07-12 stsp if (err)
7120 a0e95631 2019-07-12 stsp goto done;
7121 a0e95631 2019-07-12 stsp
7122 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7123 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7124 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7125 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7126 a0e95631 2019-07-12 stsp err = sync_err;
7127 818c7501 2019-07-11 stsp done:
7128 a0e95631 2019-07-12 stsp free(fileindex_path);
7129 a0e95631 2019-07-12 stsp free(head_commit_id);
7130 a0e95631 2019-07-12 stsp if (head_ref)
7131 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7132 818c7501 2019-07-11 stsp if (err) {
7133 818c7501 2019-07-11 stsp free(*new_commit_id);
7134 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7135 818c7501 2019-07-11 stsp }
7136 818c7501 2019-07-11 stsp return err;
7137 818c7501 2019-07-11 stsp }
7138 818c7501 2019-07-11 stsp
7139 818c7501 2019-07-11 stsp const struct got_error *
7140 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7141 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7142 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7143 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7144 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7145 be94c032 2023-02-20 thomas struct got_repository *repo)
7146 0ebf8283 2019-07-24 stsp {
7147 0ebf8283 2019-07-24 stsp const struct got_error *err;
7148 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7149 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7150 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7151 0ebf8283 2019-07-24 stsp
7152 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7153 0ebf8283 2019-07-24 stsp if (err)
7154 0ebf8283 2019-07-24 stsp return err;
7155 0ebf8283 2019-07-24 stsp
7156 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7157 0ebf8283 2019-07-24 stsp if (err)
7158 0ebf8283 2019-07-24 stsp goto done;
7159 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7160 0ebf8283 2019-07-24 stsp if (err)
7161 0ebf8283 2019-07-24 stsp goto done;
7162 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7163 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7164 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp }
7166 0ebf8283 2019-07-24 stsp
7167 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7168 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7169 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7170 0ebf8283 2019-07-24 stsp done:
7171 0ebf8283 2019-07-24 stsp if (commit_ref)
7172 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7173 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7174 0ebf8283 2019-07-24 stsp free(commit_id);
7175 0ebf8283 2019-07-24 stsp return err;
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp
7178 0ebf8283 2019-07-24 stsp const struct got_error *
7179 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7180 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7181 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7182 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7183 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7184 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7185 0ebf8283 2019-07-24 stsp {
7186 0ebf8283 2019-07-24 stsp const struct got_error *err;
7187 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7188 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7189 0ebf8283 2019-07-24 stsp
7190 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7191 0ebf8283 2019-07-24 stsp if (err)
7192 0ebf8283 2019-07-24 stsp return err;
7193 0ebf8283 2019-07-24 stsp
7194 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7195 0ebf8283 2019-07-24 stsp if (err)
7196 0ebf8283 2019-07-24 stsp goto done;
7197 0ebf8283 2019-07-24 stsp
7198 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7199 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7200 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7201 0ebf8283 2019-07-24 stsp done:
7202 0ebf8283 2019-07-24 stsp if (commit_ref)
7203 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7204 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7205 0ebf8283 2019-07-24 stsp return err;
7206 0ebf8283 2019-07-24 stsp }
7207 0ebf8283 2019-07-24 stsp
7208 0ebf8283 2019-07-24 stsp const struct got_error *
7209 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7210 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7211 818c7501 2019-07-11 stsp {
7212 3e3a69f1 2019-07-25 stsp if (fileindex)
7213 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7214 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7215 69844fba 2019-07-11 stsp }
7216 69844fba 2019-07-11 stsp
7217 69844fba 2019-07-11 stsp static const struct got_error *
7218 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7219 69844fba 2019-07-11 stsp {
7220 69844fba 2019-07-11 stsp const struct got_error *err;
7221 69844fba 2019-07-11 stsp struct got_reference *ref;
7222 69844fba 2019-07-11 stsp
7223 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7224 69844fba 2019-07-11 stsp if (err) {
7225 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7226 69844fba 2019-07-11 stsp return NULL;
7227 69844fba 2019-07-11 stsp return err;
7228 69844fba 2019-07-11 stsp }
7229 69844fba 2019-07-11 stsp
7230 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7231 69844fba 2019-07-11 stsp got_ref_close(ref);
7232 69844fba 2019-07-11 stsp return err;
7233 818c7501 2019-07-11 stsp }
7234 818c7501 2019-07-11 stsp
7235 69844fba 2019-07-11 stsp static const struct got_error *
7236 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7237 69844fba 2019-07-11 stsp {
7238 69844fba 2019-07-11 stsp const struct got_error *err;
7239 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7240 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7241 69844fba 2019-07-11 stsp
7242 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7243 69844fba 2019-07-11 stsp if (err)
7244 69844fba 2019-07-11 stsp goto done;
7245 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7246 69844fba 2019-07-11 stsp if (err)
7247 69844fba 2019-07-11 stsp goto done;
7248 69844fba 2019-07-11 stsp
7249 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7250 69844fba 2019-07-11 stsp if (err)
7251 69844fba 2019-07-11 stsp goto done;
7252 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7253 69844fba 2019-07-11 stsp if (err)
7254 69844fba 2019-07-11 stsp goto done;
7255 69844fba 2019-07-11 stsp
7256 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7257 69844fba 2019-07-11 stsp if (err)
7258 69844fba 2019-07-11 stsp goto done;
7259 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7260 69844fba 2019-07-11 stsp if (err)
7261 69844fba 2019-07-11 stsp goto done;
7262 69844fba 2019-07-11 stsp
7263 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7264 69844fba 2019-07-11 stsp if (err)
7265 69844fba 2019-07-11 stsp goto done;
7266 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7267 69844fba 2019-07-11 stsp if (err)
7268 69844fba 2019-07-11 stsp goto done;
7269 69844fba 2019-07-11 stsp
7270 69844fba 2019-07-11 stsp done:
7271 69844fba 2019-07-11 stsp free(tmp_branch_name);
7272 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7273 69844fba 2019-07-11 stsp free(branch_ref_name);
7274 69844fba 2019-07-11 stsp free(commit_ref_name);
7275 e600f124 2021-03-21 stsp return err;
7276 e600f124 2021-03-21 stsp }
7277 e600f124 2021-03-21 stsp
7278 ef20f542 2022-06-26 thomas static const struct got_error *
7279 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7280 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7281 e600f124 2021-03-21 stsp {
7282 e600f124 2021-03-21 stsp const struct got_error *err;
7283 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7284 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7285 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7286 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7287 e600f124 2021-03-21 stsp char *refname = NULL;
7288 e600f124 2021-03-21 stsp
7289 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7290 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7291 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7292 e600f124 2021-03-21 stsp branch_name += 11;
7293 e600f124 2021-03-21 stsp
7294 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7295 e600f124 2021-03-21 stsp if (err)
7296 e600f124 2021-03-21 stsp return err;
7297 e600f124 2021-03-21 stsp
7298 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7299 e600f124 2021-03-21 stsp new_id_str) == -1) {
7300 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7301 e600f124 2021-03-21 stsp goto done;
7302 e600f124 2021-03-21 stsp }
7303 e600f124 2021-03-21 stsp
7304 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7305 e600f124 2021-03-21 stsp if (err)
7306 e600f124 2021-03-21 stsp goto done;
7307 e600f124 2021-03-21 stsp
7308 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7309 e600f124 2021-03-21 stsp if (err)
7310 e600f124 2021-03-21 stsp goto done;
7311 e600f124 2021-03-21 stsp
7312 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7313 e600f124 2021-03-21 stsp done:
7314 e600f124 2021-03-21 stsp free(new_id_str);
7315 e600f124 2021-03-21 stsp free(refname);
7316 e600f124 2021-03-21 stsp free(old_commit_id);
7317 e600f124 2021-03-21 stsp if (ref)
7318 e600f124 2021-03-21 stsp got_ref_close(ref);
7319 69844fba 2019-07-11 stsp return err;
7320 69844fba 2019-07-11 stsp }
7321 69844fba 2019-07-11 stsp
7322 818c7501 2019-07-11 stsp const struct got_error *
7323 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7324 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7325 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7326 f08f28be 2023-02-03 thomas int create_backup)
7327 818c7501 2019-07-11 stsp {
7328 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7329 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7330 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7331 818c7501 2019-07-11 stsp
7332 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7333 818c7501 2019-07-11 stsp if (err)
7334 818c7501 2019-07-11 stsp return err;
7335 e600f124 2021-03-21 stsp
7336 e600f124 2021-03-21 stsp if (create_backup) {
7337 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7338 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7339 e600f124 2021-03-21 stsp if (err)
7340 e600f124 2021-03-21 stsp goto done;
7341 e600f124 2021-03-21 stsp }
7342 818c7501 2019-07-11 stsp
7343 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7344 818c7501 2019-07-11 stsp if (err)
7345 818c7501 2019-07-11 stsp goto done;
7346 818c7501 2019-07-11 stsp
7347 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7348 818c7501 2019-07-11 stsp if (err)
7349 818c7501 2019-07-11 stsp goto done;
7350 818c7501 2019-07-11 stsp
7351 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7352 818c7501 2019-07-11 stsp if (err)
7353 818c7501 2019-07-11 stsp goto done;
7354 818c7501 2019-07-11 stsp
7355 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7356 a615e0e7 2020-12-16 stsp if (err)
7357 a615e0e7 2020-12-16 stsp goto done;
7358 a615e0e7 2020-12-16 stsp
7359 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7360 a615e0e7 2020-12-16 stsp if (err)
7361 a615e0e7 2020-12-16 stsp goto done;
7362 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7363 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7364 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7365 a615e0e7 2020-12-16 stsp err = sync_err;
7366 818c7501 2019-07-11 stsp done:
7367 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7368 a615e0e7 2020-12-16 stsp free(fileindex_path);
7369 818c7501 2019-07-11 stsp free(new_head_commit_id);
7370 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7371 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7372 818c7501 2019-07-11 stsp err = unlockerr;
7373 818c7501 2019-07-11 stsp return err;
7374 818c7501 2019-07-11 stsp }
7375 8641a332 2023-04-14 thomas
7376 8641a332 2023-04-14 thomas static const struct got_error *
7377 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7378 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7379 8641a332 2023-04-14 thomas struct got_repository *repo)
7380 8641a332 2023-04-14 thomas {
7381 8641a332 2023-04-14 thomas const struct got_error *err;
7382 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7383 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7384 8641a332 2023-04-14 thomas
7385 8641a332 2023-04-14 thomas if (id1) {
7386 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7387 8641a332 2023-04-14 thomas if (err)
7388 8641a332 2023-04-14 thomas goto done;
7389 8641a332 2023-04-14 thomas
7390 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7391 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7392 8641a332 2023-04-14 thomas if (err)
7393 8641a332 2023-04-14 thomas goto done;
7394 8641a332 2023-04-14 thomas }
7395 818c7501 2019-07-11 stsp
7396 8641a332 2023-04-14 thomas if (id2) {
7397 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7398 8641a332 2023-04-14 thomas if (err)
7399 8641a332 2023-04-14 thomas goto done;
7400 8641a332 2023-04-14 thomas
7401 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7402 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7403 8641a332 2023-04-14 thomas if (err)
7404 8641a332 2023-04-14 thomas goto done;
7405 8641a332 2023-04-14 thomas }
7406 8641a332 2023-04-14 thomas
7407 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7408 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7409 8641a332 2023-04-14 thomas if (err)
7410 8641a332 2023-04-14 thomas goto done;
7411 8641a332 2023-04-14 thomas done:
7412 8641a332 2023-04-14 thomas if (commit1)
7413 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7414 8641a332 2023-04-14 thomas if (commit2)
7415 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7416 8641a332 2023-04-14 thomas if (tree1)
7417 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7418 8641a332 2023-04-14 thomas if (tree2)
7419 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7420 8641a332 2023-04-14 thomas return err;
7421 8641a332 2023-04-14 thomas }
7422 8641a332 2023-04-14 thomas
7423 8641a332 2023-04-14 thomas static const struct got_error *
7424 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7425 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7426 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7427 8641a332 2023-04-14 thomas {
7428 8641a332 2023-04-14 thomas const struct got_error *err;
7429 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7430 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7431 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7432 8641a332 2023-04-14 thomas
7433 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7434 8641a332 2023-04-14 thomas
7435 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7436 8641a332 2023-04-14 thomas if (err)
7437 8641a332 2023-04-14 thomas goto done;
7438 8641a332 2023-04-14 thomas
7439 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7440 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7441 8641a332 2023-04-14 thomas
7442 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7443 8641a332 2023-04-14 thomas continue;
7444 8641a332 2023-04-14 thomas
7445 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7446 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7447 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7448 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7449 8641a332 2023-04-14 thomas goto done;
7450 8641a332 2023-04-14 thomas }
7451 8641a332 2023-04-14 thomas } else {
7452 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7453 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7454 8641a332 2023-04-14 thomas goto done;
7455 8641a332 2023-04-14 thomas }
7456 8641a332 2023-04-14 thomas
7457 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7458 8641a332 2023-04-14 thomas path_prefix, abspath);
7459 8641a332 2023-04-14 thomas if (err)
7460 8641a332 2023-04-14 thomas goto done;
7461 8641a332 2023-04-14 thomas free(abspath);
7462 8641a332 2023-04-14 thomas abspath = NULL;
7463 8641a332 2023-04-14 thomas }
7464 8641a332 2023-04-14 thomas
7465 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7466 8641a332 2023-04-14 thomas if (err)
7467 8641a332 2023-04-14 thomas goto done;
7468 8641a332 2023-04-14 thomas wt_path = NULL;
7469 8641a332 2023-04-14 thomas }
7470 8641a332 2023-04-14 thomas
7471 8641a332 2023-04-14 thomas done:
7472 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7473 8641a332 2023-04-14 thomas free(abspath);
7474 8641a332 2023-04-14 thomas free(wt_path);
7475 8641a332 2023-04-14 thomas return err;
7476 8641a332 2023-04-14 thomas }
7477 8641a332 2023-04-14 thomas
7478 8641a332 2023-04-14 thomas static const struct got_error *
7479 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7480 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7481 8641a332 2023-04-14 thomas struct got_repository *repo)
7482 8641a332 2023-04-14 thomas {
7483 8641a332 2023-04-14 thomas const struct got_error *err;
7484 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7485 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7486 8641a332 2023-04-14 thomas
7487 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7488 8641a332 2023-04-14 thomas if (err)
7489 8641a332 2023-04-14 thomas goto done;
7490 8641a332 2023-04-14 thomas
7491 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7492 8641a332 2023-04-14 thomas
7493 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7494 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7495 8641a332 2023-04-14 thomas if (err)
7496 8641a332 2023-04-14 thomas goto done;
7497 8641a332 2023-04-14 thomas done:
7498 8641a332 2023-04-14 thomas if (commit)
7499 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7500 8641a332 2023-04-14 thomas return err;
7501 8641a332 2023-04-14 thomas }
7502 8641a332 2023-04-14 thomas
7503 818c7501 2019-07-11 stsp const struct got_error *
7504 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7505 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7506 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7507 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7508 818c7501 2019-07-11 stsp {
7509 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7510 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7511 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7512 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7513 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7514 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7515 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7516 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7517 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7518 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7519 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7520 818c7501 2019-07-11 stsp
7521 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7522 8641a332 2023-04-14 thomas
7523 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7524 818c7501 2019-07-11 stsp if (err)
7525 818c7501 2019-07-11 stsp return err;
7526 8641a332 2023-04-14 thomas
7527 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7528 8641a332 2023-04-14 thomas if (err)
7529 8641a332 2023-04-14 thomas goto done;
7530 8641a332 2023-04-14 thomas
7531 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7532 8641a332 2023-04-14 thomas if (err)
7533 8641a332 2023-04-14 thomas goto done;
7534 8641a332 2023-04-14 thomas
7535 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7536 8641a332 2023-04-14 thomas if (err)
7537 8641a332 2023-04-14 thomas goto done;
7538 945f9229 2022-04-16 thomas
7539 8641a332 2023-04-14 thomas /*
7540 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7541 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7542 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7543 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7544 8641a332 2023-04-14 thomas */
7545 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7546 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7547 8641a332 2023-04-14 thomas if (err)
7548 8641a332 2023-04-14 thomas goto done;
7549 8641a332 2023-04-14 thomas
7550 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7551 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7552 818c7501 2019-07-11 stsp if (err)
7553 818c7501 2019-07-11 stsp goto done;
7554 818c7501 2019-07-11 stsp
7555 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7556 818c7501 2019-07-11 stsp if (err)
7557 818c7501 2019-07-11 stsp goto done;
7558 818c7501 2019-07-11 stsp
7559 818c7501 2019-07-11 stsp /*
7560 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7561 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7562 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7563 818c7501 2019-07-11 stsp */
7564 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7565 818c7501 2019-07-11 stsp if (err)
7566 818c7501 2019-07-11 stsp goto done;
7567 818c7501 2019-07-11 stsp
7568 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7569 25a102ed 2023-04-14 thomas if (err)
7570 25a102ed 2023-04-14 thomas goto done;
7571 25a102ed 2023-04-14 thomas
7572 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7573 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7574 818c7501 2019-07-11 stsp if (err)
7575 818c7501 2019-07-11 stsp goto done;
7576 818c7501 2019-07-11 stsp
7577 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7578 945f9229 2022-04-16 thomas worktree->path_prefix);
7579 ca355955 2019-07-12 stsp if (err)
7580 ca355955 2019-07-12 stsp goto done;
7581 ca355955 2019-07-12 stsp
7582 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7583 ca355955 2019-07-12 stsp if (err)
7584 ca355955 2019-07-12 stsp goto done;
7585 ca355955 2019-07-12 stsp
7586 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7587 818c7501 2019-07-11 stsp if (err)
7588 818c7501 2019-07-11 stsp goto done;
7589 818c7501 2019-07-11 stsp
7590 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7591 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7592 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7593 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7594 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7595 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7596 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7597 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7598 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7599 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7600 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7601 55bd499d 2019-07-12 stsp if (err)
7602 1f1abb7e 2019-08-08 stsp goto sync;
7603 55bd499d 2019-07-12 stsp
7604 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7605 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7606 ca355955 2019-07-12 stsp sync:
7607 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7608 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7609 ca355955 2019-07-12 stsp err = sync_err;
7610 818c7501 2019-07-11 stsp done:
7611 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7612 818c7501 2019-07-11 stsp got_ref_close(resolved);
7613 a3a2faf2 2019-07-12 stsp free(tree_id);
7614 818c7501 2019-07-11 stsp free(commit_id);
7615 8641a332 2023-04-14 thomas free(merged_commit_id);
7616 945f9229 2022-04-16 thomas if (commit)
7617 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7618 0ebf8283 2019-07-24 stsp if (fileindex)
7619 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7620 0ebf8283 2019-07-24 stsp free(fileindex_path);
7621 8641a332 2023-04-14 thomas free(commit_ref_name);
7622 8641a332 2023-04-14 thomas if (commit_ref)
7623 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7624 0ebf8283 2019-07-24 stsp
7625 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7626 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7627 0ebf8283 2019-07-24 stsp err = unlockerr;
7628 0ebf8283 2019-07-24 stsp return err;
7629 0ebf8283 2019-07-24 stsp }
7630 0ebf8283 2019-07-24 stsp
7631 0ebf8283 2019-07-24 stsp const struct got_error *
7632 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7633 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7634 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7635 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7636 0ebf8283 2019-07-24 stsp {
7637 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7638 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7639 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7640 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7641 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7642 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7643 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7644 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7645 0ebf8283 2019-07-24 stsp
7646 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7647 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7648 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7649 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7650 0ebf8283 2019-07-24 stsp
7651 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7652 0ebf8283 2019-07-24 stsp if (err)
7653 0ebf8283 2019-07-24 stsp return err;
7654 0ebf8283 2019-07-24 stsp
7655 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7656 0ebf8283 2019-07-24 stsp if (err)
7657 0ebf8283 2019-07-24 stsp goto done;
7658 0ebf8283 2019-07-24 stsp
7659 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7660 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7661 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7662 0ebf8283 2019-07-24 stsp &ok_arg);
7663 0ebf8283 2019-07-24 stsp if (err)
7664 0ebf8283 2019-07-24 stsp goto done;
7665 0ebf8283 2019-07-24 stsp
7666 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7667 0ebf8283 2019-07-24 stsp if (err)
7668 0ebf8283 2019-07-24 stsp goto done;
7669 0ebf8283 2019-07-24 stsp
7670 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7671 0ebf8283 2019-07-24 stsp if (err)
7672 0ebf8283 2019-07-24 stsp goto done;
7673 0ebf8283 2019-07-24 stsp
7674 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7675 0ebf8283 2019-07-24 stsp worktree);
7676 0ebf8283 2019-07-24 stsp if (err)
7677 0ebf8283 2019-07-24 stsp goto done;
7678 0ebf8283 2019-07-24 stsp
7679 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7680 0ebf8283 2019-07-24 stsp 0);
7681 0ebf8283 2019-07-24 stsp if (err)
7682 0ebf8283 2019-07-24 stsp goto done;
7683 0ebf8283 2019-07-24 stsp
7684 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7685 0ebf8283 2019-07-24 stsp if (err)
7686 0ebf8283 2019-07-24 stsp goto done;
7687 0ebf8283 2019-07-24 stsp
7688 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7689 0ebf8283 2019-07-24 stsp if (err)
7690 0ebf8283 2019-07-24 stsp goto done;
7691 0ebf8283 2019-07-24 stsp
7692 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7693 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7694 0ebf8283 2019-07-24 stsp if (err)
7695 0ebf8283 2019-07-24 stsp goto done;
7696 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7697 0ebf8283 2019-07-24 stsp if (err)
7698 0ebf8283 2019-07-24 stsp goto done;
7699 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7700 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7701 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7702 0ebf8283 2019-07-24 stsp goto done;
7703 0ebf8283 2019-07-24 stsp }
7704 0ebf8283 2019-07-24 stsp
7705 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7706 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7707 0ebf8283 2019-07-24 stsp if (err)
7708 0ebf8283 2019-07-24 stsp goto done;
7709 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7710 0ebf8283 2019-07-24 stsp if (err)
7711 0ebf8283 2019-07-24 stsp goto done;
7712 0ebf8283 2019-07-24 stsp
7713 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7714 0ebf8283 2019-07-24 stsp if (err)
7715 0ebf8283 2019-07-24 stsp goto done;
7716 0ebf8283 2019-07-24 stsp done:
7717 0ebf8283 2019-07-24 stsp free(fileindex_path);
7718 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7719 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7720 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7721 0ebf8283 2019-07-24 stsp if (wt_branch)
7722 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7723 0ebf8283 2019-07-24 stsp if (err) {
7724 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7725 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7726 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7727 0ebf8283 2019-07-24 stsp }
7728 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7729 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7730 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7731 0ebf8283 2019-07-24 stsp }
7732 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7733 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7734 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7735 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7736 3e3a69f1 2019-07-25 stsp }
7737 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7738 0ebf8283 2019-07-24 stsp }
7739 0ebf8283 2019-07-24 stsp return err;
7740 0ebf8283 2019-07-24 stsp }
7741 0ebf8283 2019-07-24 stsp
7742 0ebf8283 2019-07-24 stsp const struct got_error *
7743 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7744 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7745 0ebf8283 2019-07-24 stsp {
7746 3e3a69f1 2019-07-25 stsp if (fileindex)
7747 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7748 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7749 0ebf8283 2019-07-24 stsp }
7750 0ebf8283 2019-07-24 stsp
7751 0ebf8283 2019-07-24 stsp const struct got_error *
7752 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7753 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7754 0ebf8283 2019-07-24 stsp {
7755 0ebf8283 2019-07-24 stsp const struct got_error *err;
7756 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7757 0ebf8283 2019-07-24 stsp
7758 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7759 0ebf8283 2019-07-24 stsp if (err)
7760 0ebf8283 2019-07-24 stsp return err;
7761 0ebf8283 2019-07-24 stsp
7762 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7763 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7764 0ebf8283 2019-07-24 stsp return NULL;
7765 0ebf8283 2019-07-24 stsp }
7766 0ebf8283 2019-07-24 stsp
7767 0ebf8283 2019-07-24 stsp const struct got_error *
7768 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7769 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7770 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7771 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7772 0ebf8283 2019-07-24 stsp {
7773 0ebf8283 2019-07-24 stsp const struct got_error *err;
7774 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7775 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7776 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7777 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7778 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7779 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7780 0ebf8283 2019-07-24 stsp
7781 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7782 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7783 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7784 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7785 0ebf8283 2019-07-24 stsp
7786 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7787 3e3a69f1 2019-07-25 stsp if (err)
7788 3e3a69f1 2019-07-25 stsp return err;
7789 3e3a69f1 2019-07-25 stsp
7790 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7791 3e3a69f1 2019-07-25 stsp if (err)
7792 f032f1f7 2019-08-04 stsp goto done;
7793 f032f1f7 2019-08-04 stsp
7794 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7795 f032f1f7 2019-08-04 stsp &have_staged_files);
7796 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7797 f032f1f7 2019-08-04 stsp goto done;
7798 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7799 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7800 3e3a69f1 2019-07-25 stsp goto done;
7801 f032f1f7 2019-08-04 stsp }
7802 3e3a69f1 2019-07-25 stsp
7803 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7804 0ebf8283 2019-07-24 stsp if (err)
7805 f032f1f7 2019-08-04 stsp goto done;
7806 0ebf8283 2019-07-24 stsp
7807 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7808 0ebf8283 2019-07-24 stsp if (err)
7809 0ebf8283 2019-07-24 stsp goto done;
7810 0ebf8283 2019-07-24 stsp
7811 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7812 0ebf8283 2019-07-24 stsp if (err)
7813 0ebf8283 2019-07-24 stsp goto done;
7814 0ebf8283 2019-07-24 stsp
7815 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7816 0ebf8283 2019-07-24 stsp worktree);
7817 0ebf8283 2019-07-24 stsp if (err)
7818 0ebf8283 2019-07-24 stsp goto done;
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7821 0ebf8283 2019-07-24 stsp if (err)
7822 0ebf8283 2019-07-24 stsp goto done;
7823 0ebf8283 2019-07-24 stsp
7824 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7825 0ebf8283 2019-07-24 stsp if (err)
7826 0ebf8283 2019-07-24 stsp goto done;
7827 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7828 0ebf8283 2019-07-24 stsp if (err)
7829 0ebf8283 2019-07-24 stsp goto done;
7830 0ebf8283 2019-07-24 stsp
7831 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7832 0ebf8283 2019-07-24 stsp if (err)
7833 0ebf8283 2019-07-24 stsp goto done;
7834 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7835 0ebf8283 2019-07-24 stsp if (err)
7836 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp
7838 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7839 0ebf8283 2019-07-24 stsp if (err)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 0ebf8283 2019-07-24 stsp done:
7842 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7843 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7844 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7845 0ebf8283 2019-07-24 stsp if (commit_ref)
7846 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7847 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7848 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7849 0ebf8283 2019-07-24 stsp if (err) {
7850 0ebf8283 2019-07-24 stsp free(*commit_id);
7851 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7852 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7853 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7854 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7855 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7856 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7857 0ebf8283 2019-07-24 stsp }
7858 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7859 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7860 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7861 3e3a69f1 2019-07-25 stsp }
7862 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7863 0ebf8283 2019-07-24 stsp }
7864 0ebf8283 2019-07-24 stsp return err;
7865 0ebf8283 2019-07-24 stsp }
7866 0ebf8283 2019-07-24 stsp
7867 0ebf8283 2019-07-24 stsp static const struct got_error *
7868 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7869 0ebf8283 2019-07-24 stsp {
7870 0ebf8283 2019-07-24 stsp const struct got_error *err;
7871 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7872 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7873 0ebf8283 2019-07-24 stsp
7874 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7875 0ebf8283 2019-07-24 stsp if (err)
7876 0ebf8283 2019-07-24 stsp goto done;
7877 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7878 0ebf8283 2019-07-24 stsp if (err)
7879 0ebf8283 2019-07-24 stsp goto done;
7880 0ebf8283 2019-07-24 stsp
7881 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7882 0ebf8283 2019-07-24 stsp worktree);
7883 0ebf8283 2019-07-24 stsp if (err)
7884 0ebf8283 2019-07-24 stsp goto done;
7885 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7886 0ebf8283 2019-07-24 stsp if (err)
7887 0ebf8283 2019-07-24 stsp goto done;
7888 0ebf8283 2019-07-24 stsp
7889 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7890 0ebf8283 2019-07-24 stsp if (err)
7891 0ebf8283 2019-07-24 stsp goto done;
7892 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7893 0ebf8283 2019-07-24 stsp if (err)
7894 0ebf8283 2019-07-24 stsp goto done;
7895 0ebf8283 2019-07-24 stsp
7896 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7897 0ebf8283 2019-07-24 stsp if (err)
7898 0ebf8283 2019-07-24 stsp goto done;
7899 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7900 0ebf8283 2019-07-24 stsp if (err)
7901 0ebf8283 2019-07-24 stsp goto done;
7902 0ebf8283 2019-07-24 stsp done:
7903 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7904 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7905 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7906 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7907 0ebf8283 2019-07-24 stsp return err;
7908 0ebf8283 2019-07-24 stsp }
7909 0ebf8283 2019-07-24 stsp
7910 0ebf8283 2019-07-24 stsp const struct got_error *
7911 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7912 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7913 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7914 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7915 0ebf8283 2019-07-24 stsp {
7916 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7917 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7918 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7919 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7920 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7921 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7922 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7923 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7924 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7925 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7926 0ebf8283 2019-07-24 stsp
7927 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7928 8641a332 2023-04-14 thomas
7929 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7930 0ebf8283 2019-07-24 stsp if (err)
7931 0ebf8283 2019-07-24 stsp return err;
7932 945f9229 2022-04-16 thomas
7933 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7934 8641a332 2023-04-14 thomas if (err)
7935 8641a332 2023-04-14 thomas goto done;
7936 8641a332 2023-04-14 thomas
7937 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7938 8641a332 2023-04-14 thomas if (err) {
7939 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7940 8641a332 2023-04-14 thomas goto done;
7941 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7942 8641a332 2023-04-14 thomas commit_ref = NULL;
7943 8641a332 2023-04-14 thomas }
7944 8641a332 2023-04-14 thomas
7945 8641a332 2023-04-14 thomas if (commit_ref) {
7946 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7947 8641a332 2023-04-14 thomas if (err)
7948 8641a332 2023-04-14 thomas goto done;
7949 8641a332 2023-04-14 thomas
7950 8641a332 2023-04-14 thomas /*
7951 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7952 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7953 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7954 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7955 8641a332 2023-04-14 thomas */
7956 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7957 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7958 8641a332 2023-04-14 thomas if (err)
7959 8641a332 2023-04-14 thomas goto done;
7960 8641a332 2023-04-14 thomas }
7961 8641a332 2023-04-14 thomas
7962 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7963 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7964 0ebf8283 2019-07-24 stsp if (err)
7965 0ebf8283 2019-07-24 stsp goto done;
7966 0ebf8283 2019-07-24 stsp
7967 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7968 0ebf8283 2019-07-24 stsp if (err)
7969 0ebf8283 2019-07-24 stsp goto done;
7970 0ebf8283 2019-07-24 stsp
7971 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7972 0ebf8283 2019-07-24 stsp if (err)
7973 0ebf8283 2019-07-24 stsp goto done;
7974 0ebf8283 2019-07-24 stsp
7975 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7976 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7977 25a102ed 2023-04-14 thomas if (err)
7978 25a102ed 2023-04-14 thomas goto done;
7979 25a102ed 2023-04-14 thomas
7980 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7981 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7982 0ebf8283 2019-07-24 stsp if (err)
7983 0ebf8283 2019-07-24 stsp goto done;
7984 0ebf8283 2019-07-24 stsp
7985 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7986 0ebf8283 2019-07-24 stsp if (err)
7987 0ebf8283 2019-07-24 stsp goto done;
7988 0ebf8283 2019-07-24 stsp
7989 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7990 0ebf8283 2019-07-24 stsp if (err)
7991 0ebf8283 2019-07-24 stsp goto done;
7992 0ebf8283 2019-07-24 stsp
7993 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7994 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7995 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7996 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7997 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7998 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7999 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8000 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8001 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8002 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8003 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8004 0ebf8283 2019-07-24 stsp if (err)
8005 1f1abb7e 2019-08-08 stsp goto sync;
8006 0ebf8283 2019-07-24 stsp
8007 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8008 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8009 0ebf8283 2019-07-24 stsp sync:
8010 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8011 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8012 0ebf8283 2019-07-24 stsp err = sync_err;
8013 0ebf8283 2019-07-24 stsp done:
8014 8641a332 2023-04-14 thomas if (resolved)
8015 8641a332 2023-04-14 thomas got_ref_close(resolved);
8016 8641a332 2023-04-14 thomas if (commit_ref)
8017 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8018 8641a332 2023-04-14 thomas free(merged_commit_id);
8019 0ebf8283 2019-07-24 stsp free(tree_id);
8020 818c7501 2019-07-11 stsp free(fileindex_path);
8021 8641a332 2023-04-14 thomas free(commit_ref_name);
8022 818c7501 2019-07-11 stsp
8023 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8024 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8025 818c7501 2019-07-11 stsp err = unlockerr;
8026 818c7501 2019-07-11 stsp return err;
8027 818c7501 2019-07-11 stsp }
8028 0ebf8283 2019-07-24 stsp
8029 0ebf8283 2019-07-24 stsp const struct got_error *
8030 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8031 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8032 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8033 0ebf8283 2019-07-24 stsp {
8034 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8035 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8036 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8037 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8038 0ebf8283 2019-07-24 stsp
8039 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8040 0ebf8283 2019-07-24 stsp if (err)
8041 0ebf8283 2019-07-24 stsp return err;
8042 0ebf8283 2019-07-24 stsp
8043 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8044 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8045 e600f124 2021-03-21 stsp if (err)
8046 e600f124 2021-03-21 stsp goto done;
8047 e600f124 2021-03-21 stsp
8048 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8049 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
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_change_ref(resolved, new_head_commit_id);
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_write(resolved, repo);
8058 0ebf8283 2019-07-24 stsp if (err)
8059 0ebf8283 2019-07-24 stsp goto done;
8060 0ebf8283 2019-07-24 stsp
8061 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8062 0ebf8283 2019-07-24 stsp if (err)
8063 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp
8065 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8066 a615e0e7 2020-12-16 stsp if (err)
8067 a615e0e7 2020-12-16 stsp goto done;
8068 a615e0e7 2020-12-16 stsp
8069 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8070 a615e0e7 2020-12-16 stsp if (err)
8071 a615e0e7 2020-12-16 stsp goto done;
8072 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8073 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8074 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8075 a615e0e7 2020-12-16 stsp err = sync_err;
8076 0ebf8283 2019-07-24 stsp done:
8077 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8078 a615e0e7 2020-12-16 stsp free(fileindex_path);
8079 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8080 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8081 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8082 0ebf8283 2019-07-24 stsp err = unlockerr;
8083 0ebf8283 2019-07-24 stsp return err;
8084 0ebf8283 2019-07-24 stsp }
8085 0ebf8283 2019-07-24 stsp
8086 0ebf8283 2019-07-24 stsp const struct got_error *
8087 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8088 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8089 0ebf8283 2019-07-24 stsp {
8090 0ebf8283 2019-07-24 stsp const struct got_error *err;
8091 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8092 0ebf8283 2019-07-24 stsp
8093 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8094 0ebf8283 2019-07-24 stsp if (err)
8095 0ebf8283 2019-07-24 stsp return err;
8096 0ebf8283 2019-07-24 stsp
8097 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8098 0ebf8283 2019-07-24 stsp if (err)
8099 0ebf8283 2019-07-24 stsp goto done;
8100 0ebf8283 2019-07-24 stsp
8101 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8102 0ebf8283 2019-07-24 stsp done:
8103 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8104 2822a352 2019-10-15 stsp return err;
8105 2822a352 2019-10-15 stsp }
8106 2822a352 2019-10-15 stsp
8107 2822a352 2019-10-15 stsp const struct got_error *
8108 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8109 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8110 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8111 2822a352 2019-10-15 stsp struct got_repository *repo)
8112 2822a352 2019-10-15 stsp {
8113 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8114 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8115 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8116 2822a352 2019-10-15 stsp
8117 2822a352 2019-10-15 stsp *fileindex = NULL;
8118 2822a352 2019-10-15 stsp *branch_ref = NULL;
8119 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8120 2822a352 2019-10-15 stsp
8121 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8122 2822a352 2019-10-15 stsp if (err)
8123 2822a352 2019-10-15 stsp return err;
8124 2822a352 2019-10-15 stsp
8125 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8126 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8127 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8128 2822a352 2019-10-15 stsp "update -b or different branch name required");
8129 2822a352 2019-10-15 stsp goto done;
8130 2822a352 2019-10-15 stsp }
8131 2822a352 2019-10-15 stsp
8132 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8133 2822a352 2019-10-15 stsp if (err)
8134 2822a352 2019-10-15 stsp goto done;
8135 2822a352 2019-10-15 stsp
8136 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8137 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8138 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8139 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8140 2822a352 2019-10-15 stsp &ok_arg);
8141 2822a352 2019-10-15 stsp if (err)
8142 2822a352 2019-10-15 stsp goto done;
8143 2822a352 2019-10-15 stsp
8144 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8145 2822a352 2019-10-15 stsp if (err)
8146 2822a352 2019-10-15 stsp goto done;
8147 2822a352 2019-10-15 stsp
8148 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8149 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8150 2822a352 2019-10-15 stsp done:
8151 2822a352 2019-10-15 stsp if (err) {
8152 2822a352 2019-10-15 stsp if (*branch_ref) {
8153 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8154 2822a352 2019-10-15 stsp *branch_ref = NULL;
8155 2822a352 2019-10-15 stsp }
8156 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8157 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8158 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8159 2822a352 2019-10-15 stsp }
8160 2822a352 2019-10-15 stsp if (*fileindex) {
8161 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8162 2822a352 2019-10-15 stsp *fileindex = NULL;
8163 2822a352 2019-10-15 stsp }
8164 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8165 2822a352 2019-10-15 stsp }
8166 0cb83759 2019-08-03 stsp return err;
8167 0cb83759 2019-08-03 stsp }
8168 0cb83759 2019-08-03 stsp
8169 2822a352 2019-10-15 stsp const struct got_error *
8170 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8171 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8172 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8173 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8174 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8175 2822a352 2019-10-15 stsp {
8176 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8177 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8178 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8179 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8180 2822a352 2019-10-15 stsp
8181 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8182 2822a352 2019-10-15 stsp if (err)
8183 2822a352 2019-10-15 stsp goto done;
8184 2822a352 2019-10-15 stsp
8185 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8186 2822a352 2019-10-15 stsp if (err)
8187 2822a352 2019-10-15 stsp goto done;
8188 2822a352 2019-10-15 stsp
8189 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8190 945f9229 2022-04-16 thomas if (err)
8191 945f9229 2022-04-16 thomas goto done;
8192 945f9229 2022-04-16 thomas
8193 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8194 2822a352 2019-10-15 stsp worktree->path_prefix);
8195 2822a352 2019-10-15 stsp if (err)
8196 2822a352 2019-10-15 stsp goto done;
8197 2822a352 2019-10-15 stsp
8198 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8199 2822a352 2019-10-15 stsp if (err)
8200 2822a352 2019-10-15 stsp goto done;
8201 2822a352 2019-10-15 stsp
8202 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8203 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8204 2822a352 2019-10-15 stsp if (err)
8205 2822a352 2019-10-15 stsp goto sync;
8206 2822a352 2019-10-15 stsp
8207 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8208 2822a352 2019-10-15 stsp if (err)
8209 2822a352 2019-10-15 stsp goto sync;
8210 2822a352 2019-10-15 stsp
8211 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8212 6e210706 2021-01-22 stsp if (err)
8213 6e210706 2021-01-22 stsp goto sync;
8214 6e210706 2021-01-22 stsp
8215 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8216 2822a352 2019-10-15 stsp sync:
8217 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8218 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8219 2822a352 2019-10-15 stsp err = sync_err;
8220 2822a352 2019-10-15 stsp
8221 2822a352 2019-10-15 stsp done:
8222 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8223 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8224 2822a352 2019-10-15 stsp err = unlockerr;
8225 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8226 2822a352 2019-10-15 stsp
8227 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8228 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8229 2822a352 2019-10-15 stsp err = unlockerr;
8230 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8231 2822a352 2019-10-15 stsp
8232 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8233 2822a352 2019-10-15 stsp free(fileindex_path);
8234 2822a352 2019-10-15 stsp free(tree_id);
8235 945f9229 2022-04-16 thomas if (commit)
8236 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8237 2822a352 2019-10-15 stsp
8238 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8239 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8240 2822a352 2019-10-15 stsp err = unlockerr;
8241 2822a352 2019-10-15 stsp return err;
8242 2822a352 2019-10-15 stsp }
8243 2822a352 2019-10-15 stsp
8244 2822a352 2019-10-15 stsp const struct got_error *
8245 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8246 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8247 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8248 2822a352 2019-10-15 stsp {
8249 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8250 8b692cd0 2019-10-21 stsp
8251 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8252 8b692cd0 2019-10-21 stsp
8253 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8254 8b692cd0 2019-10-21 stsp
8255 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8256 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8257 8b692cd0 2019-10-21 stsp err = unlockerr;
8258 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8259 8b692cd0 2019-10-21 stsp
8260 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8261 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8262 8b692cd0 2019-10-21 stsp err = unlockerr;
8263 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8264 10604dce 2021-09-24 thomas
8265 10604dce 2021-09-24 thomas return err;
8266 10604dce 2021-09-24 thomas }
8267 10604dce 2021-09-24 thomas
8268 10604dce 2021-09-24 thomas const struct got_error *
8269 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8270 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8271 10604dce 2021-09-24 thomas {
8272 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8273 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8274 10604dce 2021-09-24 thomas
8275 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8276 10604dce 2021-09-24 thomas if (err)
8277 10604dce 2021-09-24 thomas goto done;
8278 8b692cd0 2019-10-21 stsp
8279 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8280 10604dce 2021-09-24 thomas
8281 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8282 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8283 10604dce 2021-09-24 thomas err = sync_err;
8284 10604dce 2021-09-24 thomas done:
8285 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8286 10604dce 2021-09-24 thomas free(fileindex_path);
8287 8b692cd0 2019-10-21 stsp return err;
8288 2822a352 2019-10-15 stsp }
8289 2822a352 2019-10-15 stsp
8290 10604dce 2021-09-24 thomas static const struct got_error *
8291 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8292 10604dce 2021-09-24 thomas {
8293 10604dce 2021-09-24 thomas const struct got_error *err;
8294 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8295 10604dce 2021-09-24 thomas
8296 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8297 10604dce 2021-09-24 thomas if (err)
8298 10604dce 2021-09-24 thomas goto done;
8299 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8300 10604dce 2021-09-24 thomas if (err)
8301 10604dce 2021-09-24 thomas goto done;
8302 10604dce 2021-09-24 thomas
8303 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8304 10604dce 2021-09-24 thomas if (err)
8305 10604dce 2021-09-24 thomas goto done;
8306 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8307 10604dce 2021-09-24 thomas if (err)
8308 10604dce 2021-09-24 thomas goto done;
8309 10604dce 2021-09-24 thomas
8310 10604dce 2021-09-24 thomas done:
8311 10604dce 2021-09-24 thomas free(branch_refname);
8312 10604dce 2021-09-24 thomas free(commit_refname);
8313 10604dce 2021-09-24 thomas return err;
8314 10604dce 2021-09-24 thomas }
8315 10604dce 2021-09-24 thomas
8316 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8317 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8318 10604dce 2021-09-24 thomas const char *branch_name;
8319 10604dce 2021-09-24 thomas };
8320 10604dce 2021-09-24 thomas
8321 10604dce 2021-09-24 thomas static const struct got_error *
8322 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8323 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8324 10604dce 2021-09-24 thomas {
8325 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8326 10604dce 2021-09-24 thomas
8327 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8328 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8329 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8330 10604dce 2021-09-24 thomas
8331 10604dce 2021-09-24 thomas return NULL;
8332 10604dce 2021-09-24 thomas }
8333 10604dce 2021-09-24 thomas
8334 10604dce 2021-09-24 thomas
8335 10604dce 2021-09-24 thomas const struct got_error *
8336 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8337 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8338 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8339 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8340 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8341 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8342 10604dce 2021-09-24 thomas {
8343 10604dce 2021-09-24 thomas const struct got_error *err;
8344 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8345 10604dce 2021-09-24 thomas
8346 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8347 10604dce 2021-09-24 thomas if (err)
8348 10604dce 2021-09-24 thomas goto done;
8349 10604dce 2021-09-24 thomas
8350 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8351 10604dce 2021-09-24 thomas worktree);
8352 10604dce 2021-09-24 thomas if (err)
8353 10604dce 2021-09-24 thomas goto done;
8354 10604dce 2021-09-24 thomas
8355 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8356 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8357 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8358 10604dce 2021-09-24 thomas done:
8359 10604dce 2021-09-24 thomas free(fileindex_path);
8360 10604dce 2021-09-24 thomas return err;
8361 10604dce 2021-09-24 thomas }
8362 10604dce 2021-09-24 thomas
8363 10604dce 2021-09-24 thomas const struct got_error *
8364 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8365 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8366 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8367 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8368 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8369 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8370 ae1e948a 2021-09-28 thomas
8371 10604dce 2021-09-24 thomas {
8372 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8373 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8374 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8375 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8376 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8377 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8378 10604dce 2021-09-24 thomas int have_staged_files = 0;
8379 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8380 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8381 10604dce 2021-09-24 thomas
8382 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8383 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8384 10604dce 2021-09-24 thomas
8385 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8386 10604dce 2021-09-24 thomas
8387 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8388 10604dce 2021-09-24 thomas if (err)
8389 10604dce 2021-09-24 thomas goto done;
8390 10604dce 2021-09-24 thomas
8391 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8392 10604dce 2021-09-24 thomas if (err)
8393 10604dce 2021-09-24 thomas goto done;
8394 10604dce 2021-09-24 thomas
8395 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8396 10604dce 2021-09-24 thomas if (err)
8397 10604dce 2021-09-24 thomas goto done;
8398 10604dce 2021-09-24 thomas
8399 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8400 10604dce 2021-09-24 thomas &have_staged_files);
8401 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8402 10604dce 2021-09-24 thomas goto done;
8403 10604dce 2021-09-24 thomas if (have_staged_files) {
8404 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8405 10604dce 2021-09-24 thomas goto done;
8406 10604dce 2021-09-24 thomas }
8407 10604dce 2021-09-24 thomas
8408 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8409 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8410 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8411 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8412 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8413 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8414 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8415 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8416 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8417 10604dce 2021-09-24 thomas if (err)
8418 10604dce 2021-09-24 thomas goto done;
8419 10604dce 2021-09-24 thomas
8420 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8421 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8422 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8423 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8424 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8425 10604dce 2021-09-24 thomas if (err)
8426 10604dce 2021-09-24 thomas goto done;
8427 10604dce 2021-09-24 thomas
8428 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8429 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8430 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8431 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8432 10604dce 2021-09-24 thomas err = sync_err;
8433 10604dce 2021-09-24 thomas done:
8434 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8435 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8436 21c2d8be 2023-01-10 thomas
8437 10604dce 2021-09-24 thomas free_commitable(ct);
8438 10604dce 2021-09-24 thomas }
8439 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8440 10604dce 2021-09-24 thomas free(fileindex_path);
8441 10604dce 2021-09-24 thomas return err;
8442 10604dce 2021-09-24 thomas }
8443 10604dce 2021-09-24 thomas
8444 10604dce 2021-09-24 thomas const struct got_error *
8445 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8446 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8447 10604dce 2021-09-24 thomas {
8448 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8449 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8450 10604dce 2021-09-24 thomas
8451 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8452 10604dce 2021-09-24 thomas if (err)
8453 10604dce 2021-09-24 thomas goto done;
8454 10604dce 2021-09-24 thomas
8455 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8456 10604dce 2021-09-24 thomas if (err)
8457 10604dce 2021-09-24 thomas goto done;
8458 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8459 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8460 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8461 10604dce 2021-09-24 thomas err = sync_err;
8462 10604dce 2021-09-24 thomas done:
8463 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8464 10604dce 2021-09-24 thomas free(fileindex_path);
8465 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8466 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8467 10604dce 2021-09-24 thomas err = unlockerr;
8468 10604dce 2021-09-24 thomas return err;
8469 10604dce 2021-09-24 thomas }
8470 10604dce 2021-09-24 thomas
8471 10604dce 2021-09-24 thomas const struct got_error *
8472 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8473 10604dce 2021-09-24 thomas struct got_repository *repo)
8474 10604dce 2021-09-24 thomas {
8475 10604dce 2021-09-24 thomas const struct got_error *err;
8476 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8477 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8478 10604dce 2021-09-24 thomas
8479 10604dce 2021-09-24 thomas *in_progress = 0;
8480 10604dce 2021-09-24 thomas
8481 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8482 10604dce 2021-09-24 thomas if (err)
8483 10604dce 2021-09-24 thomas return err;
8484 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8485 dfe86d1f 2021-09-24 thomas free(branch_refname);
8486 10604dce 2021-09-24 thomas if (err) {
8487 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8488 10604dce 2021-09-24 thomas return err;
8489 10604dce 2021-09-24 thomas } else
8490 10604dce 2021-09-24 thomas *in_progress = 1;
8491 10604dce 2021-09-24 thomas
8492 10604dce 2021-09-24 thomas return NULL;
8493 10604dce 2021-09-24 thomas }
8494 10604dce 2021-09-24 thomas
8495 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8496 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8497 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8498 10604dce 2021-09-24 thomas {
8499 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8500 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8501 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8502 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8503 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8504 10604dce 2021-09-24 thomas
8505 10604dce 2021-09-24 thomas *fileindex = NULL;
8506 10604dce 2021-09-24 thomas
8507 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8508 10604dce 2021-09-24 thomas if (err)
8509 10604dce 2021-09-24 thomas return err;
8510 10604dce 2021-09-24 thomas
8511 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8512 10604dce 2021-09-24 thomas if (err)
8513 10604dce 2021-09-24 thomas goto done;
8514 10604dce 2021-09-24 thomas
8515 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8516 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8517 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8518 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8519 10604dce 2021-09-24 thomas &ok_arg);
8520 10604dce 2021-09-24 thomas if (err)
8521 10604dce 2021-09-24 thomas goto done;
8522 10604dce 2021-09-24 thomas
8523 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8524 10604dce 2021-09-24 thomas 0);
8525 10604dce 2021-09-24 thomas if (err)
8526 10604dce 2021-09-24 thomas goto done;
8527 10604dce 2021-09-24 thomas
8528 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8529 10604dce 2021-09-24 thomas if (err)
8530 10604dce 2021-09-24 thomas goto done;
8531 10604dce 2021-09-24 thomas
8532 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8533 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8534 10604dce 2021-09-24 thomas goto done;
8535 10604dce 2021-09-24 thomas }
8536 2b72f32d 2023-06-22 thomas
8537 2b72f32d 2023-06-22 thomas done:
8538 2b72f32d 2023-06-22 thomas free(fileindex_path);
8539 2b72f32d 2023-06-22 thomas if (wt_branch)
8540 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8541 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8542 2b72f32d 2023-06-22 thomas if (err) {
8543 2b72f32d 2023-06-22 thomas if (*fileindex) {
8544 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8545 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8546 2b72f32d 2023-06-22 thomas }
8547 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8548 2b72f32d 2023-06-22 thomas }
8549 2b72f32d 2023-06-22 thomas return err;
8550 2b72f32d 2023-06-22 thomas }
8551 10604dce 2021-09-24 thomas
8552 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8553 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8554 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8555 2b72f32d 2023-06-22 thomas {
8556 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8557 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8558 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8559 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8560 2b72f32d 2023-06-22 thomas
8561 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8562 2b72f32d 2023-06-22 thomas if (err)
8563 2b72f32d 2023-06-22 thomas return err;
8564 2b72f32d 2023-06-22 thomas
8565 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8566 2b72f32d 2023-06-22 thomas if (err)
8567 2b72f32d 2023-06-22 thomas return err;
8568 2b72f32d 2023-06-22 thomas
8569 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8570 10604dce 2021-09-24 thomas if (err)
8571 10604dce 2021-09-24 thomas goto done;
8572 10604dce 2021-09-24 thomas
8573 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8574 10604dce 2021-09-24 thomas if (err)
8575 10604dce 2021-09-24 thomas goto done;
8576 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8577 10604dce 2021-09-24 thomas if (err)
8578 10604dce 2021-09-24 thomas goto done;
8579 10604dce 2021-09-24 thomas
8580 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8581 10604dce 2021-09-24 thomas if (err)
8582 10604dce 2021-09-24 thomas goto done;
8583 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8584 10604dce 2021-09-24 thomas if (err)
8585 10604dce 2021-09-24 thomas goto done;
8586 10604dce 2021-09-24 thomas
8587 10604dce 2021-09-24 thomas done:
8588 10604dce 2021-09-24 thomas free(branch_refname);
8589 10604dce 2021-09-24 thomas free(commit_refname);
8590 10604dce 2021-09-24 thomas if (branch_ref)
8591 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8592 10604dce 2021-09-24 thomas if (commit_ref)
8593 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8594 2b72f32d 2023-06-22 thomas free(branch_tip);
8595 10604dce 2021-09-24 thomas return err;
8596 10604dce 2021-09-24 thomas }
8597 10604dce 2021-09-24 thomas
8598 10604dce 2021-09-24 thomas const struct got_error *
8599 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8600 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8601 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8602 10604dce 2021-09-24 thomas {
8603 10604dce 2021-09-24 thomas const struct got_error *err;
8604 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8605 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8606 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8607 10604dce 2021-09-24 thomas int have_staged_files = 0;
8608 10604dce 2021-09-24 thomas
8609 10604dce 2021-09-24 thomas *branch_name = NULL;
8610 10604dce 2021-09-24 thomas *branch_tip = NULL;
8611 10604dce 2021-09-24 thomas *fileindex = NULL;
8612 10604dce 2021-09-24 thomas
8613 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8614 10604dce 2021-09-24 thomas if (err)
8615 10604dce 2021-09-24 thomas return err;
8616 10604dce 2021-09-24 thomas
8617 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8618 10604dce 2021-09-24 thomas if (err)
8619 10604dce 2021-09-24 thomas goto done;
8620 10604dce 2021-09-24 thomas
8621 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8622 10604dce 2021-09-24 thomas &have_staged_files);
8623 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8624 10604dce 2021-09-24 thomas goto done;
8625 10604dce 2021-09-24 thomas if (have_staged_files) {
8626 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8627 10604dce 2021-09-24 thomas goto done;
8628 10604dce 2021-09-24 thomas }
8629 10604dce 2021-09-24 thomas
8630 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8631 10604dce 2021-09-24 thomas if (err)
8632 10604dce 2021-09-24 thomas goto done;
8633 10604dce 2021-09-24 thomas
8634 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8635 10604dce 2021-09-24 thomas if (err)
8636 10604dce 2021-09-24 thomas goto done;
8637 10604dce 2021-09-24 thomas
8638 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8639 10604dce 2021-09-24 thomas if (err)
8640 10604dce 2021-09-24 thomas goto done;
8641 10604dce 2021-09-24 thomas
8642 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8643 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8644 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8645 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8646 10604dce 2021-09-24 thomas goto done;
8647 10604dce 2021-09-24 thomas }
8648 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8649 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8650 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8651 10604dce 2021-09-24 thomas goto done;
8652 10604dce 2021-09-24 thomas }
8653 10604dce 2021-09-24 thomas
8654 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8655 10604dce 2021-09-24 thomas if (err)
8656 10604dce 2021-09-24 thomas goto done;
8657 10604dce 2021-09-24 thomas
8658 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8659 10604dce 2021-09-24 thomas if (err)
8660 10604dce 2021-09-24 thomas goto done;
8661 10604dce 2021-09-24 thomas done:
8662 10604dce 2021-09-24 thomas free(commit_refname);
8663 10604dce 2021-09-24 thomas free(branch_refname);
8664 10604dce 2021-09-24 thomas free(fileindex_path);
8665 10604dce 2021-09-24 thomas if (commit_ref)
8666 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8667 10604dce 2021-09-24 thomas if (branch_ref)
8668 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8669 10604dce 2021-09-24 thomas if (err) {
8670 10604dce 2021-09-24 thomas if (*branch_name) {
8671 10604dce 2021-09-24 thomas free(*branch_name);
8672 10604dce 2021-09-24 thomas *branch_name = NULL;
8673 10604dce 2021-09-24 thomas }
8674 10604dce 2021-09-24 thomas free(*branch_tip);
8675 10604dce 2021-09-24 thomas *branch_tip = NULL;
8676 10604dce 2021-09-24 thomas if (*fileindex) {
8677 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8678 10604dce 2021-09-24 thomas *fileindex = NULL;
8679 10604dce 2021-09-24 thomas }
8680 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8681 10604dce 2021-09-24 thomas }
8682 10604dce 2021-09-24 thomas return err;
8683 10604dce 2021-09-24 thomas }
8684 10604dce 2021-09-24 thomas
8685 10604dce 2021-09-24 thomas const struct got_error *
8686 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8687 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8688 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8689 10604dce 2021-09-24 thomas {
8690 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8691 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8692 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8693 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8694 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8695 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8696 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8697 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8698 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8699 8641a332 2023-04-14 thomas
8700 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8701 8641a332 2023-04-14 thomas
8702 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8703 8641a332 2023-04-14 thomas if (err)
8704 8641a332 2023-04-14 thomas goto done;
8705 8641a332 2023-04-14 thomas
8706 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8707 8641a332 2023-04-14 thomas if (err)
8708 8641a332 2023-04-14 thomas goto done;
8709 8641a332 2023-04-14 thomas
8710 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8711 8641a332 2023-04-14 thomas if (err)
8712 8641a332 2023-04-14 thomas goto done;
8713 8641a332 2023-04-14 thomas
8714 8641a332 2023-04-14 thomas /*
8715 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8716 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8717 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8718 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8719 8641a332 2023-04-14 thomas */
8720 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8721 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8722 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8723 8641a332 2023-04-14 thomas if (err)
8724 8641a332 2023-04-14 thomas goto done;
8725 10604dce 2021-09-24 thomas
8726 8641a332 2023-04-14 thomas
8727 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8728 945f9229 2022-04-16 thomas worktree->base_commit_id);
8729 945f9229 2022-04-16 thomas if (err)
8730 945f9229 2022-04-16 thomas goto done;
8731 945f9229 2022-04-16 thomas
8732 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8733 945f9229 2022-04-16 thomas worktree->path_prefix);
8734 10604dce 2021-09-24 thomas if (err)
8735 10604dce 2021-09-24 thomas goto done;
8736 10604dce 2021-09-24 thomas
8737 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8738 10604dce 2021-09-24 thomas if (err)
8739 10604dce 2021-09-24 thomas goto done;
8740 10604dce 2021-09-24 thomas
8741 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8742 10604dce 2021-09-24 thomas if (err)
8743 10604dce 2021-09-24 thomas goto done;
8744 10604dce 2021-09-24 thomas
8745 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8746 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8747 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8748 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8749 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8750 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8751 10604dce 2021-09-24 thomas rfa.repo = repo;
8752 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8753 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8754 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8755 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8756 10604dce 2021-09-24 thomas if (err)
8757 10604dce 2021-09-24 thomas goto sync;
8758 10604dce 2021-09-24 thomas
8759 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8760 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8761 10604dce 2021-09-24 thomas sync:
8762 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8763 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8764 10604dce 2021-09-24 thomas err = sync_err;
8765 10604dce 2021-09-24 thomas done:
8766 10604dce 2021-09-24 thomas free(tree_id);
8767 8641a332 2023-04-14 thomas free(merged_commit_id);
8768 945f9229 2022-04-16 thomas if (commit)
8769 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8770 10604dce 2021-09-24 thomas if (fileindex)
8771 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8772 10604dce 2021-09-24 thomas free(fileindex_path);
8773 8641a332 2023-04-14 thomas if (commit_ref)
8774 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8775 8641a332 2023-04-14 thomas free(commit_ref_name);
8776 10604dce 2021-09-24 thomas
8777 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8778 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8779 10604dce 2021-09-24 thomas err = unlockerr;
8780 10604dce 2021-09-24 thomas return err;
8781 10604dce 2021-09-24 thomas }
8782 10604dce 2021-09-24 thomas
8783 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8784 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8785 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8786 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8787 2db2652d 2019-08-07 stsp struct got_repository *repo;
8788 2db2652d 2019-08-07 stsp int have_changes;
8789 2db2652d 2019-08-07 stsp };
8790 2db2652d 2019-08-07 stsp
8791 ef20f542 2022-06-26 thomas static const struct got_error *
8792 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8793 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8794 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8795 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8796 735ef5ac 2019-08-03 stsp {
8797 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8798 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8799 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8800 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8801 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8802 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8803 8b13ce36 2019-08-08 stsp
8804 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8805 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8806 8b13ce36 2019-08-08 stsp return NULL;
8807 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8808 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8809 735ef5ac 2019-08-03 stsp
8810 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8811 735ef5ac 2019-08-03 stsp if (ie == NULL)
8812 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8813 735ef5ac 2019-08-03 stsp
8814 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8815 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8816 735ef5ac 2019-08-03 stsp relpath) == -1)
8817 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8818 735ef5ac 2019-08-03 stsp
8819 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8820 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8821 43010591 2023-02-17 thomas &base_commit_id, ie);
8822 735ef5ac 2019-08-03 stsp }
8823 735ef5ac 2019-08-03 stsp
8824 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8825 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8826 3aa5969e 2019-08-06 stsp goto done;
8827 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8828 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8829 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8830 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8831 735ef5ac 2019-08-03 stsp goto done;
8832 3aa5969e 2019-08-06 stsp }
8833 735ef5ac 2019-08-03 stsp
8834 2db2652d 2019-08-07 stsp a->have_changes = 1;
8835 2db2652d 2019-08-07 stsp
8836 735ef5ac 2019-08-03 stsp p = in_repo_path;
8837 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8838 735ef5ac 2019-08-03 stsp p++;
8839 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8840 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8841 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8842 735ef5ac 2019-08-03 stsp done:
8843 735ef5ac 2019-08-03 stsp free(in_repo_path);
8844 dc424a06 2019-08-07 stsp return err;
8845 dc424a06 2019-08-07 stsp }
8846 dc424a06 2019-08-07 stsp
8847 2db2652d 2019-08-07 stsp struct stage_path_arg {
8848 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8849 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8850 2db2652d 2019-08-07 stsp struct got_repository *repo;
8851 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8852 2db2652d 2019-08-07 stsp void *status_arg;
8853 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8854 2db2652d 2019-08-07 stsp void *patch_arg;
8855 7b5dc508 2019-10-28 stsp int staged_something;
8856 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8857 2db2652d 2019-08-07 stsp };
8858 2db2652d 2019-08-07 stsp
8859 2db2652d 2019-08-07 stsp static const struct got_error *
8860 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8861 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8862 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8863 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8864 0cb83759 2019-08-03 stsp {
8865 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8866 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8867 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8868 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8869 0cb83759 2019-08-03 stsp uint32_t stage;
8870 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8871 0aeb8099 2020-07-23 stsp struct stat sb;
8872 8b13ce36 2019-08-08 stsp
8873 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8874 8b13ce36 2019-08-08 stsp return NULL;
8875 0cb83759 2019-08-03 stsp
8876 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8877 d3e7c587 2019-08-03 stsp if (ie == NULL)
8878 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8879 0cb83759 2019-08-03 stsp
8880 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8881 2db2652d 2019-08-07 stsp relpath)== -1)
8882 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8883 0cb83759 2019-08-03 stsp
8884 0cb83759 2019-08-03 stsp switch (status) {
8885 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8886 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8887 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8888 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8889 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8890 0aeb8099 2020-07-23 stsp break;
8891 0aeb8099 2020-07-23 stsp }
8892 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8893 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8894 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8895 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8896 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8897 dc424a06 2019-08-07 stsp if (err)
8898 dc424a06 2019-08-07 stsp break;
8899 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8900 dc424a06 2019-08-07 stsp break;
8901 dc424a06 2019-08-07 stsp } else {
8902 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8903 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8904 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8905 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8906 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8907 dc424a06 2019-08-07 stsp break;
8908 dc424a06 2019-08-07 stsp }
8909 dc424a06 2019-08-07 stsp }
8910 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8911 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8912 0cb83759 2019-08-03 stsp if (err)
8913 dc424a06 2019-08-07 stsp break;
8914 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8915 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8916 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8917 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8918 0cb83759 2019-08-03 stsp else
8919 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8920 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8921 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8922 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8923 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8924 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8925 35213c7c 2020-07-23 stsp ssize_t target_len;
8926 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8927 35213c7c 2020-07-23 stsp sizeof(target_path));
8928 35213c7c 2020-07-23 stsp if (target_len == -1) {
8929 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8930 35213c7c 2020-07-23 stsp ondisk_path);
8931 35213c7c 2020-07-23 stsp break;
8932 35213c7c 2020-07-23 stsp }
8933 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8934 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8935 35213c7c 2020-07-23 stsp a->worktree->root_path);
8936 35213c7c 2020-07-23 stsp if (err)
8937 35213c7c 2020-07-23 stsp break;
8938 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8939 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8940 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8941 35213c7c 2020-07-23 stsp break;
8942 35213c7c 2020-07-23 stsp }
8943 35213c7c 2020-07-23 stsp }
8944 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8945 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8946 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8947 35213c7c 2020-07-23 stsp else
8948 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8949 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8950 0aeb8099 2020-07-23 stsp } else {
8951 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8952 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8953 0aeb8099 2020-07-23 stsp }
8954 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8955 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8956 dc424a06 2019-08-07 stsp break;
8957 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8958 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8959 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8960 f289c82c 2022-06-13 thomas if (err)
8961 f289c82c 2022-06-13 thomas break;
8962 f289c82c 2022-06-13 thomas /*
8963 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8964 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8965 f289c82c 2022-06-13 thomas */
8966 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8967 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8968 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8969 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8970 f289c82c 2022-06-13 thomas }
8971 0cb83759 2019-08-03 stsp break;
8972 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8973 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8974 d3e7c587 2019-08-03 stsp break;
8975 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8976 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8977 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8978 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8979 dc424a06 2019-08-07 stsp if (err)
8980 dc424a06 2019-08-07 stsp break;
8981 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8982 88f33a19 2019-08-08 stsp break;
8983 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8984 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8985 dc424a06 2019-08-07 stsp break;
8986 88f33a19 2019-08-08 stsp }
8987 dc424a06 2019-08-07 stsp }
8988 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8989 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8990 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8991 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8992 dc424a06 2019-08-07 stsp break;
8993 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8994 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8995 12463d8b 2019-12-13 stsp de_name);
8996 0cb83759 2019-08-03 stsp break;
8997 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8998 d3e7c587 2019-08-03 stsp break;
8999 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9000 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9001 ebf48fd5 2019-08-03 stsp break;
9002 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9003 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9004 2a06fe5f 2019-08-24 stsp break;
9005 0cb83759 2019-08-03 stsp default:
9006 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9007 537ac44b 2019-08-03 stsp break;
9008 0cb83759 2019-08-03 stsp }
9009 dc424a06 2019-08-07 stsp
9010 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9011 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9012 dc424a06 2019-08-07 stsp free(path_content);
9013 2db2652d 2019-08-07 stsp free(ondisk_path);
9014 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9015 0ebf8283 2019-07-24 stsp return err;
9016 0ebf8283 2019-07-24 stsp }
9017 0cb83759 2019-08-03 stsp
9018 0cb83759 2019-08-03 stsp const struct got_error *
9019 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9020 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9021 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9022 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9023 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9024 0cb83759 2019-08-03 stsp {
9025 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9026 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9027 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9028 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9029 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9030 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9031 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9032 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9033 0cb83759 2019-08-03 stsp
9034 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9035 0cb83759 2019-08-03 stsp if (err)
9036 0cb83759 2019-08-03 stsp return err;
9037 0cb83759 2019-08-03 stsp
9038 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9039 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9040 735ef5ac 2019-08-03 stsp if (err)
9041 735ef5ac 2019-08-03 stsp goto done;
9042 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9043 735ef5ac 2019-08-03 stsp if (err)
9044 735ef5ac 2019-08-03 stsp goto done;
9045 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9046 0cb83759 2019-08-03 stsp if (err)
9047 0cb83759 2019-08-03 stsp goto done;
9048 0cb83759 2019-08-03 stsp
9049 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9050 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9051 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9052 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9053 2db2652d 2019-08-07 stsp oka.repo = repo;
9054 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9055 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9056 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9057 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9058 735ef5ac 2019-08-03 stsp if (err)
9059 735ef5ac 2019-08-03 stsp goto done;
9060 735ef5ac 2019-08-03 stsp }
9061 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9062 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9063 2db2652d 2019-08-07 stsp goto done;
9064 2db2652d 2019-08-07 stsp }
9065 735ef5ac 2019-08-03 stsp
9066 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9067 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9068 2db2652d 2019-08-07 stsp spa.repo = repo;
9069 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9070 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9071 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9072 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9073 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9074 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9075 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9076 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9077 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9078 42005733 2019-08-03 stsp if (err)
9079 2db2652d 2019-08-07 stsp goto done;
9080 7b5dc508 2019-10-28 stsp }
9081 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9082 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9083 7b5dc508 2019-10-28 stsp goto done;
9084 0cb83759 2019-08-03 stsp }
9085 0cb83759 2019-08-03 stsp
9086 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9087 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9088 0cb83759 2019-08-03 stsp err = sync_err;
9089 0cb83759 2019-08-03 stsp done:
9090 735ef5ac 2019-08-03 stsp if (head_ref)
9091 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9092 735ef5ac 2019-08-03 stsp free(head_commit_id);
9093 0cb83759 2019-08-03 stsp free(fileindex_path);
9094 0cb83759 2019-08-03 stsp if (fileindex)
9095 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9096 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9097 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9098 0cb83759 2019-08-03 stsp err = unlockerr;
9099 0cb83759 2019-08-03 stsp return err;
9100 0cb83759 2019-08-03 stsp }
9101 ad493afc 2019-08-03 stsp
9102 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9103 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9104 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9105 ad493afc 2019-08-03 stsp struct got_repository *repo;
9106 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9107 ad493afc 2019-08-03 stsp void *progress_arg;
9108 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9109 2e1f37b0 2019-08-08 stsp void *patch_arg;
9110 ad493afc 2019-08-03 stsp };
9111 2e1f37b0 2019-08-08 stsp
9112 2e1f37b0 2019-08-08 stsp static const struct got_error *
9113 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9114 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9115 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9116 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9117 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9118 2e1f37b0 2019-08-08 stsp {
9119 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9120 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9121 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9122 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9123 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9124 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9125 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9126 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9127 2e1f37b0 2019-08-08 stsp
9128 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9129 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9130 2e1f37b0 2019-08-08 stsp
9131 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9132 2e1f37b0 2019-08-08 stsp if (err)
9133 2e1f37b0 2019-08-08 stsp return err;
9134 f4ae6ddb 2022-07-01 thomas
9135 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9136 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9137 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9138 f4ae6ddb 2022-07-01 thomas goto done;
9139 f4ae6ddb 2022-07-01 thomas }
9140 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9141 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9142 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9143 f4ae6ddb 2022-07-01 thomas goto done;
9144 f4ae6ddb 2022-07-01 thomas }
9145 f4ae6ddb 2022-07-01 thomas
9146 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9147 2e1f37b0 2019-08-08 stsp if (err)
9148 2e1f37b0 2019-08-08 stsp goto done;
9149 2e1f37b0 2019-08-08 stsp
9150 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9151 2e1f37b0 2019-08-08 stsp if (err)
9152 2e1f37b0 2019-08-08 stsp goto done;
9153 2e1f37b0 2019-08-08 stsp
9154 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9155 2e1f37b0 2019-08-08 stsp if (err)
9156 2e1f37b0 2019-08-08 stsp goto done;
9157 2e1f37b0 2019-08-08 stsp
9158 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9159 f4ae6ddb 2022-07-01 thomas fd2);
9160 2e1f37b0 2019-08-08 stsp if (err)
9161 2e1f37b0 2019-08-08 stsp goto done;
9162 2e1f37b0 2019-08-08 stsp
9163 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9164 2e1f37b0 2019-08-08 stsp if (err)
9165 2e1f37b0 2019-08-08 stsp goto done;
9166 ad493afc 2019-08-03 stsp
9167 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9168 2e1f37b0 2019-08-08 stsp if (err)
9169 2e1f37b0 2019-08-08 stsp goto done;
9170 2e1f37b0 2019-08-08 stsp
9171 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9172 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9173 2e1f37b0 2019-08-08 stsp if (err)
9174 2e1f37b0 2019-08-08 stsp goto done;
9175 2e1f37b0 2019-08-08 stsp
9176 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9177 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9178 2e1f37b0 2019-08-08 stsp if (err)
9179 2e1f37b0 2019-08-08 stsp goto done;
9180 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9181 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9182 2e1f37b0 2019-08-08 stsp if (err)
9183 2e1f37b0 2019-08-08 stsp goto done;
9184 2e1f37b0 2019-08-08 stsp
9185 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9186 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9187 2e1f37b0 2019-08-08 stsp goto done;
9188 2e1f37b0 2019-08-08 stsp }
9189 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9190 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9191 2e1f37b0 2019-08-08 stsp goto done;
9192 2e1f37b0 2019-08-08 stsp }
9193 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9194 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9195 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9196 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9197 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9198 fe621944 2020-11-10 stsp nchanges++;
9199 fe621944 2020-11-10 stsp }
9200 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9201 2e1f37b0 2019-08-08 stsp int choice;
9202 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9203 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9204 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9205 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9206 2e1f37b0 2019-08-08 stsp if (err)
9207 2e1f37b0 2019-08-08 stsp goto done;
9208 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9209 2e1f37b0 2019-08-08 stsp have_content = 1;
9210 19e4b907 2019-08-08 stsp else
9211 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9212 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9213 2e1f37b0 2019-08-08 stsp break;
9214 2e1f37b0 2019-08-08 stsp }
9215 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9216 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9217 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9218 2e1f37b0 2019-08-08 stsp done:
9219 2e1f37b0 2019-08-08 stsp free(label1);
9220 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9221 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9222 2e1f37b0 2019-08-08 stsp if (blob)
9223 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9224 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9225 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9226 2e1f37b0 2019-08-08 stsp if (staged_blob)
9227 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9228 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9229 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9230 fe621944 2020-11-10 stsp err = free_err;
9231 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9232 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9233 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9234 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9235 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9236 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9237 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9238 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9239 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9240 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9241 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9242 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9243 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9244 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9245 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9246 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9247 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9248 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9249 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9250 2e1f37b0 2019-08-08 stsp }
9251 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9252 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9253 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9254 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9255 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9256 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9257 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9258 2e1f37b0 2019-08-08 stsp }
9259 2e1f37b0 2019-08-08 stsp free(path1);
9260 2e1f37b0 2019-08-08 stsp free(path2);
9261 fda8017d 2020-07-23 stsp return err;
9262 fda8017d 2020-07-23 stsp }
9263 fda8017d 2020-07-23 stsp
9264 fda8017d 2020-07-23 stsp static const struct got_error *
9265 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9266 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9267 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9268 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9269 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9270 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9271 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9272 fda8017d 2020-07-23 stsp {
9273 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9274 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9275 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9276 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9277 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9278 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9279 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9280 fda8017d 2020-07-23 stsp struct stat sb;
9281 fda8017d 2020-07-23 stsp
9282 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9283 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9284 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9285 fda8017d 2020-07-23 stsp if (err)
9286 fda8017d 2020-07-23 stsp return err;
9287 fda8017d 2020-07-23 stsp
9288 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9289 fda8017d 2020-07-23 stsp return NULL;
9290 fda8017d 2020-07-23 stsp
9291 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9292 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9293 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9294 fda8017d 2020-07-23 stsp if (err)
9295 fda8017d 2020-07-23 stsp goto done;
9296 fda8017d 2020-07-23 stsp }
9297 fda8017d 2020-07-23 stsp
9298 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9299 fda8017d 2020-07-23 stsp if (f == NULL) {
9300 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9301 fda8017d 2020-07-23 stsp path_unstaged_content);
9302 fda8017d 2020-07-23 stsp goto done;
9303 fda8017d 2020-07-23 stsp }
9304 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9305 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9306 fda8017d 2020-07-23 stsp goto done;
9307 fda8017d 2020-07-23 stsp }
9308 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9309 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9310 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9311 fda8017d 2020-07-23 stsp size_t r;
9312 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9313 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9314 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9315 fda8017d 2020-07-23 stsp goto done;
9316 fda8017d 2020-07-23 stsp }
9317 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9318 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9319 fda8017d 2020-07-23 stsp goto done;
9320 fda8017d 2020-07-23 stsp }
9321 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9322 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9323 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9324 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9325 fda8017d 2020-07-23 stsp progress_arg);
9326 fda8017d 2020-07-23 stsp } else {
9327 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9328 67a66647 2021-05-31 stsp
9329 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9330 67a66647 2021-05-31 stsp if (err)
9331 67a66647 2021-05-31 stsp return err;
9332 67a66647 2021-05-31 stsp
9333 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9334 67a66647 2021-05-31 stsp parent) == -1) {
9335 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9336 67a66647 2021-05-31 stsp base_path = NULL;
9337 67a66647 2021-05-31 stsp goto done;
9338 67a66647 2021-05-31 stsp }
9339 67a66647 2021-05-31 stsp
9340 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9341 fc2a50f2 2022-11-01 thomas base_path, "");
9342 67a66647 2021-05-31 stsp if (err)
9343 67a66647 2021-05-31 stsp goto done;
9344 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9345 67a66647 2021-05-31 stsp blob_base);
9346 67a66647 2021-05-31 stsp if (err)
9347 67a66647 2021-05-31 stsp goto done;
9348 67a66647 2021-05-31 stsp
9349 b6b86fd1 2022-08-30 thomas /*
9350 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9351 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9352 eec2f5a9 2021-06-03 stsp */
9353 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9354 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9355 eec2f5a9 2021-06-03 stsp ondisk_path);
9356 eec2f5a9 2021-06-03 stsp if (err)
9357 eec2f5a9 2021-06-03 stsp goto done;
9358 eec2f5a9 2021-06-03 stsp } else {
9359 eec2f5a9 2021-06-03 stsp int fd;
9360 06340621 2021-12-31 thomas fd = open(ondisk_path,
9361 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9362 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9363 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9364 eec2f5a9 2021-06-03 stsp goto done;
9365 eec2f5a9 2021-06-03 stsp }
9366 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9367 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9368 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9369 eec2f5a9 2021-06-03 stsp close(fd);
9370 eec2f5a9 2021-06-03 stsp goto done;
9371 eec2f5a9 2021-06-03 stsp }
9372 eec2f5a9 2021-06-03 stsp }
9373 eec2f5a9 2021-06-03 stsp
9374 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9375 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9376 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9377 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9378 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9379 fda8017d 2020-07-23 stsp }
9380 fda8017d 2020-07-23 stsp if (err)
9381 fda8017d 2020-07-23 stsp goto done;
9382 fda8017d 2020-07-23 stsp
9383 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9384 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9385 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9386 2ac8aa02 2020-11-09 stsp } else {
9387 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9388 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9389 2ac8aa02 2020-11-09 stsp }
9390 fda8017d 2020-07-23 stsp done:
9391 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9392 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9393 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9394 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9395 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9396 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9397 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9398 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9399 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9400 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9401 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9402 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9403 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9404 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9405 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9406 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9407 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9408 67a66647 2021-05-31 stsp free(blob_base_path);
9409 67a66647 2021-05-31 stsp free(parent);
9410 67a66647 2021-05-31 stsp free(base_path);
9411 2e1f37b0 2019-08-08 stsp return err;
9412 2e1f37b0 2019-08-08 stsp }
9413 2e1f37b0 2019-08-08 stsp
9414 ad493afc 2019-08-03 stsp static const struct got_error *
9415 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9416 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9417 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9418 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9419 ad493afc 2019-08-03 stsp {
9420 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9421 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9422 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9423 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9424 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9425 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9426 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9427 9bc94a15 2019-08-03 stsp struct stat sb;
9428 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9429 ad493afc 2019-08-03 stsp
9430 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9431 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9432 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9433 2e1f37b0 2019-08-08 stsp return NULL;
9434 2e1f37b0 2019-08-08 stsp
9435 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9436 ad493afc 2019-08-03 stsp if (ie == NULL)
9437 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9438 9bc94a15 2019-08-03 stsp
9439 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9440 9bc94a15 2019-08-03 stsp == -1)
9441 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9442 ad493afc 2019-08-03 stsp
9443 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9444 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9445 f69721c3 2019-10-21 stsp if (err)
9446 f69721c3 2019-10-21 stsp goto done;
9447 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9448 f69721c3 2019-10-21 stsp id_str) == -1) {
9449 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9450 f4ae6ddb 2022-07-01 thomas goto done;
9451 f4ae6ddb 2022-07-01 thomas }
9452 f4ae6ddb 2022-07-01 thomas
9453 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9454 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9455 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9456 f4ae6ddb 2022-07-01 thomas goto done;
9457 f4ae6ddb 2022-07-01 thomas }
9458 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9459 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9460 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9461 f69721c3 2019-10-21 stsp goto done;
9462 f69721c3 2019-10-21 stsp }
9463 f69721c3 2019-10-21 stsp
9464 ad493afc 2019-08-03 stsp switch (staged_status) {
9465 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9466 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9467 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9468 ad493afc 2019-08-03 stsp if (err)
9469 ad493afc 2019-08-03 stsp break;
9470 ad493afc 2019-08-03 stsp /* fall through */
9471 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9472 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9473 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9474 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9475 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9476 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9477 2e1f37b0 2019-08-08 stsp if (err)
9478 2e1f37b0 2019-08-08 stsp break;
9479 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9480 2e1f37b0 2019-08-08 stsp break;
9481 2e1f37b0 2019-08-08 stsp } else {
9482 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9483 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9484 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9485 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9486 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9487 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9488 2e1f37b0 2019-08-08 stsp }
9489 2e1f37b0 2019-08-08 stsp }
9490 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9491 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9492 ad493afc 2019-08-03 stsp if (err)
9493 ad493afc 2019-08-03 stsp break;
9494 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9495 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9496 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9497 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9498 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9499 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9500 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9501 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9502 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9503 ea7786be 2020-07-23 stsp break;
9504 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9505 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9506 36bf999c 2020-07-23 stsp char *staged_target;
9507 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9508 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9509 36bf999c 2020-07-23 stsp if (err)
9510 36bf999c 2020-07-23 stsp goto done;
9511 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9512 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9513 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9514 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9515 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9516 36bf999c 2020-07-23 stsp free(staged_target);
9517 dfe9fba0 2020-07-23 stsp } else {
9518 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9519 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9520 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9521 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9522 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9523 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9524 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9525 dfe9fba0 2020-07-23 stsp }
9526 ea7786be 2020-07-23 stsp break;
9527 ea7786be 2020-07-23 stsp default:
9528 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9529 ea7786be 2020-07-23 stsp break;
9530 ea7786be 2020-07-23 stsp }
9531 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9532 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9533 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9534 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9535 2ac8aa02 2020-11-09 stsp }
9536 ad493afc 2019-08-03 stsp break;
9537 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9538 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9539 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9540 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9541 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9542 2e1f37b0 2019-08-08 stsp if (err)
9543 2e1f37b0 2019-08-08 stsp break;
9544 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9545 2e1f37b0 2019-08-08 stsp break;
9546 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9547 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9548 2e1f37b0 2019-08-08 stsp break;
9549 2e1f37b0 2019-08-08 stsp }
9550 2e1f37b0 2019-08-08 stsp }
9551 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9552 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9553 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9554 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9555 9bc94a15 2019-08-03 stsp if (err)
9556 9bc94a15 2019-08-03 stsp break;
9557 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9558 ad493afc 2019-08-03 stsp break;
9559 ad493afc 2019-08-03 stsp }
9560 f69721c3 2019-10-21 stsp done:
9561 ad493afc 2019-08-03 stsp free(ondisk_path);
9562 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9563 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9564 ad493afc 2019-08-03 stsp if (blob_base)
9565 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9566 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9567 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9568 ad493afc 2019-08-03 stsp if (blob_staged)
9569 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9570 f69721c3 2019-10-21 stsp free(id_str);
9571 f69721c3 2019-10-21 stsp free(label_orig);
9572 ad493afc 2019-08-03 stsp return err;
9573 ad493afc 2019-08-03 stsp }
9574 ad493afc 2019-08-03 stsp
9575 ad493afc 2019-08-03 stsp const struct got_error *
9576 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9577 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9578 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9579 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9580 ad493afc 2019-08-03 stsp struct got_repository *repo)
9581 ad493afc 2019-08-03 stsp {
9582 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9583 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9584 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9585 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9586 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9587 ad493afc 2019-08-03 stsp
9588 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9589 ad493afc 2019-08-03 stsp if (err)
9590 ad493afc 2019-08-03 stsp return err;
9591 ad493afc 2019-08-03 stsp
9592 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9593 ad493afc 2019-08-03 stsp if (err)
9594 ad493afc 2019-08-03 stsp goto done;
9595 ad493afc 2019-08-03 stsp
9596 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9597 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9598 ad493afc 2019-08-03 stsp upa.repo = repo;
9599 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9600 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9601 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9602 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9603 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9604 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9605 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9606 ad493afc 2019-08-03 stsp if (err)
9607 ad493afc 2019-08-03 stsp goto done;
9608 ad493afc 2019-08-03 stsp }
9609 ad493afc 2019-08-03 stsp
9610 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9611 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9612 ad493afc 2019-08-03 stsp err = sync_err;
9613 ad493afc 2019-08-03 stsp done:
9614 ad493afc 2019-08-03 stsp free(fileindex_path);
9615 ad493afc 2019-08-03 stsp if (fileindex)
9616 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9617 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9618 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9619 ad493afc 2019-08-03 stsp err = unlockerr;
9620 ad493afc 2019-08-03 stsp return err;
9621 ad493afc 2019-08-03 stsp }
9622 b2118c49 2020-07-28 stsp
9623 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9624 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9625 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9626 b2118c49 2020-07-28 stsp void *info_arg;
9627 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9628 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9629 b2118c49 2020-07-28 stsp void *cancel_arg;
9630 b2118c49 2020-07-28 stsp };
9631 b2118c49 2020-07-28 stsp
9632 b2118c49 2020-07-28 stsp static const struct got_error *
9633 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9634 b2118c49 2020-07-28 stsp {
9635 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9636 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9637 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9638 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9639 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9640 b2118c49 2020-07-28 stsp int stage;
9641 b2118c49 2020-07-28 stsp
9642 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9643 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9644 b2118c49 2020-07-28 stsp
9645 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9646 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9647 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9648 b2118c49 2020-07-28 stsp break;
9649 b2118c49 2020-07-28 stsp }
9650 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9651 b2118c49 2020-07-28 stsp return NULL;
9652 b2118c49 2020-07-28 stsp
9653 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9654 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9655 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9656 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9657 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9658 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9659 43010591 2023-02-17 thomas &staged_blob_id, ie);
9660 b2118c49 2020-07-28 stsp }
9661 b2118c49 2020-07-28 stsp
9662 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9663 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9664 b2118c49 2020-07-28 stsp
9665 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9666 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9667 b2118c49 2020-07-28 stsp }
9668 b2118c49 2020-07-28 stsp
9669 b2118c49 2020-07-28 stsp const struct got_error *
9670 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9671 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9672 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9673 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9674 b2118c49 2020-07-28 stsp
9675 b2118c49 2020-07-28 stsp {
9676 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9677 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9678 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9679 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9680 b2118c49 2020-07-28 stsp
9681 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9682 b2118c49 2020-07-28 stsp if (err)
9683 b2118c49 2020-07-28 stsp return err;
9684 b2118c49 2020-07-28 stsp
9685 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9686 b2118c49 2020-07-28 stsp if (err)
9687 b2118c49 2020-07-28 stsp goto done;
9688 b2118c49 2020-07-28 stsp
9689 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9690 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9691 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9692 b2118c49 2020-07-28 stsp arg.paths = paths;
9693 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9694 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9695 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9696 b2118c49 2020-07-28 stsp &arg);
9697 b2118c49 2020-07-28 stsp done:
9698 b2118c49 2020-07-28 stsp free(fileindex_path);
9699 b2118c49 2020-07-28 stsp if (fileindex)
9700 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9701 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9702 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9703 b2118c49 2020-07-28 stsp err = unlockerr;
9704 b2118c49 2020-07-28 stsp return err;
9705 b2118c49 2020-07-28 stsp }
9706 814624e7 2022-03-22 thomas
9707 814624e7 2022-03-22 thomas static const struct got_error *
9708 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9709 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9710 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9711 814624e7 2022-03-22 thomas {
9712 814624e7 2022-03-22 thomas const struct got_error *err;
9713 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9714 814624e7 2022-03-22 thomas struct stat sb;
9715 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9716 814624e7 2022-03-22 thomas
9717 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9718 814624e7 2022-03-22 thomas if (err)
9719 814624e7 2022-03-22 thomas return err;
9720 814624e7 2022-03-22 thomas
9721 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9722 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9723 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9724 814624e7 2022-03-22 thomas
9725 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9726 814624e7 2022-03-22 thomas if (ie) {
9727 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9728 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9729 12de5570 2022-05-01 thomas repo);
9730 814624e7 2022-03-22 thomas if (err)
9731 814624e7 2022-03-22 thomas goto done;
9732 814624e7 2022-03-22 thomas } else {
9733 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9734 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9735 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9736 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9737 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9738 814624e7 2022-03-22 thomas ondisk_path);
9739 814624e7 2022-03-22 thomas goto done;
9740 814624e7 2022-03-22 thomas }
9741 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9742 814624e7 2022-03-22 thomas }
9743 814624e7 2022-03-22 thomas }
9744 814624e7 2022-03-22 thomas
9745 814624e7 2022-03-22 thomas done:
9746 814624e7 2022-03-22 thomas free(ondisk_path);
9747 814624e7 2022-03-22 thomas return err;
9748 814624e7 2022-03-22 thomas }
9749 814624e7 2022-03-22 thomas
9750 814624e7 2022-03-22 thomas static const struct got_error *
9751 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9752 814624e7 2022-03-22 thomas unsigned char staged_status)
9753 814624e7 2022-03-22 thomas {
9754 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9755 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9756 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9757 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9758 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9759 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9760 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9762 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9763 814624e7 2022-03-22 thomas return NULL;
9764 814624e7 2022-03-22 thomas }
9765 814624e7 2022-03-22 thomas
9766 814624e7 2022-03-22 thomas static const struct got_error *
9767 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9768 814624e7 2022-03-22 thomas {
9769 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9770 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9771 814624e7 2022-03-22 thomas return NULL;
9772 814624e7 2022-03-22 thomas }
9773 814624e7 2022-03-22 thomas
9774 814624e7 2022-03-22 thomas static const struct got_error *
9775 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9776 814624e7 2022-03-22 thomas unsigned char staged_status)
9777 814624e7 2022-03-22 thomas {
9778 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9779 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9780 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9781 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9782 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9783 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9784 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9785 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9786 814624e7 2022-03-22 thomas return NULL;
9787 814624e7 2022-03-22 thomas }
9788 814624e7 2022-03-22 thomas
9789 814624e7 2022-03-22 thomas const struct got_error *
9790 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9791 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9792 814624e7 2022-03-22 thomas {
9793 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9794 814624e7 2022-03-22 thomas }
9795 814624e7 2022-03-22 thomas
9796 814624e7 2022-03-22 thomas const struct got_error *
9797 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9798 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9799 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9800 814624e7 2022-03-22 thomas {
9801 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9802 814624e7 2022-03-22 thomas int file_renamed = 0;
9803 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9804 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9805 814624e7 2022-03-22 thomas
9806 814624e7 2022-03-22 thomas *oldpath = NULL;
9807 814624e7 2022-03-22 thomas *newpath = NULL;
9808 814624e7 2022-03-22 thomas
9809 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9810 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9811 814624e7 2022-03-22 thomas if (err)
9812 814624e7 2022-03-22 thomas goto done;
9813 814624e7 2022-03-22 thomas
9814 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9815 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9816 814624e7 2022-03-22 thomas if (err)
9817 814624e7 2022-03-22 thomas goto done;
9818 814624e7 2022-03-22 thomas
9819 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9820 814624e7 2022-03-22 thomas file_renamed = 1;
9821 814624e7 2022-03-22 thomas
9822 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9823 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9824 814624e7 2022-03-22 thomas else if (file_renamed) {
9825 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9826 814624e7 2022-03-22 thomas if (err == NULL)
9827 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9828 814624e7 2022-03-22 thomas } else if (old == NULL)
9829 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9830 814624e7 2022-03-22 thomas else
9831 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9832 814624e7 2022-03-22 thomas
9833 814624e7 2022-03-22 thomas done:
9834 814624e7 2022-03-22 thomas if (err) {
9835 814624e7 2022-03-22 thomas free(*oldpath);
9836 814624e7 2022-03-22 thomas *oldpath = NULL;
9837 814624e7 2022-03-22 thomas free(*newpath);
9838 814624e7 2022-03-22 thomas *newpath = NULL;
9839 814624e7 2022-03-22 thomas }
9840 814624e7 2022-03-22 thomas return err;
9841 814624e7 2022-03-22 thomas }
9842 814624e7 2022-03-22 thomas
9843 814624e7 2022-03-22 thomas const struct got_error *
9844 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9845 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9846 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9847 814624e7 2022-03-22 thomas {
9848 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9849 5e22b737 2022-05-31 thomas
9850 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9851 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9852 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9853 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9854 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9855 5e22b737 2022-05-31 thomas saa.repo = repo;
9856 5e22b737 2022-05-31 thomas
9857 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9858 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9859 814624e7 2022-03-22 thomas }
9860 5e22b737 2022-05-31 thomas
9861 5e22b737 2022-05-31 thomas const struct got_error *
9862 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9863 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9864 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9865 5e22b737 2022-05-31 thomas {
9866 8ec7352a 2023-06-01 thomas const struct got_error *err;
9867 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9868 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9869 5e22b737 2022-05-31 thomas
9870 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9871 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9872 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9873 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9874 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9875 5e22b737 2022-05-31 thomas sda.repo = repo;
9876 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9877 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9878 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9879 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9880 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9881 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9882 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9883 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9884 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9885 8ec7352a 2023-06-01 thomas
9886 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9887 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9888 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9889 8ec7352a 2023-06-01 thomas return err;
9890 5e22b737 2022-05-31 thomas }
9891 5e22b737 2022-05-31 thomas
9892 5e22b737 2022-05-31 thomas const struct got_error *
9893 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9894 36832a8e 2022-05-31 thomas const char *fileindex_path)
9895 5e22b737 2022-05-31 thomas {
9896 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9897 5e22b737 2022-05-31 thomas
9898 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9899 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9900 5e22b737 2022-05-31 thomas
9901 5e22b737 2022-05-31 thomas return err;
9902 5e22b737 2022-05-31 thomas }