Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
999 67a66647 2021-05-31 stsp if (err)
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1002 db590691 2021-06-02 stsp } else {
1003 db590691 2021-06-02 stsp /*
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp */
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1012 db590691 2021-06-02 stsp }
1013 67a66647 2021-05-31 stsp }
1014 67a66647 2021-05-31 stsp
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1019 14c901f1 2019-08-08 stsp }
1020 14c901f1 2019-08-08 stsp
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp }
1037 eec2f5a9 2021-06-03 stsp
1038 b6b86fd1 2022-08-30 thomas /*
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp */
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp if (err)
1045 eec2f5a9 2021-06-03 stsp goto done;
1046 eec2f5a9 2021-06-03 stsp } else {
1047 eec2f5a9 2021-06-03 stsp int fd;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp }
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1058 eec2f5a9 2021-06-03 stsp }
1059 14c901f1 2019-08-08 stsp }
1060 14c901f1 2019-08-08 stsp
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 14c901f1 2019-08-08 stsp done:
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 67a66647 2021-05-31 stsp }
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp }
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1084 4a1ddfc2 2019-01-12 stsp }
1085 4a1ddfc2 2019-01-12 stsp
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp {
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 13d9040b 2019-03-27 stsp
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 65b05cec 2020-07-23 stsp
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp if (err)
1098 054041d0 2020-06-24 stsp return err;
1099 054041d0 2020-06-24 stsp
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1106 054041d0 2020-06-24 stsp done:
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp else
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1112 1ebedb77 2019-10-19 stsp }
1113 1ebedb77 2019-10-19 stsp
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp {
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1126 1ebedb77 2019-10-19 stsp }
1127 1ebedb77 2019-10-19 stsp
1128 a2c162eb 2022-10-30 thomas return st_mode;
1129 a2c162eb 2022-10-30 thomas }
1130 a2c162eb 2022-10-30 thomas
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1133 a2c162eb 2022-10-30 thomas {
1134 a2c162eb 2022-10-30 thomas mode_t um;
1135 a2c162eb 2022-10-30 thomas
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1139 13d9040b 2019-03-27 stsp }
1140 13d9040b 2019-03-27 stsp
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 8ba819a3 2020-07-23 stsp
1150 5a1fbc73 2020-07-23 stsp /*
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1153 5a1fbc73 2020-07-23 stsp */
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1157 5a1fbc73 2020-07-23 stsp {
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 5a1fbc73 2020-07-23 stsp int fd;
1162 5a1fbc73 2020-07-23 stsp
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 c6e8a826 2021-04-05 stsp
1165 5a1fbc73 2020-07-23 stsp /*
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1172 5a1fbc73 2020-07-23 stsp */
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1177 5a1fbc73 2020-07-23 stsp
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 5a1fbc73 2020-07-23 stsp }
1187 5a1fbc73 2020-07-23 stsp
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp }
1194 3c1ec782 2020-07-23 stsp
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 3c1ec782 2020-07-23 stsp {
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1208 3c1ec782 2020-07-23 stsp }
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp /*
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1216 3c1ec782 2020-07-23 stsp */
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1220 ce031e9e 2020-10-20 stsp if (err)
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1225 ce031e9e 2020-10-20 stsp }
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp }
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1234 3c1ec782 2020-07-23 stsp if (err)
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp } else {
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp if (err)
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp }
1258 5a1fbc73 2020-07-23 stsp
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 9d31a1d8 2018-03-11 stsp {
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 8ba819a3 2020-07-23 stsp
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 2e1fa222 2020-07-23 stsp
1275 9e39ca77 2022-07-21 thomas /*
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp */
1281 8ba819a3 2020-07-23 stsp do {
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1285 3521b466 2022-07-21 thomas
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1295 8ba819a3 2020-07-23 stsp }
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1305 8ba819a3 2020-07-23 stsp
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1308 3c1ec782 2020-07-23 stsp if (err)
1309 3c1ec782 2020-07-23 stsp return err;
1310 8ba819a3 2020-07-23 stsp
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1319 906c123b 2020-07-23 stsp }
1320 906c123b 2020-07-23 stsp
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1328 c90c8ce3 2020-07-23 stsp }
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1331 5a1fbc73 2020-07-23 stsp if (err)
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1339 c6e8a826 2021-04-05 stsp } else {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 c6e8a826 2021-04-05 stsp }
1343 f35fa46a 2020-07-23 stsp }
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp }
1346 f35fa46a 2020-07-23 stsp
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1350 f4994adc 2020-10-20 stsp if (err)
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1354 f35fa46a 2020-07-23 stsp if (err)
1355 9e39ca77 2022-07-21 thomas return err;
1356 f35fa46a 2020-07-23 stsp /*
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1359 f35fa46a 2020-07-23 stsp */
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1379 8ba819a3 2020-07-23 stsp } else {
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1382 8ba819a3 2020-07-23 stsp }
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp }
1388 8ba819a3 2020-07-23 stsp
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 8ba819a3 2020-07-23 stsp {
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1403 8ba819a3 2020-07-23 stsp
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1411 f5375317 2020-10-20 stsp if (err)
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1416 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp if (err)
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 a2c162eb 2022-10-30 thomas mode);
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1430 3b9f0f87 2020-07-23 stsp }
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1437 d70b8e30 2018-12-27 stsp } else {
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp if (err)
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1443 a2c162eb 2022-10-30 thomas
1444 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1445 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1446 a2c162eb 2022-10-30 thomas tmppath);
1447 a2c162eb 2022-10-30 thomas goto done;
1448 a2c162eb 2022-10-30 thomas }
1449 9d31a1d8 2018-03-11 stsp }
1450 507dc3bb 2018-12-29 stsp } else
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 3818e3c4 2020-11-01 naddy }
1453 3818e3c4 2020-11-01 naddy
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp if (err)
1465 bd6aa359 2020-07-23 stsp goto done;
1466 bd6aa359 2020-07-23 stsp }
1467 d7b62c98 2018-12-27 stsp
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp do {
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (err)
1473 9d31a1d8 2018-03-11 stsp break;
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1483 9d31a1d8 2018-03-11 stsp }
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp }
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 9d31a1d8 2018-03-11 stsp
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1491 816dc654 2019-02-16 stsp }
1492 9d31a1d8 2018-03-11 stsp
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1497 f6d8c0ac 2020-11-09 stsp }
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1502 68ed9ba5 2019-02-10 stsp }
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1505 507dc3bb 2018-12-29 stsp }
1506 507dc3bb 2018-12-29 stsp
1507 9d31a1d8 2018-03-11 stsp done:
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1514 6353ad76 2019-02-08 stsp }
1515 6353ad76 2019-02-08 stsp
1516 be94c032 2023-02-20 thomas /*
1517 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1519 be94c032 2023-02-20 thomas */
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1522 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1523 be94c032 2023-02-20 thomas FILE *ondisk_file)
1524 7154f6ce 2019-03-27 stsp {
1525 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1526 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1530 7154f6ce 2019-03-27 stsp };
1531 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1532 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1533 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1534 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1535 9bdd68dd 2020-01-02 naddy char *line = NULL;
1536 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1537 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1538 7154f6ce 2019-03-27 stsp
1539 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1540 8de9d8ad 2023-02-20 thomas return NULL;
1541 be94c032 2023-02-20 thomas
1542 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1543 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1544 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1545 be94c032 2023-02-20 thomas
1546 8de9d8ad 2023-02-20 thomas if (blob) {
1547 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1548 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 be94c032 2023-02-20 thomas if (err)
1550 be94c032 2023-02-20 thomas goto done;
1551 8de9d8ad 2023-02-20 thomas }
1552 be94c032 2023-02-20 thomas
1553 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 8de9d8ad 2023-02-20 thomas if (err)
1556 8de9d8ad 2023-02-20 thomas goto done;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1559 8de9d8ad 2023-02-20 thomas
1560 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1562 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1563 450d9f6b 2023-02-20 thomas off_t pos;
1564 8de9d8ad 2023-02-20 thomas
1565 8de9d8ad 2023-02-20 thomas /*
1566 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1567 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1568 8de9d8ad 2023-02-20 thomas */
1569 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1570 8de9d8ad 2023-02-20 thomas
1571 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1573 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1574 7154f6ce 2019-03-27 stsp }
1575 7154f6ce 2019-03-27 stsp
1576 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1577 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 450d9f6b 2023-02-20 thomas goto done;
1580 7154f6ce 2019-03-27 stsp }
1581 8de9d8ad 2023-02-20 thomas
1582 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1584 450d9f6b 2023-02-20 thomas
1585 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1586 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1587 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1588 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 8de9d8ad 2023-02-20 thomas break;
1592 8de9d8ad 2023-02-20 thomas }
1593 8de9d8ad 2023-02-20 thomas
1594 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1595 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1596 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1597 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1599 8de9d8ad 2023-02-20 thomas goto done;
1600 8de9d8ad 2023-02-20 thomas } else
1601 8de9d8ad 2023-02-20 thomas i++;
1602 8de9d8ad 2023-02-20 thomas }
1603 8de9d8ad 2023-02-20 thomas ++ln;
1604 8de9d8ad 2023-02-20 thomas }
1605 7154f6ce 2019-03-27 stsp }
1606 be94c032 2023-02-20 thomas
1607 be94c032 2023-02-20 thomas done:
1608 9bdd68dd 2020-01-02 naddy free(line);
1609 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1611 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1612 8de9d8ad 2023-02-20 thomas if (err == NULL)
1613 8de9d8ad 2023-02-20 thomas err = free_err;
1614 7154f6ce 2019-03-27 stsp
1615 7154f6ce 2019-03-27 stsp return err;
1616 e2b1e152 2019-07-13 stsp }
1617 e2b1e152 2019-07-13 stsp
1618 e2b1e152 2019-07-13 stsp static int
1619 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 1ebedb77 2019-10-19 stsp {
1621 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 1ebedb77 2019-10-19 stsp }
1624 1ebedb77 2019-10-19 stsp
1625 1ebedb77 2019-10-19 stsp static int
1626 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 e2b1e152 2019-07-13 stsp {
1628 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1633 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1634 c363b2c1 2019-08-03 stsp }
1635 c363b2c1 2019-08-03 stsp
1636 c363b2c1 2019-08-03 stsp static unsigned char
1637 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1638 c363b2c1 2019-08-03 stsp {
1639 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1644 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1646 c363b2c1 2019-08-03 stsp default:
1647 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp }
1650 a919d5c4 2020-07-23 stsp
1651 a919d5c4 2020-07-23 stsp static const struct got_error *
1652 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1653 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1654 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1655 a919d5c4 2020-07-23 stsp {
1656 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1657 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1658 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1659 a919d5c4 2020-07-23 stsp ssize_t elen;
1660 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1661 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1665 a919d5c4 2020-07-23 stsp
1666 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1667 a919d5c4 2020-07-23 stsp do {
1668 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1669 a919d5c4 2020-07-23 stsp if (err)
1670 a919d5c4 2020-07-23 stsp return err;
1671 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1672 a919d5c4 2020-07-23 stsp /*
1673 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1674 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1675 a919d5c4 2020-07-23 stsp */
1676 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1677 a919d5c4 2020-07-23 stsp }
1678 a919d5c4 2020-07-23 stsp if (len > 0) {
1679 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1680 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1681 a919d5c4 2020-07-23 stsp len - hdrlen);
1682 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1683 a919d5c4 2020-07-23 stsp hdrlen = 0;
1684 a919d5c4 2020-07-23 stsp }
1685 a919d5c4 2020-07-23 stsp } while (len != 0);
1686 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1687 a919d5c4 2020-07-23 stsp
1688 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1689 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 a919d5c4 2020-07-23 stsp if (elen == -1)
1691 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1692 a919d5c4 2020-07-23 stsp } else {
1693 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1694 a919d5c4 2020-07-23 stsp if (elen == -1)
1695 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1696 c363b2c1 2019-08-03 stsp }
1697 a919d5c4 2020-07-23 stsp
1698 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 a919d5c4 2020-07-23 stsp
1701 a919d5c4 2020-07-23 stsp return NULL;
1702 7154f6ce 2019-03-27 stsp }
1703 7154f6ce 2019-03-27 stsp
1704 7154f6ce 2019-03-27 stsp static const struct got_error *
1705 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1706 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1707 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1708 6353ad76 2019-02-08 stsp {
1709 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1710 6353ad76 2019-02-08 stsp struct got_object_id id;
1711 6353ad76 2019-02-08 stsp size_t hdrlen;
1712 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1713 6353ad76 2019-02-08 stsp FILE *f = NULL;
1714 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1715 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1716 6353ad76 2019-02-08 stsp size_t flen, blen;
1717 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1718 6353ad76 2019-02-08 stsp
1719 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1720 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1721 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1722 6353ad76 2019-02-08 stsp
1723 7f91a133 2019-12-13 stsp /*
1724 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1725 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1726 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1727 7f91a133 2019-12-13 stsp */
1728 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1729 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1731 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 882ef1b9 2019-12-13 stsp else
1734 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1735 882ef1b9 2019-12-13 stsp goto done;
1736 882ef1b9 2019-12-13 stsp }
1737 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1738 3d35a492 2019-12-13 stsp goto done;
1739 3d35a492 2019-12-13 stsp }
1740 7f91a133 2019-12-13 stsp } else {
1741 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1743 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1744 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1745 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1747 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1748 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1750 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1752 3d35a492 2019-12-13 stsp else
1753 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1754 3d35a492 2019-12-13 stsp goto done;
1755 3d35a492 2019-12-13 stsp }
1756 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1757 1338848f 2019-12-13 stsp goto done;
1758 a378724f 2019-02-10 stsp }
1759 a378724f 2019-02-10 stsp }
1760 6353ad76 2019-02-08 stsp
1761 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1763 1338848f 2019-12-13 stsp goto done;
1764 3f148bc6 2019-07-27 stsp }
1765 efdd40df 2019-07-27 stsp
1766 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1768 1338848f 2019-12-13 stsp goto done;
1769 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1770 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1771 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1772 1338848f 2019-12-13 stsp goto done;
1773 d00136be 2019-03-26 stsp }
1774 b8f41171 2019-02-10 stsp
1775 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1776 f179e66d 2020-07-23 stsp goto done;
1777 f179e66d 2020-07-23 stsp
1778 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1779 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1781 1338848f 2019-12-13 stsp goto done;
1782 f179e66d 2020-07-23 stsp }
1783 6353ad76 2019-02-08 stsp
1784 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1785 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp else
1788 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1789 c363b2c1 2019-08-03 stsp
1790 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1791 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1792 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1793 f4ae6ddb 2022-07-01 thomas goto done;
1794 f4ae6ddb 2022-07-01 thomas }
1795 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 6353ad76 2019-02-08 stsp if (err)
1797 1338848f 2019-12-13 stsp goto done;
1798 6353ad76 2019-02-08 stsp
1799 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1800 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1801 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1802 a919d5c4 2020-07-23 stsp goto done;
1803 a919d5c4 2020-07-23 stsp }
1804 a919d5c4 2020-07-23 stsp
1805 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1806 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 ab0d4361 2019-12-13 stsp if (fd == -1) {
1808 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1809 ab0d4361 2019-12-13 stsp goto done;
1810 ab0d4361 2019-12-13 stsp }
1811 3d35a492 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp
1813 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1814 6353ad76 2019-02-08 stsp if (f == NULL) {
1815 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1816 6353ad76 2019-02-08 stsp goto done;
1817 6353ad76 2019-02-08 stsp }
1818 1338848f 2019-12-13 stsp fd = -1;
1819 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1820 656b1f76 2019-05-11 jcs for (;;) {
1821 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1823 6353ad76 2019-02-08 stsp if (err)
1824 7154f6ce 2019-03-27 stsp goto done;
1825 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1826 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1828 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1829 7154f6ce 2019-03-27 stsp goto done;
1830 80c5c120 2019-02-19 stsp }
1831 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1832 6353ad76 2019-02-08 stsp if (flen != 0)
1833 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1834 6353ad76 2019-02-08 stsp break;
1835 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1836 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1837 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp break;
1839 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1840 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1841 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1843 6353ad76 2019-02-08 stsp break;
1844 6353ad76 2019-02-08 stsp }
1845 6353ad76 2019-02-08 stsp } else {
1846 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1847 6353ad76 2019-02-08 stsp break;
1848 6353ad76 2019-02-08 stsp }
1849 6353ad76 2019-02-08 stsp hdrlen = 0;
1850 6353ad76 2019-02-08 stsp }
1851 7154f6ce 2019-03-27 stsp
1852 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1853 7154f6ce 2019-03-27 stsp rewind(f);
1854 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1855 be94c032 2023-02-20 thomas sb, f);
1856 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1857 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1858 6353ad76 2019-02-08 stsp done:
1859 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1861 6353ad76 2019-02-08 stsp if (blob)
1862 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1863 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1864 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1865 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1866 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1867 9d31a1d8 2018-03-11 stsp return err;
1868 e2b1e152 2019-07-13 stsp }
1869 e2b1e152 2019-07-13 stsp
1870 e2b1e152 2019-07-13 stsp /*
1871 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1872 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1873 e2b1e152 2019-07-13 stsp */
1874 e2b1e152 2019-07-13 stsp static const struct got_error *
1875 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1877 e2b1e152 2019-07-13 stsp {
1878 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1880 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1881 e2b1e152 2019-07-13 stsp
1882 e2b1e152 2019-07-13 stsp return NULL;
1883 9d31a1d8 2018-03-11 stsp }
1884 9d31a1d8 2018-03-11 stsp
1885 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1886 9f323212 2023-03-10 thomas
1887 9d31a1d8 2018-03-11 stsp static const struct got_error *
1888 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1889 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1891 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 0584f854 2019-04-06 stsp void *progress_arg)
1893 9d31a1d8 2018-03-11 stsp {
1894 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1895 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1896 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1897 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1898 b8f41171 2019-02-10 stsp struct stat sb;
1899 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1900 9d31a1d8 2018-03-11 stsp
1901 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1903 6353ad76 2019-02-08 stsp
1904 abb4604f 2019-07-27 stsp if (ie) {
1905 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 a76c42e6 2019-08-03 stsp goto done;
1908 a76c42e6 2019-08-03 stsp }
1909 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 7f91a133 2019-12-13 stsp repo);
1911 abb4604f 2019-07-27 stsp if (err)
1912 abb4604f 2019-07-27 stsp goto done;
1913 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1915 c90c8ce3 2020-07-23 stsp } else {
1916 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1917 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1918 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1919 e6f4ba31 2021-09-24 thomas ondisk_path);
1920 e6f4ba31 2021-09-24 thomas goto done;
1921 e6f4ba31 2021-09-24 thomas }
1922 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1924 e6f4ba31 2021-09-24 thomas } else {
1925 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1927 e6f4ba31 2021-09-24 thomas else
1928 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1929 e6f4ba31 2021-09-24 thomas }
1930 c90c8ce3 2020-07-23 stsp }
1931 6353ad76 2019-02-08 stsp
1932 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1933 2c41dce7 2021-06-27 stsp if (ie)
1934 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1935 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1936 b8f41171 2019-02-10 stsp goto done;
1937 b8f41171 2019-02-10 stsp }
1938 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1939 a769b60b 2021-06-27 stsp if (ie)
1940 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1941 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 5036ab18 2020-04-18 stsp path);
1943 5036ab18 2020-04-18 stsp goto done;
1944 9f323212 2023-03-10 thomas }
1945 9f323212 2023-03-10 thomas
1946 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1948 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1949 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1950 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1951 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1952 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1953 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1954 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1955 9f323212 2023-03-10 thomas } else if (ie) {
1956 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1957 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1958 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1959 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1960 9f323212 2023-03-10 thomas ie->path);
1961 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1962 9f323212 2023-03-10 thomas errno == ENOENT))
1963 9f323212 2023-03-10 thomas goto done;
1964 9f323212 2023-03-10 thomas }
1965 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1966 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 9f323212 2023-03-10 thomas ie->path);
1968 9f323212 2023-03-10 thomas }
1969 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1970 5036ab18 2020-04-18 stsp }
1971 b8f41171 2019-02-10 stsp
1972 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1974 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1975 c6e8a826 2021-04-05 stsp /*
1976 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1977 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1978 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1979 c6e8a826 2021-04-05 stsp * updating contents of this file.
1980 c6e8a826 2021-04-05 stsp */
1981 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1982 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1984 c6e8a826 2021-04-05 stsp /* Same commit. */
1985 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1986 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1987 e2b1e152 2019-07-13 stsp if (err)
1988 e2b1e152 2019-07-13 stsp goto done;
1989 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 b8f41171 2019-02-10 stsp path);
1991 6353ad76 2019-02-08 stsp goto done;
1992 a378724f 2019-02-10 stsp }
1993 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1994 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1995 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1996 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1997 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1998 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1999 0f58026f 2021-04-05 stsp if (err)
2000 0f58026f 2021-04-05 stsp goto done;
2001 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2002 0f58026f 2021-04-05 stsp path);
2003 b8f41171 2019-02-10 stsp goto done;
2004 e2b1e152 2019-07-13 stsp }
2005 9d31a1d8 2018-03-11 stsp }
2006 9d31a1d8 2018-03-11 stsp
2007 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2008 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2009 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2010 f4ae6ddb 2022-07-01 thomas goto done;
2011 f4ae6ddb 2022-07-01 thomas }
2012 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2013 8da9e5f4 2019-01-12 stsp if (err)
2014 6353ad76 2019-02-08 stsp goto done;
2015 512f0d0e 2019-01-02 stsp
2016 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2017 234035bc 2019-06-01 stsp int update_timestamps;
2018 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2019 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2020 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2021 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2022 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2023 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2024 f4ae6ddb 2022-07-01 thomas goto done;
2025 f4ae6ddb 2022-07-01 thomas }
2026 234035bc 2019-06-01 stsp struct got_object_id id2;
2027 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2028 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2029 f4ae6ddb 2022-07-01 thomas fd2);
2030 234035bc 2019-06-01 stsp if (err)
2031 f69721c3 2019-10-21 stsp goto done;
2032 f69721c3 2019-10-21 stsp }
2033 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2034 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2035 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2036 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2037 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2038 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2039 f69721c3 2019-10-21 stsp goto done;
2040 f69721c3 2019-10-21 stsp }
2041 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2042 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2043 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2044 234035bc 2019-06-01 stsp goto done;
2045 f69721c3 2019-10-21 stsp }
2046 234035bc 2019-06-01 stsp }
2047 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2048 36bf999c 2020-07-23 stsp char *link_target;
2049 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2050 36bf999c 2020-07-23 stsp if (err)
2051 36bf999c 2020-07-23 stsp goto done;
2052 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2053 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2054 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2055 36bf999c 2020-07-23 stsp free(link_target);
2056 993e2a1b 2020-07-23 stsp } else {
2057 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2058 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2059 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2060 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2061 993e2a1b 2020-07-23 stsp }
2062 f69721c3 2019-10-21 stsp free(label_orig);
2063 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2064 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2065 f4ae6ddb 2022-07-01 thomas goto done;
2066 f4ae6ddb 2022-07-01 thomas }
2067 234035bc 2019-06-01 stsp if (blob2)
2068 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2069 909d120e 2019-09-22 stsp if (err)
2070 909d120e 2019-09-22 stsp goto done;
2071 234035bc 2019-06-01 stsp /*
2072 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2073 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2074 234035bc 2019-06-01 stsp * unmodified files again.
2075 234035bc 2019-06-01 stsp */
2076 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2077 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2078 234035bc 2019-06-01 stsp update_timestamps);
2079 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2080 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2081 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2082 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2083 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2084 1ee397ad 2019-07-12 stsp if (err)
2085 1ee397ad 2019-07-12 stsp goto done;
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 13d9040b 2019-03-27 stsp if (err)
2089 13d9040b 2019-03-27 stsp goto done;
2090 13d9040b 2019-03-27 stsp } else {
2091 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2092 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2093 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2094 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2095 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2096 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2097 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2098 2e1fa222 2020-07-23 stsp } else {
2099 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2100 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2101 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2102 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2103 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2104 2e1fa222 2020-07-23 stsp }
2105 13d9040b 2019-03-27 stsp if (err)
2106 13d9040b 2019-03-27 stsp goto done;
2107 65b05cec 2020-07-23 stsp
2108 054041d0 2020-06-24 stsp if (ie) {
2109 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2110 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2111 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2112 054041d0 2020-06-24 stsp } else {
2113 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2114 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2115 054041d0 2020-06-24 stsp &blob->id);
2116 054041d0 2020-06-24 stsp }
2117 13d9040b 2019-03-27 stsp if (err)
2118 13d9040b 2019-03-27 stsp goto done;
2119 65b05cec 2020-07-23 stsp
2120 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2121 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2122 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2123 2e1fa222 2020-07-23 stsp }
2124 13d9040b 2019-03-27 stsp }
2125 f4ae6ddb 2022-07-01 thomas
2126 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2127 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2128 f4ae6ddb 2022-07-01 thomas goto done;
2129 f4ae6ddb 2022-07-01 thomas }
2130 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2131 6353ad76 2019-02-08 stsp done:
2132 6353ad76 2019-02-08 stsp free(ondisk_path);
2133 8da9e5f4 2019-01-12 stsp return err;
2134 90285c3b 2019-01-08 stsp }
2135 90285c3b 2019-01-08 stsp
2136 90285c3b 2019-01-08 stsp static const struct got_error *
2137 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2138 90285c3b 2019-01-08 stsp {
2139 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2140 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2141 90285c3b 2019-01-08 stsp
2142 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2143 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2144 90285c3b 2019-01-08 stsp
2145 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2146 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2147 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2148 c97665ae 2019-01-13 stsp } else {
2149 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2150 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2151 1c4cdd89 2021-06-20 stsp if (err)
2152 1c4cdd89 2021-06-20 stsp goto done;
2153 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2154 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2155 fddefe3b 2020-10-20 stsp free(ondisk_path);
2156 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2157 1c4cdd89 2021-06-20 stsp parent = NULL;
2158 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2159 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2160 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2161 fddefe3b 2020-10-20 stsp ondisk_path);
2162 90285c3b 2019-01-08 stsp break;
2163 90285c3b 2019-01-08 stsp }
2164 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2165 1c4cdd89 2021-06-20 stsp if (err)
2166 1c4cdd89 2021-06-20 stsp break;
2167 1c4cdd89 2021-06-20 stsp }
2168 90285c3b 2019-01-08 stsp }
2169 1c4cdd89 2021-06-20 stsp done:
2170 90285c3b 2019-01-08 stsp free(ondisk_path);
2171 1c4cdd89 2021-06-20 stsp free(parent);
2172 90285c3b 2019-01-08 stsp return err;
2173 512f0d0e 2019-01-02 stsp }
2174 512f0d0e 2019-01-02 stsp
2175 708d8e67 2019-03-27 stsp static const struct got_error *
2176 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2177 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2178 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2179 708d8e67 2019-03-27 stsp {
2180 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2181 708d8e67 2019-03-27 stsp unsigned char status;
2182 708d8e67 2019-03-27 stsp struct stat sb;
2183 708d8e67 2019-03-27 stsp char *ondisk_path;
2184 708d8e67 2019-03-27 stsp
2185 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2186 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2187 a76c42e6 2019-08-03 stsp
2188 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2189 708d8e67 2019-03-27 stsp == -1)
2190 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2191 708d8e67 2019-03-27 stsp
2192 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2193 708d8e67 2019-03-27 stsp if (err)
2194 5a58a424 2020-06-23 stsp goto done;
2195 708d8e67 2019-03-27 stsp
2196 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2197 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2198 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2199 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2200 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2201 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2202 993e2a1b 2020-07-23 stsp goto done;
2203 993e2a1b 2020-07-23 stsp }
2204 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2205 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2206 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2207 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2208 993e2a1b 2020-07-23 stsp if (err)
2209 993e2a1b 2020-07-23 stsp goto done;
2210 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2211 993e2a1b 2020-07-23 stsp ie->path);
2212 993e2a1b 2020-07-23 stsp goto done;
2213 993e2a1b 2020-07-23 stsp }
2214 993e2a1b 2020-07-23 stsp
2215 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2216 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2217 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2218 1ee397ad 2019-07-12 stsp if (err)
2219 5a58a424 2020-06-23 stsp goto done;
2220 fc6346c4 2019-03-27 stsp /*
2221 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2222 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2223 fc6346c4 2019-03-27 stsp */
2224 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2225 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2226 fc6346c4 2019-03-27 stsp } else {
2227 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2228 1ee397ad 2019-07-12 stsp if (err)
2229 5a58a424 2020-06-23 stsp goto done;
2230 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2231 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2232 fc6346c4 2019-03-27 stsp if (err)
2233 5a58a424 2020-06-23 stsp goto done;
2234 fc6346c4 2019-03-27 stsp }
2235 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2236 708d8e67 2019-03-27 stsp }
2237 5a58a424 2020-06-23 stsp done:
2238 5a58a424 2020-06-23 stsp free(ondisk_path);
2239 fc6346c4 2019-03-27 stsp return err;
2240 708d8e67 2019-03-27 stsp }
2241 708d8e67 2019-03-27 stsp
2242 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2243 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2244 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2245 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2246 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2247 8da9e5f4 2019-01-12 stsp void *progress_arg;
2248 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2249 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2250 8da9e5f4 2019-01-12 stsp };
2251 8da9e5f4 2019-01-12 stsp
2252 512f0d0e 2019-01-02 stsp static const struct got_error *
2253 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2254 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2255 512f0d0e 2019-01-02 stsp {
2256 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2257 0584f854 2019-04-06 stsp
2258 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2259 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2260 512f0d0e 2019-01-02 stsp
2261 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2262 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2263 8da9e5f4 2019-01-12 stsp }
2264 512f0d0e 2019-01-02 stsp
2265 8da9e5f4 2019-01-12 stsp static const struct got_error *
2266 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2267 8da9e5f4 2019-01-12 stsp {
2268 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2269 7a9df742 2019-01-08 stsp
2270 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2271 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2272 0584f854 2019-04-06 stsp
2273 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2274 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2275 9d31a1d8 2018-03-11 stsp }
2276 9d31a1d8 2018-03-11 stsp
2277 9d31a1d8 2018-03-11 stsp static const struct got_error *
2278 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2279 9d31a1d8 2018-03-11 stsp {
2280 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2281 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2282 8da9e5f4 2019-01-12 stsp char *path;
2283 9d31a1d8 2018-03-11 stsp
2284 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2285 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2286 0584f854 2019-04-06 stsp
2287 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2288 63c5ca5d 2019-08-24 stsp return NULL;
2289 63c5ca5d 2019-08-24 stsp
2290 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2291 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2292 8da9e5f4 2019-01-12 stsp == -1)
2293 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2294 9d31a1d8 2018-03-11 stsp
2295 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2296 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2297 8da9e5f4 2019-01-12 stsp else
2298 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2299 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2300 9d31a1d8 2018-03-11 stsp
2301 8da9e5f4 2019-01-12 stsp free(path);
2302 0cd1c46a 2019-03-11 stsp return err;
2303 0cd1c46a 2019-03-11 stsp }
2304 0cd1c46a 2019-03-11 stsp
2305 b2118c49 2020-07-28 stsp const struct got_error *
2306 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2307 b2118c49 2020-07-28 stsp {
2308 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2309 b2118c49 2020-07-28 stsp
2310 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2311 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2312 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2313 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2314 b2118c49 2020-07-28 stsp }
2315 b2118c49 2020-07-28 stsp
2316 b2118c49 2020-07-28 stsp return NULL;
2317 b2118c49 2020-07-28 stsp }
2318 b2118c49 2020-07-28 stsp
2319 818c7501 2019-07-11 stsp static const struct got_error *
2320 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2321 0cd1c46a 2019-03-11 stsp {
2322 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2323 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2324 0cd1c46a 2019-03-11 stsp
2325 517bab73 2019-03-11 stsp *refname = NULL;
2326 517bab73 2019-03-11 stsp
2327 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2328 b2118c49 2020-07-28 stsp if (err)
2329 b2118c49 2020-07-28 stsp return err;
2330 0cd1c46a 2019-03-11 stsp
2331 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2332 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2333 0647c563 2019-03-11 stsp *refname = NULL;
2334 0cd1c46a 2019-03-11 stsp }
2335 517bab73 2019-03-11 stsp free(uuidstr);
2336 517bab73 2019-03-11 stsp return err;
2337 517bab73 2019-03-11 stsp }
2338 517bab73 2019-03-11 stsp
2339 818c7501 2019-07-11 stsp const struct got_error *
2340 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2341 f6cd0243 2023-01-28 thomas const char *prefix)
2342 f6cd0243 2023-01-28 thomas {
2343 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2344 f6cd0243 2023-01-28 thomas }
2345 f6cd0243 2023-01-28 thomas
2346 f6cd0243 2023-01-28 thomas const struct got_error *
2347 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2348 818c7501 2019-07-11 stsp {
2349 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2350 818c7501 2019-07-11 stsp }
2351 818c7501 2019-07-11 stsp
2352 818c7501 2019-07-11 stsp static const struct got_error *
2353 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2354 818c7501 2019-07-11 stsp {
2355 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2356 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2357 818c7501 2019-07-11 stsp }
2358 818c7501 2019-07-11 stsp
2359 818c7501 2019-07-11 stsp static const struct got_error *
2360 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2361 818c7501 2019-07-11 stsp {
2362 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2363 818c7501 2019-07-11 stsp }
2364 818c7501 2019-07-11 stsp
2365 818c7501 2019-07-11 stsp static const struct got_error *
2366 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2367 818c7501 2019-07-11 stsp {
2368 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2369 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2370 818c7501 2019-07-11 stsp }
2371 818c7501 2019-07-11 stsp
2372 818c7501 2019-07-11 stsp static const struct got_error *
2373 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 818c7501 2019-07-11 stsp {
2375 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2376 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2377 0ebf8283 2019-07-24 stsp }
2378 0ebf8283 2019-07-24 stsp
2379 0ebf8283 2019-07-24 stsp static const struct got_error *
2380 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2381 0ebf8283 2019-07-24 stsp {
2382 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2383 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2384 0ebf8283 2019-07-24 stsp }
2385 0ebf8283 2019-07-24 stsp
2386 0ebf8283 2019-07-24 stsp static const struct got_error *
2387 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2388 0ebf8283 2019-07-24 stsp {
2389 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2390 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2391 0ebf8283 2019-07-24 stsp }
2392 0ebf8283 2019-07-24 stsp
2393 0ebf8283 2019-07-24 stsp static const struct got_error *
2394 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2395 0ebf8283 2019-07-24 stsp {
2396 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2397 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2398 818c7501 2019-07-11 stsp }
2399 818c7501 2019-07-11 stsp
2400 0ebf8283 2019-07-24 stsp static const struct got_error *
2401 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 0ebf8283 2019-07-24 stsp {
2403 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2404 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2405 0ebf8283 2019-07-24 stsp }
2406 818c7501 2019-07-11 stsp
2407 0ebf8283 2019-07-24 stsp const struct got_error *
2408 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2409 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2410 0ebf8283 2019-07-24 stsp {
2411 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2412 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2413 0ebf8283 2019-07-24 stsp *path = NULL;
2414 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2415 0ebf8283 2019-07-24 stsp }
2416 0ebf8283 2019-07-24 stsp return NULL;
2417 10604dce 2021-09-24 thomas }
2418 10604dce 2021-09-24 thomas
2419 10604dce 2021-09-24 thomas static const struct got_error *
2420 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2421 10604dce 2021-09-24 thomas {
2422 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2423 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2424 0ebf8283 2019-07-24 stsp }
2425 0ebf8283 2019-07-24 stsp
2426 10604dce 2021-09-24 thomas static const struct got_error *
2427 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2428 10604dce 2021-09-24 thomas {
2429 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2430 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2431 10604dce 2021-09-24 thomas }
2432 10604dce 2021-09-24 thomas
2433 517bab73 2019-03-11 stsp /*
2434 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2435 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2436 517bab73 2019-03-11 stsp */
2437 517bab73 2019-03-11 stsp static const struct got_error *
2438 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2439 517bab73 2019-03-11 stsp {
2440 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2441 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2442 517bab73 2019-03-11 stsp char *refname;
2443 517bab73 2019-03-11 stsp
2444 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2445 517bab73 2019-03-11 stsp if (err)
2446 517bab73 2019-03-11 stsp return err;
2447 0cd1c46a 2019-03-11 stsp
2448 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2449 0cd1c46a 2019-03-11 stsp if (err)
2450 0cd1c46a 2019-03-11 stsp goto done;
2451 0cd1c46a 2019-03-11 stsp
2452 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2453 0cd1c46a 2019-03-11 stsp done:
2454 0cd1c46a 2019-03-11 stsp free(refname);
2455 0cd1c46a 2019-03-11 stsp if (ref)
2456 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2457 3e3a69f1 2019-07-25 stsp return err;
2458 3e3a69f1 2019-07-25 stsp }
2459 3e3a69f1 2019-07-25 stsp
2460 3e3a69f1 2019-07-25 stsp static const struct got_error *
2461 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2462 3e3a69f1 2019-07-25 stsp {
2463 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2464 3e3a69f1 2019-07-25 stsp
2465 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2466 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2467 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2468 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2469 3e3a69f1 2019-07-25 stsp }
2470 9d31a1d8 2018-03-11 stsp return err;
2471 9d31a1d8 2018-03-11 stsp }
2472 9d31a1d8 2018-03-11 stsp
2473 3e3a69f1 2019-07-25 stsp
2474 ebf99748 2019-05-09 stsp static const struct got_error *
2475 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2476 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2477 ebf99748 2019-05-09 stsp {
2478 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2479 ebf99748 2019-05-09 stsp FILE *index = NULL;
2480 0cd1c46a 2019-03-11 stsp
2481 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2482 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2483 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2484 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2485 ebf99748 2019-05-09 stsp
2486 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2487 3e3a69f1 2019-07-25 stsp if (err)
2488 ebf99748 2019-05-09 stsp goto done;
2489 ebf99748 2019-05-09 stsp
2490 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2491 ebf99748 2019-05-09 stsp if (index == NULL) {
2492 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2493 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2494 ebf99748 2019-05-09 stsp } else {
2495 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2496 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2497 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2498 ebf99748 2019-05-09 stsp }
2499 ebf99748 2019-05-09 stsp done:
2500 ebf99748 2019-05-09 stsp if (err) {
2501 ebf99748 2019-05-09 stsp free(*fileindex_path);
2502 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2503 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2504 ebf99748 2019-05-09 stsp *fileindex = NULL;
2505 ebf99748 2019-05-09 stsp }
2506 ebf99748 2019-05-09 stsp return err;
2507 ebf99748 2019-05-09 stsp }
2508 c932eeeb 2019-05-22 stsp
2509 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2510 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2511 c932eeeb 2019-05-22 stsp const char *path;
2512 c932eeeb 2019-05-22 stsp size_t path_len;
2513 c932eeeb 2019-05-22 stsp const char *entry_name;
2514 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2515 a484d721 2019-06-10 stsp void *progress_arg;
2516 c932eeeb 2019-05-22 stsp };
2517 ebf99748 2019-05-09 stsp
2518 c932eeeb 2019-05-22 stsp static const struct got_error *
2519 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2520 c932eeeb 2019-05-22 stsp {
2521 1ee397ad 2019-07-12 stsp const struct got_error *err;
2522 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2523 c932eeeb 2019-05-22 stsp
2524 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2525 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2526 c932eeeb 2019-05-22 stsp return NULL;
2527 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2528 102ff934 2019-06-11 stsp return NULL;
2529 102ff934 2019-06-11 stsp
2530 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2531 a769b60b 2021-06-27 stsp return NULL;
2532 a769b60b 2021-06-27 stsp
2533 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2534 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2535 c932eeeb 2019-05-22 stsp return NULL;
2536 c932eeeb 2019-05-22 stsp
2537 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2538 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2539 edd02c5e 2019-07-11 stsp ie->path);
2540 1ee397ad 2019-07-12 stsp if (err)
2541 1ee397ad 2019-07-12 stsp return err;
2542 1ee397ad 2019-07-12 stsp }
2543 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2544 c932eeeb 2019-05-22 stsp return NULL;
2545 a615e0e7 2020-12-16 stsp }
2546 a615e0e7 2020-12-16 stsp
2547 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2548 a615e0e7 2020-12-16 stsp static const struct got_error *
2549 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2550 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2551 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2552 a615e0e7 2020-12-16 stsp {
2553 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2554 a615e0e7 2020-12-16 stsp
2555 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2556 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2557 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2558 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2559 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2560 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2561 a615e0e7 2020-12-16 stsp
2562 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2563 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2564 9c6338c4 2019-06-02 stsp }
2565 9c6338c4 2019-06-02 stsp
2566 9c6338c4 2019-06-02 stsp static const struct got_error *
2567 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2568 9c6338c4 2019-06-02 stsp {
2569 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2570 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2571 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2572 867630bb 2020-01-17 stsp struct timespec timeout;
2573 9c6338c4 2019-06-02 stsp
2574 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2575 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2576 9c6338c4 2019-06-02 stsp if (err)
2577 9c6338c4 2019-06-02 stsp goto done;
2578 9c6338c4 2019-06-02 stsp
2579 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2580 9c6338c4 2019-06-02 stsp if (err)
2581 9c6338c4 2019-06-02 stsp goto done;
2582 9c6338c4 2019-06-02 stsp
2583 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2584 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2585 9c6338c4 2019-06-02 stsp fileindex_path);
2586 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2587 9c6338c4 2019-06-02 stsp }
2588 867630bb 2020-01-17 stsp
2589 867630bb 2020-01-17 stsp /*
2590 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2591 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2592 867630bb 2020-01-17 stsp * was recorded in the file index.
2593 867630bb 2020-01-17 stsp */
2594 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2595 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2596 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2597 9c6338c4 2019-06-02 stsp done:
2598 9c6338c4 2019-06-02 stsp if (new_index)
2599 9c6338c4 2019-06-02 stsp fclose(new_index);
2600 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2601 9c6338c4 2019-06-02 stsp return err;
2602 c932eeeb 2019-05-22 stsp }
2603 6ced4176 2019-07-12 stsp
2604 6ced4176 2019-07-12 stsp static const struct got_error *
2605 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2606 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2607 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2608 945f9229 2022-04-16 thomas struct got_repository *repo)
2609 6ced4176 2019-07-12 stsp {
2610 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2611 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2612 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2613 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2614 6ced4176 2019-07-12 stsp
2615 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2616 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2617 6ced4176 2019-07-12 stsp *tree_id = NULL;
2618 6ced4176 2019-07-12 stsp
2619 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2620 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2621 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2622 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2623 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2624 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2625 6ced4176 2019-07-12 stsp goto done;
2626 6ced4176 2019-07-12 stsp }
2627 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2628 945f9229 2022-04-16 thomas worktree->path_prefix);
2629 6ced4176 2019-07-12 stsp if (err)
2630 6ced4176 2019-07-12 stsp goto done;
2631 6ced4176 2019-07-12 stsp return NULL;
2632 6ced4176 2019-07-12 stsp }
2633 6ced4176 2019-07-12 stsp
2634 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2635 6ced4176 2019-07-12 stsp
2636 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2637 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2638 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2639 6ced4176 2019-07-12 stsp goto done;
2640 6ced4176 2019-07-12 stsp }
2641 6ced4176 2019-07-12 stsp
2642 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2643 6ced4176 2019-07-12 stsp if (err)
2644 6ced4176 2019-07-12 stsp goto done;
2645 6ced4176 2019-07-12 stsp
2646 6ced4176 2019-07-12 stsp free(in_repo_path);
2647 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2648 6ced4176 2019-07-12 stsp
2649 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2650 6ced4176 2019-07-12 stsp if (err)
2651 6ced4176 2019-07-12 stsp goto done;
2652 c932eeeb 2019-05-22 stsp
2653 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2654 6ced4176 2019-07-12 stsp /* Check out a single file. */
2655 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2656 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2657 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2658 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2659 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2660 6ced4176 2019-07-12 stsp goto done;
2661 6ced4176 2019-07-12 stsp }
2662 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2663 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2664 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2665 6ced4176 2019-07-12 stsp goto done;
2666 6ced4176 2019-07-12 stsp }
2667 6ced4176 2019-07-12 stsp } else {
2668 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2669 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2670 6ced4176 2019-07-12 stsp if (err)
2671 6ced4176 2019-07-12 stsp return err;
2672 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2673 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2674 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2675 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2676 6ced4176 2019-07-12 stsp goto done;
2677 6ced4176 2019-07-12 stsp }
2678 6ced4176 2019-07-12 stsp }
2679 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2680 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2681 6ced4176 2019-07-12 stsp } else {
2682 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2683 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2684 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2685 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2686 6ced4176 2019-07-12 stsp goto done;
2687 6ced4176 2019-07-12 stsp }
2688 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2689 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2690 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2691 6ced4176 2019-07-12 stsp goto done;
2692 6ced4176 2019-07-12 stsp }
2693 6ced4176 2019-07-12 stsp }
2694 6ced4176 2019-07-12 stsp done:
2695 6ced4176 2019-07-12 stsp free(id);
2696 6ced4176 2019-07-12 stsp free(in_repo_path);
2697 6ced4176 2019-07-12 stsp if (err) {
2698 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2699 6ced4176 2019-07-12 stsp free(*tree_relpath);
2700 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2701 6ced4176 2019-07-12 stsp free(*tree_id);
2702 6ced4176 2019-07-12 stsp *tree_id = NULL;
2703 6ced4176 2019-07-12 stsp }
2704 07ed472d 2019-07-12 stsp return err;
2705 07ed472d 2019-07-12 stsp }
2706 07ed472d 2019-07-12 stsp
2707 07ed472d 2019-07-12 stsp static const struct got_error *
2708 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2709 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2710 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2711 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2712 07ed472d 2019-07-12 stsp {
2713 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2714 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2715 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2716 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2717 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2718 07ed472d 2019-07-12 stsp
2719 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2720 7f47418f 2019-12-20 stsp if (err) {
2721 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2722 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2723 7f47418f 2019-12-20 stsp goto done;
2724 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2725 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2726 7f47418f 2019-12-20 stsp if (err)
2727 7f47418f 2019-12-20 stsp return err;
2728 7f47418f 2019-12-20 stsp }
2729 07ed472d 2019-07-12 stsp
2730 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2731 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2732 07ed472d 2019-07-12 stsp if (err)
2733 07ed472d 2019-07-12 stsp goto done;
2734 07ed472d 2019-07-12 stsp
2735 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2736 07ed472d 2019-07-12 stsp if (err)
2737 07ed472d 2019-07-12 stsp goto done;
2738 07ed472d 2019-07-12 stsp
2739 07ed472d 2019-07-12 stsp if (entry_name &&
2740 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2741 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2742 07ed472d 2019-07-12 stsp goto done;
2743 07ed472d 2019-07-12 stsp }
2744 07ed472d 2019-07-12 stsp
2745 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2746 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2747 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2748 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2749 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2750 07ed472d 2019-07-12 stsp arg.repo = repo;
2751 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2752 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2753 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2754 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2755 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2756 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2757 07ed472d 2019-07-12 stsp done:
2758 07ed472d 2019-07-12 stsp if (tree)
2759 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2760 07ed472d 2019-07-12 stsp if (commit)
2761 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2762 6ced4176 2019-07-12 stsp return err;
2763 6ced4176 2019-07-12 stsp }
2764 6ced4176 2019-07-12 stsp
2765 9d31a1d8 2018-03-11 stsp const struct got_error *
2766 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2767 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2768 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2769 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2770 9d31a1d8 2018-03-11 stsp {
2771 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2772 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2773 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2774 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2775 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2776 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2777 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2778 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2779 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2780 f2ea84fa 2019-07-27 stsp int entry_type;
2781 f2ea84fa 2019-07-27 stsp char *relpath;
2782 f2ea84fa 2019-07-27 stsp char *entry_name;
2783 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2784 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2785 9d31a1d8 2018-03-11 stsp
2786 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2787 f2ea84fa 2019-07-27 stsp
2788 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2789 9d31a1d8 2018-03-11 stsp if (err)
2790 9d31a1d8 2018-03-11 stsp return err;
2791 945f9229 2022-04-16 thomas
2792 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2793 945f9229 2022-04-16 thomas worktree->base_commit_id);
2794 945f9229 2022-04-16 thomas if (err)
2795 945f9229 2022-04-16 thomas goto done;
2796 93a30277 2018-12-24 stsp
2797 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2798 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2799 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2800 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2801 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2802 f2ea84fa 2019-07-27 stsp goto done;
2803 f2ea84fa 2019-07-27 stsp }
2804 6ced4176 2019-07-12 stsp
2805 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2806 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2807 945f9229 2022-04-16 thomas worktree, repo);
2808 f2ea84fa 2019-07-27 stsp if (err) {
2809 f2ea84fa 2019-07-27 stsp free(tpd);
2810 6ced4176 2019-07-12 stsp goto done;
2811 6ced4176 2019-07-12 stsp }
2812 f2ea84fa 2019-07-27 stsp
2813 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2814 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2815 f2ea84fa 2019-07-27 stsp if (err) {
2816 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2817 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2818 f2ea84fa 2019-07-27 stsp free(tpd);
2819 f2ea84fa 2019-07-27 stsp goto done;
2820 f2ea84fa 2019-07-27 stsp }
2821 f2ea84fa 2019-07-27 stsp } else
2822 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2823 f2ea84fa 2019-07-27 stsp
2824 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2825 6ced4176 2019-07-12 stsp }
2826 6ced4176 2019-07-12 stsp
2827 51514078 2018-12-25 stsp /*
2828 51514078 2018-12-25 stsp * Read the file index.
2829 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2830 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2831 51514078 2018-12-25 stsp */
2832 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2833 8da9e5f4 2019-01-12 stsp if (err)
2834 c4cdcb68 2019-04-03 stsp goto done;
2835 c4cdcb68 2019-04-03 stsp
2836 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2837 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2838 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2839 f2ea84fa 2019-07-27 stsp
2840 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2841 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2842 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2843 f2ea84fa 2019-07-27 stsp if (err)
2844 f2ea84fa 2019-07-27 stsp break;
2845 f2ea84fa 2019-07-27 stsp
2846 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2847 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2848 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2849 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2850 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2851 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2852 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2853 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2854 f2ea84fa 2019-07-27 stsp if (err)
2855 f2ea84fa 2019-07-27 stsp break;
2856 f2ea84fa 2019-07-27 stsp
2857 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2858 711d9cd9 2019-07-12 stsp }
2859 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2860 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2861 af12c6b9 2019-06-04 stsp err = sync_err;
2862 9d31a1d8 2018-03-11 stsp done:
2863 9c6338c4 2019-06-02 stsp free(fileindex_path);
2864 edfa7d7f 2018-09-11 stsp if (tree)
2865 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2866 9d31a1d8 2018-03-11 stsp if (commit)
2867 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2868 5ade8233 2019-07-12 stsp if (fileindex)
2869 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2870 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2871 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2872 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2873 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2874 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2875 f2ea84fa 2019-07-27 stsp free(tpd);
2876 f2ea84fa 2019-07-27 stsp }
2877 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2878 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2879 9d31a1d8 2018-03-11 stsp err = unlockerr;
2880 f8d1f275 2019-02-04 stsp return err;
2881 f8d1f275 2019-02-04 stsp }
2882 f8d1f275 2019-02-04 stsp
2883 0529f8df 2023-03-10 thomas static const struct got_error *
2884 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2885 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2886 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2887 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2888 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2889 0529f8df 2023-03-10 thomas struct got_repository *repo,
2890 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2891 0529f8df 2023-03-10 thomas {
2892 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2893 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2894 0529f8df 2023-03-10 thomas
2895 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2896 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2897 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2898 0529f8df 2023-03-10 thomas restoring_missing_file,
2899 0529f8df 2023-03-10 thomas reverting_versioned_file,
2900 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2901 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2902 0529f8df 2023-03-10 thomas } else {
2903 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2904 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2905 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2906 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2907 0529f8df 2023-03-10 thomas }
2908 0529f8df 2023-03-10 thomas if (err)
2909 0529f8df 2023-03-10 thomas return err;
2910 0529f8df 2023-03-10 thomas if (ie == NULL) {
2911 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2912 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2913 0529f8df 2023-03-10 thomas if (err)
2914 0529f8df 2023-03-10 thomas return err;
2915 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2916 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2917 0529f8df 2023-03-10 thomas if (err) {
2918 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2919 0529f8df 2023-03-10 thomas return err;
2920 0529f8df 2023-03-10 thomas }
2921 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2922 0529f8df 2023-03-10 thomas if (err) {
2923 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2924 0529f8df 2023-03-10 thomas return err;
2925 0529f8df 2023-03-10 thomas }
2926 0529f8df 2023-03-10 thomas } else {
2927 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2928 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2929 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2930 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2931 0529f8df 2023-03-10 thomas if (err)
2932 0529f8df 2023-03-10 thomas return err;
2933 0529f8df 2023-03-10 thomas }
2934 0529f8df 2023-03-10 thomas
2935 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2936 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2937 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2938 0529f8df 2023-03-10 thomas }
2939 0529f8df 2023-03-10 thomas
2940 0529f8df 2023-03-10 thomas return NULL;
2941 0529f8df 2023-03-10 thomas }
2942 0529f8df 2023-03-10 thomas
2943 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2944 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2945 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2946 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2947 234035bc 2019-06-01 stsp void *progress_arg;
2948 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2949 234035bc 2019-06-01 stsp void *cancel_arg;
2950 f69721c3 2019-10-21 stsp const char *label_orig;
2951 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2952 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2953 234035bc 2019-06-01 stsp };
2954 234035bc 2019-06-01 stsp
2955 234035bc 2019-06-01 stsp static const struct got_error *
2956 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2957 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2958 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2959 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2960 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2961 234035bc 2019-06-01 stsp {
2962 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2963 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2964 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2965 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2966 234035bc 2019-06-01 stsp struct stat sb;
2967 234035bc 2019-06-01 stsp unsigned char status;
2968 234035bc 2019-06-01 stsp int local_changes_subsumed;
2969 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2970 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2971 234035bc 2019-06-01 stsp
2972 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2973 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2974 d6c87207 2019-08-02 stsp strlen(path2));
2975 1ee397ad 2019-07-12 stsp if (ie == NULL)
2976 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2977 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2978 234035bc 2019-06-01 stsp
2979 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2980 234035bc 2019-06-01 stsp path2) == -1)
2981 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2982 234035bc 2019-06-01 stsp
2983 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2984 7f91a133 2019-12-13 stsp repo);
2985 234035bc 2019-06-01 stsp if (err)
2986 234035bc 2019-06-01 stsp goto done;
2987 234035bc 2019-06-01 stsp
2988 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2989 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2990 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2991 234035bc 2019-06-01 stsp goto done;
2992 234035bc 2019-06-01 stsp }
2993 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2994 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2995 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2996 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2997 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2998 234035bc 2019-06-01 stsp goto done;
2999 234035bc 2019-06-01 stsp }
3000 234035bc 2019-06-01 stsp
3001 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3002 36bf999c 2020-07-23 stsp char *link_target2;
3003 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3004 36bf999c 2020-07-23 stsp if (err)
3005 36bf999c 2020-07-23 stsp goto done;
3006 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3007 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3008 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3009 36bf999c 2020-07-23 stsp free(link_target2);
3010 af57b12a 2020-07-23 stsp } else {
3011 1af628f4 2021-06-03 stsp int fd;
3012 1af628f4 2021-06-03 stsp
3013 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3014 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3015 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3016 1af628f4 2021-06-03 stsp goto done;
3017 1af628f4 2021-06-03 stsp }
3018 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3019 1af628f4 2021-06-03 stsp f_orig, blob1);
3020 1af628f4 2021-06-03 stsp if (err)
3021 1af628f4 2021-06-03 stsp goto done;
3022 1af628f4 2021-06-03 stsp
3023 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3024 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3025 1af628f4 2021-06-03 stsp goto done;
3026 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3027 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3028 1af628f4 2021-06-03 stsp if (err)
3029 1af628f4 2021-06-03 stsp goto done;
3030 1af628f4 2021-06-03 stsp
3031 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3032 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3033 1af628f4 2021-06-03 stsp if (fd == -1) {
3034 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3035 1af628f4 2021-06-03 stsp ondisk_path);
3036 1af628f4 2021-06-03 stsp goto done;
3037 1af628f4 2021-06-03 stsp }
3038 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3039 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3040 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3041 1af628f4 2021-06-03 stsp ondisk_path);
3042 1af628f4 2021-06-03 stsp close(fd);
3043 1af628f4 2021-06-03 stsp goto done;
3044 1af628f4 2021-06-03 stsp }
3045 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3046 1af628f4 2021-06-03 stsp if (err)
3047 1af628f4 2021-06-03 stsp goto done;
3048 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3049 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3050 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3051 1af628f4 2021-06-03 stsp goto done;
3052 1af628f4 2021-06-03 stsp }
3053 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3054 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3055 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3056 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3057 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3058 af57b12a 2020-07-23 stsp }
3059 234035bc 2019-06-01 stsp } else if (blob1) {
3060 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3061 d6c87207 2019-08-02 stsp strlen(path1));
3062 1ee397ad 2019-07-12 stsp if (ie == NULL)
3063 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3064 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3065 2b92fad7 2019-06-02 stsp
3066 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3067 2b92fad7 2019-06-02 stsp path1) == -1)
3068 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3069 2b92fad7 2019-06-02 stsp
3070 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3071 7f91a133 2019-12-13 stsp repo);
3072 2b92fad7 2019-06-02 stsp if (err)
3073 2b92fad7 2019-06-02 stsp goto done;
3074 2b92fad7 2019-06-02 stsp
3075 2b92fad7 2019-06-02 stsp switch (status) {
3076 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3077 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3078 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3079 1ee397ad 2019-07-12 stsp if (err)
3080 1ee397ad 2019-07-12 stsp goto done;
3081 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3082 2b92fad7 2019-06-02 stsp if (err)
3083 2b92fad7 2019-06-02 stsp goto done;
3084 2b92fad7 2019-06-02 stsp if (ie)
3085 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3086 2b92fad7 2019-06-02 stsp break;
3087 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3088 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3089 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3090 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3091 1ee397ad 2019-07-12 stsp if (err)
3092 1ee397ad 2019-07-12 stsp goto done;
3093 2b92fad7 2019-06-02 stsp if (ie)
3094 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3095 2b92fad7 2019-06-02 stsp break;
3096 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3097 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3098 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3099 e8f02263 2022-01-23 thomas off_t blob1_size;
3100 0a22ca1a 2020-09-23 stsp /*
3101 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3102 0a22ca1a 2020-09-23 stsp * exists in the repository.
3103 0a22ca1a 2020-09-23 stsp */
3104 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3105 e8f02263 2022-01-23 thomas &blob1_size, path1);
3106 0a22ca1a 2020-09-23 stsp if (err)
3107 0a22ca1a 2020-09-23 stsp goto done;
3108 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3109 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3110 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3111 0a22ca1a 2020-09-23 stsp if (err)
3112 0a22ca1a 2020-09-23 stsp goto done;
3113 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3114 0a22ca1a 2020-09-23 stsp path1);
3115 0a22ca1a 2020-09-23 stsp if (err)
3116 0a22ca1a 2020-09-23 stsp goto done;
3117 0a22ca1a 2020-09-23 stsp if (ie)
3118 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3119 0a22ca1a 2020-09-23 stsp ie);
3120 0a22ca1a 2020-09-23 stsp } else {
3121 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3122 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3123 0a22ca1a 2020-09-23 stsp }
3124 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3125 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3126 0a22ca1a 2020-09-23 stsp free(id);
3127 0a22ca1a 2020-09-23 stsp if (err)
3128 0a22ca1a 2020-09-23 stsp goto done;
3129 0a22ca1a 2020-09-23 stsp break;
3130 0a22ca1a 2020-09-23 stsp }
3131 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3132 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3133 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3134 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3135 1ee397ad 2019-07-12 stsp if (err)
3136 1ee397ad 2019-07-12 stsp goto done;
3137 2b92fad7 2019-06-02 stsp break;
3138 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3139 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3140 1ee397ad 2019-07-12 stsp if (err)
3141 1ee397ad 2019-07-12 stsp goto done;
3142 2b92fad7 2019-06-02 stsp break;
3143 2b92fad7 2019-06-02 stsp default:
3144 2b92fad7 2019-06-02 stsp break;
3145 2b92fad7 2019-06-02 stsp }
3146 234035bc 2019-06-01 stsp } else if (blob2) {
3147 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3148 234035bc 2019-06-01 stsp path2) == -1)
3149 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3150 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3151 d6c87207 2019-08-02 stsp strlen(path2));
3152 234035bc 2019-06-01 stsp if (ie) {
3153 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3154 7f91a133 2019-12-13 stsp -1, NULL, repo);
3155 234035bc 2019-06-01 stsp if (err)
3156 234035bc 2019-06-01 stsp goto done;
3157 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3158 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3159 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3160 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3161 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3162 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3163 1ee397ad 2019-07-12 stsp status, path2);
3164 234035bc 2019-06-01 stsp goto done;
3165 234035bc 2019-06-01 stsp }
3166 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3167 36bf999c 2020-07-23 stsp char *link_target2;
3168 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3169 36bf999c 2020-07-23 stsp blob2);
3170 36bf999c 2020-07-23 stsp if (err)
3171 36bf999c 2020-07-23 stsp goto done;
3172 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3173 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3174 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3175 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3176 36bf999c 2020-07-23 stsp free(link_target2);
3177 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3178 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3179 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3180 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3181 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3182 526a746f 2020-07-23 stsp a->progress_arg);
3183 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3184 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3185 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3186 526a746f 2020-07-23 stsp }
3187 dfe9fba0 2020-07-23 stsp if (err)
3188 dfe9fba0 2020-07-23 stsp goto done;
3189 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3190 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3191 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3192 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3193 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3194 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3195 234035bc 2019-06-01 stsp if (err)
3196 234035bc 2019-06-01 stsp goto done;
3197 234035bc 2019-06-01 stsp }
3198 234035bc 2019-06-01 stsp } else {
3199 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3200 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3201 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3202 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3203 234035bc 2019-06-01 stsp if (err)
3204 2b92fad7 2019-06-02 stsp goto done;
3205 234035bc 2019-06-01 stsp }
3206 234035bc 2019-06-01 stsp }
3207 234035bc 2019-06-01 stsp done:
3208 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3209 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3210 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3211 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3212 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3213 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3214 1af628f4 2021-06-03 stsp free(id_str);
3215 54d5be07 2021-06-03 stsp free(label_deriv2);
3216 234035bc 2019-06-01 stsp free(ondisk_path);
3217 234035bc 2019-06-01 stsp return err;
3218 234035bc 2019-06-01 stsp }
3219 234035bc 2019-06-01 stsp
3220 69de9dd4 2021-09-03 stsp static const struct got_error *
3221 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3222 69de9dd4 2021-09-03 stsp {
3223 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3224 69de9dd4 2021-09-03 stsp
3225 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3226 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3227 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3228 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3229 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3230 69de9dd4 2021-09-03 stsp
3231 69de9dd4 2021-09-03 stsp return NULL;
3232 69de9dd4 2021-09-03 stsp }
3233 69de9dd4 2021-09-03 stsp
3234 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3235 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3236 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3237 234035bc 2019-06-01 stsp struct got_repository *repo;
3238 234035bc 2019-06-01 stsp };
3239 234035bc 2019-06-01 stsp
3240 234035bc 2019-06-01 stsp static const struct got_error *
3241 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3242 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3243 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3244 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3245 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3246 234035bc 2019-06-01 stsp {
3247 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3248 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3249 234035bc 2019-06-01 stsp unsigned char status;
3250 234035bc 2019-06-01 stsp struct stat sb;
3251 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3252 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3253 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3254 234035bc 2019-06-01 stsp char *ondisk_path;
3255 234035bc 2019-06-01 stsp
3256 69de9dd4 2021-09-03 stsp if (id == NULL)
3257 69de9dd4 2021-09-03 stsp return NULL;
3258 234035bc 2019-06-01 stsp
3259 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3260 69de9dd4 2021-09-03 stsp if (ie == NULL)
3261 69de9dd4 2021-09-03 stsp return NULL;
3262 69de9dd4 2021-09-03 stsp
3263 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3264 234035bc 2019-06-01 stsp == -1)
3265 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3266 234035bc 2019-06-01 stsp
3267 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3268 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3269 5546d466 2021-09-02 stsp free(ondisk_path);
3270 234035bc 2019-06-01 stsp if (err)
3271 234035bc 2019-06-01 stsp return err;
3272 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3273 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3274 234035bc 2019-06-01 stsp
3275 234035bc 2019-06-01 stsp return NULL;
3276 234035bc 2019-06-01 stsp }
3277 234035bc 2019-06-01 stsp
3278 818c7501 2019-07-11 stsp static const struct got_error *
3279 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3280 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3281 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3282 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3283 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3284 234035bc 2019-06-01 stsp {
3285 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3286 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3287 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3288 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3289 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3290 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3291 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3292 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3293 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3294 234035bc 2019-06-01 stsp
3295 03415a1a 2019-06-02 stsp if (commit_id1) {
3296 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3297 945f9229 2022-04-16 thomas if (err)
3298 945f9229 2022-04-16 thomas goto done;
3299 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3300 03415a1a 2019-06-02 stsp worktree->path_prefix);
3301 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3302 03415a1a 2019-06-02 stsp goto done;
3303 69d57f3d 2020-07-31 stsp }
3304 69d57f3d 2020-07-31 stsp if (tree_id1) {
3305 69d57f3d 2020-07-31 stsp char *id_str;
3306 03415a1a 2019-06-02 stsp
3307 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3308 03415a1a 2019-06-02 stsp if (err)
3309 03415a1a 2019-06-02 stsp goto done;
3310 f69721c3 2019-10-21 stsp
3311 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3312 f69721c3 2019-10-21 stsp if (err)
3313 f69721c3 2019-10-21 stsp goto done;
3314 f69721c3 2019-10-21 stsp
3315 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3316 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3317 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3318 f69721c3 2019-10-21 stsp free(id_str);
3319 f69721c3 2019-10-21 stsp goto done;
3320 f69721c3 2019-10-21 stsp }
3321 f69721c3 2019-10-21 stsp free(id_str);
3322 a0f32f33 2022-06-13 thomas
3323 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3324 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3325 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3326 a0f32f33 2022-06-13 thomas goto done;
3327 a0f32f33 2022-06-13 thomas }
3328 03415a1a 2019-06-02 stsp }
3329 234035bc 2019-06-01 stsp
3330 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3331 945f9229 2022-04-16 thomas if (err)
3332 945f9229 2022-04-16 thomas goto done;
3333 945f9229 2022-04-16 thomas
3334 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3335 234035bc 2019-06-01 stsp worktree->path_prefix);
3336 234035bc 2019-06-01 stsp if (err)
3337 234035bc 2019-06-01 stsp goto done;
3338 234035bc 2019-06-01 stsp
3339 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3340 234035bc 2019-06-01 stsp if (err)
3341 234035bc 2019-06-01 stsp goto done;
3342 234035bc 2019-06-01 stsp
3343 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3344 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3345 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3346 19a6a6b5 2022-07-01 thomas goto done;
3347 19a6a6b5 2022-07-01 thomas }
3348 19a6a6b5 2022-07-01 thomas
3349 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3350 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3351 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3352 a0f32f33 2022-06-13 thomas goto done;
3353 a0f32f33 2022-06-13 thomas }
3354 a0f32f33 2022-06-13 thomas
3355 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3356 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3357 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3358 19a6a6b5 2022-07-01 thomas goto done;
3359 19a6a6b5 2022-07-01 thomas }
3360 19a6a6b5 2022-07-01 thomas
3361 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3362 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3363 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3364 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3365 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3366 69de9dd4 2021-09-03 stsp if (err)
3367 69de9dd4 2021-09-03 stsp goto done;
3368 69de9dd4 2021-09-03 stsp
3369 234035bc 2019-06-01 stsp arg.worktree = worktree;
3370 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3371 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3372 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3373 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3374 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3375 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3376 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3377 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3378 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3379 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3380 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3381 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3382 af12c6b9 2019-06-04 stsp err = sync_err;
3383 234035bc 2019-06-01 stsp done:
3384 945f9229 2022-04-16 thomas if (commit1)
3385 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3386 945f9229 2022-04-16 thomas if (commit2)
3387 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3388 234035bc 2019-06-01 stsp if (tree1)
3389 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3390 234035bc 2019-06-01 stsp if (tree2)
3391 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3392 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3393 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3394 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3395 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3396 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3397 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3398 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3399 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3400 f69721c3 2019-10-21 stsp free(label_orig);
3401 818c7501 2019-07-11 stsp return err;
3402 818c7501 2019-07-11 stsp }
3403 234035bc 2019-06-01 stsp
3404 818c7501 2019-07-11 stsp const struct got_error *
3405 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3406 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3407 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3408 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3409 818c7501 2019-07-11 stsp {
3410 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3411 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3412 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3413 818c7501 2019-07-11 stsp
3414 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3415 818c7501 2019-07-11 stsp if (err)
3416 818c7501 2019-07-11 stsp return err;
3417 818c7501 2019-07-11 stsp
3418 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3419 818c7501 2019-07-11 stsp if (err)
3420 818c7501 2019-07-11 stsp goto done;
3421 818c7501 2019-07-11 stsp
3422 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3423 69de9dd4 2021-09-03 stsp worktree);
3424 818c7501 2019-07-11 stsp if (err)
3425 818c7501 2019-07-11 stsp goto done;
3426 818c7501 2019-07-11 stsp
3427 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3428 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3429 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3430 818c7501 2019-07-11 stsp done:
3431 818c7501 2019-07-11 stsp if (fileindex)
3432 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3433 818c7501 2019-07-11 stsp free(fileindex_path);
3434 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3435 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3436 234035bc 2019-06-01 stsp err = unlockerr;
3437 234035bc 2019-06-01 stsp return err;
3438 234035bc 2019-06-01 stsp }
3439 234035bc 2019-06-01 stsp
3440 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3441 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3442 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3443 927df6b7 2019-02-10 stsp const char *status_path;
3444 927df6b7 2019-02-10 stsp size_t status_path_len;
3445 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3446 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3447 f8d1f275 2019-02-04 stsp void *status_arg;
3448 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3449 f8d1f275 2019-02-04 stsp void *cancel_arg;
3450 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3451 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3452 f2a9dc41 2019-12-13 tracey int report_unchanged;
3453 3143d852 2020-06-25 stsp int no_ignores;
3454 f8d1f275 2019-02-04 stsp };
3455 88d0e355 2019-08-03 stsp
3456 f8d1f275 2019-02-04 stsp static const struct got_error *
3457 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3458 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3459 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3460 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3461 927df6b7 2019-02-10 stsp {
3462 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3463 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3464 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3465 927df6b7 2019-02-10 stsp struct stat sb;
3466 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3467 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3468 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3469 927df6b7 2019-02-10 stsp
3470 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3471 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3472 98eaaa12 2019-08-03 stsp if (err)
3473 98eaaa12 2019-08-03 stsp return err;
3474 98eaaa12 2019-08-03 stsp
3475 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3476 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3477 98eaaa12 2019-08-03 stsp return NULL;
3478 98eaaa12 2019-08-03 stsp
3479 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3480 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3481 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3482 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3483 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3484 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3485 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3486 43010591 2023-02-17 thomas &staged_blob_id, ie);
3487 98eaaa12 2019-08-03 stsp }
3488 98eaaa12 2019-08-03 stsp
3489 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3490 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3491 927df6b7 2019-02-10 stsp }
3492 927df6b7 2019-02-10 stsp
3493 927df6b7 2019-02-10 stsp static const struct got_error *
3494 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3495 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3496 f8d1f275 2019-02-04 stsp {
3497 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3498 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3499 f8d1f275 2019-02-04 stsp char *abspath;
3500 f8d1f275 2019-02-04 stsp
3501 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3502 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3503 0584f854 2019-04-06 stsp
3504 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3505 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3506 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3507 927df6b7 2019-02-10 stsp return NULL;
3508 927df6b7 2019-02-10 stsp
3509 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3510 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3511 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3512 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3513 f8d1f275 2019-02-04 stsp } else {
3514 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3515 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3517 f8d1f275 2019-02-04 stsp }
3518 f8d1f275 2019-02-04 stsp
3519 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3520 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3521 f8d1f275 2019-02-04 stsp free(abspath);
3522 9d31a1d8 2018-03-11 stsp return err;
3523 9d31a1d8 2018-03-11 stsp }
3524 f8d1f275 2019-02-04 stsp
3525 f8d1f275 2019-02-04 stsp static const struct got_error *
3526 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3527 f8d1f275 2019-02-04 stsp {
3528 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3529 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3530 2ec1f75b 2019-03-26 stsp unsigned char status;
3531 927df6b7 2019-02-10 stsp
3532 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3533 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3534 0584f854 2019-04-06 stsp
3535 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3536 927df6b7 2019-02-10 stsp return NULL;
3537 927df6b7 2019-02-10 stsp
3538 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3539 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3540 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3541 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3542 2ec1f75b 2019-03-26 stsp else
3543 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3544 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3545 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3546 6841da00 2019-08-08 stsp }
3547 6841da00 2019-08-08 stsp
3548 ef20f542 2022-06-26 thomas static void
3549 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3550 6841da00 2019-08-08 stsp {
3551 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3552 6841da00 2019-08-08 stsp
3553 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3554 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3555 21c2d8be 2023-01-10 thomas
3556 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3557 6841da00 2019-08-08 stsp }
3558 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3559 6841da00 2019-08-08 stsp }
3560 6841da00 2019-08-08 stsp
3561 6841da00 2019-08-08 stsp static const struct got_error *
3562 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3563 6841da00 2019-08-08 stsp {
3564 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3565 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3566 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3567 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3568 6841da00 2019-08-08 stsp size_t linesize = 0;
3569 6841da00 2019-08-08 stsp ssize_t linelen;
3570 6841da00 2019-08-08 stsp
3571 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3572 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3573 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3574 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3575 6841da00 2019-08-08 stsp
3576 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3577 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3578 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3579 bd8de430 2019-10-04 stsp
3580 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3581 bd8de430 2019-10-04 stsp if (line[0] == '#')
3582 bd8de430 2019-10-04 stsp continue;
3583 bd8de430 2019-10-04 stsp
3584 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3585 bd8de430 2019-10-04 stsp if (line[0] == '!')
3586 bd8de430 2019-10-04 stsp continue;
3587 bd8de430 2019-10-04 stsp
3588 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3589 6841da00 2019-08-08 stsp line) == -1) {
3590 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3591 6841da00 2019-08-08 stsp goto done;
3592 6841da00 2019-08-08 stsp }
3593 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3594 6841da00 2019-08-08 stsp if (err)
3595 6841da00 2019-08-08 stsp goto done;
3596 6841da00 2019-08-08 stsp }
3597 6841da00 2019-08-08 stsp if (ferror(f)) {
3598 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3599 6841da00 2019-08-08 stsp goto done;
3600 6841da00 2019-08-08 stsp }
3601 6841da00 2019-08-08 stsp
3602 6841da00 2019-08-08 stsp dirpath = strdup(path);
3603 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3604 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3605 6841da00 2019-08-08 stsp goto done;
3606 6841da00 2019-08-08 stsp }
3607 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3608 6841da00 2019-08-08 stsp done:
3609 6841da00 2019-08-08 stsp free(line);
3610 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3611 6841da00 2019-08-08 stsp free(dirpath);
3612 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3613 6841da00 2019-08-08 stsp }
3614 6841da00 2019-08-08 stsp return err;
3615 1b5d300f 2023-02-20 thomas }
3616 1b5d300f 2023-02-20 thomas
3617 1b5d300f 2023-02-20 thomas static int
3618 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3619 1b5d300f 2023-02-20 thomas int flags)
3620 1b5d300f 2023-02-20 thomas {
3621 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3622 1b5d300f 2023-02-20 thomas
3623 1b5d300f 2023-02-20 thomas /*
3624 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3625 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3626 1b5d300f 2023-02-20 thomas */
3627 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3628 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3629 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3630 1b5d300f 2023-02-20 thomas
3631 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3632 1b5d300f 2023-02-20 thomas }
3633 1b5d300f 2023-02-20 thomas
3634 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3635 6841da00 2019-08-08 stsp }
3636 6841da00 2019-08-08 stsp
3637 ef20f542 2022-06-26 thomas static int
3638 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3639 6841da00 2019-08-08 stsp {
3640 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3641 bd8de430 2019-10-04 stsp
3642 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3643 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3644 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3645 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3646 bd8de430 2019-10-04 stsp
3647 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3648 1b5d300f 2023-02-20 thomas const char *p;
3649 6841da00 2019-08-08 stsp
3650 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3651 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3652 bd8de430 2019-10-04 stsp continue;
3653 bd8de430 2019-10-04 stsp p = path;
3654 bd8de430 2019-10-04 stsp while (*p) {
3655 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3656 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3657 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3658 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3659 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3660 bd8de430 2019-10-04 stsp p++;
3661 bd8de430 2019-10-04 stsp while (*p == '/')
3662 bd8de430 2019-10-04 stsp p++;
3663 bd8de430 2019-10-04 stsp continue;
3664 bd8de430 2019-10-04 stsp }
3665 bd8de430 2019-10-04 stsp return 1;
3666 bd8de430 2019-10-04 stsp }
3667 bd8de430 2019-10-04 stsp }
3668 bd8de430 2019-10-04 stsp }
3669 bd8de430 2019-10-04 stsp
3670 6841da00 2019-08-08 stsp /*
3671 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3672 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3673 6841da00 2019-08-08 stsp * ignores backwards.
3674 6841da00 2019-08-08 stsp */
3675 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3676 6841da00 2019-08-08 stsp while (pe) {
3677 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3678 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3679 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3680 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3681 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3682 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3683 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3684 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3685 1b5d300f 2023-02-20 thomas path, flags))
3686 6841da00 2019-08-08 stsp continue;
3687 6841da00 2019-08-08 stsp return 1;
3688 6841da00 2019-08-08 stsp }
3689 6841da00 2019-08-08 stsp }
3690 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3691 6841da00 2019-08-08 stsp }
3692 6841da00 2019-08-08 stsp
3693 6841da00 2019-08-08 stsp return 0;
3694 6841da00 2019-08-08 stsp }
3695 6841da00 2019-08-08 stsp
3696 6841da00 2019-08-08 stsp static const struct got_error *
3697 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3698 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3699 6841da00 2019-08-08 stsp {
3700 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3701 6841da00 2019-08-08 stsp char *ignorespath;
3702 886cec17 2019-12-15 stsp int fd = -1;
3703 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3704 6841da00 2019-08-08 stsp
3705 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3706 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3707 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3708 6841da00 2019-08-08 stsp
3709 886cec17 2019-12-15 stsp if (dirfd != -1) {
3710 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3711 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3712 886cec17 2019-12-15 stsp if (fd == -1) {
3713 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3714 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3715 886cec17 2019-12-15 stsp ignorespath);
3716 886cec17 2019-12-15 stsp } else {
3717 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3718 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3719 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3720 886cec17 2019-12-15 stsp ignorespath);
3721 886cec17 2019-12-15 stsp else {
3722 886cec17 2019-12-15 stsp fd = -1;
3723 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3724 886cec17 2019-12-15 stsp }
3725 886cec17 2019-12-15 stsp }
3726 886cec17 2019-12-15 stsp } else {
3727 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3728 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3729 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3730 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3731 886cec17 2019-12-15 stsp ignorespath);
3732 886cec17 2019-12-15 stsp } else
3733 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3734 886cec17 2019-12-15 stsp }
3735 6841da00 2019-08-08 stsp
3736 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3737 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3738 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3739 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3740 6841da00 2019-08-08 stsp free(ignorespath);
3741 6841da00 2019-08-08 stsp return err;
3742 f8d1f275 2019-02-04 stsp }
3743 f8d1f275 2019-02-04 stsp
3744 f8d1f275 2019-02-04 stsp static const struct got_error *
3745 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3746 6092c299 2021-10-04 thomas int dirfd)
3747 f8d1f275 2019-02-04 stsp {
3748 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3749 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3750 f8d1f275 2019-02-04 stsp char *path = NULL;
3751 6092c299 2021-10-04 thomas
3752 6092c299 2021-10-04 thomas if (ignore != NULL)
3753 6092c299 2021-10-04 thomas *ignore = 0;
3754 f8d1f275 2019-02-04 stsp
3755 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3756 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3757 0584f854 2019-04-06 stsp
3758 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3759 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3760 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3761 f8d1f275 2019-02-04 stsp } else {
3762 f8d1f275 2019-02-04 stsp path = de->d_name;
3763 f8d1f275 2019-02-04 stsp }
3764 f8d1f275 2019-02-04 stsp
3765 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3766 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3767 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3768 6092c299 2021-10-04 thomas *ignore = 1;
3769 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3770 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3771 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3772 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3773 f8d1f275 2019-02-04 stsp if (parent_path[0])
3774 f8d1f275 2019-02-04 stsp free(path);
3775 b72f483a 2019-02-05 stsp return err;
3776 f8d1f275 2019-02-04 stsp }
3777 f8d1f275 2019-02-04 stsp
3778 347d1d3e 2019-07-12 stsp static const struct got_error *
3779 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3780 3143d852 2020-06-25 stsp {
3781 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3782 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3783 3143d852 2020-06-25 stsp
3784 3143d852 2020-06-25 stsp if (a->no_ignores)
3785 3143d852 2020-06-25 stsp return NULL;
3786 3143d852 2020-06-25 stsp
3787 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3788 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3789 3143d852 2020-06-25 stsp if (err)
3790 3143d852 2020-06-25 stsp return err;
3791 3143d852 2020-06-25 stsp
3792 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3793 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3794 3143d852 2020-06-25 stsp
3795 3143d852 2020-06-25 stsp return err;
3796 3143d852 2020-06-25 stsp }
3797 3143d852 2020-06-25 stsp
3798 3143d852 2020-06-25 stsp static const struct got_error *
3799 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3800 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3801 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3802 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3803 abb4604f 2019-07-27 stsp {
3804 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3805 abb4604f 2019-07-27 stsp struct stat sb;
3806 ff56836b 2021-07-08 stsp
3807 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3808 abb4604f 2019-07-27 stsp if (ie)
3809 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3810 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3811 abb4604f 2019-07-27 stsp
3812 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3813 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3814 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3815 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3816 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3817 abb4604f 2019-07-27 stsp }
3818 abb4604f 2019-07-27 stsp
3819 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3820 fe1d8685 2022-02-12 thomas return NULL;
3821 fe1d8685 2022-02-12 thomas
3822 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3823 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3824 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3825 abb4604f 2019-07-27 stsp
3826 abb4604f 2019-07-27 stsp return NULL;
3827 3143d852 2020-06-25 stsp }
3828 3143d852 2020-06-25 stsp
3829 3143d852 2020-06-25 stsp static const struct got_error *
3830 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3831 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3832 3143d852 2020-06-25 stsp {
3833 3143d852 2020-06-25 stsp const struct got_error *err;
3834 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3835 3143d852 2020-06-25 stsp
3836 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3837 3143d852 2020-06-25 stsp ".cvsignore");
3838 3143d852 2020-06-25 stsp if (err)
3839 3143d852 2020-06-25 stsp return err;
3840 3143d852 2020-06-25 stsp
3841 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3842 3143d852 2020-06-25 stsp ".gitignore");
3843 3143d852 2020-06-25 stsp if (err)
3844 3143d852 2020-06-25 stsp return err;
3845 3143d852 2020-06-25 stsp
3846 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3847 3143d852 2020-06-25 stsp if (err) {
3848 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3849 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3850 3143d852 2020-06-25 stsp return err;
3851 3143d852 2020-06-25 stsp }
3852 3143d852 2020-06-25 stsp for (;;) {
3853 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3854 3143d852 2020-06-25 stsp ".cvsignore");
3855 3143d852 2020-06-25 stsp if (err)
3856 3143d852 2020-06-25 stsp break;
3857 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3858 3143d852 2020-06-25 stsp ".gitignore");
3859 3143d852 2020-06-25 stsp if (err)
3860 3143d852 2020-06-25 stsp break;
3861 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3862 3143d852 2020-06-25 stsp if (err) {
3863 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3864 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3865 3143d852 2020-06-25 stsp break;
3866 3143d852 2020-06-25 stsp }
3867 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3868 ff56836b 2021-07-08 stsp break;
3869 b737c85a 2020-06-26 stsp free(parent_path);
3870 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3871 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3872 3143d852 2020-06-25 stsp }
3873 3143d852 2020-06-25 stsp
3874 b737c85a 2020-06-26 stsp free(parent_path);
3875 b737c85a 2020-06-26 stsp free(next_parent_path);
3876 3143d852 2020-06-25 stsp return err;
3877 abb4604f 2019-07-27 stsp }
3878 abb4604f 2019-07-27 stsp
3879 abb4604f 2019-07-27 stsp static const struct got_error *
3880 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3881 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3882 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3883 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3884 f2a9dc41 2019-12-13 tracey int report_unchanged)
3885 f8d1f275 2019-02-04 stsp {
3886 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3887 6fc93f37 2019-12-13 stsp int fd = -1;
3888 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3889 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3890 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3891 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3892 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3893 3143d852 2020-06-25 stsp
3894 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3895 f8d1f275 2019-02-04 stsp
3896 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3897 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3898 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3899 8dc303cc 2019-07-27 stsp
3900 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3901 a78810f8 2022-03-13 thomas if (ie) {
3902 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3903 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3904 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3905 a78810f8 2022-03-13 thomas goto done;
3906 a78810f8 2022-03-13 thomas }
3907 a78810f8 2022-03-13 thomas
3908 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3909 6fc93f37 2019-12-13 stsp if (fd == -1) {
3910 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3911 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3912 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3913 ff56836b 2021-07-08 stsp else {
3914 ff56836b 2021-07-08 stsp if (!no_ignores) {
3915 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3916 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3917 ff56836b 2021-07-08 stsp if (err)
3918 ff56836b 2021-07-08 stsp goto done;
3919 ff56836b 2021-07-08 stsp }
3920 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3921 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3922 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3923 ff56836b 2021-07-08 stsp }
3924 abb4604f 2019-07-27 stsp } else {
3925 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3926 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3927 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3928 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3929 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3930 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3931 abb4604f 2019-07-27 stsp arg.status_path = path;
3932 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3933 abb4604f 2019-07-27 stsp arg.repo = repo;
3934 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3935 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3936 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3937 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3938 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3939 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3940 022fae89 2019-12-06 tracey if (!no_ignores) {
3941 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3942 3143d852 2020-06-25 stsp worktree->root_path, path);
3943 3143d852 2020-06-25 stsp if (err)
3944 3143d852 2020-06-25 stsp goto done;
3945 022fae89 2019-12-06 tracey }
3946 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3947 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3948 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3949 927df6b7 2019-02-10 stsp }
3950 3143d852 2020-06-25 stsp done:
3951 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3952 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3953 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3954 927df6b7 2019-02-10 stsp free(ondisk_path);
3955 347d1d3e 2019-07-12 stsp return err;
3956 347d1d3e 2019-07-12 stsp }
3957 347d1d3e 2019-07-12 stsp
3958 347d1d3e 2019-07-12 stsp const struct got_error *
3959 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3960 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3961 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3962 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3963 347d1d3e 2019-07-12 stsp {
3964 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3965 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3966 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3967 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3968 347d1d3e 2019-07-12 stsp
3969 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3970 347d1d3e 2019-07-12 stsp if (err)
3971 347d1d3e 2019-07-12 stsp return err;
3972 347d1d3e 2019-07-12 stsp
3973 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3974 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3975 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3976 f6343036 2021-06-22 stsp no_ignores, 0);
3977 72ea6654 2019-07-27 stsp if (err)
3978 72ea6654 2019-07-27 stsp break;
3979 72ea6654 2019-07-27 stsp }
3980 f8d1f275 2019-02-04 stsp free(fileindex_path);
3981 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3982 f8d1f275 2019-02-04 stsp return err;
3983 f8d1f275 2019-02-04 stsp }
3984 6c7ab921 2019-03-18 stsp
3985 6c7ab921 2019-03-18 stsp const struct got_error *
3986 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3987 6c7ab921 2019-03-18 stsp const char *arg)
3988 6c7ab921 2019-03-18 stsp {
3989 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3990 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3991 6c7ab921 2019-03-18 stsp size_t len;
3992 00bb5ea0 2020-07-23 stsp struct stat sb;
3993 01740607 2020-11-04 stsp char *abspath = NULL;
3994 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3995 6c7ab921 2019-03-18 stsp
3996 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3997 6c7ab921 2019-03-18 stsp
3998 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3999 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4000 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4001 00bb5ea0 2020-07-23 stsp
4002 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4003 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4004 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4005 00bb5ea0 2020-07-23 stsp goto done;
4006 00bb5ea0 2020-07-23 stsp }
4007 727173c3 2020-11-06 stsp sb.st_mode = 0;
4008 00bb5ea0 2020-07-23 stsp }
4009 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4010 00bb5ea0 2020-07-23 stsp /*
4011 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4012 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4013 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4014 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4015 00bb5ea0 2020-07-23 stsp */
4016 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4017 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4018 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4019 00bb5ea0 2020-07-23 stsp goto done;
4020 00bb5ea0 2020-07-23 stsp }
4021 00bb5ea0 2020-07-23 stsp
4022 00bb5ea0 2020-07-23 stsp }
4023 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4024 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4025 00bb5ea0 2020-07-23 stsp if (err)
4026 d0710d08 2019-07-22 stsp goto done;
4027 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4028 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4029 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4030 00bb5ea0 2020-07-23 stsp goto done;
4031 00bb5ea0 2020-07-23 stsp }
4032 00bb5ea0 2020-07-23 stsp } else {
4033 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4034 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4035 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4036 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4037 00bb5ea0 2020-07-23 stsp goto done;
4038 00bb5ea0 2020-07-23 stsp }
4039 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4040 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4041 01740607 2020-11-04 stsp goto done;
4042 01740607 2020-11-04 stsp }
4043 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4044 01740607 2020-11-04 stsp sizeof(canonpath));
4045 01740607 2020-11-04 stsp if (err)
4046 00bb5ea0 2020-07-23 stsp goto done;
4047 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4048 01740607 2020-11-04 stsp if (resolved == NULL) {
4049 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4050 01740607 2020-11-04 stsp goto done;
4051 00bb5ea0 2020-07-23 stsp }
4052 d0710d08 2019-07-22 stsp }
4053 d0710d08 2019-07-22 stsp }
4054 6c7ab921 2019-03-18 stsp
4055 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4056 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4057 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4058 6c7ab921 2019-03-18 stsp goto done;
4059 6c7ab921 2019-03-18 stsp }
4060 6c7ab921 2019-03-18 stsp
4061 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4062 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4063 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4064 f6d88e1a 2019-05-29 stsp if (err)
4065 f6d88e1a 2019-05-29 stsp goto done;
4066 f6d88e1a 2019-05-29 stsp } else {
4067 f6d88e1a 2019-05-29 stsp path = strdup("");
4068 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4069 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4070 f6d88e1a 2019-05-29 stsp goto done;
4071 f6d88e1a 2019-05-29 stsp }
4072 6c7ab921 2019-03-18 stsp }
4073 6c7ab921 2019-03-18 stsp
4074 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4075 6c7ab921 2019-03-18 stsp len = strlen(path);
4076 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4077 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4078 6c7ab921 2019-03-18 stsp len--;
4079 6c7ab921 2019-03-18 stsp }
4080 6c7ab921 2019-03-18 stsp done:
4081 01740607 2020-11-04 stsp free(abspath);
4082 6c7ab921 2019-03-18 stsp free(resolved);
4083 d0710d08 2019-07-22 stsp free(cwd);
4084 6c7ab921 2019-03-18 stsp if (err == NULL)
4085 6c7ab921 2019-03-18 stsp *wt_path = path;
4086 6c7ab921 2019-03-18 stsp else
4087 6c7ab921 2019-03-18 stsp free(path);
4088 d00136be 2019-03-26 stsp return err;
4089 d00136be 2019-03-26 stsp }
4090 d00136be 2019-03-26 stsp
4091 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4092 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4093 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4094 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4095 4e68cba3 2019-11-23 stsp void *progress_arg;
4096 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4097 4e68cba3 2019-11-23 stsp };
4098 4e68cba3 2019-11-23 stsp
4099 4e68cba3 2019-11-23 stsp static const struct got_error *
4100 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4101 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4102 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4103 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4104 07f5b47a 2019-06-02 stsp {
4105 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4106 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4107 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4108 6d022e97 2019-08-04 stsp struct stat sb;
4109 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4110 07f5b47a 2019-06-02 stsp
4111 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4112 dbb83fbd 2019-12-12 stsp relpath) == -1)
4113 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4114 dbb83fbd 2019-12-12 stsp
4115 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4116 6d022e97 2019-08-04 stsp if (ie) {
4117 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4118 12463d8b 2019-12-13 stsp de_name, a->repo);
4119 6d022e97 2019-08-04 stsp if (err)
4120 dbb83fbd 2019-12-12 stsp goto done;
4121 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4122 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4123 dbb83fbd 2019-12-12 stsp goto done;
4124 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4125 dbb83fbd 2019-12-12 stsp if (err)
4126 dbb83fbd 2019-12-12 stsp goto done;
4127 6d022e97 2019-08-04 stsp }
4128 07f5b47a 2019-06-02 stsp
4129 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4130 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4131 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4132 84bf00a6 2022-02-12 thomas else
4133 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4134 dbb83fbd 2019-12-12 stsp goto done;
4135 4e68cba3 2019-11-23 stsp }
4136 07f5b47a 2019-06-02 stsp
4137 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4138 4e68cba3 2019-11-23 stsp if (err)
4139 4e68cba3 2019-11-23 stsp goto done;
4140 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4141 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4142 3969253a 2020-03-07 stsp if (err) {
4143 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4144 3969253a 2020-03-07 stsp goto done;
4145 3969253a 2020-03-07 stsp }
4146 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4147 07f5b47a 2019-06-02 stsp if (err) {
4148 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4149 4e68cba3 2019-11-23 stsp goto done;
4150 07f5b47a 2019-06-02 stsp }
4151 4e68cba3 2019-11-23 stsp done:
4152 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4153 dbb83fbd 2019-12-12 stsp if (err)
4154 dbb83fbd 2019-12-12 stsp return err;
4155 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4156 dbb83fbd 2019-12-12 stsp return NULL;
4157 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4158 07f5b47a 2019-06-02 stsp }
4159 07f5b47a 2019-06-02 stsp
4160 d00136be 2019-03-26 stsp const struct got_error *
4161 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4162 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4163 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4164 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4165 d00136be 2019-03-26 stsp {
4166 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4167 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4168 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4169 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4170 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4171 d00136be 2019-03-26 stsp
4172 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4173 d00136be 2019-03-26 stsp if (err)
4174 d00136be 2019-03-26 stsp return err;
4175 d00136be 2019-03-26 stsp
4176 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4177 d00136be 2019-03-26 stsp if (err)
4178 d00136be 2019-03-26 stsp goto done;
4179 d00136be 2019-03-26 stsp
4180 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4181 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4182 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4183 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4184 4e68cba3 2019-11-23 stsp saa.repo = repo;
4185 4e68cba3 2019-11-23 stsp
4186 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4187 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4188 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4189 1dd54920 2019-05-11 stsp if (err)
4190 af12c6b9 2019-06-04 stsp break;
4191 1dd54920 2019-05-11 stsp }
4192 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4193 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4194 af12c6b9 2019-06-04 stsp err = sync_err;
4195 d00136be 2019-03-26 stsp done:
4196 fb399478 2019-07-12 stsp free(fileindex_path);
4197 d00136be 2019-03-26 stsp if (fileindex)
4198 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4199 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4200 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4201 d00136be 2019-03-26 stsp err = unlockerr;
4202 6c7ab921 2019-03-18 stsp return err;
4203 6c7ab921 2019-03-18 stsp }
4204 17ed4618 2019-06-02 stsp
4205 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4206 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4207 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4208 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4209 f2a9dc41 2019-12-13 tracey void *progress_arg;
4210 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4211 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4212 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4213 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4214 766841c2 2020-08-13 stsp const char *status_codes;
4215 f2a9dc41 2019-12-13 tracey };
4216 f2a9dc41 2019-12-13 tracey
4217 f2a9dc41 2019-12-13 tracey static const struct got_error *
4218 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4219 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4220 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4221 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4222 17ed4618 2019-06-02 stsp {
4223 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4224 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4225 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4226 17ed4618 2019-06-02 stsp struct stat sb;
4227 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4228 d64cc78a 2022-01-25 thomas
4229 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4230 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4231 d64cc78a 2022-01-25 thomas return NULL;
4232 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4233 d64cc78a 2022-01-25 thomas }
4234 17ed4618 2019-06-02 stsp
4235 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4236 17ed4618 2019-06-02 stsp if (ie == NULL)
4237 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4238 2ec1f75b 2019-03-26 stsp
4239 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4240 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4241 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4242 9acbc4fa 2019-08-03 stsp return NULL;
4243 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4244 9acbc4fa 2019-08-03 stsp }
4245 9acbc4fa 2019-08-03 stsp
4246 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4247 f2a9dc41 2019-12-13 tracey relpath) == -1)
4248 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4249 f2a9dc41 2019-12-13 tracey
4250 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4251 12463d8b 2019-12-13 stsp a->repo);
4252 17ed4618 2019-06-02 stsp if (err)
4253 f2a9dc41 2019-12-13 tracey goto done;
4254 17ed4618 2019-06-02 stsp
4255 766841c2 2020-08-13 stsp if (a->status_codes) {
4256 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4257 766841c2 2020-08-13 stsp int i;
4258 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4259 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4260 766841c2 2020-08-13 stsp break;
4261 766841c2 2020-08-13 stsp }
4262 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4263 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4264 766841c2 2020-08-13 stsp free(ondisk_path);
4265 766841c2 2020-08-13 stsp return NULL;
4266 766841c2 2020-08-13 stsp }
4267 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4268 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4269 766841c2 2020-08-13 stsp static char msg[64];
4270 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4271 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4272 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4273 766841c2 2020-08-13 stsp goto done;
4274 766841c2 2020-08-13 stsp }
4275 766841c2 2020-08-13 stsp }
4276 766841c2 2020-08-13 stsp
4277 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4278 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4279 f2a9dc41 2019-12-13 tracey goto done;
4280 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4281 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4282 f2a9dc41 2019-12-13 tracey goto done;
4283 f2a9dc41 2019-12-13 tracey }
4284 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4285 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4286 d64cc78a 2022-01-25 thomas goto done;
4287 d64cc78a 2022-01-25 thomas }
4288 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4289 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4290 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4291 f2a9dc41 2019-12-13 tracey goto done;
4292 f2a9dc41 2019-12-13 tracey }
4293 17ed4618 2019-06-02 stsp }
4294 17ed4618 2019-06-02 stsp
4295 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4296 20a2ad1c 2020-10-20 stsp size_t root_len;
4297 20a2ad1c 2020-10-20 stsp
4298 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4299 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4300 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4301 12463d8b 2019-12-13 stsp ondisk_path);
4302 12463d8b 2019-12-13 stsp goto done;
4303 12463d8b 2019-12-13 stsp }
4304 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4305 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4306 12463d8b 2019-12-13 stsp goto done;
4307 15341bfd 2020-03-05 tracey }
4308 15341bfd 2020-03-05 tracey
4309 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4310 20a2ad1c 2020-10-20 stsp do {
4311 20a2ad1c 2020-10-20 stsp char *parent;
4312 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4313 20a2ad1c 2020-10-20 stsp if (err)
4314 2513f20a 2020-10-20 stsp goto done;
4315 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4316 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4317 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4318 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4319 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4320 20a2ad1c 2020-10-20 stsp ondisk_path);
4321 15341bfd 2020-03-05 tracey break;
4322 15341bfd 2020-03-05 tracey }
4323 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4324 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4325 f2a9dc41 2019-12-13 tracey }
4326 17ed4618 2019-06-02 stsp
4327 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4328 f2a9dc41 2019-12-13 tracey done:
4329 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4330 f2a9dc41 2019-12-13 tracey if (err)
4331 f2a9dc41 2019-12-13 tracey return err;
4332 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4333 f2a9dc41 2019-12-13 tracey return NULL;
4334 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4335 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4336 17ed4618 2019-06-02 stsp }
4337 17ed4618 2019-06-02 stsp
4338 2ec1f75b 2019-03-26 stsp const struct got_error *
4339 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4340 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4341 766841c2 2020-08-13 stsp const char *status_codes,
4342 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4343 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4344 2ec1f75b 2019-03-26 stsp {
4345 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4346 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4347 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4348 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4349 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4350 2ec1f75b 2019-03-26 stsp
4351 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4352 2ec1f75b 2019-03-26 stsp if (err)
4353 2ec1f75b 2019-03-26 stsp return err;
4354 2ec1f75b 2019-03-26 stsp
4355 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4356 2ec1f75b 2019-03-26 stsp if (err)
4357 2ec1f75b 2019-03-26 stsp goto done;
4358 f2a9dc41 2019-12-13 tracey
4359 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4360 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4361 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4362 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4363 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4364 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4365 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4366 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4367 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4368 2ec1f75b 2019-03-26 stsp
4369 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4370 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4371 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4372 17ed4618 2019-06-02 stsp if (err)
4373 af12c6b9 2019-06-04 stsp break;
4374 2ec1f75b 2019-03-26 stsp }
4375 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4376 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4377 af12c6b9 2019-06-04 stsp err = sync_err;
4378 2ec1f75b 2019-03-26 stsp done:
4379 fb399478 2019-07-12 stsp free(fileindex_path);
4380 2ec1f75b 2019-03-26 stsp if (fileindex)
4381 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4382 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4383 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4384 2ec1f75b 2019-03-26 stsp err = unlockerr;
4385 33aa809d 2019-08-08 stsp return err;
4386 33aa809d 2019-08-08 stsp }
4387 33aa809d 2019-08-08 stsp
4388 33aa809d 2019-08-08 stsp static const struct got_error *
4389 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4390 33aa809d 2019-08-08 stsp {
4391 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4392 33aa809d 2019-08-08 stsp char *line = NULL;
4393 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4394 33aa809d 2019-08-08 stsp ssize_t linelen;
4395 33aa809d 2019-08-08 stsp
4396 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4397 33aa809d 2019-08-08 stsp if (linelen == -1) {
4398 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4399 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4400 33aa809d 2019-08-08 stsp goto done;
4401 33aa809d 2019-08-08 stsp }
4402 33aa809d 2019-08-08 stsp return NULL;
4403 33aa809d 2019-08-08 stsp }
4404 33aa809d 2019-08-08 stsp if (outfile) {
4405 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4406 33aa809d 2019-08-08 stsp if (n != linelen) {
4407 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4408 33aa809d 2019-08-08 stsp goto done;
4409 33aa809d 2019-08-08 stsp }
4410 33aa809d 2019-08-08 stsp }
4411 33aa809d 2019-08-08 stsp if (rejectfile) {
4412 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4413 33aa809d 2019-08-08 stsp if (n != linelen)
4414 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4415 33aa809d 2019-08-08 stsp }
4416 33aa809d 2019-08-08 stsp done:
4417 33aa809d 2019-08-08 stsp free(line);
4418 2ec1f75b 2019-03-26 stsp return err;
4419 2ec1f75b 2019-03-26 stsp }
4420 1f1abb7e 2019-08-08 stsp
4421 33aa809d 2019-08-08 stsp static const struct got_error *
4422 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4423 33aa809d 2019-08-08 stsp {
4424 33aa809d 2019-08-08 stsp char *line = NULL;
4425 33aa809d 2019-08-08 stsp size_t linesize = 0;
4426 33aa809d 2019-08-08 stsp ssize_t linelen;
4427 33aa809d 2019-08-08 stsp
4428 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4429 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4430 500467ff 2019-09-25 hiltjo if (ferror(f))
4431 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4432 500467ff 2019-09-25 hiltjo return NULL;
4433 500467ff 2019-09-25 hiltjo }
4434 33aa809d 2019-08-08 stsp free(line);
4435 33aa809d 2019-08-08 stsp return NULL;
4436 33aa809d 2019-08-08 stsp }
4437 33aa809d 2019-08-08 stsp
4438 33aa809d 2019-08-08 stsp static const struct got_error *
4439 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4440 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4441 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4442 abc59930 2021-09-05 naddy {
4443 33aa809d 2019-08-08 stsp const struct got_error *err;
4444 33aa809d 2019-08-08 stsp
4445 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4446 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4447 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4448 33aa809d 2019-08-08 stsp if (err)
4449 33aa809d 2019-08-08 stsp return err;
4450 33aa809d 2019-08-08 stsp (*line_cur1)++;
4451 33aa809d 2019-08-08 stsp }
4452 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4453 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4454 33aa809d 2019-08-08 stsp if (rejectfile)
4455 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4456 33aa809d 2019-08-08 stsp else
4457 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4458 33aa809d 2019-08-08 stsp if (err)
4459 33aa809d 2019-08-08 stsp return err;
4460 33aa809d 2019-08-08 stsp (*line_cur2)++;
4461 33aa809d 2019-08-08 stsp }
4462 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4463 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4464 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4465 33aa809d 2019-08-08 stsp if (err)
4466 33aa809d 2019-08-08 stsp return err;
4467 33aa809d 2019-08-08 stsp (*line_cur2)++;
4468 33aa809d 2019-08-08 stsp }
4469 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4470 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4471 33aa809d 2019-08-08 stsp if (rejectfile)
4472 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4473 33aa809d 2019-08-08 stsp else
4474 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4475 33aa809d 2019-08-08 stsp if (err)
4476 33aa809d 2019-08-08 stsp return err;
4477 33aa809d 2019-08-08 stsp (*line_cur1)++;
4478 33aa809d 2019-08-08 stsp }
4479 f1e81a05 2019-08-10 stsp
4480 f1e81a05 2019-08-10 stsp return NULL;
4481 f1e81a05 2019-08-10 stsp }
4482 f1e81a05 2019-08-10 stsp
4483 f1e81a05 2019-08-10 stsp static const struct got_error *
4484 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4485 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4486 f1e81a05 2019-08-10 stsp {
4487 f1e81a05 2019-08-10 stsp const struct got_error *err;
4488 f1e81a05 2019-08-10 stsp
4489 f1e81a05 2019-08-10 stsp if (outfile) {
4490 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4491 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4492 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4493 f1e81a05 2019-08-10 stsp if (err)
4494 f1e81a05 2019-08-10 stsp return err;
4495 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4496 f1e81a05 2019-08-10 stsp }
4497 f1e81a05 2019-08-10 stsp }
4498 f1e81a05 2019-08-10 stsp if (rejectfile) {
4499 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4500 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4501 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4502 f1e81a05 2019-08-10 stsp if (err)
4503 f1e81a05 2019-08-10 stsp return err;
4504 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4505 f1e81a05 2019-08-10 stsp }
4506 33aa809d 2019-08-08 stsp }
4507 33aa809d 2019-08-08 stsp
4508 33aa809d 2019-08-08 stsp return NULL;
4509 33aa809d 2019-08-08 stsp }
4510 33aa809d 2019-08-08 stsp
4511 33aa809d 2019-08-08 stsp static const struct got_error *
4512 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4513 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4514 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4515 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4516 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4517 33aa809d 2019-08-08 stsp {
4518 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4519 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4520 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4521 33aa809d 2019-08-08 stsp FILE *hunkfile;
4522 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4523 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4524 fe621944 2020-11-10 stsp int rc;
4525 33aa809d 2019-08-08 stsp
4526 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4527 33aa809d 2019-08-08 stsp
4528 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4529 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4530 fe621944 2020-11-10 stsp start_old = cc.left.start;
4531 fe621944 2020-11-10 stsp end_old = cc.left.end;
4532 fe621944 2020-11-10 stsp start_new = cc.right.start;
4533 fe621944 2020-11-10 stsp end_new = cc.right.end;
4534 33aa809d 2019-08-08 stsp
4535 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4536 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4537 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4538 33aa809d 2019-08-08 stsp
4539 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4540 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4541 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4542 33aa809d 2019-08-08 stsp
4543 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4544 fe621944 2020-11-10 stsp if (diff_state == NULL)
4545 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4546 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4547 fe621944 2020-11-10 stsp
4548 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4549 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4550 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4551 33aa809d 2019-08-08 stsp goto done;
4552 33aa809d 2019-08-08 stsp }
4553 fe621944 2020-11-10 stsp
4554 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4555 fe621944 2020-11-10 stsp diff_result, &cc);
4556 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4557 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4558 33aa809d 2019-08-08 stsp goto done;
4559 33aa809d 2019-08-08 stsp }
4560 fe621944 2020-11-10 stsp
4561 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4562 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4563 33aa809d 2019-08-08 stsp goto done;
4564 33aa809d 2019-08-08 stsp }
4565 33aa809d 2019-08-08 stsp
4566 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4567 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4568 33aa809d 2019-08-08 stsp if (err)
4569 33aa809d 2019-08-08 stsp goto done;
4570 33aa809d 2019-08-08 stsp
4571 33aa809d 2019-08-08 stsp switch (*choice) {
4572 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4573 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4574 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4575 33aa809d 2019-08-08 stsp break;
4576 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4577 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4578 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4579 33aa809d 2019-08-08 stsp break;
4580 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4581 33aa809d 2019-08-08 stsp break;
4582 33aa809d 2019-08-08 stsp default:
4583 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4584 33aa809d 2019-08-08 stsp break;
4585 33aa809d 2019-08-08 stsp }
4586 33aa809d 2019-08-08 stsp done:
4587 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4588 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4589 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4590 33aa809d 2019-08-08 stsp return err;
4591 33aa809d 2019-08-08 stsp }
4592 33aa809d 2019-08-08 stsp
4593 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4594 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4595 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4596 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4597 1f1abb7e 2019-08-08 stsp void *progress_arg;
4598 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4599 33aa809d 2019-08-08 stsp void *patch_arg;
4600 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4601 10604dce 2021-09-24 thomas int unlink_added_files;
4602 1f1abb7e 2019-08-08 stsp };
4603 a129376b 2019-03-28 stsp
4604 e20a8b6f 2019-06-04 stsp static const struct got_error *
4605 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4606 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4607 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4608 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4609 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4610 33aa809d 2019-08-08 stsp {
4611 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4612 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4613 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4614 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4615 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4616 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4617 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4618 fe621944 2020-11-10 stsp struct stat sb2;
4619 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4620 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4621 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4622 33aa809d 2019-08-08 stsp
4623 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4624 33aa809d 2019-08-08 stsp
4625 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4626 33aa809d 2019-08-08 stsp if (err)
4627 33aa809d 2019-08-08 stsp return err;
4628 33aa809d 2019-08-08 stsp
4629 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4630 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4631 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4632 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4633 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4634 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4635 fa3cef63 2020-07-23 stsp goto done;
4636 fa3cef63 2020-07-23 stsp }
4637 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4638 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4639 48b4f239 2021-12-31 thomas if (link_len == -1) {
4640 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4641 48b4f239 2021-12-31 thomas path2);
4642 48b4f239 2021-12-31 thomas }
4643 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4644 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4645 12463d8b 2019-12-13 stsp }
4646 12463d8b 2019-12-13 stsp } else {
4647 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4648 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4649 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4650 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4651 fa3cef63 2020-07-23 stsp goto done;
4652 fa3cef63 2020-07-23 stsp }
4653 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4654 fa3cef63 2020-07-23 stsp sizeof(link_target));
4655 fa3cef63 2020-07-23 stsp if (link_len == -1)
4656 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4657 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4658 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4659 12463d8b 2019-12-13 stsp }
4660 1ebedb77 2019-10-19 stsp }
4661 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4662 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4663 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4664 fa3cef63 2020-07-23 stsp goto done;
4665 fa3cef63 2020-07-23 stsp }
4666 1ebedb77 2019-10-19 stsp
4667 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4668 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4669 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4670 fa3cef63 2020-07-23 stsp goto done;
4671 fa3cef63 2020-07-23 stsp }
4672 fa3cef63 2020-07-23 stsp fd2 = -1;
4673 fa3cef63 2020-07-23 stsp } else {
4674 fa3cef63 2020-07-23 stsp size_t n;
4675 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4676 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4677 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4678 fa3cef63 2020-07-23 stsp goto done;
4679 fa3cef63 2020-07-23 stsp }
4680 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4681 fa3cef63 2020-07-23 stsp if (n != link_len) {
4682 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4683 fa3cef63 2020-07-23 stsp goto done;
4684 fa3cef63 2020-07-23 stsp }
4685 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4686 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4687 fa3cef63 2020-07-23 stsp goto done;
4688 fa3cef63 2020-07-23 stsp }
4689 fa3cef63 2020-07-23 stsp rewind(f2);
4690 33aa809d 2019-08-08 stsp }
4691 33aa809d 2019-08-08 stsp
4692 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4693 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4694 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4695 f4ae6ddb 2022-07-01 thomas goto done;
4696 f4ae6ddb 2022-07-01 thomas }
4697 f4ae6ddb 2022-07-01 thomas
4698 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4699 33aa809d 2019-08-08 stsp if (err)
4700 33aa809d 2019-08-08 stsp goto done;
4701 33aa809d 2019-08-08 stsp
4702 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4703 33aa809d 2019-08-08 stsp if (err)
4704 33aa809d 2019-08-08 stsp goto done;
4705 33aa809d 2019-08-08 stsp
4706 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4707 33aa809d 2019-08-08 stsp if (err)
4708 33aa809d 2019-08-08 stsp goto done;
4709 33aa809d 2019-08-08 stsp
4710 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4711 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4712 33aa809d 2019-08-08 stsp if (err)
4713 33aa809d 2019-08-08 stsp goto done;
4714 33aa809d 2019-08-08 stsp
4715 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4716 fc2a50f2 2022-11-01 thomas "");
4717 33aa809d 2019-08-08 stsp if (err)
4718 33aa809d 2019-08-08 stsp goto done;
4719 33aa809d 2019-08-08 stsp
4720 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4721 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4722 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4723 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4724 fe621944 2020-11-10 stsp
4725 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4726 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4727 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4728 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4729 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4730 fe621944 2020-11-10 stsp nchanges++;
4731 fe621944 2020-11-10 stsp }
4732 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4733 33aa809d 2019-08-08 stsp int choice;
4734 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4735 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4736 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4737 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4738 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4739 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4740 33aa809d 2019-08-08 stsp if (err)
4741 33aa809d 2019-08-08 stsp goto done;
4742 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4743 33aa809d 2019-08-08 stsp have_content = 1;
4744 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4745 33aa809d 2019-08-08 stsp break;
4746 33aa809d 2019-08-08 stsp }
4747 1ebedb77 2019-10-19 stsp if (have_content) {
4748 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4749 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4750 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4751 1ebedb77 2019-10-19 stsp if (err)
4752 1ebedb77 2019-10-19 stsp goto done;
4753 1ebedb77 2019-10-19 stsp
4754 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4755 a2c162eb 2022-10-30 thomas mode_t mode;
4756 a2c162eb 2022-10-30 thomas
4757 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4758 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4759 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4760 fa3cef63 2020-07-23 stsp goto done;
4761 fa3cef63 2020-07-23 stsp }
4762 1ebedb77 2019-10-19 stsp }
4763 1ebedb77 2019-10-19 stsp }
4764 33aa809d 2019-08-08 stsp done:
4765 33aa809d 2019-08-08 stsp free(id_str);
4766 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4767 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4768 33aa809d 2019-08-08 stsp if (blob)
4769 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4770 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4771 fe621944 2020-11-10 stsp if (err == NULL)
4772 fe621944 2020-11-10 stsp err = free_err;
4773 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4774 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4775 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4776 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4777 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4778 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4779 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4780 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4781 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4782 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4783 33aa809d 2019-08-08 stsp if (err || !have_content) {
4784 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4785 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4786 33aa809d 2019-08-08 stsp free(*path_outfile);
4787 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4788 33aa809d 2019-08-08 stsp }
4789 33aa809d 2019-08-08 stsp free(path1);
4790 33aa809d 2019-08-08 stsp return err;
4791 33aa809d 2019-08-08 stsp }
4792 33aa809d 2019-08-08 stsp
4793 33aa809d 2019-08-08 stsp static const struct got_error *
4794 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4795 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4796 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4797 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4798 a129376b 2019-03-28 stsp {
4799 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4800 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4801 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4802 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4803 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4804 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4805 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4806 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4807 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4808 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4809 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4810 f4ae6ddb 2022-07-01 thomas int fd = -1;
4811 a129376b 2019-03-28 stsp
4812 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4813 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4814 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4815 d3bcc3d1 2019-08-08 stsp return NULL;
4816 3d69ad8d 2019-08-17 semarie
4817 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4818 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4819 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4820 d3bcc3d1 2019-08-08 stsp
4821 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4822 65084dad 2019-08-08 stsp if (ie == NULL)
4823 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4824 a129376b 2019-03-28 stsp
4825 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4826 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4827 a129376b 2019-03-28 stsp if (err) {
4828 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4829 a129376b 2019-03-28 stsp goto done;
4830 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4831 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4832 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4833 e20a8b6f 2019-06-04 stsp goto done;
4834 e20a8b6f 2019-06-04 stsp }
4835 a129376b 2019-03-28 stsp }
4836 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4837 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4838 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4839 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4840 a129376b 2019-03-28 stsp goto done;
4841 a129376b 2019-03-28 stsp }
4842 a129376b 2019-03-28 stsp } else {
4843 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4844 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4845 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4846 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4847 a129376b 2019-03-28 stsp goto done;
4848 a129376b 2019-03-28 stsp }
4849 a129376b 2019-03-28 stsp } else {
4850 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4851 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4852 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4853 a129376b 2019-03-28 stsp goto done;
4854 a129376b 2019-03-28 stsp }
4855 a129376b 2019-03-28 stsp }
4856 a129376b 2019-03-28 stsp }
4857 a129376b 2019-03-28 stsp
4858 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4859 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4860 945f9229 2022-04-16 thomas if (err)
4861 945f9229 2022-04-16 thomas goto done;
4862 945f9229 2022-04-16 thomas
4863 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4864 a9fa2909 2019-07-27 stsp if (err) {
4865 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4866 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4867 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4868 a9fa2909 2019-07-27 stsp goto done;
4869 a9fa2909 2019-07-27 stsp } else {
4870 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4871 a9fa2909 2019-07-27 stsp if (err)
4872 a9fa2909 2019-07-27 stsp goto done;
4873 a9fa2909 2019-07-27 stsp
4874 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4875 1233e6b6 2020-10-19 stsp if (err)
4876 a9fa2909 2019-07-27 stsp goto done;
4877 a9fa2909 2019-07-27 stsp
4878 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4879 1233e6b6 2020-10-19 stsp free(te_name);
4880 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4881 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4882 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4883 a9fa2909 2019-07-27 stsp goto done;
4884 a9fa2909 2019-07-27 stsp }
4885 a129376b 2019-03-28 stsp }
4886 a129376b 2019-03-28 stsp
4887 a129376b 2019-03-28 stsp switch (status) {
4888 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4889 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4890 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4891 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4892 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4893 33aa809d 2019-08-08 stsp if (err)
4894 33aa809d 2019-08-08 stsp goto done;
4895 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4896 33aa809d 2019-08-08 stsp break;
4897 33aa809d 2019-08-08 stsp }
4898 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4899 1f1abb7e 2019-08-08 stsp ie->path);
4900 1ee397ad 2019-07-12 stsp if (err)
4901 1ee397ad 2019-07-12 stsp goto done;
4902 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4903 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4904 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4905 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4906 10604dce 2021-09-24 thomas relpath) == -1) {
4907 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4908 10604dce 2021-09-24 thomas goto done;
4909 10604dce 2021-09-24 thomas }
4910 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4911 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4912 10604dce 2021-09-24 thomas ondisk_path);
4913 10604dce 2021-09-24 thomas break;
4914 10604dce 2021-09-24 thomas }
4915 10604dce 2021-09-24 thomas }
4916 a129376b 2019-03-28 stsp break;
4917 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4918 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4919 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4920 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4921 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4922 33aa809d 2019-08-08 stsp if (err)
4923 33aa809d 2019-08-08 stsp goto done;
4924 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4925 33aa809d 2019-08-08 stsp break;
4926 33aa809d 2019-08-08 stsp }
4927 33aa809d 2019-08-08 stsp /* fall through */
4928 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4929 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4930 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4931 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4932 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4933 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4934 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4935 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4936 43010591 2023-02-17 thomas else
4937 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4938 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4939 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4940 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4941 f4ae6ddb 2022-07-01 thomas goto done;
4942 f4ae6ddb 2022-07-01 thomas }
4943 f4ae6ddb 2022-07-01 thomas
4944 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4945 a129376b 2019-03-28 stsp if (err)
4946 65084dad 2019-08-08 stsp goto done;
4947 65084dad 2019-08-08 stsp
4948 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4949 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4950 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4951 a129376b 2019-03-28 stsp goto done;
4952 65084dad 2019-08-08 stsp }
4953 65084dad 2019-08-08 stsp
4954 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4955 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4956 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4957 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4958 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4959 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4960 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4961 33aa809d 2019-08-08 stsp break;
4962 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4963 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4964 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4965 369fd7e5 2020-07-23 stsp path_content);
4966 369fd7e5 2020-07-23 stsp break;
4967 369fd7e5 2020-07-23 stsp }
4968 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4969 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4970 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4971 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4972 369fd7e5 2020-07-23 stsp } else {
4973 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4974 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4975 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4976 369fd7e5 2020-07-23 stsp goto done;
4977 369fd7e5 2020-07-23 stsp }
4978 33aa809d 2019-08-08 stsp }
4979 33aa809d 2019-08-08 stsp } else {
4980 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4981 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4982 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4983 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4984 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4985 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4986 2e1fa222 2020-07-23 stsp } else {
4987 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4988 2e1fa222 2020-07-23 stsp ie->path,
4989 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4990 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4991 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4992 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4993 2e1fa222 2020-07-23 stsp }
4994 a129376b 2019-03-28 stsp if (err)
4995 a129376b 2019-03-28 stsp goto done;
4996 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4997 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4998 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4999 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5000 437adc9d 2020-12-10 yzhong blob->id.sha1,
5001 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5002 2e1fa222 2020-07-23 stsp if (err)
5003 2e1fa222 2020-07-23 stsp goto done;
5004 2e1fa222 2020-07-23 stsp }
5005 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5006 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5007 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5008 33aa809d 2019-08-08 stsp }
5009 a129376b 2019-03-28 stsp }
5010 a129376b 2019-03-28 stsp break;
5011 e20a8b6f 2019-06-04 stsp }
5012 a129376b 2019-03-28 stsp default:
5013 1f1abb7e 2019-08-08 stsp break;
5014 a129376b 2019-03-28 stsp }
5015 a129376b 2019-03-28 stsp done:
5016 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5017 33aa809d 2019-08-08 stsp free(path_content);
5018 e20a8b6f 2019-06-04 stsp free(parent_path);
5019 a129376b 2019-03-28 stsp free(tree_path);
5020 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5021 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5022 a129376b 2019-03-28 stsp if (blob)
5023 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5024 a129376b 2019-03-28 stsp if (tree)
5025 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5026 a129376b 2019-03-28 stsp free(tree_id);
5027 945f9229 2022-04-16 thomas if (base_commit)
5028 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5029 e20a8b6f 2019-06-04 stsp return err;
5030 e20a8b6f 2019-06-04 stsp }
5031 e20a8b6f 2019-06-04 stsp
5032 e20a8b6f 2019-06-04 stsp const struct got_error *
5033 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5034 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5035 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5036 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5037 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5038 e20a8b6f 2019-06-04 stsp {
5039 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5040 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5041 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5042 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5043 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5044 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5045 e20a8b6f 2019-06-04 stsp
5046 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5047 e20a8b6f 2019-06-04 stsp if (err)
5048 e20a8b6f 2019-06-04 stsp return err;
5049 e20a8b6f 2019-06-04 stsp
5050 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5051 e20a8b6f 2019-06-04 stsp if (err)
5052 e20a8b6f 2019-06-04 stsp goto done;
5053 e20a8b6f 2019-06-04 stsp
5054 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5055 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5056 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5057 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5058 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5059 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5060 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5061 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5062 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5063 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5064 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5065 e20a8b6f 2019-06-04 stsp if (err)
5066 af12c6b9 2019-06-04 stsp break;
5067 e20a8b6f 2019-06-04 stsp }
5068 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5069 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5070 e20a8b6f 2019-06-04 stsp err = sync_err;
5071 af12c6b9 2019-06-04 stsp done:
5072 fb399478 2019-07-12 stsp free(fileindex_path);
5073 a129376b 2019-03-28 stsp if (fileindex)
5074 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5075 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5076 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5077 a129376b 2019-03-28 stsp err = unlockerr;
5078 c4296144 2019-05-09 stsp return err;
5079 c4296144 2019-05-09 stsp }
5080 c4296144 2019-05-09 stsp
5081 cf066bf8 2019-05-09 stsp static void
5082 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5083 cf066bf8 2019-05-09 stsp {
5084 24519714 2019-05-09 stsp free(ct->path);
5085 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5086 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5087 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5088 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5089 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5090 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5091 cf066bf8 2019-05-09 stsp free(ct);
5092 cf066bf8 2019-05-09 stsp }
5093 24519714 2019-05-09 stsp
5094 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5095 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5096 24519714 2019-05-09 stsp struct got_repository *repo;
5097 24519714 2019-05-09 stsp struct got_worktree *worktree;
5098 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5099 5f8a88c6 2019-08-03 stsp int have_staged_files;
5100 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5101 ef899790 2022-11-01 thomas int diff_header_shown;
5102 be94c032 2023-02-20 thomas int commit_conflicts;
5103 ef899790 2022-11-01 thomas FILE *diff_outfile;
5104 ef899790 2022-11-01 thomas FILE *f1;
5105 ef899790 2022-11-01 thomas FILE *f2;
5106 24519714 2019-05-09 stsp };
5107 ef899790 2022-11-01 thomas
5108 ef899790 2022-11-01 thomas /*
5109 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5110 ef899790 2022-11-01 thomas * it as content to the diff engine.
5111 ef899790 2022-11-01 thomas */
5112 ef899790 2022-11-01 thomas static const struct got_error *
5113 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5114 ef899790 2022-11-01 thomas const char *abspath)
5115 ef899790 2022-11-01 thomas {
5116 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5117 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5118 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5119 ef899790 2022-11-01 thomas
5120 ef899790 2022-11-01 thomas *fd = -1;
5121 ef899790 2022-11-01 thomas
5122 ef899790 2022-11-01 thomas if (dirfd != -1) {
5123 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5124 ef899790 2022-11-01 thomas if (target_len == -1)
5125 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5126 ef899790 2022-11-01 thomas } else {
5127 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5128 ef899790 2022-11-01 thomas if (target_len == -1)
5129 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5130 ef899790 2022-11-01 thomas }
5131 ef899790 2022-11-01 thomas
5132 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5133 ef899790 2022-11-01 thomas if (*fd == -1)
5134 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5135 ef899790 2022-11-01 thomas
5136 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5137 ef899790 2022-11-01 thomas if (outlen == -1) {
5138 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5139 ef899790 2022-11-01 thomas goto done;
5140 ef899790 2022-11-01 thomas }
5141 ef899790 2022-11-01 thomas
5142 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5143 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5144 ef899790 2022-11-01 thomas goto done;
5145 ef899790 2022-11-01 thomas }
5146 ef899790 2022-11-01 thomas done:
5147 ef899790 2022-11-01 thomas if (err) {
5148 ef899790 2022-11-01 thomas close(*fd);
5149 ef899790 2022-11-01 thomas *fd = -1;
5150 ef899790 2022-11-01 thomas }
5151 ef899790 2022-11-01 thomas return err;
5152 ef899790 2022-11-01 thomas }
5153 ef899790 2022-11-01 thomas
5154 ef899790 2022-11-01 thomas static const struct got_error *
5155 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5156 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5157 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5158 ef899790 2022-11-01 thomas {
5159 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5160 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5161 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5162 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5163 ef899790 2022-11-01 thomas char *label1 = NULL;
5164 ef899790 2022-11-01 thomas struct stat sb;
5165 ef899790 2022-11-01 thomas off_t size1 = 0;
5166 ef899790 2022-11-01 thomas int f2_exists = 0;
5167 ef899790 2022-11-01 thomas char *id_str = NULL;
5168 ef899790 2022-11-01 thomas
5169 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5170 d259e491 2022-11-01 thomas
5171 d259e491 2022-11-01 thomas if (diff_staged) {
5172 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5173 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5174 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5175 d259e491 2022-11-01 thomas return NULL;
5176 d259e491 2022-11-01 thomas } else {
5177 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5178 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5179 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5180 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5181 d259e491 2022-11-01 thomas return NULL;
5182 d259e491 2022-11-01 thomas }
5183 ef899790 2022-11-01 thomas
5184 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5185 ef899790 2022-11-01 thomas if (err)
5186 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5187 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5188 ef899790 2022-11-01 thomas if (err)
5189 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5190 ef899790 2022-11-01 thomas
5191 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5192 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5193 ef899790 2022-11-01 thomas if (err)
5194 ef899790 2022-11-01 thomas return err;
5195 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5196 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5197 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5198 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5199 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5200 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5201 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5202 ef899790 2022-11-01 thomas }
5203 ef899790 2022-11-01 thomas
5204 ef899790 2022-11-01 thomas if (diff_staged) {
5205 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5206 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5207 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5208 ef899790 2022-11-01 thomas label1 = ct->path;
5209 ef899790 2022-11-01 thomas label2 = ct->path;
5210 ef899790 2022-11-01 thomas break;
5211 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5212 ef899790 2022-11-01 thomas label2 = ct->path;
5213 ef899790 2022-11-01 thomas break;
5214 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5215 ef899790 2022-11-01 thomas label1 = ct->path;
5216 ef899790 2022-11-01 thomas break;
5217 ef899790 2022-11-01 thomas default:
5218 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5219 ef899790 2022-11-01 thomas }
5220 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5221 ef899790 2022-11-01 thomas if (fd1 == -1) {
5222 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5223 ef899790 2022-11-01 thomas goto done;
5224 ef899790 2022-11-01 thomas }
5225 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5226 ef899790 2022-11-01 thomas if (fd2 == -1) {
5227 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5228 ef899790 2022-11-01 thomas goto done;
5229 ef899790 2022-11-01 thomas }
5230 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5231 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5232 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5233 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5234 ef899790 2022-11-01 thomas goto done;
5235 ef899790 2022-11-01 thomas }
5236 ef899790 2022-11-01 thomas
5237 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5238 ef899790 2022-11-01 thomas if (fd1 == -1) {
5239 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5240 ef899790 2022-11-01 thomas goto done;
5241 ef899790 2022-11-01 thomas }
5242 ef899790 2022-11-01 thomas
5243 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5244 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5245 ef899790 2022-11-01 thomas 8192, fd1);
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 (ct->status != GOT_STATUS_DELETE) {
5251 ef899790 2022-11-01 thomas if (dirfd != -1) {
5252 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5253 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5254 ef899790 2022-11-01 thomas if (fd == -1) {
5255 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5256 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5257 ef899790 2022-11-01 thomas ct->ondisk_path);
5258 ef899790 2022-11-01 thomas goto done;
5259 ef899790 2022-11-01 thomas }
5260 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5261 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5262 ef899790 2022-11-01 thomas if (err)
5263 ef899790 2022-11-01 thomas goto done;
5264 ef899790 2022-11-01 thomas }
5265 ef899790 2022-11-01 thomas } else {
5266 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5267 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5268 ef899790 2022-11-01 thomas if (fd == -1) {
5269 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5270 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5271 ef899790 2022-11-01 thomas ct->ondisk_path);
5272 ef899790 2022-11-01 thomas goto done;
5273 ef899790 2022-11-01 thomas }
5274 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5275 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5276 ef899790 2022-11-01 thomas if (err)
5277 ef899790 2022-11-01 thomas goto done;
5278 ef899790 2022-11-01 thomas }
5279 ef899790 2022-11-01 thomas }
5280 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5281 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5282 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5283 ef899790 2022-11-01 thomas goto done;
5284 ef899790 2022-11-01 thomas }
5285 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5286 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5287 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5288 ef899790 2022-11-01 thomas goto done;
5289 ef899790 2022-11-01 thomas }
5290 ef899790 2022-11-01 thomas fd = -1;
5291 ef899790 2022-11-01 thomas f2_exists = 1;
5292 ef899790 2022-11-01 thomas }
5293 ef899790 2022-11-01 thomas
5294 ef899790 2022-11-01 thomas if (blob1) {
5295 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5296 ef899790 2022-11-01 thomas f1, blob1);
5297 ef899790 2022-11-01 thomas if (err)
5298 ef899790 2022-11-01 thomas goto done;
5299 ef899790 2022-11-01 thomas }
5300 ef899790 2022-11-01 thomas
5301 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5302 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5303 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5304 ef899790 2022-11-01 thomas done:
5305 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5306 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5307 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5308 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5309 ef899790 2022-11-01 thomas if (blob1)
5310 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5311 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5312 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5313 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5314 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5315 ef899790 2022-11-01 thomas return err;
5316 ef899790 2022-11-01 thomas }
5317 cf066bf8 2019-05-09 stsp
5318 c4296144 2019-05-09 stsp static const struct got_error *
5319 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5320 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5321 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5322 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5323 c4296144 2019-05-09 stsp {
5324 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5325 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5326 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5327 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5328 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5329 768aea60 2019-05-09 stsp struct stat sb;
5330 c4296144 2019-05-09 stsp
5331 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5332 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5333 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5334 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5335 5f8a88c6 2019-08-03 stsp return NULL;
5336 5f8a88c6 2019-08-03 stsp } else {
5337 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5338 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5339 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5340 be94c032 2023-02-20 thomas }
5341 c4296144 2019-05-09 stsp
5342 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5343 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5344 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5345 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5346 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5347 5f8a88c6 2019-08-03 stsp return NULL;
5348 5f8a88c6 2019-08-03 stsp }
5349 0b5cc0d6 2019-05-09 stsp
5350 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5352 036813ee 2019-05-09 stsp goto done;
5353 036813ee 2019-05-09 stsp }
5354 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5355 036813ee 2019-05-09 stsp parent_path = strdup("");
5356 69960a46 2019-05-09 stsp if (parent_path == NULL)
5357 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5358 69960a46 2019-05-09 stsp } else {
5359 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5360 69960a46 2019-05-09 stsp if (err)
5361 69960a46 2019-05-09 stsp return err;
5362 69960a46 2019-05-09 stsp }
5363 c4296144 2019-05-09 stsp
5364 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5365 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5366 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5367 c4296144 2019-05-09 stsp goto done;
5368 768aea60 2019-05-09 stsp }
5369 768aea60 2019-05-09 stsp
5370 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5371 768aea60 2019-05-09 stsp relpath) == -1) {
5372 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5373 768aea60 2019-05-09 stsp goto done;
5374 768aea60 2019-05-09 stsp }
5375 0aeb8099 2020-07-23 stsp
5376 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5377 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5378 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5379 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5380 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5381 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5382 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5383 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5384 0aeb8099 2020-07-23 stsp break;
5385 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5386 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5387 0aeb8099 2020-07-23 stsp break;
5388 0aeb8099 2020-07-23 stsp default:
5389 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5390 0aeb8099 2020-07-23 stsp goto done;
5391 0aeb8099 2020-07-23 stsp }
5392 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5393 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5394 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5395 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5396 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5397 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5398 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5399 12463d8b 2019-12-13 stsp ct->ondisk_path);
5400 12463d8b 2019-12-13 stsp goto done;
5401 12463d8b 2019-12-13 stsp }
5402 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5403 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5404 768aea60 2019-05-09 stsp goto done;
5405 768aea60 2019-05-09 stsp }
5406 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5407 c4296144 2019-05-09 stsp }
5408 c4296144 2019-05-09 stsp
5409 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5410 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5411 44d03001 2019-05-09 stsp relpath) == -1) {
5412 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5413 44d03001 2019-05-09 stsp goto done;
5414 44d03001 2019-05-09 stsp }
5415 44d03001 2019-05-09 stsp
5416 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5417 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5418 35213c7c 2020-07-23 stsp int is_bad_symlink;
5419 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5420 35213c7c 2020-07-23 stsp ssize_t target_len;
5421 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5422 35213c7c 2020-07-23 stsp sizeof(target_path));
5423 35213c7c 2020-07-23 stsp if (target_len == -1) {
5424 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5425 35213c7c 2020-07-23 stsp ct->ondisk_path);
5426 35213c7c 2020-07-23 stsp goto done;
5427 35213c7c 2020-07-23 stsp }
5428 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5429 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5430 35213c7c 2020-07-23 stsp if (err)
5431 35213c7c 2020-07-23 stsp goto done;
5432 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5433 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5434 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5435 35213c7c 2020-07-23 stsp goto done;
5436 35213c7c 2020-07-23 stsp }
5437 35213c7c 2020-07-23 stsp }
5438 35213c7c 2020-07-23 stsp
5439 35213c7c 2020-07-23 stsp
5440 cf066bf8 2019-05-09 stsp ct->status = status;
5441 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5442 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5443 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5444 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5445 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5446 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5447 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5448 b416585c 2019-05-13 stsp goto done;
5449 b416585c 2019-05-13 stsp }
5450 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5451 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5452 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5453 f0b75401 2019-08-03 stsp goto done;
5454 f0b75401 2019-08-03 stsp }
5455 f0b75401 2019-08-03 stsp }
5456 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5457 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5458 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5459 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5460 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5461 036813ee 2019-05-09 stsp goto done;
5462 036813ee 2019-05-09 stsp }
5463 ca2503ea 2019-05-09 stsp }
5464 24519714 2019-05-09 stsp ct->path = strdup(path);
5465 24519714 2019-05-09 stsp if (ct->path == NULL) {
5466 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5467 24519714 2019-05-09 stsp goto done;
5468 24519714 2019-05-09 stsp }
5469 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5470 ef899790 2022-11-01 thomas if (err)
5471 ef899790 2022-11-01 thomas goto done;
5472 ef899790 2022-11-01 thomas
5473 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5474 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5475 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5476 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5477 ef899790 2022-11-01 thomas if (err)
5478 ef899790 2022-11-01 thomas goto done;
5479 ef899790 2022-11-01 thomas }
5480 c4296144 2019-05-09 stsp done:
5481 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5482 ed175427 2019-05-09 stsp free_commitable(ct);
5483 24519714 2019-05-09 stsp free(parent_path);
5484 036813ee 2019-05-09 stsp free(path);
5485 a129376b 2019-03-28 stsp return err;
5486 a129376b 2019-03-28 stsp }
5487 c4296144 2019-05-09 stsp
5488 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5489 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5490 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5491 036813ee 2019-05-09 stsp struct got_repository *);
5492 ed175427 2019-05-09 stsp
5493 ed175427 2019-05-09 stsp static const struct got_error *
5494 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5495 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5496 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5497 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5498 afa376bf 2019-05-09 stsp struct got_repository *repo)
5499 ed175427 2019-05-09 stsp {
5500 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5501 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5502 036813ee 2019-05-09 stsp char *subpath;
5503 ed175427 2019-05-09 stsp
5504 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5505 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5506 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5507 ed175427 2019-05-09 stsp
5508 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5509 ed175427 2019-05-09 stsp if (err)
5510 ed175427 2019-05-09 stsp return err;
5511 ed175427 2019-05-09 stsp
5512 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5513 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5514 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5515 036813ee 2019-05-09 stsp free(subpath);
5516 036813ee 2019-05-09 stsp return err;
5517 036813ee 2019-05-09 stsp }
5518 ed175427 2019-05-09 stsp
5519 036813ee 2019-05-09 stsp static const struct got_error *
5520 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5521 036813ee 2019-05-09 stsp {
5522 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5523 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5524 ed175427 2019-05-09 stsp
5525 036813ee 2019-05-09 stsp *match = 0;
5526 ed175427 2019-05-09 stsp
5527 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5528 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5529 0f63689d 2019-05-10 stsp return NULL;
5530 036813ee 2019-05-09 stsp }
5531 0b5cc0d6 2019-05-09 stsp
5532 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5533 0f63689d 2019-05-10 stsp if (err)
5534 0f63689d 2019-05-10 stsp return err;
5535 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5536 036813ee 2019-05-09 stsp free(ct_parent_path);
5537 036813ee 2019-05-09 stsp return err;
5538 036813ee 2019-05-09 stsp }
5539 0b5cc0d6 2019-05-09 stsp
5540 768aea60 2019-05-09 stsp static mode_t
5541 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5542 768aea60 2019-05-09 stsp {
5543 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5544 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5545 3d9a4ec4 2020-07-23 stsp
5546 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5547 768aea60 2019-05-09 stsp }
5548 768aea60 2019-05-09 stsp
5549 036813ee 2019-05-09 stsp static const struct got_error *
5550 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5551 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5552 036813ee 2019-05-09 stsp {
5553 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5554 ca2503ea 2019-05-09 stsp
5555 036813ee 2019-05-09 stsp *new_te = NULL;
5556 0b5cc0d6 2019-05-09 stsp
5557 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5558 036813ee 2019-05-09 stsp if (err)
5559 036813ee 2019-05-09 stsp goto done;
5560 0b5cc0d6 2019-05-09 stsp
5561 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5562 036813ee 2019-05-09 stsp
5563 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5564 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5565 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5566 5f8a88c6 2019-08-03 stsp else
5567 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5568 036813ee 2019-05-09 stsp done:
5569 036813ee 2019-05-09 stsp if (err && *new_te) {
5570 56e0773d 2019-11-28 stsp free(*new_te);
5571 036813ee 2019-05-09 stsp *new_te = NULL;
5572 036813ee 2019-05-09 stsp }
5573 036813ee 2019-05-09 stsp return err;
5574 036813ee 2019-05-09 stsp }
5575 036813ee 2019-05-09 stsp
5576 036813ee 2019-05-09 stsp static const struct got_error *
5577 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5578 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5579 036813ee 2019-05-09 stsp {
5580 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5581 102b254e 2020-10-19 stsp char *ct_name = NULL;
5582 036813ee 2019-05-09 stsp
5583 036813ee 2019-05-09 stsp *new_te = NULL;
5584 036813ee 2019-05-09 stsp
5585 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5586 036813ee 2019-05-09 stsp if (*new_te == NULL)
5587 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5588 036813ee 2019-05-09 stsp
5589 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5590 102b254e 2020-10-19 stsp if (err)
5591 036813ee 2019-05-09 stsp goto done;
5592 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5593 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5594 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5595 036813ee 2019-05-09 stsp goto done;
5596 036813ee 2019-05-09 stsp }
5597 036813ee 2019-05-09 stsp
5598 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5599 036813ee 2019-05-09 stsp
5600 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5601 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5602 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5603 5f8a88c6 2019-08-03 stsp else
5604 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5605 036813ee 2019-05-09 stsp done:
5606 102b254e 2020-10-19 stsp free(ct_name);
5607 036813ee 2019-05-09 stsp if (err && *new_te) {
5608 56e0773d 2019-11-28 stsp free(*new_te);
5609 036813ee 2019-05-09 stsp *new_te = NULL;
5610 036813ee 2019-05-09 stsp }
5611 036813ee 2019-05-09 stsp return err;
5612 036813ee 2019-05-09 stsp }
5613 036813ee 2019-05-09 stsp
5614 036813ee 2019-05-09 stsp static const struct got_error *
5615 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5616 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5617 036813ee 2019-05-09 stsp {
5618 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5619 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5620 036813ee 2019-05-09 stsp
5621 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5622 036813ee 2019-05-09 stsp if (err)
5623 036813ee 2019-05-09 stsp return err;
5624 036813ee 2019-05-09 stsp if (new_pe == NULL)
5625 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5626 036813ee 2019-05-09 stsp return NULL;
5627 afa376bf 2019-05-09 stsp }
5628 afa376bf 2019-05-09 stsp
5629 afa376bf 2019-05-09 stsp static const struct got_error *
5630 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5631 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5632 afa376bf 2019-05-09 stsp {
5633 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5634 5f8a88c6 2019-08-03 stsp unsigned char status;
5635 ae1e948a 2021-09-28 thomas
5636 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5637 ae1e948a 2021-09-28 thomas return NULL;
5638 5f8a88c6 2019-08-03 stsp
5639 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5640 afa376bf 2019-05-09 stsp ct_path++;
5641 5f8a88c6 2019-08-03 stsp
5642 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5643 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5644 5f8a88c6 2019-08-03 stsp else
5645 5f8a88c6 2019-08-03 stsp status = ct->status;
5646 5f8a88c6 2019-08-03 stsp
5647 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5648 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5649 036813ee 2019-05-09 stsp }
5650 036813ee 2019-05-09 stsp
5651 036813ee 2019-05-09 stsp static const struct got_error *
5652 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5653 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5654 44d03001 2019-05-09 stsp {
5655 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5656 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5657 44d03001 2019-05-09 stsp char *te_path;
5658 44d03001 2019-05-09 stsp
5659 44d03001 2019-05-09 stsp *modified = 0;
5660 44d03001 2019-05-09 stsp
5661 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5662 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5663 44d03001 2019-05-09 stsp te->name) == -1)
5664 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5665 44d03001 2019-05-09 stsp
5666 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5667 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5668 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5669 44d03001 2019-05-09 stsp strlen(te_path));
5670 62d463ca 2020-10-20 naddy if (*modified)
5671 44d03001 2019-05-09 stsp break;
5672 44d03001 2019-05-09 stsp }
5673 44d03001 2019-05-09 stsp
5674 44d03001 2019-05-09 stsp free(te_path);
5675 44d03001 2019-05-09 stsp return err;
5676 44d03001 2019-05-09 stsp }
5677 44d03001 2019-05-09 stsp
5678 44d03001 2019-05-09 stsp static const struct got_error *
5679 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5680 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5681 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5682 036813ee 2019-05-09 stsp {
5683 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5684 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5685 036813ee 2019-05-09 stsp
5686 036813ee 2019-05-09 stsp *ctp = NULL;
5687 036813ee 2019-05-09 stsp
5688 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5689 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5690 036813ee 2019-05-09 stsp char *ct_name = NULL;
5691 036813ee 2019-05-09 stsp int path_matches;
5692 036813ee 2019-05-09 stsp
5693 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5694 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5695 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5696 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5697 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5698 5f8a88c6 2019-08-03 stsp continue;
5699 5f8a88c6 2019-08-03 stsp } else {
5700 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5701 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5702 5f8a88c6 2019-08-03 stsp continue;
5703 5f8a88c6 2019-08-03 stsp }
5704 036813ee 2019-05-09 stsp
5705 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5706 036813ee 2019-05-09 stsp continue;
5707 036813ee 2019-05-09 stsp
5708 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5709 62d463ca 2020-10-20 naddy if (err)
5710 036813ee 2019-05-09 stsp return err;
5711 036813ee 2019-05-09 stsp if (!path_matches)
5712 036813ee 2019-05-09 stsp continue;
5713 036813ee 2019-05-09 stsp
5714 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5715 d34b633e 2020-10-19 stsp if (err)
5716 d34b633e 2020-10-19 stsp return err;
5717 d34b633e 2020-10-19 stsp
5718 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5719 d34b633e 2020-10-19 stsp free(ct_name);
5720 036813ee 2019-05-09 stsp continue;
5721 d34b633e 2020-10-19 stsp }
5722 d34b633e 2020-10-19 stsp free(ct_name);
5723 036813ee 2019-05-09 stsp
5724 036813ee 2019-05-09 stsp *ctp = ct;
5725 036813ee 2019-05-09 stsp break;
5726 036813ee 2019-05-09 stsp }
5727 2b496619 2019-07-10 stsp
5728 2b496619 2019-07-10 stsp return err;
5729 2b496619 2019-07-10 stsp }
5730 2b496619 2019-07-10 stsp
5731 2b496619 2019-07-10 stsp static const struct got_error *
5732 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5733 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5734 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5735 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5736 2b496619 2019-07-10 stsp struct got_repository *repo)
5737 2b496619 2019-07-10 stsp {
5738 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5739 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5740 2b496619 2019-07-10 stsp char *subtree_path;
5741 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5742 ba580f68 2020-03-22 stsp int nentries;
5743 2b496619 2019-07-10 stsp
5744 2b496619 2019-07-10 stsp *new_tep = NULL;
5745 2b496619 2019-07-10 stsp
5746 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5747 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5748 2b496619 2019-07-10 stsp child_path) == -1)
5749 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5750 036813ee 2019-05-09 stsp
5751 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5752 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5753 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5754 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5755 56e0773d 2019-11-28 stsp
5756 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5757 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5758 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5759 2b496619 2019-07-10 stsp goto done;
5760 2b496619 2019-07-10 stsp }
5761 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5762 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5763 2b496619 2019-07-10 stsp if (err) {
5764 56e0773d 2019-11-28 stsp free(new_te);
5765 2b496619 2019-07-10 stsp goto done;
5766 2b496619 2019-07-10 stsp }
5767 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5768 2b496619 2019-07-10 stsp done:
5769 56e0773d 2019-11-28 stsp free(id);
5770 2b496619 2019-07-10 stsp free(subtree_path);
5771 2b496619 2019-07-10 stsp if (err == NULL)
5772 2b496619 2019-07-10 stsp *new_tep = new_te;
5773 036813ee 2019-05-09 stsp return err;
5774 036813ee 2019-05-09 stsp }
5775 036813ee 2019-05-09 stsp
5776 036813ee 2019-05-09 stsp static const struct got_error *
5777 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5778 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5779 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5780 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5781 036813ee 2019-05-09 stsp struct got_repository *repo)
5782 036813ee 2019-05-09 stsp {
5783 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5784 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5785 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5786 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5787 036813ee 2019-05-09 stsp
5788 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5789 ba580f68 2020-03-22 stsp *nentries = 0;
5790 036813ee 2019-05-09 stsp
5791 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5792 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5793 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5794 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5795 036813ee 2019-05-09 stsp
5796 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5797 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5798 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5799 036813ee 2019-05-09 stsp continue;
5800 036813ee 2019-05-09 stsp
5801 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5802 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5803 036813ee 2019-05-09 stsp continue;
5804 036813ee 2019-05-09 stsp
5805 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5806 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5807 036813ee 2019-05-09 stsp if (err)
5808 036813ee 2019-05-09 stsp goto done;
5809 036813ee 2019-05-09 stsp
5810 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5811 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5812 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5813 036813ee 2019-05-09 stsp if (err)
5814 036813ee 2019-05-09 stsp goto done;
5815 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5816 afa376bf 2019-05-09 stsp if (err)
5817 afa376bf 2019-05-09 stsp goto done;
5818 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5819 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5820 2b496619 2019-07-10 stsp if (err)
5821 2f51b5b3 2019-05-09 stsp goto done;
5822 ba580f68 2020-03-22 stsp (*nentries)++;
5823 2b496619 2019-07-10 stsp } else {
5824 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5825 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5826 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5827 2b496619 2019-07-10 stsp == NULL) {
5828 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5829 2b496619 2019-07-10 stsp child_path, path_base_tree,
5830 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5831 2b496619 2019-07-10 stsp repo);
5832 2b496619 2019-07-10 stsp if (err)
5833 2b496619 2019-07-10 stsp goto done;
5834 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5835 2b496619 2019-07-10 stsp if (err)
5836 2b496619 2019-07-10 stsp goto done;
5837 ba580f68 2020-03-22 stsp (*nentries)++;
5838 9ba0479c 2019-05-10 stsp }
5839 ed175427 2019-05-09 stsp }
5840 2f51b5b3 2019-05-09 stsp }
5841 2f51b5b3 2019-05-09 stsp
5842 2f51b5b3 2019-05-09 stsp if (base_tree) {
5843 56e0773d 2019-11-28 stsp int i, nbase_entries;
5844 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5845 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5846 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5847 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5848 63c5ca5d 2019-08-24 stsp
5849 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5850 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5851 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5852 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5853 63c5ca5d 2019-08-24 stsp if (err)
5854 63c5ca5d 2019-08-24 stsp goto done;
5855 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5856 63c5ca5d 2019-08-24 stsp if (err)
5857 63c5ca5d 2019-08-24 stsp goto done;
5858 ba580f68 2020-03-22 stsp (*nentries)++;
5859 63c5ca5d 2019-08-24 stsp continue;
5860 63c5ca5d 2019-08-24 stsp }
5861 2f51b5b3 2019-05-09 stsp
5862 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5863 44d03001 2019-05-09 stsp int modified;
5864 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5865 036813ee 2019-05-09 stsp if (err)
5866 036813ee 2019-05-09 stsp goto done;
5867 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5868 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5869 2f51b5b3 2019-05-09 stsp if (err)
5870 2f51b5b3 2019-05-09 stsp goto done;
5871 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5872 44d03001 2019-05-09 stsp if (modified) {
5873 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5874 ba580f68 2020-03-22 stsp int nsubentries;
5875 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5876 ba580f68 2020-03-22 stsp &nsubentries, te,
5877 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5878 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5879 44d03001 2019-05-09 stsp if (err)
5880 44d03001 2019-05-09 stsp goto done;
5881 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5882 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5883 ba580f68 2020-03-22 stsp free(new_id);
5884 ba580f68 2020-03-22 stsp continue;
5885 ba580f68 2020-03-22 stsp }
5886 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5887 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5888 56e0773d 2019-11-28 stsp free(new_id);
5889 44d03001 2019-05-09 stsp }
5890 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5891 036813ee 2019-05-09 stsp if (err)
5892 036813ee 2019-05-09 stsp goto done;
5893 ba580f68 2020-03-22 stsp (*nentries)++;
5894 2f51b5b3 2019-05-09 stsp continue;
5895 036813ee 2019-05-09 stsp }
5896 2f51b5b3 2019-05-09 stsp
5897 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5898 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5899 f66c734c 2019-09-22 stsp if (err)
5900 f66c734c 2019-09-22 stsp goto done;
5901 2f51b5b3 2019-05-09 stsp if (ct) {
5902 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5903 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5904 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5905 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5906 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5907 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5908 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5909 2f51b5b3 2019-05-09 stsp if (err)
5910 2f51b5b3 2019-05-09 stsp goto done;
5911 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5912 2f51b5b3 2019-05-09 stsp if (err)
5913 2f51b5b3 2019-05-09 stsp goto done;
5914 ba580f68 2020-03-22 stsp (*nentries)++;
5915 2f51b5b3 2019-05-09 stsp }
5916 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5917 b416585c 2019-05-13 stsp status_arg);
5918 afa376bf 2019-05-09 stsp if (err)
5919 afa376bf 2019-05-09 stsp goto done;
5920 2f51b5b3 2019-05-09 stsp } else {
5921 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5922 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5923 2f51b5b3 2019-05-09 stsp if (err)
5924 2f51b5b3 2019-05-09 stsp goto done;
5925 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5926 2f51b5b3 2019-05-09 stsp if (err)
5927 2f51b5b3 2019-05-09 stsp goto done;
5928 ba580f68 2020-03-22 stsp (*nentries)++;
5929 2f51b5b3 2019-05-09 stsp }
5930 ca2503ea 2019-05-09 stsp }
5931 ed175427 2019-05-09 stsp }
5932 0b5cc0d6 2019-05-09 stsp
5933 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5934 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5935 ed175427 2019-05-09 stsp done:
5936 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5937 2e1fa222 2020-07-23 stsp return err;
5938 2e1fa222 2020-07-23 stsp }
5939 2e1fa222 2020-07-23 stsp
5940 2e1fa222 2020-07-23 stsp static const struct got_error *
5941 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5942 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5943 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5944 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5945 ebf99748 2019-05-09 stsp {
5946 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5947 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5948 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5949 ebf99748 2019-05-09 stsp
5950 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5951 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5952 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5953 ebf99748 2019-05-09 stsp
5954 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5955 437adc9d 2020-12-10 yzhong
5956 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5957 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5958 437adc9d 2020-12-10 yzhong if (err)
5959 437adc9d 2020-12-10 yzhong goto done;
5960 437adc9d 2020-12-10 yzhong
5961 ebf99748 2019-05-09 stsp if (ie) {
5962 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5963 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5964 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5965 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5966 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5967 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5968 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5969 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5970 437adc9d 2020-12-10 yzhong
5971 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5972 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5973 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5974 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5975 72fd46fa 2019-09-06 stsp !have_staged_files);
5976 ebf99748 2019-05-09 stsp } else
5977 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5978 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5979 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5980 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5981 72fd46fa 2019-09-06 stsp !have_staged_files);
5982 ebf99748 2019-05-09 stsp } else {
5983 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5984 ebf99748 2019-05-09 stsp if (err)
5985 437adc9d 2020-12-10 yzhong goto done;
5986 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5987 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5988 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5989 3969253a 2020-03-07 stsp if (err) {
5990 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5991 437adc9d 2020-12-10 yzhong goto done;
5992 3969253a 2020-03-07 stsp }
5993 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5994 3969253a 2020-03-07 stsp if (err) {
5995 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5996 437adc9d 2020-12-10 yzhong goto done;
5997 3969253a 2020-03-07 stsp }
5998 ebf99748 2019-05-09 stsp }
5999 437adc9d 2020-12-10 yzhong free(relpath);
6000 437adc9d 2020-12-10 yzhong relpath = NULL;
6001 ebf99748 2019-05-09 stsp }
6002 437adc9d 2020-12-10 yzhong done:
6003 437adc9d 2020-12-10 yzhong free(relpath);
6004 d56d26ce 2019-05-10 stsp return err;
6005 d56d26ce 2019-05-10 stsp }
6006 735ef5ac 2019-08-03 stsp
6007 d56d26ce 2019-05-10 stsp
6008 d56d26ce 2019-05-10 stsp static const struct got_error *
6009 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6010 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6011 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6012 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6013 735ef5ac 2019-08-03 stsp int ood_errcode)
6014 d56d26ce 2019-05-10 stsp {
6015 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6016 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6017 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6018 1a36436d 2019-06-10 stsp
6019 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6020 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6021 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6022 1a36436d 2019-06-10 stsp return NULL;
6023 9bead371 2019-07-28 stsp /*
6024 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6025 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6026 9bead371 2019-07-28 stsp */
6027 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6028 945f9229 2022-04-16 thomas if (err)
6029 945f9229 2022-04-16 thomas goto done;
6030 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6031 9bead371 2019-07-28 stsp if (err) {
6032 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6033 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6034 1a36436d 2019-06-10 stsp goto done;
6035 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6036 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6037 1a36436d 2019-06-10 stsp } else {
6038 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6039 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6040 945f9229 2022-04-16 thomas if (err)
6041 945f9229 2022-04-16 thomas goto done;
6042 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6043 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6044 1a36436d 2019-06-10 stsp goto done;
6045 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6046 1a36436d 2019-06-10 stsp }
6047 1a36436d 2019-06-10 stsp done:
6048 1a36436d 2019-06-10 stsp free(id);
6049 945f9229 2022-04-16 thomas if (commit)
6050 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6051 1a36436d 2019-06-10 stsp return err;
6052 ebf99748 2019-05-09 stsp }
6053 ebf99748 2019-05-09 stsp
6054 ef20f542 2022-06-26 thomas static const struct got_error *
6055 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6056 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6057 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6058 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6059 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6060 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6061 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6062 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6063 afa376bf 2019-05-09 stsp struct got_repository *repo)
6064 c4296144 2019-05-09 stsp {
6065 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6066 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6067 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6068 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6069 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6070 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6071 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6072 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6073 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6074 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6075 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6076 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6077 892eab0a 2022-07-22 thomas time_t timestamp;
6078 c4296144 2019-05-09 stsp
6079 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6080 c4296144 2019-05-09 stsp
6081 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6082 675c7539 2019-05-09 stsp
6083 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6084 588edf97 2019-05-10 stsp if (err)
6085 588edf97 2019-05-10 stsp goto done;
6086 de18fc63 2019-05-09 stsp
6087 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6088 588edf97 2019-05-10 stsp if (err)
6089 588edf97 2019-05-10 stsp goto done;
6090 588edf97 2019-05-10 stsp
6091 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6092 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6093 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6094 33ad4cbe 2019-05-12 jcs if (err)
6095 33ad4cbe 2019-05-12 jcs goto done;
6096 33ad4cbe 2019-05-12 jcs }
6097 c4296144 2019-05-09 stsp
6098 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6099 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6100 33ad4cbe 2019-05-12 jcs goto done;
6101 33ad4cbe 2019-05-12 jcs }
6102 33ad4cbe 2019-05-12 jcs
6103 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6104 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6105 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6106 cf066bf8 2019-05-09 stsp char *ondisk_path;
6107 cf066bf8 2019-05-09 stsp
6108 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6109 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6110 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6111 5f8a88c6 2019-08-03 stsp continue;
6112 5f8a88c6 2019-08-03 stsp
6113 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6114 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6115 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6116 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6117 cf066bf8 2019-05-09 stsp continue;
6118 cf066bf8 2019-05-09 stsp
6119 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6120 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6121 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6122 cf066bf8 2019-05-09 stsp goto done;
6123 cf066bf8 2019-05-09 stsp }
6124 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6125 2e1fa222 2020-07-23 stsp free(ondisk_path);
6126 2e1fa222 2020-07-23 stsp if (err)
6127 cf066bf8 2019-05-09 stsp goto done;
6128 cf066bf8 2019-05-09 stsp }
6129 036813ee 2019-05-09 stsp
6130 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6131 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6132 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6133 036813ee 2019-05-09 stsp if (err)
6134 036813ee 2019-05-09 stsp goto done;
6135 036813ee 2019-05-09 stsp
6136 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6137 de18fc63 2019-05-09 stsp if (err)
6138 de18fc63 2019-05-09 stsp goto done;
6139 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6140 10604dce 2021-09-24 thomas nparents++;
6141 10604dce 2021-09-24 thomas if (parent_id2) {
6142 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6143 10604dce 2021-09-24 thomas if (err)
6144 10604dce 2021-09-24 thomas goto done;
6145 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6146 10604dce 2021-09-24 thomas nparents++;
6147 10604dce 2021-09-24 thomas }
6148 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6149 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6150 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6151 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6152 33ad4cbe 2019-05-12 jcs free(logmsg);
6153 9d40349a 2019-05-09 stsp if (err)
6154 9d40349a 2019-05-09 stsp goto done;
6155 9d40349a 2019-05-09 stsp
6156 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6157 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6158 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6159 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6160 09f5bd90 2019-05-09 stsp goto done;
6161 09f5bd90 2019-05-09 stsp }
6162 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6163 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6164 09f5bd90 2019-05-09 stsp if (err)
6165 09f5bd90 2019-05-09 stsp goto done;
6166 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6167 ebf99748 2019-05-09 stsp if (err)
6168 ebf99748 2019-05-09 stsp goto done;
6169 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6170 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6171 09f5bd90 2019-05-09 stsp goto done;
6172 09f5bd90 2019-05-09 stsp }
6173 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6174 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6175 f2c16586 2019-05-09 stsp if (err)
6176 f2c16586 2019-05-09 stsp goto done;
6177 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6178 f2c16586 2019-05-09 stsp if (err)
6179 f2c16586 2019-05-09 stsp goto done;
6180 f2c16586 2019-05-09 stsp
6181 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6182 09f5bd90 2019-05-09 stsp if (err)
6183 09f5bd90 2019-05-09 stsp goto done;
6184 09f5bd90 2019-05-09 stsp
6185 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6186 ebf99748 2019-05-09 stsp if (err)
6187 ebf99748 2019-05-09 stsp goto done;
6188 c4296144 2019-05-09 stsp done:
6189 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6190 588edf97 2019-05-10 stsp if (head_tree)
6191 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6192 588edf97 2019-05-10 stsp if (head_commit)
6193 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6194 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6195 2f17228e 2019-05-12 stsp if (head_ref2) {
6196 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6197 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6198 2f17228e 2019-05-12 stsp err = unlockerr;
6199 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6200 39cd0ff6 2019-07-12 stsp }
6201 39cd0ff6 2019-07-12 stsp return err;
6202 39cd0ff6 2019-07-12 stsp }
6203 5c1e53bc 2019-07-28 stsp
6204 5c1e53bc 2019-07-28 stsp static const struct got_error *
6205 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6206 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6207 5c1e53bc 2019-07-28 stsp {
6208 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6209 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6210 39cd0ff6 2019-07-12 stsp
6211 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6212 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6213 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6214 5c1e53bc 2019-07-28 stsp
6215 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6216 5c1e53bc 2019-07-28 stsp ct_path++;
6217 5c1e53bc 2019-07-28 stsp
6218 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6219 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6220 5c1e53bc 2019-07-28 stsp break;
6221 5c1e53bc 2019-07-28 stsp }
6222 5c1e53bc 2019-07-28 stsp
6223 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6224 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6225 5c1e53bc 2019-07-28 stsp
6226 5c1e53bc 2019-07-28 stsp return NULL;
6227 5c1e53bc 2019-07-28 stsp }
6228 5c1e53bc 2019-07-28 stsp
6229 f0b75401 2019-08-03 stsp static const struct got_error *
6230 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6231 f0b75401 2019-08-03 stsp {
6232 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6233 f0b75401 2019-08-03 stsp
6234 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6235 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6236 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6237 f0b75401 2019-08-03 stsp }
6238 f0b75401 2019-08-03 stsp
6239 f0b75401 2019-08-03 stsp return NULL;
6240 f0b75401 2019-08-03 stsp }
6241 f0b75401 2019-08-03 stsp
6242 5f8a88c6 2019-08-03 stsp static const struct got_error *
6243 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6244 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6245 5f8a88c6 2019-08-03 stsp {
6246 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6247 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6248 5f8a88c6 2019-08-03 stsp
6249 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6250 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6251 5f8a88c6 2019-08-03 stsp continue;
6252 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6253 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6254 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6255 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6256 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6257 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6258 5f8a88c6 2019-08-03 stsp }
6259 5f8a88c6 2019-08-03 stsp
6260 5f8a88c6 2019-08-03 stsp return NULL;
6261 5f8a88c6 2019-08-03 stsp }
6262 5f8a88c6 2019-08-03 stsp
6263 39cd0ff6 2019-07-12 stsp const struct got_error *
6264 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6265 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6266 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6267 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6268 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6269 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6270 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6271 39cd0ff6 2019-07-12 stsp {
6272 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6273 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6274 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6275 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6276 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6277 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6278 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6279 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6280 ef899790 2022-11-01 thomas char *diff_path = NULL;
6281 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6282 39cd0ff6 2019-07-12 stsp
6283 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6284 39cd0ff6 2019-07-12 stsp
6285 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6286 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6287 39cd0ff6 2019-07-12 stsp
6288 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6289 39cd0ff6 2019-07-12 stsp if (err)
6290 39cd0ff6 2019-07-12 stsp goto done;
6291 39cd0ff6 2019-07-12 stsp
6292 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6293 39cd0ff6 2019-07-12 stsp if (err)
6294 39cd0ff6 2019-07-12 stsp goto done;
6295 39cd0ff6 2019-07-12 stsp
6296 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6297 39cd0ff6 2019-07-12 stsp if (err)
6298 39cd0ff6 2019-07-12 stsp goto done;
6299 39cd0ff6 2019-07-12 stsp
6300 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6301 39cd0ff6 2019-07-12 stsp if (err)
6302 39cd0ff6 2019-07-12 stsp goto done;
6303 39cd0ff6 2019-07-12 stsp
6304 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6305 5f8a88c6 2019-08-03 stsp &have_staged_files);
6306 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6307 5f8a88c6 2019-08-03 stsp goto done;
6308 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6309 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6310 5f8a88c6 2019-08-03 stsp if (err)
6311 5f8a88c6 2019-08-03 stsp goto done;
6312 5f8a88c6 2019-08-03 stsp }
6313 5f8a88c6 2019-08-03 stsp
6314 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6315 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6316 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6317 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6318 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6319 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6320 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6321 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6322 ef899790 2022-11-01 thomas if (show_diff) {
6323 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6324 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6325 ef899790 2022-11-01 thomas if (err)
6326 ef899790 2022-11-01 thomas goto done;
6327 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6328 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6329 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6330 ef899790 2022-11-01 thomas goto done;
6331 ef899790 2022-11-01 thomas }
6332 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6333 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6334 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6335 ef899790 2022-11-01 thomas goto done;
6336 ef899790 2022-11-01 thomas }
6337 ef899790 2022-11-01 thomas }
6338 ef899790 2022-11-01 thomas
6339 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6340 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6341 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6342 5c1e53bc 2019-07-28 stsp if (err)
6343 5c1e53bc 2019-07-28 stsp goto done;
6344 5c1e53bc 2019-07-28 stsp }
6345 39cd0ff6 2019-07-12 stsp
6346 ef899790 2022-11-01 thomas if (show_diff) {
6347 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6348 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6349 ef899790 2022-11-01 thomas goto done;
6350 ef899790 2022-11-01 thomas }
6351 ef899790 2022-11-01 thomas }
6352 ef899790 2022-11-01 thomas
6353 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6354 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6355 39cd0ff6 2019-07-12 stsp goto done;
6356 5c1e53bc 2019-07-28 stsp }
6357 5c1e53bc 2019-07-28 stsp
6358 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6359 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6360 5c1e53bc 2019-07-28 stsp if (err)
6361 5c1e53bc 2019-07-28 stsp goto done;
6362 39cd0ff6 2019-07-12 stsp }
6363 f0b75401 2019-08-03 stsp
6364 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6365 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6366 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6367 f0b75401 2019-08-03 stsp
6368 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6369 f0b75401 2019-08-03 stsp ct_path++;
6370 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6371 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6372 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6373 b50cabdf 2019-07-12 stsp if (err)
6374 b50cabdf 2019-07-12 stsp goto done;
6375 f0b75401 2019-08-03 stsp
6376 b50cabdf 2019-07-12 stsp }
6377 b50cabdf 2019-07-12 stsp
6378 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6379 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6380 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6381 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6382 39cd0ff6 2019-07-12 stsp if (err)
6383 39cd0ff6 2019-07-12 stsp goto done;
6384 39cd0ff6 2019-07-12 stsp
6385 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6386 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6387 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6388 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6389 39cd0ff6 2019-07-12 stsp err = sync_err;
6390 39cd0ff6 2019-07-12 stsp done:
6391 39cd0ff6 2019-07-12 stsp if (fileindex)
6392 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6393 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6394 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6395 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6396 39cd0ff6 2019-07-12 stsp err = unlockerr;
6397 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6398 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6399 21c2d8be 2023-01-10 thomas
6400 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6401 39cd0ff6 2019-07-12 stsp }
6402 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6403 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6404 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6405 ef899790 2022-11-01 thomas free(diff_path);
6406 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6407 ef899790 2022-11-01 thomas err == NULL)
6408 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6409 c4296144 2019-05-09 stsp return err;
6410 8656d6c4 2019-05-20 stsp }
6411 8656d6c4 2019-05-20 stsp
6412 8656d6c4 2019-05-20 stsp const char *
6413 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6414 8656d6c4 2019-05-20 stsp {
6415 8656d6c4 2019-05-20 stsp return ct->path;
6416 8656d6c4 2019-05-20 stsp }
6417 8656d6c4 2019-05-20 stsp
6418 8656d6c4 2019-05-20 stsp unsigned int
6419 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6420 8656d6c4 2019-05-20 stsp {
6421 8656d6c4 2019-05-20 stsp return ct->status;
6422 818c7501 2019-07-11 stsp }
6423 818c7501 2019-07-11 stsp
6424 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6425 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6426 818c7501 2019-07-11 stsp struct got_repository *repo;
6427 818c7501 2019-07-11 stsp };
6428 818c7501 2019-07-11 stsp
6429 818c7501 2019-07-11 stsp static const struct got_error *
6430 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6431 818c7501 2019-07-11 stsp {
6432 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6433 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6434 818c7501 2019-07-11 stsp unsigned char status;
6435 818c7501 2019-07-11 stsp struct stat sb;
6436 818c7501 2019-07-11 stsp char *ondisk_path;
6437 818c7501 2019-07-11 stsp
6438 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6439 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6440 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6441 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6442 818c7501 2019-07-11 stsp
6443 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6444 818c7501 2019-07-11 stsp == -1)
6445 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6446 818c7501 2019-07-11 stsp
6447 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6448 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6449 818c7501 2019-07-11 stsp free(ondisk_path);
6450 818c7501 2019-07-11 stsp if (err)
6451 818c7501 2019-07-11 stsp return err;
6452 818c7501 2019-07-11 stsp
6453 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6454 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6455 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6456 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6457 818c7501 2019-07-11 stsp
6458 818c7501 2019-07-11 stsp return NULL;
6459 818c7501 2019-07-11 stsp }
6460 818c7501 2019-07-11 stsp
6461 818c7501 2019-07-11 stsp const struct got_error *
6462 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6463 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6464 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6465 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6466 818c7501 2019-07-11 stsp {
6467 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6468 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6469 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6470 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6471 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6472 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6473 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6474 818c7501 2019-07-11 stsp
6475 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6476 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6477 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6478 818c7501 2019-07-11 stsp
6479 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6480 818c7501 2019-07-11 stsp if (err)
6481 818c7501 2019-07-11 stsp return err;
6482 818c7501 2019-07-11 stsp
6483 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6484 818c7501 2019-07-11 stsp if (err)
6485 818c7501 2019-07-11 stsp goto done;
6486 818c7501 2019-07-11 stsp
6487 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6488 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6489 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6490 818c7501 2019-07-11 stsp &ok_arg);
6491 818c7501 2019-07-11 stsp if (err)
6492 818c7501 2019-07-11 stsp goto done;
6493 818c7501 2019-07-11 stsp
6494 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6495 818c7501 2019-07-11 stsp if (err)
6496 818c7501 2019-07-11 stsp goto done;
6497 818c7501 2019-07-11 stsp
6498 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6499 818c7501 2019-07-11 stsp if (err)
6500 818c7501 2019-07-11 stsp goto done;
6501 818c7501 2019-07-11 stsp
6502 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6503 818c7501 2019-07-11 stsp if (err)
6504 818c7501 2019-07-11 stsp goto done;
6505 818c7501 2019-07-11 stsp
6506 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6507 818c7501 2019-07-11 stsp 0);
6508 e51d7b55 2020-01-04 stsp if (err)
6509 e51d7b55 2020-01-04 stsp goto done;
6510 e51d7b55 2020-01-04 stsp
6511 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6512 818c7501 2019-07-11 stsp if (err)
6513 818c7501 2019-07-11 stsp goto done;
6514 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6515 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6516 e51d7b55 2020-01-04 stsp goto done;
6517 e51d7b55 2020-01-04 stsp }
6518 818c7501 2019-07-11 stsp
6519 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6520 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6521 818c7501 2019-07-11 stsp if (err)
6522 818c7501 2019-07-11 stsp goto done;
6523 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6524 818c7501 2019-07-11 stsp if (err)
6525 818c7501 2019-07-11 stsp goto done;
6526 818c7501 2019-07-11 stsp
6527 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6528 818c7501 2019-07-11 stsp
6529 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6530 818c7501 2019-07-11 stsp if (err)
6531 818c7501 2019-07-11 stsp goto done;
6532 818c7501 2019-07-11 stsp
6533 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6534 818c7501 2019-07-11 stsp if (err)
6535 818c7501 2019-07-11 stsp goto done;
6536 818c7501 2019-07-11 stsp
6537 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6538 818c7501 2019-07-11 stsp worktree->base_commit_id);
6539 818c7501 2019-07-11 stsp if (err)
6540 818c7501 2019-07-11 stsp goto done;
6541 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6542 818c7501 2019-07-11 stsp if (err)
6543 818c7501 2019-07-11 stsp goto done;
6544 818c7501 2019-07-11 stsp
6545 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6546 818c7501 2019-07-11 stsp if (err)
6547 818c7501 2019-07-11 stsp goto done;
6548 818c7501 2019-07-11 stsp done:
6549 818c7501 2019-07-11 stsp free(fileindex_path);
6550 818c7501 2019-07-11 stsp free(tmp_branch_name);
6551 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6552 818c7501 2019-07-11 stsp free(branch_ref_name);
6553 818c7501 2019-07-11 stsp if (branch_ref)
6554 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6555 818c7501 2019-07-11 stsp if (wt_branch)
6556 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6557 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6558 818c7501 2019-07-11 stsp if (err) {
6559 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6560 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6561 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6562 818c7501 2019-07-11 stsp }
6563 818c7501 2019-07-11 stsp if (*tmp_branch) {
6564 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6565 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6566 818c7501 2019-07-11 stsp }
6567 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6568 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6569 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6570 3e3a69f1 2019-07-25 stsp }
6571 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6572 818c7501 2019-07-11 stsp }
6573 818c7501 2019-07-11 stsp return err;
6574 818c7501 2019-07-11 stsp }
6575 818c7501 2019-07-11 stsp
6576 818c7501 2019-07-11 stsp const struct got_error *
6577 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6578 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6579 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6580 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6581 818c7501 2019-07-11 stsp {
6582 818c7501 2019-07-11 stsp const struct got_error *err;
6583 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6584 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6585 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6586 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6587 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6588 818c7501 2019-07-11 stsp
6589 818c7501 2019-07-11 stsp *commit_id = NULL;
6590 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6591 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6592 3e3a69f1 2019-07-25 stsp *branch = NULL;
6593 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6594 3e3a69f1 2019-07-25 stsp
6595 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6596 3e3a69f1 2019-07-25 stsp if (err)
6597 3e3a69f1 2019-07-25 stsp return err;
6598 818c7501 2019-07-11 stsp
6599 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6600 3e3a69f1 2019-07-25 stsp if (err)
6601 f032f1f7 2019-08-04 stsp goto done;
6602 f032f1f7 2019-08-04 stsp
6603 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6604 f032f1f7 2019-08-04 stsp &have_staged_files);
6605 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6606 f032f1f7 2019-08-04 stsp goto done;
6607 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6608 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6609 3e3a69f1 2019-07-25 stsp goto done;
6610 f032f1f7 2019-08-04 stsp }
6611 3e3a69f1 2019-07-25 stsp
6612 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6613 818c7501 2019-07-11 stsp if (err)
6614 f032f1f7 2019-08-04 stsp goto done;
6615 818c7501 2019-07-11 stsp
6616 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
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 = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6621 818c7501 2019-07-11 stsp if (err)
6622 818c7501 2019-07-11 stsp goto done;
6623 818c7501 2019-07-11 stsp
6624 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6625 818c7501 2019-07-11 stsp if (err)
6626 818c7501 2019-07-11 stsp goto done;
6627 818c7501 2019-07-11 stsp
6628 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6629 818c7501 2019-07-11 stsp if (err)
6630 818c7501 2019-07-11 stsp goto done;
6631 818c7501 2019-07-11 stsp
6632 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6633 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6634 818c7501 2019-07-11 stsp if (err)
6635 818c7501 2019-07-11 stsp goto done;
6636 818c7501 2019-07-11 stsp
6637 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6638 818c7501 2019-07-11 stsp if (err)
6639 818c7501 2019-07-11 stsp goto done;
6640 818c7501 2019-07-11 stsp
6641 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6642 818c7501 2019-07-11 stsp if (err)
6643 818c7501 2019-07-11 stsp goto done;
6644 818c7501 2019-07-11 stsp
6645 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6646 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6647 818c7501 2019-07-11 stsp if (err)
6648 818c7501 2019-07-11 stsp goto done;
6649 818c7501 2019-07-11 stsp
6650 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6651 818c7501 2019-07-11 stsp if (err)
6652 818c7501 2019-07-11 stsp goto done;
6653 818c7501 2019-07-11 stsp done:
6654 818c7501 2019-07-11 stsp free(commit_ref_name);
6655 818c7501 2019-07-11 stsp free(branch_ref_name);
6656 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6657 818c7501 2019-07-11 stsp if (commit_ref)
6658 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6659 818c7501 2019-07-11 stsp if (branch_ref)
6660 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6661 818c7501 2019-07-11 stsp if (err) {
6662 818c7501 2019-07-11 stsp free(*commit_id);
6663 818c7501 2019-07-11 stsp *commit_id = NULL;
6664 818c7501 2019-07-11 stsp if (*tmp_branch) {
6665 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6666 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6667 818c7501 2019-07-11 stsp }
6668 818c7501 2019-07-11 stsp if (*new_base_branch) {
6669 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6670 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6671 818c7501 2019-07-11 stsp }
6672 818c7501 2019-07-11 stsp if (*branch) {
6673 818c7501 2019-07-11 stsp got_ref_close(*branch);
6674 818c7501 2019-07-11 stsp *branch = NULL;
6675 818c7501 2019-07-11 stsp }
6676 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6677 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6678 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6679 3e3a69f1 2019-07-25 stsp }
6680 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6681 818c7501 2019-07-11 stsp }
6682 818c7501 2019-07-11 stsp return err;
6683 c4296144 2019-05-09 stsp }
6684 818c7501 2019-07-11 stsp
6685 818c7501 2019-07-11 stsp const struct got_error *
6686 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6687 818c7501 2019-07-11 stsp {
6688 818c7501 2019-07-11 stsp const struct got_error *err;
6689 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6690 818c7501 2019-07-11 stsp
6691 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6692 818c7501 2019-07-11 stsp if (err)
6693 818c7501 2019-07-11 stsp return err;
6694 818c7501 2019-07-11 stsp
6695 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6696 818c7501 2019-07-11 stsp free(tmp_branch_name);
6697 818c7501 2019-07-11 stsp return NULL;
6698 818c7501 2019-07-11 stsp }
6699 818c7501 2019-07-11 stsp
6700 818c7501 2019-07-11 stsp static const struct got_error *
6701 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6702 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6703 818c7501 2019-07-11 stsp {
6704 0ebf8283 2019-07-24 stsp *logmsg = arg;
6705 818c7501 2019-07-11 stsp return NULL;
6706 818c7501 2019-07-11 stsp }
6707 818c7501 2019-07-11 stsp
6708 818c7501 2019-07-11 stsp static const struct got_error *
6709 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6710 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6711 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6712 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6713 818c7501 2019-07-11 stsp {
6714 818c7501 2019-07-11 stsp return NULL;
6715 01757395 2019-07-12 stsp }
6716 01757395 2019-07-12 stsp
6717 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6718 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6719 01757395 2019-07-12 stsp void *progress_arg;
6720 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6721 01757395 2019-07-12 stsp };
6722 01757395 2019-07-12 stsp
6723 01757395 2019-07-12 stsp static const struct got_error *
6724 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6725 01757395 2019-07-12 stsp {
6726 01757395 2019-07-12 stsp const struct got_error *err;
6727 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6728 01757395 2019-07-12 stsp char *p;
6729 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6730 01757395 2019-07-12 stsp
6731 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6732 01757395 2019-07-12 stsp if (err)
6733 01757395 2019-07-12 stsp return err;
6734 01757395 2019-07-12 stsp
6735 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6736 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6737 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6738 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6739 01757395 2019-07-12 stsp return NULL;
6740 01757395 2019-07-12 stsp
6741 01757395 2019-07-12 stsp p = strdup(path);
6742 01757395 2019-07-12 stsp if (p == NULL)
6743 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6744 01757395 2019-07-12 stsp
6745 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6746 01757395 2019-07-12 stsp if (err || new == NULL)
6747 01757395 2019-07-12 stsp free(p);
6748 01757395 2019-07-12 stsp return err;
6749 818c7501 2019-07-11 stsp }
6750 818c7501 2019-07-11 stsp
6751 0ebf8283 2019-07-24 stsp static const struct got_error *
6752 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6753 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6754 818c7501 2019-07-11 stsp {
6755 818c7501 2019-07-11 stsp const struct got_error *err;
6756 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6757 818c7501 2019-07-11 stsp
6758 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6759 818c7501 2019-07-11 stsp if (err) {
6760 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6761 818c7501 2019-07-11 stsp goto done;
6762 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6763 818c7501 2019-07-11 stsp if (err)
6764 818c7501 2019-07-11 stsp goto done;
6765 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6766 818c7501 2019-07-11 stsp if (err)
6767 818c7501 2019-07-11 stsp goto done;
6768 de05890f 2020-03-05 stsp } else if (is_rebase) {
6769 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6770 818c7501 2019-07-11 stsp int cmp;
6771 818c7501 2019-07-11 stsp
6772 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6773 818c7501 2019-07-11 stsp if (err)
6774 818c7501 2019-07-11 stsp goto done;
6775 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6776 818c7501 2019-07-11 stsp free(stored_id);
6777 818c7501 2019-07-11 stsp if (cmp != 0) {
6778 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6779 818c7501 2019-07-11 stsp goto done;
6780 818c7501 2019-07-11 stsp }
6781 818c7501 2019-07-11 stsp }
6782 0ebf8283 2019-07-24 stsp done:
6783 0ebf8283 2019-07-24 stsp if (commit_ref)
6784 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6785 0ebf8283 2019-07-24 stsp return err;
6786 0ebf8283 2019-07-24 stsp }
6787 0ebf8283 2019-07-24 stsp
6788 0ebf8283 2019-07-24 stsp static const struct got_error *
6789 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6790 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6791 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6792 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6793 3e3a69f1 2019-07-25 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 0ebf8283 2019-07-24 stsp {
6796 0ebf8283 2019-07-24 stsp const struct got_error *err;
6797 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6798 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6799 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6800 818c7501 2019-07-11 stsp
6801 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6802 0ebf8283 2019-07-24 stsp
6803 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6804 0ebf8283 2019-07-24 stsp if (err)
6805 0ebf8283 2019-07-24 stsp return err;
6806 0ebf8283 2019-07-24 stsp
6807 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6808 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6809 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6810 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6811 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6812 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6813 818c7501 2019-07-11 stsp if (commit_ref)
6814 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6815 818c7501 2019-07-11 stsp return err;
6816 818c7501 2019-07-11 stsp }
6817 818c7501 2019-07-11 stsp
6818 818c7501 2019-07-11 stsp const struct got_error *
6819 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6820 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6821 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6822 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6823 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6824 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6825 818c7501 2019-07-11 stsp {
6826 0ebf8283 2019-07-24 stsp const struct got_error *err;
6827 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6830 0ebf8283 2019-07-24 stsp if (err)
6831 0ebf8283 2019-07-24 stsp return err;
6832 0ebf8283 2019-07-24 stsp
6833 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6834 0ebf8283 2019-07-24 stsp if (err)
6835 0ebf8283 2019-07-24 stsp goto done;
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6838 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6839 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6840 0ebf8283 2019-07-24 stsp done:
6841 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6842 0ebf8283 2019-07-24 stsp return err;
6843 0ebf8283 2019-07-24 stsp }
6844 0ebf8283 2019-07-24 stsp
6845 0ebf8283 2019-07-24 stsp const struct got_error *
6846 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6847 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6848 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6849 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6850 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6851 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6852 0ebf8283 2019-07-24 stsp {
6853 0ebf8283 2019-07-24 stsp const struct got_error *err;
6854 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6855 0ebf8283 2019-07-24 stsp
6856 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6857 0ebf8283 2019-07-24 stsp if (err)
6858 0ebf8283 2019-07-24 stsp return err;
6859 0ebf8283 2019-07-24 stsp
6860 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6861 0ebf8283 2019-07-24 stsp if (err)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6865 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6866 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6867 0ebf8283 2019-07-24 stsp done:
6868 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6869 0ebf8283 2019-07-24 stsp return err;
6870 0ebf8283 2019-07-24 stsp }
6871 0ebf8283 2019-07-24 stsp
6872 0ebf8283 2019-07-24 stsp static const struct got_error *
6873 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6874 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6875 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6876 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6877 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6878 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6879 0ebf8283 2019-07-24 stsp {
6880 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6881 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6882 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6883 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6884 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6885 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6886 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6887 818c7501 2019-07-11 stsp
6888 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6889 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6890 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6891 a0e95631 2019-07-12 stsp
6892 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6893 818c7501 2019-07-11 stsp
6894 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6895 a0e95631 2019-07-12 stsp if (err)
6896 3e3a69f1 2019-07-25 stsp return err;
6897 a0e95631 2019-07-12 stsp
6898 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6899 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6900 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6901 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6902 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6903 01757395 2019-07-12 stsp /*
6904 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6905 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6906 6c13b005 2021-09-02 stsp *
6907 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6908 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6909 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6910 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6911 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6912 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6913 01757395 2019-07-12 stsp */
6914 01757395 2019-07-12 stsp if (merged_paths) {
6915 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6916 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6917 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6918 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6919 f2a9dc41 2019-12-13 tracey 0);
6920 01757395 2019-07-12 stsp if (err)
6921 01757395 2019-07-12 stsp goto done;
6922 01757395 2019-07-12 stsp }
6923 01757395 2019-07-12 stsp } else {
6924 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6925 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6926 01757395 2019-07-12 stsp if (err)
6927 01757395 2019-07-12 stsp goto done;
6928 01757395 2019-07-12 stsp }
6929 a0e95631 2019-07-12 stsp
6930 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6931 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6932 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6933 a0e95631 2019-07-12 stsp if (err)
6934 a0e95631 2019-07-12 stsp goto done;
6935 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6936 a0e95631 2019-07-12 stsp goto done;
6937 ff0d2220 2019-07-11 stsp }
6938 818c7501 2019-07-11 stsp
6939 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6940 edd02c5e 2019-07-11 stsp if (err)
6941 edd02c5e 2019-07-11 stsp goto done;
6942 edd02c5e 2019-07-11 stsp
6943 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6944 818c7501 2019-07-11 stsp if (err)
6945 818c7501 2019-07-11 stsp goto done;
6946 818c7501 2019-07-11 stsp
6947 5943eee2 2019-08-13 stsp if (new_logmsg) {
6948 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6949 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6950 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6951 5943eee2 2019-08-13 stsp goto done;
6952 5943eee2 2019-08-13 stsp }
6953 5943eee2 2019-08-13 stsp } else {
6954 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6955 5943eee2 2019-08-13 stsp if (err)
6956 5943eee2 2019-08-13 stsp goto done;
6957 5943eee2 2019-08-13 stsp }
6958 0ebf8283 2019-07-24 stsp
6959 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6960 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6961 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6962 8db00f97 2022-07-22 thomas committer ? committer :
6963 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6964 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6965 a0e95631 2019-07-12 stsp if (err)
6966 a0e95631 2019-07-12 stsp goto done;
6967 a0e95631 2019-07-12 stsp
6968 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6969 a0e95631 2019-07-12 stsp if (err)
6970 a0e95631 2019-07-12 stsp goto done;
6971 a0e95631 2019-07-12 stsp
6972 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6973 a0e95631 2019-07-12 stsp if (err)
6974 a0e95631 2019-07-12 stsp goto done;
6975 a0e95631 2019-07-12 stsp
6976 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6977 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6978 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6979 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6980 a0e95631 2019-07-12 stsp err = sync_err;
6981 818c7501 2019-07-11 stsp done:
6982 a0e95631 2019-07-12 stsp free(fileindex_path);
6983 a0e95631 2019-07-12 stsp free(head_commit_id);
6984 a0e95631 2019-07-12 stsp if (head_ref)
6985 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6986 818c7501 2019-07-11 stsp if (err) {
6987 818c7501 2019-07-11 stsp free(*new_commit_id);
6988 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6989 818c7501 2019-07-11 stsp }
6990 818c7501 2019-07-11 stsp return err;
6991 818c7501 2019-07-11 stsp }
6992 818c7501 2019-07-11 stsp
6993 818c7501 2019-07-11 stsp const struct got_error *
6994 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6995 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6996 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6997 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6998 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6999 be94c032 2023-02-20 thomas struct got_repository *repo)
7000 0ebf8283 2019-07-24 stsp {
7001 0ebf8283 2019-07-24 stsp const struct got_error *err;
7002 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7003 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7004 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7005 0ebf8283 2019-07-24 stsp
7006 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7007 0ebf8283 2019-07-24 stsp if (err)
7008 0ebf8283 2019-07-24 stsp return err;
7009 0ebf8283 2019-07-24 stsp
7010 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7011 0ebf8283 2019-07-24 stsp if (err)
7012 0ebf8283 2019-07-24 stsp goto done;
7013 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7014 0ebf8283 2019-07-24 stsp if (err)
7015 0ebf8283 2019-07-24 stsp goto done;
7016 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7017 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7018 0ebf8283 2019-07-24 stsp goto done;
7019 0ebf8283 2019-07-24 stsp }
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7022 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7023 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7024 0ebf8283 2019-07-24 stsp done:
7025 0ebf8283 2019-07-24 stsp if (commit_ref)
7026 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7027 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7028 0ebf8283 2019-07-24 stsp free(commit_id);
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 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7034 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7035 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7036 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7037 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7038 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7039 0ebf8283 2019-07-24 stsp {
7040 0ebf8283 2019-07-24 stsp const struct got_error *err;
7041 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7042 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7045 0ebf8283 2019-07-24 stsp if (err)
7046 0ebf8283 2019-07-24 stsp return err;
7047 0ebf8283 2019-07-24 stsp
7048 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7049 0ebf8283 2019-07-24 stsp if (err)
7050 0ebf8283 2019-07-24 stsp goto done;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7053 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7054 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7055 0ebf8283 2019-07-24 stsp done:
7056 0ebf8283 2019-07-24 stsp if (commit_ref)
7057 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7058 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7059 0ebf8283 2019-07-24 stsp return err;
7060 0ebf8283 2019-07-24 stsp }
7061 0ebf8283 2019-07-24 stsp
7062 0ebf8283 2019-07-24 stsp const struct got_error *
7063 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7064 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7065 818c7501 2019-07-11 stsp {
7066 3e3a69f1 2019-07-25 stsp if (fileindex)
7067 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7068 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7069 69844fba 2019-07-11 stsp }
7070 69844fba 2019-07-11 stsp
7071 69844fba 2019-07-11 stsp static const struct got_error *
7072 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7073 69844fba 2019-07-11 stsp {
7074 69844fba 2019-07-11 stsp const struct got_error *err;
7075 69844fba 2019-07-11 stsp struct got_reference *ref;
7076 69844fba 2019-07-11 stsp
7077 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7078 69844fba 2019-07-11 stsp if (err) {
7079 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7080 69844fba 2019-07-11 stsp return NULL;
7081 69844fba 2019-07-11 stsp return err;
7082 69844fba 2019-07-11 stsp }
7083 69844fba 2019-07-11 stsp
7084 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7085 69844fba 2019-07-11 stsp got_ref_close(ref);
7086 69844fba 2019-07-11 stsp return err;
7087 818c7501 2019-07-11 stsp }
7088 818c7501 2019-07-11 stsp
7089 69844fba 2019-07-11 stsp static const struct got_error *
7090 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7091 69844fba 2019-07-11 stsp {
7092 69844fba 2019-07-11 stsp const struct got_error *err;
7093 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7094 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7095 69844fba 2019-07-11 stsp
7096 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7097 69844fba 2019-07-11 stsp if (err)
7098 69844fba 2019-07-11 stsp goto done;
7099 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7100 69844fba 2019-07-11 stsp if (err)
7101 69844fba 2019-07-11 stsp goto done;
7102 69844fba 2019-07-11 stsp
7103 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7104 69844fba 2019-07-11 stsp if (err)
7105 69844fba 2019-07-11 stsp goto done;
7106 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7107 69844fba 2019-07-11 stsp if (err)
7108 69844fba 2019-07-11 stsp goto done;
7109 69844fba 2019-07-11 stsp
7110 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7111 69844fba 2019-07-11 stsp if (err)
7112 69844fba 2019-07-11 stsp goto done;
7113 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7114 69844fba 2019-07-11 stsp if (err)
7115 69844fba 2019-07-11 stsp goto done;
7116 69844fba 2019-07-11 stsp
7117 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7118 69844fba 2019-07-11 stsp if (err)
7119 69844fba 2019-07-11 stsp goto done;
7120 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7121 69844fba 2019-07-11 stsp if (err)
7122 69844fba 2019-07-11 stsp goto done;
7123 69844fba 2019-07-11 stsp
7124 69844fba 2019-07-11 stsp done:
7125 69844fba 2019-07-11 stsp free(tmp_branch_name);
7126 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7127 69844fba 2019-07-11 stsp free(branch_ref_name);
7128 69844fba 2019-07-11 stsp free(commit_ref_name);
7129 e600f124 2021-03-21 stsp return err;
7130 e600f124 2021-03-21 stsp }
7131 e600f124 2021-03-21 stsp
7132 ef20f542 2022-06-26 thomas static const struct got_error *
7133 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7134 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7135 e600f124 2021-03-21 stsp {
7136 e600f124 2021-03-21 stsp const struct got_error *err;
7137 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7138 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7139 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7140 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7141 e600f124 2021-03-21 stsp char *refname = NULL;
7142 e600f124 2021-03-21 stsp
7143 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7144 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7145 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7146 e600f124 2021-03-21 stsp branch_name += 11;
7147 e600f124 2021-03-21 stsp
7148 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7149 e600f124 2021-03-21 stsp if (err)
7150 e600f124 2021-03-21 stsp return err;
7151 e600f124 2021-03-21 stsp
7152 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7153 e600f124 2021-03-21 stsp new_id_str) == -1) {
7154 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7155 e600f124 2021-03-21 stsp goto done;
7156 e600f124 2021-03-21 stsp }
7157 e600f124 2021-03-21 stsp
7158 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7159 e600f124 2021-03-21 stsp if (err)
7160 e600f124 2021-03-21 stsp goto done;
7161 e600f124 2021-03-21 stsp
7162 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7163 e600f124 2021-03-21 stsp if (err)
7164 e600f124 2021-03-21 stsp goto done;
7165 e600f124 2021-03-21 stsp
7166 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7167 e600f124 2021-03-21 stsp done:
7168 e600f124 2021-03-21 stsp free(new_id_str);
7169 e600f124 2021-03-21 stsp free(refname);
7170 e600f124 2021-03-21 stsp free(old_commit_id);
7171 e600f124 2021-03-21 stsp if (ref)
7172 e600f124 2021-03-21 stsp got_ref_close(ref);
7173 69844fba 2019-07-11 stsp return err;
7174 69844fba 2019-07-11 stsp }
7175 69844fba 2019-07-11 stsp
7176 818c7501 2019-07-11 stsp const struct got_error *
7177 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7178 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7179 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7180 f08f28be 2023-02-03 thomas int create_backup)
7181 818c7501 2019-07-11 stsp {
7182 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7183 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7184 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7185 818c7501 2019-07-11 stsp
7186 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7187 818c7501 2019-07-11 stsp if (err)
7188 818c7501 2019-07-11 stsp return err;
7189 e600f124 2021-03-21 stsp
7190 e600f124 2021-03-21 stsp if (create_backup) {
7191 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7192 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7193 e600f124 2021-03-21 stsp if (err)
7194 e600f124 2021-03-21 stsp goto done;
7195 e600f124 2021-03-21 stsp }
7196 818c7501 2019-07-11 stsp
7197 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7198 818c7501 2019-07-11 stsp if (err)
7199 818c7501 2019-07-11 stsp goto done;
7200 818c7501 2019-07-11 stsp
7201 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7202 818c7501 2019-07-11 stsp if (err)
7203 818c7501 2019-07-11 stsp goto done;
7204 818c7501 2019-07-11 stsp
7205 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7206 818c7501 2019-07-11 stsp if (err)
7207 818c7501 2019-07-11 stsp goto done;
7208 818c7501 2019-07-11 stsp
7209 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7210 a615e0e7 2020-12-16 stsp if (err)
7211 a615e0e7 2020-12-16 stsp goto done;
7212 a615e0e7 2020-12-16 stsp
7213 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7214 a615e0e7 2020-12-16 stsp if (err)
7215 a615e0e7 2020-12-16 stsp goto done;
7216 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7217 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7218 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7219 a615e0e7 2020-12-16 stsp err = sync_err;
7220 818c7501 2019-07-11 stsp done:
7221 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7222 a615e0e7 2020-12-16 stsp free(fileindex_path);
7223 818c7501 2019-07-11 stsp free(new_head_commit_id);
7224 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7225 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7226 818c7501 2019-07-11 stsp err = unlockerr;
7227 818c7501 2019-07-11 stsp return err;
7228 818c7501 2019-07-11 stsp }
7229 818c7501 2019-07-11 stsp
7230 818c7501 2019-07-11 stsp const struct got_error *
7231 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7232 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7233 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7234 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7235 818c7501 2019-07-11 stsp {
7236 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7237 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7238 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7239 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7240 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7241 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7242 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7243 818c7501 2019-07-11 stsp
7244 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7245 818c7501 2019-07-11 stsp if (err)
7246 818c7501 2019-07-11 stsp return err;
7247 818c7501 2019-07-11 stsp
7248 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7249 945f9229 2022-04-16 thomas worktree->base_commit_id);
7250 945f9229 2022-04-16 thomas if (err)
7251 945f9229 2022-04-16 thomas goto done;
7252 945f9229 2022-04-16 thomas
7253 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7254 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7255 818c7501 2019-07-11 stsp if (err)
7256 818c7501 2019-07-11 stsp goto done;
7257 818c7501 2019-07-11 stsp
7258 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7259 818c7501 2019-07-11 stsp if (err)
7260 818c7501 2019-07-11 stsp goto done;
7261 818c7501 2019-07-11 stsp
7262 818c7501 2019-07-11 stsp /*
7263 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7264 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7265 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7266 818c7501 2019-07-11 stsp */
7267 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7268 818c7501 2019-07-11 stsp if (err)
7269 818c7501 2019-07-11 stsp goto done;
7270 818c7501 2019-07-11 stsp
7271 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7272 818c7501 2019-07-11 stsp if (err)
7273 818c7501 2019-07-11 stsp goto done;
7274 818c7501 2019-07-11 stsp
7275 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7276 945f9229 2022-04-16 thomas worktree->path_prefix);
7277 ca355955 2019-07-12 stsp if (err)
7278 ca355955 2019-07-12 stsp goto done;
7279 ca355955 2019-07-12 stsp
7280 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7281 ca355955 2019-07-12 stsp if (err)
7282 ca355955 2019-07-12 stsp goto done;
7283 ca355955 2019-07-12 stsp
7284 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7285 818c7501 2019-07-11 stsp if (err)
7286 818c7501 2019-07-11 stsp goto done;
7287 818c7501 2019-07-11 stsp
7288 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7289 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7290 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7291 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7292 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7293 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7294 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7295 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7296 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7297 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7298 55bd499d 2019-07-12 stsp if (err)
7299 1f1abb7e 2019-08-08 stsp goto sync;
7300 55bd499d 2019-07-12 stsp
7301 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7302 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7303 ca355955 2019-07-12 stsp sync:
7304 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7305 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7306 ca355955 2019-07-12 stsp err = sync_err;
7307 818c7501 2019-07-11 stsp done:
7308 818c7501 2019-07-11 stsp got_ref_close(resolved);
7309 a3a2faf2 2019-07-12 stsp free(tree_id);
7310 818c7501 2019-07-11 stsp free(commit_id);
7311 945f9229 2022-04-16 thomas if (commit)
7312 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7313 0ebf8283 2019-07-24 stsp if (fileindex)
7314 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7315 0ebf8283 2019-07-24 stsp free(fileindex_path);
7316 0ebf8283 2019-07-24 stsp
7317 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7318 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7319 0ebf8283 2019-07-24 stsp err = unlockerr;
7320 0ebf8283 2019-07-24 stsp return err;
7321 0ebf8283 2019-07-24 stsp }
7322 0ebf8283 2019-07-24 stsp
7323 0ebf8283 2019-07-24 stsp const struct got_error *
7324 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7325 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7326 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7327 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7328 0ebf8283 2019-07-24 stsp {
7329 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7330 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7331 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7332 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7333 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7334 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7335 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7336 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7337 0ebf8283 2019-07-24 stsp
7338 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7339 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7340 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7341 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7342 0ebf8283 2019-07-24 stsp
7343 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7344 0ebf8283 2019-07-24 stsp if (err)
7345 0ebf8283 2019-07-24 stsp return err;
7346 0ebf8283 2019-07-24 stsp
7347 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7348 0ebf8283 2019-07-24 stsp if (err)
7349 0ebf8283 2019-07-24 stsp goto done;
7350 0ebf8283 2019-07-24 stsp
7351 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7352 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7353 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7354 0ebf8283 2019-07-24 stsp &ok_arg);
7355 0ebf8283 2019-07-24 stsp if (err)
7356 0ebf8283 2019-07-24 stsp goto done;
7357 0ebf8283 2019-07-24 stsp
7358 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7359 0ebf8283 2019-07-24 stsp if (err)
7360 0ebf8283 2019-07-24 stsp goto done;
7361 0ebf8283 2019-07-24 stsp
7362 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7363 0ebf8283 2019-07-24 stsp if (err)
7364 0ebf8283 2019-07-24 stsp goto done;
7365 0ebf8283 2019-07-24 stsp
7366 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7367 0ebf8283 2019-07-24 stsp worktree);
7368 0ebf8283 2019-07-24 stsp if (err)
7369 0ebf8283 2019-07-24 stsp goto done;
7370 0ebf8283 2019-07-24 stsp
7371 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7372 0ebf8283 2019-07-24 stsp 0);
7373 0ebf8283 2019-07-24 stsp if (err)
7374 0ebf8283 2019-07-24 stsp goto done;
7375 0ebf8283 2019-07-24 stsp
7376 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7377 0ebf8283 2019-07-24 stsp if (err)
7378 0ebf8283 2019-07-24 stsp goto done;
7379 0ebf8283 2019-07-24 stsp
7380 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7381 0ebf8283 2019-07-24 stsp if (err)
7382 0ebf8283 2019-07-24 stsp goto done;
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7385 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7386 0ebf8283 2019-07-24 stsp if (err)
7387 0ebf8283 2019-07-24 stsp goto done;
7388 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7389 0ebf8283 2019-07-24 stsp if (err)
7390 0ebf8283 2019-07-24 stsp goto done;
7391 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7392 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7393 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7394 0ebf8283 2019-07-24 stsp goto done;
7395 0ebf8283 2019-07-24 stsp }
7396 0ebf8283 2019-07-24 stsp
7397 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7398 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7399 0ebf8283 2019-07-24 stsp if (err)
7400 0ebf8283 2019-07-24 stsp goto done;
7401 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7402 0ebf8283 2019-07-24 stsp if (err)
7403 0ebf8283 2019-07-24 stsp goto done;
7404 0ebf8283 2019-07-24 stsp
7405 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7406 0ebf8283 2019-07-24 stsp if (err)
7407 0ebf8283 2019-07-24 stsp goto done;
7408 0ebf8283 2019-07-24 stsp done:
7409 0ebf8283 2019-07-24 stsp free(fileindex_path);
7410 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7411 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7412 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7413 0ebf8283 2019-07-24 stsp if (wt_branch)
7414 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7415 0ebf8283 2019-07-24 stsp if (err) {
7416 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7417 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7418 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7419 0ebf8283 2019-07-24 stsp }
7420 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7421 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7422 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7423 0ebf8283 2019-07-24 stsp }
7424 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7425 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7426 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7427 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7428 3e3a69f1 2019-07-25 stsp }
7429 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7430 0ebf8283 2019-07-24 stsp }
7431 0ebf8283 2019-07-24 stsp return err;
7432 0ebf8283 2019-07-24 stsp }
7433 0ebf8283 2019-07-24 stsp
7434 0ebf8283 2019-07-24 stsp const struct got_error *
7435 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7436 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7437 0ebf8283 2019-07-24 stsp {
7438 3e3a69f1 2019-07-25 stsp if (fileindex)
7439 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7440 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7441 0ebf8283 2019-07-24 stsp }
7442 0ebf8283 2019-07-24 stsp
7443 0ebf8283 2019-07-24 stsp const struct got_error *
7444 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7445 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7446 0ebf8283 2019-07-24 stsp {
7447 0ebf8283 2019-07-24 stsp const struct got_error *err;
7448 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7449 0ebf8283 2019-07-24 stsp
7450 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7451 0ebf8283 2019-07-24 stsp if (err)
7452 0ebf8283 2019-07-24 stsp return err;
7453 0ebf8283 2019-07-24 stsp
7454 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7455 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7456 0ebf8283 2019-07-24 stsp return NULL;
7457 0ebf8283 2019-07-24 stsp }
7458 0ebf8283 2019-07-24 stsp
7459 0ebf8283 2019-07-24 stsp const struct got_error *
7460 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7461 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7462 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7463 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7464 0ebf8283 2019-07-24 stsp {
7465 0ebf8283 2019-07-24 stsp const struct got_error *err;
7466 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7467 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7468 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7469 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7470 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7471 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7472 0ebf8283 2019-07-24 stsp
7473 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7474 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7475 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7476 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7477 0ebf8283 2019-07-24 stsp
7478 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7479 3e3a69f1 2019-07-25 stsp if (err)
7480 3e3a69f1 2019-07-25 stsp return err;
7481 3e3a69f1 2019-07-25 stsp
7482 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7483 3e3a69f1 2019-07-25 stsp if (err)
7484 f032f1f7 2019-08-04 stsp goto done;
7485 f032f1f7 2019-08-04 stsp
7486 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7487 f032f1f7 2019-08-04 stsp &have_staged_files);
7488 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7489 f032f1f7 2019-08-04 stsp goto done;
7490 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7491 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7492 3e3a69f1 2019-07-25 stsp goto done;
7493 f032f1f7 2019-08-04 stsp }
7494 3e3a69f1 2019-07-25 stsp
7495 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7496 0ebf8283 2019-07-24 stsp if (err)
7497 f032f1f7 2019-08-04 stsp goto done;
7498 0ebf8283 2019-07-24 stsp
7499 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7500 0ebf8283 2019-07-24 stsp if (err)
7501 0ebf8283 2019-07-24 stsp goto done;
7502 0ebf8283 2019-07-24 stsp
7503 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7504 0ebf8283 2019-07-24 stsp if (err)
7505 0ebf8283 2019-07-24 stsp goto done;
7506 0ebf8283 2019-07-24 stsp
7507 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7508 0ebf8283 2019-07-24 stsp worktree);
7509 0ebf8283 2019-07-24 stsp if (err)
7510 0ebf8283 2019-07-24 stsp goto done;
7511 0ebf8283 2019-07-24 stsp
7512 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7513 0ebf8283 2019-07-24 stsp if (err)
7514 0ebf8283 2019-07-24 stsp goto done;
7515 0ebf8283 2019-07-24 stsp
7516 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7517 0ebf8283 2019-07-24 stsp if (err)
7518 0ebf8283 2019-07-24 stsp goto done;
7519 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7520 0ebf8283 2019-07-24 stsp if (err)
7521 0ebf8283 2019-07-24 stsp goto done;
7522 0ebf8283 2019-07-24 stsp
7523 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7524 0ebf8283 2019-07-24 stsp if (err)
7525 0ebf8283 2019-07-24 stsp goto done;
7526 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7527 0ebf8283 2019-07-24 stsp if (err)
7528 0ebf8283 2019-07-24 stsp goto done;
7529 0ebf8283 2019-07-24 stsp
7530 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7531 0ebf8283 2019-07-24 stsp if (err)
7532 0ebf8283 2019-07-24 stsp goto done;
7533 0ebf8283 2019-07-24 stsp done:
7534 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7535 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7536 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7537 0ebf8283 2019-07-24 stsp if (commit_ref)
7538 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7539 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7540 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7541 0ebf8283 2019-07-24 stsp if (err) {
7542 0ebf8283 2019-07-24 stsp free(*commit_id);
7543 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7544 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7545 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7546 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7547 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7548 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7549 0ebf8283 2019-07-24 stsp }
7550 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7551 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7552 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7553 3e3a69f1 2019-07-25 stsp }
7554 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7555 0ebf8283 2019-07-24 stsp }
7556 0ebf8283 2019-07-24 stsp return err;
7557 0ebf8283 2019-07-24 stsp }
7558 0ebf8283 2019-07-24 stsp
7559 0ebf8283 2019-07-24 stsp static const struct got_error *
7560 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7561 0ebf8283 2019-07-24 stsp {
7562 0ebf8283 2019-07-24 stsp const struct got_error *err;
7563 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7564 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7565 0ebf8283 2019-07-24 stsp
7566 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7567 0ebf8283 2019-07-24 stsp if (err)
7568 0ebf8283 2019-07-24 stsp goto done;
7569 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7570 0ebf8283 2019-07-24 stsp if (err)
7571 0ebf8283 2019-07-24 stsp goto done;
7572 0ebf8283 2019-07-24 stsp
7573 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7574 0ebf8283 2019-07-24 stsp worktree);
7575 0ebf8283 2019-07-24 stsp if (err)
7576 0ebf8283 2019-07-24 stsp goto done;
7577 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7578 0ebf8283 2019-07-24 stsp if (err)
7579 0ebf8283 2019-07-24 stsp goto done;
7580 0ebf8283 2019-07-24 stsp
7581 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7582 0ebf8283 2019-07-24 stsp if (err)
7583 0ebf8283 2019-07-24 stsp goto done;
7584 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7585 0ebf8283 2019-07-24 stsp if (err)
7586 0ebf8283 2019-07-24 stsp goto done;
7587 0ebf8283 2019-07-24 stsp
7588 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7589 0ebf8283 2019-07-24 stsp if (err)
7590 0ebf8283 2019-07-24 stsp goto done;
7591 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7592 0ebf8283 2019-07-24 stsp if (err)
7593 0ebf8283 2019-07-24 stsp goto done;
7594 0ebf8283 2019-07-24 stsp done:
7595 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7596 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7597 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7598 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7599 0ebf8283 2019-07-24 stsp return err;
7600 0ebf8283 2019-07-24 stsp }
7601 0ebf8283 2019-07-24 stsp
7602 0ebf8283 2019-07-24 stsp const struct got_error *
7603 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7604 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7605 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7606 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7607 0ebf8283 2019-07-24 stsp {
7608 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7609 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7610 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7611 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7612 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7613 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7614 0ebf8283 2019-07-24 stsp
7615 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7616 0ebf8283 2019-07-24 stsp if (err)
7617 0ebf8283 2019-07-24 stsp return err;
7618 0ebf8283 2019-07-24 stsp
7619 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7620 945f9229 2022-04-16 thomas worktree->base_commit_id);
7621 945f9229 2022-04-16 thomas if (err)
7622 945f9229 2022-04-16 thomas goto done;
7623 945f9229 2022-04-16 thomas
7624 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7625 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7626 0ebf8283 2019-07-24 stsp if (err)
7627 0ebf8283 2019-07-24 stsp goto done;
7628 0ebf8283 2019-07-24 stsp
7629 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7630 0ebf8283 2019-07-24 stsp if (err)
7631 0ebf8283 2019-07-24 stsp goto done;
7632 0ebf8283 2019-07-24 stsp
7633 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7634 0ebf8283 2019-07-24 stsp if (err)
7635 0ebf8283 2019-07-24 stsp goto done;
7636 0ebf8283 2019-07-24 stsp
7637 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7638 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7639 0ebf8283 2019-07-24 stsp if (err)
7640 0ebf8283 2019-07-24 stsp goto done;
7641 0ebf8283 2019-07-24 stsp
7642 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7643 0ebf8283 2019-07-24 stsp if (err)
7644 0ebf8283 2019-07-24 stsp goto done;
7645 0ebf8283 2019-07-24 stsp
7646 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7647 0ebf8283 2019-07-24 stsp if (err)
7648 0ebf8283 2019-07-24 stsp goto done;
7649 0ebf8283 2019-07-24 stsp
7650 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7651 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7652 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7653 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7654 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7655 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7656 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7657 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7658 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7659 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7660 0ebf8283 2019-07-24 stsp if (err)
7661 1f1abb7e 2019-08-08 stsp goto sync;
7662 0ebf8283 2019-07-24 stsp
7663 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7664 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7665 0ebf8283 2019-07-24 stsp sync:
7666 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7667 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7668 0ebf8283 2019-07-24 stsp err = sync_err;
7669 0ebf8283 2019-07-24 stsp done:
7670 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7671 0ebf8283 2019-07-24 stsp free(tree_id);
7672 818c7501 2019-07-11 stsp free(fileindex_path);
7673 818c7501 2019-07-11 stsp
7674 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7675 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7676 818c7501 2019-07-11 stsp err = unlockerr;
7677 818c7501 2019-07-11 stsp return err;
7678 818c7501 2019-07-11 stsp }
7679 0ebf8283 2019-07-24 stsp
7680 0ebf8283 2019-07-24 stsp const struct got_error *
7681 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7682 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7683 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7684 0ebf8283 2019-07-24 stsp {
7685 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7686 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7687 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7688 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7689 0ebf8283 2019-07-24 stsp
7690 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7691 0ebf8283 2019-07-24 stsp if (err)
7692 0ebf8283 2019-07-24 stsp return err;
7693 0ebf8283 2019-07-24 stsp
7694 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7695 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7696 e600f124 2021-03-21 stsp if (err)
7697 e600f124 2021-03-21 stsp goto done;
7698 e600f124 2021-03-21 stsp
7699 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7700 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7701 0ebf8283 2019-07-24 stsp if (err)
7702 0ebf8283 2019-07-24 stsp goto done;
7703 0ebf8283 2019-07-24 stsp
7704 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7705 0ebf8283 2019-07-24 stsp if (err)
7706 0ebf8283 2019-07-24 stsp goto done;
7707 0ebf8283 2019-07-24 stsp
7708 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7709 0ebf8283 2019-07-24 stsp if (err)
7710 0ebf8283 2019-07-24 stsp goto done;
7711 0ebf8283 2019-07-24 stsp
7712 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7713 0ebf8283 2019-07-24 stsp if (err)
7714 0ebf8283 2019-07-24 stsp goto done;
7715 0ebf8283 2019-07-24 stsp
7716 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7717 a615e0e7 2020-12-16 stsp if (err)
7718 a615e0e7 2020-12-16 stsp goto done;
7719 a615e0e7 2020-12-16 stsp
7720 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7721 a615e0e7 2020-12-16 stsp if (err)
7722 a615e0e7 2020-12-16 stsp goto done;
7723 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7724 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7725 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7726 a615e0e7 2020-12-16 stsp err = sync_err;
7727 0ebf8283 2019-07-24 stsp done:
7728 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7729 a615e0e7 2020-12-16 stsp free(fileindex_path);
7730 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7731 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7732 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7733 0ebf8283 2019-07-24 stsp err = unlockerr;
7734 0ebf8283 2019-07-24 stsp return err;
7735 0ebf8283 2019-07-24 stsp }
7736 0ebf8283 2019-07-24 stsp
7737 0ebf8283 2019-07-24 stsp const struct got_error *
7738 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7739 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7740 0ebf8283 2019-07-24 stsp {
7741 0ebf8283 2019-07-24 stsp const struct got_error *err;
7742 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7743 0ebf8283 2019-07-24 stsp
7744 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7745 0ebf8283 2019-07-24 stsp if (err)
7746 0ebf8283 2019-07-24 stsp return err;
7747 0ebf8283 2019-07-24 stsp
7748 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7749 0ebf8283 2019-07-24 stsp if (err)
7750 0ebf8283 2019-07-24 stsp goto done;
7751 0ebf8283 2019-07-24 stsp
7752 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7753 0ebf8283 2019-07-24 stsp done:
7754 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7755 2822a352 2019-10-15 stsp return err;
7756 2822a352 2019-10-15 stsp }
7757 2822a352 2019-10-15 stsp
7758 2822a352 2019-10-15 stsp const struct got_error *
7759 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7760 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7761 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7762 2822a352 2019-10-15 stsp struct got_repository *repo)
7763 2822a352 2019-10-15 stsp {
7764 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7765 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7766 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7767 2822a352 2019-10-15 stsp
7768 2822a352 2019-10-15 stsp *fileindex = NULL;
7769 2822a352 2019-10-15 stsp *branch_ref = NULL;
7770 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7771 2822a352 2019-10-15 stsp
7772 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7773 2822a352 2019-10-15 stsp if (err)
7774 2822a352 2019-10-15 stsp return err;
7775 2822a352 2019-10-15 stsp
7776 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7777 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7778 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7779 2822a352 2019-10-15 stsp "update -b or different branch name required");
7780 2822a352 2019-10-15 stsp goto done;
7781 2822a352 2019-10-15 stsp }
7782 2822a352 2019-10-15 stsp
7783 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7784 2822a352 2019-10-15 stsp if (err)
7785 2822a352 2019-10-15 stsp goto done;
7786 2822a352 2019-10-15 stsp
7787 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7788 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7789 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7790 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7791 2822a352 2019-10-15 stsp &ok_arg);
7792 2822a352 2019-10-15 stsp if (err)
7793 2822a352 2019-10-15 stsp goto done;
7794 2822a352 2019-10-15 stsp
7795 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7796 2822a352 2019-10-15 stsp if (err)
7797 2822a352 2019-10-15 stsp goto done;
7798 2822a352 2019-10-15 stsp
7799 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7800 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7801 2822a352 2019-10-15 stsp done:
7802 2822a352 2019-10-15 stsp if (err) {
7803 2822a352 2019-10-15 stsp if (*branch_ref) {
7804 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7805 2822a352 2019-10-15 stsp *branch_ref = NULL;
7806 2822a352 2019-10-15 stsp }
7807 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7808 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7809 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7810 2822a352 2019-10-15 stsp }
7811 2822a352 2019-10-15 stsp if (*fileindex) {
7812 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7813 2822a352 2019-10-15 stsp *fileindex = NULL;
7814 2822a352 2019-10-15 stsp }
7815 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7816 2822a352 2019-10-15 stsp }
7817 0cb83759 2019-08-03 stsp return err;
7818 0cb83759 2019-08-03 stsp }
7819 0cb83759 2019-08-03 stsp
7820 2822a352 2019-10-15 stsp const struct got_error *
7821 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7822 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7823 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7824 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7825 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7826 2822a352 2019-10-15 stsp {
7827 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7828 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7829 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7830 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7831 2822a352 2019-10-15 stsp
7832 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7833 2822a352 2019-10-15 stsp if (err)
7834 2822a352 2019-10-15 stsp goto done;
7835 2822a352 2019-10-15 stsp
7836 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7837 2822a352 2019-10-15 stsp if (err)
7838 2822a352 2019-10-15 stsp goto done;
7839 2822a352 2019-10-15 stsp
7840 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7841 945f9229 2022-04-16 thomas if (err)
7842 945f9229 2022-04-16 thomas goto done;
7843 945f9229 2022-04-16 thomas
7844 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7845 2822a352 2019-10-15 stsp worktree->path_prefix);
7846 2822a352 2019-10-15 stsp if (err)
7847 2822a352 2019-10-15 stsp goto done;
7848 2822a352 2019-10-15 stsp
7849 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7850 2822a352 2019-10-15 stsp if (err)
7851 2822a352 2019-10-15 stsp goto done;
7852 2822a352 2019-10-15 stsp
7853 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7854 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7855 2822a352 2019-10-15 stsp if (err)
7856 2822a352 2019-10-15 stsp goto sync;
7857 2822a352 2019-10-15 stsp
7858 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7859 2822a352 2019-10-15 stsp if (err)
7860 2822a352 2019-10-15 stsp goto sync;
7861 2822a352 2019-10-15 stsp
7862 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7863 6e210706 2021-01-22 stsp if (err)
7864 6e210706 2021-01-22 stsp goto sync;
7865 6e210706 2021-01-22 stsp
7866 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7867 2822a352 2019-10-15 stsp sync:
7868 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7869 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7870 2822a352 2019-10-15 stsp err = sync_err;
7871 2822a352 2019-10-15 stsp
7872 2822a352 2019-10-15 stsp done:
7873 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7874 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7875 2822a352 2019-10-15 stsp err = unlockerr;
7876 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7877 2822a352 2019-10-15 stsp
7878 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7879 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7880 2822a352 2019-10-15 stsp err = unlockerr;
7881 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7882 2822a352 2019-10-15 stsp
7883 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7884 2822a352 2019-10-15 stsp free(fileindex_path);
7885 2822a352 2019-10-15 stsp free(tree_id);
7886 945f9229 2022-04-16 thomas if (commit)
7887 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7888 2822a352 2019-10-15 stsp
7889 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7890 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7891 2822a352 2019-10-15 stsp err = unlockerr;
7892 2822a352 2019-10-15 stsp return err;
7893 2822a352 2019-10-15 stsp }
7894 2822a352 2019-10-15 stsp
7895 2822a352 2019-10-15 stsp const struct got_error *
7896 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7897 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7898 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7899 2822a352 2019-10-15 stsp {
7900 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7901 8b692cd0 2019-10-21 stsp
7902 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7903 8b692cd0 2019-10-21 stsp
7904 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7905 8b692cd0 2019-10-21 stsp
7906 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7907 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7908 8b692cd0 2019-10-21 stsp err = unlockerr;
7909 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7910 8b692cd0 2019-10-21 stsp
7911 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7912 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7913 8b692cd0 2019-10-21 stsp err = unlockerr;
7914 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7915 10604dce 2021-09-24 thomas
7916 10604dce 2021-09-24 thomas return err;
7917 10604dce 2021-09-24 thomas }
7918 10604dce 2021-09-24 thomas
7919 10604dce 2021-09-24 thomas const struct got_error *
7920 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7921 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7922 10604dce 2021-09-24 thomas {
7923 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7924 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7925 10604dce 2021-09-24 thomas
7926 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7927 10604dce 2021-09-24 thomas if (err)
7928 10604dce 2021-09-24 thomas goto done;
7929 8b692cd0 2019-10-21 stsp
7930 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7931 10604dce 2021-09-24 thomas
7932 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7933 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7934 10604dce 2021-09-24 thomas err = sync_err;
7935 10604dce 2021-09-24 thomas done:
7936 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7937 10604dce 2021-09-24 thomas free(fileindex_path);
7938 8b692cd0 2019-10-21 stsp return err;
7939 2822a352 2019-10-15 stsp }
7940 2822a352 2019-10-15 stsp
7941 10604dce 2021-09-24 thomas static const struct got_error *
7942 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7943 10604dce 2021-09-24 thomas {
7944 10604dce 2021-09-24 thomas const struct got_error *err;
7945 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7946 10604dce 2021-09-24 thomas
7947 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7948 10604dce 2021-09-24 thomas if (err)
7949 10604dce 2021-09-24 thomas goto done;
7950 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7951 10604dce 2021-09-24 thomas if (err)
7952 10604dce 2021-09-24 thomas goto done;
7953 10604dce 2021-09-24 thomas
7954 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7955 10604dce 2021-09-24 thomas if (err)
7956 10604dce 2021-09-24 thomas goto done;
7957 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7958 10604dce 2021-09-24 thomas if (err)
7959 10604dce 2021-09-24 thomas goto done;
7960 10604dce 2021-09-24 thomas
7961 10604dce 2021-09-24 thomas done:
7962 10604dce 2021-09-24 thomas free(branch_refname);
7963 10604dce 2021-09-24 thomas free(commit_refname);
7964 10604dce 2021-09-24 thomas return err;
7965 10604dce 2021-09-24 thomas }
7966 10604dce 2021-09-24 thomas
7967 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7968 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7969 10604dce 2021-09-24 thomas const char *branch_name;
7970 10604dce 2021-09-24 thomas };
7971 10604dce 2021-09-24 thomas
7972 10604dce 2021-09-24 thomas static const struct got_error *
7973 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7974 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7975 10604dce 2021-09-24 thomas {
7976 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7977 10604dce 2021-09-24 thomas
7978 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7979 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7980 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7981 10604dce 2021-09-24 thomas
7982 10604dce 2021-09-24 thomas return NULL;
7983 10604dce 2021-09-24 thomas }
7984 10604dce 2021-09-24 thomas
7985 10604dce 2021-09-24 thomas
7986 10604dce 2021-09-24 thomas const struct got_error *
7987 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7988 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7989 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7990 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7991 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7992 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7993 10604dce 2021-09-24 thomas {
7994 10604dce 2021-09-24 thomas const struct got_error *err;
7995 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7996 10604dce 2021-09-24 thomas
7997 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7998 10604dce 2021-09-24 thomas if (err)
7999 10604dce 2021-09-24 thomas goto done;
8000 10604dce 2021-09-24 thomas
8001 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8002 10604dce 2021-09-24 thomas worktree);
8003 10604dce 2021-09-24 thomas if (err)
8004 10604dce 2021-09-24 thomas goto done;
8005 10604dce 2021-09-24 thomas
8006 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8007 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8008 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8009 10604dce 2021-09-24 thomas done:
8010 10604dce 2021-09-24 thomas free(fileindex_path);
8011 10604dce 2021-09-24 thomas return err;
8012 10604dce 2021-09-24 thomas }
8013 10604dce 2021-09-24 thomas
8014 10604dce 2021-09-24 thomas const struct got_error *
8015 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8016 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8017 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8018 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8019 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8020 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8021 ae1e948a 2021-09-28 thomas
8022 10604dce 2021-09-24 thomas {
8023 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8024 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8025 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8026 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8027 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8028 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8029 10604dce 2021-09-24 thomas int have_staged_files = 0;
8030 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8031 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8032 10604dce 2021-09-24 thomas
8033 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8034 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8035 10604dce 2021-09-24 thomas
8036 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8037 10604dce 2021-09-24 thomas
8038 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8039 10604dce 2021-09-24 thomas if (err)
8040 10604dce 2021-09-24 thomas goto done;
8041 10604dce 2021-09-24 thomas
8042 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8043 10604dce 2021-09-24 thomas if (err)
8044 10604dce 2021-09-24 thomas goto done;
8045 10604dce 2021-09-24 thomas
8046 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8047 10604dce 2021-09-24 thomas if (err)
8048 10604dce 2021-09-24 thomas goto done;
8049 10604dce 2021-09-24 thomas
8050 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8051 10604dce 2021-09-24 thomas &have_staged_files);
8052 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8053 10604dce 2021-09-24 thomas goto done;
8054 10604dce 2021-09-24 thomas if (have_staged_files) {
8055 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8056 10604dce 2021-09-24 thomas goto done;
8057 10604dce 2021-09-24 thomas }
8058 10604dce 2021-09-24 thomas
8059 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8060 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8061 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8062 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8063 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8064 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8065 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8066 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8067 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8068 10604dce 2021-09-24 thomas if (err)
8069 10604dce 2021-09-24 thomas goto done;
8070 10604dce 2021-09-24 thomas
8071 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
8072 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8073 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
8074 10604dce 2021-09-24 thomas goto done;
8075 10604dce 2021-09-24 thomas }
8076 10604dce 2021-09-24 thomas
8077 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8078 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8079 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
8080 10604dce 2021-09-24 thomas
8081 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
8082 10604dce 2021-09-24 thomas ct_path++;
8083 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
8084 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8085 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8086 10604dce 2021-09-24 thomas if (err)
8087 10604dce 2021-09-24 thomas goto done;
8088 10604dce 2021-09-24 thomas
8089 10604dce 2021-09-24 thomas }
8090 10604dce 2021-09-24 thomas
8091 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8092 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8093 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8094 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8095 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8096 10604dce 2021-09-24 thomas if (err)
8097 10604dce 2021-09-24 thomas goto done;
8098 10604dce 2021-09-24 thomas
8099 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8100 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8101 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8102 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8103 10604dce 2021-09-24 thomas err = sync_err;
8104 10604dce 2021-09-24 thomas done:
8105 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8106 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8107 21c2d8be 2023-01-10 thomas
8108 10604dce 2021-09-24 thomas free_commitable(ct);
8109 10604dce 2021-09-24 thomas }
8110 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8111 10604dce 2021-09-24 thomas free(fileindex_path);
8112 10604dce 2021-09-24 thomas return err;
8113 10604dce 2021-09-24 thomas }
8114 10604dce 2021-09-24 thomas
8115 10604dce 2021-09-24 thomas const struct got_error *
8116 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8117 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8118 10604dce 2021-09-24 thomas {
8119 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8120 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8121 10604dce 2021-09-24 thomas
8122 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8123 10604dce 2021-09-24 thomas if (err)
8124 10604dce 2021-09-24 thomas goto done;
8125 10604dce 2021-09-24 thomas
8126 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8127 10604dce 2021-09-24 thomas if (err)
8128 10604dce 2021-09-24 thomas goto done;
8129 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8130 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8131 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8132 10604dce 2021-09-24 thomas err = sync_err;
8133 10604dce 2021-09-24 thomas done:
8134 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8135 10604dce 2021-09-24 thomas free(fileindex_path);
8136 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8137 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8138 10604dce 2021-09-24 thomas err = unlockerr;
8139 10604dce 2021-09-24 thomas return err;
8140 10604dce 2021-09-24 thomas }
8141 10604dce 2021-09-24 thomas
8142 10604dce 2021-09-24 thomas const struct got_error *
8143 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8144 10604dce 2021-09-24 thomas struct got_repository *repo)
8145 10604dce 2021-09-24 thomas {
8146 10604dce 2021-09-24 thomas const struct got_error *err;
8147 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8148 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8149 10604dce 2021-09-24 thomas
8150 10604dce 2021-09-24 thomas *in_progress = 0;
8151 10604dce 2021-09-24 thomas
8152 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8153 10604dce 2021-09-24 thomas if (err)
8154 10604dce 2021-09-24 thomas return err;
8155 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8156 dfe86d1f 2021-09-24 thomas free(branch_refname);
8157 10604dce 2021-09-24 thomas if (err) {
8158 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8159 10604dce 2021-09-24 thomas return err;
8160 10604dce 2021-09-24 thomas } else
8161 10604dce 2021-09-24 thomas *in_progress = 1;
8162 10604dce 2021-09-24 thomas
8163 10604dce 2021-09-24 thomas return NULL;
8164 10604dce 2021-09-24 thomas }
8165 10604dce 2021-09-24 thomas
8166 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8167 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8168 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8169 10604dce 2021-09-24 thomas {
8170 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8171 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8172 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8173 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8174 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8175 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8176 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8177 10604dce 2021-09-24 thomas
8178 10604dce 2021-09-24 thomas *fileindex = NULL;
8179 10604dce 2021-09-24 thomas
8180 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8181 10604dce 2021-09-24 thomas if (err)
8182 10604dce 2021-09-24 thomas return err;
8183 10604dce 2021-09-24 thomas
8184 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8185 10604dce 2021-09-24 thomas if (err)
8186 10604dce 2021-09-24 thomas goto done;
8187 10604dce 2021-09-24 thomas
8188 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8189 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8190 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8191 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8192 10604dce 2021-09-24 thomas &ok_arg);
8193 10604dce 2021-09-24 thomas if (err)
8194 10604dce 2021-09-24 thomas goto done;
8195 10604dce 2021-09-24 thomas
8196 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8197 10604dce 2021-09-24 thomas if (err)
8198 10604dce 2021-09-24 thomas return err;
8199 10604dce 2021-09-24 thomas
8200 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8201 10604dce 2021-09-24 thomas if (err)
8202 10604dce 2021-09-24 thomas return err;
8203 10604dce 2021-09-24 thomas
8204 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8205 10604dce 2021-09-24 thomas 0);
8206 10604dce 2021-09-24 thomas if (err)
8207 10604dce 2021-09-24 thomas goto done;
8208 10604dce 2021-09-24 thomas
8209 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8210 10604dce 2021-09-24 thomas if (err)
8211 10604dce 2021-09-24 thomas goto done;
8212 10604dce 2021-09-24 thomas
8213 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8214 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8215 10604dce 2021-09-24 thomas goto done;
8216 10604dce 2021-09-24 thomas }
8217 10604dce 2021-09-24 thomas
8218 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8219 10604dce 2021-09-24 thomas if (err)
8220 10604dce 2021-09-24 thomas goto done;
8221 10604dce 2021-09-24 thomas
8222 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8223 10604dce 2021-09-24 thomas if (err)
8224 10604dce 2021-09-24 thomas goto done;
8225 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8226 10604dce 2021-09-24 thomas if (err)
8227 10604dce 2021-09-24 thomas goto done;
8228 10604dce 2021-09-24 thomas
8229 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8230 10604dce 2021-09-24 thomas if (err)
8231 10604dce 2021-09-24 thomas goto done;
8232 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8233 10604dce 2021-09-24 thomas if (err)
8234 10604dce 2021-09-24 thomas goto done;
8235 10604dce 2021-09-24 thomas
8236 10604dce 2021-09-24 thomas done:
8237 10604dce 2021-09-24 thomas free(branch_refname);
8238 10604dce 2021-09-24 thomas free(commit_refname);
8239 10604dce 2021-09-24 thomas free(fileindex_path);
8240 10604dce 2021-09-24 thomas if (branch_ref)
8241 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8242 10604dce 2021-09-24 thomas if (commit_ref)
8243 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8244 10604dce 2021-09-24 thomas if (wt_branch)
8245 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8246 10604dce 2021-09-24 thomas free(wt_branch_tip);
8247 10604dce 2021-09-24 thomas if (err) {
8248 10604dce 2021-09-24 thomas if (*fileindex) {
8249 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8250 10604dce 2021-09-24 thomas *fileindex = NULL;
8251 10604dce 2021-09-24 thomas }
8252 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8253 10604dce 2021-09-24 thomas }
8254 10604dce 2021-09-24 thomas return err;
8255 10604dce 2021-09-24 thomas }
8256 10604dce 2021-09-24 thomas
8257 10604dce 2021-09-24 thomas const struct got_error *
8258 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8259 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8260 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8261 10604dce 2021-09-24 thomas {
8262 10604dce 2021-09-24 thomas const struct got_error *err;
8263 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8264 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8265 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8266 10604dce 2021-09-24 thomas int have_staged_files = 0;
8267 10604dce 2021-09-24 thomas
8268 10604dce 2021-09-24 thomas *branch_name = NULL;
8269 10604dce 2021-09-24 thomas *branch_tip = NULL;
8270 10604dce 2021-09-24 thomas *fileindex = NULL;
8271 10604dce 2021-09-24 thomas
8272 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8273 10604dce 2021-09-24 thomas if (err)
8274 10604dce 2021-09-24 thomas return err;
8275 10604dce 2021-09-24 thomas
8276 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8277 10604dce 2021-09-24 thomas if (err)
8278 10604dce 2021-09-24 thomas goto done;
8279 10604dce 2021-09-24 thomas
8280 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8281 10604dce 2021-09-24 thomas &have_staged_files);
8282 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8283 10604dce 2021-09-24 thomas goto done;
8284 10604dce 2021-09-24 thomas if (have_staged_files) {
8285 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8286 10604dce 2021-09-24 thomas goto done;
8287 10604dce 2021-09-24 thomas }
8288 10604dce 2021-09-24 thomas
8289 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8290 10604dce 2021-09-24 thomas if (err)
8291 10604dce 2021-09-24 thomas goto done;
8292 10604dce 2021-09-24 thomas
8293 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8294 10604dce 2021-09-24 thomas if (err)
8295 10604dce 2021-09-24 thomas goto done;
8296 10604dce 2021-09-24 thomas
8297 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8298 10604dce 2021-09-24 thomas if (err)
8299 10604dce 2021-09-24 thomas goto done;
8300 10604dce 2021-09-24 thomas
8301 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8302 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8303 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8304 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8305 10604dce 2021-09-24 thomas goto done;
8306 10604dce 2021-09-24 thomas }
8307 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8308 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8309 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8310 10604dce 2021-09-24 thomas goto done;
8311 10604dce 2021-09-24 thomas }
8312 10604dce 2021-09-24 thomas
8313 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8314 10604dce 2021-09-24 thomas if (err)
8315 10604dce 2021-09-24 thomas goto done;
8316 10604dce 2021-09-24 thomas
8317 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8318 10604dce 2021-09-24 thomas if (err)
8319 10604dce 2021-09-24 thomas goto done;
8320 10604dce 2021-09-24 thomas done:
8321 10604dce 2021-09-24 thomas free(commit_refname);
8322 10604dce 2021-09-24 thomas free(branch_refname);
8323 10604dce 2021-09-24 thomas free(fileindex_path);
8324 10604dce 2021-09-24 thomas if (commit_ref)
8325 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8326 10604dce 2021-09-24 thomas if (branch_ref)
8327 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8328 10604dce 2021-09-24 thomas if (err) {
8329 10604dce 2021-09-24 thomas if (*branch_name) {
8330 10604dce 2021-09-24 thomas free(*branch_name);
8331 10604dce 2021-09-24 thomas *branch_name = NULL;
8332 10604dce 2021-09-24 thomas }
8333 10604dce 2021-09-24 thomas free(*branch_tip);
8334 10604dce 2021-09-24 thomas *branch_tip = NULL;
8335 10604dce 2021-09-24 thomas if (*fileindex) {
8336 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8337 10604dce 2021-09-24 thomas *fileindex = NULL;
8338 10604dce 2021-09-24 thomas }
8339 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8340 10604dce 2021-09-24 thomas }
8341 10604dce 2021-09-24 thomas return err;
8342 10604dce 2021-09-24 thomas }
8343 10604dce 2021-09-24 thomas
8344 10604dce 2021-09-24 thomas const struct got_error *
8345 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8346 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8347 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8348 10604dce 2021-09-24 thomas {
8349 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8350 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8351 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8352 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8353 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8354 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8355 10604dce 2021-09-24 thomas
8356 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8357 945f9229 2022-04-16 thomas worktree->base_commit_id);
8358 945f9229 2022-04-16 thomas if (err)
8359 945f9229 2022-04-16 thomas goto done;
8360 945f9229 2022-04-16 thomas
8361 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8362 945f9229 2022-04-16 thomas worktree->path_prefix);
8363 10604dce 2021-09-24 thomas if (err)
8364 10604dce 2021-09-24 thomas goto done;
8365 10604dce 2021-09-24 thomas
8366 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8367 10604dce 2021-09-24 thomas if (err)
8368 10604dce 2021-09-24 thomas goto done;
8369 10604dce 2021-09-24 thomas
8370 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8371 10604dce 2021-09-24 thomas if (err)
8372 10604dce 2021-09-24 thomas goto done;
8373 10604dce 2021-09-24 thomas
8374 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8375 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8376 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8377 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8378 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8379 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8380 10604dce 2021-09-24 thomas rfa.repo = repo;
8381 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8382 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8383 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8384 10604dce 2021-09-24 thomas if (err)
8385 10604dce 2021-09-24 thomas goto sync;
8386 10604dce 2021-09-24 thomas
8387 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8388 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8389 10604dce 2021-09-24 thomas sync:
8390 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8391 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8392 10604dce 2021-09-24 thomas err = sync_err;
8393 10604dce 2021-09-24 thomas done:
8394 10604dce 2021-09-24 thomas free(tree_id);
8395 10604dce 2021-09-24 thomas free(commit_id);
8396 945f9229 2022-04-16 thomas if (commit)
8397 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8398 10604dce 2021-09-24 thomas if (fileindex)
8399 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8400 10604dce 2021-09-24 thomas free(fileindex_path);
8401 10604dce 2021-09-24 thomas
8402 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8403 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8404 10604dce 2021-09-24 thomas err = unlockerr;
8405 10604dce 2021-09-24 thomas return err;
8406 10604dce 2021-09-24 thomas }
8407 10604dce 2021-09-24 thomas
8408 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8409 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8410 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8411 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8412 2db2652d 2019-08-07 stsp struct got_repository *repo;
8413 2db2652d 2019-08-07 stsp int have_changes;
8414 2db2652d 2019-08-07 stsp };
8415 2db2652d 2019-08-07 stsp
8416 ef20f542 2022-06-26 thomas static const struct got_error *
8417 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8418 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8419 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8420 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8421 735ef5ac 2019-08-03 stsp {
8422 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8423 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8424 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8425 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8426 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8427 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8428 8b13ce36 2019-08-08 stsp
8429 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8430 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8431 8b13ce36 2019-08-08 stsp return NULL;
8432 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8433 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8434 735ef5ac 2019-08-03 stsp
8435 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8436 735ef5ac 2019-08-03 stsp if (ie == NULL)
8437 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8438 735ef5ac 2019-08-03 stsp
8439 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8440 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8441 735ef5ac 2019-08-03 stsp relpath) == -1)
8442 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8443 735ef5ac 2019-08-03 stsp
8444 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8445 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8446 43010591 2023-02-17 thomas &base_commit_id, ie);
8447 735ef5ac 2019-08-03 stsp }
8448 735ef5ac 2019-08-03 stsp
8449 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8450 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8451 3aa5969e 2019-08-06 stsp goto done;
8452 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8453 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8454 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8455 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8456 735ef5ac 2019-08-03 stsp goto done;
8457 3aa5969e 2019-08-06 stsp }
8458 735ef5ac 2019-08-03 stsp
8459 2db2652d 2019-08-07 stsp a->have_changes = 1;
8460 2db2652d 2019-08-07 stsp
8461 735ef5ac 2019-08-03 stsp p = in_repo_path;
8462 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8463 735ef5ac 2019-08-03 stsp p++;
8464 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8465 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8466 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8467 735ef5ac 2019-08-03 stsp done:
8468 735ef5ac 2019-08-03 stsp free(in_repo_path);
8469 dc424a06 2019-08-07 stsp return err;
8470 dc424a06 2019-08-07 stsp }
8471 dc424a06 2019-08-07 stsp
8472 2db2652d 2019-08-07 stsp struct stage_path_arg {
8473 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8474 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8475 2db2652d 2019-08-07 stsp struct got_repository *repo;
8476 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8477 2db2652d 2019-08-07 stsp void *status_arg;
8478 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8479 2db2652d 2019-08-07 stsp void *patch_arg;
8480 7b5dc508 2019-10-28 stsp int staged_something;
8481 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8482 2db2652d 2019-08-07 stsp };
8483 2db2652d 2019-08-07 stsp
8484 2db2652d 2019-08-07 stsp static const struct got_error *
8485 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8486 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8487 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8488 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8489 0cb83759 2019-08-03 stsp {
8490 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8491 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8492 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8493 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8494 0cb83759 2019-08-03 stsp uint32_t stage;
8495 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8496 0aeb8099 2020-07-23 stsp struct stat sb;
8497 8b13ce36 2019-08-08 stsp
8498 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8499 8b13ce36 2019-08-08 stsp return NULL;
8500 0cb83759 2019-08-03 stsp
8501 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8502 d3e7c587 2019-08-03 stsp if (ie == NULL)
8503 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8504 0cb83759 2019-08-03 stsp
8505 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8506 2db2652d 2019-08-07 stsp relpath)== -1)
8507 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8508 0cb83759 2019-08-03 stsp
8509 0cb83759 2019-08-03 stsp switch (status) {
8510 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8511 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8512 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8513 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8514 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8515 0aeb8099 2020-07-23 stsp break;
8516 0aeb8099 2020-07-23 stsp }
8517 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8518 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8519 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8520 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8521 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8522 dc424a06 2019-08-07 stsp if (err)
8523 dc424a06 2019-08-07 stsp break;
8524 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8525 dc424a06 2019-08-07 stsp break;
8526 dc424a06 2019-08-07 stsp } else {
8527 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8528 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8529 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8530 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8531 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8532 dc424a06 2019-08-07 stsp break;
8533 dc424a06 2019-08-07 stsp }
8534 dc424a06 2019-08-07 stsp }
8535 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8536 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8537 0cb83759 2019-08-03 stsp if (err)
8538 dc424a06 2019-08-07 stsp break;
8539 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8540 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8541 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8542 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8543 0cb83759 2019-08-03 stsp else
8544 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8545 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8546 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8547 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8548 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8549 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8550 35213c7c 2020-07-23 stsp ssize_t target_len;
8551 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8552 35213c7c 2020-07-23 stsp sizeof(target_path));
8553 35213c7c 2020-07-23 stsp if (target_len == -1) {
8554 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8555 35213c7c 2020-07-23 stsp ondisk_path);
8556 35213c7c 2020-07-23 stsp break;
8557 35213c7c 2020-07-23 stsp }
8558 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8559 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8560 35213c7c 2020-07-23 stsp a->worktree->root_path);
8561 35213c7c 2020-07-23 stsp if (err)
8562 35213c7c 2020-07-23 stsp break;
8563 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8564 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8565 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8566 35213c7c 2020-07-23 stsp break;
8567 35213c7c 2020-07-23 stsp }
8568 35213c7c 2020-07-23 stsp }
8569 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8570 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8571 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8572 35213c7c 2020-07-23 stsp else
8573 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8574 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8575 0aeb8099 2020-07-23 stsp } else {
8576 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8577 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8578 0aeb8099 2020-07-23 stsp }
8579 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8580 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8581 dc424a06 2019-08-07 stsp break;
8582 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8583 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8584 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8585 f289c82c 2022-06-13 thomas if (err)
8586 f289c82c 2022-06-13 thomas break;
8587 f289c82c 2022-06-13 thomas /*
8588 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8589 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8590 f289c82c 2022-06-13 thomas */
8591 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8592 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8593 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8594 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8595 f289c82c 2022-06-13 thomas }
8596 0cb83759 2019-08-03 stsp break;
8597 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8598 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8599 d3e7c587 2019-08-03 stsp break;
8600 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8601 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8602 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8603 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8604 dc424a06 2019-08-07 stsp if (err)
8605 dc424a06 2019-08-07 stsp break;
8606 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8607 88f33a19 2019-08-08 stsp break;
8608 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8609 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8610 dc424a06 2019-08-07 stsp break;
8611 88f33a19 2019-08-08 stsp }
8612 dc424a06 2019-08-07 stsp }
8613 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8614 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8615 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8616 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8617 dc424a06 2019-08-07 stsp break;
8618 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8619 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8620 12463d8b 2019-12-13 stsp de_name);
8621 0cb83759 2019-08-03 stsp break;
8622 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8623 d3e7c587 2019-08-03 stsp break;
8624 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8625 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8626 ebf48fd5 2019-08-03 stsp break;
8627 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8628 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8629 2a06fe5f 2019-08-24 stsp break;
8630 0cb83759 2019-08-03 stsp default:
8631 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8632 537ac44b 2019-08-03 stsp break;
8633 0cb83759 2019-08-03 stsp }
8634 dc424a06 2019-08-07 stsp
8635 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8636 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8637 dc424a06 2019-08-07 stsp free(path_content);
8638 2db2652d 2019-08-07 stsp free(ondisk_path);
8639 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8640 0ebf8283 2019-07-24 stsp return err;
8641 0ebf8283 2019-07-24 stsp }
8642 0cb83759 2019-08-03 stsp
8643 0cb83759 2019-08-03 stsp const struct got_error *
8644 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8645 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8646 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8647 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8648 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8649 0cb83759 2019-08-03 stsp {
8650 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8651 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8652 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8653 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8654 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8655 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8656 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8657 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8658 0cb83759 2019-08-03 stsp
8659 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8660 0cb83759 2019-08-03 stsp if (err)
8661 0cb83759 2019-08-03 stsp return err;
8662 0cb83759 2019-08-03 stsp
8663 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8664 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8665 735ef5ac 2019-08-03 stsp if (err)
8666 735ef5ac 2019-08-03 stsp goto done;
8667 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8668 735ef5ac 2019-08-03 stsp if (err)
8669 735ef5ac 2019-08-03 stsp goto done;
8670 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8671 0cb83759 2019-08-03 stsp if (err)
8672 0cb83759 2019-08-03 stsp goto done;
8673 0cb83759 2019-08-03 stsp
8674 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8675 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8676 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8677 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8678 2db2652d 2019-08-07 stsp oka.repo = repo;
8679 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8680 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8681 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8682 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8683 735ef5ac 2019-08-03 stsp if (err)
8684 735ef5ac 2019-08-03 stsp goto done;
8685 735ef5ac 2019-08-03 stsp }
8686 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8687 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8688 2db2652d 2019-08-07 stsp goto done;
8689 2db2652d 2019-08-07 stsp }
8690 735ef5ac 2019-08-03 stsp
8691 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8692 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8693 2db2652d 2019-08-07 stsp spa.repo = repo;
8694 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8695 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8696 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8697 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8698 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8699 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8700 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8701 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8702 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8703 42005733 2019-08-03 stsp if (err)
8704 2db2652d 2019-08-07 stsp goto done;
8705 7b5dc508 2019-10-28 stsp }
8706 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8707 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8708 7b5dc508 2019-10-28 stsp goto done;
8709 0cb83759 2019-08-03 stsp }
8710 0cb83759 2019-08-03 stsp
8711 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8712 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8713 0cb83759 2019-08-03 stsp err = sync_err;
8714 0cb83759 2019-08-03 stsp done:
8715 735ef5ac 2019-08-03 stsp if (head_ref)
8716 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8717 735ef5ac 2019-08-03 stsp free(head_commit_id);
8718 0cb83759 2019-08-03 stsp free(fileindex_path);
8719 0cb83759 2019-08-03 stsp if (fileindex)
8720 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8721 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8722 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8723 0cb83759 2019-08-03 stsp err = unlockerr;
8724 0cb83759 2019-08-03 stsp return err;
8725 0cb83759 2019-08-03 stsp }
8726 ad493afc 2019-08-03 stsp
8727 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8728 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8729 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8730 ad493afc 2019-08-03 stsp struct got_repository *repo;
8731 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8732 ad493afc 2019-08-03 stsp void *progress_arg;
8733 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8734 2e1f37b0 2019-08-08 stsp void *patch_arg;
8735 ad493afc 2019-08-03 stsp };
8736 2e1f37b0 2019-08-08 stsp
8737 2e1f37b0 2019-08-08 stsp static const struct got_error *
8738 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8739 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8740 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8741 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8742 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8743 2e1f37b0 2019-08-08 stsp {
8744 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8745 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8746 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8747 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8748 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8749 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8750 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8751 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8752 2e1f37b0 2019-08-08 stsp
8753 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8754 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8755 2e1f37b0 2019-08-08 stsp
8756 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8757 2e1f37b0 2019-08-08 stsp if (err)
8758 2e1f37b0 2019-08-08 stsp return err;
8759 f4ae6ddb 2022-07-01 thomas
8760 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8761 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8762 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8763 f4ae6ddb 2022-07-01 thomas goto done;
8764 f4ae6ddb 2022-07-01 thomas }
8765 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8766 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8767 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8768 f4ae6ddb 2022-07-01 thomas goto done;
8769 f4ae6ddb 2022-07-01 thomas }
8770 f4ae6ddb 2022-07-01 thomas
8771 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8772 2e1f37b0 2019-08-08 stsp if (err)
8773 2e1f37b0 2019-08-08 stsp goto done;
8774 2e1f37b0 2019-08-08 stsp
8775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8776 2e1f37b0 2019-08-08 stsp if (err)
8777 2e1f37b0 2019-08-08 stsp goto done;
8778 2e1f37b0 2019-08-08 stsp
8779 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8780 2e1f37b0 2019-08-08 stsp if (err)
8781 2e1f37b0 2019-08-08 stsp goto done;
8782 2e1f37b0 2019-08-08 stsp
8783 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8784 f4ae6ddb 2022-07-01 thomas fd2);
8785 2e1f37b0 2019-08-08 stsp if (err)
8786 2e1f37b0 2019-08-08 stsp goto done;
8787 2e1f37b0 2019-08-08 stsp
8788 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8789 2e1f37b0 2019-08-08 stsp if (err)
8790 2e1f37b0 2019-08-08 stsp goto done;
8791 ad493afc 2019-08-03 stsp
8792 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8793 2e1f37b0 2019-08-08 stsp if (err)
8794 2e1f37b0 2019-08-08 stsp goto done;
8795 2e1f37b0 2019-08-08 stsp
8796 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8797 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8798 2e1f37b0 2019-08-08 stsp if (err)
8799 2e1f37b0 2019-08-08 stsp goto done;
8800 2e1f37b0 2019-08-08 stsp
8801 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8802 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8803 2e1f37b0 2019-08-08 stsp if (err)
8804 2e1f37b0 2019-08-08 stsp goto done;
8805 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8806 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8807 2e1f37b0 2019-08-08 stsp if (err)
8808 2e1f37b0 2019-08-08 stsp goto done;
8809 2e1f37b0 2019-08-08 stsp
8810 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8811 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8812 2e1f37b0 2019-08-08 stsp goto done;
8813 2e1f37b0 2019-08-08 stsp }
8814 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8815 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8816 2e1f37b0 2019-08-08 stsp goto done;
8817 2e1f37b0 2019-08-08 stsp }
8818 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8819 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8820 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8821 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8822 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8823 fe621944 2020-11-10 stsp nchanges++;
8824 fe621944 2020-11-10 stsp }
8825 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8826 2e1f37b0 2019-08-08 stsp int choice;
8827 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8828 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8829 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8830 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8831 2e1f37b0 2019-08-08 stsp if (err)
8832 2e1f37b0 2019-08-08 stsp goto done;
8833 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8834 2e1f37b0 2019-08-08 stsp have_content = 1;
8835 19e4b907 2019-08-08 stsp else
8836 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8837 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8838 2e1f37b0 2019-08-08 stsp break;
8839 2e1f37b0 2019-08-08 stsp }
8840 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8841 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8842 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8843 2e1f37b0 2019-08-08 stsp done:
8844 2e1f37b0 2019-08-08 stsp free(label1);
8845 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8846 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8847 2e1f37b0 2019-08-08 stsp if (blob)
8848 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8849 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8850 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8851 2e1f37b0 2019-08-08 stsp if (staged_blob)
8852 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8853 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8854 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8855 fe621944 2020-11-10 stsp err = free_err;
8856 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8857 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8858 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8859 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8860 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8861 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8862 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8863 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8864 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8865 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8866 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8867 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8868 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8869 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8870 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8871 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8872 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8873 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8874 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8875 2e1f37b0 2019-08-08 stsp }
8876 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8877 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8878 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8879 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8880 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8881 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8882 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8883 2e1f37b0 2019-08-08 stsp }
8884 2e1f37b0 2019-08-08 stsp free(path1);
8885 2e1f37b0 2019-08-08 stsp free(path2);
8886 fda8017d 2020-07-23 stsp return err;
8887 fda8017d 2020-07-23 stsp }
8888 fda8017d 2020-07-23 stsp
8889 fda8017d 2020-07-23 stsp static const struct got_error *
8890 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8891 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8892 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8893 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8894 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8895 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8896 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8897 fda8017d 2020-07-23 stsp {
8898 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8899 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8900 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8901 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8902 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8903 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8904 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8905 fda8017d 2020-07-23 stsp struct stat sb;
8906 fda8017d 2020-07-23 stsp
8907 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8908 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8909 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8910 fda8017d 2020-07-23 stsp if (err)
8911 fda8017d 2020-07-23 stsp return err;
8912 fda8017d 2020-07-23 stsp
8913 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8914 fda8017d 2020-07-23 stsp return NULL;
8915 fda8017d 2020-07-23 stsp
8916 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8917 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8918 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8919 fda8017d 2020-07-23 stsp if (err)
8920 fda8017d 2020-07-23 stsp goto done;
8921 fda8017d 2020-07-23 stsp }
8922 fda8017d 2020-07-23 stsp
8923 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8924 fda8017d 2020-07-23 stsp if (f == NULL) {
8925 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8926 fda8017d 2020-07-23 stsp path_unstaged_content);
8927 fda8017d 2020-07-23 stsp goto done;
8928 fda8017d 2020-07-23 stsp }
8929 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8930 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8931 fda8017d 2020-07-23 stsp goto done;
8932 fda8017d 2020-07-23 stsp }
8933 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8934 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8935 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8936 fda8017d 2020-07-23 stsp size_t r;
8937 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8938 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8939 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8940 fda8017d 2020-07-23 stsp goto done;
8941 fda8017d 2020-07-23 stsp }
8942 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8943 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8944 fda8017d 2020-07-23 stsp goto done;
8945 fda8017d 2020-07-23 stsp }
8946 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8947 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8948 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8949 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8950 fda8017d 2020-07-23 stsp progress_arg);
8951 fda8017d 2020-07-23 stsp } else {
8952 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8953 67a66647 2021-05-31 stsp
8954 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8955 67a66647 2021-05-31 stsp if (err)
8956 67a66647 2021-05-31 stsp return err;
8957 67a66647 2021-05-31 stsp
8958 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8959 67a66647 2021-05-31 stsp parent) == -1) {
8960 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8961 67a66647 2021-05-31 stsp base_path = NULL;
8962 67a66647 2021-05-31 stsp goto done;
8963 67a66647 2021-05-31 stsp }
8964 67a66647 2021-05-31 stsp
8965 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8966 fc2a50f2 2022-11-01 thomas base_path, "");
8967 67a66647 2021-05-31 stsp if (err)
8968 67a66647 2021-05-31 stsp goto done;
8969 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8970 67a66647 2021-05-31 stsp blob_base);
8971 67a66647 2021-05-31 stsp if (err)
8972 67a66647 2021-05-31 stsp goto done;
8973 67a66647 2021-05-31 stsp
8974 b6b86fd1 2022-08-30 thomas /*
8975 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8976 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8977 eec2f5a9 2021-06-03 stsp */
8978 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8979 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8980 eec2f5a9 2021-06-03 stsp ondisk_path);
8981 eec2f5a9 2021-06-03 stsp if (err)
8982 eec2f5a9 2021-06-03 stsp goto done;
8983 eec2f5a9 2021-06-03 stsp } else {
8984 eec2f5a9 2021-06-03 stsp int fd;
8985 06340621 2021-12-31 thomas fd = open(ondisk_path,
8986 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8987 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8988 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8989 eec2f5a9 2021-06-03 stsp goto done;
8990 eec2f5a9 2021-06-03 stsp }
8991 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8992 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8993 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8994 eec2f5a9 2021-06-03 stsp close(fd);
8995 eec2f5a9 2021-06-03 stsp goto done;
8996 eec2f5a9 2021-06-03 stsp }
8997 eec2f5a9 2021-06-03 stsp }
8998 eec2f5a9 2021-06-03 stsp
8999 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9000 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9001 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9002 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9003 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9004 fda8017d 2020-07-23 stsp }
9005 fda8017d 2020-07-23 stsp if (err)
9006 fda8017d 2020-07-23 stsp goto done;
9007 fda8017d 2020-07-23 stsp
9008 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9009 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9010 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9011 2ac8aa02 2020-11-09 stsp } else {
9012 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9013 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9014 2ac8aa02 2020-11-09 stsp }
9015 fda8017d 2020-07-23 stsp done:
9016 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9017 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9018 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9019 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9020 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9021 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9022 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9023 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9024 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9025 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9026 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9027 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9028 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9029 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9030 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9031 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9032 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9033 67a66647 2021-05-31 stsp free(blob_base_path);
9034 67a66647 2021-05-31 stsp free(parent);
9035 67a66647 2021-05-31 stsp free(base_path);
9036 2e1f37b0 2019-08-08 stsp return err;
9037 2e1f37b0 2019-08-08 stsp }
9038 2e1f37b0 2019-08-08 stsp
9039 ad493afc 2019-08-03 stsp static const struct got_error *
9040 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9041 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9042 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9043 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9044 ad493afc 2019-08-03 stsp {
9045 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9046 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9047 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9048 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9049 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9050 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9051 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9052 9bc94a15 2019-08-03 stsp struct stat sb;
9053 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9054 ad493afc 2019-08-03 stsp
9055 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9056 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9057 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9058 2e1f37b0 2019-08-08 stsp return NULL;
9059 2e1f37b0 2019-08-08 stsp
9060 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9061 ad493afc 2019-08-03 stsp if (ie == NULL)
9062 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9063 9bc94a15 2019-08-03 stsp
9064 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9065 9bc94a15 2019-08-03 stsp == -1)
9066 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9067 ad493afc 2019-08-03 stsp
9068 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9069 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9070 f69721c3 2019-10-21 stsp if (err)
9071 f69721c3 2019-10-21 stsp goto done;
9072 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9073 f69721c3 2019-10-21 stsp id_str) == -1) {
9074 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9075 f4ae6ddb 2022-07-01 thomas goto done;
9076 f4ae6ddb 2022-07-01 thomas }
9077 f4ae6ddb 2022-07-01 thomas
9078 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9079 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9080 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9081 f4ae6ddb 2022-07-01 thomas goto done;
9082 f4ae6ddb 2022-07-01 thomas }
9083 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9084 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9085 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9086 f69721c3 2019-10-21 stsp goto done;
9087 f69721c3 2019-10-21 stsp }
9088 f69721c3 2019-10-21 stsp
9089 ad493afc 2019-08-03 stsp switch (staged_status) {
9090 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9091 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9092 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9093 ad493afc 2019-08-03 stsp if (err)
9094 ad493afc 2019-08-03 stsp break;
9095 ad493afc 2019-08-03 stsp /* fall through */
9096 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9097 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9098 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9099 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9100 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9101 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9102 2e1f37b0 2019-08-08 stsp if (err)
9103 2e1f37b0 2019-08-08 stsp break;
9104 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9105 2e1f37b0 2019-08-08 stsp break;
9106 2e1f37b0 2019-08-08 stsp } else {
9107 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9108 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9109 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9110 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9111 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9112 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9113 2e1f37b0 2019-08-08 stsp }
9114 2e1f37b0 2019-08-08 stsp }
9115 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9116 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9117 ad493afc 2019-08-03 stsp if (err)
9118 ad493afc 2019-08-03 stsp break;
9119 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9120 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9121 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9122 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9123 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9124 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9125 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9126 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9127 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9128 ea7786be 2020-07-23 stsp break;
9129 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9130 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9131 36bf999c 2020-07-23 stsp char *staged_target;
9132 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9133 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9134 36bf999c 2020-07-23 stsp if (err)
9135 36bf999c 2020-07-23 stsp goto done;
9136 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9137 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9138 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9139 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9140 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9141 36bf999c 2020-07-23 stsp free(staged_target);
9142 dfe9fba0 2020-07-23 stsp } else {
9143 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9144 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9145 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9146 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9147 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9148 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9149 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9150 dfe9fba0 2020-07-23 stsp }
9151 ea7786be 2020-07-23 stsp break;
9152 ea7786be 2020-07-23 stsp default:
9153 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9154 ea7786be 2020-07-23 stsp break;
9155 ea7786be 2020-07-23 stsp }
9156 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9157 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9158 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9159 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9160 2ac8aa02 2020-11-09 stsp }
9161 ad493afc 2019-08-03 stsp break;
9162 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9163 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9164 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9165 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9166 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9167 2e1f37b0 2019-08-08 stsp if (err)
9168 2e1f37b0 2019-08-08 stsp break;
9169 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9170 2e1f37b0 2019-08-08 stsp break;
9171 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9172 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9173 2e1f37b0 2019-08-08 stsp break;
9174 2e1f37b0 2019-08-08 stsp }
9175 2e1f37b0 2019-08-08 stsp }
9176 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9177 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9178 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9179 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9180 9bc94a15 2019-08-03 stsp if (err)
9181 9bc94a15 2019-08-03 stsp break;
9182 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9183 ad493afc 2019-08-03 stsp break;
9184 ad493afc 2019-08-03 stsp }
9185 f69721c3 2019-10-21 stsp done:
9186 ad493afc 2019-08-03 stsp free(ondisk_path);
9187 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9188 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9189 ad493afc 2019-08-03 stsp if (blob_base)
9190 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9191 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9192 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9193 ad493afc 2019-08-03 stsp if (blob_staged)
9194 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9195 f69721c3 2019-10-21 stsp free(id_str);
9196 f69721c3 2019-10-21 stsp free(label_orig);
9197 ad493afc 2019-08-03 stsp return err;
9198 ad493afc 2019-08-03 stsp }
9199 ad493afc 2019-08-03 stsp
9200 ad493afc 2019-08-03 stsp const struct got_error *
9201 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9202 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9203 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9204 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9205 ad493afc 2019-08-03 stsp struct got_repository *repo)
9206 ad493afc 2019-08-03 stsp {
9207 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9208 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9209 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9210 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9211 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9212 ad493afc 2019-08-03 stsp
9213 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9214 ad493afc 2019-08-03 stsp if (err)
9215 ad493afc 2019-08-03 stsp return err;
9216 ad493afc 2019-08-03 stsp
9217 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9218 ad493afc 2019-08-03 stsp if (err)
9219 ad493afc 2019-08-03 stsp goto done;
9220 ad493afc 2019-08-03 stsp
9221 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9222 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9223 ad493afc 2019-08-03 stsp upa.repo = repo;
9224 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9225 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9226 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9227 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9228 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9229 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9230 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9231 ad493afc 2019-08-03 stsp if (err)
9232 ad493afc 2019-08-03 stsp goto done;
9233 ad493afc 2019-08-03 stsp }
9234 ad493afc 2019-08-03 stsp
9235 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9236 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9237 ad493afc 2019-08-03 stsp err = sync_err;
9238 ad493afc 2019-08-03 stsp done:
9239 ad493afc 2019-08-03 stsp free(fileindex_path);
9240 ad493afc 2019-08-03 stsp if (fileindex)
9241 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9242 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9243 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9244 ad493afc 2019-08-03 stsp err = unlockerr;
9245 ad493afc 2019-08-03 stsp return err;
9246 ad493afc 2019-08-03 stsp }
9247 b2118c49 2020-07-28 stsp
9248 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9249 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9250 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9251 b2118c49 2020-07-28 stsp void *info_arg;
9252 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9253 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9254 b2118c49 2020-07-28 stsp void *cancel_arg;
9255 b2118c49 2020-07-28 stsp };
9256 b2118c49 2020-07-28 stsp
9257 b2118c49 2020-07-28 stsp static const struct got_error *
9258 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9259 b2118c49 2020-07-28 stsp {
9260 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9261 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9262 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9263 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9264 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9265 b2118c49 2020-07-28 stsp int stage;
9266 b2118c49 2020-07-28 stsp
9267 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9268 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9269 b2118c49 2020-07-28 stsp
9270 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9271 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9272 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9273 b2118c49 2020-07-28 stsp break;
9274 b2118c49 2020-07-28 stsp }
9275 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9276 b2118c49 2020-07-28 stsp return NULL;
9277 b2118c49 2020-07-28 stsp
9278 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9279 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9280 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9281 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9282 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9283 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9284 43010591 2023-02-17 thomas &staged_blob_id, ie);
9285 b2118c49 2020-07-28 stsp }
9286 b2118c49 2020-07-28 stsp
9287 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9288 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9289 b2118c49 2020-07-28 stsp
9290 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9291 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9292 b2118c49 2020-07-28 stsp }
9293 b2118c49 2020-07-28 stsp
9294 b2118c49 2020-07-28 stsp const struct got_error *
9295 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9296 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9297 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9298 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9299 b2118c49 2020-07-28 stsp
9300 b2118c49 2020-07-28 stsp {
9301 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9302 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9303 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9304 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9305 b2118c49 2020-07-28 stsp
9306 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9307 b2118c49 2020-07-28 stsp if (err)
9308 b2118c49 2020-07-28 stsp return err;
9309 b2118c49 2020-07-28 stsp
9310 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9311 b2118c49 2020-07-28 stsp if (err)
9312 b2118c49 2020-07-28 stsp goto done;
9313 b2118c49 2020-07-28 stsp
9314 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9315 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9316 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9317 b2118c49 2020-07-28 stsp arg.paths = paths;
9318 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9319 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9320 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9321 b2118c49 2020-07-28 stsp &arg);
9322 b2118c49 2020-07-28 stsp done:
9323 b2118c49 2020-07-28 stsp free(fileindex_path);
9324 b2118c49 2020-07-28 stsp if (fileindex)
9325 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9326 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9327 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9328 b2118c49 2020-07-28 stsp err = unlockerr;
9329 b2118c49 2020-07-28 stsp return err;
9330 b2118c49 2020-07-28 stsp }
9331 814624e7 2022-03-22 thomas
9332 814624e7 2022-03-22 thomas static const struct got_error *
9333 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9334 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9335 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9336 814624e7 2022-03-22 thomas {
9337 814624e7 2022-03-22 thomas const struct got_error *err;
9338 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9339 814624e7 2022-03-22 thomas struct stat sb;
9340 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9341 814624e7 2022-03-22 thomas
9342 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9343 814624e7 2022-03-22 thomas if (err)
9344 814624e7 2022-03-22 thomas return err;
9345 814624e7 2022-03-22 thomas
9346 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9347 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9348 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9349 814624e7 2022-03-22 thomas
9350 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9351 814624e7 2022-03-22 thomas if (ie) {
9352 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9353 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9354 12de5570 2022-05-01 thomas repo);
9355 814624e7 2022-03-22 thomas if (err)
9356 814624e7 2022-03-22 thomas goto done;
9357 814624e7 2022-03-22 thomas } else {
9358 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9359 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9360 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9361 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9362 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9363 814624e7 2022-03-22 thomas ondisk_path);
9364 814624e7 2022-03-22 thomas goto done;
9365 814624e7 2022-03-22 thomas }
9366 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9367 814624e7 2022-03-22 thomas }
9368 814624e7 2022-03-22 thomas }
9369 814624e7 2022-03-22 thomas
9370 814624e7 2022-03-22 thomas done:
9371 814624e7 2022-03-22 thomas free(ondisk_path);
9372 814624e7 2022-03-22 thomas return err;
9373 814624e7 2022-03-22 thomas }
9374 814624e7 2022-03-22 thomas
9375 814624e7 2022-03-22 thomas static const struct got_error *
9376 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9377 814624e7 2022-03-22 thomas unsigned char staged_status)
9378 814624e7 2022-03-22 thomas {
9379 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9380 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9381 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9382 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9383 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9384 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9385 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9386 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9387 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9388 814624e7 2022-03-22 thomas return NULL;
9389 814624e7 2022-03-22 thomas }
9390 814624e7 2022-03-22 thomas
9391 814624e7 2022-03-22 thomas static const struct got_error *
9392 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9393 814624e7 2022-03-22 thomas {
9394 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9395 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9396 814624e7 2022-03-22 thomas return NULL;
9397 814624e7 2022-03-22 thomas }
9398 814624e7 2022-03-22 thomas
9399 814624e7 2022-03-22 thomas static const struct got_error *
9400 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9401 814624e7 2022-03-22 thomas unsigned char staged_status)
9402 814624e7 2022-03-22 thomas {
9403 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9404 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9405 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9406 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9407 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9408 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9409 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9410 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9411 814624e7 2022-03-22 thomas return NULL;
9412 814624e7 2022-03-22 thomas }
9413 814624e7 2022-03-22 thomas
9414 814624e7 2022-03-22 thomas const struct got_error *
9415 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9416 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9417 814624e7 2022-03-22 thomas {
9418 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9419 814624e7 2022-03-22 thomas }
9420 814624e7 2022-03-22 thomas
9421 814624e7 2022-03-22 thomas const struct got_error *
9422 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9423 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9424 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9425 814624e7 2022-03-22 thomas {
9426 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9427 814624e7 2022-03-22 thomas int file_renamed = 0;
9428 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9429 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9430 814624e7 2022-03-22 thomas
9431 814624e7 2022-03-22 thomas *oldpath = NULL;
9432 814624e7 2022-03-22 thomas *newpath = NULL;
9433 814624e7 2022-03-22 thomas
9434 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9435 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9436 814624e7 2022-03-22 thomas if (err)
9437 814624e7 2022-03-22 thomas goto done;
9438 814624e7 2022-03-22 thomas
9439 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9440 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9441 814624e7 2022-03-22 thomas if (err)
9442 814624e7 2022-03-22 thomas goto done;
9443 814624e7 2022-03-22 thomas
9444 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9445 814624e7 2022-03-22 thomas file_renamed = 1;
9446 814624e7 2022-03-22 thomas
9447 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9448 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9449 814624e7 2022-03-22 thomas else if (file_renamed) {
9450 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9451 814624e7 2022-03-22 thomas if (err == NULL)
9452 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9453 814624e7 2022-03-22 thomas } else if (old == NULL)
9454 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9455 814624e7 2022-03-22 thomas else
9456 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9457 814624e7 2022-03-22 thomas
9458 814624e7 2022-03-22 thomas done:
9459 814624e7 2022-03-22 thomas if (err) {
9460 814624e7 2022-03-22 thomas free(*oldpath);
9461 814624e7 2022-03-22 thomas *oldpath = NULL;
9462 814624e7 2022-03-22 thomas free(*newpath);
9463 814624e7 2022-03-22 thomas *newpath = NULL;
9464 814624e7 2022-03-22 thomas }
9465 814624e7 2022-03-22 thomas return err;
9466 814624e7 2022-03-22 thomas }
9467 814624e7 2022-03-22 thomas
9468 814624e7 2022-03-22 thomas const struct got_error *
9469 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9470 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9471 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9472 814624e7 2022-03-22 thomas {
9473 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9474 5e22b737 2022-05-31 thomas
9475 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9476 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9477 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9478 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9479 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9480 5e22b737 2022-05-31 thomas saa.repo = repo;
9481 5e22b737 2022-05-31 thomas
9482 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9483 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9484 814624e7 2022-03-22 thomas }
9485 5e22b737 2022-05-31 thomas
9486 5e22b737 2022-05-31 thomas const struct got_error *
9487 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9488 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9489 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9490 5e22b737 2022-05-31 thomas {
9491 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9492 5e22b737 2022-05-31 thomas
9493 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9494 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9495 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9496 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9497 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9498 5e22b737 2022-05-31 thomas sda.repo = repo;
9499 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9500 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9501 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9502 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9503 5e22b737 2022-05-31 thomas
9504 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9505 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9506 5e22b737 2022-05-31 thomas }
9507 5e22b737 2022-05-31 thomas
9508 5e22b737 2022-05-31 thomas const struct got_error *
9509 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9510 36832a8e 2022-05-31 thomas const char *fileindex_path)
9511 5e22b737 2022-05-31 thomas {
9512 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9513 5e22b737 2022-05-31 thomas
9514 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9515 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9516 5e22b737 2022-05-31 thomas
9517 5e22b737 2022-05-31 thomas return err;
9518 5e22b737 2022-05-31 thomas }