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 21908da4 2019-01-13 stsp if (errno == ENOENT) {
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 f5375317 2020-10-20 stsp free(parent);
1415 21908da4 2019-01-13 stsp if (err)
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 a2c162eb 2022-10-30 thomas mode);
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1428 3b9f0f87 2020-07-23 stsp }
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1435 d70b8e30 2018-12-27 stsp } else {
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1438 507dc3bb 2018-12-29 stsp if (err)
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1441 a2c162eb 2022-10-30 thomas
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1446 a2c162eb 2022-10-30 thomas }
1447 9d31a1d8 2018-03-11 stsp }
1448 507dc3bb 2018-12-29 stsp } else
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1450 3818e3c4 2020-11-01 naddy }
1451 3818e3c4 2020-11-01 naddy
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 bd6aa359 2020-07-23 stsp path);
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 bd6aa359 2020-07-23 stsp if (err)
1463 bd6aa359 2020-07-23 stsp goto done;
1464 bd6aa359 2020-07-23 stsp }
1465 d7b62c98 2018-12-27 stsp
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1467 9d31a1d8 2018-03-11 stsp do {
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1470 9d31a1d8 2018-03-11 stsp if (err)
1471 9d31a1d8 2018-03-11 stsp break;
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1481 9d31a1d8 2018-03-11 stsp }
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1485 9d31a1d8 2018-03-11 stsp
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1489 816dc654 2019-02-16 stsp }
1490 9d31a1d8 2018-03-11 stsp
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1495 f6d8c0ac 2020-11-09 stsp }
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1500 68ed9ba5 2019-02-10 stsp }
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1503 507dc3bb 2018-12-29 stsp }
1504 507dc3bb 2018-12-29 stsp
1505 9d31a1d8 2018-03-11 stsp done:
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1512 6353ad76 2019-02-08 stsp }
1513 6353ad76 2019-02-08 stsp
1514 be94c032 2023-02-20 thomas /*
1515 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1517 be94c032 2023-02-20 thomas */
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1520 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1521 be94c032 2023-02-20 thomas FILE *ondisk_file)
1522 7154f6ce 2019-03-27 stsp {
1523 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1528 7154f6ce 2019-03-27 stsp };
1529 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1530 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1531 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1532 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1533 9bdd68dd 2020-01-02 naddy char *line = NULL;
1534 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1535 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1536 7154f6ce 2019-03-27 stsp
1537 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1538 8de9d8ad 2023-02-20 thomas return NULL;
1539 be94c032 2023-02-20 thomas
1540 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1541 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1542 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1543 be94c032 2023-02-20 thomas
1544 8de9d8ad 2023-02-20 thomas if (blob) {
1545 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1546 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 be94c032 2023-02-20 thomas if (err)
1548 be94c032 2023-02-20 thomas goto done;
1549 8de9d8ad 2023-02-20 thomas }
1550 be94c032 2023-02-20 thomas
1551 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 8de9d8ad 2023-02-20 thomas if (err)
1554 8de9d8ad 2023-02-20 thomas goto done;
1555 8de9d8ad 2023-02-20 thomas
1556 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1560 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1561 450d9f6b 2023-02-20 thomas off_t pos;
1562 8de9d8ad 2023-02-20 thomas
1563 8de9d8ad 2023-02-20 thomas /*
1564 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1565 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1566 8de9d8ad 2023-02-20 thomas */
1567 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1568 8de9d8ad 2023-02-20 thomas
1569 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1571 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1572 7154f6ce 2019-03-27 stsp }
1573 7154f6ce 2019-03-27 stsp
1574 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1575 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 450d9f6b 2023-02-20 thomas goto done;
1578 7154f6ce 2019-03-27 stsp }
1579 8de9d8ad 2023-02-20 thomas
1580 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1582 450d9f6b 2023-02-20 thomas
1583 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1584 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1585 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1586 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1587 8de9d8ad 2023-02-20 thomas break;
1588 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas }
1591 8de9d8ad 2023-02-20 thomas
1592 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1593 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1594 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1595 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1597 8de9d8ad 2023-02-20 thomas goto done;
1598 8de9d8ad 2023-02-20 thomas } else
1599 8de9d8ad 2023-02-20 thomas i++;
1600 8de9d8ad 2023-02-20 thomas }
1601 8de9d8ad 2023-02-20 thomas ++ln;
1602 8de9d8ad 2023-02-20 thomas }
1603 7154f6ce 2019-03-27 stsp }
1604 be94c032 2023-02-20 thomas
1605 be94c032 2023-02-20 thomas done:
1606 9bdd68dd 2020-01-02 naddy free(line);
1607 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1609 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1610 8de9d8ad 2023-02-20 thomas if (err == NULL)
1611 8de9d8ad 2023-02-20 thomas err = free_err;
1612 7154f6ce 2019-03-27 stsp
1613 7154f6ce 2019-03-27 stsp return err;
1614 e2b1e152 2019-07-13 stsp }
1615 e2b1e152 2019-07-13 stsp
1616 e2b1e152 2019-07-13 stsp static int
1617 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1618 1ebedb77 2019-10-19 stsp {
1619 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1621 1ebedb77 2019-10-19 stsp }
1622 1ebedb77 2019-10-19 stsp
1623 1ebedb77 2019-10-19 stsp static int
1624 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1625 e2b1e152 2019-07-13 stsp {
1626 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1631 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1632 c363b2c1 2019-08-03 stsp }
1633 c363b2c1 2019-08-03 stsp
1634 c363b2c1 2019-08-03 stsp static unsigned char
1635 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1636 c363b2c1 2019-08-03 stsp {
1637 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1638 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1639 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1644 c363b2c1 2019-08-03 stsp default:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1646 a919d5c4 2020-07-23 stsp }
1647 a919d5c4 2020-07-23 stsp }
1648 a919d5c4 2020-07-23 stsp
1649 a919d5c4 2020-07-23 stsp static const struct got_error *
1650 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1651 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1652 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1653 a919d5c4 2020-07-23 stsp {
1654 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1655 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1656 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1657 a919d5c4 2020-07-23 stsp ssize_t elen;
1658 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1659 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1665 a919d5c4 2020-07-23 stsp do {
1666 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1667 a919d5c4 2020-07-23 stsp if (err)
1668 a919d5c4 2020-07-23 stsp return err;
1669 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1670 a919d5c4 2020-07-23 stsp /*
1671 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1672 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1673 a919d5c4 2020-07-23 stsp */
1674 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1675 a919d5c4 2020-07-23 stsp }
1676 a919d5c4 2020-07-23 stsp if (len > 0) {
1677 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1678 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1679 a919d5c4 2020-07-23 stsp len - hdrlen);
1680 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1681 a919d5c4 2020-07-23 stsp hdrlen = 0;
1682 a919d5c4 2020-07-23 stsp }
1683 a919d5c4 2020-07-23 stsp } while (len != 0);
1684 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1685 a919d5c4 2020-07-23 stsp
1686 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1687 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 a919d5c4 2020-07-23 stsp if (elen == -1)
1689 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1690 a919d5c4 2020-07-23 stsp } else {
1691 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1692 a919d5c4 2020-07-23 stsp if (elen == -1)
1693 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1694 c363b2c1 2019-08-03 stsp }
1695 a919d5c4 2020-07-23 stsp
1696 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp return NULL;
1700 7154f6ce 2019-03-27 stsp }
1701 7154f6ce 2019-03-27 stsp
1702 7154f6ce 2019-03-27 stsp static const struct got_error *
1703 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1704 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1705 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1706 6353ad76 2019-02-08 stsp {
1707 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1708 6353ad76 2019-02-08 stsp struct got_object_id id;
1709 6353ad76 2019-02-08 stsp size_t hdrlen;
1710 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1711 6353ad76 2019-02-08 stsp FILE *f = NULL;
1712 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1713 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1714 6353ad76 2019-02-08 stsp size_t flen, blen;
1715 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1716 6353ad76 2019-02-08 stsp
1717 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1718 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1719 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1720 6353ad76 2019-02-08 stsp
1721 7f91a133 2019-12-13 stsp /*
1722 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1723 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1724 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1725 7f91a133 2019-12-13 stsp */
1726 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1727 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1729 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1730 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1731 882ef1b9 2019-12-13 stsp else
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1733 882ef1b9 2019-12-13 stsp goto done;
1734 882ef1b9 2019-12-13 stsp }
1735 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1736 3d35a492 2019-12-13 stsp goto done;
1737 3d35a492 2019-12-13 stsp }
1738 7f91a133 2019-12-13 stsp } else {
1739 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1741 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1742 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1743 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1745 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1746 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1748 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1749 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1750 3d35a492 2019-12-13 stsp else
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1752 3d35a492 2019-12-13 stsp goto done;
1753 3d35a492 2019-12-13 stsp }
1754 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1755 1338848f 2019-12-13 stsp goto done;
1756 a378724f 2019-02-10 stsp }
1757 a378724f 2019-02-10 stsp }
1758 6353ad76 2019-02-08 stsp
1759 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1761 1338848f 2019-12-13 stsp goto done;
1762 3f148bc6 2019-07-27 stsp }
1763 efdd40df 2019-07-27 stsp
1764 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1766 1338848f 2019-12-13 stsp goto done;
1767 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1768 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1769 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1770 1338848f 2019-12-13 stsp goto done;
1771 d00136be 2019-03-26 stsp }
1772 b8f41171 2019-02-10 stsp
1773 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1774 f179e66d 2020-07-23 stsp goto done;
1775 f179e66d 2020-07-23 stsp
1776 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1777 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1779 1338848f 2019-12-13 stsp goto done;
1780 f179e66d 2020-07-23 stsp }
1781 6353ad76 2019-02-08 stsp
1782 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1783 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1784 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1785 c363b2c1 2019-08-03 stsp else
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp
1788 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1789 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1790 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1791 f4ae6ddb 2022-07-01 thomas goto done;
1792 f4ae6ddb 2022-07-01 thomas }
1793 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1794 6353ad76 2019-02-08 stsp if (err)
1795 1338848f 2019-12-13 stsp goto done;
1796 6353ad76 2019-02-08 stsp
1797 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1798 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1799 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1800 a919d5c4 2020-07-23 stsp goto done;
1801 a919d5c4 2020-07-23 stsp }
1802 a919d5c4 2020-07-23 stsp
1803 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1804 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 ab0d4361 2019-12-13 stsp if (fd == -1) {
1806 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1807 ab0d4361 2019-12-13 stsp goto done;
1808 ab0d4361 2019-12-13 stsp }
1809 3d35a492 2019-12-13 stsp }
1810 3d35a492 2019-12-13 stsp
1811 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1812 6353ad76 2019-02-08 stsp if (f == NULL) {
1813 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1814 6353ad76 2019-02-08 stsp goto done;
1815 6353ad76 2019-02-08 stsp }
1816 1338848f 2019-12-13 stsp fd = -1;
1817 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1818 656b1f76 2019-05-11 jcs for (;;) {
1819 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1821 6353ad76 2019-02-08 stsp if (err)
1822 7154f6ce 2019-03-27 stsp goto done;
1823 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1824 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1826 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1827 7154f6ce 2019-03-27 stsp goto done;
1828 80c5c120 2019-02-19 stsp }
1829 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1830 6353ad76 2019-02-08 stsp if (flen != 0)
1831 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1832 6353ad76 2019-02-08 stsp break;
1833 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1835 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1836 6353ad76 2019-02-08 stsp break;
1837 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1838 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1839 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1841 6353ad76 2019-02-08 stsp break;
1842 6353ad76 2019-02-08 stsp }
1843 6353ad76 2019-02-08 stsp } else {
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp }
1847 6353ad76 2019-02-08 stsp hdrlen = 0;
1848 6353ad76 2019-02-08 stsp }
1849 7154f6ce 2019-03-27 stsp
1850 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1851 7154f6ce 2019-03-27 stsp rewind(f);
1852 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1853 be94c032 2023-02-20 thomas sb, f);
1854 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1855 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1856 6353ad76 2019-02-08 stsp done:
1857 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1859 6353ad76 2019-02-08 stsp if (blob)
1860 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1861 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1862 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1863 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1864 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1865 9d31a1d8 2018-03-11 stsp return err;
1866 e2b1e152 2019-07-13 stsp }
1867 e2b1e152 2019-07-13 stsp
1868 e2b1e152 2019-07-13 stsp /*
1869 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1870 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1871 e2b1e152 2019-07-13 stsp */
1872 e2b1e152 2019-07-13 stsp static const struct got_error *
1873 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1875 e2b1e152 2019-07-13 stsp {
1876 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1878 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1879 e2b1e152 2019-07-13 stsp
1880 e2b1e152 2019-07-13 stsp return NULL;
1881 9d31a1d8 2018-03-11 stsp }
1882 9d31a1d8 2018-03-11 stsp
1883 9d31a1d8 2018-03-11 stsp static const struct got_error *
1884 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1885 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1887 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1888 0584f854 2019-04-06 stsp void *progress_arg)
1889 9d31a1d8 2018-03-11 stsp {
1890 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1891 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1892 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1893 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1894 b8f41171 2019-02-10 stsp struct stat sb;
1895 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1896 9d31a1d8 2018-03-11 stsp
1897 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1899 6353ad76 2019-02-08 stsp
1900 abb4604f 2019-07-27 stsp if (ie) {
1901 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1903 a76c42e6 2019-08-03 stsp goto done;
1904 a76c42e6 2019-08-03 stsp }
1905 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1906 7f91a133 2019-12-13 stsp repo);
1907 abb4604f 2019-07-27 stsp if (err)
1908 abb4604f 2019-07-27 stsp goto done;
1909 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1911 c90c8ce3 2020-07-23 stsp } else {
1912 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1913 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1914 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1915 e6f4ba31 2021-09-24 thomas ondisk_path);
1916 e6f4ba31 2021-09-24 thomas goto done;
1917 e6f4ba31 2021-09-24 thomas }
1918 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1919 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1920 e6f4ba31 2021-09-24 thomas } else {
1921 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1922 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1923 e6f4ba31 2021-09-24 thomas else
1924 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1925 e6f4ba31 2021-09-24 thomas }
1926 c90c8ce3 2020-07-23 stsp }
1927 6353ad76 2019-02-08 stsp
1928 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1929 2c41dce7 2021-06-27 stsp if (ie)
1930 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1931 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1932 b8f41171 2019-02-10 stsp goto done;
1933 b8f41171 2019-02-10 stsp }
1934 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1935 a769b60b 2021-06-27 stsp if (ie)
1936 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1937 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1938 5036ab18 2020-04-18 stsp path);
1939 5036ab18 2020-04-18 stsp goto done;
1940 5036ab18 2020-04-18 stsp }
1941 b8f41171 2019-02-10 stsp
1942 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1943 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1944 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1945 c6e8a826 2021-04-05 stsp /*
1946 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1947 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1948 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1949 c6e8a826 2021-04-05 stsp * updating contents of this file.
1950 c6e8a826 2021-04-05 stsp */
1951 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1952 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1953 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1954 c6e8a826 2021-04-05 stsp /* Same commit. */
1955 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1956 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1957 e2b1e152 2019-07-13 stsp if (err)
1958 e2b1e152 2019-07-13 stsp goto done;
1959 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1960 b8f41171 2019-02-10 stsp path);
1961 6353ad76 2019-02-08 stsp goto done;
1962 a378724f 2019-02-10 stsp }
1963 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1964 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1965 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1966 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1967 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1968 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1969 0f58026f 2021-04-05 stsp if (err)
1970 0f58026f 2021-04-05 stsp goto done;
1971 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1972 0f58026f 2021-04-05 stsp path);
1973 b8f41171 2019-02-10 stsp goto done;
1974 e2b1e152 2019-07-13 stsp }
1975 9d31a1d8 2018-03-11 stsp }
1976 9d31a1d8 2018-03-11 stsp
1977 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1978 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1979 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1980 f4ae6ddb 2022-07-01 thomas goto done;
1981 f4ae6ddb 2022-07-01 thomas }
1982 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1983 8da9e5f4 2019-01-12 stsp if (err)
1984 6353ad76 2019-02-08 stsp goto done;
1985 512f0d0e 2019-01-02 stsp
1986 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1987 234035bc 2019-06-01 stsp int update_timestamps;
1988 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1989 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1990 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1991 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1992 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1993 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1994 f4ae6ddb 2022-07-01 thomas goto done;
1995 f4ae6ddb 2022-07-01 thomas }
1996 234035bc 2019-06-01 stsp struct got_object_id id2;
1997 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
1998 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1999 f4ae6ddb 2022-07-01 thomas fd2);
2000 234035bc 2019-06-01 stsp if (err)
2001 f69721c3 2019-10-21 stsp goto done;
2002 f69721c3 2019-10-21 stsp }
2003 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2004 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2005 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2006 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2007 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2008 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2009 f69721c3 2019-10-21 stsp goto done;
2010 f69721c3 2019-10-21 stsp }
2011 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2012 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2013 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2014 234035bc 2019-06-01 stsp goto done;
2015 f69721c3 2019-10-21 stsp }
2016 234035bc 2019-06-01 stsp }
2017 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2018 36bf999c 2020-07-23 stsp char *link_target;
2019 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2020 36bf999c 2020-07-23 stsp if (err)
2021 36bf999c 2020-07-23 stsp goto done;
2022 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2023 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2024 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2025 36bf999c 2020-07-23 stsp free(link_target);
2026 993e2a1b 2020-07-23 stsp } else {
2027 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2028 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2029 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2030 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2031 993e2a1b 2020-07-23 stsp }
2032 f69721c3 2019-10-21 stsp free(label_orig);
2033 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2034 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2035 f4ae6ddb 2022-07-01 thomas goto done;
2036 f4ae6ddb 2022-07-01 thomas }
2037 234035bc 2019-06-01 stsp if (blob2)
2038 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2039 909d120e 2019-09-22 stsp if (err)
2040 909d120e 2019-09-22 stsp goto done;
2041 234035bc 2019-06-01 stsp /*
2042 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2043 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2044 234035bc 2019-06-01 stsp * unmodified files again.
2045 234035bc 2019-06-01 stsp */
2046 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2047 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2048 234035bc 2019-06-01 stsp update_timestamps);
2049 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2050 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2052 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2053 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2054 1ee397ad 2019-07-12 stsp if (err)
2055 1ee397ad 2019-07-12 stsp goto done;
2056 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 13d9040b 2019-03-27 stsp if (err)
2059 13d9040b 2019-03-27 stsp goto done;
2060 13d9040b 2019-03-27 stsp } else {
2061 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2062 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2063 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2064 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2065 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2066 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2067 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2068 2e1fa222 2020-07-23 stsp } else {
2069 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2070 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2071 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2072 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2073 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2074 2e1fa222 2020-07-23 stsp }
2075 13d9040b 2019-03-27 stsp if (err)
2076 13d9040b 2019-03-27 stsp goto done;
2077 65b05cec 2020-07-23 stsp
2078 054041d0 2020-06-24 stsp if (ie) {
2079 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2080 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2081 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2082 054041d0 2020-06-24 stsp } else {
2083 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2084 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2085 054041d0 2020-06-24 stsp &blob->id);
2086 054041d0 2020-06-24 stsp }
2087 13d9040b 2019-03-27 stsp if (err)
2088 13d9040b 2019-03-27 stsp goto done;
2089 65b05cec 2020-07-23 stsp
2090 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2091 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2092 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2093 2e1fa222 2020-07-23 stsp }
2094 13d9040b 2019-03-27 stsp }
2095 f4ae6ddb 2022-07-01 thomas
2096 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2097 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2098 f4ae6ddb 2022-07-01 thomas goto done;
2099 f4ae6ddb 2022-07-01 thomas }
2100 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2101 6353ad76 2019-02-08 stsp done:
2102 6353ad76 2019-02-08 stsp free(ondisk_path);
2103 8da9e5f4 2019-01-12 stsp return err;
2104 90285c3b 2019-01-08 stsp }
2105 90285c3b 2019-01-08 stsp
2106 90285c3b 2019-01-08 stsp static const struct got_error *
2107 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2108 90285c3b 2019-01-08 stsp {
2109 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2110 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2111 90285c3b 2019-01-08 stsp
2112 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2113 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2114 90285c3b 2019-01-08 stsp
2115 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2116 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2117 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2118 c97665ae 2019-01-13 stsp } else {
2119 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2120 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2121 1c4cdd89 2021-06-20 stsp if (err)
2122 1c4cdd89 2021-06-20 stsp goto done;
2123 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2124 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2125 fddefe3b 2020-10-20 stsp free(ondisk_path);
2126 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2127 1c4cdd89 2021-06-20 stsp parent = NULL;
2128 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2129 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2130 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2131 fddefe3b 2020-10-20 stsp ondisk_path);
2132 90285c3b 2019-01-08 stsp break;
2133 90285c3b 2019-01-08 stsp }
2134 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2135 1c4cdd89 2021-06-20 stsp if (err)
2136 1c4cdd89 2021-06-20 stsp break;
2137 1c4cdd89 2021-06-20 stsp }
2138 90285c3b 2019-01-08 stsp }
2139 1c4cdd89 2021-06-20 stsp done:
2140 90285c3b 2019-01-08 stsp free(ondisk_path);
2141 1c4cdd89 2021-06-20 stsp free(parent);
2142 90285c3b 2019-01-08 stsp return err;
2143 512f0d0e 2019-01-02 stsp }
2144 512f0d0e 2019-01-02 stsp
2145 708d8e67 2019-03-27 stsp static const struct got_error *
2146 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2147 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2148 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2149 708d8e67 2019-03-27 stsp {
2150 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2151 708d8e67 2019-03-27 stsp unsigned char status;
2152 708d8e67 2019-03-27 stsp struct stat sb;
2153 708d8e67 2019-03-27 stsp char *ondisk_path;
2154 708d8e67 2019-03-27 stsp
2155 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2156 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2157 a76c42e6 2019-08-03 stsp
2158 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2159 708d8e67 2019-03-27 stsp == -1)
2160 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2161 708d8e67 2019-03-27 stsp
2162 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2163 708d8e67 2019-03-27 stsp if (err)
2164 5a58a424 2020-06-23 stsp goto done;
2165 708d8e67 2019-03-27 stsp
2166 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2167 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2168 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2169 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2170 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2171 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2172 993e2a1b 2020-07-23 stsp goto done;
2173 993e2a1b 2020-07-23 stsp }
2174 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2175 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2176 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2177 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2178 993e2a1b 2020-07-23 stsp if (err)
2179 993e2a1b 2020-07-23 stsp goto done;
2180 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2181 993e2a1b 2020-07-23 stsp ie->path);
2182 993e2a1b 2020-07-23 stsp goto done;
2183 993e2a1b 2020-07-23 stsp }
2184 993e2a1b 2020-07-23 stsp
2185 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2186 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2187 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2188 1ee397ad 2019-07-12 stsp if (err)
2189 5a58a424 2020-06-23 stsp goto done;
2190 fc6346c4 2019-03-27 stsp /*
2191 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2192 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2193 fc6346c4 2019-03-27 stsp */
2194 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2195 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2196 fc6346c4 2019-03-27 stsp } else {
2197 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2198 1ee397ad 2019-07-12 stsp if (err)
2199 5a58a424 2020-06-23 stsp goto done;
2200 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2201 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2202 fc6346c4 2019-03-27 stsp if (err)
2203 5a58a424 2020-06-23 stsp goto done;
2204 fc6346c4 2019-03-27 stsp }
2205 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2206 708d8e67 2019-03-27 stsp }
2207 5a58a424 2020-06-23 stsp done:
2208 5a58a424 2020-06-23 stsp free(ondisk_path);
2209 fc6346c4 2019-03-27 stsp return err;
2210 708d8e67 2019-03-27 stsp }
2211 708d8e67 2019-03-27 stsp
2212 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2213 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2214 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2215 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2216 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2217 8da9e5f4 2019-01-12 stsp void *progress_arg;
2218 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2219 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2220 8da9e5f4 2019-01-12 stsp };
2221 8da9e5f4 2019-01-12 stsp
2222 512f0d0e 2019-01-02 stsp static const struct got_error *
2223 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2224 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2225 512f0d0e 2019-01-02 stsp {
2226 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2227 0584f854 2019-04-06 stsp
2228 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2229 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2230 512f0d0e 2019-01-02 stsp
2231 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2232 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2233 8da9e5f4 2019-01-12 stsp }
2234 512f0d0e 2019-01-02 stsp
2235 8da9e5f4 2019-01-12 stsp static const struct got_error *
2236 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 8da9e5f4 2019-01-12 stsp {
2238 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2239 7a9df742 2019-01-08 stsp
2240 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2242 0584f854 2019-04-06 stsp
2243 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2244 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2245 9d31a1d8 2018-03-11 stsp }
2246 9d31a1d8 2018-03-11 stsp
2247 9d31a1d8 2018-03-11 stsp static const struct got_error *
2248 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2249 9d31a1d8 2018-03-11 stsp {
2250 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2251 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2252 8da9e5f4 2019-01-12 stsp char *path;
2253 9d31a1d8 2018-03-11 stsp
2254 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2255 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2256 0584f854 2019-04-06 stsp
2257 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2258 63c5ca5d 2019-08-24 stsp return NULL;
2259 63c5ca5d 2019-08-24 stsp
2260 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2261 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2262 8da9e5f4 2019-01-12 stsp == -1)
2263 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2264 9d31a1d8 2018-03-11 stsp
2265 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2266 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2267 8da9e5f4 2019-01-12 stsp else
2268 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2269 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2270 9d31a1d8 2018-03-11 stsp
2271 8da9e5f4 2019-01-12 stsp free(path);
2272 0cd1c46a 2019-03-11 stsp return err;
2273 0cd1c46a 2019-03-11 stsp }
2274 0cd1c46a 2019-03-11 stsp
2275 b2118c49 2020-07-28 stsp const struct got_error *
2276 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2277 b2118c49 2020-07-28 stsp {
2278 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2279 b2118c49 2020-07-28 stsp
2280 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2281 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2282 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2283 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2284 b2118c49 2020-07-28 stsp }
2285 b2118c49 2020-07-28 stsp
2286 b2118c49 2020-07-28 stsp return NULL;
2287 b2118c49 2020-07-28 stsp }
2288 b2118c49 2020-07-28 stsp
2289 818c7501 2019-07-11 stsp static const struct got_error *
2290 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2291 0cd1c46a 2019-03-11 stsp {
2292 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2293 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2294 0cd1c46a 2019-03-11 stsp
2295 517bab73 2019-03-11 stsp *refname = NULL;
2296 517bab73 2019-03-11 stsp
2297 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2298 b2118c49 2020-07-28 stsp if (err)
2299 b2118c49 2020-07-28 stsp return err;
2300 0cd1c46a 2019-03-11 stsp
2301 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2302 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2303 0647c563 2019-03-11 stsp *refname = NULL;
2304 0cd1c46a 2019-03-11 stsp }
2305 517bab73 2019-03-11 stsp free(uuidstr);
2306 517bab73 2019-03-11 stsp return err;
2307 517bab73 2019-03-11 stsp }
2308 517bab73 2019-03-11 stsp
2309 818c7501 2019-07-11 stsp const struct got_error *
2310 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2311 f6cd0243 2023-01-28 thomas const char *prefix)
2312 f6cd0243 2023-01-28 thomas {
2313 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2314 f6cd0243 2023-01-28 thomas }
2315 f6cd0243 2023-01-28 thomas
2316 f6cd0243 2023-01-28 thomas const struct got_error *
2317 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2318 818c7501 2019-07-11 stsp {
2319 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2320 818c7501 2019-07-11 stsp }
2321 818c7501 2019-07-11 stsp
2322 818c7501 2019-07-11 stsp static const struct got_error *
2323 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2324 818c7501 2019-07-11 stsp {
2325 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2326 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2327 818c7501 2019-07-11 stsp }
2328 818c7501 2019-07-11 stsp
2329 818c7501 2019-07-11 stsp static const struct got_error *
2330 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2331 818c7501 2019-07-11 stsp {
2332 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2333 818c7501 2019-07-11 stsp }
2334 818c7501 2019-07-11 stsp
2335 818c7501 2019-07-11 stsp static const struct got_error *
2336 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2337 818c7501 2019-07-11 stsp {
2338 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2339 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2340 818c7501 2019-07-11 stsp }
2341 818c7501 2019-07-11 stsp
2342 818c7501 2019-07-11 stsp static const struct got_error *
2343 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2344 818c7501 2019-07-11 stsp {
2345 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2346 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2347 0ebf8283 2019-07-24 stsp }
2348 0ebf8283 2019-07-24 stsp
2349 0ebf8283 2019-07-24 stsp static const struct got_error *
2350 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2351 0ebf8283 2019-07-24 stsp {
2352 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2353 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2354 0ebf8283 2019-07-24 stsp }
2355 0ebf8283 2019-07-24 stsp
2356 0ebf8283 2019-07-24 stsp static const struct got_error *
2357 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2358 0ebf8283 2019-07-24 stsp {
2359 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2360 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2361 0ebf8283 2019-07-24 stsp }
2362 0ebf8283 2019-07-24 stsp
2363 0ebf8283 2019-07-24 stsp static const struct got_error *
2364 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2365 0ebf8283 2019-07-24 stsp {
2366 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2367 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2368 818c7501 2019-07-11 stsp }
2369 818c7501 2019-07-11 stsp
2370 0ebf8283 2019-07-24 stsp static const struct got_error *
2371 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2372 0ebf8283 2019-07-24 stsp {
2373 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2374 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2375 0ebf8283 2019-07-24 stsp }
2376 818c7501 2019-07-11 stsp
2377 0ebf8283 2019-07-24 stsp const struct got_error *
2378 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2379 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2380 0ebf8283 2019-07-24 stsp {
2381 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2382 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2383 0ebf8283 2019-07-24 stsp *path = NULL;
2384 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2385 0ebf8283 2019-07-24 stsp }
2386 0ebf8283 2019-07-24 stsp return NULL;
2387 10604dce 2021-09-24 thomas }
2388 10604dce 2021-09-24 thomas
2389 10604dce 2021-09-24 thomas static const struct got_error *
2390 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2391 10604dce 2021-09-24 thomas {
2392 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2393 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2394 0ebf8283 2019-07-24 stsp }
2395 0ebf8283 2019-07-24 stsp
2396 10604dce 2021-09-24 thomas static const struct got_error *
2397 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2398 10604dce 2021-09-24 thomas {
2399 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2400 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2401 10604dce 2021-09-24 thomas }
2402 10604dce 2021-09-24 thomas
2403 517bab73 2019-03-11 stsp /*
2404 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2405 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2406 517bab73 2019-03-11 stsp */
2407 517bab73 2019-03-11 stsp static const struct got_error *
2408 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2409 517bab73 2019-03-11 stsp {
2410 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2411 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2412 517bab73 2019-03-11 stsp char *refname;
2413 517bab73 2019-03-11 stsp
2414 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2415 517bab73 2019-03-11 stsp if (err)
2416 517bab73 2019-03-11 stsp return err;
2417 0cd1c46a 2019-03-11 stsp
2418 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2419 0cd1c46a 2019-03-11 stsp if (err)
2420 0cd1c46a 2019-03-11 stsp goto done;
2421 0cd1c46a 2019-03-11 stsp
2422 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2423 0cd1c46a 2019-03-11 stsp done:
2424 0cd1c46a 2019-03-11 stsp free(refname);
2425 0cd1c46a 2019-03-11 stsp if (ref)
2426 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2427 3e3a69f1 2019-07-25 stsp return err;
2428 3e3a69f1 2019-07-25 stsp }
2429 3e3a69f1 2019-07-25 stsp
2430 3e3a69f1 2019-07-25 stsp static const struct got_error *
2431 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2432 3e3a69f1 2019-07-25 stsp {
2433 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2434 3e3a69f1 2019-07-25 stsp
2435 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2436 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2437 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2438 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2439 3e3a69f1 2019-07-25 stsp }
2440 9d31a1d8 2018-03-11 stsp return err;
2441 9d31a1d8 2018-03-11 stsp }
2442 9d31a1d8 2018-03-11 stsp
2443 3e3a69f1 2019-07-25 stsp
2444 ebf99748 2019-05-09 stsp static const struct got_error *
2445 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2446 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2447 ebf99748 2019-05-09 stsp {
2448 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2449 ebf99748 2019-05-09 stsp FILE *index = NULL;
2450 0cd1c46a 2019-03-11 stsp
2451 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2452 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2453 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2454 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2455 ebf99748 2019-05-09 stsp
2456 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2457 3e3a69f1 2019-07-25 stsp if (err)
2458 ebf99748 2019-05-09 stsp goto done;
2459 ebf99748 2019-05-09 stsp
2460 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2461 ebf99748 2019-05-09 stsp if (index == NULL) {
2462 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2463 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2464 ebf99748 2019-05-09 stsp } else {
2465 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2466 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2467 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2468 ebf99748 2019-05-09 stsp }
2469 ebf99748 2019-05-09 stsp done:
2470 ebf99748 2019-05-09 stsp if (err) {
2471 ebf99748 2019-05-09 stsp free(*fileindex_path);
2472 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2473 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2474 ebf99748 2019-05-09 stsp *fileindex = NULL;
2475 ebf99748 2019-05-09 stsp }
2476 ebf99748 2019-05-09 stsp return err;
2477 ebf99748 2019-05-09 stsp }
2478 c932eeeb 2019-05-22 stsp
2479 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2480 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2481 c932eeeb 2019-05-22 stsp const char *path;
2482 c932eeeb 2019-05-22 stsp size_t path_len;
2483 c932eeeb 2019-05-22 stsp const char *entry_name;
2484 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2485 a484d721 2019-06-10 stsp void *progress_arg;
2486 c932eeeb 2019-05-22 stsp };
2487 ebf99748 2019-05-09 stsp
2488 c932eeeb 2019-05-22 stsp static const struct got_error *
2489 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2490 c932eeeb 2019-05-22 stsp {
2491 1ee397ad 2019-07-12 stsp const struct got_error *err;
2492 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2493 c932eeeb 2019-05-22 stsp
2494 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2495 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2496 c932eeeb 2019-05-22 stsp return NULL;
2497 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2498 102ff934 2019-06-11 stsp return NULL;
2499 102ff934 2019-06-11 stsp
2500 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2501 a769b60b 2021-06-27 stsp return NULL;
2502 a769b60b 2021-06-27 stsp
2503 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2504 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2505 c932eeeb 2019-05-22 stsp return NULL;
2506 c932eeeb 2019-05-22 stsp
2507 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2508 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2509 edd02c5e 2019-07-11 stsp ie->path);
2510 1ee397ad 2019-07-12 stsp if (err)
2511 1ee397ad 2019-07-12 stsp return err;
2512 1ee397ad 2019-07-12 stsp }
2513 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2514 c932eeeb 2019-05-22 stsp return NULL;
2515 a615e0e7 2020-12-16 stsp }
2516 a615e0e7 2020-12-16 stsp
2517 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2518 a615e0e7 2020-12-16 stsp static const struct got_error *
2519 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2520 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2521 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2522 a615e0e7 2020-12-16 stsp {
2523 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2524 a615e0e7 2020-12-16 stsp
2525 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2526 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2527 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2528 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2529 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2530 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2531 a615e0e7 2020-12-16 stsp
2532 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2533 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2534 9c6338c4 2019-06-02 stsp }
2535 9c6338c4 2019-06-02 stsp
2536 9c6338c4 2019-06-02 stsp static const struct got_error *
2537 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2538 9c6338c4 2019-06-02 stsp {
2539 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2540 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2541 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2542 867630bb 2020-01-17 stsp struct timespec timeout;
2543 9c6338c4 2019-06-02 stsp
2544 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2545 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2546 9c6338c4 2019-06-02 stsp if (err)
2547 9c6338c4 2019-06-02 stsp goto done;
2548 9c6338c4 2019-06-02 stsp
2549 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2550 9c6338c4 2019-06-02 stsp if (err)
2551 9c6338c4 2019-06-02 stsp goto done;
2552 9c6338c4 2019-06-02 stsp
2553 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2554 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2555 9c6338c4 2019-06-02 stsp fileindex_path);
2556 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2557 9c6338c4 2019-06-02 stsp }
2558 867630bb 2020-01-17 stsp
2559 867630bb 2020-01-17 stsp /*
2560 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2561 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2562 867630bb 2020-01-17 stsp * was recorded in the file index.
2563 867630bb 2020-01-17 stsp */
2564 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2565 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2566 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2567 9c6338c4 2019-06-02 stsp done:
2568 9c6338c4 2019-06-02 stsp if (new_index)
2569 9c6338c4 2019-06-02 stsp fclose(new_index);
2570 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2571 9c6338c4 2019-06-02 stsp return err;
2572 c932eeeb 2019-05-22 stsp }
2573 6ced4176 2019-07-12 stsp
2574 6ced4176 2019-07-12 stsp static const struct got_error *
2575 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2576 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2577 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2578 945f9229 2022-04-16 thomas struct got_repository *repo)
2579 6ced4176 2019-07-12 stsp {
2580 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2581 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2582 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2583 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2584 6ced4176 2019-07-12 stsp
2585 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2586 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2587 6ced4176 2019-07-12 stsp *tree_id = NULL;
2588 6ced4176 2019-07-12 stsp
2589 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2590 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2591 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2592 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2593 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2594 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2595 6ced4176 2019-07-12 stsp goto done;
2596 6ced4176 2019-07-12 stsp }
2597 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2598 945f9229 2022-04-16 thomas worktree->path_prefix);
2599 6ced4176 2019-07-12 stsp if (err)
2600 6ced4176 2019-07-12 stsp goto done;
2601 6ced4176 2019-07-12 stsp return NULL;
2602 6ced4176 2019-07-12 stsp }
2603 6ced4176 2019-07-12 stsp
2604 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2605 6ced4176 2019-07-12 stsp
2606 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2607 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2608 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2609 6ced4176 2019-07-12 stsp goto done;
2610 6ced4176 2019-07-12 stsp }
2611 6ced4176 2019-07-12 stsp
2612 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2613 6ced4176 2019-07-12 stsp if (err)
2614 6ced4176 2019-07-12 stsp goto done;
2615 6ced4176 2019-07-12 stsp
2616 6ced4176 2019-07-12 stsp free(in_repo_path);
2617 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2618 6ced4176 2019-07-12 stsp
2619 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2620 6ced4176 2019-07-12 stsp if (err)
2621 6ced4176 2019-07-12 stsp goto done;
2622 c932eeeb 2019-05-22 stsp
2623 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2624 6ced4176 2019-07-12 stsp /* Check out a single file. */
2625 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2626 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2627 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2628 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2629 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2630 6ced4176 2019-07-12 stsp goto done;
2631 6ced4176 2019-07-12 stsp }
2632 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2633 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2634 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2635 6ced4176 2019-07-12 stsp goto done;
2636 6ced4176 2019-07-12 stsp }
2637 6ced4176 2019-07-12 stsp } else {
2638 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2639 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2640 6ced4176 2019-07-12 stsp if (err)
2641 6ced4176 2019-07-12 stsp return err;
2642 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2643 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2644 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2645 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2646 6ced4176 2019-07-12 stsp goto done;
2647 6ced4176 2019-07-12 stsp }
2648 6ced4176 2019-07-12 stsp }
2649 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2650 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2651 6ced4176 2019-07-12 stsp } else {
2652 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2653 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2654 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2655 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2656 6ced4176 2019-07-12 stsp goto done;
2657 6ced4176 2019-07-12 stsp }
2658 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2659 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2660 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2661 6ced4176 2019-07-12 stsp goto done;
2662 6ced4176 2019-07-12 stsp }
2663 6ced4176 2019-07-12 stsp }
2664 6ced4176 2019-07-12 stsp done:
2665 6ced4176 2019-07-12 stsp free(id);
2666 6ced4176 2019-07-12 stsp free(in_repo_path);
2667 6ced4176 2019-07-12 stsp if (err) {
2668 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2669 6ced4176 2019-07-12 stsp free(*tree_relpath);
2670 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2671 6ced4176 2019-07-12 stsp free(*tree_id);
2672 6ced4176 2019-07-12 stsp *tree_id = NULL;
2673 6ced4176 2019-07-12 stsp }
2674 07ed472d 2019-07-12 stsp return err;
2675 07ed472d 2019-07-12 stsp }
2676 07ed472d 2019-07-12 stsp
2677 07ed472d 2019-07-12 stsp static const struct got_error *
2678 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2679 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2680 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2681 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2682 07ed472d 2019-07-12 stsp {
2683 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2684 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2685 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2686 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2687 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2688 07ed472d 2019-07-12 stsp
2689 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2690 7f47418f 2019-12-20 stsp if (err) {
2691 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2692 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2693 7f47418f 2019-12-20 stsp goto done;
2694 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2695 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2696 7f47418f 2019-12-20 stsp if (err)
2697 7f47418f 2019-12-20 stsp return err;
2698 7f47418f 2019-12-20 stsp }
2699 07ed472d 2019-07-12 stsp
2700 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2701 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2702 07ed472d 2019-07-12 stsp if (err)
2703 07ed472d 2019-07-12 stsp goto done;
2704 07ed472d 2019-07-12 stsp
2705 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2706 07ed472d 2019-07-12 stsp if (err)
2707 07ed472d 2019-07-12 stsp goto done;
2708 07ed472d 2019-07-12 stsp
2709 07ed472d 2019-07-12 stsp if (entry_name &&
2710 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2711 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2712 07ed472d 2019-07-12 stsp goto done;
2713 07ed472d 2019-07-12 stsp }
2714 07ed472d 2019-07-12 stsp
2715 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2716 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2717 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2718 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2719 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2720 07ed472d 2019-07-12 stsp arg.repo = repo;
2721 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2722 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2723 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2724 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2725 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2726 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2727 07ed472d 2019-07-12 stsp done:
2728 07ed472d 2019-07-12 stsp if (tree)
2729 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2730 07ed472d 2019-07-12 stsp if (commit)
2731 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2732 6ced4176 2019-07-12 stsp return err;
2733 6ced4176 2019-07-12 stsp }
2734 6ced4176 2019-07-12 stsp
2735 9d31a1d8 2018-03-11 stsp const struct got_error *
2736 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2737 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2738 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2739 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2740 9d31a1d8 2018-03-11 stsp {
2741 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2742 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2743 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2744 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2745 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2746 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2747 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2748 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2749 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2750 f2ea84fa 2019-07-27 stsp int entry_type;
2751 f2ea84fa 2019-07-27 stsp char *relpath;
2752 f2ea84fa 2019-07-27 stsp char *entry_name;
2753 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2754 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2755 9d31a1d8 2018-03-11 stsp
2756 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2757 f2ea84fa 2019-07-27 stsp
2758 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2759 9d31a1d8 2018-03-11 stsp if (err)
2760 9d31a1d8 2018-03-11 stsp return err;
2761 945f9229 2022-04-16 thomas
2762 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2763 945f9229 2022-04-16 thomas worktree->base_commit_id);
2764 945f9229 2022-04-16 thomas if (err)
2765 945f9229 2022-04-16 thomas goto done;
2766 93a30277 2018-12-24 stsp
2767 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2768 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2769 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2770 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2771 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2772 f2ea84fa 2019-07-27 stsp goto done;
2773 f2ea84fa 2019-07-27 stsp }
2774 6ced4176 2019-07-12 stsp
2775 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2776 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2777 945f9229 2022-04-16 thomas worktree, repo);
2778 f2ea84fa 2019-07-27 stsp if (err) {
2779 f2ea84fa 2019-07-27 stsp free(tpd);
2780 6ced4176 2019-07-12 stsp goto done;
2781 6ced4176 2019-07-12 stsp }
2782 f2ea84fa 2019-07-27 stsp
2783 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2785 f2ea84fa 2019-07-27 stsp if (err) {
2786 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2787 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2788 f2ea84fa 2019-07-27 stsp free(tpd);
2789 f2ea84fa 2019-07-27 stsp goto done;
2790 f2ea84fa 2019-07-27 stsp }
2791 f2ea84fa 2019-07-27 stsp } else
2792 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2793 f2ea84fa 2019-07-27 stsp
2794 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2795 6ced4176 2019-07-12 stsp }
2796 6ced4176 2019-07-12 stsp
2797 51514078 2018-12-25 stsp /*
2798 51514078 2018-12-25 stsp * Read the file index.
2799 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2800 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2801 51514078 2018-12-25 stsp */
2802 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 8da9e5f4 2019-01-12 stsp if (err)
2804 c4cdcb68 2019-04-03 stsp goto done;
2805 c4cdcb68 2019-04-03 stsp
2806 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2808 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2809 f2ea84fa 2019-07-27 stsp
2810 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2811 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2812 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 f2ea84fa 2019-07-27 stsp if (err)
2814 f2ea84fa 2019-07-27 stsp break;
2815 f2ea84fa 2019-07-27 stsp
2816 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2817 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2818 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2819 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2820 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2821 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2822 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2823 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2824 f2ea84fa 2019-07-27 stsp if (err)
2825 f2ea84fa 2019-07-27 stsp break;
2826 f2ea84fa 2019-07-27 stsp
2827 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2828 711d9cd9 2019-07-12 stsp }
2829 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2830 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2831 af12c6b9 2019-06-04 stsp err = sync_err;
2832 9d31a1d8 2018-03-11 stsp done:
2833 9c6338c4 2019-06-02 stsp free(fileindex_path);
2834 edfa7d7f 2018-09-11 stsp if (tree)
2835 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2836 9d31a1d8 2018-03-11 stsp if (commit)
2837 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2838 5ade8233 2019-07-12 stsp if (fileindex)
2839 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2840 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2841 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2842 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2844 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2845 f2ea84fa 2019-07-27 stsp free(tpd);
2846 f2ea84fa 2019-07-27 stsp }
2847 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2848 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2849 9d31a1d8 2018-03-11 stsp err = unlockerr;
2850 f8d1f275 2019-02-04 stsp return err;
2851 f8d1f275 2019-02-04 stsp }
2852 f8d1f275 2019-02-04 stsp
2853 0529f8df 2023-03-10 thomas static const struct got_error *
2854 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2855 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2856 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2857 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2858 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2859 0529f8df 2023-03-10 thomas struct got_repository *repo,
2860 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2861 0529f8df 2023-03-10 thomas {
2862 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2863 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2864 0529f8df 2023-03-10 thomas
2865 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2866 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2867 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2868 0529f8df 2023-03-10 thomas restoring_missing_file,
2869 0529f8df 2023-03-10 thomas reverting_versioned_file,
2870 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2871 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2872 0529f8df 2023-03-10 thomas } else {
2873 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2874 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2875 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2876 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2877 0529f8df 2023-03-10 thomas }
2878 0529f8df 2023-03-10 thomas if (err)
2879 0529f8df 2023-03-10 thomas return err;
2880 0529f8df 2023-03-10 thomas if (ie == NULL) {
2881 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2882 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2883 0529f8df 2023-03-10 thomas if (err)
2884 0529f8df 2023-03-10 thomas return err;
2885 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2886 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2887 0529f8df 2023-03-10 thomas if (err) {
2888 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2889 0529f8df 2023-03-10 thomas return err;
2890 0529f8df 2023-03-10 thomas }
2891 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2892 0529f8df 2023-03-10 thomas if (err) {
2893 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2894 0529f8df 2023-03-10 thomas return err;
2895 0529f8df 2023-03-10 thomas }
2896 0529f8df 2023-03-10 thomas } else {
2897 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2898 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2899 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2900 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2901 0529f8df 2023-03-10 thomas if (err)
2902 0529f8df 2023-03-10 thomas return err;
2903 0529f8df 2023-03-10 thomas }
2904 0529f8df 2023-03-10 thomas
2905 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2906 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2907 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2908 0529f8df 2023-03-10 thomas }
2909 0529f8df 2023-03-10 thomas
2910 0529f8df 2023-03-10 thomas return NULL;
2911 0529f8df 2023-03-10 thomas }
2912 0529f8df 2023-03-10 thomas
2913 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2914 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2915 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2916 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2917 234035bc 2019-06-01 stsp void *progress_arg;
2918 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2919 234035bc 2019-06-01 stsp void *cancel_arg;
2920 f69721c3 2019-10-21 stsp const char *label_orig;
2921 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2922 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2923 234035bc 2019-06-01 stsp };
2924 234035bc 2019-06-01 stsp
2925 234035bc 2019-06-01 stsp static const struct got_error *
2926 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2927 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2928 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2929 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2930 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2931 234035bc 2019-06-01 stsp {
2932 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2933 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2934 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2935 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2936 234035bc 2019-06-01 stsp struct stat sb;
2937 234035bc 2019-06-01 stsp unsigned char status;
2938 234035bc 2019-06-01 stsp int local_changes_subsumed;
2939 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2940 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2941 234035bc 2019-06-01 stsp
2942 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2943 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2944 d6c87207 2019-08-02 stsp strlen(path2));
2945 1ee397ad 2019-07-12 stsp if (ie == NULL)
2946 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2947 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2948 234035bc 2019-06-01 stsp
2949 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2950 234035bc 2019-06-01 stsp path2) == -1)
2951 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2952 234035bc 2019-06-01 stsp
2953 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2954 7f91a133 2019-12-13 stsp repo);
2955 234035bc 2019-06-01 stsp if (err)
2956 234035bc 2019-06-01 stsp goto done;
2957 234035bc 2019-06-01 stsp
2958 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2959 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2960 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2961 234035bc 2019-06-01 stsp goto done;
2962 234035bc 2019-06-01 stsp }
2963 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2964 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2965 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2966 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2967 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2968 234035bc 2019-06-01 stsp goto done;
2969 234035bc 2019-06-01 stsp }
2970 234035bc 2019-06-01 stsp
2971 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2972 36bf999c 2020-07-23 stsp char *link_target2;
2973 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2974 36bf999c 2020-07-23 stsp if (err)
2975 36bf999c 2020-07-23 stsp goto done;
2976 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2977 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2978 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2979 36bf999c 2020-07-23 stsp free(link_target2);
2980 af57b12a 2020-07-23 stsp } else {
2981 1af628f4 2021-06-03 stsp int fd;
2982 1af628f4 2021-06-03 stsp
2983 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2984 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2985 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2986 1af628f4 2021-06-03 stsp goto done;
2987 1af628f4 2021-06-03 stsp }
2988 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2989 1af628f4 2021-06-03 stsp f_orig, blob1);
2990 1af628f4 2021-06-03 stsp if (err)
2991 1af628f4 2021-06-03 stsp goto done;
2992 1af628f4 2021-06-03 stsp
2993 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2994 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2995 1af628f4 2021-06-03 stsp goto done;
2996 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2997 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2998 1af628f4 2021-06-03 stsp if (err)
2999 1af628f4 2021-06-03 stsp goto done;
3000 1af628f4 2021-06-03 stsp
3001 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3002 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3003 1af628f4 2021-06-03 stsp if (fd == -1) {
3004 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3005 1af628f4 2021-06-03 stsp ondisk_path);
3006 1af628f4 2021-06-03 stsp goto done;
3007 1af628f4 2021-06-03 stsp }
3008 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3009 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3010 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3011 1af628f4 2021-06-03 stsp ondisk_path);
3012 1af628f4 2021-06-03 stsp close(fd);
3013 1af628f4 2021-06-03 stsp goto done;
3014 1af628f4 2021-06-03 stsp }
3015 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3016 1af628f4 2021-06-03 stsp if (err)
3017 1af628f4 2021-06-03 stsp goto done;
3018 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3019 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3020 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3021 1af628f4 2021-06-03 stsp goto done;
3022 1af628f4 2021-06-03 stsp }
3023 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3024 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3025 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3026 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3027 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3028 af57b12a 2020-07-23 stsp }
3029 234035bc 2019-06-01 stsp } else if (blob1) {
3030 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3031 d6c87207 2019-08-02 stsp strlen(path1));
3032 1ee397ad 2019-07-12 stsp if (ie == NULL)
3033 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3034 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3035 2b92fad7 2019-06-02 stsp
3036 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3037 2b92fad7 2019-06-02 stsp path1) == -1)
3038 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3039 2b92fad7 2019-06-02 stsp
3040 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3041 7f91a133 2019-12-13 stsp repo);
3042 2b92fad7 2019-06-02 stsp if (err)
3043 2b92fad7 2019-06-02 stsp goto done;
3044 2b92fad7 2019-06-02 stsp
3045 2b92fad7 2019-06-02 stsp switch (status) {
3046 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3047 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3048 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3049 1ee397ad 2019-07-12 stsp if (err)
3050 1ee397ad 2019-07-12 stsp goto done;
3051 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3052 2b92fad7 2019-06-02 stsp if (err)
3053 2b92fad7 2019-06-02 stsp goto done;
3054 2b92fad7 2019-06-02 stsp if (ie)
3055 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3056 2b92fad7 2019-06-02 stsp break;
3057 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3058 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3059 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3060 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3061 1ee397ad 2019-07-12 stsp if (err)
3062 1ee397ad 2019-07-12 stsp goto done;
3063 2b92fad7 2019-06-02 stsp if (ie)
3064 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3065 2b92fad7 2019-06-02 stsp break;
3066 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3067 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3068 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3069 e8f02263 2022-01-23 thomas off_t blob1_size;
3070 0a22ca1a 2020-09-23 stsp /*
3071 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3072 0a22ca1a 2020-09-23 stsp * exists in the repository.
3073 0a22ca1a 2020-09-23 stsp */
3074 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3075 e8f02263 2022-01-23 thomas &blob1_size, path1);
3076 0a22ca1a 2020-09-23 stsp if (err)
3077 0a22ca1a 2020-09-23 stsp goto done;
3078 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3079 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3080 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3081 0a22ca1a 2020-09-23 stsp if (err)
3082 0a22ca1a 2020-09-23 stsp goto done;
3083 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3084 0a22ca1a 2020-09-23 stsp path1);
3085 0a22ca1a 2020-09-23 stsp if (err)
3086 0a22ca1a 2020-09-23 stsp goto done;
3087 0a22ca1a 2020-09-23 stsp if (ie)
3088 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3089 0a22ca1a 2020-09-23 stsp ie);
3090 0a22ca1a 2020-09-23 stsp } else {
3091 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3092 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3093 0a22ca1a 2020-09-23 stsp }
3094 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3095 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3096 0a22ca1a 2020-09-23 stsp free(id);
3097 0a22ca1a 2020-09-23 stsp if (err)
3098 0a22ca1a 2020-09-23 stsp goto done;
3099 0a22ca1a 2020-09-23 stsp break;
3100 0a22ca1a 2020-09-23 stsp }
3101 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3102 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3103 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3104 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3105 1ee397ad 2019-07-12 stsp if (err)
3106 1ee397ad 2019-07-12 stsp goto done;
3107 2b92fad7 2019-06-02 stsp break;
3108 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3109 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3110 1ee397ad 2019-07-12 stsp if (err)
3111 1ee397ad 2019-07-12 stsp goto done;
3112 2b92fad7 2019-06-02 stsp break;
3113 2b92fad7 2019-06-02 stsp default:
3114 2b92fad7 2019-06-02 stsp break;
3115 2b92fad7 2019-06-02 stsp }
3116 234035bc 2019-06-01 stsp } else if (blob2) {
3117 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3118 234035bc 2019-06-01 stsp path2) == -1)
3119 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3120 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3121 d6c87207 2019-08-02 stsp strlen(path2));
3122 234035bc 2019-06-01 stsp if (ie) {
3123 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3124 7f91a133 2019-12-13 stsp -1, NULL, repo);
3125 234035bc 2019-06-01 stsp if (err)
3126 234035bc 2019-06-01 stsp goto done;
3127 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3128 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3129 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3130 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3131 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3132 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3133 1ee397ad 2019-07-12 stsp status, path2);
3134 234035bc 2019-06-01 stsp goto done;
3135 234035bc 2019-06-01 stsp }
3136 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3137 36bf999c 2020-07-23 stsp char *link_target2;
3138 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3139 36bf999c 2020-07-23 stsp blob2);
3140 36bf999c 2020-07-23 stsp if (err)
3141 36bf999c 2020-07-23 stsp goto done;
3142 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3143 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3144 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3145 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3146 36bf999c 2020-07-23 stsp free(link_target2);
3147 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3148 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3149 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3150 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3151 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3152 526a746f 2020-07-23 stsp a->progress_arg);
3153 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3154 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3155 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3156 526a746f 2020-07-23 stsp }
3157 dfe9fba0 2020-07-23 stsp if (err)
3158 dfe9fba0 2020-07-23 stsp goto done;
3159 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3160 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3161 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3162 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3163 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3164 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3165 234035bc 2019-06-01 stsp if (err)
3166 234035bc 2019-06-01 stsp goto done;
3167 234035bc 2019-06-01 stsp }
3168 234035bc 2019-06-01 stsp } else {
3169 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3170 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3171 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3172 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3173 234035bc 2019-06-01 stsp if (err)
3174 2b92fad7 2019-06-02 stsp goto done;
3175 234035bc 2019-06-01 stsp }
3176 234035bc 2019-06-01 stsp }
3177 234035bc 2019-06-01 stsp done:
3178 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3179 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3180 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3181 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3182 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3183 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3184 1af628f4 2021-06-03 stsp free(id_str);
3185 54d5be07 2021-06-03 stsp free(label_deriv2);
3186 234035bc 2019-06-01 stsp free(ondisk_path);
3187 234035bc 2019-06-01 stsp return err;
3188 234035bc 2019-06-01 stsp }
3189 234035bc 2019-06-01 stsp
3190 69de9dd4 2021-09-03 stsp static const struct got_error *
3191 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3192 69de9dd4 2021-09-03 stsp {
3193 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3194 69de9dd4 2021-09-03 stsp
3195 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3196 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3197 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3198 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3199 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3200 69de9dd4 2021-09-03 stsp
3201 69de9dd4 2021-09-03 stsp return NULL;
3202 69de9dd4 2021-09-03 stsp }
3203 69de9dd4 2021-09-03 stsp
3204 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3205 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3206 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3207 234035bc 2019-06-01 stsp struct got_repository *repo;
3208 234035bc 2019-06-01 stsp };
3209 234035bc 2019-06-01 stsp
3210 234035bc 2019-06-01 stsp static const struct got_error *
3211 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3212 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3213 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3214 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3215 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3216 234035bc 2019-06-01 stsp {
3217 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3218 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3219 234035bc 2019-06-01 stsp unsigned char status;
3220 234035bc 2019-06-01 stsp struct stat sb;
3221 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3222 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3223 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3224 234035bc 2019-06-01 stsp char *ondisk_path;
3225 234035bc 2019-06-01 stsp
3226 69de9dd4 2021-09-03 stsp if (id == NULL)
3227 69de9dd4 2021-09-03 stsp return NULL;
3228 234035bc 2019-06-01 stsp
3229 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3230 69de9dd4 2021-09-03 stsp if (ie == NULL)
3231 69de9dd4 2021-09-03 stsp return NULL;
3232 69de9dd4 2021-09-03 stsp
3233 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3234 234035bc 2019-06-01 stsp == -1)
3235 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3236 234035bc 2019-06-01 stsp
3237 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3238 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3239 5546d466 2021-09-02 stsp free(ondisk_path);
3240 234035bc 2019-06-01 stsp if (err)
3241 234035bc 2019-06-01 stsp return err;
3242 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3243 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3244 234035bc 2019-06-01 stsp
3245 234035bc 2019-06-01 stsp return NULL;
3246 234035bc 2019-06-01 stsp }
3247 234035bc 2019-06-01 stsp
3248 818c7501 2019-07-11 stsp static const struct got_error *
3249 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3250 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3251 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3252 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3253 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3254 234035bc 2019-06-01 stsp {
3255 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3256 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3257 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3258 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3259 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3260 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3261 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3262 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3263 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3264 234035bc 2019-06-01 stsp
3265 03415a1a 2019-06-02 stsp if (commit_id1) {
3266 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3267 945f9229 2022-04-16 thomas if (err)
3268 945f9229 2022-04-16 thomas goto done;
3269 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3270 03415a1a 2019-06-02 stsp worktree->path_prefix);
3271 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3272 03415a1a 2019-06-02 stsp goto done;
3273 69d57f3d 2020-07-31 stsp }
3274 69d57f3d 2020-07-31 stsp if (tree_id1) {
3275 69d57f3d 2020-07-31 stsp char *id_str;
3276 03415a1a 2019-06-02 stsp
3277 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3278 03415a1a 2019-06-02 stsp if (err)
3279 03415a1a 2019-06-02 stsp goto done;
3280 f69721c3 2019-10-21 stsp
3281 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3282 f69721c3 2019-10-21 stsp if (err)
3283 f69721c3 2019-10-21 stsp goto done;
3284 f69721c3 2019-10-21 stsp
3285 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3286 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3287 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3288 f69721c3 2019-10-21 stsp free(id_str);
3289 f69721c3 2019-10-21 stsp goto done;
3290 f69721c3 2019-10-21 stsp }
3291 f69721c3 2019-10-21 stsp free(id_str);
3292 a0f32f33 2022-06-13 thomas
3293 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3294 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3295 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3296 a0f32f33 2022-06-13 thomas goto done;
3297 a0f32f33 2022-06-13 thomas }
3298 03415a1a 2019-06-02 stsp }
3299 234035bc 2019-06-01 stsp
3300 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3301 945f9229 2022-04-16 thomas if (err)
3302 945f9229 2022-04-16 thomas goto done;
3303 945f9229 2022-04-16 thomas
3304 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3305 234035bc 2019-06-01 stsp worktree->path_prefix);
3306 234035bc 2019-06-01 stsp if (err)
3307 234035bc 2019-06-01 stsp goto done;
3308 234035bc 2019-06-01 stsp
3309 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3310 234035bc 2019-06-01 stsp if (err)
3311 234035bc 2019-06-01 stsp goto done;
3312 234035bc 2019-06-01 stsp
3313 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3314 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3315 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3316 19a6a6b5 2022-07-01 thomas goto done;
3317 19a6a6b5 2022-07-01 thomas }
3318 19a6a6b5 2022-07-01 thomas
3319 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3320 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3321 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3322 a0f32f33 2022-06-13 thomas goto done;
3323 a0f32f33 2022-06-13 thomas }
3324 a0f32f33 2022-06-13 thomas
3325 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3326 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3327 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3328 19a6a6b5 2022-07-01 thomas goto done;
3329 19a6a6b5 2022-07-01 thomas }
3330 19a6a6b5 2022-07-01 thomas
3331 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3332 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3333 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3334 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3335 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3336 69de9dd4 2021-09-03 stsp if (err)
3337 69de9dd4 2021-09-03 stsp goto done;
3338 69de9dd4 2021-09-03 stsp
3339 234035bc 2019-06-01 stsp arg.worktree = worktree;
3340 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3341 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3342 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3343 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3344 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3345 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3346 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3347 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3348 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3349 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3350 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3351 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3352 af12c6b9 2019-06-04 stsp err = sync_err;
3353 234035bc 2019-06-01 stsp done:
3354 945f9229 2022-04-16 thomas if (commit1)
3355 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3356 945f9229 2022-04-16 thomas if (commit2)
3357 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3358 234035bc 2019-06-01 stsp if (tree1)
3359 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3360 234035bc 2019-06-01 stsp if (tree2)
3361 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3362 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3363 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3364 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3365 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3366 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3367 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3368 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3369 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3370 f69721c3 2019-10-21 stsp free(label_orig);
3371 818c7501 2019-07-11 stsp return err;
3372 818c7501 2019-07-11 stsp }
3373 234035bc 2019-06-01 stsp
3374 818c7501 2019-07-11 stsp const struct got_error *
3375 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3376 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3377 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3378 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3379 818c7501 2019-07-11 stsp {
3380 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3381 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3382 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3383 818c7501 2019-07-11 stsp
3384 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3385 818c7501 2019-07-11 stsp if (err)
3386 818c7501 2019-07-11 stsp return err;
3387 818c7501 2019-07-11 stsp
3388 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3389 818c7501 2019-07-11 stsp if (err)
3390 818c7501 2019-07-11 stsp goto done;
3391 818c7501 2019-07-11 stsp
3392 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3393 69de9dd4 2021-09-03 stsp worktree);
3394 818c7501 2019-07-11 stsp if (err)
3395 818c7501 2019-07-11 stsp goto done;
3396 818c7501 2019-07-11 stsp
3397 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3398 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3399 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3400 818c7501 2019-07-11 stsp done:
3401 818c7501 2019-07-11 stsp if (fileindex)
3402 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3403 818c7501 2019-07-11 stsp free(fileindex_path);
3404 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3405 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3406 234035bc 2019-06-01 stsp err = unlockerr;
3407 234035bc 2019-06-01 stsp return err;
3408 234035bc 2019-06-01 stsp }
3409 234035bc 2019-06-01 stsp
3410 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3411 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3412 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3413 927df6b7 2019-02-10 stsp const char *status_path;
3414 927df6b7 2019-02-10 stsp size_t status_path_len;
3415 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3416 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3417 f8d1f275 2019-02-04 stsp void *status_arg;
3418 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3419 f8d1f275 2019-02-04 stsp void *cancel_arg;
3420 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3421 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3422 f2a9dc41 2019-12-13 tracey int report_unchanged;
3423 3143d852 2020-06-25 stsp int no_ignores;
3424 f8d1f275 2019-02-04 stsp };
3425 88d0e355 2019-08-03 stsp
3426 f8d1f275 2019-02-04 stsp static const struct got_error *
3427 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3428 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3429 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3430 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3431 927df6b7 2019-02-10 stsp {
3432 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3433 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3434 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3435 927df6b7 2019-02-10 stsp struct stat sb;
3436 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3437 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3438 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3439 927df6b7 2019-02-10 stsp
3440 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3441 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3442 98eaaa12 2019-08-03 stsp if (err)
3443 98eaaa12 2019-08-03 stsp return err;
3444 98eaaa12 2019-08-03 stsp
3445 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3446 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3447 98eaaa12 2019-08-03 stsp return NULL;
3448 98eaaa12 2019-08-03 stsp
3449 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3450 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3451 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3452 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3453 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3454 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3455 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3456 43010591 2023-02-17 thomas &staged_blob_id, ie);
3457 98eaaa12 2019-08-03 stsp }
3458 98eaaa12 2019-08-03 stsp
3459 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3460 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3461 927df6b7 2019-02-10 stsp }
3462 927df6b7 2019-02-10 stsp
3463 927df6b7 2019-02-10 stsp static const struct got_error *
3464 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3465 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3466 f8d1f275 2019-02-04 stsp {
3467 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3468 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3469 f8d1f275 2019-02-04 stsp char *abspath;
3470 f8d1f275 2019-02-04 stsp
3471 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3472 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3473 0584f854 2019-04-06 stsp
3474 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3475 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3476 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3477 927df6b7 2019-02-10 stsp return NULL;
3478 927df6b7 2019-02-10 stsp
3479 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3480 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3481 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3482 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3483 f8d1f275 2019-02-04 stsp } else {
3484 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3485 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3486 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3487 f8d1f275 2019-02-04 stsp }
3488 f8d1f275 2019-02-04 stsp
3489 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3490 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3491 f8d1f275 2019-02-04 stsp free(abspath);
3492 9d31a1d8 2018-03-11 stsp return err;
3493 9d31a1d8 2018-03-11 stsp }
3494 f8d1f275 2019-02-04 stsp
3495 f8d1f275 2019-02-04 stsp static const struct got_error *
3496 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3497 f8d1f275 2019-02-04 stsp {
3498 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3499 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3500 2ec1f75b 2019-03-26 stsp unsigned char status;
3501 927df6b7 2019-02-10 stsp
3502 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3503 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3504 0584f854 2019-04-06 stsp
3505 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3506 927df6b7 2019-02-10 stsp return NULL;
3507 927df6b7 2019-02-10 stsp
3508 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3509 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3510 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3511 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3512 2ec1f75b 2019-03-26 stsp else
3513 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3514 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3515 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3516 6841da00 2019-08-08 stsp }
3517 6841da00 2019-08-08 stsp
3518 ef20f542 2022-06-26 thomas static void
3519 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3520 6841da00 2019-08-08 stsp {
3521 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3522 6841da00 2019-08-08 stsp
3523 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3524 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3525 21c2d8be 2023-01-10 thomas
3526 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3527 6841da00 2019-08-08 stsp }
3528 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3529 6841da00 2019-08-08 stsp }
3530 6841da00 2019-08-08 stsp
3531 6841da00 2019-08-08 stsp static const struct got_error *
3532 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3533 6841da00 2019-08-08 stsp {
3534 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3535 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3536 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3537 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3538 6841da00 2019-08-08 stsp size_t linesize = 0;
3539 6841da00 2019-08-08 stsp ssize_t linelen;
3540 6841da00 2019-08-08 stsp
3541 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3542 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3543 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3544 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3545 6841da00 2019-08-08 stsp
3546 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3547 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3548 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3549 bd8de430 2019-10-04 stsp
3550 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3551 bd8de430 2019-10-04 stsp if (line[0] == '#')
3552 bd8de430 2019-10-04 stsp continue;
3553 bd8de430 2019-10-04 stsp
3554 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3555 bd8de430 2019-10-04 stsp if (line[0] == '!')
3556 bd8de430 2019-10-04 stsp continue;
3557 bd8de430 2019-10-04 stsp
3558 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3559 6841da00 2019-08-08 stsp line) == -1) {
3560 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3561 6841da00 2019-08-08 stsp goto done;
3562 6841da00 2019-08-08 stsp }
3563 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3564 6841da00 2019-08-08 stsp if (err)
3565 6841da00 2019-08-08 stsp goto done;
3566 6841da00 2019-08-08 stsp }
3567 6841da00 2019-08-08 stsp if (ferror(f)) {
3568 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3569 6841da00 2019-08-08 stsp goto done;
3570 6841da00 2019-08-08 stsp }
3571 6841da00 2019-08-08 stsp
3572 6841da00 2019-08-08 stsp dirpath = strdup(path);
3573 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3574 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3575 6841da00 2019-08-08 stsp goto done;
3576 6841da00 2019-08-08 stsp }
3577 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3578 6841da00 2019-08-08 stsp done:
3579 6841da00 2019-08-08 stsp free(line);
3580 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3581 6841da00 2019-08-08 stsp free(dirpath);
3582 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3583 6841da00 2019-08-08 stsp }
3584 6841da00 2019-08-08 stsp return err;
3585 1b5d300f 2023-02-20 thomas }
3586 1b5d300f 2023-02-20 thomas
3587 1b5d300f 2023-02-20 thomas static int
3588 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3589 1b5d300f 2023-02-20 thomas int flags)
3590 1b5d300f 2023-02-20 thomas {
3591 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3592 1b5d300f 2023-02-20 thomas
3593 1b5d300f 2023-02-20 thomas /*
3594 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3595 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3596 1b5d300f 2023-02-20 thomas */
3597 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3598 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3599 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3600 1b5d300f 2023-02-20 thomas
3601 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3602 1b5d300f 2023-02-20 thomas }
3603 1b5d300f 2023-02-20 thomas
3604 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3605 6841da00 2019-08-08 stsp }
3606 6841da00 2019-08-08 stsp
3607 ef20f542 2022-06-26 thomas static int
3608 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3609 6841da00 2019-08-08 stsp {
3610 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3611 bd8de430 2019-10-04 stsp
3612 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3613 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3614 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3615 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3616 bd8de430 2019-10-04 stsp
3617 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3618 1b5d300f 2023-02-20 thomas const char *p;
3619 6841da00 2019-08-08 stsp
3620 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3621 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3622 bd8de430 2019-10-04 stsp continue;
3623 bd8de430 2019-10-04 stsp p = path;
3624 bd8de430 2019-10-04 stsp while (*p) {
3625 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3626 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3627 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3628 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3629 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3630 bd8de430 2019-10-04 stsp p++;
3631 bd8de430 2019-10-04 stsp while (*p == '/')
3632 bd8de430 2019-10-04 stsp p++;
3633 bd8de430 2019-10-04 stsp continue;
3634 bd8de430 2019-10-04 stsp }
3635 bd8de430 2019-10-04 stsp return 1;
3636 bd8de430 2019-10-04 stsp }
3637 bd8de430 2019-10-04 stsp }
3638 bd8de430 2019-10-04 stsp }
3639 bd8de430 2019-10-04 stsp
3640 6841da00 2019-08-08 stsp /*
3641 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3642 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3643 6841da00 2019-08-08 stsp * ignores backwards.
3644 6841da00 2019-08-08 stsp */
3645 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3646 6841da00 2019-08-08 stsp while (pe) {
3647 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3648 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3649 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3650 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3651 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3652 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3653 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3654 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3655 1b5d300f 2023-02-20 thomas path, flags))
3656 6841da00 2019-08-08 stsp continue;
3657 6841da00 2019-08-08 stsp return 1;
3658 6841da00 2019-08-08 stsp }
3659 6841da00 2019-08-08 stsp }
3660 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3661 6841da00 2019-08-08 stsp }
3662 6841da00 2019-08-08 stsp
3663 6841da00 2019-08-08 stsp return 0;
3664 6841da00 2019-08-08 stsp }
3665 6841da00 2019-08-08 stsp
3666 6841da00 2019-08-08 stsp static const struct got_error *
3667 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3668 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3669 6841da00 2019-08-08 stsp {
3670 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3671 6841da00 2019-08-08 stsp char *ignorespath;
3672 886cec17 2019-12-15 stsp int fd = -1;
3673 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3674 6841da00 2019-08-08 stsp
3675 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3676 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3677 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3678 6841da00 2019-08-08 stsp
3679 886cec17 2019-12-15 stsp if (dirfd != -1) {
3680 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3681 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3682 886cec17 2019-12-15 stsp if (fd == -1) {
3683 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3684 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3685 886cec17 2019-12-15 stsp ignorespath);
3686 886cec17 2019-12-15 stsp } else {
3687 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3688 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3689 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3690 886cec17 2019-12-15 stsp ignorespath);
3691 886cec17 2019-12-15 stsp else {
3692 886cec17 2019-12-15 stsp fd = -1;
3693 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3694 886cec17 2019-12-15 stsp }
3695 886cec17 2019-12-15 stsp }
3696 886cec17 2019-12-15 stsp } else {
3697 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3698 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3699 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3700 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3701 886cec17 2019-12-15 stsp ignorespath);
3702 886cec17 2019-12-15 stsp } else
3703 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3704 886cec17 2019-12-15 stsp }
3705 6841da00 2019-08-08 stsp
3706 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3707 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3708 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3709 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3710 6841da00 2019-08-08 stsp free(ignorespath);
3711 6841da00 2019-08-08 stsp return err;
3712 f8d1f275 2019-02-04 stsp }
3713 f8d1f275 2019-02-04 stsp
3714 f8d1f275 2019-02-04 stsp static const struct got_error *
3715 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3716 6092c299 2021-10-04 thomas int dirfd)
3717 f8d1f275 2019-02-04 stsp {
3718 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3719 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3720 f8d1f275 2019-02-04 stsp char *path = NULL;
3721 6092c299 2021-10-04 thomas
3722 6092c299 2021-10-04 thomas if (ignore != NULL)
3723 6092c299 2021-10-04 thomas *ignore = 0;
3724 f8d1f275 2019-02-04 stsp
3725 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3726 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3727 0584f854 2019-04-06 stsp
3728 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3729 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3730 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3731 f8d1f275 2019-02-04 stsp } else {
3732 f8d1f275 2019-02-04 stsp path = de->d_name;
3733 f8d1f275 2019-02-04 stsp }
3734 f8d1f275 2019-02-04 stsp
3735 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3736 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3737 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3738 6092c299 2021-10-04 thomas *ignore = 1;
3739 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3740 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3741 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3742 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3743 f8d1f275 2019-02-04 stsp if (parent_path[0])
3744 f8d1f275 2019-02-04 stsp free(path);
3745 b72f483a 2019-02-05 stsp return err;
3746 f8d1f275 2019-02-04 stsp }
3747 f8d1f275 2019-02-04 stsp
3748 347d1d3e 2019-07-12 stsp static const struct got_error *
3749 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3750 3143d852 2020-06-25 stsp {
3751 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3752 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3753 3143d852 2020-06-25 stsp
3754 3143d852 2020-06-25 stsp if (a->no_ignores)
3755 3143d852 2020-06-25 stsp return NULL;
3756 3143d852 2020-06-25 stsp
3757 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3758 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3759 3143d852 2020-06-25 stsp if (err)
3760 3143d852 2020-06-25 stsp return err;
3761 3143d852 2020-06-25 stsp
3762 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3763 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3764 3143d852 2020-06-25 stsp
3765 3143d852 2020-06-25 stsp return err;
3766 3143d852 2020-06-25 stsp }
3767 3143d852 2020-06-25 stsp
3768 3143d852 2020-06-25 stsp static const struct got_error *
3769 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3770 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3771 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3772 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3773 abb4604f 2019-07-27 stsp {
3774 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3775 abb4604f 2019-07-27 stsp struct stat sb;
3776 ff56836b 2021-07-08 stsp
3777 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3778 abb4604f 2019-07-27 stsp if (ie)
3779 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3780 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3781 abb4604f 2019-07-27 stsp
3782 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3783 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3784 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3785 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3786 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3787 abb4604f 2019-07-27 stsp }
3788 abb4604f 2019-07-27 stsp
3789 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3790 fe1d8685 2022-02-12 thomas return NULL;
3791 fe1d8685 2022-02-12 thomas
3792 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3793 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3794 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3795 abb4604f 2019-07-27 stsp
3796 abb4604f 2019-07-27 stsp return NULL;
3797 3143d852 2020-06-25 stsp }
3798 3143d852 2020-06-25 stsp
3799 3143d852 2020-06-25 stsp static const struct got_error *
3800 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3801 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3802 3143d852 2020-06-25 stsp {
3803 3143d852 2020-06-25 stsp const struct got_error *err;
3804 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3805 3143d852 2020-06-25 stsp
3806 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3807 3143d852 2020-06-25 stsp ".cvsignore");
3808 3143d852 2020-06-25 stsp if (err)
3809 3143d852 2020-06-25 stsp return err;
3810 3143d852 2020-06-25 stsp
3811 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3812 3143d852 2020-06-25 stsp ".gitignore");
3813 3143d852 2020-06-25 stsp if (err)
3814 3143d852 2020-06-25 stsp return err;
3815 3143d852 2020-06-25 stsp
3816 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3817 3143d852 2020-06-25 stsp if (err) {
3818 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3819 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3820 3143d852 2020-06-25 stsp return err;
3821 3143d852 2020-06-25 stsp }
3822 3143d852 2020-06-25 stsp for (;;) {
3823 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3824 3143d852 2020-06-25 stsp ".cvsignore");
3825 3143d852 2020-06-25 stsp if (err)
3826 3143d852 2020-06-25 stsp break;
3827 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3828 3143d852 2020-06-25 stsp ".gitignore");
3829 3143d852 2020-06-25 stsp if (err)
3830 3143d852 2020-06-25 stsp break;
3831 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3832 3143d852 2020-06-25 stsp if (err) {
3833 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3834 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3835 3143d852 2020-06-25 stsp break;
3836 3143d852 2020-06-25 stsp }
3837 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3838 ff56836b 2021-07-08 stsp break;
3839 b737c85a 2020-06-26 stsp free(parent_path);
3840 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3841 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3842 3143d852 2020-06-25 stsp }
3843 3143d852 2020-06-25 stsp
3844 b737c85a 2020-06-26 stsp free(parent_path);
3845 b737c85a 2020-06-26 stsp free(next_parent_path);
3846 3143d852 2020-06-25 stsp return err;
3847 abb4604f 2019-07-27 stsp }
3848 abb4604f 2019-07-27 stsp
3849 abb4604f 2019-07-27 stsp static const struct got_error *
3850 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3851 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3852 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3853 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3854 f2a9dc41 2019-12-13 tracey int report_unchanged)
3855 f8d1f275 2019-02-04 stsp {
3856 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3857 6fc93f37 2019-12-13 stsp int fd = -1;
3858 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3859 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3860 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3861 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3862 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3863 3143d852 2020-06-25 stsp
3864 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3865 f8d1f275 2019-02-04 stsp
3866 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3867 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3868 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3869 8dc303cc 2019-07-27 stsp
3870 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3871 a78810f8 2022-03-13 thomas if (ie) {
3872 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3873 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3874 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3875 a78810f8 2022-03-13 thomas goto done;
3876 a78810f8 2022-03-13 thomas }
3877 a78810f8 2022-03-13 thomas
3878 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3879 6fc93f37 2019-12-13 stsp if (fd == -1) {
3880 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3881 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3882 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3883 ff56836b 2021-07-08 stsp else {
3884 ff56836b 2021-07-08 stsp if (!no_ignores) {
3885 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3886 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3887 ff56836b 2021-07-08 stsp if (err)
3888 ff56836b 2021-07-08 stsp goto done;
3889 ff56836b 2021-07-08 stsp }
3890 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3891 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3892 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3893 ff56836b 2021-07-08 stsp }
3894 abb4604f 2019-07-27 stsp } else {
3895 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3896 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3897 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3898 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3899 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3900 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3901 abb4604f 2019-07-27 stsp arg.status_path = path;
3902 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3903 abb4604f 2019-07-27 stsp arg.repo = repo;
3904 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3905 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3906 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3907 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3908 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3909 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3910 022fae89 2019-12-06 tracey if (!no_ignores) {
3911 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3912 3143d852 2020-06-25 stsp worktree->root_path, path);
3913 3143d852 2020-06-25 stsp if (err)
3914 3143d852 2020-06-25 stsp goto done;
3915 022fae89 2019-12-06 tracey }
3916 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3917 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3918 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3919 927df6b7 2019-02-10 stsp }
3920 3143d852 2020-06-25 stsp done:
3921 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3922 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3923 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3924 927df6b7 2019-02-10 stsp free(ondisk_path);
3925 347d1d3e 2019-07-12 stsp return err;
3926 347d1d3e 2019-07-12 stsp }
3927 347d1d3e 2019-07-12 stsp
3928 347d1d3e 2019-07-12 stsp const struct got_error *
3929 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3930 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3931 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3932 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3933 347d1d3e 2019-07-12 stsp {
3934 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3935 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3936 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3937 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3938 347d1d3e 2019-07-12 stsp
3939 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3940 347d1d3e 2019-07-12 stsp if (err)
3941 347d1d3e 2019-07-12 stsp return err;
3942 347d1d3e 2019-07-12 stsp
3943 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3944 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3945 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3946 f6343036 2021-06-22 stsp no_ignores, 0);
3947 72ea6654 2019-07-27 stsp if (err)
3948 72ea6654 2019-07-27 stsp break;
3949 72ea6654 2019-07-27 stsp }
3950 f8d1f275 2019-02-04 stsp free(fileindex_path);
3951 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3952 f8d1f275 2019-02-04 stsp return err;
3953 f8d1f275 2019-02-04 stsp }
3954 6c7ab921 2019-03-18 stsp
3955 6c7ab921 2019-03-18 stsp const struct got_error *
3956 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3957 6c7ab921 2019-03-18 stsp const char *arg)
3958 6c7ab921 2019-03-18 stsp {
3959 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3960 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3961 6c7ab921 2019-03-18 stsp size_t len;
3962 00bb5ea0 2020-07-23 stsp struct stat sb;
3963 01740607 2020-11-04 stsp char *abspath = NULL;
3964 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3965 6c7ab921 2019-03-18 stsp
3966 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3967 6c7ab921 2019-03-18 stsp
3968 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3969 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3970 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3971 00bb5ea0 2020-07-23 stsp
3972 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3973 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3974 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3975 00bb5ea0 2020-07-23 stsp goto done;
3976 00bb5ea0 2020-07-23 stsp }
3977 727173c3 2020-11-06 stsp sb.st_mode = 0;
3978 00bb5ea0 2020-07-23 stsp }
3979 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3980 00bb5ea0 2020-07-23 stsp /*
3981 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3982 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3983 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3984 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3985 00bb5ea0 2020-07-23 stsp */
3986 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3987 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3988 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3989 00bb5ea0 2020-07-23 stsp goto done;
3990 00bb5ea0 2020-07-23 stsp }
3991 00bb5ea0 2020-07-23 stsp
3992 00bb5ea0 2020-07-23 stsp }
3993 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3994 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3995 00bb5ea0 2020-07-23 stsp if (err)
3996 d0710d08 2019-07-22 stsp goto done;
3997 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3998 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3999 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4000 00bb5ea0 2020-07-23 stsp goto done;
4001 00bb5ea0 2020-07-23 stsp }
4002 00bb5ea0 2020-07-23 stsp } else {
4003 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4004 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4005 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4006 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4007 00bb5ea0 2020-07-23 stsp goto done;
4008 00bb5ea0 2020-07-23 stsp }
4009 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4010 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4011 01740607 2020-11-04 stsp goto done;
4012 01740607 2020-11-04 stsp }
4013 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4014 01740607 2020-11-04 stsp sizeof(canonpath));
4015 01740607 2020-11-04 stsp if (err)
4016 00bb5ea0 2020-07-23 stsp goto done;
4017 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4018 01740607 2020-11-04 stsp if (resolved == NULL) {
4019 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4020 01740607 2020-11-04 stsp goto done;
4021 00bb5ea0 2020-07-23 stsp }
4022 d0710d08 2019-07-22 stsp }
4023 d0710d08 2019-07-22 stsp }
4024 6c7ab921 2019-03-18 stsp
4025 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4026 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4027 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4028 6c7ab921 2019-03-18 stsp goto done;
4029 6c7ab921 2019-03-18 stsp }
4030 6c7ab921 2019-03-18 stsp
4031 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4032 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4033 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4034 f6d88e1a 2019-05-29 stsp if (err)
4035 f6d88e1a 2019-05-29 stsp goto done;
4036 f6d88e1a 2019-05-29 stsp } else {
4037 f6d88e1a 2019-05-29 stsp path = strdup("");
4038 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4039 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4040 f6d88e1a 2019-05-29 stsp goto done;
4041 f6d88e1a 2019-05-29 stsp }
4042 6c7ab921 2019-03-18 stsp }
4043 6c7ab921 2019-03-18 stsp
4044 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4045 6c7ab921 2019-03-18 stsp len = strlen(path);
4046 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4047 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4048 6c7ab921 2019-03-18 stsp len--;
4049 6c7ab921 2019-03-18 stsp }
4050 6c7ab921 2019-03-18 stsp done:
4051 01740607 2020-11-04 stsp free(abspath);
4052 6c7ab921 2019-03-18 stsp free(resolved);
4053 d0710d08 2019-07-22 stsp free(cwd);
4054 6c7ab921 2019-03-18 stsp if (err == NULL)
4055 6c7ab921 2019-03-18 stsp *wt_path = path;
4056 6c7ab921 2019-03-18 stsp else
4057 6c7ab921 2019-03-18 stsp free(path);
4058 d00136be 2019-03-26 stsp return err;
4059 d00136be 2019-03-26 stsp }
4060 d00136be 2019-03-26 stsp
4061 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4062 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4063 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4064 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4065 4e68cba3 2019-11-23 stsp void *progress_arg;
4066 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4067 4e68cba3 2019-11-23 stsp };
4068 4e68cba3 2019-11-23 stsp
4069 4e68cba3 2019-11-23 stsp static const struct got_error *
4070 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4071 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4072 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4073 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4074 07f5b47a 2019-06-02 stsp {
4075 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4076 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4077 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4078 6d022e97 2019-08-04 stsp struct stat sb;
4079 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4080 07f5b47a 2019-06-02 stsp
4081 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4082 dbb83fbd 2019-12-12 stsp relpath) == -1)
4083 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4084 dbb83fbd 2019-12-12 stsp
4085 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4086 6d022e97 2019-08-04 stsp if (ie) {
4087 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4088 12463d8b 2019-12-13 stsp de_name, a->repo);
4089 6d022e97 2019-08-04 stsp if (err)
4090 dbb83fbd 2019-12-12 stsp goto done;
4091 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4092 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4093 dbb83fbd 2019-12-12 stsp goto done;
4094 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4095 dbb83fbd 2019-12-12 stsp if (err)
4096 dbb83fbd 2019-12-12 stsp goto done;
4097 6d022e97 2019-08-04 stsp }
4098 07f5b47a 2019-06-02 stsp
4099 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4100 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4101 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4102 84bf00a6 2022-02-12 thomas else
4103 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4104 dbb83fbd 2019-12-12 stsp goto done;
4105 4e68cba3 2019-11-23 stsp }
4106 07f5b47a 2019-06-02 stsp
4107 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4108 4e68cba3 2019-11-23 stsp if (err)
4109 4e68cba3 2019-11-23 stsp goto done;
4110 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4111 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4112 3969253a 2020-03-07 stsp if (err) {
4113 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4114 3969253a 2020-03-07 stsp goto done;
4115 3969253a 2020-03-07 stsp }
4116 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4117 07f5b47a 2019-06-02 stsp if (err) {
4118 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4119 4e68cba3 2019-11-23 stsp goto done;
4120 07f5b47a 2019-06-02 stsp }
4121 4e68cba3 2019-11-23 stsp done:
4122 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4123 dbb83fbd 2019-12-12 stsp if (err)
4124 dbb83fbd 2019-12-12 stsp return err;
4125 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4126 dbb83fbd 2019-12-12 stsp return NULL;
4127 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4128 07f5b47a 2019-06-02 stsp }
4129 07f5b47a 2019-06-02 stsp
4130 d00136be 2019-03-26 stsp const struct got_error *
4131 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4132 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4133 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4134 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4135 d00136be 2019-03-26 stsp {
4136 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4137 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4138 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4139 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4140 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4141 d00136be 2019-03-26 stsp
4142 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4143 d00136be 2019-03-26 stsp if (err)
4144 d00136be 2019-03-26 stsp return err;
4145 d00136be 2019-03-26 stsp
4146 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4147 d00136be 2019-03-26 stsp if (err)
4148 d00136be 2019-03-26 stsp goto done;
4149 d00136be 2019-03-26 stsp
4150 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4151 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4152 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4153 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4154 4e68cba3 2019-11-23 stsp saa.repo = repo;
4155 4e68cba3 2019-11-23 stsp
4156 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4157 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4158 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4159 1dd54920 2019-05-11 stsp if (err)
4160 af12c6b9 2019-06-04 stsp break;
4161 1dd54920 2019-05-11 stsp }
4162 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4163 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4164 af12c6b9 2019-06-04 stsp err = sync_err;
4165 d00136be 2019-03-26 stsp done:
4166 fb399478 2019-07-12 stsp free(fileindex_path);
4167 d00136be 2019-03-26 stsp if (fileindex)
4168 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4169 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4170 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4171 d00136be 2019-03-26 stsp err = unlockerr;
4172 6c7ab921 2019-03-18 stsp return err;
4173 6c7ab921 2019-03-18 stsp }
4174 17ed4618 2019-06-02 stsp
4175 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4176 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4177 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4178 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4179 f2a9dc41 2019-12-13 tracey void *progress_arg;
4180 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4181 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4182 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4183 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4184 766841c2 2020-08-13 stsp const char *status_codes;
4185 f2a9dc41 2019-12-13 tracey };
4186 f2a9dc41 2019-12-13 tracey
4187 f2a9dc41 2019-12-13 tracey static const struct got_error *
4188 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4189 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4190 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4191 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4192 17ed4618 2019-06-02 stsp {
4193 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4194 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4195 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4196 17ed4618 2019-06-02 stsp struct stat sb;
4197 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4198 d64cc78a 2022-01-25 thomas
4199 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4200 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4201 d64cc78a 2022-01-25 thomas return NULL;
4202 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4203 d64cc78a 2022-01-25 thomas }
4204 17ed4618 2019-06-02 stsp
4205 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4206 17ed4618 2019-06-02 stsp if (ie == NULL)
4207 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4208 2ec1f75b 2019-03-26 stsp
4209 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4210 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4211 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4212 9acbc4fa 2019-08-03 stsp return NULL;
4213 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4214 9acbc4fa 2019-08-03 stsp }
4215 9acbc4fa 2019-08-03 stsp
4216 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4217 f2a9dc41 2019-12-13 tracey relpath) == -1)
4218 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4219 f2a9dc41 2019-12-13 tracey
4220 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4221 12463d8b 2019-12-13 stsp a->repo);
4222 17ed4618 2019-06-02 stsp if (err)
4223 f2a9dc41 2019-12-13 tracey goto done;
4224 17ed4618 2019-06-02 stsp
4225 766841c2 2020-08-13 stsp if (a->status_codes) {
4226 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4227 766841c2 2020-08-13 stsp int i;
4228 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4229 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4230 766841c2 2020-08-13 stsp break;
4231 766841c2 2020-08-13 stsp }
4232 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4233 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4234 766841c2 2020-08-13 stsp free(ondisk_path);
4235 766841c2 2020-08-13 stsp return NULL;
4236 766841c2 2020-08-13 stsp }
4237 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4238 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4239 766841c2 2020-08-13 stsp static char msg[64];
4240 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4241 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4242 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4243 766841c2 2020-08-13 stsp goto done;
4244 766841c2 2020-08-13 stsp }
4245 766841c2 2020-08-13 stsp }
4246 766841c2 2020-08-13 stsp
4247 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4248 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4249 f2a9dc41 2019-12-13 tracey goto done;
4250 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4251 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4252 f2a9dc41 2019-12-13 tracey goto done;
4253 f2a9dc41 2019-12-13 tracey }
4254 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4255 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4256 d64cc78a 2022-01-25 thomas goto done;
4257 d64cc78a 2022-01-25 thomas }
4258 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4259 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4260 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4261 f2a9dc41 2019-12-13 tracey goto done;
4262 f2a9dc41 2019-12-13 tracey }
4263 17ed4618 2019-06-02 stsp }
4264 17ed4618 2019-06-02 stsp
4265 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4266 20a2ad1c 2020-10-20 stsp size_t root_len;
4267 20a2ad1c 2020-10-20 stsp
4268 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4269 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4270 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4271 12463d8b 2019-12-13 stsp ondisk_path);
4272 12463d8b 2019-12-13 stsp goto done;
4273 12463d8b 2019-12-13 stsp }
4274 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4275 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4276 12463d8b 2019-12-13 stsp goto done;
4277 15341bfd 2020-03-05 tracey }
4278 15341bfd 2020-03-05 tracey
4279 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4280 20a2ad1c 2020-10-20 stsp do {
4281 20a2ad1c 2020-10-20 stsp char *parent;
4282 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4283 20a2ad1c 2020-10-20 stsp if (err)
4284 2513f20a 2020-10-20 stsp goto done;
4285 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4286 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4287 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4288 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4289 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4290 20a2ad1c 2020-10-20 stsp ondisk_path);
4291 15341bfd 2020-03-05 tracey break;
4292 15341bfd 2020-03-05 tracey }
4293 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4294 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4295 f2a9dc41 2019-12-13 tracey }
4296 17ed4618 2019-06-02 stsp
4297 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4298 f2a9dc41 2019-12-13 tracey done:
4299 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4300 f2a9dc41 2019-12-13 tracey if (err)
4301 f2a9dc41 2019-12-13 tracey return err;
4302 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4303 f2a9dc41 2019-12-13 tracey return NULL;
4304 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4305 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4306 17ed4618 2019-06-02 stsp }
4307 17ed4618 2019-06-02 stsp
4308 2ec1f75b 2019-03-26 stsp const struct got_error *
4309 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4310 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4311 766841c2 2020-08-13 stsp const char *status_codes,
4312 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4313 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4314 2ec1f75b 2019-03-26 stsp {
4315 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4316 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4317 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4318 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4319 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4320 2ec1f75b 2019-03-26 stsp
4321 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4322 2ec1f75b 2019-03-26 stsp if (err)
4323 2ec1f75b 2019-03-26 stsp return err;
4324 2ec1f75b 2019-03-26 stsp
4325 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4326 2ec1f75b 2019-03-26 stsp if (err)
4327 2ec1f75b 2019-03-26 stsp goto done;
4328 f2a9dc41 2019-12-13 tracey
4329 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4330 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4331 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4332 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4333 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4334 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4335 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4336 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4337 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4338 2ec1f75b 2019-03-26 stsp
4339 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4340 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4341 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4342 17ed4618 2019-06-02 stsp if (err)
4343 af12c6b9 2019-06-04 stsp break;
4344 2ec1f75b 2019-03-26 stsp }
4345 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4346 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4347 af12c6b9 2019-06-04 stsp err = sync_err;
4348 2ec1f75b 2019-03-26 stsp done:
4349 fb399478 2019-07-12 stsp free(fileindex_path);
4350 2ec1f75b 2019-03-26 stsp if (fileindex)
4351 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4352 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4353 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4354 2ec1f75b 2019-03-26 stsp err = unlockerr;
4355 33aa809d 2019-08-08 stsp return err;
4356 33aa809d 2019-08-08 stsp }
4357 33aa809d 2019-08-08 stsp
4358 33aa809d 2019-08-08 stsp static const struct got_error *
4359 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4360 33aa809d 2019-08-08 stsp {
4361 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4362 33aa809d 2019-08-08 stsp char *line = NULL;
4363 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4364 33aa809d 2019-08-08 stsp ssize_t linelen;
4365 33aa809d 2019-08-08 stsp
4366 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4367 33aa809d 2019-08-08 stsp if (linelen == -1) {
4368 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4369 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4370 33aa809d 2019-08-08 stsp goto done;
4371 33aa809d 2019-08-08 stsp }
4372 33aa809d 2019-08-08 stsp return NULL;
4373 33aa809d 2019-08-08 stsp }
4374 33aa809d 2019-08-08 stsp if (outfile) {
4375 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4376 33aa809d 2019-08-08 stsp if (n != linelen) {
4377 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4378 33aa809d 2019-08-08 stsp goto done;
4379 33aa809d 2019-08-08 stsp }
4380 33aa809d 2019-08-08 stsp }
4381 33aa809d 2019-08-08 stsp if (rejectfile) {
4382 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4383 33aa809d 2019-08-08 stsp if (n != linelen)
4384 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4385 33aa809d 2019-08-08 stsp }
4386 33aa809d 2019-08-08 stsp done:
4387 33aa809d 2019-08-08 stsp free(line);
4388 2ec1f75b 2019-03-26 stsp return err;
4389 2ec1f75b 2019-03-26 stsp }
4390 1f1abb7e 2019-08-08 stsp
4391 33aa809d 2019-08-08 stsp static const struct got_error *
4392 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4393 33aa809d 2019-08-08 stsp {
4394 33aa809d 2019-08-08 stsp char *line = NULL;
4395 33aa809d 2019-08-08 stsp size_t linesize = 0;
4396 33aa809d 2019-08-08 stsp ssize_t linelen;
4397 33aa809d 2019-08-08 stsp
4398 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4399 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4400 500467ff 2019-09-25 hiltjo if (ferror(f))
4401 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4402 500467ff 2019-09-25 hiltjo return NULL;
4403 500467ff 2019-09-25 hiltjo }
4404 33aa809d 2019-08-08 stsp free(line);
4405 33aa809d 2019-08-08 stsp return NULL;
4406 33aa809d 2019-08-08 stsp }
4407 33aa809d 2019-08-08 stsp
4408 33aa809d 2019-08-08 stsp static const struct got_error *
4409 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4410 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4411 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4412 abc59930 2021-09-05 naddy {
4413 33aa809d 2019-08-08 stsp const struct got_error *err;
4414 33aa809d 2019-08-08 stsp
4415 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4416 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4417 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4418 33aa809d 2019-08-08 stsp if (err)
4419 33aa809d 2019-08-08 stsp return err;
4420 33aa809d 2019-08-08 stsp (*line_cur1)++;
4421 33aa809d 2019-08-08 stsp }
4422 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4423 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4424 33aa809d 2019-08-08 stsp if (rejectfile)
4425 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4426 33aa809d 2019-08-08 stsp else
4427 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4428 33aa809d 2019-08-08 stsp if (err)
4429 33aa809d 2019-08-08 stsp return err;
4430 33aa809d 2019-08-08 stsp (*line_cur2)++;
4431 33aa809d 2019-08-08 stsp }
4432 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4433 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4434 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4435 33aa809d 2019-08-08 stsp if (err)
4436 33aa809d 2019-08-08 stsp return err;
4437 33aa809d 2019-08-08 stsp (*line_cur2)++;
4438 33aa809d 2019-08-08 stsp }
4439 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4440 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4441 33aa809d 2019-08-08 stsp if (rejectfile)
4442 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4443 33aa809d 2019-08-08 stsp else
4444 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4445 33aa809d 2019-08-08 stsp if (err)
4446 33aa809d 2019-08-08 stsp return err;
4447 33aa809d 2019-08-08 stsp (*line_cur1)++;
4448 33aa809d 2019-08-08 stsp }
4449 f1e81a05 2019-08-10 stsp
4450 f1e81a05 2019-08-10 stsp return NULL;
4451 f1e81a05 2019-08-10 stsp }
4452 f1e81a05 2019-08-10 stsp
4453 f1e81a05 2019-08-10 stsp static const struct got_error *
4454 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4455 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4456 f1e81a05 2019-08-10 stsp {
4457 f1e81a05 2019-08-10 stsp const struct got_error *err;
4458 f1e81a05 2019-08-10 stsp
4459 f1e81a05 2019-08-10 stsp if (outfile) {
4460 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4461 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4462 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4463 f1e81a05 2019-08-10 stsp if (err)
4464 f1e81a05 2019-08-10 stsp return err;
4465 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4466 f1e81a05 2019-08-10 stsp }
4467 f1e81a05 2019-08-10 stsp }
4468 f1e81a05 2019-08-10 stsp if (rejectfile) {
4469 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4470 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4471 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4472 f1e81a05 2019-08-10 stsp if (err)
4473 f1e81a05 2019-08-10 stsp return err;
4474 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4475 f1e81a05 2019-08-10 stsp }
4476 33aa809d 2019-08-08 stsp }
4477 33aa809d 2019-08-08 stsp
4478 33aa809d 2019-08-08 stsp return NULL;
4479 33aa809d 2019-08-08 stsp }
4480 33aa809d 2019-08-08 stsp
4481 33aa809d 2019-08-08 stsp static const struct got_error *
4482 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4483 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4484 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4485 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4486 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4487 33aa809d 2019-08-08 stsp {
4488 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4489 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4490 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4491 33aa809d 2019-08-08 stsp FILE *hunkfile;
4492 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4493 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4494 fe621944 2020-11-10 stsp int rc;
4495 33aa809d 2019-08-08 stsp
4496 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4497 33aa809d 2019-08-08 stsp
4498 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4499 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4500 fe621944 2020-11-10 stsp start_old = cc.left.start;
4501 fe621944 2020-11-10 stsp end_old = cc.left.end;
4502 fe621944 2020-11-10 stsp start_new = cc.right.start;
4503 fe621944 2020-11-10 stsp end_new = cc.right.end;
4504 33aa809d 2019-08-08 stsp
4505 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4506 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4507 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4508 33aa809d 2019-08-08 stsp
4509 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4510 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4511 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4512 33aa809d 2019-08-08 stsp
4513 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4514 fe621944 2020-11-10 stsp if (diff_state == NULL)
4515 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4516 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4517 fe621944 2020-11-10 stsp
4518 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4519 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4520 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4521 33aa809d 2019-08-08 stsp goto done;
4522 33aa809d 2019-08-08 stsp }
4523 fe621944 2020-11-10 stsp
4524 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4525 fe621944 2020-11-10 stsp diff_result, &cc);
4526 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4527 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4528 33aa809d 2019-08-08 stsp goto done;
4529 33aa809d 2019-08-08 stsp }
4530 fe621944 2020-11-10 stsp
4531 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4532 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4533 33aa809d 2019-08-08 stsp goto done;
4534 33aa809d 2019-08-08 stsp }
4535 33aa809d 2019-08-08 stsp
4536 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4537 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4538 33aa809d 2019-08-08 stsp if (err)
4539 33aa809d 2019-08-08 stsp goto done;
4540 33aa809d 2019-08-08 stsp
4541 33aa809d 2019-08-08 stsp switch (*choice) {
4542 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4543 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4544 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4545 33aa809d 2019-08-08 stsp break;
4546 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4547 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4548 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4549 33aa809d 2019-08-08 stsp break;
4550 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4551 33aa809d 2019-08-08 stsp break;
4552 33aa809d 2019-08-08 stsp default:
4553 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4554 33aa809d 2019-08-08 stsp break;
4555 33aa809d 2019-08-08 stsp }
4556 33aa809d 2019-08-08 stsp done:
4557 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4558 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4559 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4560 33aa809d 2019-08-08 stsp return err;
4561 33aa809d 2019-08-08 stsp }
4562 33aa809d 2019-08-08 stsp
4563 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4564 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4565 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4566 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4567 1f1abb7e 2019-08-08 stsp void *progress_arg;
4568 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4569 33aa809d 2019-08-08 stsp void *patch_arg;
4570 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4571 10604dce 2021-09-24 thomas int unlink_added_files;
4572 1f1abb7e 2019-08-08 stsp };
4573 a129376b 2019-03-28 stsp
4574 e20a8b6f 2019-06-04 stsp static const struct got_error *
4575 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4576 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4577 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4578 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4579 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4580 33aa809d 2019-08-08 stsp {
4581 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4582 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4583 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4584 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4585 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4586 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4587 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4588 fe621944 2020-11-10 stsp struct stat sb2;
4589 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4590 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4591 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4592 33aa809d 2019-08-08 stsp
4593 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4594 33aa809d 2019-08-08 stsp
4595 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4596 33aa809d 2019-08-08 stsp if (err)
4597 33aa809d 2019-08-08 stsp return err;
4598 33aa809d 2019-08-08 stsp
4599 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4600 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4601 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4602 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4603 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4604 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4605 fa3cef63 2020-07-23 stsp goto done;
4606 fa3cef63 2020-07-23 stsp }
4607 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4608 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4609 48b4f239 2021-12-31 thomas if (link_len == -1) {
4610 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4611 48b4f239 2021-12-31 thomas path2);
4612 48b4f239 2021-12-31 thomas }
4613 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4614 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4615 12463d8b 2019-12-13 stsp }
4616 12463d8b 2019-12-13 stsp } else {
4617 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4618 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4619 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4620 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4621 fa3cef63 2020-07-23 stsp goto done;
4622 fa3cef63 2020-07-23 stsp }
4623 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4624 fa3cef63 2020-07-23 stsp sizeof(link_target));
4625 fa3cef63 2020-07-23 stsp if (link_len == -1)
4626 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4627 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4628 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4629 12463d8b 2019-12-13 stsp }
4630 1ebedb77 2019-10-19 stsp }
4631 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4632 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4633 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4634 fa3cef63 2020-07-23 stsp goto done;
4635 fa3cef63 2020-07-23 stsp }
4636 1ebedb77 2019-10-19 stsp
4637 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4638 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4639 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4640 fa3cef63 2020-07-23 stsp goto done;
4641 fa3cef63 2020-07-23 stsp }
4642 fa3cef63 2020-07-23 stsp fd2 = -1;
4643 fa3cef63 2020-07-23 stsp } else {
4644 fa3cef63 2020-07-23 stsp size_t n;
4645 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4646 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4647 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4648 fa3cef63 2020-07-23 stsp goto done;
4649 fa3cef63 2020-07-23 stsp }
4650 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4651 fa3cef63 2020-07-23 stsp if (n != link_len) {
4652 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4653 fa3cef63 2020-07-23 stsp goto done;
4654 fa3cef63 2020-07-23 stsp }
4655 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4656 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4657 fa3cef63 2020-07-23 stsp goto done;
4658 fa3cef63 2020-07-23 stsp }
4659 fa3cef63 2020-07-23 stsp rewind(f2);
4660 33aa809d 2019-08-08 stsp }
4661 33aa809d 2019-08-08 stsp
4662 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4663 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4664 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4665 f4ae6ddb 2022-07-01 thomas goto done;
4666 f4ae6ddb 2022-07-01 thomas }
4667 f4ae6ddb 2022-07-01 thomas
4668 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4669 33aa809d 2019-08-08 stsp if (err)
4670 33aa809d 2019-08-08 stsp goto done;
4671 33aa809d 2019-08-08 stsp
4672 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4673 33aa809d 2019-08-08 stsp if (err)
4674 33aa809d 2019-08-08 stsp goto done;
4675 33aa809d 2019-08-08 stsp
4676 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4677 33aa809d 2019-08-08 stsp if (err)
4678 33aa809d 2019-08-08 stsp goto done;
4679 33aa809d 2019-08-08 stsp
4680 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4681 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4682 33aa809d 2019-08-08 stsp if (err)
4683 33aa809d 2019-08-08 stsp goto done;
4684 33aa809d 2019-08-08 stsp
4685 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4686 fc2a50f2 2022-11-01 thomas "");
4687 33aa809d 2019-08-08 stsp if (err)
4688 33aa809d 2019-08-08 stsp goto done;
4689 33aa809d 2019-08-08 stsp
4690 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4691 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4692 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4693 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4694 fe621944 2020-11-10 stsp
4695 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4696 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4697 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4698 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4699 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4700 fe621944 2020-11-10 stsp nchanges++;
4701 fe621944 2020-11-10 stsp }
4702 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4703 33aa809d 2019-08-08 stsp int choice;
4704 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4705 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4706 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4707 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4708 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4709 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4710 33aa809d 2019-08-08 stsp if (err)
4711 33aa809d 2019-08-08 stsp goto done;
4712 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4713 33aa809d 2019-08-08 stsp have_content = 1;
4714 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4715 33aa809d 2019-08-08 stsp break;
4716 33aa809d 2019-08-08 stsp }
4717 1ebedb77 2019-10-19 stsp if (have_content) {
4718 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4719 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4720 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4721 1ebedb77 2019-10-19 stsp if (err)
4722 1ebedb77 2019-10-19 stsp goto done;
4723 1ebedb77 2019-10-19 stsp
4724 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4725 a2c162eb 2022-10-30 thomas mode_t mode;
4726 a2c162eb 2022-10-30 thomas
4727 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4728 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4729 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4730 fa3cef63 2020-07-23 stsp goto done;
4731 fa3cef63 2020-07-23 stsp }
4732 1ebedb77 2019-10-19 stsp }
4733 1ebedb77 2019-10-19 stsp }
4734 33aa809d 2019-08-08 stsp done:
4735 33aa809d 2019-08-08 stsp free(id_str);
4736 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4737 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4738 33aa809d 2019-08-08 stsp if (blob)
4739 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4740 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4741 fe621944 2020-11-10 stsp if (err == NULL)
4742 fe621944 2020-11-10 stsp err = free_err;
4743 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4744 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4745 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4746 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4747 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4748 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4749 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4750 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4751 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4752 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4753 33aa809d 2019-08-08 stsp if (err || !have_content) {
4754 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4755 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4756 33aa809d 2019-08-08 stsp free(*path_outfile);
4757 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4758 33aa809d 2019-08-08 stsp }
4759 33aa809d 2019-08-08 stsp free(path1);
4760 33aa809d 2019-08-08 stsp return err;
4761 33aa809d 2019-08-08 stsp }
4762 33aa809d 2019-08-08 stsp
4763 33aa809d 2019-08-08 stsp static const struct got_error *
4764 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4765 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4766 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4767 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4768 a129376b 2019-03-28 stsp {
4769 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4770 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4771 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4772 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4773 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4774 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4775 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4776 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4777 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4778 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4779 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4780 f4ae6ddb 2022-07-01 thomas int fd = -1;
4781 a129376b 2019-03-28 stsp
4782 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4783 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4784 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4785 d3bcc3d1 2019-08-08 stsp return NULL;
4786 3d69ad8d 2019-08-17 semarie
4787 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4788 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4789 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4790 d3bcc3d1 2019-08-08 stsp
4791 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4792 65084dad 2019-08-08 stsp if (ie == NULL)
4793 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4794 a129376b 2019-03-28 stsp
4795 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4796 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4797 a129376b 2019-03-28 stsp if (err) {
4798 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4799 a129376b 2019-03-28 stsp goto done;
4800 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4801 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4802 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4803 e20a8b6f 2019-06-04 stsp goto done;
4804 e20a8b6f 2019-06-04 stsp }
4805 a129376b 2019-03-28 stsp }
4806 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4807 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4808 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4809 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4810 a129376b 2019-03-28 stsp goto done;
4811 a129376b 2019-03-28 stsp }
4812 a129376b 2019-03-28 stsp } else {
4813 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4814 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4815 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4816 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4817 a129376b 2019-03-28 stsp goto done;
4818 a129376b 2019-03-28 stsp }
4819 a129376b 2019-03-28 stsp } else {
4820 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4821 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4822 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4823 a129376b 2019-03-28 stsp goto done;
4824 a129376b 2019-03-28 stsp }
4825 a129376b 2019-03-28 stsp }
4826 a129376b 2019-03-28 stsp }
4827 a129376b 2019-03-28 stsp
4828 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4829 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4830 945f9229 2022-04-16 thomas if (err)
4831 945f9229 2022-04-16 thomas goto done;
4832 945f9229 2022-04-16 thomas
4833 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4834 a9fa2909 2019-07-27 stsp if (err) {
4835 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4836 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4837 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4838 a9fa2909 2019-07-27 stsp goto done;
4839 a9fa2909 2019-07-27 stsp } else {
4840 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4841 a9fa2909 2019-07-27 stsp if (err)
4842 a9fa2909 2019-07-27 stsp goto done;
4843 a9fa2909 2019-07-27 stsp
4844 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4845 1233e6b6 2020-10-19 stsp if (err)
4846 a9fa2909 2019-07-27 stsp goto done;
4847 a9fa2909 2019-07-27 stsp
4848 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4849 1233e6b6 2020-10-19 stsp free(te_name);
4850 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4851 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4852 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4853 a9fa2909 2019-07-27 stsp goto done;
4854 a9fa2909 2019-07-27 stsp }
4855 a129376b 2019-03-28 stsp }
4856 a129376b 2019-03-28 stsp
4857 a129376b 2019-03-28 stsp switch (status) {
4858 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4859 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4860 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4861 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4862 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4863 33aa809d 2019-08-08 stsp if (err)
4864 33aa809d 2019-08-08 stsp goto done;
4865 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4866 33aa809d 2019-08-08 stsp break;
4867 33aa809d 2019-08-08 stsp }
4868 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4869 1f1abb7e 2019-08-08 stsp ie->path);
4870 1ee397ad 2019-07-12 stsp if (err)
4871 1ee397ad 2019-07-12 stsp goto done;
4872 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4873 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4874 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4875 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4876 10604dce 2021-09-24 thomas relpath) == -1) {
4877 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4878 10604dce 2021-09-24 thomas goto done;
4879 10604dce 2021-09-24 thomas }
4880 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4881 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4882 10604dce 2021-09-24 thomas ondisk_path);
4883 10604dce 2021-09-24 thomas break;
4884 10604dce 2021-09-24 thomas }
4885 10604dce 2021-09-24 thomas }
4886 a129376b 2019-03-28 stsp break;
4887 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4888 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4889 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4890 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4891 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4892 33aa809d 2019-08-08 stsp if (err)
4893 33aa809d 2019-08-08 stsp goto done;
4894 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4895 33aa809d 2019-08-08 stsp break;
4896 33aa809d 2019-08-08 stsp }
4897 33aa809d 2019-08-08 stsp /* fall through */
4898 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4899 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4900 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4901 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4902 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4903 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4904 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4905 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4906 43010591 2023-02-17 thomas else
4907 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4908 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4909 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4910 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4911 f4ae6ddb 2022-07-01 thomas goto done;
4912 f4ae6ddb 2022-07-01 thomas }
4913 f4ae6ddb 2022-07-01 thomas
4914 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4915 a129376b 2019-03-28 stsp if (err)
4916 65084dad 2019-08-08 stsp goto done;
4917 65084dad 2019-08-08 stsp
4918 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4919 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4920 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4921 a129376b 2019-03-28 stsp goto done;
4922 65084dad 2019-08-08 stsp }
4923 65084dad 2019-08-08 stsp
4924 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4925 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4926 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4927 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4928 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4929 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4930 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4931 33aa809d 2019-08-08 stsp break;
4932 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4933 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4934 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4935 369fd7e5 2020-07-23 stsp path_content);
4936 369fd7e5 2020-07-23 stsp break;
4937 369fd7e5 2020-07-23 stsp }
4938 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4939 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4940 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4941 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4942 369fd7e5 2020-07-23 stsp } else {
4943 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4944 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4945 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4946 369fd7e5 2020-07-23 stsp goto done;
4947 369fd7e5 2020-07-23 stsp }
4948 33aa809d 2019-08-08 stsp }
4949 33aa809d 2019-08-08 stsp } else {
4950 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4951 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4952 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4953 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4954 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4955 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4956 2e1fa222 2020-07-23 stsp } else {
4957 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4958 2e1fa222 2020-07-23 stsp ie->path,
4959 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4960 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4961 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4962 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4963 2e1fa222 2020-07-23 stsp }
4964 a129376b 2019-03-28 stsp if (err)
4965 a129376b 2019-03-28 stsp goto done;
4966 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4967 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4968 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4969 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4970 437adc9d 2020-12-10 yzhong blob->id.sha1,
4971 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4972 2e1fa222 2020-07-23 stsp if (err)
4973 2e1fa222 2020-07-23 stsp goto done;
4974 2e1fa222 2020-07-23 stsp }
4975 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4976 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4977 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4978 33aa809d 2019-08-08 stsp }
4979 a129376b 2019-03-28 stsp }
4980 a129376b 2019-03-28 stsp break;
4981 e20a8b6f 2019-06-04 stsp }
4982 a129376b 2019-03-28 stsp default:
4983 1f1abb7e 2019-08-08 stsp break;
4984 a129376b 2019-03-28 stsp }
4985 a129376b 2019-03-28 stsp done:
4986 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4987 33aa809d 2019-08-08 stsp free(path_content);
4988 e20a8b6f 2019-06-04 stsp free(parent_path);
4989 a129376b 2019-03-28 stsp free(tree_path);
4990 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4991 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4992 a129376b 2019-03-28 stsp if (blob)
4993 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4994 a129376b 2019-03-28 stsp if (tree)
4995 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4996 a129376b 2019-03-28 stsp free(tree_id);
4997 945f9229 2022-04-16 thomas if (base_commit)
4998 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4999 e20a8b6f 2019-06-04 stsp return err;
5000 e20a8b6f 2019-06-04 stsp }
5001 e20a8b6f 2019-06-04 stsp
5002 e20a8b6f 2019-06-04 stsp const struct got_error *
5003 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5004 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5005 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5006 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5007 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5008 e20a8b6f 2019-06-04 stsp {
5009 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5010 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5011 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5012 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5013 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5014 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5015 e20a8b6f 2019-06-04 stsp
5016 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5017 e20a8b6f 2019-06-04 stsp if (err)
5018 e20a8b6f 2019-06-04 stsp return err;
5019 e20a8b6f 2019-06-04 stsp
5020 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5021 e20a8b6f 2019-06-04 stsp if (err)
5022 e20a8b6f 2019-06-04 stsp goto done;
5023 e20a8b6f 2019-06-04 stsp
5024 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5025 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5026 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5027 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5028 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5029 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5030 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5031 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5032 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5033 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5034 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5035 e20a8b6f 2019-06-04 stsp if (err)
5036 af12c6b9 2019-06-04 stsp break;
5037 e20a8b6f 2019-06-04 stsp }
5038 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5039 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5040 e20a8b6f 2019-06-04 stsp err = sync_err;
5041 af12c6b9 2019-06-04 stsp done:
5042 fb399478 2019-07-12 stsp free(fileindex_path);
5043 a129376b 2019-03-28 stsp if (fileindex)
5044 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5045 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5046 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5047 a129376b 2019-03-28 stsp err = unlockerr;
5048 c4296144 2019-05-09 stsp return err;
5049 c4296144 2019-05-09 stsp }
5050 c4296144 2019-05-09 stsp
5051 cf066bf8 2019-05-09 stsp static void
5052 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5053 cf066bf8 2019-05-09 stsp {
5054 24519714 2019-05-09 stsp free(ct->path);
5055 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5056 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5057 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5058 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5059 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5060 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5061 cf066bf8 2019-05-09 stsp free(ct);
5062 cf066bf8 2019-05-09 stsp }
5063 24519714 2019-05-09 stsp
5064 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5065 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5066 24519714 2019-05-09 stsp struct got_repository *repo;
5067 24519714 2019-05-09 stsp struct got_worktree *worktree;
5068 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5069 5f8a88c6 2019-08-03 stsp int have_staged_files;
5070 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5071 ef899790 2022-11-01 thomas int diff_header_shown;
5072 be94c032 2023-02-20 thomas int commit_conflicts;
5073 ef899790 2022-11-01 thomas FILE *diff_outfile;
5074 ef899790 2022-11-01 thomas FILE *f1;
5075 ef899790 2022-11-01 thomas FILE *f2;
5076 24519714 2019-05-09 stsp };
5077 ef899790 2022-11-01 thomas
5078 ef899790 2022-11-01 thomas /*
5079 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5080 ef899790 2022-11-01 thomas * it as content to the diff engine.
5081 ef899790 2022-11-01 thomas */
5082 ef899790 2022-11-01 thomas static const struct got_error *
5083 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5084 ef899790 2022-11-01 thomas const char *abspath)
5085 ef899790 2022-11-01 thomas {
5086 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5087 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5088 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5089 ef899790 2022-11-01 thomas
5090 ef899790 2022-11-01 thomas *fd = -1;
5091 ef899790 2022-11-01 thomas
5092 ef899790 2022-11-01 thomas if (dirfd != -1) {
5093 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5094 ef899790 2022-11-01 thomas if (target_len == -1)
5095 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5096 ef899790 2022-11-01 thomas } else {
5097 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5098 ef899790 2022-11-01 thomas if (target_len == -1)
5099 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5100 ef899790 2022-11-01 thomas }
5101 ef899790 2022-11-01 thomas
5102 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5103 ef899790 2022-11-01 thomas if (*fd == -1)
5104 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5105 ef899790 2022-11-01 thomas
5106 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5107 ef899790 2022-11-01 thomas if (outlen == -1) {
5108 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5109 ef899790 2022-11-01 thomas goto done;
5110 ef899790 2022-11-01 thomas }
5111 ef899790 2022-11-01 thomas
5112 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5113 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5114 ef899790 2022-11-01 thomas goto done;
5115 ef899790 2022-11-01 thomas }
5116 ef899790 2022-11-01 thomas done:
5117 ef899790 2022-11-01 thomas if (err) {
5118 ef899790 2022-11-01 thomas close(*fd);
5119 ef899790 2022-11-01 thomas *fd = -1;
5120 ef899790 2022-11-01 thomas }
5121 ef899790 2022-11-01 thomas return err;
5122 ef899790 2022-11-01 thomas }
5123 ef899790 2022-11-01 thomas
5124 ef899790 2022-11-01 thomas static const struct got_error *
5125 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5126 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5127 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5128 ef899790 2022-11-01 thomas {
5129 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5130 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5131 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5132 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5133 ef899790 2022-11-01 thomas char *label1 = NULL;
5134 ef899790 2022-11-01 thomas struct stat sb;
5135 ef899790 2022-11-01 thomas off_t size1 = 0;
5136 ef899790 2022-11-01 thomas int f2_exists = 0;
5137 ef899790 2022-11-01 thomas char *id_str = NULL;
5138 ef899790 2022-11-01 thomas
5139 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5140 d259e491 2022-11-01 thomas
5141 d259e491 2022-11-01 thomas if (diff_staged) {
5142 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5143 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5144 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5145 d259e491 2022-11-01 thomas return NULL;
5146 d259e491 2022-11-01 thomas } else {
5147 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5148 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5149 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5150 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5151 d259e491 2022-11-01 thomas return NULL;
5152 d259e491 2022-11-01 thomas }
5153 ef899790 2022-11-01 thomas
5154 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5155 ef899790 2022-11-01 thomas if (err)
5156 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5157 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5158 ef899790 2022-11-01 thomas if (err)
5159 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5160 ef899790 2022-11-01 thomas
5161 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5162 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5163 ef899790 2022-11-01 thomas if (err)
5164 ef899790 2022-11-01 thomas return err;
5165 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5166 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5167 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5168 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5169 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5170 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5171 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5172 ef899790 2022-11-01 thomas }
5173 ef899790 2022-11-01 thomas
5174 ef899790 2022-11-01 thomas if (diff_staged) {
5175 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5176 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5177 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5178 ef899790 2022-11-01 thomas label1 = ct->path;
5179 ef899790 2022-11-01 thomas label2 = ct->path;
5180 ef899790 2022-11-01 thomas break;
5181 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5182 ef899790 2022-11-01 thomas label2 = ct->path;
5183 ef899790 2022-11-01 thomas break;
5184 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5185 ef899790 2022-11-01 thomas label1 = ct->path;
5186 ef899790 2022-11-01 thomas break;
5187 ef899790 2022-11-01 thomas default:
5188 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5189 ef899790 2022-11-01 thomas }
5190 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5191 ef899790 2022-11-01 thomas if (fd1 == -1) {
5192 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5193 ef899790 2022-11-01 thomas goto done;
5194 ef899790 2022-11-01 thomas }
5195 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5196 ef899790 2022-11-01 thomas if (fd2 == -1) {
5197 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5198 ef899790 2022-11-01 thomas goto done;
5199 ef899790 2022-11-01 thomas }
5200 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5201 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5202 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5203 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5204 ef899790 2022-11-01 thomas goto done;
5205 ef899790 2022-11-01 thomas }
5206 ef899790 2022-11-01 thomas
5207 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5208 ef899790 2022-11-01 thomas if (fd1 == -1) {
5209 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5210 ef899790 2022-11-01 thomas goto done;
5211 ef899790 2022-11-01 thomas }
5212 ef899790 2022-11-01 thomas
5213 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5214 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5215 ef899790 2022-11-01 thomas 8192, fd1);
5216 ef899790 2022-11-01 thomas if (err)
5217 ef899790 2022-11-01 thomas goto done;
5218 ef899790 2022-11-01 thomas }
5219 ef899790 2022-11-01 thomas
5220 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5221 ef899790 2022-11-01 thomas if (dirfd != -1) {
5222 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5223 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5224 ef899790 2022-11-01 thomas if (fd == -1) {
5225 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5226 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5227 ef899790 2022-11-01 thomas ct->ondisk_path);
5228 ef899790 2022-11-01 thomas goto done;
5229 ef899790 2022-11-01 thomas }
5230 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5231 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5232 ef899790 2022-11-01 thomas if (err)
5233 ef899790 2022-11-01 thomas goto done;
5234 ef899790 2022-11-01 thomas }
5235 ef899790 2022-11-01 thomas } else {
5236 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5237 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5238 ef899790 2022-11-01 thomas if (fd == -1) {
5239 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5240 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5241 ef899790 2022-11-01 thomas ct->ondisk_path);
5242 ef899790 2022-11-01 thomas goto done;
5243 ef899790 2022-11-01 thomas }
5244 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5245 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5246 ef899790 2022-11-01 thomas if (err)
5247 ef899790 2022-11-01 thomas goto done;
5248 ef899790 2022-11-01 thomas }
5249 ef899790 2022-11-01 thomas }
5250 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5251 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5252 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5253 ef899790 2022-11-01 thomas goto done;
5254 ef899790 2022-11-01 thomas }
5255 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5256 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5257 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5258 ef899790 2022-11-01 thomas goto done;
5259 ef899790 2022-11-01 thomas }
5260 ef899790 2022-11-01 thomas fd = -1;
5261 ef899790 2022-11-01 thomas f2_exists = 1;
5262 ef899790 2022-11-01 thomas }
5263 ef899790 2022-11-01 thomas
5264 ef899790 2022-11-01 thomas if (blob1) {
5265 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5266 ef899790 2022-11-01 thomas f1, blob1);
5267 ef899790 2022-11-01 thomas if (err)
5268 ef899790 2022-11-01 thomas goto done;
5269 ef899790 2022-11-01 thomas }
5270 ef899790 2022-11-01 thomas
5271 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5272 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5273 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5274 ef899790 2022-11-01 thomas done:
5275 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5276 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5277 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5278 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5279 ef899790 2022-11-01 thomas if (blob1)
5280 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5281 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5282 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5283 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5284 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5285 ef899790 2022-11-01 thomas return err;
5286 ef899790 2022-11-01 thomas }
5287 cf066bf8 2019-05-09 stsp
5288 c4296144 2019-05-09 stsp static const struct got_error *
5289 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5290 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5291 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5292 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5293 c4296144 2019-05-09 stsp {
5294 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5295 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5296 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5297 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5298 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5299 768aea60 2019-05-09 stsp struct stat sb;
5300 c4296144 2019-05-09 stsp
5301 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5302 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5303 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5304 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5305 5f8a88c6 2019-08-03 stsp return NULL;
5306 5f8a88c6 2019-08-03 stsp } else {
5307 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5308 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5309 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5310 be94c032 2023-02-20 thomas }
5311 c4296144 2019-05-09 stsp
5312 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5313 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5314 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5315 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5316 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5317 5f8a88c6 2019-08-03 stsp return NULL;
5318 5f8a88c6 2019-08-03 stsp }
5319 0b5cc0d6 2019-05-09 stsp
5320 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5321 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5322 036813ee 2019-05-09 stsp goto done;
5323 036813ee 2019-05-09 stsp }
5324 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5325 036813ee 2019-05-09 stsp parent_path = strdup("");
5326 69960a46 2019-05-09 stsp if (parent_path == NULL)
5327 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5328 69960a46 2019-05-09 stsp } else {
5329 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5330 69960a46 2019-05-09 stsp if (err)
5331 69960a46 2019-05-09 stsp return err;
5332 69960a46 2019-05-09 stsp }
5333 c4296144 2019-05-09 stsp
5334 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5335 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5336 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5337 c4296144 2019-05-09 stsp goto done;
5338 768aea60 2019-05-09 stsp }
5339 768aea60 2019-05-09 stsp
5340 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5341 768aea60 2019-05-09 stsp relpath) == -1) {
5342 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5343 768aea60 2019-05-09 stsp goto done;
5344 768aea60 2019-05-09 stsp }
5345 0aeb8099 2020-07-23 stsp
5346 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5347 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5348 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5349 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5350 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5351 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5352 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5353 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5354 0aeb8099 2020-07-23 stsp break;
5355 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5356 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5357 0aeb8099 2020-07-23 stsp break;
5358 0aeb8099 2020-07-23 stsp default:
5359 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5360 0aeb8099 2020-07-23 stsp goto done;
5361 0aeb8099 2020-07-23 stsp }
5362 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5363 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5364 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5365 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5366 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5367 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5368 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5369 12463d8b 2019-12-13 stsp ct->ondisk_path);
5370 12463d8b 2019-12-13 stsp goto done;
5371 12463d8b 2019-12-13 stsp }
5372 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5373 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5374 768aea60 2019-05-09 stsp goto done;
5375 768aea60 2019-05-09 stsp }
5376 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5377 c4296144 2019-05-09 stsp }
5378 c4296144 2019-05-09 stsp
5379 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5380 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5381 44d03001 2019-05-09 stsp relpath) == -1) {
5382 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5383 44d03001 2019-05-09 stsp goto done;
5384 44d03001 2019-05-09 stsp }
5385 44d03001 2019-05-09 stsp
5386 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5387 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5388 35213c7c 2020-07-23 stsp int is_bad_symlink;
5389 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5390 35213c7c 2020-07-23 stsp ssize_t target_len;
5391 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5392 35213c7c 2020-07-23 stsp sizeof(target_path));
5393 35213c7c 2020-07-23 stsp if (target_len == -1) {
5394 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5395 35213c7c 2020-07-23 stsp ct->ondisk_path);
5396 35213c7c 2020-07-23 stsp goto done;
5397 35213c7c 2020-07-23 stsp }
5398 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5399 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5400 35213c7c 2020-07-23 stsp if (err)
5401 35213c7c 2020-07-23 stsp goto done;
5402 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5403 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5404 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5405 35213c7c 2020-07-23 stsp goto done;
5406 35213c7c 2020-07-23 stsp }
5407 35213c7c 2020-07-23 stsp }
5408 35213c7c 2020-07-23 stsp
5409 35213c7c 2020-07-23 stsp
5410 cf066bf8 2019-05-09 stsp ct->status = status;
5411 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5412 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5413 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5414 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5415 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5416 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5417 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5418 b416585c 2019-05-13 stsp goto done;
5419 b416585c 2019-05-13 stsp }
5420 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5421 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5422 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5423 f0b75401 2019-08-03 stsp goto done;
5424 f0b75401 2019-08-03 stsp }
5425 f0b75401 2019-08-03 stsp }
5426 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5427 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5428 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5429 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5430 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5431 036813ee 2019-05-09 stsp goto done;
5432 036813ee 2019-05-09 stsp }
5433 ca2503ea 2019-05-09 stsp }
5434 24519714 2019-05-09 stsp ct->path = strdup(path);
5435 24519714 2019-05-09 stsp if (ct->path == NULL) {
5436 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5437 24519714 2019-05-09 stsp goto done;
5438 24519714 2019-05-09 stsp }
5439 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5440 ef899790 2022-11-01 thomas if (err)
5441 ef899790 2022-11-01 thomas goto done;
5442 ef899790 2022-11-01 thomas
5443 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5444 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5445 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5446 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5447 ef899790 2022-11-01 thomas if (err)
5448 ef899790 2022-11-01 thomas goto done;
5449 ef899790 2022-11-01 thomas }
5450 c4296144 2019-05-09 stsp done:
5451 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5452 ed175427 2019-05-09 stsp free_commitable(ct);
5453 24519714 2019-05-09 stsp free(parent_path);
5454 036813ee 2019-05-09 stsp free(path);
5455 a129376b 2019-03-28 stsp return err;
5456 a129376b 2019-03-28 stsp }
5457 c4296144 2019-05-09 stsp
5458 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5459 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5460 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5461 036813ee 2019-05-09 stsp struct got_repository *);
5462 ed175427 2019-05-09 stsp
5463 ed175427 2019-05-09 stsp static const struct got_error *
5464 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5465 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5466 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5467 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5468 afa376bf 2019-05-09 stsp struct got_repository *repo)
5469 ed175427 2019-05-09 stsp {
5470 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5471 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5472 036813ee 2019-05-09 stsp char *subpath;
5473 ed175427 2019-05-09 stsp
5474 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5475 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5476 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5477 ed175427 2019-05-09 stsp
5478 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5479 ed175427 2019-05-09 stsp if (err)
5480 ed175427 2019-05-09 stsp return err;
5481 ed175427 2019-05-09 stsp
5482 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5483 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5484 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5485 036813ee 2019-05-09 stsp free(subpath);
5486 036813ee 2019-05-09 stsp return err;
5487 036813ee 2019-05-09 stsp }
5488 ed175427 2019-05-09 stsp
5489 036813ee 2019-05-09 stsp static const struct got_error *
5490 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5491 036813ee 2019-05-09 stsp {
5492 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5493 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5494 ed175427 2019-05-09 stsp
5495 036813ee 2019-05-09 stsp *match = 0;
5496 ed175427 2019-05-09 stsp
5497 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5498 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5499 0f63689d 2019-05-10 stsp return NULL;
5500 036813ee 2019-05-09 stsp }
5501 0b5cc0d6 2019-05-09 stsp
5502 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5503 0f63689d 2019-05-10 stsp if (err)
5504 0f63689d 2019-05-10 stsp return err;
5505 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5506 036813ee 2019-05-09 stsp free(ct_parent_path);
5507 036813ee 2019-05-09 stsp return err;
5508 036813ee 2019-05-09 stsp }
5509 0b5cc0d6 2019-05-09 stsp
5510 768aea60 2019-05-09 stsp static mode_t
5511 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5512 768aea60 2019-05-09 stsp {
5513 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5514 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5515 3d9a4ec4 2020-07-23 stsp
5516 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5517 768aea60 2019-05-09 stsp }
5518 768aea60 2019-05-09 stsp
5519 036813ee 2019-05-09 stsp static const struct got_error *
5520 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5521 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5522 036813ee 2019-05-09 stsp {
5523 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5524 ca2503ea 2019-05-09 stsp
5525 036813ee 2019-05-09 stsp *new_te = NULL;
5526 0b5cc0d6 2019-05-09 stsp
5527 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5528 036813ee 2019-05-09 stsp if (err)
5529 036813ee 2019-05-09 stsp goto done;
5530 0b5cc0d6 2019-05-09 stsp
5531 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5532 036813ee 2019-05-09 stsp
5533 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5534 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5535 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5536 5f8a88c6 2019-08-03 stsp else
5537 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5538 036813ee 2019-05-09 stsp done:
5539 036813ee 2019-05-09 stsp if (err && *new_te) {
5540 56e0773d 2019-11-28 stsp free(*new_te);
5541 036813ee 2019-05-09 stsp *new_te = NULL;
5542 036813ee 2019-05-09 stsp }
5543 036813ee 2019-05-09 stsp return err;
5544 036813ee 2019-05-09 stsp }
5545 036813ee 2019-05-09 stsp
5546 036813ee 2019-05-09 stsp static const struct got_error *
5547 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5548 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5549 036813ee 2019-05-09 stsp {
5550 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5551 102b254e 2020-10-19 stsp char *ct_name = NULL;
5552 036813ee 2019-05-09 stsp
5553 036813ee 2019-05-09 stsp *new_te = NULL;
5554 036813ee 2019-05-09 stsp
5555 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5556 036813ee 2019-05-09 stsp if (*new_te == NULL)
5557 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5558 036813ee 2019-05-09 stsp
5559 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5560 102b254e 2020-10-19 stsp if (err)
5561 036813ee 2019-05-09 stsp goto done;
5562 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5563 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5564 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5565 036813ee 2019-05-09 stsp goto done;
5566 036813ee 2019-05-09 stsp }
5567 036813ee 2019-05-09 stsp
5568 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5569 036813ee 2019-05-09 stsp
5570 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5571 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5572 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5573 5f8a88c6 2019-08-03 stsp else
5574 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5575 036813ee 2019-05-09 stsp done:
5576 102b254e 2020-10-19 stsp free(ct_name);
5577 036813ee 2019-05-09 stsp if (err && *new_te) {
5578 56e0773d 2019-11-28 stsp free(*new_te);
5579 036813ee 2019-05-09 stsp *new_te = NULL;
5580 036813ee 2019-05-09 stsp }
5581 036813ee 2019-05-09 stsp return err;
5582 036813ee 2019-05-09 stsp }
5583 036813ee 2019-05-09 stsp
5584 036813ee 2019-05-09 stsp static const struct got_error *
5585 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5586 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5587 036813ee 2019-05-09 stsp {
5588 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5589 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5590 036813ee 2019-05-09 stsp
5591 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5592 036813ee 2019-05-09 stsp if (err)
5593 036813ee 2019-05-09 stsp return err;
5594 036813ee 2019-05-09 stsp if (new_pe == NULL)
5595 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5596 036813ee 2019-05-09 stsp return NULL;
5597 afa376bf 2019-05-09 stsp }
5598 afa376bf 2019-05-09 stsp
5599 afa376bf 2019-05-09 stsp static const struct got_error *
5600 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5601 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5602 afa376bf 2019-05-09 stsp {
5603 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5604 5f8a88c6 2019-08-03 stsp unsigned char status;
5605 ae1e948a 2021-09-28 thomas
5606 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5607 ae1e948a 2021-09-28 thomas return NULL;
5608 5f8a88c6 2019-08-03 stsp
5609 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5610 afa376bf 2019-05-09 stsp ct_path++;
5611 5f8a88c6 2019-08-03 stsp
5612 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5613 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5614 5f8a88c6 2019-08-03 stsp else
5615 5f8a88c6 2019-08-03 stsp status = ct->status;
5616 5f8a88c6 2019-08-03 stsp
5617 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5618 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5619 036813ee 2019-05-09 stsp }
5620 036813ee 2019-05-09 stsp
5621 036813ee 2019-05-09 stsp static const struct got_error *
5622 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5623 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5624 44d03001 2019-05-09 stsp {
5625 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5626 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5627 44d03001 2019-05-09 stsp char *te_path;
5628 44d03001 2019-05-09 stsp
5629 44d03001 2019-05-09 stsp *modified = 0;
5630 44d03001 2019-05-09 stsp
5631 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5632 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5633 44d03001 2019-05-09 stsp te->name) == -1)
5634 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5635 44d03001 2019-05-09 stsp
5636 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5637 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5638 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5639 44d03001 2019-05-09 stsp strlen(te_path));
5640 62d463ca 2020-10-20 naddy if (*modified)
5641 44d03001 2019-05-09 stsp break;
5642 44d03001 2019-05-09 stsp }
5643 44d03001 2019-05-09 stsp
5644 44d03001 2019-05-09 stsp free(te_path);
5645 44d03001 2019-05-09 stsp return err;
5646 44d03001 2019-05-09 stsp }
5647 44d03001 2019-05-09 stsp
5648 44d03001 2019-05-09 stsp static const struct got_error *
5649 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5650 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5651 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5652 036813ee 2019-05-09 stsp {
5653 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5654 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5655 036813ee 2019-05-09 stsp
5656 036813ee 2019-05-09 stsp *ctp = NULL;
5657 036813ee 2019-05-09 stsp
5658 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5659 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5660 036813ee 2019-05-09 stsp char *ct_name = NULL;
5661 036813ee 2019-05-09 stsp int path_matches;
5662 036813ee 2019-05-09 stsp
5663 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5664 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5665 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5666 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5667 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5668 5f8a88c6 2019-08-03 stsp continue;
5669 5f8a88c6 2019-08-03 stsp } else {
5670 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5671 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5672 5f8a88c6 2019-08-03 stsp continue;
5673 5f8a88c6 2019-08-03 stsp }
5674 036813ee 2019-05-09 stsp
5675 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5676 036813ee 2019-05-09 stsp continue;
5677 036813ee 2019-05-09 stsp
5678 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5679 62d463ca 2020-10-20 naddy if (err)
5680 036813ee 2019-05-09 stsp return err;
5681 036813ee 2019-05-09 stsp if (!path_matches)
5682 036813ee 2019-05-09 stsp continue;
5683 036813ee 2019-05-09 stsp
5684 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5685 d34b633e 2020-10-19 stsp if (err)
5686 d34b633e 2020-10-19 stsp return err;
5687 d34b633e 2020-10-19 stsp
5688 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5689 d34b633e 2020-10-19 stsp free(ct_name);
5690 036813ee 2019-05-09 stsp continue;
5691 d34b633e 2020-10-19 stsp }
5692 d34b633e 2020-10-19 stsp free(ct_name);
5693 036813ee 2019-05-09 stsp
5694 036813ee 2019-05-09 stsp *ctp = ct;
5695 036813ee 2019-05-09 stsp break;
5696 036813ee 2019-05-09 stsp }
5697 2b496619 2019-07-10 stsp
5698 2b496619 2019-07-10 stsp return err;
5699 2b496619 2019-07-10 stsp }
5700 2b496619 2019-07-10 stsp
5701 2b496619 2019-07-10 stsp static const struct got_error *
5702 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5703 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5704 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5705 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5706 2b496619 2019-07-10 stsp struct got_repository *repo)
5707 2b496619 2019-07-10 stsp {
5708 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5709 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5710 2b496619 2019-07-10 stsp char *subtree_path;
5711 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5712 ba580f68 2020-03-22 stsp int nentries;
5713 2b496619 2019-07-10 stsp
5714 2b496619 2019-07-10 stsp *new_tep = NULL;
5715 2b496619 2019-07-10 stsp
5716 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5717 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5718 2b496619 2019-07-10 stsp child_path) == -1)
5719 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5720 036813ee 2019-05-09 stsp
5721 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5722 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5723 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5724 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5725 56e0773d 2019-11-28 stsp
5726 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5727 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5728 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5729 2b496619 2019-07-10 stsp goto done;
5730 2b496619 2019-07-10 stsp }
5731 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5732 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5733 2b496619 2019-07-10 stsp if (err) {
5734 56e0773d 2019-11-28 stsp free(new_te);
5735 2b496619 2019-07-10 stsp goto done;
5736 2b496619 2019-07-10 stsp }
5737 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5738 2b496619 2019-07-10 stsp done:
5739 56e0773d 2019-11-28 stsp free(id);
5740 2b496619 2019-07-10 stsp free(subtree_path);
5741 2b496619 2019-07-10 stsp if (err == NULL)
5742 2b496619 2019-07-10 stsp *new_tep = new_te;
5743 036813ee 2019-05-09 stsp return err;
5744 036813ee 2019-05-09 stsp }
5745 036813ee 2019-05-09 stsp
5746 036813ee 2019-05-09 stsp static const struct got_error *
5747 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5748 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5749 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5750 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5751 036813ee 2019-05-09 stsp struct got_repository *repo)
5752 036813ee 2019-05-09 stsp {
5753 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5754 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5755 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5756 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5757 036813ee 2019-05-09 stsp
5758 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5759 ba580f68 2020-03-22 stsp *nentries = 0;
5760 036813ee 2019-05-09 stsp
5761 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5762 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5763 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5764 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5765 036813ee 2019-05-09 stsp
5766 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5767 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5768 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5769 036813ee 2019-05-09 stsp continue;
5770 036813ee 2019-05-09 stsp
5771 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5772 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5773 036813ee 2019-05-09 stsp continue;
5774 036813ee 2019-05-09 stsp
5775 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5776 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5777 036813ee 2019-05-09 stsp if (err)
5778 036813ee 2019-05-09 stsp goto done;
5779 036813ee 2019-05-09 stsp
5780 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5781 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5782 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5783 036813ee 2019-05-09 stsp if (err)
5784 036813ee 2019-05-09 stsp goto done;
5785 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5786 afa376bf 2019-05-09 stsp if (err)
5787 afa376bf 2019-05-09 stsp goto done;
5788 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5789 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5790 2b496619 2019-07-10 stsp if (err)
5791 2f51b5b3 2019-05-09 stsp goto done;
5792 ba580f68 2020-03-22 stsp (*nentries)++;
5793 2b496619 2019-07-10 stsp } else {
5794 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5795 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5796 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5797 2b496619 2019-07-10 stsp == NULL) {
5798 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5799 2b496619 2019-07-10 stsp child_path, path_base_tree,
5800 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5801 2b496619 2019-07-10 stsp repo);
5802 2b496619 2019-07-10 stsp if (err)
5803 2b496619 2019-07-10 stsp goto done;
5804 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5805 2b496619 2019-07-10 stsp if (err)
5806 2b496619 2019-07-10 stsp goto done;
5807 ba580f68 2020-03-22 stsp (*nentries)++;
5808 9ba0479c 2019-05-10 stsp }
5809 ed175427 2019-05-09 stsp }
5810 2f51b5b3 2019-05-09 stsp }
5811 2f51b5b3 2019-05-09 stsp
5812 2f51b5b3 2019-05-09 stsp if (base_tree) {
5813 56e0773d 2019-11-28 stsp int i, nbase_entries;
5814 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5815 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5816 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5817 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5818 63c5ca5d 2019-08-24 stsp
5819 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5820 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5821 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5822 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5823 63c5ca5d 2019-08-24 stsp if (err)
5824 63c5ca5d 2019-08-24 stsp goto done;
5825 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5826 63c5ca5d 2019-08-24 stsp if (err)
5827 63c5ca5d 2019-08-24 stsp goto done;
5828 ba580f68 2020-03-22 stsp (*nentries)++;
5829 63c5ca5d 2019-08-24 stsp continue;
5830 63c5ca5d 2019-08-24 stsp }
5831 2f51b5b3 2019-05-09 stsp
5832 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5833 44d03001 2019-05-09 stsp int modified;
5834 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5835 036813ee 2019-05-09 stsp if (err)
5836 036813ee 2019-05-09 stsp goto done;
5837 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5838 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5839 2f51b5b3 2019-05-09 stsp if (err)
5840 2f51b5b3 2019-05-09 stsp goto done;
5841 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5842 44d03001 2019-05-09 stsp if (modified) {
5843 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5844 ba580f68 2020-03-22 stsp int nsubentries;
5845 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5846 ba580f68 2020-03-22 stsp &nsubentries, te,
5847 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5848 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5849 44d03001 2019-05-09 stsp if (err)
5850 44d03001 2019-05-09 stsp goto done;
5851 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5852 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5853 ba580f68 2020-03-22 stsp free(new_id);
5854 ba580f68 2020-03-22 stsp continue;
5855 ba580f68 2020-03-22 stsp }
5856 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5857 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5858 56e0773d 2019-11-28 stsp free(new_id);
5859 44d03001 2019-05-09 stsp }
5860 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5861 036813ee 2019-05-09 stsp if (err)
5862 036813ee 2019-05-09 stsp goto done;
5863 ba580f68 2020-03-22 stsp (*nentries)++;
5864 2f51b5b3 2019-05-09 stsp continue;
5865 036813ee 2019-05-09 stsp }
5866 2f51b5b3 2019-05-09 stsp
5867 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5868 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5869 f66c734c 2019-09-22 stsp if (err)
5870 f66c734c 2019-09-22 stsp goto done;
5871 2f51b5b3 2019-05-09 stsp if (ct) {
5872 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5873 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5874 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5875 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5876 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5877 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5878 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5879 2f51b5b3 2019-05-09 stsp if (err)
5880 2f51b5b3 2019-05-09 stsp goto done;
5881 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5882 2f51b5b3 2019-05-09 stsp if (err)
5883 2f51b5b3 2019-05-09 stsp goto done;
5884 ba580f68 2020-03-22 stsp (*nentries)++;
5885 2f51b5b3 2019-05-09 stsp }
5886 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5887 b416585c 2019-05-13 stsp status_arg);
5888 afa376bf 2019-05-09 stsp if (err)
5889 afa376bf 2019-05-09 stsp goto done;
5890 2f51b5b3 2019-05-09 stsp } else {
5891 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5892 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5893 2f51b5b3 2019-05-09 stsp if (err)
5894 2f51b5b3 2019-05-09 stsp goto done;
5895 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5896 2f51b5b3 2019-05-09 stsp if (err)
5897 2f51b5b3 2019-05-09 stsp goto done;
5898 ba580f68 2020-03-22 stsp (*nentries)++;
5899 2f51b5b3 2019-05-09 stsp }
5900 ca2503ea 2019-05-09 stsp }
5901 ed175427 2019-05-09 stsp }
5902 0b5cc0d6 2019-05-09 stsp
5903 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5904 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5905 ed175427 2019-05-09 stsp done:
5906 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5907 2e1fa222 2020-07-23 stsp return err;
5908 2e1fa222 2020-07-23 stsp }
5909 2e1fa222 2020-07-23 stsp
5910 2e1fa222 2020-07-23 stsp static const struct got_error *
5911 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5912 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5913 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5914 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5915 ebf99748 2019-05-09 stsp {
5916 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5917 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5918 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5919 ebf99748 2019-05-09 stsp
5920 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5921 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5922 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5923 ebf99748 2019-05-09 stsp
5924 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5925 437adc9d 2020-12-10 yzhong
5926 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5927 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5928 437adc9d 2020-12-10 yzhong if (err)
5929 437adc9d 2020-12-10 yzhong goto done;
5930 437adc9d 2020-12-10 yzhong
5931 ebf99748 2019-05-09 stsp if (ie) {
5932 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5933 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5934 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5935 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5936 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5937 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5938 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5939 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5940 437adc9d 2020-12-10 yzhong
5941 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5942 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5943 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5944 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5945 72fd46fa 2019-09-06 stsp !have_staged_files);
5946 ebf99748 2019-05-09 stsp } else
5947 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5948 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5949 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5950 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5951 72fd46fa 2019-09-06 stsp !have_staged_files);
5952 ebf99748 2019-05-09 stsp } else {
5953 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5954 ebf99748 2019-05-09 stsp if (err)
5955 437adc9d 2020-12-10 yzhong goto done;
5956 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5957 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5958 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5959 3969253a 2020-03-07 stsp if (err) {
5960 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5961 437adc9d 2020-12-10 yzhong goto done;
5962 3969253a 2020-03-07 stsp }
5963 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5964 3969253a 2020-03-07 stsp if (err) {
5965 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5966 437adc9d 2020-12-10 yzhong goto done;
5967 3969253a 2020-03-07 stsp }
5968 ebf99748 2019-05-09 stsp }
5969 437adc9d 2020-12-10 yzhong free(relpath);
5970 437adc9d 2020-12-10 yzhong relpath = NULL;
5971 ebf99748 2019-05-09 stsp }
5972 437adc9d 2020-12-10 yzhong done:
5973 437adc9d 2020-12-10 yzhong free(relpath);
5974 d56d26ce 2019-05-10 stsp return err;
5975 d56d26ce 2019-05-10 stsp }
5976 735ef5ac 2019-08-03 stsp
5977 d56d26ce 2019-05-10 stsp
5978 d56d26ce 2019-05-10 stsp static const struct got_error *
5979 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5980 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5981 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5982 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5983 735ef5ac 2019-08-03 stsp int ood_errcode)
5984 d56d26ce 2019-05-10 stsp {
5985 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5986 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5987 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5988 1a36436d 2019-06-10 stsp
5989 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5990 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5991 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5992 1a36436d 2019-06-10 stsp return NULL;
5993 9bead371 2019-07-28 stsp /*
5994 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5995 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5996 9bead371 2019-07-28 stsp */
5997 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5998 945f9229 2022-04-16 thomas if (err)
5999 945f9229 2022-04-16 thomas goto done;
6000 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6001 9bead371 2019-07-28 stsp if (err) {
6002 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6003 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6004 1a36436d 2019-06-10 stsp goto done;
6005 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6006 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6007 1a36436d 2019-06-10 stsp } else {
6008 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6009 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6010 945f9229 2022-04-16 thomas if (err)
6011 945f9229 2022-04-16 thomas goto done;
6012 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6013 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6014 1a36436d 2019-06-10 stsp goto done;
6015 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6016 1a36436d 2019-06-10 stsp }
6017 1a36436d 2019-06-10 stsp done:
6018 1a36436d 2019-06-10 stsp free(id);
6019 945f9229 2022-04-16 thomas if (commit)
6020 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6021 1a36436d 2019-06-10 stsp return err;
6022 ebf99748 2019-05-09 stsp }
6023 ebf99748 2019-05-09 stsp
6024 ef20f542 2022-06-26 thomas static const struct got_error *
6025 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6026 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6027 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6028 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6029 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6030 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6031 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6032 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6033 afa376bf 2019-05-09 stsp struct got_repository *repo)
6034 c4296144 2019-05-09 stsp {
6035 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6036 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6037 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6038 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6039 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6040 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6041 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6042 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6043 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6044 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6045 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6046 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6047 892eab0a 2022-07-22 thomas time_t timestamp;
6048 c4296144 2019-05-09 stsp
6049 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6050 c4296144 2019-05-09 stsp
6051 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6052 675c7539 2019-05-09 stsp
6053 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6054 588edf97 2019-05-10 stsp if (err)
6055 588edf97 2019-05-10 stsp goto done;
6056 de18fc63 2019-05-09 stsp
6057 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6058 588edf97 2019-05-10 stsp if (err)
6059 588edf97 2019-05-10 stsp goto done;
6060 588edf97 2019-05-10 stsp
6061 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6062 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6063 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6064 33ad4cbe 2019-05-12 jcs if (err)
6065 33ad4cbe 2019-05-12 jcs goto done;
6066 33ad4cbe 2019-05-12 jcs }
6067 c4296144 2019-05-09 stsp
6068 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6069 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6070 33ad4cbe 2019-05-12 jcs goto done;
6071 33ad4cbe 2019-05-12 jcs }
6072 33ad4cbe 2019-05-12 jcs
6073 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6074 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6075 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6076 cf066bf8 2019-05-09 stsp char *ondisk_path;
6077 cf066bf8 2019-05-09 stsp
6078 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6079 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6080 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6081 5f8a88c6 2019-08-03 stsp continue;
6082 5f8a88c6 2019-08-03 stsp
6083 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6084 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6085 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6086 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6087 cf066bf8 2019-05-09 stsp continue;
6088 cf066bf8 2019-05-09 stsp
6089 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6090 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6091 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6092 cf066bf8 2019-05-09 stsp goto done;
6093 cf066bf8 2019-05-09 stsp }
6094 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6095 2e1fa222 2020-07-23 stsp free(ondisk_path);
6096 2e1fa222 2020-07-23 stsp if (err)
6097 cf066bf8 2019-05-09 stsp goto done;
6098 cf066bf8 2019-05-09 stsp }
6099 036813ee 2019-05-09 stsp
6100 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6101 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6102 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6103 036813ee 2019-05-09 stsp if (err)
6104 036813ee 2019-05-09 stsp goto done;
6105 036813ee 2019-05-09 stsp
6106 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6107 de18fc63 2019-05-09 stsp if (err)
6108 de18fc63 2019-05-09 stsp goto done;
6109 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6110 10604dce 2021-09-24 thomas nparents++;
6111 10604dce 2021-09-24 thomas if (parent_id2) {
6112 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6113 10604dce 2021-09-24 thomas if (err)
6114 10604dce 2021-09-24 thomas goto done;
6115 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6116 10604dce 2021-09-24 thomas nparents++;
6117 10604dce 2021-09-24 thomas }
6118 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6119 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6120 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6121 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6122 33ad4cbe 2019-05-12 jcs free(logmsg);
6123 9d40349a 2019-05-09 stsp if (err)
6124 9d40349a 2019-05-09 stsp goto done;
6125 9d40349a 2019-05-09 stsp
6126 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6127 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6128 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6129 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6130 09f5bd90 2019-05-09 stsp goto done;
6131 09f5bd90 2019-05-09 stsp }
6132 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6133 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6134 09f5bd90 2019-05-09 stsp if (err)
6135 09f5bd90 2019-05-09 stsp goto done;
6136 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6137 ebf99748 2019-05-09 stsp if (err)
6138 ebf99748 2019-05-09 stsp goto done;
6139 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6140 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6141 09f5bd90 2019-05-09 stsp goto done;
6142 09f5bd90 2019-05-09 stsp }
6143 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6144 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6145 f2c16586 2019-05-09 stsp if (err)
6146 f2c16586 2019-05-09 stsp goto done;
6147 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6148 f2c16586 2019-05-09 stsp if (err)
6149 f2c16586 2019-05-09 stsp goto done;
6150 f2c16586 2019-05-09 stsp
6151 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6152 09f5bd90 2019-05-09 stsp if (err)
6153 09f5bd90 2019-05-09 stsp goto done;
6154 09f5bd90 2019-05-09 stsp
6155 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6156 ebf99748 2019-05-09 stsp if (err)
6157 ebf99748 2019-05-09 stsp goto done;
6158 c4296144 2019-05-09 stsp done:
6159 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6160 588edf97 2019-05-10 stsp if (head_tree)
6161 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6162 588edf97 2019-05-10 stsp if (head_commit)
6163 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6164 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6165 2f17228e 2019-05-12 stsp if (head_ref2) {
6166 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6167 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6168 2f17228e 2019-05-12 stsp err = unlockerr;
6169 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6170 39cd0ff6 2019-07-12 stsp }
6171 39cd0ff6 2019-07-12 stsp return err;
6172 39cd0ff6 2019-07-12 stsp }
6173 5c1e53bc 2019-07-28 stsp
6174 5c1e53bc 2019-07-28 stsp static const struct got_error *
6175 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6176 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6177 5c1e53bc 2019-07-28 stsp {
6178 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6179 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6180 39cd0ff6 2019-07-12 stsp
6181 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6182 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6183 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6184 5c1e53bc 2019-07-28 stsp
6185 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6186 5c1e53bc 2019-07-28 stsp ct_path++;
6187 5c1e53bc 2019-07-28 stsp
6188 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6189 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6190 5c1e53bc 2019-07-28 stsp break;
6191 5c1e53bc 2019-07-28 stsp }
6192 5c1e53bc 2019-07-28 stsp
6193 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6194 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6195 5c1e53bc 2019-07-28 stsp
6196 5c1e53bc 2019-07-28 stsp return NULL;
6197 5c1e53bc 2019-07-28 stsp }
6198 5c1e53bc 2019-07-28 stsp
6199 f0b75401 2019-08-03 stsp static const struct got_error *
6200 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6201 f0b75401 2019-08-03 stsp {
6202 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6203 f0b75401 2019-08-03 stsp
6204 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6205 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6206 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6207 f0b75401 2019-08-03 stsp }
6208 f0b75401 2019-08-03 stsp
6209 f0b75401 2019-08-03 stsp return NULL;
6210 f0b75401 2019-08-03 stsp }
6211 f0b75401 2019-08-03 stsp
6212 5f8a88c6 2019-08-03 stsp static const struct got_error *
6213 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6214 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6215 5f8a88c6 2019-08-03 stsp {
6216 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6217 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6218 5f8a88c6 2019-08-03 stsp
6219 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6220 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6221 5f8a88c6 2019-08-03 stsp continue;
6222 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6223 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6224 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6225 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6226 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6227 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6228 5f8a88c6 2019-08-03 stsp }
6229 5f8a88c6 2019-08-03 stsp
6230 5f8a88c6 2019-08-03 stsp return NULL;
6231 5f8a88c6 2019-08-03 stsp }
6232 5f8a88c6 2019-08-03 stsp
6233 39cd0ff6 2019-07-12 stsp const struct got_error *
6234 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6235 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6236 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6237 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6238 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6239 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6240 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6241 39cd0ff6 2019-07-12 stsp {
6242 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6243 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6244 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6245 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6246 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6247 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6248 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6249 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6250 ef899790 2022-11-01 thomas char *diff_path = NULL;
6251 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6252 39cd0ff6 2019-07-12 stsp
6253 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6254 39cd0ff6 2019-07-12 stsp
6255 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6256 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6257 39cd0ff6 2019-07-12 stsp
6258 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6259 39cd0ff6 2019-07-12 stsp if (err)
6260 39cd0ff6 2019-07-12 stsp goto done;
6261 39cd0ff6 2019-07-12 stsp
6262 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6263 39cd0ff6 2019-07-12 stsp if (err)
6264 39cd0ff6 2019-07-12 stsp goto done;
6265 39cd0ff6 2019-07-12 stsp
6266 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6267 39cd0ff6 2019-07-12 stsp if (err)
6268 39cd0ff6 2019-07-12 stsp goto done;
6269 39cd0ff6 2019-07-12 stsp
6270 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6271 39cd0ff6 2019-07-12 stsp if (err)
6272 39cd0ff6 2019-07-12 stsp goto done;
6273 39cd0ff6 2019-07-12 stsp
6274 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6275 5f8a88c6 2019-08-03 stsp &have_staged_files);
6276 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6277 5f8a88c6 2019-08-03 stsp goto done;
6278 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6279 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6280 5f8a88c6 2019-08-03 stsp if (err)
6281 5f8a88c6 2019-08-03 stsp goto done;
6282 5f8a88c6 2019-08-03 stsp }
6283 5f8a88c6 2019-08-03 stsp
6284 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6285 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6286 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6287 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6288 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6289 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6290 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6291 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6292 ef899790 2022-11-01 thomas if (show_diff) {
6293 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6294 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6295 ef899790 2022-11-01 thomas if (err)
6296 ef899790 2022-11-01 thomas goto done;
6297 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6298 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6299 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6300 ef899790 2022-11-01 thomas goto done;
6301 ef899790 2022-11-01 thomas }
6302 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6303 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6304 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6305 ef899790 2022-11-01 thomas goto done;
6306 ef899790 2022-11-01 thomas }
6307 ef899790 2022-11-01 thomas }
6308 ef899790 2022-11-01 thomas
6309 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6310 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6311 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6312 5c1e53bc 2019-07-28 stsp if (err)
6313 5c1e53bc 2019-07-28 stsp goto done;
6314 5c1e53bc 2019-07-28 stsp }
6315 39cd0ff6 2019-07-12 stsp
6316 ef899790 2022-11-01 thomas if (show_diff) {
6317 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6318 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6319 ef899790 2022-11-01 thomas goto done;
6320 ef899790 2022-11-01 thomas }
6321 ef899790 2022-11-01 thomas }
6322 ef899790 2022-11-01 thomas
6323 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6324 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6325 39cd0ff6 2019-07-12 stsp goto done;
6326 5c1e53bc 2019-07-28 stsp }
6327 5c1e53bc 2019-07-28 stsp
6328 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6329 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6330 5c1e53bc 2019-07-28 stsp if (err)
6331 5c1e53bc 2019-07-28 stsp goto done;
6332 39cd0ff6 2019-07-12 stsp }
6333 f0b75401 2019-08-03 stsp
6334 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6335 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6336 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6337 f0b75401 2019-08-03 stsp
6338 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6339 f0b75401 2019-08-03 stsp ct_path++;
6340 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6341 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6342 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6343 b50cabdf 2019-07-12 stsp if (err)
6344 b50cabdf 2019-07-12 stsp goto done;
6345 f0b75401 2019-08-03 stsp
6346 b50cabdf 2019-07-12 stsp }
6347 b50cabdf 2019-07-12 stsp
6348 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6349 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6350 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6351 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6352 39cd0ff6 2019-07-12 stsp if (err)
6353 39cd0ff6 2019-07-12 stsp goto done;
6354 39cd0ff6 2019-07-12 stsp
6355 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6356 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6357 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6358 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6359 39cd0ff6 2019-07-12 stsp err = sync_err;
6360 39cd0ff6 2019-07-12 stsp done:
6361 39cd0ff6 2019-07-12 stsp if (fileindex)
6362 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6363 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6364 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6365 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6366 39cd0ff6 2019-07-12 stsp err = unlockerr;
6367 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6368 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6369 21c2d8be 2023-01-10 thomas
6370 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6371 39cd0ff6 2019-07-12 stsp }
6372 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6373 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6374 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6375 ef899790 2022-11-01 thomas free(diff_path);
6376 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6377 ef899790 2022-11-01 thomas err == NULL)
6378 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6379 c4296144 2019-05-09 stsp return err;
6380 8656d6c4 2019-05-20 stsp }
6381 8656d6c4 2019-05-20 stsp
6382 8656d6c4 2019-05-20 stsp const char *
6383 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6384 8656d6c4 2019-05-20 stsp {
6385 8656d6c4 2019-05-20 stsp return ct->path;
6386 8656d6c4 2019-05-20 stsp }
6387 8656d6c4 2019-05-20 stsp
6388 8656d6c4 2019-05-20 stsp unsigned int
6389 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6390 8656d6c4 2019-05-20 stsp {
6391 8656d6c4 2019-05-20 stsp return ct->status;
6392 818c7501 2019-07-11 stsp }
6393 818c7501 2019-07-11 stsp
6394 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6395 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6396 818c7501 2019-07-11 stsp struct got_repository *repo;
6397 818c7501 2019-07-11 stsp };
6398 818c7501 2019-07-11 stsp
6399 818c7501 2019-07-11 stsp static const struct got_error *
6400 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6401 818c7501 2019-07-11 stsp {
6402 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6403 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6404 818c7501 2019-07-11 stsp unsigned char status;
6405 818c7501 2019-07-11 stsp struct stat sb;
6406 818c7501 2019-07-11 stsp char *ondisk_path;
6407 818c7501 2019-07-11 stsp
6408 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6409 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6410 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6411 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6412 818c7501 2019-07-11 stsp
6413 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6414 818c7501 2019-07-11 stsp == -1)
6415 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6416 818c7501 2019-07-11 stsp
6417 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6418 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6419 818c7501 2019-07-11 stsp free(ondisk_path);
6420 818c7501 2019-07-11 stsp if (err)
6421 818c7501 2019-07-11 stsp return err;
6422 818c7501 2019-07-11 stsp
6423 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6424 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6425 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6426 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6427 818c7501 2019-07-11 stsp
6428 818c7501 2019-07-11 stsp return NULL;
6429 818c7501 2019-07-11 stsp }
6430 818c7501 2019-07-11 stsp
6431 818c7501 2019-07-11 stsp const struct got_error *
6432 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6433 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6434 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6435 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6436 818c7501 2019-07-11 stsp {
6437 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6438 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6439 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6440 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6441 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6442 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6443 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6444 818c7501 2019-07-11 stsp
6445 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6446 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6447 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6448 818c7501 2019-07-11 stsp
6449 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6450 818c7501 2019-07-11 stsp if (err)
6451 818c7501 2019-07-11 stsp return err;
6452 818c7501 2019-07-11 stsp
6453 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6454 818c7501 2019-07-11 stsp if (err)
6455 818c7501 2019-07-11 stsp goto done;
6456 818c7501 2019-07-11 stsp
6457 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6458 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6459 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6460 818c7501 2019-07-11 stsp &ok_arg);
6461 818c7501 2019-07-11 stsp if (err)
6462 818c7501 2019-07-11 stsp goto done;
6463 818c7501 2019-07-11 stsp
6464 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6465 818c7501 2019-07-11 stsp if (err)
6466 818c7501 2019-07-11 stsp goto done;
6467 818c7501 2019-07-11 stsp
6468 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6469 818c7501 2019-07-11 stsp if (err)
6470 818c7501 2019-07-11 stsp goto done;
6471 818c7501 2019-07-11 stsp
6472 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6473 818c7501 2019-07-11 stsp if (err)
6474 818c7501 2019-07-11 stsp goto done;
6475 818c7501 2019-07-11 stsp
6476 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6477 818c7501 2019-07-11 stsp 0);
6478 e51d7b55 2020-01-04 stsp if (err)
6479 e51d7b55 2020-01-04 stsp goto done;
6480 e51d7b55 2020-01-04 stsp
6481 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6482 818c7501 2019-07-11 stsp if (err)
6483 818c7501 2019-07-11 stsp goto done;
6484 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6485 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6486 e51d7b55 2020-01-04 stsp goto done;
6487 e51d7b55 2020-01-04 stsp }
6488 818c7501 2019-07-11 stsp
6489 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6490 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6491 818c7501 2019-07-11 stsp if (err)
6492 818c7501 2019-07-11 stsp goto done;
6493 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6494 818c7501 2019-07-11 stsp if (err)
6495 818c7501 2019-07-11 stsp goto done;
6496 818c7501 2019-07-11 stsp
6497 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6498 818c7501 2019-07-11 stsp
6499 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6500 818c7501 2019-07-11 stsp if (err)
6501 818c7501 2019-07-11 stsp goto done;
6502 818c7501 2019-07-11 stsp
6503 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6504 818c7501 2019-07-11 stsp if (err)
6505 818c7501 2019-07-11 stsp goto done;
6506 818c7501 2019-07-11 stsp
6507 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6508 818c7501 2019-07-11 stsp worktree->base_commit_id);
6509 818c7501 2019-07-11 stsp if (err)
6510 818c7501 2019-07-11 stsp goto done;
6511 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6512 818c7501 2019-07-11 stsp if (err)
6513 818c7501 2019-07-11 stsp goto done;
6514 818c7501 2019-07-11 stsp
6515 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6516 818c7501 2019-07-11 stsp if (err)
6517 818c7501 2019-07-11 stsp goto done;
6518 818c7501 2019-07-11 stsp done:
6519 818c7501 2019-07-11 stsp free(fileindex_path);
6520 818c7501 2019-07-11 stsp free(tmp_branch_name);
6521 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6522 818c7501 2019-07-11 stsp free(branch_ref_name);
6523 818c7501 2019-07-11 stsp if (branch_ref)
6524 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6525 818c7501 2019-07-11 stsp if (wt_branch)
6526 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6527 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6528 818c7501 2019-07-11 stsp if (err) {
6529 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6530 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6531 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6532 818c7501 2019-07-11 stsp }
6533 818c7501 2019-07-11 stsp if (*tmp_branch) {
6534 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6535 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6536 818c7501 2019-07-11 stsp }
6537 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6538 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6539 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6540 3e3a69f1 2019-07-25 stsp }
6541 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6542 818c7501 2019-07-11 stsp }
6543 818c7501 2019-07-11 stsp return err;
6544 818c7501 2019-07-11 stsp }
6545 818c7501 2019-07-11 stsp
6546 818c7501 2019-07-11 stsp const struct got_error *
6547 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6548 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6549 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6550 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6551 818c7501 2019-07-11 stsp {
6552 818c7501 2019-07-11 stsp const struct got_error *err;
6553 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6554 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6555 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6556 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6557 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6558 818c7501 2019-07-11 stsp
6559 818c7501 2019-07-11 stsp *commit_id = NULL;
6560 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6561 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6562 3e3a69f1 2019-07-25 stsp *branch = NULL;
6563 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6564 3e3a69f1 2019-07-25 stsp
6565 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6566 3e3a69f1 2019-07-25 stsp if (err)
6567 3e3a69f1 2019-07-25 stsp return err;
6568 818c7501 2019-07-11 stsp
6569 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6570 3e3a69f1 2019-07-25 stsp if (err)
6571 f032f1f7 2019-08-04 stsp goto done;
6572 f032f1f7 2019-08-04 stsp
6573 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6574 f032f1f7 2019-08-04 stsp &have_staged_files);
6575 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6576 f032f1f7 2019-08-04 stsp goto done;
6577 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6578 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6579 3e3a69f1 2019-07-25 stsp goto done;
6580 f032f1f7 2019-08-04 stsp }
6581 3e3a69f1 2019-07-25 stsp
6582 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6583 818c7501 2019-07-11 stsp if (err)
6584 f032f1f7 2019-08-04 stsp goto done;
6585 818c7501 2019-07-11 stsp
6586 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6587 818c7501 2019-07-11 stsp if (err)
6588 818c7501 2019-07-11 stsp goto done;
6589 818c7501 2019-07-11 stsp
6590 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6591 818c7501 2019-07-11 stsp if (err)
6592 818c7501 2019-07-11 stsp goto done;
6593 818c7501 2019-07-11 stsp
6594 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6595 818c7501 2019-07-11 stsp if (err)
6596 818c7501 2019-07-11 stsp goto done;
6597 818c7501 2019-07-11 stsp
6598 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6599 818c7501 2019-07-11 stsp if (err)
6600 818c7501 2019-07-11 stsp goto done;
6601 818c7501 2019-07-11 stsp
6602 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6603 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6604 818c7501 2019-07-11 stsp if (err)
6605 818c7501 2019-07-11 stsp goto done;
6606 818c7501 2019-07-11 stsp
6607 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6608 818c7501 2019-07-11 stsp if (err)
6609 818c7501 2019-07-11 stsp goto done;
6610 818c7501 2019-07-11 stsp
6611 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6612 818c7501 2019-07-11 stsp if (err)
6613 818c7501 2019-07-11 stsp goto done;
6614 818c7501 2019-07-11 stsp
6615 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6616 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6617 818c7501 2019-07-11 stsp if (err)
6618 818c7501 2019-07-11 stsp goto done;
6619 818c7501 2019-07-11 stsp
6620 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6621 818c7501 2019-07-11 stsp if (err)
6622 818c7501 2019-07-11 stsp goto done;
6623 818c7501 2019-07-11 stsp done:
6624 818c7501 2019-07-11 stsp free(commit_ref_name);
6625 818c7501 2019-07-11 stsp free(branch_ref_name);
6626 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6627 818c7501 2019-07-11 stsp if (commit_ref)
6628 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6629 818c7501 2019-07-11 stsp if (branch_ref)
6630 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6631 818c7501 2019-07-11 stsp if (err) {
6632 818c7501 2019-07-11 stsp free(*commit_id);
6633 818c7501 2019-07-11 stsp *commit_id = NULL;
6634 818c7501 2019-07-11 stsp if (*tmp_branch) {
6635 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6636 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6637 818c7501 2019-07-11 stsp }
6638 818c7501 2019-07-11 stsp if (*new_base_branch) {
6639 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6640 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6641 818c7501 2019-07-11 stsp }
6642 818c7501 2019-07-11 stsp if (*branch) {
6643 818c7501 2019-07-11 stsp got_ref_close(*branch);
6644 818c7501 2019-07-11 stsp *branch = NULL;
6645 818c7501 2019-07-11 stsp }
6646 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6647 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6648 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6649 3e3a69f1 2019-07-25 stsp }
6650 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6651 818c7501 2019-07-11 stsp }
6652 818c7501 2019-07-11 stsp return err;
6653 c4296144 2019-05-09 stsp }
6654 818c7501 2019-07-11 stsp
6655 818c7501 2019-07-11 stsp const struct got_error *
6656 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6657 818c7501 2019-07-11 stsp {
6658 818c7501 2019-07-11 stsp const struct got_error *err;
6659 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6660 818c7501 2019-07-11 stsp
6661 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6662 818c7501 2019-07-11 stsp if (err)
6663 818c7501 2019-07-11 stsp return err;
6664 818c7501 2019-07-11 stsp
6665 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6666 818c7501 2019-07-11 stsp free(tmp_branch_name);
6667 818c7501 2019-07-11 stsp return NULL;
6668 818c7501 2019-07-11 stsp }
6669 818c7501 2019-07-11 stsp
6670 818c7501 2019-07-11 stsp static const struct got_error *
6671 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6672 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6673 818c7501 2019-07-11 stsp {
6674 0ebf8283 2019-07-24 stsp *logmsg = arg;
6675 818c7501 2019-07-11 stsp return NULL;
6676 818c7501 2019-07-11 stsp }
6677 818c7501 2019-07-11 stsp
6678 818c7501 2019-07-11 stsp static const struct got_error *
6679 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6680 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6681 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6682 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6683 818c7501 2019-07-11 stsp {
6684 818c7501 2019-07-11 stsp return NULL;
6685 01757395 2019-07-12 stsp }
6686 01757395 2019-07-12 stsp
6687 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6688 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6689 01757395 2019-07-12 stsp void *progress_arg;
6690 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6691 01757395 2019-07-12 stsp };
6692 01757395 2019-07-12 stsp
6693 01757395 2019-07-12 stsp static const struct got_error *
6694 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6695 01757395 2019-07-12 stsp {
6696 01757395 2019-07-12 stsp const struct got_error *err;
6697 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6698 01757395 2019-07-12 stsp char *p;
6699 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6700 01757395 2019-07-12 stsp
6701 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6702 01757395 2019-07-12 stsp if (err)
6703 01757395 2019-07-12 stsp return err;
6704 01757395 2019-07-12 stsp
6705 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6706 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6707 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6708 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6709 01757395 2019-07-12 stsp return NULL;
6710 01757395 2019-07-12 stsp
6711 01757395 2019-07-12 stsp p = strdup(path);
6712 01757395 2019-07-12 stsp if (p == NULL)
6713 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6714 01757395 2019-07-12 stsp
6715 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6716 01757395 2019-07-12 stsp if (err || new == NULL)
6717 01757395 2019-07-12 stsp free(p);
6718 01757395 2019-07-12 stsp return err;
6719 818c7501 2019-07-11 stsp }
6720 818c7501 2019-07-11 stsp
6721 0ebf8283 2019-07-24 stsp static const struct got_error *
6722 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6723 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6724 818c7501 2019-07-11 stsp {
6725 818c7501 2019-07-11 stsp const struct got_error *err;
6726 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6727 818c7501 2019-07-11 stsp
6728 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6729 818c7501 2019-07-11 stsp if (err) {
6730 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6731 818c7501 2019-07-11 stsp goto done;
6732 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6733 818c7501 2019-07-11 stsp if (err)
6734 818c7501 2019-07-11 stsp goto done;
6735 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6736 818c7501 2019-07-11 stsp if (err)
6737 818c7501 2019-07-11 stsp goto done;
6738 de05890f 2020-03-05 stsp } else if (is_rebase) {
6739 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6740 818c7501 2019-07-11 stsp int cmp;
6741 818c7501 2019-07-11 stsp
6742 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6743 818c7501 2019-07-11 stsp if (err)
6744 818c7501 2019-07-11 stsp goto done;
6745 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6746 818c7501 2019-07-11 stsp free(stored_id);
6747 818c7501 2019-07-11 stsp if (cmp != 0) {
6748 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6749 818c7501 2019-07-11 stsp goto done;
6750 818c7501 2019-07-11 stsp }
6751 818c7501 2019-07-11 stsp }
6752 0ebf8283 2019-07-24 stsp done:
6753 0ebf8283 2019-07-24 stsp if (commit_ref)
6754 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6755 0ebf8283 2019-07-24 stsp return err;
6756 0ebf8283 2019-07-24 stsp }
6757 0ebf8283 2019-07-24 stsp
6758 0ebf8283 2019-07-24 stsp static const struct got_error *
6759 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6760 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6761 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6762 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6763 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6764 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6765 0ebf8283 2019-07-24 stsp {
6766 0ebf8283 2019-07-24 stsp const struct got_error *err;
6767 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6768 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6769 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6770 818c7501 2019-07-11 stsp
6771 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6772 0ebf8283 2019-07-24 stsp
6773 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6774 0ebf8283 2019-07-24 stsp if (err)
6775 0ebf8283 2019-07-24 stsp return err;
6776 0ebf8283 2019-07-24 stsp
6777 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6778 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6779 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6780 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6781 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6782 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6783 818c7501 2019-07-11 stsp if (commit_ref)
6784 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6785 818c7501 2019-07-11 stsp return err;
6786 818c7501 2019-07-11 stsp }
6787 818c7501 2019-07-11 stsp
6788 818c7501 2019-07-11 stsp const struct got_error *
6789 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6790 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6791 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6792 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6793 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6794 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6795 818c7501 2019-07-11 stsp {
6796 0ebf8283 2019-07-24 stsp const struct got_error *err;
6797 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6798 0ebf8283 2019-07-24 stsp
6799 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6800 0ebf8283 2019-07-24 stsp if (err)
6801 0ebf8283 2019-07-24 stsp return err;
6802 0ebf8283 2019-07-24 stsp
6803 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6804 0ebf8283 2019-07-24 stsp if (err)
6805 0ebf8283 2019-07-24 stsp goto done;
6806 0ebf8283 2019-07-24 stsp
6807 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6808 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6809 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6810 0ebf8283 2019-07-24 stsp done:
6811 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6812 0ebf8283 2019-07-24 stsp return err;
6813 0ebf8283 2019-07-24 stsp }
6814 0ebf8283 2019-07-24 stsp
6815 0ebf8283 2019-07-24 stsp const struct got_error *
6816 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6817 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6818 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6819 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6820 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6821 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6822 0ebf8283 2019-07-24 stsp {
6823 0ebf8283 2019-07-24 stsp const struct got_error *err;
6824 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6825 0ebf8283 2019-07-24 stsp
6826 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6827 0ebf8283 2019-07-24 stsp if (err)
6828 0ebf8283 2019-07-24 stsp return err;
6829 0ebf8283 2019-07-24 stsp
6830 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6831 0ebf8283 2019-07-24 stsp if (err)
6832 0ebf8283 2019-07-24 stsp goto done;
6833 0ebf8283 2019-07-24 stsp
6834 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6835 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6836 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6837 0ebf8283 2019-07-24 stsp done:
6838 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6839 0ebf8283 2019-07-24 stsp return err;
6840 0ebf8283 2019-07-24 stsp }
6841 0ebf8283 2019-07-24 stsp
6842 0ebf8283 2019-07-24 stsp static const struct got_error *
6843 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6844 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6845 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6846 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6847 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6848 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6849 0ebf8283 2019-07-24 stsp {
6850 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6851 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6852 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6853 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6854 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6855 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6856 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6857 818c7501 2019-07-11 stsp
6858 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6859 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6860 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6861 a0e95631 2019-07-12 stsp
6862 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6863 818c7501 2019-07-11 stsp
6864 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6865 a0e95631 2019-07-12 stsp if (err)
6866 3e3a69f1 2019-07-25 stsp return err;
6867 a0e95631 2019-07-12 stsp
6868 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6869 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6870 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6871 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6872 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6873 01757395 2019-07-12 stsp /*
6874 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6875 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6876 6c13b005 2021-09-02 stsp *
6877 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6878 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6879 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6880 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6881 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6882 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6883 01757395 2019-07-12 stsp */
6884 01757395 2019-07-12 stsp if (merged_paths) {
6885 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6886 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6887 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6888 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6889 f2a9dc41 2019-12-13 tracey 0);
6890 01757395 2019-07-12 stsp if (err)
6891 01757395 2019-07-12 stsp goto done;
6892 01757395 2019-07-12 stsp }
6893 01757395 2019-07-12 stsp } else {
6894 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6895 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6896 01757395 2019-07-12 stsp if (err)
6897 01757395 2019-07-12 stsp goto done;
6898 01757395 2019-07-12 stsp }
6899 a0e95631 2019-07-12 stsp
6900 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6901 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6902 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6903 a0e95631 2019-07-12 stsp if (err)
6904 a0e95631 2019-07-12 stsp goto done;
6905 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6906 a0e95631 2019-07-12 stsp goto done;
6907 ff0d2220 2019-07-11 stsp }
6908 818c7501 2019-07-11 stsp
6909 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6910 edd02c5e 2019-07-11 stsp if (err)
6911 edd02c5e 2019-07-11 stsp goto done;
6912 edd02c5e 2019-07-11 stsp
6913 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6914 818c7501 2019-07-11 stsp if (err)
6915 818c7501 2019-07-11 stsp goto done;
6916 818c7501 2019-07-11 stsp
6917 5943eee2 2019-08-13 stsp if (new_logmsg) {
6918 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6919 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6920 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6921 5943eee2 2019-08-13 stsp goto done;
6922 5943eee2 2019-08-13 stsp }
6923 5943eee2 2019-08-13 stsp } else {
6924 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6925 5943eee2 2019-08-13 stsp if (err)
6926 5943eee2 2019-08-13 stsp goto done;
6927 5943eee2 2019-08-13 stsp }
6928 0ebf8283 2019-07-24 stsp
6929 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6930 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6931 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6932 8db00f97 2022-07-22 thomas committer ? committer :
6933 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6934 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6935 a0e95631 2019-07-12 stsp if (err)
6936 a0e95631 2019-07-12 stsp goto done;
6937 a0e95631 2019-07-12 stsp
6938 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6939 a0e95631 2019-07-12 stsp if (err)
6940 a0e95631 2019-07-12 stsp goto done;
6941 a0e95631 2019-07-12 stsp
6942 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6943 a0e95631 2019-07-12 stsp if (err)
6944 a0e95631 2019-07-12 stsp goto done;
6945 a0e95631 2019-07-12 stsp
6946 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6947 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6948 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6949 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6950 a0e95631 2019-07-12 stsp err = sync_err;
6951 818c7501 2019-07-11 stsp done:
6952 a0e95631 2019-07-12 stsp free(fileindex_path);
6953 a0e95631 2019-07-12 stsp free(head_commit_id);
6954 a0e95631 2019-07-12 stsp if (head_ref)
6955 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6956 818c7501 2019-07-11 stsp if (err) {
6957 818c7501 2019-07-11 stsp free(*new_commit_id);
6958 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6959 818c7501 2019-07-11 stsp }
6960 818c7501 2019-07-11 stsp return err;
6961 818c7501 2019-07-11 stsp }
6962 818c7501 2019-07-11 stsp
6963 818c7501 2019-07-11 stsp const struct got_error *
6964 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6965 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6966 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6967 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6968 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6969 be94c032 2023-02-20 thomas struct got_repository *repo)
6970 0ebf8283 2019-07-24 stsp {
6971 0ebf8283 2019-07-24 stsp const struct got_error *err;
6972 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6973 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6974 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6975 0ebf8283 2019-07-24 stsp
6976 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6977 0ebf8283 2019-07-24 stsp if (err)
6978 0ebf8283 2019-07-24 stsp return err;
6979 0ebf8283 2019-07-24 stsp
6980 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6981 0ebf8283 2019-07-24 stsp if (err)
6982 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6984 0ebf8283 2019-07-24 stsp if (err)
6985 0ebf8283 2019-07-24 stsp goto done;
6986 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6987 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6988 0ebf8283 2019-07-24 stsp goto done;
6989 0ebf8283 2019-07-24 stsp }
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6992 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6993 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
6994 0ebf8283 2019-07-24 stsp done:
6995 0ebf8283 2019-07-24 stsp if (commit_ref)
6996 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6997 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6998 0ebf8283 2019-07-24 stsp free(commit_id);
6999 0ebf8283 2019-07-24 stsp return err;
7000 0ebf8283 2019-07-24 stsp }
7001 0ebf8283 2019-07-24 stsp
7002 0ebf8283 2019-07-24 stsp const struct got_error *
7003 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7004 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7005 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7006 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7007 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7008 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7009 0ebf8283 2019-07-24 stsp {
7010 0ebf8283 2019-07-24 stsp const struct got_error *err;
7011 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7012 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7015 0ebf8283 2019-07-24 stsp if (err)
7016 0ebf8283 2019-07-24 stsp return err;
7017 0ebf8283 2019-07-24 stsp
7018 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7019 0ebf8283 2019-07-24 stsp if (err)
7020 0ebf8283 2019-07-24 stsp goto done;
7021 0ebf8283 2019-07-24 stsp
7022 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7023 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7024 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7025 0ebf8283 2019-07-24 stsp done:
7026 0ebf8283 2019-07-24 stsp if (commit_ref)
7027 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7028 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7029 0ebf8283 2019-07-24 stsp return err;
7030 0ebf8283 2019-07-24 stsp }
7031 0ebf8283 2019-07-24 stsp
7032 0ebf8283 2019-07-24 stsp const struct got_error *
7033 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7034 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7035 818c7501 2019-07-11 stsp {
7036 3e3a69f1 2019-07-25 stsp if (fileindex)
7037 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7038 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7039 69844fba 2019-07-11 stsp }
7040 69844fba 2019-07-11 stsp
7041 69844fba 2019-07-11 stsp static const struct got_error *
7042 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7043 69844fba 2019-07-11 stsp {
7044 69844fba 2019-07-11 stsp const struct got_error *err;
7045 69844fba 2019-07-11 stsp struct got_reference *ref;
7046 69844fba 2019-07-11 stsp
7047 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7048 69844fba 2019-07-11 stsp if (err) {
7049 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7050 69844fba 2019-07-11 stsp return NULL;
7051 69844fba 2019-07-11 stsp return err;
7052 69844fba 2019-07-11 stsp }
7053 69844fba 2019-07-11 stsp
7054 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7055 69844fba 2019-07-11 stsp got_ref_close(ref);
7056 69844fba 2019-07-11 stsp return err;
7057 818c7501 2019-07-11 stsp }
7058 818c7501 2019-07-11 stsp
7059 69844fba 2019-07-11 stsp static const struct got_error *
7060 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7061 69844fba 2019-07-11 stsp {
7062 69844fba 2019-07-11 stsp const struct got_error *err;
7063 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7064 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7065 69844fba 2019-07-11 stsp
7066 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7067 69844fba 2019-07-11 stsp if (err)
7068 69844fba 2019-07-11 stsp goto done;
7069 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7070 69844fba 2019-07-11 stsp if (err)
7071 69844fba 2019-07-11 stsp goto done;
7072 69844fba 2019-07-11 stsp
7073 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7074 69844fba 2019-07-11 stsp if (err)
7075 69844fba 2019-07-11 stsp goto done;
7076 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7077 69844fba 2019-07-11 stsp if (err)
7078 69844fba 2019-07-11 stsp goto done;
7079 69844fba 2019-07-11 stsp
7080 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7081 69844fba 2019-07-11 stsp if (err)
7082 69844fba 2019-07-11 stsp goto done;
7083 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7084 69844fba 2019-07-11 stsp if (err)
7085 69844fba 2019-07-11 stsp goto done;
7086 69844fba 2019-07-11 stsp
7087 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7088 69844fba 2019-07-11 stsp if (err)
7089 69844fba 2019-07-11 stsp goto done;
7090 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7091 69844fba 2019-07-11 stsp if (err)
7092 69844fba 2019-07-11 stsp goto done;
7093 69844fba 2019-07-11 stsp
7094 69844fba 2019-07-11 stsp done:
7095 69844fba 2019-07-11 stsp free(tmp_branch_name);
7096 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7097 69844fba 2019-07-11 stsp free(branch_ref_name);
7098 69844fba 2019-07-11 stsp free(commit_ref_name);
7099 e600f124 2021-03-21 stsp return err;
7100 e600f124 2021-03-21 stsp }
7101 e600f124 2021-03-21 stsp
7102 ef20f542 2022-06-26 thomas static const struct got_error *
7103 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7104 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7105 e600f124 2021-03-21 stsp {
7106 e600f124 2021-03-21 stsp const struct got_error *err;
7107 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7108 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7109 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7110 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7111 e600f124 2021-03-21 stsp char *refname = NULL;
7112 e600f124 2021-03-21 stsp
7113 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7114 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7115 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7116 e600f124 2021-03-21 stsp branch_name += 11;
7117 e600f124 2021-03-21 stsp
7118 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7119 e600f124 2021-03-21 stsp if (err)
7120 e600f124 2021-03-21 stsp return err;
7121 e600f124 2021-03-21 stsp
7122 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7123 e600f124 2021-03-21 stsp new_id_str) == -1) {
7124 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7125 e600f124 2021-03-21 stsp goto done;
7126 e600f124 2021-03-21 stsp }
7127 e600f124 2021-03-21 stsp
7128 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7129 e600f124 2021-03-21 stsp if (err)
7130 e600f124 2021-03-21 stsp goto done;
7131 e600f124 2021-03-21 stsp
7132 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7133 e600f124 2021-03-21 stsp if (err)
7134 e600f124 2021-03-21 stsp goto done;
7135 e600f124 2021-03-21 stsp
7136 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7137 e600f124 2021-03-21 stsp done:
7138 e600f124 2021-03-21 stsp free(new_id_str);
7139 e600f124 2021-03-21 stsp free(refname);
7140 e600f124 2021-03-21 stsp free(old_commit_id);
7141 e600f124 2021-03-21 stsp if (ref)
7142 e600f124 2021-03-21 stsp got_ref_close(ref);
7143 69844fba 2019-07-11 stsp return err;
7144 69844fba 2019-07-11 stsp }
7145 69844fba 2019-07-11 stsp
7146 818c7501 2019-07-11 stsp const struct got_error *
7147 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7148 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7149 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7150 f08f28be 2023-02-03 thomas int create_backup)
7151 818c7501 2019-07-11 stsp {
7152 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7153 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7154 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7155 818c7501 2019-07-11 stsp
7156 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7157 818c7501 2019-07-11 stsp if (err)
7158 818c7501 2019-07-11 stsp return err;
7159 e600f124 2021-03-21 stsp
7160 e600f124 2021-03-21 stsp if (create_backup) {
7161 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7162 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7163 e600f124 2021-03-21 stsp if (err)
7164 e600f124 2021-03-21 stsp goto done;
7165 e600f124 2021-03-21 stsp }
7166 818c7501 2019-07-11 stsp
7167 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7168 818c7501 2019-07-11 stsp if (err)
7169 818c7501 2019-07-11 stsp goto done;
7170 818c7501 2019-07-11 stsp
7171 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7172 818c7501 2019-07-11 stsp if (err)
7173 818c7501 2019-07-11 stsp goto done;
7174 818c7501 2019-07-11 stsp
7175 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7176 818c7501 2019-07-11 stsp if (err)
7177 818c7501 2019-07-11 stsp goto done;
7178 818c7501 2019-07-11 stsp
7179 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7180 a615e0e7 2020-12-16 stsp if (err)
7181 a615e0e7 2020-12-16 stsp goto done;
7182 a615e0e7 2020-12-16 stsp
7183 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7184 a615e0e7 2020-12-16 stsp if (err)
7185 a615e0e7 2020-12-16 stsp goto done;
7186 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7187 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7188 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7189 a615e0e7 2020-12-16 stsp err = sync_err;
7190 818c7501 2019-07-11 stsp done:
7191 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7192 a615e0e7 2020-12-16 stsp free(fileindex_path);
7193 818c7501 2019-07-11 stsp free(new_head_commit_id);
7194 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7195 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7196 818c7501 2019-07-11 stsp err = unlockerr;
7197 818c7501 2019-07-11 stsp return err;
7198 818c7501 2019-07-11 stsp }
7199 818c7501 2019-07-11 stsp
7200 818c7501 2019-07-11 stsp const struct got_error *
7201 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7202 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7203 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7204 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7205 818c7501 2019-07-11 stsp {
7206 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7207 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7208 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7209 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7210 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7211 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7212 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7213 818c7501 2019-07-11 stsp
7214 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7215 818c7501 2019-07-11 stsp if (err)
7216 818c7501 2019-07-11 stsp return err;
7217 818c7501 2019-07-11 stsp
7218 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7219 945f9229 2022-04-16 thomas worktree->base_commit_id);
7220 945f9229 2022-04-16 thomas if (err)
7221 945f9229 2022-04-16 thomas goto done;
7222 945f9229 2022-04-16 thomas
7223 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7224 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7225 818c7501 2019-07-11 stsp if (err)
7226 818c7501 2019-07-11 stsp goto done;
7227 818c7501 2019-07-11 stsp
7228 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7229 818c7501 2019-07-11 stsp if (err)
7230 818c7501 2019-07-11 stsp goto done;
7231 818c7501 2019-07-11 stsp
7232 818c7501 2019-07-11 stsp /*
7233 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7234 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7235 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7236 818c7501 2019-07-11 stsp */
7237 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7238 818c7501 2019-07-11 stsp if (err)
7239 818c7501 2019-07-11 stsp goto done;
7240 818c7501 2019-07-11 stsp
7241 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7242 818c7501 2019-07-11 stsp if (err)
7243 818c7501 2019-07-11 stsp goto done;
7244 818c7501 2019-07-11 stsp
7245 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7246 945f9229 2022-04-16 thomas worktree->path_prefix);
7247 ca355955 2019-07-12 stsp if (err)
7248 ca355955 2019-07-12 stsp goto done;
7249 ca355955 2019-07-12 stsp
7250 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7251 ca355955 2019-07-12 stsp if (err)
7252 ca355955 2019-07-12 stsp goto done;
7253 ca355955 2019-07-12 stsp
7254 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7255 818c7501 2019-07-11 stsp if (err)
7256 818c7501 2019-07-11 stsp goto done;
7257 818c7501 2019-07-11 stsp
7258 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7259 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7260 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7261 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7262 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7263 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7264 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7265 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7266 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7267 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7268 55bd499d 2019-07-12 stsp if (err)
7269 1f1abb7e 2019-08-08 stsp goto sync;
7270 55bd499d 2019-07-12 stsp
7271 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7272 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7273 ca355955 2019-07-12 stsp sync:
7274 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7275 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7276 ca355955 2019-07-12 stsp err = sync_err;
7277 818c7501 2019-07-11 stsp done:
7278 818c7501 2019-07-11 stsp got_ref_close(resolved);
7279 a3a2faf2 2019-07-12 stsp free(tree_id);
7280 818c7501 2019-07-11 stsp free(commit_id);
7281 945f9229 2022-04-16 thomas if (commit)
7282 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7283 0ebf8283 2019-07-24 stsp if (fileindex)
7284 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7285 0ebf8283 2019-07-24 stsp free(fileindex_path);
7286 0ebf8283 2019-07-24 stsp
7287 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7288 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7289 0ebf8283 2019-07-24 stsp err = unlockerr;
7290 0ebf8283 2019-07-24 stsp return err;
7291 0ebf8283 2019-07-24 stsp }
7292 0ebf8283 2019-07-24 stsp
7293 0ebf8283 2019-07-24 stsp const struct got_error *
7294 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7295 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7296 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7297 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7298 0ebf8283 2019-07-24 stsp {
7299 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7300 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7301 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7302 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7303 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7304 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7305 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7306 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7307 0ebf8283 2019-07-24 stsp
7308 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7309 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7310 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7311 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7312 0ebf8283 2019-07-24 stsp
7313 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7314 0ebf8283 2019-07-24 stsp if (err)
7315 0ebf8283 2019-07-24 stsp return err;
7316 0ebf8283 2019-07-24 stsp
7317 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7318 0ebf8283 2019-07-24 stsp if (err)
7319 0ebf8283 2019-07-24 stsp goto done;
7320 0ebf8283 2019-07-24 stsp
7321 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7322 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7323 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7324 0ebf8283 2019-07-24 stsp &ok_arg);
7325 0ebf8283 2019-07-24 stsp if (err)
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7329 0ebf8283 2019-07-24 stsp if (err)
7330 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp
7332 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7333 0ebf8283 2019-07-24 stsp if (err)
7334 0ebf8283 2019-07-24 stsp goto done;
7335 0ebf8283 2019-07-24 stsp
7336 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7337 0ebf8283 2019-07-24 stsp worktree);
7338 0ebf8283 2019-07-24 stsp if (err)
7339 0ebf8283 2019-07-24 stsp goto done;
7340 0ebf8283 2019-07-24 stsp
7341 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7342 0ebf8283 2019-07-24 stsp 0);
7343 0ebf8283 2019-07-24 stsp if (err)
7344 0ebf8283 2019-07-24 stsp goto done;
7345 0ebf8283 2019-07-24 stsp
7346 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7347 0ebf8283 2019-07-24 stsp if (err)
7348 0ebf8283 2019-07-24 stsp goto done;
7349 0ebf8283 2019-07-24 stsp
7350 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7351 0ebf8283 2019-07-24 stsp if (err)
7352 0ebf8283 2019-07-24 stsp goto done;
7353 0ebf8283 2019-07-24 stsp
7354 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7355 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7356 0ebf8283 2019-07-24 stsp if (err)
7357 0ebf8283 2019-07-24 stsp goto done;
7358 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7359 0ebf8283 2019-07-24 stsp if (err)
7360 0ebf8283 2019-07-24 stsp goto done;
7361 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7362 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7363 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7364 0ebf8283 2019-07-24 stsp goto done;
7365 0ebf8283 2019-07-24 stsp }
7366 0ebf8283 2019-07-24 stsp
7367 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7368 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7369 0ebf8283 2019-07-24 stsp if (err)
7370 0ebf8283 2019-07-24 stsp goto done;
7371 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7372 0ebf8283 2019-07-24 stsp if (err)
7373 0ebf8283 2019-07-24 stsp goto done;
7374 0ebf8283 2019-07-24 stsp
7375 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7376 0ebf8283 2019-07-24 stsp if (err)
7377 0ebf8283 2019-07-24 stsp goto done;
7378 0ebf8283 2019-07-24 stsp done:
7379 0ebf8283 2019-07-24 stsp free(fileindex_path);
7380 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7381 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7382 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7383 0ebf8283 2019-07-24 stsp if (wt_branch)
7384 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7385 0ebf8283 2019-07-24 stsp if (err) {
7386 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7387 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7388 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7389 0ebf8283 2019-07-24 stsp }
7390 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7391 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7392 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7393 0ebf8283 2019-07-24 stsp }
7394 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7395 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7396 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7397 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7398 3e3a69f1 2019-07-25 stsp }
7399 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7400 0ebf8283 2019-07-24 stsp }
7401 0ebf8283 2019-07-24 stsp return err;
7402 0ebf8283 2019-07-24 stsp }
7403 0ebf8283 2019-07-24 stsp
7404 0ebf8283 2019-07-24 stsp const struct got_error *
7405 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7406 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7407 0ebf8283 2019-07-24 stsp {
7408 3e3a69f1 2019-07-25 stsp if (fileindex)
7409 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7410 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7411 0ebf8283 2019-07-24 stsp }
7412 0ebf8283 2019-07-24 stsp
7413 0ebf8283 2019-07-24 stsp const struct got_error *
7414 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7415 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7416 0ebf8283 2019-07-24 stsp {
7417 0ebf8283 2019-07-24 stsp const struct got_error *err;
7418 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7419 0ebf8283 2019-07-24 stsp
7420 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7421 0ebf8283 2019-07-24 stsp if (err)
7422 0ebf8283 2019-07-24 stsp return err;
7423 0ebf8283 2019-07-24 stsp
7424 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7425 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7426 0ebf8283 2019-07-24 stsp return NULL;
7427 0ebf8283 2019-07-24 stsp }
7428 0ebf8283 2019-07-24 stsp
7429 0ebf8283 2019-07-24 stsp const struct got_error *
7430 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7431 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7432 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7433 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7434 0ebf8283 2019-07-24 stsp {
7435 0ebf8283 2019-07-24 stsp const struct got_error *err;
7436 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7437 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7438 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7439 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7440 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7441 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7442 0ebf8283 2019-07-24 stsp
7443 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7444 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7445 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7446 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7447 0ebf8283 2019-07-24 stsp
7448 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7449 3e3a69f1 2019-07-25 stsp if (err)
7450 3e3a69f1 2019-07-25 stsp return err;
7451 3e3a69f1 2019-07-25 stsp
7452 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7453 3e3a69f1 2019-07-25 stsp if (err)
7454 f032f1f7 2019-08-04 stsp goto done;
7455 f032f1f7 2019-08-04 stsp
7456 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7457 f032f1f7 2019-08-04 stsp &have_staged_files);
7458 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7459 f032f1f7 2019-08-04 stsp goto done;
7460 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7461 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7462 3e3a69f1 2019-07-25 stsp goto done;
7463 f032f1f7 2019-08-04 stsp }
7464 3e3a69f1 2019-07-25 stsp
7465 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7466 0ebf8283 2019-07-24 stsp if (err)
7467 f032f1f7 2019-08-04 stsp goto done;
7468 0ebf8283 2019-07-24 stsp
7469 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7470 0ebf8283 2019-07-24 stsp if (err)
7471 0ebf8283 2019-07-24 stsp goto done;
7472 0ebf8283 2019-07-24 stsp
7473 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7474 0ebf8283 2019-07-24 stsp if (err)
7475 0ebf8283 2019-07-24 stsp goto done;
7476 0ebf8283 2019-07-24 stsp
7477 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7478 0ebf8283 2019-07-24 stsp worktree);
7479 0ebf8283 2019-07-24 stsp if (err)
7480 0ebf8283 2019-07-24 stsp goto done;
7481 0ebf8283 2019-07-24 stsp
7482 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7483 0ebf8283 2019-07-24 stsp if (err)
7484 0ebf8283 2019-07-24 stsp goto done;
7485 0ebf8283 2019-07-24 stsp
7486 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7487 0ebf8283 2019-07-24 stsp if (err)
7488 0ebf8283 2019-07-24 stsp goto done;
7489 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7490 0ebf8283 2019-07-24 stsp if (err)
7491 0ebf8283 2019-07-24 stsp goto done;
7492 0ebf8283 2019-07-24 stsp
7493 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7494 0ebf8283 2019-07-24 stsp if (err)
7495 0ebf8283 2019-07-24 stsp goto done;
7496 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7497 0ebf8283 2019-07-24 stsp if (err)
7498 0ebf8283 2019-07-24 stsp goto done;
7499 0ebf8283 2019-07-24 stsp
7500 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7501 0ebf8283 2019-07-24 stsp if (err)
7502 0ebf8283 2019-07-24 stsp goto done;
7503 0ebf8283 2019-07-24 stsp done:
7504 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7505 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7506 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7507 0ebf8283 2019-07-24 stsp if (commit_ref)
7508 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7509 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7510 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7511 0ebf8283 2019-07-24 stsp if (err) {
7512 0ebf8283 2019-07-24 stsp free(*commit_id);
7513 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7514 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7515 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7516 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7517 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7518 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7519 0ebf8283 2019-07-24 stsp }
7520 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7521 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7522 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7523 3e3a69f1 2019-07-25 stsp }
7524 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7525 0ebf8283 2019-07-24 stsp }
7526 0ebf8283 2019-07-24 stsp return err;
7527 0ebf8283 2019-07-24 stsp }
7528 0ebf8283 2019-07-24 stsp
7529 0ebf8283 2019-07-24 stsp static const struct got_error *
7530 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7531 0ebf8283 2019-07-24 stsp {
7532 0ebf8283 2019-07-24 stsp const struct got_error *err;
7533 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7534 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7535 0ebf8283 2019-07-24 stsp
7536 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7537 0ebf8283 2019-07-24 stsp if (err)
7538 0ebf8283 2019-07-24 stsp goto done;
7539 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7540 0ebf8283 2019-07-24 stsp if (err)
7541 0ebf8283 2019-07-24 stsp goto done;
7542 0ebf8283 2019-07-24 stsp
7543 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7544 0ebf8283 2019-07-24 stsp worktree);
7545 0ebf8283 2019-07-24 stsp if (err)
7546 0ebf8283 2019-07-24 stsp goto done;
7547 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7548 0ebf8283 2019-07-24 stsp if (err)
7549 0ebf8283 2019-07-24 stsp goto done;
7550 0ebf8283 2019-07-24 stsp
7551 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7552 0ebf8283 2019-07-24 stsp if (err)
7553 0ebf8283 2019-07-24 stsp goto done;
7554 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7555 0ebf8283 2019-07-24 stsp if (err)
7556 0ebf8283 2019-07-24 stsp goto done;
7557 0ebf8283 2019-07-24 stsp
7558 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7559 0ebf8283 2019-07-24 stsp if (err)
7560 0ebf8283 2019-07-24 stsp goto done;
7561 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7562 0ebf8283 2019-07-24 stsp if (err)
7563 0ebf8283 2019-07-24 stsp goto done;
7564 0ebf8283 2019-07-24 stsp done:
7565 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7566 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7567 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7568 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7569 0ebf8283 2019-07-24 stsp return err;
7570 0ebf8283 2019-07-24 stsp }
7571 0ebf8283 2019-07-24 stsp
7572 0ebf8283 2019-07-24 stsp const struct got_error *
7573 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7574 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7575 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7576 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7577 0ebf8283 2019-07-24 stsp {
7578 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7579 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7580 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7581 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7582 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7583 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7584 0ebf8283 2019-07-24 stsp
7585 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7586 0ebf8283 2019-07-24 stsp if (err)
7587 0ebf8283 2019-07-24 stsp return err;
7588 0ebf8283 2019-07-24 stsp
7589 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7590 945f9229 2022-04-16 thomas worktree->base_commit_id);
7591 945f9229 2022-04-16 thomas if (err)
7592 945f9229 2022-04-16 thomas goto done;
7593 945f9229 2022-04-16 thomas
7594 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7595 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7596 0ebf8283 2019-07-24 stsp if (err)
7597 0ebf8283 2019-07-24 stsp goto done;
7598 0ebf8283 2019-07-24 stsp
7599 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7600 0ebf8283 2019-07-24 stsp if (err)
7601 0ebf8283 2019-07-24 stsp goto done;
7602 0ebf8283 2019-07-24 stsp
7603 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7604 0ebf8283 2019-07-24 stsp if (err)
7605 0ebf8283 2019-07-24 stsp goto done;
7606 0ebf8283 2019-07-24 stsp
7607 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7608 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7609 0ebf8283 2019-07-24 stsp if (err)
7610 0ebf8283 2019-07-24 stsp goto done;
7611 0ebf8283 2019-07-24 stsp
7612 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7613 0ebf8283 2019-07-24 stsp if (err)
7614 0ebf8283 2019-07-24 stsp goto done;
7615 0ebf8283 2019-07-24 stsp
7616 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7617 0ebf8283 2019-07-24 stsp if (err)
7618 0ebf8283 2019-07-24 stsp goto done;
7619 0ebf8283 2019-07-24 stsp
7620 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7621 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7622 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7623 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7624 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7625 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7626 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7627 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7628 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7629 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7630 0ebf8283 2019-07-24 stsp if (err)
7631 1f1abb7e 2019-08-08 stsp goto sync;
7632 0ebf8283 2019-07-24 stsp
7633 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7634 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7635 0ebf8283 2019-07-24 stsp sync:
7636 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7637 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7638 0ebf8283 2019-07-24 stsp err = sync_err;
7639 0ebf8283 2019-07-24 stsp done:
7640 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7641 0ebf8283 2019-07-24 stsp free(tree_id);
7642 818c7501 2019-07-11 stsp free(fileindex_path);
7643 818c7501 2019-07-11 stsp
7644 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7645 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7646 818c7501 2019-07-11 stsp err = unlockerr;
7647 818c7501 2019-07-11 stsp return err;
7648 818c7501 2019-07-11 stsp }
7649 0ebf8283 2019-07-24 stsp
7650 0ebf8283 2019-07-24 stsp const struct got_error *
7651 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7652 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7653 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7654 0ebf8283 2019-07-24 stsp {
7655 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7656 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7657 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7658 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7659 0ebf8283 2019-07-24 stsp
7660 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7661 0ebf8283 2019-07-24 stsp if (err)
7662 0ebf8283 2019-07-24 stsp return err;
7663 0ebf8283 2019-07-24 stsp
7664 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7665 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7666 e600f124 2021-03-21 stsp if (err)
7667 e600f124 2021-03-21 stsp goto done;
7668 e600f124 2021-03-21 stsp
7669 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7670 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
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 = got_ref_change_ref(resolved, new_head_commit_id);
7675 0ebf8283 2019-07-24 stsp if (err)
7676 0ebf8283 2019-07-24 stsp goto done;
7677 0ebf8283 2019-07-24 stsp
7678 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7679 0ebf8283 2019-07-24 stsp if (err)
7680 0ebf8283 2019-07-24 stsp goto done;
7681 0ebf8283 2019-07-24 stsp
7682 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7683 0ebf8283 2019-07-24 stsp if (err)
7684 0ebf8283 2019-07-24 stsp goto done;
7685 0ebf8283 2019-07-24 stsp
7686 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7687 a615e0e7 2020-12-16 stsp if (err)
7688 a615e0e7 2020-12-16 stsp goto done;
7689 a615e0e7 2020-12-16 stsp
7690 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7691 a615e0e7 2020-12-16 stsp if (err)
7692 a615e0e7 2020-12-16 stsp goto done;
7693 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7694 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7695 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7696 a615e0e7 2020-12-16 stsp err = sync_err;
7697 0ebf8283 2019-07-24 stsp done:
7698 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7699 a615e0e7 2020-12-16 stsp free(fileindex_path);
7700 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7701 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7702 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7703 0ebf8283 2019-07-24 stsp err = unlockerr;
7704 0ebf8283 2019-07-24 stsp return err;
7705 0ebf8283 2019-07-24 stsp }
7706 0ebf8283 2019-07-24 stsp
7707 0ebf8283 2019-07-24 stsp const struct got_error *
7708 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7709 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7710 0ebf8283 2019-07-24 stsp {
7711 0ebf8283 2019-07-24 stsp const struct got_error *err;
7712 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7713 0ebf8283 2019-07-24 stsp
7714 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7715 0ebf8283 2019-07-24 stsp if (err)
7716 0ebf8283 2019-07-24 stsp return err;
7717 0ebf8283 2019-07-24 stsp
7718 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7719 0ebf8283 2019-07-24 stsp if (err)
7720 0ebf8283 2019-07-24 stsp goto done;
7721 0ebf8283 2019-07-24 stsp
7722 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7723 0ebf8283 2019-07-24 stsp done:
7724 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7725 2822a352 2019-10-15 stsp return err;
7726 2822a352 2019-10-15 stsp }
7727 2822a352 2019-10-15 stsp
7728 2822a352 2019-10-15 stsp const struct got_error *
7729 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7730 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7731 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7732 2822a352 2019-10-15 stsp struct got_repository *repo)
7733 2822a352 2019-10-15 stsp {
7734 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7735 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7736 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7737 2822a352 2019-10-15 stsp
7738 2822a352 2019-10-15 stsp *fileindex = NULL;
7739 2822a352 2019-10-15 stsp *branch_ref = NULL;
7740 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7741 2822a352 2019-10-15 stsp
7742 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7743 2822a352 2019-10-15 stsp if (err)
7744 2822a352 2019-10-15 stsp return err;
7745 2822a352 2019-10-15 stsp
7746 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7747 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7748 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7749 2822a352 2019-10-15 stsp "update -b or different branch name required");
7750 2822a352 2019-10-15 stsp goto done;
7751 2822a352 2019-10-15 stsp }
7752 2822a352 2019-10-15 stsp
7753 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7754 2822a352 2019-10-15 stsp if (err)
7755 2822a352 2019-10-15 stsp goto done;
7756 2822a352 2019-10-15 stsp
7757 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7758 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7759 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7760 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7761 2822a352 2019-10-15 stsp &ok_arg);
7762 2822a352 2019-10-15 stsp if (err)
7763 2822a352 2019-10-15 stsp goto done;
7764 2822a352 2019-10-15 stsp
7765 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7766 2822a352 2019-10-15 stsp if (err)
7767 2822a352 2019-10-15 stsp goto done;
7768 2822a352 2019-10-15 stsp
7769 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7770 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7771 2822a352 2019-10-15 stsp done:
7772 2822a352 2019-10-15 stsp if (err) {
7773 2822a352 2019-10-15 stsp if (*branch_ref) {
7774 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7775 2822a352 2019-10-15 stsp *branch_ref = NULL;
7776 2822a352 2019-10-15 stsp }
7777 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7778 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7779 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7780 2822a352 2019-10-15 stsp }
7781 2822a352 2019-10-15 stsp if (*fileindex) {
7782 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7783 2822a352 2019-10-15 stsp *fileindex = NULL;
7784 2822a352 2019-10-15 stsp }
7785 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7786 2822a352 2019-10-15 stsp }
7787 0cb83759 2019-08-03 stsp return err;
7788 0cb83759 2019-08-03 stsp }
7789 0cb83759 2019-08-03 stsp
7790 2822a352 2019-10-15 stsp const struct got_error *
7791 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7792 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7793 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7794 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7795 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7796 2822a352 2019-10-15 stsp {
7797 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7798 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7799 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7800 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7801 2822a352 2019-10-15 stsp
7802 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7803 2822a352 2019-10-15 stsp if (err)
7804 2822a352 2019-10-15 stsp goto done;
7805 2822a352 2019-10-15 stsp
7806 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7807 2822a352 2019-10-15 stsp if (err)
7808 2822a352 2019-10-15 stsp goto done;
7809 2822a352 2019-10-15 stsp
7810 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7811 945f9229 2022-04-16 thomas if (err)
7812 945f9229 2022-04-16 thomas goto done;
7813 945f9229 2022-04-16 thomas
7814 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7815 2822a352 2019-10-15 stsp worktree->path_prefix);
7816 2822a352 2019-10-15 stsp if (err)
7817 2822a352 2019-10-15 stsp goto done;
7818 2822a352 2019-10-15 stsp
7819 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7820 2822a352 2019-10-15 stsp if (err)
7821 2822a352 2019-10-15 stsp goto done;
7822 2822a352 2019-10-15 stsp
7823 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7824 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7825 2822a352 2019-10-15 stsp if (err)
7826 2822a352 2019-10-15 stsp goto sync;
7827 2822a352 2019-10-15 stsp
7828 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7829 2822a352 2019-10-15 stsp if (err)
7830 2822a352 2019-10-15 stsp goto sync;
7831 2822a352 2019-10-15 stsp
7832 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7833 6e210706 2021-01-22 stsp if (err)
7834 6e210706 2021-01-22 stsp goto sync;
7835 6e210706 2021-01-22 stsp
7836 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7837 2822a352 2019-10-15 stsp sync:
7838 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7839 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7840 2822a352 2019-10-15 stsp err = sync_err;
7841 2822a352 2019-10-15 stsp
7842 2822a352 2019-10-15 stsp done:
7843 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7844 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7845 2822a352 2019-10-15 stsp err = unlockerr;
7846 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7847 2822a352 2019-10-15 stsp
7848 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7849 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7850 2822a352 2019-10-15 stsp err = unlockerr;
7851 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7852 2822a352 2019-10-15 stsp
7853 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7854 2822a352 2019-10-15 stsp free(fileindex_path);
7855 2822a352 2019-10-15 stsp free(tree_id);
7856 945f9229 2022-04-16 thomas if (commit)
7857 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7858 2822a352 2019-10-15 stsp
7859 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7860 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7861 2822a352 2019-10-15 stsp err = unlockerr;
7862 2822a352 2019-10-15 stsp return err;
7863 2822a352 2019-10-15 stsp }
7864 2822a352 2019-10-15 stsp
7865 2822a352 2019-10-15 stsp const struct got_error *
7866 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7867 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7868 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7869 2822a352 2019-10-15 stsp {
7870 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7871 8b692cd0 2019-10-21 stsp
7872 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7873 8b692cd0 2019-10-21 stsp
7874 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7875 8b692cd0 2019-10-21 stsp
7876 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7877 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7878 8b692cd0 2019-10-21 stsp err = unlockerr;
7879 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7880 8b692cd0 2019-10-21 stsp
7881 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7882 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7883 8b692cd0 2019-10-21 stsp err = unlockerr;
7884 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7885 10604dce 2021-09-24 thomas
7886 10604dce 2021-09-24 thomas return err;
7887 10604dce 2021-09-24 thomas }
7888 10604dce 2021-09-24 thomas
7889 10604dce 2021-09-24 thomas const struct got_error *
7890 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7891 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7892 10604dce 2021-09-24 thomas {
7893 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7894 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7895 10604dce 2021-09-24 thomas
7896 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7897 10604dce 2021-09-24 thomas if (err)
7898 10604dce 2021-09-24 thomas goto done;
7899 8b692cd0 2019-10-21 stsp
7900 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7901 10604dce 2021-09-24 thomas
7902 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7903 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7904 10604dce 2021-09-24 thomas err = sync_err;
7905 10604dce 2021-09-24 thomas done:
7906 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7907 10604dce 2021-09-24 thomas free(fileindex_path);
7908 8b692cd0 2019-10-21 stsp return err;
7909 2822a352 2019-10-15 stsp }
7910 2822a352 2019-10-15 stsp
7911 10604dce 2021-09-24 thomas static const struct got_error *
7912 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7913 10604dce 2021-09-24 thomas {
7914 10604dce 2021-09-24 thomas const struct got_error *err;
7915 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7916 10604dce 2021-09-24 thomas
7917 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7918 10604dce 2021-09-24 thomas if (err)
7919 10604dce 2021-09-24 thomas goto done;
7920 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7921 10604dce 2021-09-24 thomas if (err)
7922 10604dce 2021-09-24 thomas goto done;
7923 10604dce 2021-09-24 thomas
7924 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7925 10604dce 2021-09-24 thomas if (err)
7926 10604dce 2021-09-24 thomas goto done;
7927 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7928 10604dce 2021-09-24 thomas if (err)
7929 10604dce 2021-09-24 thomas goto done;
7930 10604dce 2021-09-24 thomas
7931 10604dce 2021-09-24 thomas done:
7932 10604dce 2021-09-24 thomas free(branch_refname);
7933 10604dce 2021-09-24 thomas free(commit_refname);
7934 10604dce 2021-09-24 thomas return err;
7935 10604dce 2021-09-24 thomas }
7936 10604dce 2021-09-24 thomas
7937 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7938 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7939 10604dce 2021-09-24 thomas const char *branch_name;
7940 10604dce 2021-09-24 thomas };
7941 10604dce 2021-09-24 thomas
7942 10604dce 2021-09-24 thomas static const struct got_error *
7943 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7944 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7945 10604dce 2021-09-24 thomas {
7946 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7947 10604dce 2021-09-24 thomas
7948 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7949 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7950 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7951 10604dce 2021-09-24 thomas
7952 10604dce 2021-09-24 thomas return NULL;
7953 10604dce 2021-09-24 thomas }
7954 10604dce 2021-09-24 thomas
7955 10604dce 2021-09-24 thomas
7956 10604dce 2021-09-24 thomas const struct got_error *
7957 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7958 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7959 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7960 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7961 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7962 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7963 10604dce 2021-09-24 thomas {
7964 10604dce 2021-09-24 thomas const struct got_error *err;
7965 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7966 10604dce 2021-09-24 thomas
7967 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7968 10604dce 2021-09-24 thomas if (err)
7969 10604dce 2021-09-24 thomas goto done;
7970 10604dce 2021-09-24 thomas
7971 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7972 10604dce 2021-09-24 thomas worktree);
7973 10604dce 2021-09-24 thomas if (err)
7974 10604dce 2021-09-24 thomas goto done;
7975 10604dce 2021-09-24 thomas
7976 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7977 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7978 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7979 10604dce 2021-09-24 thomas done:
7980 10604dce 2021-09-24 thomas free(fileindex_path);
7981 10604dce 2021-09-24 thomas return err;
7982 10604dce 2021-09-24 thomas }
7983 10604dce 2021-09-24 thomas
7984 10604dce 2021-09-24 thomas const struct got_error *
7985 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7986 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7987 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7988 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7989 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
7990 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7991 ae1e948a 2021-09-28 thomas
7992 10604dce 2021-09-24 thomas {
7993 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7994 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7995 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7996 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7997 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7998 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7999 10604dce 2021-09-24 thomas int have_staged_files = 0;
8000 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8001 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8002 10604dce 2021-09-24 thomas
8003 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8004 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8005 10604dce 2021-09-24 thomas
8006 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8007 10604dce 2021-09-24 thomas
8008 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8009 10604dce 2021-09-24 thomas if (err)
8010 10604dce 2021-09-24 thomas goto done;
8011 10604dce 2021-09-24 thomas
8012 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8013 10604dce 2021-09-24 thomas if (err)
8014 10604dce 2021-09-24 thomas goto done;
8015 10604dce 2021-09-24 thomas
8016 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8017 10604dce 2021-09-24 thomas if (err)
8018 10604dce 2021-09-24 thomas goto done;
8019 10604dce 2021-09-24 thomas
8020 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8021 10604dce 2021-09-24 thomas &have_staged_files);
8022 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8023 10604dce 2021-09-24 thomas goto done;
8024 10604dce 2021-09-24 thomas if (have_staged_files) {
8025 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8026 10604dce 2021-09-24 thomas goto done;
8027 10604dce 2021-09-24 thomas }
8028 10604dce 2021-09-24 thomas
8029 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8030 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8031 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8032 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8033 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8034 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8035 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8036 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8037 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8038 10604dce 2021-09-24 thomas if (err)
8039 10604dce 2021-09-24 thomas goto done;
8040 10604dce 2021-09-24 thomas
8041 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
8042 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8043 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
8044 10604dce 2021-09-24 thomas goto done;
8045 10604dce 2021-09-24 thomas }
8046 10604dce 2021-09-24 thomas
8047 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8048 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8049 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
8050 10604dce 2021-09-24 thomas
8051 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
8052 10604dce 2021-09-24 thomas ct_path++;
8053 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
8054 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8055 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8056 10604dce 2021-09-24 thomas if (err)
8057 10604dce 2021-09-24 thomas goto done;
8058 10604dce 2021-09-24 thomas
8059 10604dce 2021-09-24 thomas }
8060 10604dce 2021-09-24 thomas
8061 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8062 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8063 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8064 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8065 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8066 10604dce 2021-09-24 thomas if (err)
8067 10604dce 2021-09-24 thomas goto done;
8068 10604dce 2021-09-24 thomas
8069 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8070 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8071 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8072 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8073 10604dce 2021-09-24 thomas err = sync_err;
8074 10604dce 2021-09-24 thomas done:
8075 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8076 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8077 21c2d8be 2023-01-10 thomas
8078 10604dce 2021-09-24 thomas free_commitable(ct);
8079 10604dce 2021-09-24 thomas }
8080 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8081 10604dce 2021-09-24 thomas free(fileindex_path);
8082 10604dce 2021-09-24 thomas return err;
8083 10604dce 2021-09-24 thomas }
8084 10604dce 2021-09-24 thomas
8085 10604dce 2021-09-24 thomas const struct got_error *
8086 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8087 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8088 10604dce 2021-09-24 thomas {
8089 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8090 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8091 10604dce 2021-09-24 thomas
8092 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8093 10604dce 2021-09-24 thomas if (err)
8094 10604dce 2021-09-24 thomas goto done;
8095 10604dce 2021-09-24 thomas
8096 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8097 10604dce 2021-09-24 thomas if (err)
8098 10604dce 2021-09-24 thomas goto done;
8099 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8100 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8101 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8102 10604dce 2021-09-24 thomas err = sync_err;
8103 10604dce 2021-09-24 thomas done:
8104 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8105 10604dce 2021-09-24 thomas free(fileindex_path);
8106 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8107 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8108 10604dce 2021-09-24 thomas err = unlockerr;
8109 10604dce 2021-09-24 thomas return err;
8110 10604dce 2021-09-24 thomas }
8111 10604dce 2021-09-24 thomas
8112 10604dce 2021-09-24 thomas const struct got_error *
8113 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8114 10604dce 2021-09-24 thomas struct got_repository *repo)
8115 10604dce 2021-09-24 thomas {
8116 10604dce 2021-09-24 thomas const struct got_error *err;
8117 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8118 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8119 10604dce 2021-09-24 thomas
8120 10604dce 2021-09-24 thomas *in_progress = 0;
8121 10604dce 2021-09-24 thomas
8122 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8123 10604dce 2021-09-24 thomas if (err)
8124 10604dce 2021-09-24 thomas return err;
8125 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8126 dfe86d1f 2021-09-24 thomas free(branch_refname);
8127 10604dce 2021-09-24 thomas if (err) {
8128 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8129 10604dce 2021-09-24 thomas return err;
8130 10604dce 2021-09-24 thomas } else
8131 10604dce 2021-09-24 thomas *in_progress = 1;
8132 10604dce 2021-09-24 thomas
8133 10604dce 2021-09-24 thomas return NULL;
8134 10604dce 2021-09-24 thomas }
8135 10604dce 2021-09-24 thomas
8136 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8137 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8138 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8139 10604dce 2021-09-24 thomas {
8140 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8141 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8142 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8143 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8144 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8145 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8146 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8147 10604dce 2021-09-24 thomas
8148 10604dce 2021-09-24 thomas *fileindex = NULL;
8149 10604dce 2021-09-24 thomas
8150 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8151 10604dce 2021-09-24 thomas if (err)
8152 10604dce 2021-09-24 thomas return err;
8153 10604dce 2021-09-24 thomas
8154 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8155 10604dce 2021-09-24 thomas if (err)
8156 10604dce 2021-09-24 thomas goto done;
8157 10604dce 2021-09-24 thomas
8158 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8159 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8160 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8161 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8162 10604dce 2021-09-24 thomas &ok_arg);
8163 10604dce 2021-09-24 thomas if (err)
8164 10604dce 2021-09-24 thomas goto done;
8165 10604dce 2021-09-24 thomas
8166 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8167 10604dce 2021-09-24 thomas if (err)
8168 10604dce 2021-09-24 thomas return err;
8169 10604dce 2021-09-24 thomas
8170 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8171 10604dce 2021-09-24 thomas if (err)
8172 10604dce 2021-09-24 thomas return err;
8173 10604dce 2021-09-24 thomas
8174 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8175 10604dce 2021-09-24 thomas 0);
8176 10604dce 2021-09-24 thomas if (err)
8177 10604dce 2021-09-24 thomas goto done;
8178 10604dce 2021-09-24 thomas
8179 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8180 10604dce 2021-09-24 thomas if (err)
8181 10604dce 2021-09-24 thomas goto done;
8182 10604dce 2021-09-24 thomas
8183 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8184 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8185 10604dce 2021-09-24 thomas goto done;
8186 10604dce 2021-09-24 thomas }
8187 10604dce 2021-09-24 thomas
8188 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8189 10604dce 2021-09-24 thomas if (err)
8190 10604dce 2021-09-24 thomas goto done;
8191 10604dce 2021-09-24 thomas
8192 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8193 10604dce 2021-09-24 thomas if (err)
8194 10604dce 2021-09-24 thomas goto done;
8195 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8196 10604dce 2021-09-24 thomas if (err)
8197 10604dce 2021-09-24 thomas goto done;
8198 10604dce 2021-09-24 thomas
8199 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8200 10604dce 2021-09-24 thomas if (err)
8201 10604dce 2021-09-24 thomas goto done;
8202 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8203 10604dce 2021-09-24 thomas if (err)
8204 10604dce 2021-09-24 thomas goto done;
8205 10604dce 2021-09-24 thomas
8206 10604dce 2021-09-24 thomas done:
8207 10604dce 2021-09-24 thomas free(branch_refname);
8208 10604dce 2021-09-24 thomas free(commit_refname);
8209 10604dce 2021-09-24 thomas free(fileindex_path);
8210 10604dce 2021-09-24 thomas if (branch_ref)
8211 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8212 10604dce 2021-09-24 thomas if (commit_ref)
8213 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8214 10604dce 2021-09-24 thomas if (wt_branch)
8215 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8216 10604dce 2021-09-24 thomas free(wt_branch_tip);
8217 10604dce 2021-09-24 thomas if (err) {
8218 10604dce 2021-09-24 thomas if (*fileindex) {
8219 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8220 10604dce 2021-09-24 thomas *fileindex = NULL;
8221 10604dce 2021-09-24 thomas }
8222 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8223 10604dce 2021-09-24 thomas }
8224 10604dce 2021-09-24 thomas return err;
8225 10604dce 2021-09-24 thomas }
8226 10604dce 2021-09-24 thomas
8227 10604dce 2021-09-24 thomas const struct got_error *
8228 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8229 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8230 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8231 10604dce 2021-09-24 thomas {
8232 10604dce 2021-09-24 thomas const struct got_error *err;
8233 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8234 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8235 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8236 10604dce 2021-09-24 thomas int have_staged_files = 0;
8237 10604dce 2021-09-24 thomas
8238 10604dce 2021-09-24 thomas *branch_name = NULL;
8239 10604dce 2021-09-24 thomas *branch_tip = NULL;
8240 10604dce 2021-09-24 thomas *fileindex = NULL;
8241 10604dce 2021-09-24 thomas
8242 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8243 10604dce 2021-09-24 thomas if (err)
8244 10604dce 2021-09-24 thomas return err;
8245 10604dce 2021-09-24 thomas
8246 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8247 10604dce 2021-09-24 thomas if (err)
8248 10604dce 2021-09-24 thomas goto done;
8249 10604dce 2021-09-24 thomas
8250 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8251 10604dce 2021-09-24 thomas &have_staged_files);
8252 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8253 10604dce 2021-09-24 thomas goto done;
8254 10604dce 2021-09-24 thomas if (have_staged_files) {
8255 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8256 10604dce 2021-09-24 thomas goto done;
8257 10604dce 2021-09-24 thomas }
8258 10604dce 2021-09-24 thomas
8259 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8260 10604dce 2021-09-24 thomas if (err)
8261 10604dce 2021-09-24 thomas goto done;
8262 10604dce 2021-09-24 thomas
8263 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8264 10604dce 2021-09-24 thomas if (err)
8265 10604dce 2021-09-24 thomas goto done;
8266 10604dce 2021-09-24 thomas
8267 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8268 10604dce 2021-09-24 thomas if (err)
8269 10604dce 2021-09-24 thomas goto done;
8270 10604dce 2021-09-24 thomas
8271 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8272 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8273 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8274 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8275 10604dce 2021-09-24 thomas goto done;
8276 10604dce 2021-09-24 thomas }
8277 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8278 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8279 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8280 10604dce 2021-09-24 thomas goto done;
8281 10604dce 2021-09-24 thomas }
8282 10604dce 2021-09-24 thomas
8283 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8284 10604dce 2021-09-24 thomas if (err)
8285 10604dce 2021-09-24 thomas goto done;
8286 10604dce 2021-09-24 thomas
8287 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8288 10604dce 2021-09-24 thomas if (err)
8289 10604dce 2021-09-24 thomas goto done;
8290 10604dce 2021-09-24 thomas done:
8291 10604dce 2021-09-24 thomas free(commit_refname);
8292 10604dce 2021-09-24 thomas free(branch_refname);
8293 10604dce 2021-09-24 thomas free(fileindex_path);
8294 10604dce 2021-09-24 thomas if (commit_ref)
8295 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8296 10604dce 2021-09-24 thomas if (branch_ref)
8297 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8298 10604dce 2021-09-24 thomas if (err) {
8299 10604dce 2021-09-24 thomas if (*branch_name) {
8300 10604dce 2021-09-24 thomas free(*branch_name);
8301 10604dce 2021-09-24 thomas *branch_name = NULL;
8302 10604dce 2021-09-24 thomas }
8303 10604dce 2021-09-24 thomas free(*branch_tip);
8304 10604dce 2021-09-24 thomas *branch_tip = NULL;
8305 10604dce 2021-09-24 thomas if (*fileindex) {
8306 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8307 10604dce 2021-09-24 thomas *fileindex = NULL;
8308 10604dce 2021-09-24 thomas }
8309 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8310 10604dce 2021-09-24 thomas }
8311 10604dce 2021-09-24 thomas return err;
8312 10604dce 2021-09-24 thomas }
8313 10604dce 2021-09-24 thomas
8314 10604dce 2021-09-24 thomas const struct got_error *
8315 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8316 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8317 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8318 10604dce 2021-09-24 thomas {
8319 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8320 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8321 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8322 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8323 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8324 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8325 10604dce 2021-09-24 thomas
8326 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8327 945f9229 2022-04-16 thomas worktree->base_commit_id);
8328 945f9229 2022-04-16 thomas if (err)
8329 945f9229 2022-04-16 thomas goto done;
8330 945f9229 2022-04-16 thomas
8331 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8332 945f9229 2022-04-16 thomas worktree->path_prefix);
8333 10604dce 2021-09-24 thomas if (err)
8334 10604dce 2021-09-24 thomas goto done;
8335 10604dce 2021-09-24 thomas
8336 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8337 10604dce 2021-09-24 thomas if (err)
8338 10604dce 2021-09-24 thomas goto done;
8339 10604dce 2021-09-24 thomas
8340 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8341 10604dce 2021-09-24 thomas if (err)
8342 10604dce 2021-09-24 thomas goto done;
8343 10604dce 2021-09-24 thomas
8344 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8345 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8346 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8347 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8348 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8349 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8350 10604dce 2021-09-24 thomas rfa.repo = repo;
8351 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8352 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8353 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8354 10604dce 2021-09-24 thomas if (err)
8355 10604dce 2021-09-24 thomas goto sync;
8356 10604dce 2021-09-24 thomas
8357 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8358 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8359 10604dce 2021-09-24 thomas sync:
8360 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8361 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8362 10604dce 2021-09-24 thomas err = sync_err;
8363 10604dce 2021-09-24 thomas done:
8364 10604dce 2021-09-24 thomas free(tree_id);
8365 10604dce 2021-09-24 thomas free(commit_id);
8366 945f9229 2022-04-16 thomas if (commit)
8367 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8368 10604dce 2021-09-24 thomas if (fileindex)
8369 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8370 10604dce 2021-09-24 thomas free(fileindex_path);
8371 10604dce 2021-09-24 thomas
8372 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8373 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8374 10604dce 2021-09-24 thomas err = unlockerr;
8375 10604dce 2021-09-24 thomas return err;
8376 10604dce 2021-09-24 thomas }
8377 10604dce 2021-09-24 thomas
8378 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8379 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8380 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8381 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8382 2db2652d 2019-08-07 stsp struct got_repository *repo;
8383 2db2652d 2019-08-07 stsp int have_changes;
8384 2db2652d 2019-08-07 stsp };
8385 2db2652d 2019-08-07 stsp
8386 ef20f542 2022-06-26 thomas static const struct got_error *
8387 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8388 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8389 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8390 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8391 735ef5ac 2019-08-03 stsp {
8392 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8393 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8394 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8395 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8396 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8397 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8398 8b13ce36 2019-08-08 stsp
8399 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8400 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8401 8b13ce36 2019-08-08 stsp return NULL;
8402 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8403 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8404 735ef5ac 2019-08-03 stsp
8405 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8406 735ef5ac 2019-08-03 stsp if (ie == NULL)
8407 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8408 735ef5ac 2019-08-03 stsp
8409 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8410 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8411 735ef5ac 2019-08-03 stsp relpath) == -1)
8412 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8413 735ef5ac 2019-08-03 stsp
8414 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8415 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8416 43010591 2023-02-17 thomas &base_commit_id, ie);
8417 735ef5ac 2019-08-03 stsp }
8418 735ef5ac 2019-08-03 stsp
8419 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8420 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8421 3aa5969e 2019-08-06 stsp goto done;
8422 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8423 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8424 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8425 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8426 735ef5ac 2019-08-03 stsp goto done;
8427 3aa5969e 2019-08-06 stsp }
8428 735ef5ac 2019-08-03 stsp
8429 2db2652d 2019-08-07 stsp a->have_changes = 1;
8430 2db2652d 2019-08-07 stsp
8431 735ef5ac 2019-08-03 stsp p = in_repo_path;
8432 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8433 735ef5ac 2019-08-03 stsp p++;
8434 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8435 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8436 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8437 735ef5ac 2019-08-03 stsp done:
8438 735ef5ac 2019-08-03 stsp free(in_repo_path);
8439 dc424a06 2019-08-07 stsp return err;
8440 dc424a06 2019-08-07 stsp }
8441 dc424a06 2019-08-07 stsp
8442 2db2652d 2019-08-07 stsp struct stage_path_arg {
8443 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8444 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8445 2db2652d 2019-08-07 stsp struct got_repository *repo;
8446 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8447 2db2652d 2019-08-07 stsp void *status_arg;
8448 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8449 2db2652d 2019-08-07 stsp void *patch_arg;
8450 7b5dc508 2019-10-28 stsp int staged_something;
8451 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8452 2db2652d 2019-08-07 stsp };
8453 2db2652d 2019-08-07 stsp
8454 2db2652d 2019-08-07 stsp static const struct got_error *
8455 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8456 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8457 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8458 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8459 0cb83759 2019-08-03 stsp {
8460 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8461 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8462 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8463 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8464 0cb83759 2019-08-03 stsp uint32_t stage;
8465 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8466 0aeb8099 2020-07-23 stsp struct stat sb;
8467 8b13ce36 2019-08-08 stsp
8468 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8469 8b13ce36 2019-08-08 stsp return NULL;
8470 0cb83759 2019-08-03 stsp
8471 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8472 d3e7c587 2019-08-03 stsp if (ie == NULL)
8473 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8474 0cb83759 2019-08-03 stsp
8475 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8476 2db2652d 2019-08-07 stsp relpath)== -1)
8477 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8478 0cb83759 2019-08-03 stsp
8479 0cb83759 2019-08-03 stsp switch (status) {
8480 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8481 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8482 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8483 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8484 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8485 0aeb8099 2020-07-23 stsp break;
8486 0aeb8099 2020-07-23 stsp }
8487 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8488 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8489 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8490 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8491 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8492 dc424a06 2019-08-07 stsp if (err)
8493 dc424a06 2019-08-07 stsp break;
8494 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8495 dc424a06 2019-08-07 stsp break;
8496 dc424a06 2019-08-07 stsp } else {
8497 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8498 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8499 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8500 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8501 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8502 dc424a06 2019-08-07 stsp break;
8503 dc424a06 2019-08-07 stsp }
8504 dc424a06 2019-08-07 stsp }
8505 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8506 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8507 0cb83759 2019-08-03 stsp if (err)
8508 dc424a06 2019-08-07 stsp break;
8509 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8510 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8511 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8512 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8513 0cb83759 2019-08-03 stsp else
8514 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8515 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8516 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8517 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8518 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8519 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8520 35213c7c 2020-07-23 stsp ssize_t target_len;
8521 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8522 35213c7c 2020-07-23 stsp sizeof(target_path));
8523 35213c7c 2020-07-23 stsp if (target_len == -1) {
8524 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8525 35213c7c 2020-07-23 stsp ondisk_path);
8526 35213c7c 2020-07-23 stsp break;
8527 35213c7c 2020-07-23 stsp }
8528 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8529 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8530 35213c7c 2020-07-23 stsp a->worktree->root_path);
8531 35213c7c 2020-07-23 stsp if (err)
8532 35213c7c 2020-07-23 stsp break;
8533 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8534 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8535 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8536 35213c7c 2020-07-23 stsp break;
8537 35213c7c 2020-07-23 stsp }
8538 35213c7c 2020-07-23 stsp }
8539 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8540 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8541 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8542 35213c7c 2020-07-23 stsp else
8543 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8544 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8545 0aeb8099 2020-07-23 stsp } else {
8546 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8547 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8548 0aeb8099 2020-07-23 stsp }
8549 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8550 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8551 dc424a06 2019-08-07 stsp break;
8552 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8553 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8554 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8555 f289c82c 2022-06-13 thomas if (err)
8556 f289c82c 2022-06-13 thomas break;
8557 f289c82c 2022-06-13 thomas /*
8558 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8559 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8560 f289c82c 2022-06-13 thomas */
8561 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8562 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8563 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8564 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8565 f289c82c 2022-06-13 thomas }
8566 0cb83759 2019-08-03 stsp break;
8567 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8568 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8569 d3e7c587 2019-08-03 stsp break;
8570 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8571 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8572 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8573 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8574 dc424a06 2019-08-07 stsp if (err)
8575 dc424a06 2019-08-07 stsp break;
8576 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8577 88f33a19 2019-08-08 stsp break;
8578 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8579 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8580 dc424a06 2019-08-07 stsp break;
8581 88f33a19 2019-08-08 stsp }
8582 dc424a06 2019-08-07 stsp }
8583 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8584 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8585 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8586 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8587 dc424a06 2019-08-07 stsp break;
8588 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8589 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8590 12463d8b 2019-12-13 stsp de_name);
8591 0cb83759 2019-08-03 stsp break;
8592 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8593 d3e7c587 2019-08-03 stsp break;
8594 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8595 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8596 ebf48fd5 2019-08-03 stsp break;
8597 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8598 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8599 2a06fe5f 2019-08-24 stsp break;
8600 0cb83759 2019-08-03 stsp default:
8601 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8602 537ac44b 2019-08-03 stsp break;
8603 0cb83759 2019-08-03 stsp }
8604 dc424a06 2019-08-07 stsp
8605 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8606 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8607 dc424a06 2019-08-07 stsp free(path_content);
8608 2db2652d 2019-08-07 stsp free(ondisk_path);
8609 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8610 0ebf8283 2019-07-24 stsp return err;
8611 0ebf8283 2019-07-24 stsp }
8612 0cb83759 2019-08-03 stsp
8613 0cb83759 2019-08-03 stsp const struct got_error *
8614 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8615 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8616 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8617 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8618 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8619 0cb83759 2019-08-03 stsp {
8620 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8621 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8622 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8623 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8624 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8625 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8626 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8627 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8628 0cb83759 2019-08-03 stsp
8629 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8630 0cb83759 2019-08-03 stsp if (err)
8631 0cb83759 2019-08-03 stsp return err;
8632 0cb83759 2019-08-03 stsp
8633 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8634 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8635 735ef5ac 2019-08-03 stsp if (err)
8636 735ef5ac 2019-08-03 stsp goto done;
8637 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8638 735ef5ac 2019-08-03 stsp if (err)
8639 735ef5ac 2019-08-03 stsp goto done;
8640 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8641 0cb83759 2019-08-03 stsp if (err)
8642 0cb83759 2019-08-03 stsp goto done;
8643 0cb83759 2019-08-03 stsp
8644 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8645 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8646 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8647 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8648 2db2652d 2019-08-07 stsp oka.repo = repo;
8649 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8650 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8651 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8652 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8653 735ef5ac 2019-08-03 stsp if (err)
8654 735ef5ac 2019-08-03 stsp goto done;
8655 735ef5ac 2019-08-03 stsp }
8656 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8657 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8658 2db2652d 2019-08-07 stsp goto done;
8659 2db2652d 2019-08-07 stsp }
8660 735ef5ac 2019-08-03 stsp
8661 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8662 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8663 2db2652d 2019-08-07 stsp spa.repo = repo;
8664 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8665 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8666 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8667 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8668 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8669 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8670 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8671 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8672 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8673 42005733 2019-08-03 stsp if (err)
8674 2db2652d 2019-08-07 stsp goto done;
8675 7b5dc508 2019-10-28 stsp }
8676 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8677 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8678 7b5dc508 2019-10-28 stsp goto done;
8679 0cb83759 2019-08-03 stsp }
8680 0cb83759 2019-08-03 stsp
8681 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8682 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8683 0cb83759 2019-08-03 stsp err = sync_err;
8684 0cb83759 2019-08-03 stsp done:
8685 735ef5ac 2019-08-03 stsp if (head_ref)
8686 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8687 735ef5ac 2019-08-03 stsp free(head_commit_id);
8688 0cb83759 2019-08-03 stsp free(fileindex_path);
8689 0cb83759 2019-08-03 stsp if (fileindex)
8690 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8691 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8692 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8693 0cb83759 2019-08-03 stsp err = unlockerr;
8694 0cb83759 2019-08-03 stsp return err;
8695 0cb83759 2019-08-03 stsp }
8696 ad493afc 2019-08-03 stsp
8697 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8698 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8699 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8700 ad493afc 2019-08-03 stsp struct got_repository *repo;
8701 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8702 ad493afc 2019-08-03 stsp void *progress_arg;
8703 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8704 2e1f37b0 2019-08-08 stsp void *patch_arg;
8705 ad493afc 2019-08-03 stsp };
8706 2e1f37b0 2019-08-08 stsp
8707 2e1f37b0 2019-08-08 stsp static const struct got_error *
8708 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8709 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8710 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8711 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8712 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8713 2e1f37b0 2019-08-08 stsp {
8714 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8715 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8716 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8717 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8718 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8719 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8720 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8721 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8722 2e1f37b0 2019-08-08 stsp
8723 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8724 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8725 2e1f37b0 2019-08-08 stsp
8726 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8727 2e1f37b0 2019-08-08 stsp if (err)
8728 2e1f37b0 2019-08-08 stsp return err;
8729 f4ae6ddb 2022-07-01 thomas
8730 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8731 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8732 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8733 f4ae6ddb 2022-07-01 thomas goto done;
8734 f4ae6ddb 2022-07-01 thomas }
8735 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8736 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8737 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8738 f4ae6ddb 2022-07-01 thomas goto done;
8739 f4ae6ddb 2022-07-01 thomas }
8740 f4ae6ddb 2022-07-01 thomas
8741 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8742 2e1f37b0 2019-08-08 stsp if (err)
8743 2e1f37b0 2019-08-08 stsp goto done;
8744 2e1f37b0 2019-08-08 stsp
8745 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8746 2e1f37b0 2019-08-08 stsp if (err)
8747 2e1f37b0 2019-08-08 stsp goto done;
8748 2e1f37b0 2019-08-08 stsp
8749 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8750 2e1f37b0 2019-08-08 stsp if (err)
8751 2e1f37b0 2019-08-08 stsp goto done;
8752 2e1f37b0 2019-08-08 stsp
8753 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8754 f4ae6ddb 2022-07-01 thomas fd2);
8755 2e1f37b0 2019-08-08 stsp if (err)
8756 2e1f37b0 2019-08-08 stsp goto done;
8757 2e1f37b0 2019-08-08 stsp
8758 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8759 2e1f37b0 2019-08-08 stsp if (err)
8760 2e1f37b0 2019-08-08 stsp goto done;
8761 ad493afc 2019-08-03 stsp
8762 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8763 2e1f37b0 2019-08-08 stsp if (err)
8764 2e1f37b0 2019-08-08 stsp goto done;
8765 2e1f37b0 2019-08-08 stsp
8766 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8767 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8768 2e1f37b0 2019-08-08 stsp if (err)
8769 2e1f37b0 2019-08-08 stsp goto done;
8770 2e1f37b0 2019-08-08 stsp
8771 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8772 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8773 2e1f37b0 2019-08-08 stsp if (err)
8774 2e1f37b0 2019-08-08 stsp goto done;
8775 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8776 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8777 2e1f37b0 2019-08-08 stsp if (err)
8778 2e1f37b0 2019-08-08 stsp goto done;
8779 2e1f37b0 2019-08-08 stsp
8780 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8781 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8782 2e1f37b0 2019-08-08 stsp goto done;
8783 2e1f37b0 2019-08-08 stsp }
8784 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8785 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8786 2e1f37b0 2019-08-08 stsp goto done;
8787 2e1f37b0 2019-08-08 stsp }
8788 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8789 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8790 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8791 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8792 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8793 fe621944 2020-11-10 stsp nchanges++;
8794 fe621944 2020-11-10 stsp }
8795 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8796 2e1f37b0 2019-08-08 stsp int choice;
8797 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8798 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8799 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8800 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8801 2e1f37b0 2019-08-08 stsp if (err)
8802 2e1f37b0 2019-08-08 stsp goto done;
8803 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8804 2e1f37b0 2019-08-08 stsp have_content = 1;
8805 19e4b907 2019-08-08 stsp else
8806 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8807 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8808 2e1f37b0 2019-08-08 stsp break;
8809 2e1f37b0 2019-08-08 stsp }
8810 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8811 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8812 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8813 2e1f37b0 2019-08-08 stsp done:
8814 2e1f37b0 2019-08-08 stsp free(label1);
8815 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8816 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8817 2e1f37b0 2019-08-08 stsp if (blob)
8818 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8819 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8820 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8821 2e1f37b0 2019-08-08 stsp if (staged_blob)
8822 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8823 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8824 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8825 fe621944 2020-11-10 stsp err = free_err;
8826 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8827 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8828 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8829 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8830 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8831 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8832 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8833 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8834 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8835 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8836 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8837 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8838 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8839 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8840 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8841 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8842 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8843 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8844 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8845 2e1f37b0 2019-08-08 stsp }
8846 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8847 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8848 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8849 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8850 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8851 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8852 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8853 2e1f37b0 2019-08-08 stsp }
8854 2e1f37b0 2019-08-08 stsp free(path1);
8855 2e1f37b0 2019-08-08 stsp free(path2);
8856 fda8017d 2020-07-23 stsp return err;
8857 fda8017d 2020-07-23 stsp }
8858 fda8017d 2020-07-23 stsp
8859 fda8017d 2020-07-23 stsp static const struct got_error *
8860 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8861 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8862 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8863 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8864 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8865 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8866 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8867 fda8017d 2020-07-23 stsp {
8868 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8869 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8870 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8871 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8872 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8873 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8874 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8875 fda8017d 2020-07-23 stsp struct stat sb;
8876 fda8017d 2020-07-23 stsp
8877 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8878 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8879 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8880 fda8017d 2020-07-23 stsp if (err)
8881 fda8017d 2020-07-23 stsp return err;
8882 fda8017d 2020-07-23 stsp
8883 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8884 fda8017d 2020-07-23 stsp return NULL;
8885 fda8017d 2020-07-23 stsp
8886 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8887 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8888 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8889 fda8017d 2020-07-23 stsp if (err)
8890 fda8017d 2020-07-23 stsp goto done;
8891 fda8017d 2020-07-23 stsp }
8892 fda8017d 2020-07-23 stsp
8893 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8894 fda8017d 2020-07-23 stsp if (f == NULL) {
8895 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8896 fda8017d 2020-07-23 stsp path_unstaged_content);
8897 fda8017d 2020-07-23 stsp goto done;
8898 fda8017d 2020-07-23 stsp }
8899 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8900 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8901 fda8017d 2020-07-23 stsp goto done;
8902 fda8017d 2020-07-23 stsp }
8903 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8904 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8905 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8906 fda8017d 2020-07-23 stsp size_t r;
8907 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8908 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8909 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8910 fda8017d 2020-07-23 stsp goto done;
8911 fda8017d 2020-07-23 stsp }
8912 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8913 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8914 fda8017d 2020-07-23 stsp goto done;
8915 fda8017d 2020-07-23 stsp }
8916 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8917 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8918 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8919 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8920 fda8017d 2020-07-23 stsp progress_arg);
8921 fda8017d 2020-07-23 stsp } else {
8922 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8923 67a66647 2021-05-31 stsp
8924 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8925 67a66647 2021-05-31 stsp if (err)
8926 67a66647 2021-05-31 stsp return err;
8927 67a66647 2021-05-31 stsp
8928 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8929 67a66647 2021-05-31 stsp parent) == -1) {
8930 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8931 67a66647 2021-05-31 stsp base_path = NULL;
8932 67a66647 2021-05-31 stsp goto done;
8933 67a66647 2021-05-31 stsp }
8934 67a66647 2021-05-31 stsp
8935 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8936 fc2a50f2 2022-11-01 thomas base_path, "");
8937 67a66647 2021-05-31 stsp if (err)
8938 67a66647 2021-05-31 stsp goto done;
8939 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8940 67a66647 2021-05-31 stsp blob_base);
8941 67a66647 2021-05-31 stsp if (err)
8942 67a66647 2021-05-31 stsp goto done;
8943 67a66647 2021-05-31 stsp
8944 b6b86fd1 2022-08-30 thomas /*
8945 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8946 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8947 eec2f5a9 2021-06-03 stsp */
8948 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8949 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8950 eec2f5a9 2021-06-03 stsp ondisk_path);
8951 eec2f5a9 2021-06-03 stsp if (err)
8952 eec2f5a9 2021-06-03 stsp goto done;
8953 eec2f5a9 2021-06-03 stsp } else {
8954 eec2f5a9 2021-06-03 stsp int fd;
8955 06340621 2021-12-31 thomas fd = open(ondisk_path,
8956 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8957 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8958 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8959 eec2f5a9 2021-06-03 stsp goto done;
8960 eec2f5a9 2021-06-03 stsp }
8961 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8962 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8963 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8964 eec2f5a9 2021-06-03 stsp close(fd);
8965 eec2f5a9 2021-06-03 stsp goto done;
8966 eec2f5a9 2021-06-03 stsp }
8967 eec2f5a9 2021-06-03 stsp }
8968 eec2f5a9 2021-06-03 stsp
8969 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8970 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8971 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8972 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8973 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8974 fda8017d 2020-07-23 stsp }
8975 fda8017d 2020-07-23 stsp if (err)
8976 fda8017d 2020-07-23 stsp goto done;
8977 fda8017d 2020-07-23 stsp
8978 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8979 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8980 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8981 2ac8aa02 2020-11-09 stsp } else {
8982 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8983 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8984 2ac8aa02 2020-11-09 stsp }
8985 fda8017d 2020-07-23 stsp done:
8986 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8987 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8988 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8989 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8990 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8991 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8992 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8993 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8994 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8995 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8996 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8997 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8998 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8999 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9000 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9001 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9002 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9003 67a66647 2021-05-31 stsp free(blob_base_path);
9004 67a66647 2021-05-31 stsp free(parent);
9005 67a66647 2021-05-31 stsp free(base_path);
9006 2e1f37b0 2019-08-08 stsp return err;
9007 2e1f37b0 2019-08-08 stsp }
9008 2e1f37b0 2019-08-08 stsp
9009 ad493afc 2019-08-03 stsp static const struct got_error *
9010 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9011 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9012 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9013 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9014 ad493afc 2019-08-03 stsp {
9015 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9016 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9017 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9018 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9019 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9020 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9021 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9022 9bc94a15 2019-08-03 stsp struct stat sb;
9023 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9024 ad493afc 2019-08-03 stsp
9025 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9026 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9027 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9028 2e1f37b0 2019-08-08 stsp return NULL;
9029 2e1f37b0 2019-08-08 stsp
9030 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9031 ad493afc 2019-08-03 stsp if (ie == NULL)
9032 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9033 9bc94a15 2019-08-03 stsp
9034 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9035 9bc94a15 2019-08-03 stsp == -1)
9036 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9037 ad493afc 2019-08-03 stsp
9038 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9039 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9040 f69721c3 2019-10-21 stsp if (err)
9041 f69721c3 2019-10-21 stsp goto done;
9042 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9043 f69721c3 2019-10-21 stsp id_str) == -1) {
9044 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9045 f4ae6ddb 2022-07-01 thomas goto done;
9046 f4ae6ddb 2022-07-01 thomas }
9047 f4ae6ddb 2022-07-01 thomas
9048 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9049 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9050 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9051 f4ae6ddb 2022-07-01 thomas goto done;
9052 f4ae6ddb 2022-07-01 thomas }
9053 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9054 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9055 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9056 f69721c3 2019-10-21 stsp goto done;
9057 f69721c3 2019-10-21 stsp }
9058 f69721c3 2019-10-21 stsp
9059 ad493afc 2019-08-03 stsp switch (staged_status) {
9060 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9061 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9062 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9063 ad493afc 2019-08-03 stsp if (err)
9064 ad493afc 2019-08-03 stsp break;
9065 ad493afc 2019-08-03 stsp /* fall through */
9066 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9067 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9068 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9069 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9070 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9071 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9072 2e1f37b0 2019-08-08 stsp if (err)
9073 2e1f37b0 2019-08-08 stsp break;
9074 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9075 2e1f37b0 2019-08-08 stsp break;
9076 2e1f37b0 2019-08-08 stsp } else {
9077 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9078 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9079 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9080 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9081 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9082 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9083 2e1f37b0 2019-08-08 stsp }
9084 2e1f37b0 2019-08-08 stsp }
9085 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9086 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9087 ad493afc 2019-08-03 stsp if (err)
9088 ad493afc 2019-08-03 stsp break;
9089 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9090 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9091 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9092 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9093 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9094 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9095 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9096 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9097 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9098 ea7786be 2020-07-23 stsp break;
9099 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9100 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9101 36bf999c 2020-07-23 stsp char *staged_target;
9102 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9103 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9104 36bf999c 2020-07-23 stsp if (err)
9105 36bf999c 2020-07-23 stsp goto done;
9106 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9107 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9108 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9109 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9110 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9111 36bf999c 2020-07-23 stsp free(staged_target);
9112 dfe9fba0 2020-07-23 stsp } else {
9113 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9114 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9115 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9116 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9117 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9118 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9119 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9120 dfe9fba0 2020-07-23 stsp }
9121 ea7786be 2020-07-23 stsp break;
9122 ea7786be 2020-07-23 stsp default:
9123 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9124 ea7786be 2020-07-23 stsp break;
9125 ea7786be 2020-07-23 stsp }
9126 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9127 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9128 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9129 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9130 2ac8aa02 2020-11-09 stsp }
9131 ad493afc 2019-08-03 stsp break;
9132 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9133 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9134 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9135 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9136 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9137 2e1f37b0 2019-08-08 stsp if (err)
9138 2e1f37b0 2019-08-08 stsp break;
9139 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9140 2e1f37b0 2019-08-08 stsp break;
9141 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9142 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9143 2e1f37b0 2019-08-08 stsp break;
9144 2e1f37b0 2019-08-08 stsp }
9145 2e1f37b0 2019-08-08 stsp }
9146 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9147 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9148 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9149 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9150 9bc94a15 2019-08-03 stsp if (err)
9151 9bc94a15 2019-08-03 stsp break;
9152 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9153 ad493afc 2019-08-03 stsp break;
9154 ad493afc 2019-08-03 stsp }
9155 f69721c3 2019-10-21 stsp done:
9156 ad493afc 2019-08-03 stsp free(ondisk_path);
9157 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9158 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9159 ad493afc 2019-08-03 stsp if (blob_base)
9160 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9161 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9162 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9163 ad493afc 2019-08-03 stsp if (blob_staged)
9164 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9165 f69721c3 2019-10-21 stsp free(id_str);
9166 f69721c3 2019-10-21 stsp free(label_orig);
9167 ad493afc 2019-08-03 stsp return err;
9168 ad493afc 2019-08-03 stsp }
9169 ad493afc 2019-08-03 stsp
9170 ad493afc 2019-08-03 stsp const struct got_error *
9171 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9172 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9173 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9174 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9175 ad493afc 2019-08-03 stsp struct got_repository *repo)
9176 ad493afc 2019-08-03 stsp {
9177 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9178 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9179 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9180 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9181 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9182 ad493afc 2019-08-03 stsp
9183 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9184 ad493afc 2019-08-03 stsp if (err)
9185 ad493afc 2019-08-03 stsp return err;
9186 ad493afc 2019-08-03 stsp
9187 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9188 ad493afc 2019-08-03 stsp if (err)
9189 ad493afc 2019-08-03 stsp goto done;
9190 ad493afc 2019-08-03 stsp
9191 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9192 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9193 ad493afc 2019-08-03 stsp upa.repo = repo;
9194 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9195 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9196 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9197 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9198 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9199 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9200 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9201 ad493afc 2019-08-03 stsp if (err)
9202 ad493afc 2019-08-03 stsp goto done;
9203 ad493afc 2019-08-03 stsp }
9204 ad493afc 2019-08-03 stsp
9205 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9206 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9207 ad493afc 2019-08-03 stsp err = sync_err;
9208 ad493afc 2019-08-03 stsp done:
9209 ad493afc 2019-08-03 stsp free(fileindex_path);
9210 ad493afc 2019-08-03 stsp if (fileindex)
9211 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9212 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9213 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9214 ad493afc 2019-08-03 stsp err = unlockerr;
9215 ad493afc 2019-08-03 stsp return err;
9216 ad493afc 2019-08-03 stsp }
9217 b2118c49 2020-07-28 stsp
9218 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9219 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9220 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9221 b2118c49 2020-07-28 stsp void *info_arg;
9222 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9223 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9224 b2118c49 2020-07-28 stsp void *cancel_arg;
9225 b2118c49 2020-07-28 stsp };
9226 b2118c49 2020-07-28 stsp
9227 b2118c49 2020-07-28 stsp static const struct got_error *
9228 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9229 b2118c49 2020-07-28 stsp {
9230 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9231 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9232 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9233 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9234 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9235 b2118c49 2020-07-28 stsp int stage;
9236 b2118c49 2020-07-28 stsp
9237 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9238 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9239 b2118c49 2020-07-28 stsp
9240 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9241 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9242 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9243 b2118c49 2020-07-28 stsp break;
9244 b2118c49 2020-07-28 stsp }
9245 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9246 b2118c49 2020-07-28 stsp return NULL;
9247 b2118c49 2020-07-28 stsp
9248 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9249 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9250 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9251 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9252 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9253 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9254 43010591 2023-02-17 thomas &staged_blob_id, ie);
9255 b2118c49 2020-07-28 stsp }
9256 b2118c49 2020-07-28 stsp
9257 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9258 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9259 b2118c49 2020-07-28 stsp
9260 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9261 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9262 b2118c49 2020-07-28 stsp }
9263 b2118c49 2020-07-28 stsp
9264 b2118c49 2020-07-28 stsp const struct got_error *
9265 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9266 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9267 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9268 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9269 b2118c49 2020-07-28 stsp
9270 b2118c49 2020-07-28 stsp {
9271 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9272 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9273 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9274 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9275 b2118c49 2020-07-28 stsp
9276 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9277 b2118c49 2020-07-28 stsp if (err)
9278 b2118c49 2020-07-28 stsp return err;
9279 b2118c49 2020-07-28 stsp
9280 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9281 b2118c49 2020-07-28 stsp if (err)
9282 b2118c49 2020-07-28 stsp goto done;
9283 b2118c49 2020-07-28 stsp
9284 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9285 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9286 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9287 b2118c49 2020-07-28 stsp arg.paths = paths;
9288 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9289 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9290 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9291 b2118c49 2020-07-28 stsp &arg);
9292 b2118c49 2020-07-28 stsp done:
9293 b2118c49 2020-07-28 stsp free(fileindex_path);
9294 b2118c49 2020-07-28 stsp if (fileindex)
9295 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9296 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9297 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9298 b2118c49 2020-07-28 stsp err = unlockerr;
9299 b2118c49 2020-07-28 stsp return err;
9300 b2118c49 2020-07-28 stsp }
9301 814624e7 2022-03-22 thomas
9302 814624e7 2022-03-22 thomas static const struct got_error *
9303 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9304 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9305 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9306 814624e7 2022-03-22 thomas {
9307 814624e7 2022-03-22 thomas const struct got_error *err;
9308 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9309 814624e7 2022-03-22 thomas struct stat sb;
9310 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9311 814624e7 2022-03-22 thomas
9312 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9313 814624e7 2022-03-22 thomas if (err)
9314 814624e7 2022-03-22 thomas return err;
9315 814624e7 2022-03-22 thomas
9316 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9317 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9318 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9319 814624e7 2022-03-22 thomas
9320 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9321 814624e7 2022-03-22 thomas if (ie) {
9322 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9323 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9324 12de5570 2022-05-01 thomas repo);
9325 814624e7 2022-03-22 thomas if (err)
9326 814624e7 2022-03-22 thomas goto done;
9327 814624e7 2022-03-22 thomas } else {
9328 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9329 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9330 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9331 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9332 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9333 814624e7 2022-03-22 thomas ondisk_path);
9334 814624e7 2022-03-22 thomas goto done;
9335 814624e7 2022-03-22 thomas }
9336 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9337 814624e7 2022-03-22 thomas }
9338 814624e7 2022-03-22 thomas }
9339 814624e7 2022-03-22 thomas
9340 814624e7 2022-03-22 thomas done:
9341 814624e7 2022-03-22 thomas free(ondisk_path);
9342 814624e7 2022-03-22 thomas return err;
9343 814624e7 2022-03-22 thomas }
9344 814624e7 2022-03-22 thomas
9345 814624e7 2022-03-22 thomas static const struct got_error *
9346 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9347 814624e7 2022-03-22 thomas unsigned char staged_status)
9348 814624e7 2022-03-22 thomas {
9349 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9350 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9351 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9352 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9353 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9354 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9355 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9356 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9357 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9358 814624e7 2022-03-22 thomas return NULL;
9359 814624e7 2022-03-22 thomas }
9360 814624e7 2022-03-22 thomas
9361 814624e7 2022-03-22 thomas static const struct got_error *
9362 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9363 814624e7 2022-03-22 thomas {
9364 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9365 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9366 814624e7 2022-03-22 thomas return NULL;
9367 814624e7 2022-03-22 thomas }
9368 814624e7 2022-03-22 thomas
9369 814624e7 2022-03-22 thomas static const struct got_error *
9370 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9371 814624e7 2022-03-22 thomas unsigned char staged_status)
9372 814624e7 2022-03-22 thomas {
9373 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9374 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9375 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9376 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9377 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9378 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9379 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9380 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9381 814624e7 2022-03-22 thomas return NULL;
9382 814624e7 2022-03-22 thomas }
9383 814624e7 2022-03-22 thomas
9384 814624e7 2022-03-22 thomas const struct got_error *
9385 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9386 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9387 814624e7 2022-03-22 thomas {
9388 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9389 814624e7 2022-03-22 thomas }
9390 814624e7 2022-03-22 thomas
9391 814624e7 2022-03-22 thomas const struct got_error *
9392 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9393 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9394 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9395 814624e7 2022-03-22 thomas {
9396 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9397 814624e7 2022-03-22 thomas int file_renamed = 0;
9398 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9399 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9400 814624e7 2022-03-22 thomas
9401 814624e7 2022-03-22 thomas *oldpath = NULL;
9402 814624e7 2022-03-22 thomas *newpath = NULL;
9403 814624e7 2022-03-22 thomas
9404 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9405 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9406 814624e7 2022-03-22 thomas if (err)
9407 814624e7 2022-03-22 thomas goto done;
9408 814624e7 2022-03-22 thomas
9409 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9410 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9411 814624e7 2022-03-22 thomas if (err)
9412 814624e7 2022-03-22 thomas goto done;
9413 814624e7 2022-03-22 thomas
9414 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9415 814624e7 2022-03-22 thomas file_renamed = 1;
9416 814624e7 2022-03-22 thomas
9417 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9418 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9419 814624e7 2022-03-22 thomas else if (file_renamed) {
9420 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9421 814624e7 2022-03-22 thomas if (err == NULL)
9422 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9423 814624e7 2022-03-22 thomas } else if (old == NULL)
9424 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9425 814624e7 2022-03-22 thomas else
9426 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9427 814624e7 2022-03-22 thomas
9428 814624e7 2022-03-22 thomas done:
9429 814624e7 2022-03-22 thomas if (err) {
9430 814624e7 2022-03-22 thomas free(*oldpath);
9431 814624e7 2022-03-22 thomas *oldpath = NULL;
9432 814624e7 2022-03-22 thomas free(*newpath);
9433 814624e7 2022-03-22 thomas *newpath = NULL;
9434 814624e7 2022-03-22 thomas }
9435 814624e7 2022-03-22 thomas return err;
9436 814624e7 2022-03-22 thomas }
9437 814624e7 2022-03-22 thomas
9438 814624e7 2022-03-22 thomas const struct got_error *
9439 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9440 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9441 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9442 814624e7 2022-03-22 thomas {
9443 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9444 5e22b737 2022-05-31 thomas
9445 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9446 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9447 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9448 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9449 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9450 5e22b737 2022-05-31 thomas saa.repo = repo;
9451 5e22b737 2022-05-31 thomas
9452 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9453 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9454 814624e7 2022-03-22 thomas }
9455 5e22b737 2022-05-31 thomas
9456 5e22b737 2022-05-31 thomas const struct got_error *
9457 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9458 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9459 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9460 5e22b737 2022-05-31 thomas {
9461 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9462 5e22b737 2022-05-31 thomas
9463 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9464 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9465 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9466 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9467 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9468 5e22b737 2022-05-31 thomas sda.repo = repo;
9469 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9470 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9471 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9472 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9473 5e22b737 2022-05-31 thomas
9474 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9475 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9476 5e22b737 2022-05-31 thomas }
9477 5e22b737 2022-05-31 thomas
9478 5e22b737 2022-05-31 thomas const struct got_error *
9479 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9480 36832a8e 2022-05-31 thomas const char *fileindex_path)
9481 5e22b737 2022-05-31 thomas {
9482 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9483 5e22b737 2022-05-31 thomas
9484 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9485 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9486 5e22b737 2022-05-31 thomas
9487 5e22b737 2022-05-31 thomas return err;
9488 5e22b737 2022-05-31 thomas }