Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 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 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 b2b3fce1 2022-10-29 op
67 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
68 86c3caaf 2018-03-09 stsp
69 99724ed4 2018-03-10 stsp static const struct got_error *
70 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
71 99724ed4 2018-03-10 stsp {
72 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 99724ed4 2018-03-10 stsp char *path;
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
77 99724ed4 2018-03-10 stsp
78 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
79 99724ed4 2018-03-10 stsp free(path);
80 507dc3bb 2018-12-29 stsp return err;
81 507dc3bb 2018-12-29 stsp }
82 507dc3bb 2018-12-29 stsp
83 507dc3bb 2018-12-29 stsp static const struct got_error *
84 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
85 507dc3bb 2018-12-29 stsp {
86 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
87 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
88 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
89 507dc3bb 2018-12-29 stsp char *path = NULL;
90 507dc3bb 2018-12-29 stsp
91 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
93 507dc3bb 2018-12-29 stsp path = NULL;
94 507dc3bb 2018-12-29 stsp goto done;
95 507dc3bb 2018-12-29 stsp }
96 507dc3bb 2018-12-29 stsp
97 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 507dc3bb 2018-12-29 stsp if (err)
99 507dc3bb 2018-12-29 stsp goto done;
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp if (content) {
102 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
103 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
105 507dc3bb 2018-12-29 stsp goto done;
106 507dc3bb 2018-12-29 stsp }
107 507dc3bb 2018-12-29 stsp }
108 507dc3bb 2018-12-29 stsp
109 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
111 2a57020b 2019-02-20 stsp unlink(tmppath);
112 507dc3bb 2018-12-29 stsp goto done;
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp
115 507dc3bb 2018-12-29 stsp done:
116 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
117 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
118 230a42bd 2019-05-11 jcs free(tmppath);
119 09fe317a 2018-03-11 stsp return err;
120 09fe317a 2018-03-11 stsp }
121 09fe317a 2018-03-11 stsp
122 024e9686 2019-05-14 stsp static const struct got_error *
123 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
124 024e9686 2019-05-14 stsp {
125 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
126 024e9686 2019-05-14 stsp char *refstr = NULL;
127 024e9686 2019-05-14 stsp
128 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
129 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
132 024e9686 2019-05-14 stsp } else {
133 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
134 024e9686 2019-05-14 stsp if (refstr == NULL)
135 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
136 024e9686 2019-05-14 stsp }
137 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 024e9686 2019-05-14 stsp free(refstr);
139 024e9686 2019-05-14 stsp return err;
140 024e9686 2019-05-14 stsp }
141 024e9686 2019-05-14 stsp
142 86c3caaf 2018-03-09 stsp const struct got_error *
143 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
144 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
145 86c3caaf 2018-03-09 stsp {
146 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
147 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
148 ec22038e 2019-03-10 stsp uuid_t uuid;
149 ec22038e 2019-03-10 stsp uint32_t uuid_status;
150 65596e15 2018-12-24 stsp int obj_type;
151 7ac97322 2018-03-11 stsp char *path_got = NULL;
152 1451e70d 2018-03-10 stsp char *formatstr = NULL;
153 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
154 65596e15 2018-12-24 stsp char *basestr = NULL;
155 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
156 65596e15 2018-12-24 stsp
157 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
159 0c48fee2 2019-03-11 stsp goto done;
160 0c48fee2 2019-03-11 stsp }
161 0c48fee2 2019-03-11 stsp
162 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
163 65596e15 2018-12-24 stsp if (err)
164 65596e15 2018-12-24 stsp return err;
165 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
166 65596e15 2018-12-24 stsp if (err)
167 65596e15 2018-12-24 stsp return err;
168 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
170 86c3caaf 2018-03-09 stsp
171 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
172 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
174 0bb8a95e 2018-03-12 stsp }
175 577ec78f 2018-03-11 stsp
176 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
177 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
179 86c3caaf 2018-03-09 stsp goto done;
180 86c3caaf 2018-03-09 stsp }
181 86c3caaf 2018-03-09 stsp
182 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
183 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
189 86c3caaf 2018-03-09 stsp goto done;
190 86c3caaf 2018-03-09 stsp }
191 86c3caaf 2018-03-09 stsp
192 056e7441 2018-03-11 stsp /* Create an empty lock file. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 056e7441 2018-03-11 stsp if (err)
195 056e7441 2018-03-11 stsp goto done;
196 056e7441 2018-03-11 stsp
197 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
198 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 99724ed4 2018-03-10 stsp if (err)
200 86c3caaf 2018-03-09 stsp goto done;
201 86c3caaf 2018-03-09 stsp
202 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
203 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
204 65596e15 2018-12-24 stsp if (err)
205 65596e15 2018-12-24 stsp goto done;
206 65596e15 2018-12-24 stsp
207 65596e15 2018-12-24 stsp /* Record our base commit. */
208 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
209 65596e15 2018-12-24 stsp if (err)
210 65596e15 2018-12-24 stsp goto done;
211 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 99724ed4 2018-03-10 stsp if (err)
213 86c3caaf 2018-03-09 stsp goto done;
214 86c3caaf 2018-03-09 stsp
215 1451e70d 2018-03-10 stsp /* Store path to repository. */
216 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
218 99724ed4 2018-03-10 stsp if (err)
219 86c3caaf 2018-03-09 stsp goto done;
220 86c3caaf 2018-03-09 stsp
221 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
222 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
224 ec22038e 2019-03-10 stsp if (err)
225 ec22038e 2019-03-10 stsp goto done;
226 ec22038e 2019-03-10 stsp
227 ec22038e 2019-03-10 stsp /* Generate UUID. */
228 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
229 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
230 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
231 ec22038e 2019-03-10 stsp goto done;
232 ec22038e 2019-03-10 stsp }
233 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
235 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
236 ec22038e 2019-03-10 stsp goto done;
237 ec22038e 2019-03-10 stsp }
238 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 577ec78f 2018-03-11 stsp if (err)
240 577ec78f 2018-03-11 stsp goto done;
241 577ec78f 2018-03-11 stsp
242 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
243 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 1451e70d 2018-03-10 stsp goto done;
246 1451e70d 2018-03-10 stsp }
247 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 99724ed4 2018-03-10 stsp if (err)
249 1451e70d 2018-03-10 stsp goto done;
250 1451e70d 2018-03-10 stsp
251 86c3caaf 2018-03-09 stsp done:
252 65596e15 2018-12-24 stsp free(commit_id);
253 7ac97322 2018-03-11 stsp free(path_got);
254 1451e70d 2018-03-10 stsp free(formatstr);
255 0bb8a95e 2018-03-12 stsp free(absprefix);
256 65596e15 2018-12-24 stsp free(basestr);
257 ec22038e 2019-03-10 stsp free(uuidstr);
258 86c3caaf 2018-03-09 stsp return err;
259 86c3caaf 2018-03-09 stsp }
260 86c3caaf 2018-03-09 stsp
261 247140b2 2019-02-05 stsp const struct got_error *
262 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 e5dc7198 2018-12-29 stsp const char *path_prefix)
264 e5dc7198 2018-12-29 stsp {
265 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
266 e5dc7198 2018-12-29 stsp
267 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
268 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
270 e5dc7198 2018-12-29 stsp }
271 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
272 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
273 e5dc7198 2018-12-29 stsp free(absprefix);
274 e5dc7198 2018-12-29 stsp return NULL;
275 86c3caaf 2018-03-09 stsp }
276 86c3caaf 2018-03-09 stsp
277 bc70eb79 2019-05-09 stsp const char *
278 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
279 86c3caaf 2018-03-09 stsp {
280 36a38700 2019-05-10 stsp return worktree->head_ref_name;
281 024e9686 2019-05-14 stsp }
282 024e9686 2019-05-14 stsp
283 024e9686 2019-05-14 stsp const struct got_error *
284 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
285 024e9686 2019-05-14 stsp struct got_reference *head_ref)
286 024e9686 2019-05-14 stsp {
287 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
288 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
289 024e9686 2019-05-14 stsp
290 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
292 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
293 024e9686 2019-05-14 stsp path_got = NULL;
294 024e9686 2019-05-14 stsp goto done;
295 024e9686 2019-05-14 stsp }
296 024e9686 2019-05-14 stsp
297 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
298 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
299 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
300 024e9686 2019-05-14 stsp goto done;
301 024e9686 2019-05-14 stsp }
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
304 024e9686 2019-05-14 stsp if (err)
305 024e9686 2019-05-14 stsp goto done;
306 024e9686 2019-05-14 stsp
307 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
308 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
309 024e9686 2019-05-14 stsp done:
310 024e9686 2019-05-14 stsp free(path_got);
311 024e9686 2019-05-14 stsp if (err)
312 024e9686 2019-05-14 stsp free(head_ref_name);
313 024e9686 2019-05-14 stsp return err;
314 507dc3bb 2018-12-29 stsp }
315 507dc3bb 2018-12-29 stsp
316 b72f483a 2019-02-05 stsp struct got_object_id *
317 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
318 507dc3bb 2018-12-29 stsp {
319 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 507dc3bb 2018-12-29 stsp const struct got_error *
323 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
325 507dc3bb 2018-12-29 stsp {
326 507dc3bb 2018-12-29 stsp const struct got_error *err;
327 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
328 507dc3bb 2018-12-29 stsp char *id_str = NULL;
329 507dc3bb 2018-12-29 stsp char *path_got = NULL;
330 507dc3bb 2018-12-29 stsp
331 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
333 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
334 507dc3bb 2018-12-29 stsp path_got = NULL;
335 507dc3bb 2018-12-29 stsp goto done;
336 507dc3bb 2018-12-29 stsp }
337 507dc3bb 2018-12-29 stsp
338 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
339 507dc3bb 2018-12-29 stsp if (err)
340 507dc3bb 2018-12-29 stsp return err;
341 507dc3bb 2018-12-29 stsp
342 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
344 507dc3bb 2018-12-29 stsp goto done;
345 507dc3bb 2018-12-29 stsp }
346 507dc3bb 2018-12-29 stsp
347 507dc3bb 2018-12-29 stsp /* Record our base commit. */
348 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
349 507dc3bb 2018-12-29 stsp if (err)
350 507dc3bb 2018-12-29 stsp goto done;
351 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 507dc3bb 2018-12-29 stsp if (err)
353 507dc3bb 2018-12-29 stsp goto done;
354 507dc3bb 2018-12-29 stsp
355 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
356 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
357 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp }
361 507dc3bb 2018-12-29 stsp done:
362 507dc3bb 2018-12-29 stsp if (obj)
363 507dc3bb 2018-12-29 stsp got_object_close(obj);
364 507dc3bb 2018-12-29 stsp free(id_str);
365 507dc3bb 2018-12-29 stsp free(path_got);
366 507dc3bb 2018-12-29 stsp return err;
367 50b0790e 2020-09-11 stsp }
368 50b0790e 2020-09-11 stsp
369 50b0790e 2020-09-11 stsp const struct got_gotconfig *
370 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
371 50b0790e 2020-09-11 stsp {
372 50b0790e 2020-09-11 stsp return worktree->gotconfig;
373 507dc3bb 2018-12-29 stsp }
374 507dc3bb 2018-12-29 stsp
375 9d31a1d8 2018-03-11 stsp static const struct got_error *
376 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
377 86c3caaf 2018-03-09 stsp {
378 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
381 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
382 86c3caaf 2018-03-09 stsp return NULL;
383 21908da4 2019-01-13 stsp }
384 21908da4 2019-01-13 stsp
385 21908da4 2019-01-13 stsp static const struct got_error *
386 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
387 4a1ddfc2 2019-01-12 stsp {
388 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
389 4a1ddfc2 2019-01-12 stsp char *abspath;
390 4a1ddfc2 2019-01-12 stsp
391 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
393 4a1ddfc2 2019-01-12 stsp
394 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
395 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 ddcd8544 2019-03-11 stsp struct stat sb;
397 ddcd8544 2019-03-11 stsp err = NULL;
398 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
400 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
401 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
402 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 ddcd8544 2019-03-11 stsp }
404 ddcd8544 2019-03-11 stsp }
405 4a1ddfc2 2019-01-12 stsp free(abspath);
406 68c76935 2019-02-19 stsp return err;
407 68c76935 2019-02-19 stsp }
408 68c76935 2019-02-19 stsp
409 68c76935 2019-02-19 stsp static const struct got_error *
410 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
411 68c76935 2019-02-19 stsp {
412 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
413 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
414 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
415 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
416 68c76935 2019-02-19 stsp
417 68c76935 2019-02-19 stsp *same = 1;
418 68c76935 2019-02-19 stsp
419 230a42bd 2019-05-11 jcs for (;;) {
420 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
423 68c76935 2019-02-19 stsp break;
424 68c76935 2019-02-19 stsp }
425 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
427 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
428 68c76935 2019-02-19 stsp break;
429 68c76935 2019-02-19 stsp }
430 68c76935 2019-02-19 stsp if (flen1 == 0) {
431 68c76935 2019-02-19 stsp if (flen2 != 0)
432 68c76935 2019-02-19 stsp *same = 0;
433 68c76935 2019-02-19 stsp break;
434 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
435 68c76935 2019-02-19 stsp if (flen1 != 0)
436 68c76935 2019-02-19 stsp *same = 0;
437 68c76935 2019-02-19 stsp break;
438 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
439 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 68c76935 2019-02-19 stsp *same = 0;
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp } else {
444 68c76935 2019-02-19 stsp *same = 0;
445 68c76935 2019-02-19 stsp break;
446 68c76935 2019-02-19 stsp }
447 68c76935 2019-02-19 stsp }
448 68c76935 2019-02-19 stsp
449 68c76935 2019-02-19 stsp return err;
450 68c76935 2019-02-19 stsp }
451 68c76935 2019-02-19 stsp
452 68c76935 2019-02-19 stsp static const struct got_error *
453 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
454 68c76935 2019-02-19 stsp {
455 68c76935 2019-02-19 stsp struct stat sb;
456 68c76935 2019-02-19 stsp size_t size1, size2;
457 68c76935 2019-02-19 stsp
458 68c76935 2019-02-19 stsp *same = 1;
459 68c76935 2019-02-19 stsp
460 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size1 = sb.st_size;
463 68c76935 2019-02-19 stsp
464 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
465 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
466 68c76935 2019-02-19 stsp size2 = sb.st_size;
467 68c76935 2019-02-19 stsp
468 68c76935 2019-02-19 stsp if (size1 != size2) {
469 68c76935 2019-02-19 stsp *same = 0;
470 68c76935 2019-02-19 stsp return NULL;
471 68c76935 2019-02-19 stsp }
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
475 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
476 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
477 68c76935 2019-02-19 stsp
478 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
479 0e039681 2021-11-15 stsp }
480 0e039681 2021-11-15 stsp
481 0e039681 2021-11-15 stsp static const struct got_error *
482 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
483 0e039681 2021-11-15 stsp {
484 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
485 0e039681 2021-11-15 stsp size_t flen;
486 0e039681 2021-11-15 stsp ssize_t outlen;
487 0e039681 2021-11-15 stsp
488 0e039681 2021-11-15 stsp *outsize = 0;
489 0e039681 2021-11-15 stsp
490 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
491 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
492 0e039681 2021-11-15 stsp
493 0e039681 2021-11-15 stsp for (;;) {
494 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
495 0e039681 2021-11-15 stsp if (flen == 0) {
496 0e039681 2021-11-15 stsp if (ferror(f))
497 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
498 0e039681 2021-11-15 stsp if (feof(f))
499 0e039681 2021-11-15 stsp break;
500 0e039681 2021-11-15 stsp }
501 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
502 0e039681 2021-11-15 stsp if (outlen == -1)
503 0e039681 2021-11-15 stsp return got_error_from_errno("write");
504 0e039681 2021-11-15 stsp if (outlen != flen)
505 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
506 0e039681 2021-11-15 stsp *outsize += outlen;
507 0e039681 2021-11-15 stsp }
508 0e039681 2021-11-15 stsp
509 0e039681 2021-11-15 stsp return NULL;
510 0e039681 2021-11-15 stsp }
511 0e039681 2021-11-15 stsp
512 0e039681 2021-11-15 stsp static const struct got_error *
513 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
514 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 0e039681 2021-11-15 stsp const char *ondisk_path)
517 0e039681 2021-11-15 stsp {
518 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
519 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
520 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
524 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
525 0e039681 2021-11-15 stsp
526 0e039681 2021-11-15 stsp *overlapcnt = 0;
527 0e039681 2021-11-15 stsp
528 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 0e039681 2021-11-15 stsp if (err)
530 0e039681 2021-11-15 stsp return err;
531 0e039681 2021-11-15 stsp
532 0e039681 2021-11-15 stsp if (same_content)
533 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
534 0e039681 2021-11-15 stsp
535 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
536 0e039681 2021-11-15 stsp if (err)
537 0e039681 2021-11-15 stsp return err;
538 0e039681 2021-11-15 stsp changed_deriv = !same_content;
539 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
540 0e039681 2021-11-15 stsp if (err)
541 0e039681 2021-11-15 stsp return err;
542 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
543 0e039681 2021-11-15 stsp
544 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
545 0e039681 2021-11-15 stsp *overlapcnt = 1;
546 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
548 0e039681 2021-11-15 stsp goto done;
549 0e039681 2021-11-15 stsp }
550 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
552 0e039681 2021-11-15 stsp goto done;
553 0e039681 2021-11-15 stsp }
554 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
556 0e039681 2021-11-15 stsp goto done;
557 0e039681 2021-11-15 stsp }
558 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 b90054ed 2022-11-01 stsp base_path_orig, "");
560 0e039681 2021-11-15 stsp if (err)
561 0e039681 2021-11-15 stsp goto done;
562 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 b90054ed 2022-11-01 stsp base_path_deriv, "");
564 0e039681 2021-11-15 stsp if (err)
565 0e039681 2021-11-15 stsp goto done;
566 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 b90054ed 2022-11-01 stsp base_path_deriv2, "");
568 0e039681 2021-11-15 stsp if (err)
569 0e039681 2021-11-15 stsp goto done;
570 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 0e039681 2021-11-15 stsp if (err)
572 0e039681 2021-11-15 stsp goto done;
573 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 0e039681 2021-11-15 stsp if (err)
575 0e039681 2021-11-15 stsp goto done;
576 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 0e039681 2021-11-15 stsp if (err)
578 0e039681 2021-11-15 stsp goto done;
579 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
580 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
581 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
582 0e039681 2021-11-15 stsp goto done;
583 0e039681 2021-11-15 stsp }
584 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 0e039681 2021-11-15 stsp label_deriv ? " " : "",
587 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
588 0e039681 2021-11-15 stsp path_deriv) < 0) {
589 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
590 0e039681 2021-11-15 stsp goto done;
591 0e039681 2021-11-15 stsp }
592 0e039681 2021-11-15 stsp if (size_orig > 0) {
593 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
595 0e039681 2021-11-15 stsp label_orig ? " " : "",
596 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
597 0e039681 2021-11-15 stsp path_orig) < 0) {
598 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
599 0e039681 2021-11-15 stsp goto done;
600 0e039681 2021-11-15 stsp }
601 0e039681 2021-11-15 stsp }
602 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
604 0e039681 2021-11-15 stsp path_deriv2,
605 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
606 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
607 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
608 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
609 0e039681 2021-11-15 stsp goto done;
610 0e039681 2021-11-15 stsp }
611 0e039681 2021-11-15 stsp } else if (changed_deriv)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 0e039681 2021-11-15 stsp else if (changed_deriv2)
614 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 0e039681 2021-11-15 stsp done:
616 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 0e039681 2021-11-15 stsp err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
619 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
621 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
623 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
625 0e039681 2021-11-15 stsp free(path_orig);
626 0e039681 2021-11-15 stsp free(path_deriv);
627 0e039681 2021-11-15 stsp free(path_deriv2);
628 0e039681 2021-11-15 stsp free(base_path_orig);
629 0e039681 2021-11-15 stsp free(base_path_deriv);
630 0e039681 2021-11-15 stsp free(base_path_deriv2);
631 0e039681 2021-11-15 stsp return err;
632 6353ad76 2019-02-08 stsp }
633 6353ad76 2019-02-08 stsp
634 6353ad76 2019-02-08 stsp /*
635 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
636 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
637 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
638 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
639 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
640 6353ad76 2019-02-08 stsp */
641 6353ad76 2019-02-08 stsp static const struct got_error *
642 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
645 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
648 6353ad76 2019-02-08 stsp {
649 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
650 6353ad76 2019-02-08 stsp int merged_fd = -1;
651 db590691 2021-06-02 stsp FILE *f_merged = NULL;
652 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
653 234035bc 2019-06-01 stsp int overlapcnt = 0;
654 3524bbf9 2020-10-20 stsp char *parent = NULL;
655 6353ad76 2019-02-08 stsp
656 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
657 234035bc 2019-06-01 stsp
658 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
659 3524bbf9 2020-10-20 stsp if (err)
660 3524bbf9 2020-10-20 stsp return err;
661 af54ae4a 2019-02-19 stsp
662 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
664 3524bbf9 2020-10-20 stsp goto done;
665 3524bbf9 2020-10-20 stsp }
666 af54ae4a 2019-02-19 stsp
667 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 6353ad76 2019-02-08 stsp if (err)
669 6353ad76 2019-02-08 stsp goto done;
670 3b9f0f87 2020-07-23 stsp
671 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 0e039681 2021-11-15 stsp if (err) {
674 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
675 0e039681 2021-11-15 stsp goto done;
676 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 0e039681 2021-11-15 stsp ondisk_path);
679 0e039681 2021-11-15 stsp if (err)
680 0e039681 2021-11-15 stsp goto done;
681 0e039681 2021-11-15 stsp }
682 6353ad76 2019-02-08 stsp
683 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
684 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 1ee397ad 2019-07-12 stsp if (err)
686 1ee397ad 2019-07-12 stsp goto done;
687 6353ad76 2019-02-08 stsp
688 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
690 816dc654 2019-02-16 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp
693 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
694 db590691 2021-06-02 stsp if (f_merged == NULL) {
695 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
696 db590691 2021-06-02 stsp goto done;
697 db590691 2021-06-02 stsp }
698 db590691 2021-06-02 stsp merged_fd = -1;
699 db590691 2021-06-02 stsp
700 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
701 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
702 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
703 db590691 2021-06-02 stsp f_merged);
704 68c76935 2019-02-19 stsp if (err)
705 68c76935 2019-02-19 stsp goto done;
706 816dc654 2019-02-16 stsp }
707 6353ad76 2019-02-08 stsp
708 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
710 70a0c8ec 2019-02-20 stsp goto done;
711 70a0c8ec 2019-02-20 stsp }
712 70a0c8ec 2019-02-20 stsp
713 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
714 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
715 230a42bd 2019-05-11 jcs ondisk_path);
716 6353ad76 2019-02-08 stsp goto done;
717 6353ad76 2019-02-08 stsp }
718 6353ad76 2019-02-08 stsp done:
719 fdcb7daf 2019-12-15 stsp if (err) {
720 fdcb7daf 2019-12-15 stsp if (merged_path)
721 fdcb7daf 2019-12-15 stsp unlink(merged_path);
722 3b9f0f87 2020-07-23 stsp }
723 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
725 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
727 6353ad76 2019-02-08 stsp free(merged_path);
728 af54ae4a 2019-02-19 stsp free(base_path);
729 3524bbf9 2020-10-20 stsp free(parent);
730 af57b12a 2020-07-23 stsp return err;
731 af57b12a 2020-07-23 stsp }
732 af57b12a 2020-07-23 stsp
733 af57b12a 2020-07-23 stsp static const struct got_error *
734 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
735 af57b12a 2020-07-23 stsp size_t target_len)
736 af57b12a 2020-07-23 stsp {
737 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
738 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
740 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
741 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
742 af57b12a 2020-07-23 stsp ondisk_path);
743 af57b12a 2020-07-23 stsp return NULL;
744 af57b12a 2020-07-23 stsp }
745 af57b12a 2020-07-23 stsp
746 af57b12a 2020-07-23 stsp /*
747 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
749 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
750 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
751 993e2a1b 2020-07-23 stsp *
752 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
753 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
754 993e2a1b 2020-07-23 stsp *
755 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
756 993e2a1b 2020-07-23 stsp * has deleted this symlink.
757 11cc08c1 2020-07-23 stsp */
758 11cc08c1 2020-07-23 stsp static const struct got_error *
759 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
760 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
762 11cc08c1 2020-07-23 stsp {
763 11cc08c1 2020-07-23 stsp const struct got_error *err;
764 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 11cc08c1 2020-07-23 stsp FILE *f = NULL;
766 11cc08c1 2020-07-23 stsp
767 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
768 11cc08c1 2020-07-23 stsp if (err)
769 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
770 11cc08c1 2020-07-23 stsp
771 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
772 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
774 11cc08c1 2020-07-23 stsp goto done;
775 11cc08c1 2020-07-23 stsp }
776 11cc08c1 2020-07-23 stsp
777 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 11cc08c1 2020-07-23 stsp if (err)
779 3818e3c4 2020-11-01 naddy goto done;
780 3818e3c4 2020-11-01 naddy
781 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
783 11cc08c1 2020-07-23 stsp goto done;
784 3818e3c4 2020-11-01 naddy }
785 11cc08c1 2020-07-23 stsp
786 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
789 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
792 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
793 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
794 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
796 11cc08c1 2020-07-23 stsp goto done;
797 11cc08c1 2020-07-23 stsp }
798 11cc08c1 2020-07-23 stsp
799 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
801 11cc08c1 2020-07-23 stsp goto done;
802 11cc08c1 2020-07-23 stsp }
803 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
805 11cc08c1 2020-07-23 stsp goto done;
806 11cc08c1 2020-07-23 stsp }
807 11cc08c1 2020-07-23 stsp done:
808 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
809 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
810 11cc08c1 2020-07-23 stsp free(path);
811 11cc08c1 2020-07-23 stsp free(id_str);
812 11cc08c1 2020-07-23 stsp free(label_deriv);
813 11cc08c1 2020-07-23 stsp return err;
814 11cc08c1 2020-07-23 stsp }
815 d219f183 2020-07-23 stsp
816 d219f183 2020-07-23 stsp /* forward declaration */
817 d219f183 2020-07-23 stsp static const struct got_error *
818 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
820 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
821 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
822 11cc08c1 2020-07-23 stsp
823 11cc08c1 2020-07-23 stsp /*
824 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
825 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
826 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
827 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
828 af57b12a 2020-07-23 stsp */
829 af57b12a 2020-07-23 stsp static const struct got_error *
830 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
831 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
832 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
833 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
835 af57b12a 2020-07-23 stsp {
836 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
837 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
838 af57b12a 2020-07-23 stsp struct stat sb;
839 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
840 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
841 11cc08c1 2020-07-23 stsp int have_local_change = 0;
842 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
843 af57b12a 2020-07-23 stsp
844 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
845 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
846 af57b12a 2020-07-23 stsp
847 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
848 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
849 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
850 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
851 af57b12a 2020-07-23 stsp ondisk_path);
852 af57b12a 2020-07-23 stsp goto done;
853 af57b12a 2020-07-23 stsp }
854 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
855 af57b12a 2020-07-23 stsp
856 526a746f 2020-07-23 stsp if (blob_orig) {
857 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 526a746f 2020-07-23 stsp if (err)
859 526a746f 2020-07-23 stsp goto done;
860 526a746f 2020-07-23 stsp }
861 af57b12a 2020-07-23 stsp
862 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
863 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
864 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 11cc08c1 2020-07-23 stsp have_local_change = 1;
866 11cc08c1 2020-07-23 stsp
867 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
868 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
869 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
870 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
872 11cc08c1 2020-07-23 stsp
873 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
874 11cc08c1 2020-07-23 stsp if (ancestor_target) {
875 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
876 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 11cc08c1 2020-07-23 stsp path);
878 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
879 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
881 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 11cc08c1 2020-07-23 stsp path);
883 11cc08c1 2020-07-23 stsp } else {
884 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
885 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
886 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
887 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
888 11cc08c1 2020-07-23 stsp if (err)
889 11cc08c1 2020-07-23 stsp goto done;
890 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 11cc08c1 2020-07-23 stsp path);
892 11cc08c1 2020-07-23 stsp }
893 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
894 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
895 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
896 af57b12a 2020-07-23 stsp strlen(deriv_target));
897 af57b12a 2020-07-23 stsp if (err)
898 af57b12a 2020-07-23 stsp goto done;
899 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
901 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
902 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
904 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 ea7786be 2020-07-23 stsp path);
906 ea7786be 2020-07-23 stsp } else {
907 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
908 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
909 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
910 ea7786be 2020-07-23 stsp if (err)
911 ea7786be 2020-07-23 stsp goto done;
912 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 ea7786be 2020-07-23 stsp path);
914 ea7786be 2020-07-23 stsp }
915 6353ad76 2019-02-08 stsp }
916 11cc08c1 2020-07-23 stsp
917 af57b12a 2020-07-23 stsp done:
918 af57b12a 2020-07-23 stsp free(ancestor_target);
919 eec2f5a9 2021-06-03 stsp return err;
920 eec2f5a9 2021-06-03 stsp }
921 eec2f5a9 2021-06-03 stsp
922 eec2f5a9 2021-06-03 stsp static const struct got_error *
923 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
924 eec2f5a9 2021-06-03 stsp {
925 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
926 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
927 eec2f5a9 2021-06-03 stsp ssize_t target_len;
928 eec2f5a9 2021-06-03 stsp size_t n;
929 eec2f5a9 2021-06-03 stsp FILE *f;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp *outfile = NULL;
932 eec2f5a9 2021-06-03 stsp
933 eec2f5a9 2021-06-03 stsp f = got_opentemp();
934 eec2f5a9 2021-06-03 stsp if (f == NULL)
935 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
936 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
938 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
939 eec2f5a9 2021-06-03 stsp goto done;
940 eec2f5a9 2021-06-03 stsp }
941 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
942 eec2f5a9 2021-06-03 stsp if (n != target_len) {
943 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
944 eec2f5a9 2021-06-03 stsp goto done;
945 eec2f5a9 2021-06-03 stsp }
946 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
947 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
948 eec2f5a9 2021-06-03 stsp goto done;
949 eec2f5a9 2021-06-03 stsp }
950 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
952 eec2f5a9 2021-06-03 stsp goto done;
953 eec2f5a9 2021-06-03 stsp }
954 eec2f5a9 2021-06-03 stsp done:
955 eec2f5a9 2021-06-03 stsp if (err)
956 eec2f5a9 2021-06-03 stsp fclose(f);
957 eec2f5a9 2021-06-03 stsp else
958 eec2f5a9 2021-06-03 stsp *outfile = f;
959 14c901f1 2019-08-08 stsp return err;
960 14c901f1 2019-08-08 stsp }
961 14c901f1 2019-08-08 stsp
962 14c901f1 2019-08-08 stsp /*
963 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
965 14c901f1 2019-08-08 stsp * acts as the second derived version.
966 14c901f1 2019-08-08 stsp */
967 14c901f1 2019-08-08 stsp static const struct got_error *
968 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
970 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
971 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
972 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
974 14c901f1 2019-08-08 stsp {
975 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
976 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
978 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
980 14c901f1 2019-08-08 stsp
981 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
982 14c901f1 2019-08-08 stsp
983 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
984 ed6b5030 2020-10-20 stsp if (err)
985 ed6b5030 2020-10-20 stsp return err;
986 14c901f1 2019-08-08 stsp
987 67a66647 2021-05-31 stsp if (blob_orig) {
988 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 67a66647 2021-05-31 stsp parent) == -1) {
990 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
991 67a66647 2021-05-31 stsp base_path = NULL;
992 67a66647 2021-05-31 stsp goto done;
993 67a66647 2021-05-31 stsp }
994 67a66647 2021-05-31 stsp
995 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
996 b90054ed 2022-11-01 stsp base_path, "");
997 67a66647 2021-05-31 stsp if (err)
998 67a66647 2021-05-31 stsp goto done;
999 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 67a66647 2021-05-31 stsp blob_orig);
1001 67a66647 2021-05-31 stsp if (err)
1002 67a66647 2021-05-31 stsp goto done;
1003 67a66647 2021-05-31 stsp free(base_path);
1004 db590691 2021-06-02 stsp } else {
1005 db590691 2021-06-02 stsp /*
1006 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1007 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1008 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1009 db590691 2021-06-02 stsp */
1010 db590691 2021-06-02 stsp f_orig = got_opentemp();
1011 db590691 2021-06-02 stsp if (f_orig == NULL) {
1012 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1013 db590691 2021-06-02 stsp goto done;
1014 db590691 2021-06-02 stsp }
1015 67a66647 2021-05-31 stsp }
1016 67a66647 2021-05-31 stsp
1017 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1019 14c901f1 2019-08-08 stsp base_path = NULL;
1020 14c901f1 2019-08-08 stsp goto done;
1021 14c901f1 2019-08-08 stsp }
1022 14c901f1 2019-08-08 stsp
1023 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 14c901f1 2019-08-08 stsp if (err)
1025 14c901f1 2019-08-08 stsp goto done;
1026 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 14c901f1 2019-08-08 stsp blob_deriv);
1028 14c901f1 2019-08-08 stsp if (err)
1029 14c901f1 2019-08-08 stsp goto done;
1030 14c901f1 2019-08-08 stsp
1031 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 14c901f1 2019-08-08 stsp if (err)
1033 14c901f1 2019-08-08 stsp goto done;
1034 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1035 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1037 14c901f1 2019-08-08 stsp goto done;
1038 eec2f5a9 2021-06-03 stsp }
1039 eec2f5a9 2021-06-03 stsp
1040 5e91dae4 2022-08-30 stsp /*
1041 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1042 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1043 eec2f5a9 2021-06-03 stsp */
1044 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1045 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 eec2f5a9 2021-06-03 stsp if (err)
1047 eec2f5a9 2021-06-03 stsp goto done;
1048 eec2f5a9 2021-06-03 stsp } else {
1049 eec2f5a9 2021-06-03 stsp int fd;
1050 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp }
1055 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1056 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1057 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1058 eec2f5a9 2021-06-03 stsp close(fd);
1059 eec2f5a9 2021-06-03 stsp goto done;
1060 eec2f5a9 2021-06-03 stsp }
1061 14c901f1 2019-08-08 stsp }
1062 14c901f1 2019-08-08 stsp
1063 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 14c901f1 2019-08-08 stsp done:
1067 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1069 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1071 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1073 14c901f1 2019-08-08 stsp free(base_path);
1074 67a66647 2021-05-31 stsp if (blob_orig_path) {
1075 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1076 67a66647 2021-05-31 stsp free(blob_orig_path);
1077 67a66647 2021-05-31 stsp }
1078 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1079 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1080 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1081 14c901f1 2019-08-08 stsp }
1082 6353ad76 2019-02-08 stsp free(id_str);
1083 818c7501 2019-07-11 stsp free(label_deriv);
1084 ed6b5030 2020-10-20 stsp free(parent);
1085 4a1ddfc2 2019-01-12 stsp return err;
1086 4a1ddfc2 2019-01-12 stsp }
1087 4a1ddfc2 2019-01-12 stsp
1088 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1089 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1092 13d9040b 2019-03-27 stsp {
1093 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1094 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1095 13d9040b 2019-03-27 stsp
1096 65b05cec 2020-07-23 stsp *new_iep = NULL;
1097 65b05cec 2020-07-23 stsp
1098 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1099 054041d0 2020-06-24 stsp if (err)
1100 054041d0 2020-06-24 stsp return err;
1101 054041d0 2020-06-24 stsp
1102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1104 054041d0 2020-06-24 stsp if (err)
1105 054041d0 2020-06-24 stsp goto done;
1106 054041d0 2020-06-24 stsp
1107 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1108 054041d0 2020-06-24 stsp done:
1109 054041d0 2020-06-24 stsp if (err)
1110 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1111 65b05cec 2020-07-23 stsp else
1112 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1113 13d9040b 2019-03-27 stsp return err;
1114 1ebedb77 2019-10-19 stsp }
1115 1ebedb77 2019-10-19 stsp
1116 1ebedb77 2019-10-19 stsp static mode_t
1117 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1118 1ebedb77 2019-10-19 stsp {
1119 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1120 1ebedb77 2019-10-19 stsp
1121 1ebedb77 2019-10-19 stsp if (executable) {
1122 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1125 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1126 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1127 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1128 1ebedb77 2019-10-19 stsp }
1129 1ebedb77 2019-10-19 stsp
1130 b2b3fce1 2022-10-29 op return st_mode;
1131 b2b3fce1 2022-10-29 op }
1132 b2b3fce1 2022-10-29 op
1133 b2b3fce1 2022-10-29 op static mode_t
1134 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1135 b2b3fce1 2022-10-29 op {
1136 b2b3fce1 2022-10-29 op mode_t um;
1137 b2b3fce1 2022-10-29 op
1138 b2b3fce1 2022-10-29 op um = umask(000);
1139 b2b3fce1 2022-10-29 op umask(um);
1140 b2b3fce1 2022-10-29 op return mode & ~um;
1141 13d9040b 2019-03-27 stsp }
1142 13d9040b 2019-03-27 stsp
1143 8ba819a3 2020-07-23 stsp /* forward declaration */
1144 13d9040b 2019-03-27 stsp static const struct got_error *
1145 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1147 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1148 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1149 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1150 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 8ba819a3 2020-07-23 stsp
1152 5a1fbc73 2020-07-23 stsp /*
1153 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1154 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1155 5a1fbc73 2020-07-23 stsp */
1156 8ba819a3 2020-07-23 stsp static const struct got_error *
1157 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1159 5a1fbc73 2020-07-23 stsp {
1160 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1161 5a1fbc73 2020-07-23 stsp ssize_t elen;
1162 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1163 5a1fbc73 2020-07-23 stsp int fd;
1164 5a1fbc73 2020-07-23 stsp
1165 c6e8a826 2021-04-05 stsp *did_something = 0;
1166 c6e8a826 2021-04-05 stsp
1167 5a1fbc73 2020-07-23 stsp /*
1168 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1169 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1170 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1171 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1172 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1173 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1174 5a1fbc73 2020-07-23 stsp */
1175 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1177 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1178 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp
1180 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1181 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 5a1fbc73 2020-07-23 stsp if (elen == -1)
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1184 5a1fbc73 2020-07-23 stsp
1185 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1186 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1187 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1188 5a1fbc73 2020-07-23 stsp }
1189 5a1fbc73 2020-07-23 stsp
1190 c6e8a826 2021-04-05 stsp *did_something = 1;
1191 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1192 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1193 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1194 5a1fbc73 2020-07-23 stsp return err;
1195 3c1ec782 2020-07-23 stsp }
1196 3c1ec782 2020-07-23 stsp
1197 3c1ec782 2020-07-23 stsp static const struct got_error *
1198 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 3c1ec782 2020-07-23 stsp {
1201 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1202 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1203 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1206 3c1ec782 2020-07-23 stsp
1207 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1209 3c1ec782 2020-07-23 stsp return NULL;
1210 3c1ec782 2020-07-23 stsp }
1211 3c1ec782 2020-07-23 stsp
1212 3c1ec782 2020-07-23 stsp /*
1213 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1214 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1215 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1216 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1217 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1218 3c1ec782 2020-07-23 stsp */
1219 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1220 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1221 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1222 ce031e9e 2020-10-20 stsp if (err)
1223 ce031e9e 2020-10-20 stsp return err;
1224 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 ce031e9e 2020-10-20 stsp free(parent);
1226 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1227 ce031e9e 2020-10-20 stsp }
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1230 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 3c1ec782 2020-07-23 stsp free(abspath);
1232 3c1ec782 2020-07-23 stsp return err;
1233 3c1ec782 2020-07-23 stsp }
1234 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 3c1ec782 2020-07-23 stsp free(abspath);
1236 3c1ec782 2020-07-23 stsp if (err)
1237 3c1ec782 2020-07-23 stsp return err;
1238 3c1ec782 2020-07-23 stsp } else {
1239 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 3c1ec782 2020-07-23 stsp if (err)
1241 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp }
1243 3c1ec782 2020-07-23 stsp
1244 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1245 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1247 3c1ec782 2020-07-23 stsp return NULL;
1248 3c1ec782 2020-07-23 stsp }
1249 3c1ec782 2020-07-23 stsp
1250 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1251 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1253 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1254 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp
1257 3c1ec782 2020-07-23 stsp free(path_got);
1258 3c1ec782 2020-07-23 stsp return NULL;
1259 5a1fbc73 2020-07-23 stsp }
1260 5a1fbc73 2020-07-23 stsp
1261 5a1fbc73 2020-07-23 stsp static const struct got_error *
1262 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1265 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1266 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1267 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 9d31a1d8 2018-03-11 stsp {
1269 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1270 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1271 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1272 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 8ba819a3 2020-07-23 stsp
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1276 2e1fa222 2020-07-23 stsp
1277 3c29341b 2022-07-21 florian /*
1278 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1279 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1280 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1281 8ba819a3 2020-07-23 stsp * in the blob object.
1282 8ba819a3 2020-07-23 stsp */
1283 8ba819a3 2020-07-23 stsp do {
1284 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1285 538b6881 2022-07-21 florian if (err)
1286 538b6881 2022-07-21 florian return err;
1287 538b6881 2022-07-21 florian
1288 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1289 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1290 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1291 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1292 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1293 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1295 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1296 3b9f0f87 2020-07-23 stsp progress_arg);
1297 8ba819a3 2020-07-23 stsp }
1298 8ba819a3 2020-07-23 stsp if (len > 0) {
1299 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1300 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1301 8ba819a3 2020-07-23 stsp len - hdrlen);
1302 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1303 8ba819a3 2020-07-23 stsp hdrlen = 0;
1304 8ba819a3 2020-07-23 stsp }
1305 8ba819a3 2020-07-23 stsp } while (len != 0);
1306 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1307 8ba819a3 2020-07-23 stsp
1308 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1310 3c1ec782 2020-07-23 stsp if (err)
1311 3c1ec782 2020-07-23 stsp return err;
1312 8ba819a3 2020-07-23 stsp
1313 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1314 906c123b 2020-07-23 stsp /* install as a regular file */
1315 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1316 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1317 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1319 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1320 3c29341b 2022-07-21 florian return err;
1321 906c123b 2020-07-23 stsp }
1322 906c123b 2020-07-23 stsp
1323 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1324 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1325 c6e8a826 2021-04-05 stsp int symlink_replaced;
1326 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1327 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1328 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1329 3c29341b 2022-07-21 florian return err;
1330 c90c8ce3 2020-07-23 stsp }
1331 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1332 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1333 5a1fbc73 2020-07-23 stsp if (err)
1334 3c29341b 2022-07-21 florian return err;
1335 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1336 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1337 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1338 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1341 c6e8a826 2021-04-05 stsp } else {
1342 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1344 c6e8a826 2021-04-05 stsp }
1345 f35fa46a 2020-07-23 stsp }
1346 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1347 f35fa46a 2020-07-23 stsp }
1348 f35fa46a 2020-07-23 stsp
1349 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1350 f4994adc 2020-10-20 stsp char *parent;
1351 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1352 f4994adc 2020-10-20 stsp if (err)
1353 3c29341b 2022-07-21 florian return err;
1354 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1355 f4994adc 2020-10-20 stsp free(parent);
1356 f35fa46a 2020-07-23 stsp if (err)
1357 3c29341b 2022-07-21 florian return err;
1358 f35fa46a 2020-07-23 stsp /*
1359 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1360 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1361 f35fa46a 2020-07-23 stsp */
1362 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1363 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1364 3c29341b 2022-07-21 florian return err;
1365 f35fa46a 2020-07-23 stsp }
1366 f35fa46a 2020-07-23 stsp }
1367 f35fa46a 2020-07-23 stsp
1368 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1369 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1370 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1371 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1372 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1373 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1374 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1376 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1377 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1378 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1379 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1380 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1381 8ba819a3 2020-07-23 stsp } else {
1382 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1383 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1384 8ba819a3 2020-07-23 stsp }
1385 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1386 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 8ba819a3 2020-07-23 stsp return err;
1389 8ba819a3 2020-07-23 stsp }
1390 8ba819a3 2020-07-23 stsp
1391 8ba819a3 2020-07-23 stsp static const struct got_error *
1392 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1394 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1395 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1396 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1397 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 8ba819a3 2020-07-23 stsp {
1399 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1400 507dc3bb 2018-12-29 stsp int fd = -1;
1401 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1402 507dc3bb 2018-12-29 stsp int update = 0;
1403 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1404 b2b3fce1 2022-10-29 op mode_t mode;
1405 8ba819a3 2020-07-23 stsp
1406 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1409 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1410 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1411 f5375317 2020-10-20 stsp char *parent;
1412 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1413 f5375317 2020-10-20 stsp if (err)
1414 f5375317 2020-10-20 stsp return err;
1415 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
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 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 b2b3fce1 2022-10-29 op 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 b90054ed 2022-11-01 stsp 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 b2b3fce1 2022-10-29 op
1444 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1445 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1446 b2b3fce1 2022-10-29 op tmppath);
1447 b2b3fce1 2022-10-29 op goto done;
1448 b2b3fce1 2022-10-29 op }
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 12383673 2023-02-18 mark /*
1517 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1519 12383673 2023-02-18 mark */
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1522 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1523 12383673 2023-02-18 mark FILE *ondisk_file)
1524 7154f6ce 2019-03-27 stsp {
1525 d952957d 2023-02-19 mark 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 d952957d 2023-02-19 mark FILE *f1 = NULL;
1532 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1533 d952957d 2023-02-19 mark struct diff_result *r;
1534 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1535 9bdd68dd 2021-01-02 naddy char *line = NULL;
1536 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1537 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1538 7154f6ce 2019-03-27 stsp
1539 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1540 d952957d 2023-02-19 mark return NULL;
1541 12383673 2023-02-18 mark
1542 d952957d 2023-02-19 mark f1 = got_opentemp();
1543 d952957d 2023-02-19 mark if (f1 == NULL)
1544 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1545 12383673 2023-02-18 mark
1546 d952957d 2023-02-19 mark if (blob) {
1547 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1548 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 12383673 2023-02-18 mark if (err)
1550 12383673 2023-02-18 mark goto done;
1551 d952957d 2023-02-19 mark }
1552 12383673 2023-02-18 mark
1553 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 d952957d 2023-02-19 mark if (err)
1556 d952957d 2023-02-19 mark goto done;
1557 d952957d 2023-02-19 mark
1558 d952957d 2023-02-19 mark r = diffreg_result->result;
1559 d952957d 2023-02-19 mark
1560 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 d952957d 2023-02-19 mark struct diff_chunk *c;
1562 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1563 7783f73c 2023-02-20 mark off_t pos;
1564 d952957d 2023-02-19 mark
1565 d952957d 2023-02-19 mark /*
1566 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1567 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1568 d952957d 2023-02-19 mark */
1569 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1570 d952957d 2023-02-19 mark
1571 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 d952957d 2023-02-19 mark nchunks_parsed = 1;
1573 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1574 7154f6ce 2019-03-27 stsp }
1575 7154f6ce 2019-03-27 stsp
1576 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1577 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 7783f73c 2023-02-20 mark goto done;
1580 7154f6ce 2019-03-27 stsp }
1581 d952957d 2023-02-19 mark
1582 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 7783f73c 2023-02-20 mark ln = cc.right.start;
1584 7783f73c 2023-02-20 mark
1585 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1586 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1587 d952957d 2023-02-19 mark if (linelen == -1) {
1588 d952957d 2023-02-19 mark if (feof(ondisk_file))
1589 d952957d 2023-02-19 mark break;
1590 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 d952957d 2023-02-19 mark break;
1592 d952957d 2023-02-19 mark }
1593 d952957d 2023-02-19 mark
1594 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1595 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1596 d952957d 2023-02-19 mark if (strcmp(markers[i],
1597 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1599 d952957d 2023-02-19 mark goto done;
1600 d952957d 2023-02-19 mark } else
1601 d952957d 2023-02-19 mark i++;
1602 d952957d 2023-02-19 mark }
1603 d952957d 2023-02-19 mark ++ln;
1604 d952957d 2023-02-19 mark }
1605 7154f6ce 2019-03-27 stsp }
1606 12383673 2023-02-18 mark
1607 12383673 2023-02-18 mark done:
1608 9bdd68dd 2021-01-02 naddy free(line);
1609 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1611 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1612 d952957d 2023-02-19 mark if (err == NULL)
1613 d952957d 2023-02-19 mark 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 eb81bc23 2022-06-28 tracey 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 2c4740ad 2023-02-12 mark unsigned char staged_status;
1718 6353ad76 2019-02-08 stsp
1719 2c4740ad 2023-02-12 mark 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 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1743 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1744 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1745 5c02d2a5 2021-09-26 stsp 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 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp else
1788 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1789 c363b2c1 2019-08-03 stsp
1790 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1791 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1792 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1793 eb81bc23 2022-06-28 tracey goto done;
1794 eb81bc23 2022-06-28 tracey }
1795 eb81bc23 2022-06-28 tracey 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 e7ae0baf 2021-12-31 stsp 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 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1855 12383673 2023-02-18 mark 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 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 eb81bc23 2022-06-28 tracey 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 9d31a1d8 2018-03-11 stsp static const struct got_error *
1886 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1887 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1889 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 0584f854 2019-04-06 stsp void *progress_arg)
1891 9d31a1d8 2018-03-11 stsp {
1892 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1893 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1894 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1895 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1896 b8f41171 2019-02-10 stsp struct stat sb;
1897 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1898 9d31a1d8 2018-03-11 stsp
1899 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1901 6353ad76 2019-02-08 stsp
1902 abb4604f 2019-07-27 stsp if (ie) {
1903 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 a76c42e6 2019-08-03 stsp goto done;
1906 a76c42e6 2019-08-03 stsp }
1907 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1908 7f91a133 2019-12-13 stsp repo);
1909 abb4604f 2019-07-27 stsp if (err)
1910 abb4604f 2019-07-27 stsp goto done;
1911 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1913 c90c8ce3 2020-07-23 stsp } else {
1914 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1915 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1916 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1917 f6764181 2021-09-24 stsp ondisk_path);
1918 f6764181 2021-09-24 stsp goto done;
1919 f6764181 2021-09-24 stsp }
1920 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1922 f6764181 2021-09-24 stsp } else {
1923 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1925 f6764181 2021-09-24 stsp else
1926 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1927 f6764181 2021-09-24 stsp }
1928 c90c8ce3 2020-07-23 stsp }
1929 6353ad76 2019-02-08 stsp
1930 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1931 2c41dce7 2021-06-27 stsp if (ie)
1932 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1933 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1934 b8f41171 2019-02-10 stsp goto done;
1935 b8f41171 2019-02-10 stsp }
1936 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1937 a769b60b 2021-06-27 stsp if (ie)
1938 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1939 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1940 5036ab18 2020-04-18 stsp path);
1941 5036ab18 2020-04-18 stsp goto done;
1942 5036ab18 2020-04-18 stsp }
1943 b8f41171 2019-02-10 stsp
1944 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1945 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1946 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1947 c6e8a826 2021-04-05 stsp /*
1948 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1949 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1950 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1951 c6e8a826 2021-04-05 stsp * updating contents of this file.
1952 c6e8a826 2021-04-05 stsp */
1953 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1954 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1955 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1956 c6e8a826 2021-04-05 stsp /* Same commit. */
1957 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1958 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1959 e2b1e152 2019-07-13 stsp if (err)
1960 e2b1e152 2019-07-13 stsp goto done;
1961 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1962 b8f41171 2019-02-10 stsp path);
1963 6353ad76 2019-02-08 stsp goto done;
1964 a378724f 2019-02-10 stsp }
1965 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1966 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1967 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1968 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1969 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1970 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1971 0f58026f 2021-04-05 stsp if (err)
1972 0f58026f 2021-04-05 stsp goto done;
1973 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1974 0f58026f 2021-04-05 stsp path);
1975 b8f41171 2019-02-10 stsp goto done;
1976 e2b1e152 2019-07-13 stsp }
1977 9d31a1d8 2018-03-11 stsp }
1978 9d31a1d8 2018-03-11 stsp
1979 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1980 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1981 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1982 eb81bc23 2022-06-28 tracey goto done;
1983 eb81bc23 2022-06-28 tracey }
1984 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1985 8da9e5f4 2019-01-12 stsp if (err)
1986 6353ad76 2019-02-08 stsp goto done;
1987 512f0d0e 2019-01-02 stsp
1988 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1989 234035bc 2019-06-01 stsp int update_timestamps;
1990 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1991 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1992 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1993 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1994 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1995 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1996 eb81bc23 2022-06-28 tracey goto done;
1997 eb81bc23 2022-06-28 tracey }
1998 234035bc 2019-06-01 stsp struct got_object_id id2;
1999 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2000 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2001 eb81bc23 2022-06-28 tracey fd2);
2002 234035bc 2019-06-01 stsp if (err)
2003 f69721c3 2019-10-21 stsp goto done;
2004 f69721c3 2019-10-21 stsp }
2005 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2006 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2007 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2008 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2009 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2010 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2011 f69721c3 2019-10-21 stsp goto done;
2012 f69721c3 2019-10-21 stsp }
2013 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2014 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2015 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2016 234035bc 2019-06-01 stsp goto done;
2017 f69721c3 2019-10-21 stsp }
2018 234035bc 2019-06-01 stsp }
2019 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2020 36bf999c 2020-07-23 stsp char *link_target;
2021 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2022 36bf999c 2020-07-23 stsp if (err)
2023 36bf999c 2020-07-23 stsp goto done;
2024 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2025 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2026 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2027 36bf999c 2020-07-23 stsp free(link_target);
2028 993e2a1b 2020-07-23 stsp } else {
2029 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2030 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2031 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2032 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2033 993e2a1b 2020-07-23 stsp }
2034 f69721c3 2019-10-21 stsp free(label_orig);
2035 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2036 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2037 eb81bc23 2022-06-28 tracey goto done;
2038 eb81bc23 2022-06-28 tracey }
2039 234035bc 2019-06-01 stsp if (blob2)
2040 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2041 909d120e 2019-09-22 stsp if (err)
2042 909d120e 2019-09-22 stsp goto done;
2043 234035bc 2019-06-01 stsp /*
2044 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2045 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2046 234035bc 2019-06-01 stsp * unmodified files again.
2047 234035bc 2019-06-01 stsp */
2048 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2050 234035bc 2019-06-01 stsp update_timestamps);
2051 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2052 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2054 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2055 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2056 1ee397ad 2019-07-12 stsp if (err)
2057 1ee397ad 2019-07-12 stsp goto done;
2058 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2059 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2060 13d9040b 2019-03-27 stsp if (err)
2061 13d9040b 2019-03-27 stsp goto done;
2062 13d9040b 2019-03-27 stsp } else {
2063 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2064 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2065 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2066 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2067 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2068 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2069 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2070 2e1fa222 2020-07-23 stsp } else {
2071 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2072 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2073 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2074 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2075 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2076 2e1fa222 2020-07-23 stsp }
2077 13d9040b 2019-03-27 stsp if (err)
2078 13d9040b 2019-03-27 stsp goto done;
2079 65b05cec 2020-07-23 stsp
2080 054041d0 2020-06-24 stsp if (ie) {
2081 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2082 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2083 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2084 054041d0 2020-06-24 stsp } else {
2085 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2086 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2087 054041d0 2020-06-24 stsp &blob->id);
2088 054041d0 2020-06-24 stsp }
2089 13d9040b 2019-03-27 stsp if (err)
2090 13d9040b 2019-03-27 stsp goto done;
2091 65b05cec 2020-07-23 stsp
2092 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2093 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2094 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2095 2e1fa222 2020-07-23 stsp }
2096 13d9040b 2019-03-27 stsp }
2097 eb81bc23 2022-06-28 tracey
2098 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2099 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2100 eb81bc23 2022-06-28 tracey goto done;
2101 eb81bc23 2022-06-28 tracey }
2102 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2103 6353ad76 2019-02-08 stsp done:
2104 6353ad76 2019-02-08 stsp free(ondisk_path);
2105 8da9e5f4 2019-01-12 stsp return err;
2106 90285c3b 2019-01-08 stsp }
2107 90285c3b 2019-01-08 stsp
2108 90285c3b 2019-01-08 stsp static const struct got_error *
2109 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2110 90285c3b 2019-01-08 stsp {
2111 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2112 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2113 90285c3b 2019-01-08 stsp
2114 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2115 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2116 90285c3b 2019-01-08 stsp
2117 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2118 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2119 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2120 c97665ae 2019-01-13 stsp } else {
2121 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2122 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2123 1c4cdd89 2021-06-20 stsp if (err)
2124 1c4cdd89 2021-06-20 stsp goto done;
2125 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2126 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2127 fddefe3b 2020-10-20 stsp free(ondisk_path);
2128 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2129 1c4cdd89 2021-06-20 stsp parent = NULL;
2130 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2131 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2132 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2133 fddefe3b 2020-10-20 stsp ondisk_path);
2134 90285c3b 2019-01-08 stsp break;
2135 90285c3b 2019-01-08 stsp }
2136 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2137 1c4cdd89 2021-06-20 stsp if (err)
2138 1c4cdd89 2021-06-20 stsp break;
2139 1c4cdd89 2021-06-20 stsp }
2140 90285c3b 2019-01-08 stsp }
2141 1c4cdd89 2021-06-20 stsp done:
2142 90285c3b 2019-01-08 stsp free(ondisk_path);
2143 1c4cdd89 2021-06-20 stsp free(parent);
2144 90285c3b 2019-01-08 stsp return err;
2145 512f0d0e 2019-01-02 stsp }
2146 512f0d0e 2019-01-02 stsp
2147 708d8e67 2019-03-27 stsp static const struct got_error *
2148 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2149 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2150 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 708d8e67 2019-03-27 stsp {
2152 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2153 708d8e67 2019-03-27 stsp unsigned char status;
2154 708d8e67 2019-03-27 stsp struct stat sb;
2155 708d8e67 2019-03-27 stsp char *ondisk_path;
2156 708d8e67 2019-03-27 stsp
2157 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2158 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 a76c42e6 2019-08-03 stsp
2160 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2161 708d8e67 2019-03-27 stsp == -1)
2162 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2163 708d8e67 2019-03-27 stsp
2164 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2165 708d8e67 2019-03-27 stsp if (err)
2166 5a58a424 2020-06-23 stsp goto done;
2167 708d8e67 2019-03-27 stsp
2168 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2169 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2170 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2171 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2172 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2173 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2174 993e2a1b 2020-07-23 stsp goto done;
2175 993e2a1b 2020-07-23 stsp }
2176 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2177 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2178 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2179 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2180 993e2a1b 2020-07-23 stsp if (err)
2181 993e2a1b 2020-07-23 stsp goto done;
2182 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2183 993e2a1b 2020-07-23 stsp ie->path);
2184 993e2a1b 2020-07-23 stsp goto done;
2185 993e2a1b 2020-07-23 stsp }
2186 993e2a1b 2020-07-23 stsp
2187 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2188 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2189 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2190 1ee397ad 2019-07-12 stsp if (err)
2191 5a58a424 2020-06-23 stsp goto done;
2192 fc6346c4 2019-03-27 stsp /*
2193 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2194 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2195 fc6346c4 2019-03-27 stsp */
2196 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2197 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2198 fc6346c4 2019-03-27 stsp } else {
2199 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2200 1ee397ad 2019-07-12 stsp if (err)
2201 5a58a424 2020-06-23 stsp goto done;
2202 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2203 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2204 fc6346c4 2019-03-27 stsp if (err)
2205 5a58a424 2020-06-23 stsp goto done;
2206 fc6346c4 2019-03-27 stsp }
2207 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2208 708d8e67 2019-03-27 stsp }
2209 5a58a424 2020-06-23 stsp done:
2210 5a58a424 2020-06-23 stsp free(ondisk_path);
2211 fc6346c4 2019-03-27 stsp return err;
2212 708d8e67 2019-03-27 stsp }
2213 708d8e67 2019-03-27 stsp
2214 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2215 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2216 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2217 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2218 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2219 8da9e5f4 2019-01-12 stsp void *progress_arg;
2220 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2221 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2222 8da9e5f4 2019-01-12 stsp };
2223 8da9e5f4 2019-01-12 stsp
2224 512f0d0e 2019-01-02 stsp static const struct got_error *
2225 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2226 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2227 512f0d0e 2019-01-02 stsp {
2228 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2229 0584f854 2019-04-06 stsp
2230 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2231 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2232 512f0d0e 2019-01-02 stsp
2233 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2234 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2235 8da9e5f4 2019-01-12 stsp }
2236 512f0d0e 2019-01-02 stsp
2237 8da9e5f4 2019-01-12 stsp static const struct got_error *
2238 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 8da9e5f4 2019-01-12 stsp {
2240 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2241 7a9df742 2019-01-08 stsp
2242 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2243 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2244 0584f854 2019-04-06 stsp
2245 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2246 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2247 9d31a1d8 2018-03-11 stsp }
2248 9d31a1d8 2018-03-11 stsp
2249 9d31a1d8 2018-03-11 stsp static const struct got_error *
2250 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 9d31a1d8 2018-03-11 stsp {
2252 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2253 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2254 8da9e5f4 2019-01-12 stsp char *path;
2255 9d31a1d8 2018-03-11 stsp
2256 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2257 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2258 0584f854 2019-04-06 stsp
2259 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2260 63c5ca5d 2019-08-24 stsp return NULL;
2261 63c5ca5d 2019-08-24 stsp
2262 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2263 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2264 8da9e5f4 2019-01-12 stsp == -1)
2265 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2266 9d31a1d8 2018-03-11 stsp
2267 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2268 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2269 8da9e5f4 2019-01-12 stsp else
2270 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2271 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2272 9d31a1d8 2018-03-11 stsp
2273 8da9e5f4 2019-01-12 stsp free(path);
2274 0cd1c46a 2019-03-11 stsp return err;
2275 0cd1c46a 2019-03-11 stsp }
2276 0cd1c46a 2019-03-11 stsp
2277 b2118c49 2020-07-28 stsp const struct got_error *
2278 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 b2118c49 2020-07-28 stsp {
2280 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2281 b2118c49 2020-07-28 stsp
2282 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2283 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2284 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2285 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2286 b2118c49 2020-07-28 stsp }
2287 b2118c49 2020-07-28 stsp
2288 b2118c49 2020-07-28 stsp return NULL;
2289 b2118c49 2020-07-28 stsp }
2290 b2118c49 2020-07-28 stsp
2291 818c7501 2019-07-11 stsp static const struct got_error *
2292 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 0cd1c46a 2019-03-11 stsp {
2294 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2295 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2296 0cd1c46a 2019-03-11 stsp
2297 517bab73 2019-03-11 stsp *refname = NULL;
2298 517bab73 2019-03-11 stsp
2299 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2300 b2118c49 2020-07-28 stsp if (err)
2301 b2118c49 2020-07-28 stsp return err;
2302 0cd1c46a 2019-03-11 stsp
2303 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2304 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2305 0647c563 2019-03-11 stsp *refname = NULL;
2306 0cd1c46a 2019-03-11 stsp }
2307 517bab73 2019-03-11 stsp free(uuidstr);
2308 517bab73 2019-03-11 stsp return err;
2309 517bab73 2019-03-11 stsp }
2310 517bab73 2019-03-11 stsp
2311 818c7501 2019-07-11 stsp const struct got_error *
2312 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2313 9587e6cc 2023-01-28 mark const char *prefix)
2314 9587e6cc 2023-01-28 mark {
2315 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2316 9587e6cc 2023-01-28 mark }
2317 9587e6cc 2023-01-28 mark
2318 9587e6cc 2023-01-28 mark const struct got_error *
2319 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2320 818c7501 2019-07-11 stsp {
2321 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2322 818c7501 2019-07-11 stsp }
2323 818c7501 2019-07-11 stsp
2324 818c7501 2019-07-11 stsp static const struct got_error *
2325 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2326 818c7501 2019-07-11 stsp {
2327 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2328 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2329 818c7501 2019-07-11 stsp }
2330 818c7501 2019-07-11 stsp
2331 818c7501 2019-07-11 stsp static const struct got_error *
2332 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2333 818c7501 2019-07-11 stsp {
2334 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2335 818c7501 2019-07-11 stsp }
2336 818c7501 2019-07-11 stsp
2337 818c7501 2019-07-11 stsp static const struct got_error *
2338 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2339 818c7501 2019-07-11 stsp {
2340 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2341 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2342 818c7501 2019-07-11 stsp }
2343 818c7501 2019-07-11 stsp
2344 818c7501 2019-07-11 stsp static const struct got_error *
2345 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2346 818c7501 2019-07-11 stsp {
2347 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2348 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2349 0ebf8283 2019-07-24 stsp }
2350 0ebf8283 2019-07-24 stsp
2351 0ebf8283 2019-07-24 stsp static const struct got_error *
2352 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2353 0ebf8283 2019-07-24 stsp {
2354 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2355 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2356 0ebf8283 2019-07-24 stsp }
2357 0ebf8283 2019-07-24 stsp
2358 0ebf8283 2019-07-24 stsp static const struct got_error *
2359 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2360 0ebf8283 2019-07-24 stsp {
2361 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2362 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2363 0ebf8283 2019-07-24 stsp }
2364 0ebf8283 2019-07-24 stsp
2365 0ebf8283 2019-07-24 stsp static const struct got_error *
2366 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 0ebf8283 2019-07-24 stsp {
2368 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2369 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2370 818c7501 2019-07-11 stsp }
2371 818c7501 2019-07-11 stsp
2372 0ebf8283 2019-07-24 stsp static const struct got_error *
2373 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 0ebf8283 2019-07-24 stsp {
2375 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2376 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2377 0ebf8283 2019-07-24 stsp }
2378 818c7501 2019-07-11 stsp
2379 0ebf8283 2019-07-24 stsp const struct got_error *
2380 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2381 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2382 0ebf8283 2019-07-24 stsp {
2383 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2384 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2385 0ebf8283 2019-07-24 stsp *path = NULL;
2386 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2387 0ebf8283 2019-07-24 stsp }
2388 0ebf8283 2019-07-24 stsp return NULL;
2389 f259c4c1 2021-09-24 stsp }
2390 f259c4c1 2021-09-24 stsp
2391 f259c4c1 2021-09-24 stsp static const struct got_error *
2392 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2393 f259c4c1 2021-09-24 stsp {
2394 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2395 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2396 0ebf8283 2019-07-24 stsp }
2397 0ebf8283 2019-07-24 stsp
2398 f259c4c1 2021-09-24 stsp static const struct got_error *
2399 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 f259c4c1 2021-09-24 stsp {
2401 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2402 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2403 f259c4c1 2021-09-24 stsp }
2404 f259c4c1 2021-09-24 stsp
2405 517bab73 2019-03-11 stsp /*
2406 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2407 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2408 517bab73 2019-03-11 stsp */
2409 517bab73 2019-03-11 stsp static const struct got_error *
2410 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2411 517bab73 2019-03-11 stsp {
2412 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2413 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2414 517bab73 2019-03-11 stsp char *refname;
2415 517bab73 2019-03-11 stsp
2416 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2417 517bab73 2019-03-11 stsp if (err)
2418 517bab73 2019-03-11 stsp return err;
2419 0cd1c46a 2019-03-11 stsp
2420 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2421 0cd1c46a 2019-03-11 stsp if (err)
2422 0cd1c46a 2019-03-11 stsp goto done;
2423 0cd1c46a 2019-03-11 stsp
2424 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2425 0cd1c46a 2019-03-11 stsp done:
2426 0cd1c46a 2019-03-11 stsp free(refname);
2427 0cd1c46a 2019-03-11 stsp if (ref)
2428 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2429 3e3a69f1 2019-07-25 stsp return err;
2430 3e3a69f1 2019-07-25 stsp }
2431 3e3a69f1 2019-07-25 stsp
2432 3e3a69f1 2019-07-25 stsp static const struct got_error *
2433 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2434 3e3a69f1 2019-07-25 stsp {
2435 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2436 3e3a69f1 2019-07-25 stsp
2437 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2438 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2439 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2440 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2441 3e3a69f1 2019-07-25 stsp }
2442 9d31a1d8 2018-03-11 stsp return err;
2443 9d31a1d8 2018-03-11 stsp }
2444 9d31a1d8 2018-03-11 stsp
2445 3e3a69f1 2019-07-25 stsp
2446 ebf99748 2019-05-09 stsp static const struct got_error *
2447 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2448 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2449 ebf99748 2019-05-09 stsp {
2450 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2451 ebf99748 2019-05-09 stsp FILE *index = NULL;
2452 0cd1c46a 2019-03-11 stsp
2453 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2454 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2455 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2456 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2457 ebf99748 2019-05-09 stsp
2458 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2459 3e3a69f1 2019-07-25 stsp if (err)
2460 ebf99748 2019-05-09 stsp goto done;
2461 ebf99748 2019-05-09 stsp
2462 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2463 ebf99748 2019-05-09 stsp if (index == NULL) {
2464 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2465 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2466 ebf99748 2019-05-09 stsp } else {
2467 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2468 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2469 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2470 ebf99748 2019-05-09 stsp }
2471 ebf99748 2019-05-09 stsp done:
2472 ebf99748 2019-05-09 stsp if (err) {
2473 ebf99748 2019-05-09 stsp free(*fileindex_path);
2474 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2475 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2476 ebf99748 2019-05-09 stsp *fileindex = NULL;
2477 ebf99748 2019-05-09 stsp }
2478 ebf99748 2019-05-09 stsp return err;
2479 ebf99748 2019-05-09 stsp }
2480 c932eeeb 2019-05-22 stsp
2481 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2482 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2483 c932eeeb 2019-05-22 stsp const char *path;
2484 c932eeeb 2019-05-22 stsp size_t path_len;
2485 c932eeeb 2019-05-22 stsp const char *entry_name;
2486 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2487 a484d721 2019-06-10 stsp void *progress_arg;
2488 c932eeeb 2019-05-22 stsp };
2489 ebf99748 2019-05-09 stsp
2490 c932eeeb 2019-05-22 stsp static const struct got_error *
2491 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2492 c932eeeb 2019-05-22 stsp {
2493 1ee397ad 2019-07-12 stsp const struct got_error *err;
2494 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2495 c932eeeb 2019-05-22 stsp
2496 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2497 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2498 c932eeeb 2019-05-22 stsp return NULL;
2499 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2500 102ff934 2019-06-11 stsp return NULL;
2501 102ff934 2019-06-11 stsp
2502 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2503 a769b60b 2021-06-27 stsp return NULL;
2504 a769b60b 2021-06-27 stsp
2505 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2506 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2507 c932eeeb 2019-05-22 stsp return NULL;
2508 c932eeeb 2019-05-22 stsp
2509 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2510 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2511 edd02c5e 2019-07-11 stsp ie->path);
2512 1ee397ad 2019-07-12 stsp if (err)
2513 1ee397ad 2019-07-12 stsp return err;
2514 1ee397ad 2019-07-12 stsp }
2515 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2516 c932eeeb 2019-05-22 stsp return NULL;
2517 a615e0e7 2020-12-16 stsp }
2518 a615e0e7 2020-12-16 stsp
2519 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2520 a615e0e7 2020-12-16 stsp static const struct got_error *
2521 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2522 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2523 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2524 a615e0e7 2020-12-16 stsp {
2525 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2526 a615e0e7 2020-12-16 stsp
2527 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2528 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2529 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2530 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2531 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2532 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2533 a615e0e7 2020-12-16 stsp
2534 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2535 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2536 9c6338c4 2019-06-02 stsp }
2537 9c6338c4 2019-06-02 stsp
2538 9c6338c4 2019-06-02 stsp static const struct got_error *
2539 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2540 9c6338c4 2019-06-02 stsp {
2541 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2542 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2543 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2544 867630bb 2020-01-17 stsp struct timespec timeout;
2545 9c6338c4 2019-06-02 stsp
2546 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2547 b90054ed 2022-11-01 stsp fileindex_path, "");
2548 9c6338c4 2019-06-02 stsp if (err)
2549 9c6338c4 2019-06-02 stsp goto done;
2550 9c6338c4 2019-06-02 stsp
2551 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2552 9c6338c4 2019-06-02 stsp if (err)
2553 9c6338c4 2019-06-02 stsp goto done;
2554 9c6338c4 2019-06-02 stsp
2555 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2556 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2557 9c6338c4 2019-06-02 stsp fileindex_path);
2558 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2559 9c6338c4 2019-06-02 stsp }
2560 867630bb 2020-01-17 stsp
2561 867630bb 2020-01-17 stsp /*
2562 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2563 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2564 867630bb 2020-01-17 stsp * was recorded in the file index.
2565 867630bb 2020-01-17 stsp */
2566 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2567 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2568 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2569 9c6338c4 2019-06-02 stsp done:
2570 9c6338c4 2019-06-02 stsp if (new_index)
2571 9c6338c4 2019-06-02 stsp fclose(new_index);
2572 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2573 9c6338c4 2019-06-02 stsp return err;
2574 c932eeeb 2019-05-22 stsp }
2575 6ced4176 2019-07-12 stsp
2576 6ced4176 2019-07-12 stsp static const struct got_error *
2577 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2578 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2579 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2580 a44927cc 2022-04-07 stsp struct got_repository *repo)
2581 6ced4176 2019-07-12 stsp {
2582 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2583 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2584 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2585 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2586 6ced4176 2019-07-12 stsp
2587 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2588 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2589 6ced4176 2019-07-12 stsp *tree_id = NULL;
2590 6ced4176 2019-07-12 stsp
2591 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2592 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2593 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2594 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2595 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2596 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2597 6ced4176 2019-07-12 stsp goto done;
2598 6ced4176 2019-07-12 stsp }
2599 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2600 a44927cc 2022-04-07 stsp worktree->path_prefix);
2601 6ced4176 2019-07-12 stsp if (err)
2602 6ced4176 2019-07-12 stsp goto done;
2603 6ced4176 2019-07-12 stsp return NULL;
2604 6ced4176 2019-07-12 stsp }
2605 6ced4176 2019-07-12 stsp
2606 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2607 6ced4176 2019-07-12 stsp
2608 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2609 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2610 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2611 6ced4176 2019-07-12 stsp goto done;
2612 6ced4176 2019-07-12 stsp }
2613 6ced4176 2019-07-12 stsp
2614 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2615 6ced4176 2019-07-12 stsp if (err)
2616 6ced4176 2019-07-12 stsp goto done;
2617 6ced4176 2019-07-12 stsp
2618 6ced4176 2019-07-12 stsp free(in_repo_path);
2619 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2620 6ced4176 2019-07-12 stsp
2621 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2622 6ced4176 2019-07-12 stsp if (err)
2623 6ced4176 2019-07-12 stsp goto done;
2624 c932eeeb 2019-05-22 stsp
2625 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2626 6ced4176 2019-07-12 stsp /* Check out a single file. */
2627 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2628 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2629 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2630 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2631 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2632 6ced4176 2019-07-12 stsp goto done;
2633 6ced4176 2019-07-12 stsp }
2634 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2635 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2636 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2637 6ced4176 2019-07-12 stsp goto done;
2638 6ced4176 2019-07-12 stsp }
2639 6ced4176 2019-07-12 stsp } else {
2640 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2641 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2642 6ced4176 2019-07-12 stsp if (err)
2643 6ced4176 2019-07-12 stsp return err;
2644 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2645 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2646 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2647 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2648 6ced4176 2019-07-12 stsp goto done;
2649 6ced4176 2019-07-12 stsp }
2650 6ced4176 2019-07-12 stsp }
2651 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2652 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2653 6ced4176 2019-07-12 stsp } else {
2654 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2655 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2656 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2657 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2658 6ced4176 2019-07-12 stsp goto done;
2659 6ced4176 2019-07-12 stsp }
2660 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2661 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2662 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2663 6ced4176 2019-07-12 stsp goto done;
2664 6ced4176 2019-07-12 stsp }
2665 6ced4176 2019-07-12 stsp }
2666 6ced4176 2019-07-12 stsp done:
2667 6ced4176 2019-07-12 stsp free(id);
2668 6ced4176 2019-07-12 stsp free(in_repo_path);
2669 6ced4176 2019-07-12 stsp if (err) {
2670 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2671 6ced4176 2019-07-12 stsp free(*tree_relpath);
2672 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2673 6ced4176 2019-07-12 stsp free(*tree_id);
2674 6ced4176 2019-07-12 stsp *tree_id = NULL;
2675 6ced4176 2019-07-12 stsp }
2676 07ed472d 2019-07-12 stsp return err;
2677 07ed472d 2019-07-12 stsp }
2678 07ed472d 2019-07-12 stsp
2679 07ed472d 2019-07-12 stsp static const struct got_error *
2680 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2681 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2682 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2683 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2684 07ed472d 2019-07-12 stsp {
2685 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2686 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2687 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2688 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2689 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2690 07ed472d 2019-07-12 stsp
2691 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2692 7f47418f 2019-12-20 stsp if (err) {
2693 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2694 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2695 7f47418f 2019-12-20 stsp goto done;
2696 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2697 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2698 7f47418f 2019-12-20 stsp if (err)
2699 7f47418f 2019-12-20 stsp return err;
2700 7f47418f 2019-12-20 stsp }
2701 07ed472d 2019-07-12 stsp
2702 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2703 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2704 07ed472d 2019-07-12 stsp if (err)
2705 07ed472d 2019-07-12 stsp goto done;
2706 07ed472d 2019-07-12 stsp
2707 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2708 07ed472d 2019-07-12 stsp if (err)
2709 07ed472d 2019-07-12 stsp goto done;
2710 07ed472d 2019-07-12 stsp
2711 07ed472d 2019-07-12 stsp if (entry_name &&
2712 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2713 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2714 07ed472d 2019-07-12 stsp goto done;
2715 07ed472d 2019-07-12 stsp }
2716 07ed472d 2019-07-12 stsp
2717 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2718 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2719 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2720 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2721 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2722 07ed472d 2019-07-12 stsp arg.repo = repo;
2723 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2724 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2725 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2726 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2727 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2728 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2729 07ed472d 2019-07-12 stsp done:
2730 07ed472d 2019-07-12 stsp if (tree)
2731 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2732 07ed472d 2019-07-12 stsp if (commit)
2733 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2734 6ced4176 2019-07-12 stsp return err;
2735 6ced4176 2019-07-12 stsp }
2736 6ced4176 2019-07-12 stsp
2737 9d31a1d8 2018-03-11 stsp const struct got_error *
2738 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2739 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2740 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2741 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2742 9d31a1d8 2018-03-11 stsp {
2743 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2744 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2745 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2746 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2747 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2748 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2749 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2750 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2751 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2752 f2ea84fa 2019-07-27 stsp int entry_type;
2753 f2ea84fa 2019-07-27 stsp char *relpath;
2754 f2ea84fa 2019-07-27 stsp char *entry_name;
2755 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2756 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2757 9d31a1d8 2018-03-11 stsp
2758 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2759 f2ea84fa 2019-07-27 stsp
2760 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2761 9d31a1d8 2018-03-11 stsp if (err)
2762 9d31a1d8 2018-03-11 stsp return err;
2763 a44927cc 2022-04-07 stsp
2764 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2765 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2766 a44927cc 2022-04-07 stsp if (err)
2767 a44927cc 2022-04-07 stsp goto done;
2768 93a30277 2018-12-24 stsp
2769 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2770 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2771 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2772 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2773 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2774 f2ea84fa 2019-07-27 stsp goto done;
2775 f2ea84fa 2019-07-27 stsp }
2776 6ced4176 2019-07-12 stsp
2777 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2778 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2779 a44927cc 2022-04-07 stsp worktree, repo);
2780 f2ea84fa 2019-07-27 stsp if (err) {
2781 f2ea84fa 2019-07-27 stsp free(tpd);
2782 6ced4176 2019-07-12 stsp goto done;
2783 6ced4176 2019-07-12 stsp }
2784 f2ea84fa 2019-07-27 stsp
2785 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2787 f2ea84fa 2019-07-27 stsp if (err) {
2788 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2789 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2790 f2ea84fa 2019-07-27 stsp free(tpd);
2791 f2ea84fa 2019-07-27 stsp goto done;
2792 f2ea84fa 2019-07-27 stsp }
2793 f2ea84fa 2019-07-27 stsp } else
2794 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2795 f2ea84fa 2019-07-27 stsp
2796 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2797 6ced4176 2019-07-12 stsp }
2798 6ced4176 2019-07-12 stsp
2799 51514078 2018-12-25 stsp /*
2800 51514078 2018-12-25 stsp * Read the file index.
2801 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2802 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2803 51514078 2018-12-25 stsp */
2804 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 8da9e5f4 2019-01-12 stsp if (err)
2806 c4cdcb68 2019-04-03 stsp goto done;
2807 c4cdcb68 2019-04-03 stsp
2808 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2809 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2810 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2811 f2ea84fa 2019-07-27 stsp
2812 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2813 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2814 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2815 f2ea84fa 2019-07-27 stsp if (err)
2816 f2ea84fa 2019-07-27 stsp break;
2817 f2ea84fa 2019-07-27 stsp
2818 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2819 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2820 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2821 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2822 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2823 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2824 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2825 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2826 f2ea84fa 2019-07-27 stsp if (err)
2827 f2ea84fa 2019-07-27 stsp break;
2828 f2ea84fa 2019-07-27 stsp
2829 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2830 711d9cd9 2019-07-12 stsp }
2831 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2832 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2833 af12c6b9 2019-06-04 stsp err = sync_err;
2834 9d31a1d8 2018-03-11 stsp done:
2835 9c6338c4 2019-06-02 stsp free(fileindex_path);
2836 edfa7d7f 2018-09-11 stsp if (tree)
2837 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2838 9d31a1d8 2018-03-11 stsp if (commit)
2839 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2840 5ade8233 2019-07-12 stsp if (fileindex)
2841 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2842 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2843 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2844 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2845 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2846 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2847 f2ea84fa 2019-07-27 stsp free(tpd);
2848 f2ea84fa 2019-07-27 stsp }
2849 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2850 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2851 9d31a1d8 2018-03-11 stsp err = unlockerr;
2852 f8d1f275 2019-02-04 stsp return err;
2853 f8d1f275 2019-02-04 stsp }
2854 f8d1f275 2019-02-04 stsp
2855 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2856 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2857 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2858 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2859 234035bc 2019-06-01 stsp void *progress_arg;
2860 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2861 234035bc 2019-06-01 stsp void *cancel_arg;
2862 f69721c3 2019-10-21 stsp const char *label_orig;
2863 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2864 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2865 234035bc 2019-06-01 stsp };
2866 234035bc 2019-06-01 stsp
2867 234035bc 2019-06-01 stsp static const struct got_error *
2868 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2870 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2871 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2872 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 234035bc 2019-06-01 stsp {
2874 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2875 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2876 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2877 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2878 234035bc 2019-06-01 stsp struct stat sb;
2879 234035bc 2019-06-01 stsp unsigned char status;
2880 234035bc 2019-06-01 stsp int local_changes_subsumed;
2881 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2882 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2883 234035bc 2019-06-01 stsp
2884 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2885 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2886 d6c87207 2019-08-02 stsp strlen(path2));
2887 1ee397ad 2019-07-12 stsp if (ie == NULL)
2888 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2889 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2890 234035bc 2019-06-01 stsp
2891 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2892 234035bc 2019-06-01 stsp path2) == -1)
2893 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2894 234035bc 2019-06-01 stsp
2895 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2896 7f91a133 2019-12-13 stsp repo);
2897 234035bc 2019-06-01 stsp if (err)
2898 234035bc 2019-06-01 stsp goto done;
2899 234035bc 2019-06-01 stsp
2900 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2901 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2902 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2903 234035bc 2019-06-01 stsp goto done;
2904 234035bc 2019-06-01 stsp }
2905 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2906 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2907 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2908 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2909 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2910 234035bc 2019-06-01 stsp goto done;
2911 234035bc 2019-06-01 stsp }
2912 234035bc 2019-06-01 stsp
2913 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2914 36bf999c 2020-07-23 stsp char *link_target2;
2915 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2916 36bf999c 2020-07-23 stsp if (err)
2917 36bf999c 2020-07-23 stsp goto done;
2918 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2919 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2920 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2921 36bf999c 2020-07-23 stsp free(link_target2);
2922 af57b12a 2020-07-23 stsp } else {
2923 1af628f4 2021-06-03 stsp int fd;
2924 1af628f4 2021-06-03 stsp
2925 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2926 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2927 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2928 1af628f4 2021-06-03 stsp goto done;
2929 1af628f4 2021-06-03 stsp }
2930 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2931 1af628f4 2021-06-03 stsp f_orig, blob1);
2932 1af628f4 2021-06-03 stsp if (err)
2933 1af628f4 2021-06-03 stsp goto done;
2934 1af628f4 2021-06-03 stsp
2935 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2936 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2937 1af628f4 2021-06-03 stsp goto done;
2938 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2939 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2940 1af628f4 2021-06-03 stsp if (err)
2941 1af628f4 2021-06-03 stsp goto done;
2942 1af628f4 2021-06-03 stsp
2943 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2944 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2945 1af628f4 2021-06-03 stsp if (fd == -1) {
2946 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2947 1af628f4 2021-06-03 stsp ondisk_path);
2948 1af628f4 2021-06-03 stsp goto done;
2949 1af628f4 2021-06-03 stsp }
2950 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2951 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2952 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2953 1af628f4 2021-06-03 stsp ondisk_path);
2954 1af628f4 2021-06-03 stsp close(fd);
2955 1af628f4 2021-06-03 stsp goto done;
2956 1af628f4 2021-06-03 stsp }
2957 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2958 1af628f4 2021-06-03 stsp if (err)
2959 1af628f4 2021-06-03 stsp goto done;
2960 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2961 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2962 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2963 1af628f4 2021-06-03 stsp goto done;
2964 1af628f4 2021-06-03 stsp }
2965 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2966 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2967 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
2968 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2969 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2970 af57b12a 2020-07-23 stsp }
2971 234035bc 2019-06-01 stsp } else if (blob1) {
2972 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2973 d6c87207 2019-08-02 stsp strlen(path1));
2974 1ee397ad 2019-07-12 stsp if (ie == NULL)
2975 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2976 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2977 2b92fad7 2019-06-02 stsp
2978 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2979 2b92fad7 2019-06-02 stsp path1) == -1)
2980 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2981 2b92fad7 2019-06-02 stsp
2982 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2983 7f91a133 2019-12-13 stsp repo);
2984 2b92fad7 2019-06-02 stsp if (err)
2985 2b92fad7 2019-06-02 stsp goto done;
2986 2b92fad7 2019-06-02 stsp
2987 2b92fad7 2019-06-02 stsp switch (status) {
2988 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2989 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2990 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2991 1ee397ad 2019-07-12 stsp if (err)
2992 1ee397ad 2019-07-12 stsp goto done;
2993 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2994 2b92fad7 2019-06-02 stsp if (err)
2995 2b92fad7 2019-06-02 stsp goto done;
2996 2b92fad7 2019-06-02 stsp if (ie)
2997 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2998 2b92fad7 2019-06-02 stsp break;
2999 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3000 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3001 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3002 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3003 1ee397ad 2019-07-12 stsp if (err)
3004 1ee397ad 2019-07-12 stsp goto done;
3005 2b92fad7 2019-06-02 stsp if (ie)
3006 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3007 2b92fad7 2019-06-02 stsp break;
3008 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3009 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3010 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3011 72840534 2022-01-19 stsp off_t blob1_size;
3012 0a22ca1a 2020-09-23 stsp /*
3013 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3014 0a22ca1a 2020-09-23 stsp * exists in the repository.
3015 0a22ca1a 2020-09-23 stsp */
3016 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3017 72840534 2022-01-19 stsp &blob1_size, path1);
3018 0a22ca1a 2020-09-23 stsp if (err)
3019 0a22ca1a 2020-09-23 stsp goto done;
3020 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3021 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3022 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3023 0a22ca1a 2020-09-23 stsp if (err)
3024 0a22ca1a 2020-09-23 stsp goto done;
3025 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3026 0a22ca1a 2020-09-23 stsp path1);
3027 0a22ca1a 2020-09-23 stsp if (err)
3028 0a22ca1a 2020-09-23 stsp goto done;
3029 0a22ca1a 2020-09-23 stsp if (ie)
3030 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3031 0a22ca1a 2020-09-23 stsp ie);
3032 0a22ca1a 2020-09-23 stsp } else {
3033 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3034 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3035 0a22ca1a 2020-09-23 stsp }
3036 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3037 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3038 0a22ca1a 2020-09-23 stsp free(id);
3039 0a22ca1a 2020-09-23 stsp if (err)
3040 0a22ca1a 2020-09-23 stsp goto done;
3041 0a22ca1a 2020-09-23 stsp break;
3042 0a22ca1a 2020-09-23 stsp }
3043 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3044 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3045 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3046 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3047 1ee397ad 2019-07-12 stsp if (err)
3048 1ee397ad 2019-07-12 stsp goto done;
3049 2b92fad7 2019-06-02 stsp break;
3050 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3051 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3052 1ee397ad 2019-07-12 stsp if (err)
3053 1ee397ad 2019-07-12 stsp goto done;
3054 2b92fad7 2019-06-02 stsp break;
3055 2b92fad7 2019-06-02 stsp default:
3056 2b92fad7 2019-06-02 stsp break;
3057 2b92fad7 2019-06-02 stsp }
3058 234035bc 2019-06-01 stsp } else if (blob2) {
3059 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3060 234035bc 2019-06-01 stsp path2) == -1)
3061 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3062 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3063 d6c87207 2019-08-02 stsp strlen(path2));
3064 234035bc 2019-06-01 stsp if (ie) {
3065 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3066 7f91a133 2019-12-13 stsp -1, NULL, repo);
3067 234035bc 2019-06-01 stsp if (err)
3068 234035bc 2019-06-01 stsp goto done;
3069 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3070 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3071 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3072 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3073 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3074 1ee397ad 2019-07-12 stsp status, path2);
3075 234035bc 2019-06-01 stsp goto done;
3076 234035bc 2019-06-01 stsp }
3077 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3078 36bf999c 2020-07-23 stsp char *link_target2;
3079 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3080 36bf999c 2020-07-23 stsp blob2);
3081 36bf999c 2020-07-23 stsp if (err)
3082 36bf999c 2020-07-23 stsp goto done;
3083 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3084 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3085 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3086 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3087 36bf999c 2020-07-23 stsp free(link_target2);
3088 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3089 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3090 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3091 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3092 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3093 526a746f 2020-07-23 stsp a->progress_arg);
3094 dfe9fba0 2020-07-23 stsp } else {
3095 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3096 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3097 526a746f 2020-07-23 stsp }
3098 dfe9fba0 2020-07-23 stsp if (err)
3099 dfe9fba0 2020-07-23 stsp goto done;
3100 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3101 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3102 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3103 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3104 234035bc 2019-06-01 stsp if (err)
3105 234035bc 2019-06-01 stsp goto done;
3106 234035bc 2019-06-01 stsp }
3107 234035bc 2019-06-01 stsp } else {
3108 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3109 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3110 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3111 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3112 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3113 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3114 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3115 2e1fa222 2020-07-23 stsp } else {
3116 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3117 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3118 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3119 2e1fa222 2020-07-23 stsp }
3120 234035bc 2019-06-01 stsp if (err)
3121 234035bc 2019-06-01 stsp goto done;
3122 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3123 234035bc 2019-06-01 stsp if (err)
3124 234035bc 2019-06-01 stsp goto done;
3125 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3126 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3127 3969253a 2020-03-07 stsp if (err) {
3128 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3129 3969253a 2020-03-07 stsp goto done;
3130 3969253a 2020-03-07 stsp }
3131 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3132 2b92fad7 2019-06-02 stsp if (err) {
3133 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3134 2b92fad7 2019-06-02 stsp goto done;
3135 2b92fad7 2019-06-02 stsp }
3136 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3137 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3138 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3139 2e1fa222 2020-07-23 stsp }
3140 234035bc 2019-06-01 stsp }
3141 234035bc 2019-06-01 stsp }
3142 234035bc 2019-06-01 stsp done:
3143 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3144 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3145 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3146 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3147 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3148 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3149 1af628f4 2021-06-03 stsp free(id_str);
3150 54d5be07 2021-06-03 stsp free(label_deriv2);
3151 234035bc 2019-06-01 stsp free(ondisk_path);
3152 234035bc 2019-06-01 stsp return err;
3153 234035bc 2019-06-01 stsp }
3154 234035bc 2019-06-01 stsp
3155 69de9dd4 2021-09-03 stsp static const struct got_error *
3156 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3157 69de9dd4 2021-09-03 stsp {
3158 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3159 69de9dd4 2021-09-03 stsp
3160 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3161 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3162 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3163 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3164 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3165 69de9dd4 2021-09-03 stsp
3166 69de9dd4 2021-09-03 stsp return NULL;
3167 69de9dd4 2021-09-03 stsp }
3168 69de9dd4 2021-09-03 stsp
3169 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3170 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3171 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3172 234035bc 2019-06-01 stsp struct got_repository *repo;
3173 234035bc 2019-06-01 stsp };
3174 234035bc 2019-06-01 stsp
3175 234035bc 2019-06-01 stsp static const struct got_error *
3176 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3177 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3178 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3179 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3180 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3181 234035bc 2019-06-01 stsp {
3182 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3183 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3184 234035bc 2019-06-01 stsp unsigned char status;
3185 234035bc 2019-06-01 stsp struct stat sb;
3186 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3187 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3188 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3189 234035bc 2019-06-01 stsp char *ondisk_path;
3190 234035bc 2019-06-01 stsp
3191 69de9dd4 2021-09-03 stsp if (id == NULL)
3192 69de9dd4 2021-09-03 stsp return NULL;
3193 234035bc 2019-06-01 stsp
3194 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3195 69de9dd4 2021-09-03 stsp if (ie == NULL)
3196 69de9dd4 2021-09-03 stsp return NULL;
3197 69de9dd4 2021-09-03 stsp
3198 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3199 234035bc 2019-06-01 stsp == -1)
3200 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3201 234035bc 2019-06-01 stsp
3202 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3203 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3204 5546d466 2021-09-02 stsp free(ondisk_path);
3205 234035bc 2019-06-01 stsp if (err)
3206 234035bc 2019-06-01 stsp return err;
3207 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3208 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3209 234035bc 2019-06-01 stsp
3210 234035bc 2019-06-01 stsp return NULL;
3211 234035bc 2019-06-01 stsp }
3212 234035bc 2019-06-01 stsp
3213 818c7501 2019-07-11 stsp static const struct got_error *
3214 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3215 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3216 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3217 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3218 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3219 234035bc 2019-06-01 stsp {
3220 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3221 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3222 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3223 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3224 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3225 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3226 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3227 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3228 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3229 234035bc 2019-06-01 stsp
3230 03415a1a 2019-06-02 stsp if (commit_id1) {
3231 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3232 a44927cc 2022-04-07 stsp if (err)
3233 a44927cc 2022-04-07 stsp goto done;
3234 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3235 03415a1a 2019-06-02 stsp worktree->path_prefix);
3236 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3237 03415a1a 2019-06-02 stsp goto done;
3238 69d57f3d 2020-07-31 stsp }
3239 69d57f3d 2020-07-31 stsp if (tree_id1) {
3240 69d57f3d 2020-07-31 stsp char *id_str;
3241 03415a1a 2019-06-02 stsp
3242 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3243 03415a1a 2019-06-02 stsp if (err)
3244 03415a1a 2019-06-02 stsp goto done;
3245 f69721c3 2019-10-21 stsp
3246 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3247 f69721c3 2019-10-21 stsp if (err)
3248 f69721c3 2019-10-21 stsp goto done;
3249 f69721c3 2019-10-21 stsp
3250 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3251 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3252 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3253 f69721c3 2019-10-21 stsp free(id_str);
3254 f69721c3 2019-10-21 stsp goto done;
3255 f69721c3 2019-10-21 stsp }
3256 f69721c3 2019-10-21 stsp free(id_str);
3257 b72706c3 2022-06-01 stsp
3258 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3259 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3260 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3261 b72706c3 2022-06-01 stsp goto done;
3262 b72706c3 2022-06-01 stsp }
3263 03415a1a 2019-06-02 stsp }
3264 234035bc 2019-06-01 stsp
3265 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3266 a44927cc 2022-04-07 stsp if (err)
3267 a44927cc 2022-04-07 stsp goto done;
3268 a44927cc 2022-04-07 stsp
3269 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3270 234035bc 2019-06-01 stsp worktree->path_prefix);
3271 234035bc 2019-06-01 stsp if (err)
3272 234035bc 2019-06-01 stsp goto done;
3273 234035bc 2019-06-01 stsp
3274 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3275 234035bc 2019-06-01 stsp if (err)
3276 234035bc 2019-06-01 stsp goto done;
3277 234035bc 2019-06-01 stsp
3278 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3279 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3280 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3281 f9d37699 2022-06-28 stsp goto done;
3282 f9d37699 2022-06-28 stsp }
3283 f9d37699 2022-06-28 stsp
3284 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3285 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3286 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3287 b72706c3 2022-06-01 stsp goto done;
3288 b72706c3 2022-06-01 stsp }
3289 b72706c3 2022-06-01 stsp
3290 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3291 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3292 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3293 f9d37699 2022-06-28 stsp goto done;
3294 f9d37699 2022-06-28 stsp }
3295 f9d37699 2022-06-28 stsp
3296 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3297 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3298 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3299 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3300 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3301 69de9dd4 2021-09-03 stsp if (err)
3302 69de9dd4 2021-09-03 stsp goto done;
3303 69de9dd4 2021-09-03 stsp
3304 234035bc 2019-06-01 stsp arg.worktree = worktree;
3305 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3306 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3307 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3308 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3309 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3310 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3311 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3312 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3313 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3314 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3315 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3316 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3317 af12c6b9 2019-06-04 stsp err = sync_err;
3318 234035bc 2019-06-01 stsp done:
3319 a44927cc 2022-04-07 stsp if (commit1)
3320 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3321 a44927cc 2022-04-07 stsp if (commit2)
3322 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3323 234035bc 2019-06-01 stsp if (tree1)
3324 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3325 234035bc 2019-06-01 stsp if (tree2)
3326 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3327 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3328 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3329 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3330 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3331 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3332 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3333 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3334 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3335 f69721c3 2019-10-21 stsp free(label_orig);
3336 818c7501 2019-07-11 stsp return err;
3337 818c7501 2019-07-11 stsp }
3338 234035bc 2019-06-01 stsp
3339 818c7501 2019-07-11 stsp const struct got_error *
3340 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3341 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3342 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3343 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3344 818c7501 2019-07-11 stsp {
3345 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3346 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3347 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3348 818c7501 2019-07-11 stsp
3349 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3350 818c7501 2019-07-11 stsp if (err)
3351 818c7501 2019-07-11 stsp return err;
3352 818c7501 2019-07-11 stsp
3353 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3354 818c7501 2019-07-11 stsp if (err)
3355 818c7501 2019-07-11 stsp goto done;
3356 818c7501 2019-07-11 stsp
3357 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3358 69de9dd4 2021-09-03 stsp worktree);
3359 818c7501 2019-07-11 stsp if (err)
3360 818c7501 2019-07-11 stsp goto done;
3361 818c7501 2019-07-11 stsp
3362 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3363 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3364 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3365 818c7501 2019-07-11 stsp done:
3366 818c7501 2019-07-11 stsp if (fileindex)
3367 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3368 818c7501 2019-07-11 stsp free(fileindex_path);
3369 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3370 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3371 234035bc 2019-06-01 stsp err = unlockerr;
3372 234035bc 2019-06-01 stsp return err;
3373 234035bc 2019-06-01 stsp }
3374 234035bc 2019-06-01 stsp
3375 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3376 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3377 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3378 927df6b7 2019-02-10 stsp const char *status_path;
3379 927df6b7 2019-02-10 stsp size_t status_path_len;
3380 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3381 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3382 f8d1f275 2019-02-04 stsp void *status_arg;
3383 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3384 f8d1f275 2019-02-04 stsp void *cancel_arg;
3385 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3386 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3387 f2a9dc41 2019-12-13 tracey int report_unchanged;
3388 3143d852 2020-06-25 stsp int no_ignores;
3389 f8d1f275 2019-02-04 stsp };
3390 88d0e355 2019-08-03 stsp
3391 f8d1f275 2019-02-04 stsp static const struct got_error *
3392 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3393 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3394 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3395 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3396 927df6b7 2019-02-10 stsp {
3397 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3398 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3399 2c4740ad 2023-02-12 mark unsigned char staged_status;
3400 927df6b7 2019-02-10 stsp struct stat sb;
3401 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3402 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3403 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3404 927df6b7 2019-02-10 stsp
3405 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3406 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3407 98eaaa12 2019-08-03 stsp if (err)
3408 98eaaa12 2019-08-03 stsp return err;
3409 98eaaa12 2019-08-03 stsp
3410 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3411 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3412 98eaaa12 2019-08-03 stsp return NULL;
3413 98eaaa12 2019-08-03 stsp
3414 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3415 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3416 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3417 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3418 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3419 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3420 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3421 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3422 98eaaa12 2019-08-03 stsp }
3423 98eaaa12 2019-08-03 stsp
3424 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3425 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3426 927df6b7 2019-02-10 stsp }
3427 927df6b7 2019-02-10 stsp
3428 927df6b7 2019-02-10 stsp static const struct got_error *
3429 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3430 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3431 f8d1f275 2019-02-04 stsp {
3432 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3433 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3434 f8d1f275 2019-02-04 stsp char *abspath;
3435 f8d1f275 2019-02-04 stsp
3436 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3437 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3438 0584f854 2019-04-06 stsp
3439 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3440 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3441 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3442 927df6b7 2019-02-10 stsp return NULL;
3443 927df6b7 2019-02-10 stsp
3444 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3445 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3446 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3447 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3448 f8d1f275 2019-02-04 stsp } else {
3449 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3450 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3451 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3452 f8d1f275 2019-02-04 stsp }
3453 f8d1f275 2019-02-04 stsp
3454 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3455 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3456 f8d1f275 2019-02-04 stsp free(abspath);
3457 9d31a1d8 2018-03-11 stsp return err;
3458 9d31a1d8 2018-03-11 stsp }
3459 f8d1f275 2019-02-04 stsp
3460 f8d1f275 2019-02-04 stsp static const struct got_error *
3461 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3462 f8d1f275 2019-02-04 stsp {
3463 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3464 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3465 2ec1f75b 2019-03-26 stsp unsigned char status;
3466 927df6b7 2019-02-10 stsp
3467 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3468 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3469 0584f854 2019-04-06 stsp
3470 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3471 927df6b7 2019-02-10 stsp return NULL;
3472 927df6b7 2019-02-10 stsp
3473 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3474 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3475 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3476 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3477 2ec1f75b 2019-03-26 stsp else
3478 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3479 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3480 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3481 6841da00 2019-08-08 stsp }
3482 6841da00 2019-08-08 stsp
3483 336075a4 2022-06-25 op static void
3484 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3485 6841da00 2019-08-08 stsp {
3486 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3487 6841da00 2019-08-08 stsp
3488 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3489 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3490 d8bacb93 2023-01-10 mark
3491 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3492 6841da00 2019-08-08 stsp }
3493 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3494 6841da00 2019-08-08 stsp }
3495 6841da00 2019-08-08 stsp
3496 6841da00 2019-08-08 stsp static const struct got_error *
3497 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3498 6841da00 2019-08-08 stsp {
3499 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3500 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3501 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3502 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3503 6841da00 2019-08-08 stsp size_t linesize = 0;
3504 6841da00 2019-08-08 stsp ssize_t linelen;
3505 6841da00 2019-08-08 stsp
3506 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3507 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3508 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3509 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3510 6841da00 2019-08-08 stsp
3511 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3512 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3513 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3514 bd8de430 2019-10-04 stsp
3515 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3516 bd8de430 2019-10-04 stsp if (line[0] == '#')
3517 bd8de430 2019-10-04 stsp continue;
3518 bd8de430 2019-10-04 stsp
3519 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3520 bd8de430 2019-10-04 stsp if (line[0] == '!')
3521 bd8de430 2019-10-04 stsp continue;
3522 bd8de430 2019-10-04 stsp
3523 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3524 6841da00 2019-08-08 stsp line) == -1) {
3525 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3526 6841da00 2019-08-08 stsp goto done;
3527 6841da00 2019-08-08 stsp }
3528 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3529 6841da00 2019-08-08 stsp if (err)
3530 6841da00 2019-08-08 stsp goto done;
3531 6841da00 2019-08-08 stsp }
3532 6841da00 2019-08-08 stsp if (ferror(f)) {
3533 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3534 6841da00 2019-08-08 stsp goto done;
3535 6841da00 2019-08-08 stsp }
3536 6841da00 2019-08-08 stsp
3537 6841da00 2019-08-08 stsp dirpath = strdup(path);
3538 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3539 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3540 6841da00 2019-08-08 stsp goto done;
3541 6841da00 2019-08-08 stsp }
3542 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3543 6841da00 2019-08-08 stsp done:
3544 6841da00 2019-08-08 stsp free(line);
3545 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3546 6841da00 2019-08-08 stsp free(dirpath);
3547 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3548 6841da00 2019-08-08 stsp }
3549 6841da00 2019-08-08 stsp return err;
3550 249b637c 2023-02-20 stsp }
3551 249b637c 2023-02-20 stsp
3552 249b637c 2023-02-20 stsp static int
3553 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3554 249b637c 2023-02-20 stsp int flags)
3555 249b637c 2023-02-20 stsp {
3556 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3557 249b637c 2023-02-20 stsp
3558 249b637c 2023-02-20 stsp /*
3559 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3560 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3561 249b637c 2023-02-20 stsp */
3562 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3563 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3564 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3565 249b637c 2023-02-20 stsp
3566 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3567 249b637c 2023-02-20 stsp }
3568 249b637c 2023-02-20 stsp
3569 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3570 6841da00 2019-08-08 stsp }
3571 6841da00 2019-08-08 stsp
3572 336075a4 2022-06-25 op static int
3573 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3574 6841da00 2019-08-08 stsp {
3575 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3576 bd8de430 2019-10-04 stsp
3577 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3578 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3579 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3580 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3581 bd8de430 2019-10-04 stsp
3582 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3583 249b637c 2023-02-20 stsp const char *p;
3584 6841da00 2019-08-08 stsp
3585 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3586 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3587 bd8de430 2019-10-04 stsp continue;
3588 bd8de430 2019-10-04 stsp p = path;
3589 bd8de430 2019-10-04 stsp while (*p) {
3590 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3591 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3592 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3593 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3594 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3595 bd8de430 2019-10-04 stsp p++;
3596 bd8de430 2019-10-04 stsp while (*p == '/')
3597 bd8de430 2019-10-04 stsp p++;
3598 bd8de430 2019-10-04 stsp continue;
3599 bd8de430 2019-10-04 stsp }
3600 bd8de430 2019-10-04 stsp return 1;
3601 bd8de430 2019-10-04 stsp }
3602 bd8de430 2019-10-04 stsp }
3603 bd8de430 2019-10-04 stsp }
3604 bd8de430 2019-10-04 stsp
3605 6841da00 2019-08-08 stsp /*
3606 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3607 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3608 6841da00 2019-08-08 stsp * ignores backwards.
3609 6841da00 2019-08-08 stsp */
3610 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3611 6841da00 2019-08-08 stsp while (pe) {
3612 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3613 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3614 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3615 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3616 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3617 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3618 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3619 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3620 249b637c 2023-02-20 stsp path, flags))
3621 6841da00 2019-08-08 stsp continue;
3622 6841da00 2019-08-08 stsp return 1;
3623 6841da00 2019-08-08 stsp }
3624 6841da00 2019-08-08 stsp }
3625 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3626 6841da00 2019-08-08 stsp }
3627 6841da00 2019-08-08 stsp
3628 6841da00 2019-08-08 stsp return 0;
3629 6841da00 2019-08-08 stsp }
3630 6841da00 2019-08-08 stsp
3631 6841da00 2019-08-08 stsp static const struct got_error *
3632 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3633 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3634 6841da00 2019-08-08 stsp {
3635 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3636 6841da00 2019-08-08 stsp char *ignorespath;
3637 886cec17 2019-12-15 stsp int fd = -1;
3638 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3639 6841da00 2019-08-08 stsp
3640 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3641 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3642 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3643 6841da00 2019-08-08 stsp
3644 886cec17 2019-12-15 stsp if (dirfd != -1) {
3645 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3646 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3647 886cec17 2019-12-15 stsp if (fd == -1) {
3648 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3649 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3650 886cec17 2019-12-15 stsp ignorespath);
3651 886cec17 2019-12-15 stsp } else {
3652 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3653 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3654 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3655 886cec17 2019-12-15 stsp ignorespath);
3656 886cec17 2019-12-15 stsp else {
3657 886cec17 2019-12-15 stsp fd = -1;
3658 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3659 886cec17 2019-12-15 stsp }
3660 886cec17 2019-12-15 stsp }
3661 886cec17 2019-12-15 stsp } else {
3662 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3663 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3664 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3665 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3666 886cec17 2019-12-15 stsp ignorespath);
3667 886cec17 2019-12-15 stsp } else
3668 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3669 886cec17 2019-12-15 stsp }
3670 6841da00 2019-08-08 stsp
3671 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3672 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3673 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3674 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3675 6841da00 2019-08-08 stsp free(ignorespath);
3676 6841da00 2019-08-08 stsp return err;
3677 f8d1f275 2019-02-04 stsp }
3678 f8d1f275 2019-02-04 stsp
3679 f8d1f275 2019-02-04 stsp static const struct got_error *
3680 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3681 62da3196 2021-10-01 stsp int dirfd)
3682 f8d1f275 2019-02-04 stsp {
3683 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3684 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3685 f8d1f275 2019-02-04 stsp char *path = NULL;
3686 62da3196 2021-10-01 stsp
3687 62da3196 2021-10-01 stsp if (ignore != NULL)
3688 62da3196 2021-10-01 stsp *ignore = 0;
3689 f8d1f275 2019-02-04 stsp
3690 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3691 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3692 0584f854 2019-04-06 stsp
3693 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3694 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3695 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3696 f8d1f275 2019-02-04 stsp } else {
3697 f8d1f275 2019-02-04 stsp path = de->d_name;
3698 f8d1f275 2019-02-04 stsp }
3699 f8d1f275 2019-02-04 stsp
3700 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3701 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3702 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3703 62da3196 2021-10-01 stsp *ignore = 1;
3704 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3705 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3706 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3707 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3708 f8d1f275 2019-02-04 stsp if (parent_path[0])
3709 f8d1f275 2019-02-04 stsp free(path);
3710 b72f483a 2019-02-05 stsp return err;
3711 f8d1f275 2019-02-04 stsp }
3712 f8d1f275 2019-02-04 stsp
3713 347d1d3e 2019-07-12 stsp static const struct got_error *
3714 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3715 3143d852 2020-06-25 stsp {
3716 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3717 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3718 3143d852 2020-06-25 stsp
3719 3143d852 2020-06-25 stsp if (a->no_ignores)
3720 3143d852 2020-06-25 stsp return NULL;
3721 3143d852 2020-06-25 stsp
3722 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3723 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3724 3143d852 2020-06-25 stsp if (err)
3725 3143d852 2020-06-25 stsp return err;
3726 3143d852 2020-06-25 stsp
3727 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3728 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3729 3143d852 2020-06-25 stsp
3730 3143d852 2020-06-25 stsp return err;
3731 3143d852 2020-06-25 stsp }
3732 3143d852 2020-06-25 stsp
3733 3143d852 2020-06-25 stsp static const struct got_error *
3734 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3735 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3736 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3737 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3738 abb4604f 2019-07-27 stsp {
3739 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3740 abb4604f 2019-07-27 stsp struct stat sb;
3741 ff56836b 2021-07-08 stsp
3742 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3743 abb4604f 2019-07-27 stsp if (ie)
3744 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3745 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3746 abb4604f 2019-07-27 stsp
3747 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3748 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3749 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3750 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3751 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3752 abb4604f 2019-07-27 stsp }
3753 abb4604f 2019-07-27 stsp
3754 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3755 916237f3 2022-02-11 stsp return NULL;
3756 916237f3 2022-02-11 stsp
3757 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3758 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3759 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3760 abb4604f 2019-07-27 stsp
3761 abb4604f 2019-07-27 stsp return NULL;
3762 3143d852 2020-06-25 stsp }
3763 3143d852 2020-06-25 stsp
3764 3143d852 2020-06-25 stsp static const struct got_error *
3765 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3766 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3767 3143d852 2020-06-25 stsp {
3768 3143d852 2020-06-25 stsp const struct got_error *err;
3769 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3770 3143d852 2020-06-25 stsp
3771 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3772 3143d852 2020-06-25 stsp ".cvsignore");
3773 3143d852 2020-06-25 stsp if (err)
3774 3143d852 2020-06-25 stsp return err;
3775 3143d852 2020-06-25 stsp
3776 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3777 3143d852 2020-06-25 stsp ".gitignore");
3778 3143d852 2020-06-25 stsp if (err)
3779 3143d852 2020-06-25 stsp return err;
3780 3143d852 2020-06-25 stsp
3781 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3782 3143d852 2020-06-25 stsp if (err) {
3783 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3784 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3785 3143d852 2020-06-25 stsp return err;
3786 3143d852 2020-06-25 stsp }
3787 3143d852 2020-06-25 stsp for (;;) {
3788 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3789 3143d852 2020-06-25 stsp ".cvsignore");
3790 3143d852 2020-06-25 stsp if (err)
3791 3143d852 2020-06-25 stsp break;
3792 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3793 3143d852 2020-06-25 stsp ".gitignore");
3794 3143d852 2020-06-25 stsp if (err)
3795 3143d852 2020-06-25 stsp break;
3796 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3797 3143d852 2020-06-25 stsp if (err) {
3798 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3799 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3800 3143d852 2020-06-25 stsp break;
3801 3143d852 2020-06-25 stsp }
3802 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3803 ff56836b 2021-07-08 stsp break;
3804 b737c85a 2020-06-26 stsp free(parent_path);
3805 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3806 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3807 3143d852 2020-06-25 stsp }
3808 3143d852 2020-06-25 stsp
3809 b737c85a 2020-06-26 stsp free(parent_path);
3810 b737c85a 2020-06-26 stsp free(next_parent_path);
3811 3143d852 2020-06-25 stsp return err;
3812 abb4604f 2019-07-27 stsp }
3813 abb4604f 2019-07-27 stsp
3814 abb4604f 2019-07-27 stsp static const struct got_error *
3815 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3816 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3817 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3818 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3819 f2a9dc41 2019-12-13 tracey int report_unchanged)
3820 f8d1f275 2019-02-04 stsp {
3821 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3822 6fc93f37 2019-12-13 stsp int fd = -1;
3823 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3824 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3825 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3826 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3827 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3828 3143d852 2020-06-25 stsp
3829 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3830 f8d1f275 2019-02-04 stsp
3831 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3832 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3833 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3834 8dc303cc 2019-07-27 stsp
3835 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3836 a84c0d30 2022-03-12 stsp if (ie) {
3837 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3838 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3839 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3840 a84c0d30 2022-03-12 stsp goto done;
3841 a84c0d30 2022-03-12 stsp }
3842 a84c0d30 2022-03-12 stsp
3843 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3844 6fc93f37 2019-12-13 stsp if (fd == -1) {
3845 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3846 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3847 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3848 ff56836b 2021-07-08 stsp else {
3849 ff56836b 2021-07-08 stsp if (!no_ignores) {
3850 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3851 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3852 ff56836b 2021-07-08 stsp if (err)
3853 ff56836b 2021-07-08 stsp goto done;
3854 ff56836b 2021-07-08 stsp }
3855 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3856 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3857 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3858 ff56836b 2021-07-08 stsp }
3859 abb4604f 2019-07-27 stsp } else {
3860 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3861 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3862 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3863 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3864 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3865 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3866 abb4604f 2019-07-27 stsp arg.status_path = path;
3867 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3868 abb4604f 2019-07-27 stsp arg.repo = repo;
3869 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3870 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3871 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3872 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3873 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3874 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3875 022fae89 2019-12-06 tracey if (!no_ignores) {
3876 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3877 3143d852 2020-06-25 stsp worktree->root_path, path);
3878 3143d852 2020-06-25 stsp if (err)
3879 3143d852 2020-06-25 stsp goto done;
3880 022fae89 2019-12-06 tracey }
3881 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3882 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3883 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3884 927df6b7 2019-02-10 stsp }
3885 3143d852 2020-06-25 stsp done:
3886 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3887 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3888 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3889 927df6b7 2019-02-10 stsp free(ondisk_path);
3890 347d1d3e 2019-07-12 stsp return err;
3891 347d1d3e 2019-07-12 stsp }
3892 347d1d3e 2019-07-12 stsp
3893 347d1d3e 2019-07-12 stsp const struct got_error *
3894 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3895 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3896 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3897 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3898 347d1d3e 2019-07-12 stsp {
3899 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3900 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3901 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3902 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3903 347d1d3e 2019-07-12 stsp
3904 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3905 347d1d3e 2019-07-12 stsp if (err)
3906 347d1d3e 2019-07-12 stsp return err;
3907 347d1d3e 2019-07-12 stsp
3908 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3909 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3910 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3911 f6343036 2021-06-22 stsp no_ignores, 0);
3912 72ea6654 2019-07-27 stsp if (err)
3913 72ea6654 2019-07-27 stsp break;
3914 72ea6654 2019-07-27 stsp }
3915 f8d1f275 2019-02-04 stsp free(fileindex_path);
3916 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3917 f8d1f275 2019-02-04 stsp return err;
3918 f8d1f275 2019-02-04 stsp }
3919 6c7ab921 2019-03-18 stsp
3920 6c7ab921 2019-03-18 stsp const struct got_error *
3921 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3922 6c7ab921 2019-03-18 stsp const char *arg)
3923 6c7ab921 2019-03-18 stsp {
3924 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3925 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3926 6c7ab921 2019-03-18 stsp size_t len;
3927 00bb5ea0 2020-07-23 stsp struct stat sb;
3928 01740607 2020-11-04 stsp char *abspath = NULL;
3929 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3930 6c7ab921 2019-03-18 stsp
3931 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3932 6c7ab921 2019-03-18 stsp
3933 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3934 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3935 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3936 00bb5ea0 2020-07-23 stsp
3937 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3938 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3939 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3940 00bb5ea0 2020-07-23 stsp goto done;
3941 00bb5ea0 2020-07-23 stsp }
3942 727173c3 2020-11-06 stsp sb.st_mode = 0;
3943 00bb5ea0 2020-07-23 stsp }
3944 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3945 00bb5ea0 2020-07-23 stsp /*
3946 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3947 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3948 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3949 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3950 00bb5ea0 2020-07-23 stsp */
3951 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3952 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3953 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3954 00bb5ea0 2020-07-23 stsp goto done;
3955 00bb5ea0 2020-07-23 stsp }
3956 00bb5ea0 2020-07-23 stsp
3957 00bb5ea0 2020-07-23 stsp }
3958 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3959 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3960 00bb5ea0 2020-07-23 stsp if (err)
3961 d0710d08 2019-07-22 stsp goto done;
3962 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3963 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3964 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3965 00bb5ea0 2020-07-23 stsp goto done;
3966 00bb5ea0 2020-07-23 stsp }
3967 00bb5ea0 2020-07-23 stsp } else {
3968 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3969 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3970 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3971 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3972 00bb5ea0 2020-07-23 stsp goto done;
3973 00bb5ea0 2020-07-23 stsp }
3974 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3975 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3976 01740607 2020-11-04 stsp goto done;
3977 01740607 2020-11-04 stsp }
3978 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3979 01740607 2020-11-04 stsp sizeof(canonpath));
3980 01740607 2020-11-04 stsp if (err)
3981 00bb5ea0 2020-07-23 stsp goto done;
3982 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3983 01740607 2020-11-04 stsp if (resolved == NULL) {
3984 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3985 01740607 2020-11-04 stsp goto done;
3986 00bb5ea0 2020-07-23 stsp }
3987 d0710d08 2019-07-22 stsp }
3988 d0710d08 2019-07-22 stsp }
3989 6c7ab921 2019-03-18 stsp
3990 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3991 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3992 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3993 6c7ab921 2019-03-18 stsp goto done;
3994 6c7ab921 2019-03-18 stsp }
3995 6c7ab921 2019-03-18 stsp
3996 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3997 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3998 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3999 f6d88e1a 2019-05-29 stsp if (err)
4000 f6d88e1a 2019-05-29 stsp goto done;
4001 f6d88e1a 2019-05-29 stsp } else {
4002 f6d88e1a 2019-05-29 stsp path = strdup("");
4003 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4004 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4005 f6d88e1a 2019-05-29 stsp goto done;
4006 f6d88e1a 2019-05-29 stsp }
4007 6c7ab921 2019-03-18 stsp }
4008 6c7ab921 2019-03-18 stsp
4009 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4010 6c7ab921 2019-03-18 stsp len = strlen(path);
4011 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4012 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4013 6c7ab921 2019-03-18 stsp len--;
4014 6c7ab921 2019-03-18 stsp }
4015 6c7ab921 2019-03-18 stsp done:
4016 01740607 2020-11-04 stsp free(abspath);
4017 6c7ab921 2019-03-18 stsp free(resolved);
4018 d0710d08 2019-07-22 stsp free(cwd);
4019 6c7ab921 2019-03-18 stsp if (err == NULL)
4020 6c7ab921 2019-03-18 stsp *wt_path = path;
4021 6c7ab921 2019-03-18 stsp else
4022 6c7ab921 2019-03-18 stsp free(path);
4023 d00136be 2019-03-26 stsp return err;
4024 d00136be 2019-03-26 stsp }
4025 d00136be 2019-03-26 stsp
4026 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4027 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4028 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4029 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4030 4e68cba3 2019-11-23 stsp void *progress_arg;
4031 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4032 4e68cba3 2019-11-23 stsp };
4033 4e68cba3 2019-11-23 stsp
4034 4e68cba3 2019-11-23 stsp static const struct got_error *
4035 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4036 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4037 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4038 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4039 07f5b47a 2019-06-02 stsp {
4040 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4041 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4042 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4043 6d022e97 2019-08-04 stsp struct stat sb;
4044 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4045 07f5b47a 2019-06-02 stsp
4046 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4047 dbb83fbd 2019-12-12 stsp relpath) == -1)
4048 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4049 dbb83fbd 2019-12-12 stsp
4050 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4051 6d022e97 2019-08-04 stsp if (ie) {
4052 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4053 12463d8b 2019-12-13 stsp de_name, a->repo);
4054 6d022e97 2019-08-04 stsp if (err)
4055 dbb83fbd 2019-12-12 stsp goto done;
4056 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4057 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4058 dbb83fbd 2019-12-12 stsp goto done;
4059 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4060 dbb83fbd 2019-12-12 stsp if (err)
4061 dbb83fbd 2019-12-12 stsp goto done;
4062 6d022e97 2019-08-04 stsp }
4063 07f5b47a 2019-06-02 stsp
4064 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4065 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4066 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4067 9b4603c0 2022-01-31 stsp else
4068 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4069 dbb83fbd 2019-12-12 stsp goto done;
4070 4e68cba3 2019-11-23 stsp }
4071 07f5b47a 2019-06-02 stsp
4072 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4073 4e68cba3 2019-11-23 stsp if (err)
4074 4e68cba3 2019-11-23 stsp goto done;
4075 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4076 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4077 3969253a 2020-03-07 stsp if (err) {
4078 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4079 3969253a 2020-03-07 stsp goto done;
4080 3969253a 2020-03-07 stsp }
4081 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4082 07f5b47a 2019-06-02 stsp if (err) {
4083 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4084 4e68cba3 2019-11-23 stsp goto done;
4085 07f5b47a 2019-06-02 stsp }
4086 4e68cba3 2019-11-23 stsp done:
4087 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4088 dbb83fbd 2019-12-12 stsp if (err)
4089 dbb83fbd 2019-12-12 stsp return err;
4090 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4091 dbb83fbd 2019-12-12 stsp return NULL;
4092 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4093 07f5b47a 2019-06-02 stsp }
4094 07f5b47a 2019-06-02 stsp
4095 d00136be 2019-03-26 stsp const struct got_error *
4096 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4097 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4098 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4099 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4100 d00136be 2019-03-26 stsp {
4101 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4102 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4103 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4104 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4105 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4106 d00136be 2019-03-26 stsp
4107 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4108 d00136be 2019-03-26 stsp if (err)
4109 d00136be 2019-03-26 stsp return err;
4110 d00136be 2019-03-26 stsp
4111 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4112 d00136be 2019-03-26 stsp if (err)
4113 d00136be 2019-03-26 stsp goto done;
4114 d00136be 2019-03-26 stsp
4115 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4116 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4117 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4118 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4119 4e68cba3 2019-11-23 stsp saa.repo = repo;
4120 4e68cba3 2019-11-23 stsp
4121 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4122 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4123 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4124 1dd54920 2019-05-11 stsp if (err)
4125 af12c6b9 2019-06-04 stsp break;
4126 1dd54920 2019-05-11 stsp }
4127 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4128 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4129 af12c6b9 2019-06-04 stsp err = sync_err;
4130 d00136be 2019-03-26 stsp done:
4131 fb399478 2019-07-12 stsp free(fileindex_path);
4132 d00136be 2019-03-26 stsp if (fileindex)
4133 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4134 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4135 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4136 d00136be 2019-03-26 stsp err = unlockerr;
4137 6c7ab921 2019-03-18 stsp return err;
4138 6c7ab921 2019-03-18 stsp }
4139 17ed4618 2019-06-02 stsp
4140 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4141 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4142 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4143 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4144 f2a9dc41 2019-12-13 tracey void *progress_arg;
4145 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4146 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4147 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4148 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4149 766841c2 2020-08-13 stsp const char *status_codes;
4150 f2a9dc41 2019-12-13 tracey };
4151 f2a9dc41 2019-12-13 tracey
4152 f2a9dc41 2019-12-13 tracey static const struct got_error *
4153 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4154 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4155 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4156 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4157 17ed4618 2019-06-02 stsp {
4158 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4159 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4160 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4161 17ed4618 2019-06-02 stsp struct stat sb;
4162 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4163 4e12cd97 2022-01-25 stsp
4164 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4165 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4166 4e12cd97 2022-01-25 stsp return NULL;
4167 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4168 4e12cd97 2022-01-25 stsp }
4169 17ed4618 2019-06-02 stsp
4170 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4171 17ed4618 2019-06-02 stsp if (ie == NULL)
4172 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4173 2ec1f75b 2019-03-26 stsp
4174 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4175 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4176 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4177 9acbc4fa 2019-08-03 stsp return NULL;
4178 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4179 9acbc4fa 2019-08-03 stsp }
4180 9acbc4fa 2019-08-03 stsp
4181 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4182 f2a9dc41 2019-12-13 tracey relpath) == -1)
4183 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4184 f2a9dc41 2019-12-13 tracey
4185 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4186 12463d8b 2019-12-13 stsp a->repo);
4187 17ed4618 2019-06-02 stsp if (err)
4188 f2a9dc41 2019-12-13 tracey goto done;
4189 17ed4618 2019-06-02 stsp
4190 766841c2 2020-08-13 stsp if (a->status_codes) {
4191 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4192 766841c2 2020-08-13 stsp int i;
4193 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4194 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4195 766841c2 2020-08-13 stsp break;
4196 766841c2 2020-08-13 stsp }
4197 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4198 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4199 766841c2 2020-08-13 stsp free(ondisk_path);
4200 766841c2 2020-08-13 stsp return NULL;
4201 766841c2 2020-08-13 stsp }
4202 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4203 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4204 766841c2 2020-08-13 stsp static char msg[64];
4205 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4206 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4207 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4208 766841c2 2020-08-13 stsp goto done;
4209 766841c2 2020-08-13 stsp }
4210 766841c2 2020-08-13 stsp }
4211 766841c2 2020-08-13 stsp
4212 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4213 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4214 f2a9dc41 2019-12-13 tracey goto done;
4215 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4216 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4217 f2a9dc41 2019-12-13 tracey goto done;
4218 f2a9dc41 2019-12-13 tracey }
4219 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4220 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4221 4e12cd97 2022-01-25 stsp goto done;
4222 4e12cd97 2022-01-25 stsp }
4223 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4224 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4225 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4226 f2a9dc41 2019-12-13 tracey goto done;
4227 f2a9dc41 2019-12-13 tracey }
4228 17ed4618 2019-06-02 stsp }
4229 17ed4618 2019-06-02 stsp
4230 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4231 20a2ad1c 2020-10-20 stsp size_t root_len;
4232 20a2ad1c 2020-10-20 stsp
4233 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4234 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4235 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4236 12463d8b 2019-12-13 stsp ondisk_path);
4237 12463d8b 2019-12-13 stsp goto done;
4238 12463d8b 2019-12-13 stsp }
4239 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4240 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4241 12463d8b 2019-12-13 stsp goto done;
4242 15341bfd 2020-03-05 tracey }
4243 15341bfd 2020-03-05 tracey
4244 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4245 20a2ad1c 2020-10-20 stsp do {
4246 20a2ad1c 2020-10-20 stsp char *parent;
4247 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4248 20a2ad1c 2020-10-20 stsp if (err)
4249 2513f20a 2020-10-20 stsp goto done;
4250 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4251 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4252 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4253 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4254 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4255 20a2ad1c 2020-10-20 stsp ondisk_path);
4256 15341bfd 2020-03-05 tracey break;
4257 15341bfd 2020-03-05 tracey }
4258 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4259 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4260 f2a9dc41 2019-12-13 tracey }
4261 17ed4618 2019-06-02 stsp
4262 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4263 f2a9dc41 2019-12-13 tracey done:
4264 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4265 f2a9dc41 2019-12-13 tracey if (err)
4266 f2a9dc41 2019-12-13 tracey return err;
4267 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4268 f2a9dc41 2019-12-13 tracey return NULL;
4269 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4270 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4271 17ed4618 2019-06-02 stsp }
4272 17ed4618 2019-06-02 stsp
4273 2ec1f75b 2019-03-26 stsp const struct got_error *
4274 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4275 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4276 766841c2 2020-08-13 stsp const char *status_codes,
4277 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4278 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4279 2ec1f75b 2019-03-26 stsp {
4280 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4281 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4282 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4283 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4284 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4285 2ec1f75b 2019-03-26 stsp
4286 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4287 2ec1f75b 2019-03-26 stsp if (err)
4288 2ec1f75b 2019-03-26 stsp return err;
4289 2ec1f75b 2019-03-26 stsp
4290 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4291 2ec1f75b 2019-03-26 stsp if (err)
4292 2ec1f75b 2019-03-26 stsp goto done;
4293 f2a9dc41 2019-12-13 tracey
4294 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4295 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4296 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4297 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4298 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4299 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4300 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4301 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4302 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4303 2ec1f75b 2019-03-26 stsp
4304 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4305 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4306 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4307 17ed4618 2019-06-02 stsp if (err)
4308 af12c6b9 2019-06-04 stsp break;
4309 2ec1f75b 2019-03-26 stsp }
4310 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4311 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4312 af12c6b9 2019-06-04 stsp err = sync_err;
4313 2ec1f75b 2019-03-26 stsp done:
4314 fb399478 2019-07-12 stsp free(fileindex_path);
4315 2ec1f75b 2019-03-26 stsp if (fileindex)
4316 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4317 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4318 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4319 2ec1f75b 2019-03-26 stsp err = unlockerr;
4320 33aa809d 2019-08-08 stsp return err;
4321 33aa809d 2019-08-08 stsp }
4322 33aa809d 2019-08-08 stsp
4323 33aa809d 2019-08-08 stsp static const struct got_error *
4324 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4325 33aa809d 2019-08-08 stsp {
4326 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4327 33aa809d 2019-08-08 stsp char *line = NULL;
4328 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4329 33aa809d 2019-08-08 stsp ssize_t linelen;
4330 33aa809d 2019-08-08 stsp
4331 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4332 33aa809d 2019-08-08 stsp if (linelen == -1) {
4333 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4334 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4335 33aa809d 2019-08-08 stsp goto done;
4336 33aa809d 2019-08-08 stsp }
4337 33aa809d 2019-08-08 stsp return NULL;
4338 33aa809d 2019-08-08 stsp }
4339 33aa809d 2019-08-08 stsp if (outfile) {
4340 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4341 33aa809d 2019-08-08 stsp if (n != linelen) {
4342 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4343 33aa809d 2019-08-08 stsp goto done;
4344 33aa809d 2019-08-08 stsp }
4345 33aa809d 2019-08-08 stsp }
4346 33aa809d 2019-08-08 stsp if (rejectfile) {
4347 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4348 33aa809d 2019-08-08 stsp if (n != linelen)
4349 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4350 33aa809d 2019-08-08 stsp }
4351 33aa809d 2019-08-08 stsp done:
4352 33aa809d 2019-08-08 stsp free(line);
4353 2ec1f75b 2019-03-26 stsp return err;
4354 2ec1f75b 2019-03-26 stsp }
4355 1f1abb7e 2019-08-08 stsp
4356 33aa809d 2019-08-08 stsp static const struct got_error *
4357 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4358 33aa809d 2019-08-08 stsp {
4359 33aa809d 2019-08-08 stsp char *line = NULL;
4360 33aa809d 2019-08-08 stsp size_t linesize = 0;
4361 33aa809d 2019-08-08 stsp ssize_t linelen;
4362 33aa809d 2019-08-08 stsp
4363 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4364 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4365 500467ff 2019-09-25 hiltjo if (ferror(f))
4366 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4367 500467ff 2019-09-25 hiltjo return NULL;
4368 500467ff 2019-09-25 hiltjo }
4369 33aa809d 2019-08-08 stsp free(line);
4370 33aa809d 2019-08-08 stsp return NULL;
4371 33aa809d 2019-08-08 stsp }
4372 33aa809d 2019-08-08 stsp
4373 33aa809d 2019-08-08 stsp static const struct got_error *
4374 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4375 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4376 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4377 abc59930 2021-09-05 naddy {
4378 33aa809d 2019-08-08 stsp const struct got_error *err;
4379 33aa809d 2019-08-08 stsp
4380 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4381 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4382 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4383 33aa809d 2019-08-08 stsp if (err)
4384 33aa809d 2019-08-08 stsp return err;
4385 33aa809d 2019-08-08 stsp (*line_cur1)++;
4386 33aa809d 2019-08-08 stsp }
4387 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4388 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4389 33aa809d 2019-08-08 stsp if (rejectfile)
4390 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4391 33aa809d 2019-08-08 stsp else
4392 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4393 33aa809d 2019-08-08 stsp if (err)
4394 33aa809d 2019-08-08 stsp return err;
4395 33aa809d 2019-08-08 stsp (*line_cur2)++;
4396 33aa809d 2019-08-08 stsp }
4397 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4398 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4399 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4400 33aa809d 2019-08-08 stsp if (err)
4401 33aa809d 2019-08-08 stsp return err;
4402 33aa809d 2019-08-08 stsp (*line_cur2)++;
4403 33aa809d 2019-08-08 stsp }
4404 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4405 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4406 33aa809d 2019-08-08 stsp if (rejectfile)
4407 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4408 33aa809d 2019-08-08 stsp else
4409 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4410 33aa809d 2019-08-08 stsp if (err)
4411 33aa809d 2019-08-08 stsp return err;
4412 33aa809d 2019-08-08 stsp (*line_cur1)++;
4413 33aa809d 2019-08-08 stsp }
4414 f1e81a05 2019-08-10 stsp
4415 f1e81a05 2019-08-10 stsp return NULL;
4416 f1e81a05 2019-08-10 stsp }
4417 f1e81a05 2019-08-10 stsp
4418 f1e81a05 2019-08-10 stsp static const struct got_error *
4419 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4420 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4421 f1e81a05 2019-08-10 stsp {
4422 f1e81a05 2019-08-10 stsp const struct got_error *err;
4423 f1e81a05 2019-08-10 stsp
4424 f1e81a05 2019-08-10 stsp if (outfile) {
4425 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4426 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4427 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4428 f1e81a05 2019-08-10 stsp if (err)
4429 f1e81a05 2019-08-10 stsp return err;
4430 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4431 f1e81a05 2019-08-10 stsp }
4432 f1e81a05 2019-08-10 stsp }
4433 f1e81a05 2019-08-10 stsp if (rejectfile) {
4434 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4435 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4436 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4437 f1e81a05 2019-08-10 stsp if (err)
4438 f1e81a05 2019-08-10 stsp return err;
4439 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4440 f1e81a05 2019-08-10 stsp }
4441 33aa809d 2019-08-08 stsp }
4442 33aa809d 2019-08-08 stsp
4443 33aa809d 2019-08-08 stsp return NULL;
4444 33aa809d 2019-08-08 stsp }
4445 33aa809d 2019-08-08 stsp
4446 33aa809d 2019-08-08 stsp static const struct got_error *
4447 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4448 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4449 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4450 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4451 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4452 33aa809d 2019-08-08 stsp {
4453 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4454 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4455 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4456 33aa809d 2019-08-08 stsp FILE *hunkfile;
4457 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4458 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4459 fe621944 2020-11-10 stsp int rc;
4460 33aa809d 2019-08-08 stsp
4461 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4462 33aa809d 2019-08-08 stsp
4463 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4464 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4465 fe621944 2020-11-10 stsp start_old = cc.left.start;
4466 fe621944 2020-11-10 stsp end_old = cc.left.end;
4467 fe621944 2020-11-10 stsp start_new = cc.right.start;
4468 fe621944 2020-11-10 stsp end_new = cc.right.end;
4469 33aa809d 2019-08-08 stsp
4470 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4471 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4472 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4473 33aa809d 2019-08-08 stsp
4474 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4475 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4476 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4477 33aa809d 2019-08-08 stsp
4478 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4479 fe621944 2020-11-10 stsp if (diff_state == NULL)
4480 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4481 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4482 fe621944 2020-11-10 stsp
4483 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4484 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4485 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4486 33aa809d 2019-08-08 stsp goto done;
4487 33aa809d 2019-08-08 stsp }
4488 fe621944 2020-11-10 stsp
4489 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4490 fe621944 2020-11-10 stsp diff_result, &cc);
4491 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4492 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4493 33aa809d 2019-08-08 stsp goto done;
4494 33aa809d 2019-08-08 stsp }
4495 fe621944 2020-11-10 stsp
4496 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4497 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4498 33aa809d 2019-08-08 stsp goto done;
4499 33aa809d 2019-08-08 stsp }
4500 33aa809d 2019-08-08 stsp
4501 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4502 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4503 33aa809d 2019-08-08 stsp if (err)
4504 33aa809d 2019-08-08 stsp goto done;
4505 33aa809d 2019-08-08 stsp
4506 33aa809d 2019-08-08 stsp switch (*choice) {
4507 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4508 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4509 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4510 33aa809d 2019-08-08 stsp break;
4511 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4512 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4513 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4514 33aa809d 2019-08-08 stsp break;
4515 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4516 33aa809d 2019-08-08 stsp break;
4517 33aa809d 2019-08-08 stsp default:
4518 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4519 33aa809d 2019-08-08 stsp break;
4520 33aa809d 2019-08-08 stsp }
4521 33aa809d 2019-08-08 stsp done:
4522 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4523 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4524 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4525 33aa809d 2019-08-08 stsp return err;
4526 33aa809d 2019-08-08 stsp }
4527 33aa809d 2019-08-08 stsp
4528 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4529 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4530 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4531 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4532 1f1abb7e 2019-08-08 stsp void *progress_arg;
4533 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4534 33aa809d 2019-08-08 stsp void *patch_arg;
4535 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4536 f259c4c1 2021-09-24 stsp int unlink_added_files;
4537 1f1abb7e 2019-08-08 stsp };
4538 a129376b 2019-03-28 stsp
4539 e20a8b6f 2019-06-04 stsp static const struct got_error *
4540 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4541 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4542 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4543 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4544 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4545 33aa809d 2019-08-08 stsp {
4546 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4547 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4548 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4549 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4550 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4551 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4552 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4553 fe621944 2020-11-10 stsp struct stat sb2;
4554 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4555 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4556 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4557 33aa809d 2019-08-08 stsp
4558 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4559 33aa809d 2019-08-08 stsp
4560 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4561 33aa809d 2019-08-08 stsp if (err)
4562 33aa809d 2019-08-08 stsp return err;
4563 33aa809d 2019-08-08 stsp
4564 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4565 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4566 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4567 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4568 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4569 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4570 fa3cef63 2020-07-23 stsp goto done;
4571 fa3cef63 2020-07-23 stsp }
4572 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4573 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4574 c0df5966 2021-12-31 stsp if (link_len == -1) {
4575 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4576 c0df5966 2021-12-31 stsp path2);
4577 c0df5966 2021-12-31 stsp }
4578 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4579 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4580 12463d8b 2019-12-13 stsp }
4581 12463d8b 2019-12-13 stsp } else {
4582 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4583 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4584 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4585 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4586 fa3cef63 2020-07-23 stsp goto done;
4587 fa3cef63 2020-07-23 stsp }
4588 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4589 fa3cef63 2020-07-23 stsp sizeof(link_target));
4590 fa3cef63 2020-07-23 stsp if (link_len == -1)
4591 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4592 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4593 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4594 12463d8b 2019-12-13 stsp }
4595 1ebedb77 2019-10-19 stsp }
4596 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4597 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4598 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4599 fa3cef63 2020-07-23 stsp goto done;
4600 fa3cef63 2020-07-23 stsp }
4601 1ebedb77 2019-10-19 stsp
4602 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4603 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4604 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4605 fa3cef63 2020-07-23 stsp goto done;
4606 fa3cef63 2020-07-23 stsp }
4607 fa3cef63 2020-07-23 stsp fd2 = -1;
4608 fa3cef63 2020-07-23 stsp } else {
4609 fa3cef63 2020-07-23 stsp size_t n;
4610 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4611 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4612 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4613 fa3cef63 2020-07-23 stsp goto done;
4614 fa3cef63 2020-07-23 stsp }
4615 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4616 fa3cef63 2020-07-23 stsp if (n != link_len) {
4617 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4618 fa3cef63 2020-07-23 stsp goto done;
4619 fa3cef63 2020-07-23 stsp }
4620 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4621 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4622 fa3cef63 2020-07-23 stsp goto done;
4623 fa3cef63 2020-07-23 stsp }
4624 fa3cef63 2020-07-23 stsp rewind(f2);
4625 33aa809d 2019-08-08 stsp }
4626 33aa809d 2019-08-08 stsp
4627 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4628 eb81bc23 2022-06-28 tracey if (fd == -1) {
4629 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4630 eb81bc23 2022-06-28 tracey goto done;
4631 eb81bc23 2022-06-28 tracey }
4632 eb81bc23 2022-06-28 tracey
4633 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4634 33aa809d 2019-08-08 stsp if (err)
4635 33aa809d 2019-08-08 stsp goto done;
4636 33aa809d 2019-08-08 stsp
4637 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4638 33aa809d 2019-08-08 stsp if (err)
4639 33aa809d 2019-08-08 stsp goto done;
4640 33aa809d 2019-08-08 stsp
4641 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4642 33aa809d 2019-08-08 stsp if (err)
4643 33aa809d 2019-08-08 stsp goto done;
4644 33aa809d 2019-08-08 stsp
4645 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4646 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4647 33aa809d 2019-08-08 stsp if (err)
4648 33aa809d 2019-08-08 stsp goto done;
4649 33aa809d 2019-08-08 stsp
4650 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4651 b90054ed 2022-11-01 stsp "");
4652 33aa809d 2019-08-08 stsp if (err)
4653 33aa809d 2019-08-08 stsp goto done;
4654 33aa809d 2019-08-08 stsp
4655 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4656 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4657 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4658 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4659 fe621944 2020-11-10 stsp
4660 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4661 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4662 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4663 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4664 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4665 fe621944 2020-11-10 stsp nchanges++;
4666 fe621944 2020-11-10 stsp }
4667 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4668 33aa809d 2019-08-08 stsp int choice;
4669 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4670 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4671 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4672 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4673 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4674 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4675 33aa809d 2019-08-08 stsp if (err)
4676 33aa809d 2019-08-08 stsp goto done;
4677 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4678 33aa809d 2019-08-08 stsp have_content = 1;
4679 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4680 33aa809d 2019-08-08 stsp break;
4681 33aa809d 2019-08-08 stsp }
4682 1ebedb77 2019-10-19 stsp if (have_content) {
4683 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4684 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4685 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4686 1ebedb77 2019-10-19 stsp if (err)
4687 1ebedb77 2019-10-19 stsp goto done;
4688 1ebedb77 2019-10-19 stsp
4689 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4690 b2b3fce1 2022-10-29 op mode_t mode;
4691 b2b3fce1 2022-10-29 op
4692 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4693 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4694 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4695 fa3cef63 2020-07-23 stsp goto done;
4696 fa3cef63 2020-07-23 stsp }
4697 1ebedb77 2019-10-19 stsp }
4698 1ebedb77 2019-10-19 stsp }
4699 33aa809d 2019-08-08 stsp done:
4700 33aa809d 2019-08-08 stsp free(id_str);
4701 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4702 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4703 33aa809d 2019-08-08 stsp if (blob)
4704 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4705 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4706 fe621944 2020-11-10 stsp if (err == NULL)
4707 fe621944 2020-11-10 stsp err = free_err;
4708 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4709 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4710 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4711 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4712 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4713 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4714 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4715 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4716 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4717 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4718 33aa809d 2019-08-08 stsp if (err || !have_content) {
4719 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4720 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4721 33aa809d 2019-08-08 stsp free(*path_outfile);
4722 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4723 33aa809d 2019-08-08 stsp }
4724 33aa809d 2019-08-08 stsp free(path1);
4725 33aa809d 2019-08-08 stsp return err;
4726 33aa809d 2019-08-08 stsp }
4727 33aa809d 2019-08-08 stsp
4728 33aa809d 2019-08-08 stsp static const struct got_error *
4729 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4730 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4731 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4732 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4733 a129376b 2019-03-28 stsp {
4734 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4735 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4736 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4737 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4738 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4739 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4740 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4741 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4742 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4743 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4744 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4745 eb81bc23 2022-06-28 tracey int fd = -1;
4746 a129376b 2019-03-28 stsp
4747 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4748 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4749 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4750 d3bcc3d1 2019-08-08 stsp return NULL;
4751 3d69ad8d 2019-08-17 semarie
4752 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4753 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4754 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4755 d3bcc3d1 2019-08-08 stsp
4756 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4757 65084dad 2019-08-08 stsp if (ie == NULL)
4758 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4759 a129376b 2019-03-28 stsp
4760 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4761 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4762 a129376b 2019-03-28 stsp if (err) {
4763 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4764 a129376b 2019-03-28 stsp goto done;
4765 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4766 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4767 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4768 e20a8b6f 2019-06-04 stsp goto done;
4769 e20a8b6f 2019-06-04 stsp }
4770 a129376b 2019-03-28 stsp }
4771 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4772 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4773 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4774 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4775 a129376b 2019-03-28 stsp goto done;
4776 a129376b 2019-03-28 stsp }
4777 a129376b 2019-03-28 stsp } else {
4778 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4779 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4780 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4781 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4782 a129376b 2019-03-28 stsp goto done;
4783 a129376b 2019-03-28 stsp }
4784 a129376b 2019-03-28 stsp } else {
4785 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4786 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4787 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4788 a129376b 2019-03-28 stsp goto done;
4789 a129376b 2019-03-28 stsp }
4790 a129376b 2019-03-28 stsp }
4791 a129376b 2019-03-28 stsp }
4792 a129376b 2019-03-28 stsp
4793 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4794 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4795 a44927cc 2022-04-07 stsp if (err)
4796 a44927cc 2022-04-07 stsp goto done;
4797 a44927cc 2022-04-07 stsp
4798 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4799 a9fa2909 2019-07-27 stsp if (err) {
4800 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4801 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4802 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4803 a9fa2909 2019-07-27 stsp goto done;
4804 a9fa2909 2019-07-27 stsp } else {
4805 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4806 a9fa2909 2019-07-27 stsp if (err)
4807 a9fa2909 2019-07-27 stsp goto done;
4808 a9fa2909 2019-07-27 stsp
4809 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4810 1233e6b6 2020-10-19 stsp if (err)
4811 a9fa2909 2019-07-27 stsp goto done;
4812 a9fa2909 2019-07-27 stsp
4813 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4814 1233e6b6 2020-10-19 stsp free(te_name);
4815 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4816 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4817 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4818 a9fa2909 2019-07-27 stsp goto done;
4819 a9fa2909 2019-07-27 stsp }
4820 a129376b 2019-03-28 stsp }
4821 a129376b 2019-03-28 stsp
4822 a129376b 2019-03-28 stsp switch (status) {
4823 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4824 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4825 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4826 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4827 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4828 33aa809d 2019-08-08 stsp if (err)
4829 33aa809d 2019-08-08 stsp goto done;
4830 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4831 33aa809d 2019-08-08 stsp break;
4832 33aa809d 2019-08-08 stsp }
4833 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4834 1f1abb7e 2019-08-08 stsp ie->path);
4835 1ee397ad 2019-07-12 stsp if (err)
4836 1ee397ad 2019-07-12 stsp goto done;
4837 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4838 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4839 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4840 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4841 f259c4c1 2021-09-24 stsp relpath) == -1) {
4842 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4843 f259c4c1 2021-09-24 stsp goto done;
4844 f259c4c1 2021-09-24 stsp }
4845 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4846 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4847 f259c4c1 2021-09-24 stsp ondisk_path);
4848 f259c4c1 2021-09-24 stsp break;
4849 f259c4c1 2021-09-24 stsp }
4850 f259c4c1 2021-09-24 stsp }
4851 a129376b 2019-03-28 stsp break;
4852 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4853 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4854 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4855 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4856 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4857 33aa809d 2019-08-08 stsp if (err)
4858 33aa809d 2019-08-08 stsp goto done;
4859 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4860 33aa809d 2019-08-08 stsp break;
4861 33aa809d 2019-08-08 stsp }
4862 33aa809d 2019-08-08 stsp /* fall through */
4863 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4864 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4865 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4866 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4867 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4868 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4869 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
4870 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
4871 b4b2adf5 2023-02-09 op else
4872 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
4873 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4874 eb81bc23 2022-06-28 tracey if (fd == -1) {
4875 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4876 eb81bc23 2022-06-28 tracey goto done;
4877 eb81bc23 2022-06-28 tracey }
4878 eb81bc23 2022-06-28 tracey
4879 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4880 a129376b 2019-03-28 stsp if (err)
4881 65084dad 2019-08-08 stsp goto done;
4882 65084dad 2019-08-08 stsp
4883 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4884 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4885 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4886 a129376b 2019-03-28 stsp goto done;
4887 65084dad 2019-08-08 stsp }
4888 65084dad 2019-08-08 stsp
4889 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4890 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4891 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4892 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4893 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4894 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4895 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4896 33aa809d 2019-08-08 stsp break;
4897 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4898 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4899 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4900 369fd7e5 2020-07-23 stsp path_content);
4901 369fd7e5 2020-07-23 stsp break;
4902 369fd7e5 2020-07-23 stsp }
4903 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4904 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4905 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4906 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4907 369fd7e5 2020-07-23 stsp } else {
4908 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4909 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4910 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4911 369fd7e5 2020-07-23 stsp goto done;
4912 369fd7e5 2020-07-23 stsp }
4913 33aa809d 2019-08-08 stsp }
4914 33aa809d 2019-08-08 stsp } else {
4915 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4916 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4917 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4918 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4919 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4920 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4921 2e1fa222 2020-07-23 stsp } else {
4922 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4923 2e1fa222 2020-07-23 stsp ie->path,
4924 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4925 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4926 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4927 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4928 2e1fa222 2020-07-23 stsp }
4929 a129376b 2019-03-28 stsp if (err)
4930 a129376b 2019-03-28 stsp goto done;
4931 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4932 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4933 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4934 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4935 437adc9d 2020-12-10 yzhong blob->id.sha1,
4936 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4937 2e1fa222 2020-07-23 stsp if (err)
4938 2e1fa222 2020-07-23 stsp goto done;
4939 2e1fa222 2020-07-23 stsp }
4940 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4941 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4942 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4943 33aa809d 2019-08-08 stsp }
4944 a129376b 2019-03-28 stsp }
4945 a129376b 2019-03-28 stsp break;
4946 e20a8b6f 2019-06-04 stsp }
4947 a129376b 2019-03-28 stsp default:
4948 1f1abb7e 2019-08-08 stsp break;
4949 a129376b 2019-03-28 stsp }
4950 a129376b 2019-03-28 stsp done:
4951 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4952 33aa809d 2019-08-08 stsp free(path_content);
4953 e20a8b6f 2019-06-04 stsp free(parent_path);
4954 a129376b 2019-03-28 stsp free(tree_path);
4955 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4956 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4957 a129376b 2019-03-28 stsp if (blob)
4958 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4959 a129376b 2019-03-28 stsp if (tree)
4960 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4961 a129376b 2019-03-28 stsp free(tree_id);
4962 a44927cc 2022-04-07 stsp if (base_commit)
4963 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4964 e20a8b6f 2019-06-04 stsp return err;
4965 e20a8b6f 2019-06-04 stsp }
4966 e20a8b6f 2019-06-04 stsp
4967 e20a8b6f 2019-06-04 stsp const struct got_error *
4968 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4969 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4970 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4971 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4972 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4973 e20a8b6f 2019-06-04 stsp {
4974 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4975 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4976 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4977 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4978 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4979 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4980 e20a8b6f 2019-06-04 stsp
4981 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4982 e20a8b6f 2019-06-04 stsp if (err)
4983 e20a8b6f 2019-06-04 stsp return err;
4984 e20a8b6f 2019-06-04 stsp
4985 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4986 e20a8b6f 2019-06-04 stsp if (err)
4987 e20a8b6f 2019-06-04 stsp goto done;
4988 e20a8b6f 2019-06-04 stsp
4989 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4990 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4991 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4992 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4993 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4994 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4995 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4996 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4997 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4998 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4999 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5000 e20a8b6f 2019-06-04 stsp if (err)
5001 af12c6b9 2019-06-04 stsp break;
5002 e20a8b6f 2019-06-04 stsp }
5003 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5004 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5005 e20a8b6f 2019-06-04 stsp err = sync_err;
5006 af12c6b9 2019-06-04 stsp done:
5007 fb399478 2019-07-12 stsp free(fileindex_path);
5008 a129376b 2019-03-28 stsp if (fileindex)
5009 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5010 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5011 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5012 a129376b 2019-03-28 stsp err = unlockerr;
5013 c4296144 2019-05-09 stsp return err;
5014 c4296144 2019-05-09 stsp }
5015 c4296144 2019-05-09 stsp
5016 cf066bf8 2019-05-09 stsp static void
5017 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5018 cf066bf8 2019-05-09 stsp {
5019 24519714 2019-05-09 stsp free(ct->path);
5020 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5021 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5022 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5023 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5024 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5025 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5026 cf066bf8 2019-05-09 stsp free(ct);
5027 cf066bf8 2019-05-09 stsp }
5028 24519714 2019-05-09 stsp
5029 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5030 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5031 24519714 2019-05-09 stsp struct got_repository *repo;
5032 24519714 2019-05-09 stsp struct got_worktree *worktree;
5033 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5034 5f8a88c6 2019-08-03 stsp int have_staged_files;
5035 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5036 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5037 12383673 2023-02-18 mark int commit_conflicts;
5038 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5039 2a47b1e5 2022-11-01 stsp FILE *f1;
5040 2a47b1e5 2022-11-01 stsp FILE *f2;
5041 24519714 2019-05-09 stsp };
5042 2a47b1e5 2022-11-01 stsp
5043 2a47b1e5 2022-11-01 stsp /*
5044 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5045 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5046 2a47b1e5 2022-11-01 stsp */
5047 2a47b1e5 2022-11-01 stsp static const struct got_error *
5048 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5049 2a47b1e5 2022-11-01 stsp const char *abspath)
5050 2a47b1e5 2022-11-01 stsp {
5051 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5052 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5053 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5054 2a47b1e5 2022-11-01 stsp
5055 2a47b1e5 2022-11-01 stsp *fd = -1;
5056 2a47b1e5 2022-11-01 stsp
5057 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5058 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5059 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5060 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5061 2a47b1e5 2022-11-01 stsp } else {
5062 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5063 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5064 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5065 2a47b1e5 2022-11-01 stsp }
5066 2a47b1e5 2022-11-01 stsp
5067 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5068 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5069 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5070 2a47b1e5 2022-11-01 stsp
5071 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5072 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5073 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5074 2a47b1e5 2022-11-01 stsp goto done;
5075 2a47b1e5 2022-11-01 stsp }
5076 2a47b1e5 2022-11-01 stsp
5077 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5078 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5079 2a47b1e5 2022-11-01 stsp goto done;
5080 2a47b1e5 2022-11-01 stsp }
5081 2a47b1e5 2022-11-01 stsp done:
5082 2a47b1e5 2022-11-01 stsp if (err) {
5083 2a47b1e5 2022-11-01 stsp close(*fd);
5084 2a47b1e5 2022-11-01 stsp *fd = -1;
5085 2a47b1e5 2022-11-01 stsp }
5086 2a47b1e5 2022-11-01 stsp return err;
5087 2a47b1e5 2022-11-01 stsp }
5088 2a47b1e5 2022-11-01 stsp
5089 2a47b1e5 2022-11-01 stsp static const struct got_error *
5090 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5091 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5092 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5093 2a47b1e5 2022-11-01 stsp {
5094 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5095 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5096 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5097 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5098 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5099 2a47b1e5 2022-11-01 stsp struct stat sb;
5100 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5101 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5102 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5103 2a47b1e5 2022-11-01 stsp
5104 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5105 4ba5cca9 2022-11-01 stsp
5106 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5107 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5108 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5109 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5110 4ba5cca9 2022-11-01 stsp return NULL;
5111 4ba5cca9 2022-11-01 stsp } else {
5112 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5113 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5114 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5115 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5116 4ba5cca9 2022-11-01 stsp return NULL;
5117 4ba5cca9 2022-11-01 stsp }
5118 2a47b1e5 2022-11-01 stsp
5119 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5120 2a47b1e5 2022-11-01 stsp if (err)
5121 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5122 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5123 2a47b1e5 2022-11-01 stsp if (err)
5124 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5125 2a47b1e5 2022-11-01 stsp
5126 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5127 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5128 2a47b1e5 2022-11-01 stsp if (err)
5129 2a47b1e5 2022-11-01 stsp return err;
5130 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5131 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5132 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5133 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5134 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5135 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5136 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5137 2a47b1e5 2022-11-01 stsp }
5138 2a47b1e5 2022-11-01 stsp
5139 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5140 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5141 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5142 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5143 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5144 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5145 2a47b1e5 2022-11-01 stsp break;
5146 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5147 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5148 2a47b1e5 2022-11-01 stsp break;
5149 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5150 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5151 2a47b1e5 2022-11-01 stsp break;
5152 2a47b1e5 2022-11-01 stsp default:
5153 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5154 2a47b1e5 2022-11-01 stsp }
5155 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5156 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5157 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5158 2a47b1e5 2022-11-01 stsp goto done;
5159 2a47b1e5 2022-11-01 stsp }
5160 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5161 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5162 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5163 2a47b1e5 2022-11-01 stsp goto done;
5164 2a47b1e5 2022-11-01 stsp }
5165 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5166 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5167 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5168 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5169 2a47b1e5 2022-11-01 stsp goto done;
5170 2a47b1e5 2022-11-01 stsp }
5171 2a47b1e5 2022-11-01 stsp
5172 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5173 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5174 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5175 2a47b1e5 2022-11-01 stsp goto done;
5176 2a47b1e5 2022-11-01 stsp }
5177 2a47b1e5 2022-11-01 stsp
5178 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5179 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5180 2a47b1e5 2022-11-01 stsp 8192, fd1);
5181 2a47b1e5 2022-11-01 stsp if (err)
5182 2a47b1e5 2022-11-01 stsp goto done;
5183 2a47b1e5 2022-11-01 stsp }
5184 2a47b1e5 2022-11-01 stsp
5185 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5186 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5187 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5188 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5189 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5190 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5191 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5192 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5193 2a47b1e5 2022-11-01 stsp goto done;
5194 2a47b1e5 2022-11-01 stsp }
5195 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5196 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5197 2a47b1e5 2022-11-01 stsp if (err)
5198 2a47b1e5 2022-11-01 stsp goto done;
5199 2a47b1e5 2022-11-01 stsp }
5200 2a47b1e5 2022-11-01 stsp } else {
5201 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5202 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5203 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5204 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5205 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5206 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5207 2a47b1e5 2022-11-01 stsp goto done;
5208 2a47b1e5 2022-11-01 stsp }
5209 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5210 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5211 2a47b1e5 2022-11-01 stsp if (err)
5212 2a47b1e5 2022-11-01 stsp goto done;
5213 2a47b1e5 2022-11-01 stsp }
5214 2a47b1e5 2022-11-01 stsp }
5215 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5216 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5217 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5218 2a47b1e5 2022-11-01 stsp goto done;
5219 2a47b1e5 2022-11-01 stsp }
5220 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5221 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5222 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5223 2a47b1e5 2022-11-01 stsp goto done;
5224 2a47b1e5 2022-11-01 stsp }
5225 2a47b1e5 2022-11-01 stsp fd = -1;
5226 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5227 2a47b1e5 2022-11-01 stsp }
5228 2a47b1e5 2022-11-01 stsp
5229 2a47b1e5 2022-11-01 stsp if (blob1) {
5230 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5231 2a47b1e5 2022-11-01 stsp f1, blob1);
5232 2a47b1e5 2022-11-01 stsp if (err)
5233 2a47b1e5 2022-11-01 stsp goto done;
5234 2a47b1e5 2022-11-01 stsp }
5235 2a47b1e5 2022-11-01 stsp
5236 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5237 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5238 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5239 2a47b1e5 2022-11-01 stsp done:
5240 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5241 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5242 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5243 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5244 2a47b1e5 2022-11-01 stsp if (blob1)
5245 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5246 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5247 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5248 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5249 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5250 2a47b1e5 2022-11-01 stsp return err;
5251 2a47b1e5 2022-11-01 stsp }
5252 cf066bf8 2019-05-09 stsp
5253 c4296144 2019-05-09 stsp static const struct got_error *
5254 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5255 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5256 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5257 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5258 c4296144 2019-05-09 stsp {
5259 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5260 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5261 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5262 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5263 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5264 768aea60 2019-05-09 stsp struct stat sb;
5265 c4296144 2019-05-09 stsp
5266 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5267 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5268 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5269 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5270 5f8a88c6 2019-08-03 stsp return NULL;
5271 5f8a88c6 2019-08-03 stsp } else {
5272 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5273 12383673 2023-02-18 mark printf("C %s\n", relpath);
5274 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5275 12383673 2023-02-18 mark }
5276 c4296144 2019-05-09 stsp
5277 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5278 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5279 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5280 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5281 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5282 5f8a88c6 2019-08-03 stsp return NULL;
5283 5f8a88c6 2019-08-03 stsp }
5284 0b5cc0d6 2019-05-09 stsp
5285 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5286 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5287 036813ee 2019-05-09 stsp goto done;
5288 036813ee 2019-05-09 stsp }
5289 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5290 036813ee 2019-05-09 stsp parent_path = strdup("");
5291 69960a46 2019-05-09 stsp if (parent_path == NULL)
5292 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5293 69960a46 2019-05-09 stsp } else {
5294 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5295 69960a46 2019-05-09 stsp if (err)
5296 69960a46 2019-05-09 stsp return err;
5297 69960a46 2019-05-09 stsp }
5298 c4296144 2019-05-09 stsp
5299 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5300 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5301 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5302 c4296144 2019-05-09 stsp goto done;
5303 768aea60 2019-05-09 stsp }
5304 768aea60 2019-05-09 stsp
5305 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5306 768aea60 2019-05-09 stsp relpath) == -1) {
5307 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5308 768aea60 2019-05-09 stsp goto done;
5309 768aea60 2019-05-09 stsp }
5310 0aeb8099 2020-07-23 stsp
5311 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5312 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5313 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5314 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5315 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5316 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5317 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5318 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5319 0aeb8099 2020-07-23 stsp break;
5320 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5321 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5322 0aeb8099 2020-07-23 stsp break;
5323 0aeb8099 2020-07-23 stsp default:
5324 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5325 0aeb8099 2020-07-23 stsp goto done;
5326 0aeb8099 2020-07-23 stsp }
5327 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5328 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5329 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5330 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5331 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5332 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5333 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5334 12463d8b 2019-12-13 stsp ct->ondisk_path);
5335 12463d8b 2019-12-13 stsp goto done;
5336 12463d8b 2019-12-13 stsp }
5337 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5338 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5339 768aea60 2019-05-09 stsp goto done;
5340 768aea60 2019-05-09 stsp }
5341 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5342 c4296144 2019-05-09 stsp }
5343 c4296144 2019-05-09 stsp
5344 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5345 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5346 44d03001 2019-05-09 stsp relpath) == -1) {
5347 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5348 44d03001 2019-05-09 stsp goto done;
5349 44d03001 2019-05-09 stsp }
5350 44d03001 2019-05-09 stsp
5351 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5352 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5353 35213c7c 2020-07-23 stsp int is_bad_symlink;
5354 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5355 35213c7c 2020-07-23 stsp ssize_t target_len;
5356 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5357 35213c7c 2020-07-23 stsp sizeof(target_path));
5358 35213c7c 2020-07-23 stsp if (target_len == -1) {
5359 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5360 35213c7c 2020-07-23 stsp ct->ondisk_path);
5361 35213c7c 2020-07-23 stsp goto done;
5362 35213c7c 2020-07-23 stsp }
5363 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5364 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5365 35213c7c 2020-07-23 stsp if (err)
5366 35213c7c 2020-07-23 stsp goto done;
5367 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5368 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5369 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5370 35213c7c 2020-07-23 stsp goto done;
5371 35213c7c 2020-07-23 stsp }
5372 35213c7c 2020-07-23 stsp }
5373 35213c7c 2020-07-23 stsp
5374 35213c7c 2020-07-23 stsp
5375 cf066bf8 2019-05-09 stsp ct->status = status;
5376 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5377 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5378 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5379 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5380 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5381 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5382 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5383 b416585c 2019-05-13 stsp goto done;
5384 b416585c 2019-05-13 stsp }
5385 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5386 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5387 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5388 f0b75401 2019-08-03 stsp goto done;
5389 f0b75401 2019-08-03 stsp }
5390 f0b75401 2019-08-03 stsp }
5391 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5392 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5393 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5394 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5395 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5396 036813ee 2019-05-09 stsp goto done;
5397 036813ee 2019-05-09 stsp }
5398 ca2503ea 2019-05-09 stsp }
5399 24519714 2019-05-09 stsp ct->path = strdup(path);
5400 24519714 2019-05-09 stsp if (ct->path == NULL) {
5401 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5402 24519714 2019-05-09 stsp goto done;
5403 24519714 2019-05-09 stsp }
5404 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5405 2a47b1e5 2022-11-01 stsp if (err)
5406 2a47b1e5 2022-11-01 stsp goto done;
5407 2a47b1e5 2022-11-01 stsp
5408 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5409 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5410 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5411 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5412 2a47b1e5 2022-11-01 stsp if (err)
5413 2a47b1e5 2022-11-01 stsp goto done;
5414 2a47b1e5 2022-11-01 stsp }
5415 c4296144 2019-05-09 stsp done:
5416 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5417 ed175427 2019-05-09 stsp free_commitable(ct);
5418 24519714 2019-05-09 stsp free(parent_path);
5419 036813ee 2019-05-09 stsp free(path);
5420 a129376b 2019-03-28 stsp return err;
5421 a129376b 2019-03-28 stsp }
5422 c4296144 2019-05-09 stsp
5423 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5424 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5425 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5426 036813ee 2019-05-09 stsp struct got_repository *);
5427 ed175427 2019-05-09 stsp
5428 ed175427 2019-05-09 stsp static const struct got_error *
5429 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5430 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5431 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5432 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5433 afa376bf 2019-05-09 stsp struct got_repository *repo)
5434 ed175427 2019-05-09 stsp {
5435 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5436 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5437 036813ee 2019-05-09 stsp char *subpath;
5438 ed175427 2019-05-09 stsp
5439 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5440 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5441 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5442 ed175427 2019-05-09 stsp
5443 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5444 ed175427 2019-05-09 stsp if (err)
5445 ed175427 2019-05-09 stsp return err;
5446 ed175427 2019-05-09 stsp
5447 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5448 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5449 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5450 036813ee 2019-05-09 stsp free(subpath);
5451 036813ee 2019-05-09 stsp return err;
5452 036813ee 2019-05-09 stsp }
5453 ed175427 2019-05-09 stsp
5454 036813ee 2019-05-09 stsp static const struct got_error *
5455 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5456 036813ee 2019-05-09 stsp {
5457 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5458 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5459 ed175427 2019-05-09 stsp
5460 036813ee 2019-05-09 stsp *match = 0;
5461 ed175427 2019-05-09 stsp
5462 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5463 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5464 0f63689d 2019-05-10 stsp return NULL;
5465 036813ee 2019-05-09 stsp }
5466 0b5cc0d6 2019-05-09 stsp
5467 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5468 0f63689d 2019-05-10 stsp if (err)
5469 0f63689d 2019-05-10 stsp return err;
5470 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5471 036813ee 2019-05-09 stsp free(ct_parent_path);
5472 036813ee 2019-05-09 stsp return err;
5473 036813ee 2019-05-09 stsp }
5474 0b5cc0d6 2019-05-09 stsp
5475 768aea60 2019-05-09 stsp static mode_t
5476 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5477 768aea60 2019-05-09 stsp {
5478 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5479 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5480 3d9a4ec4 2020-07-23 stsp
5481 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5482 768aea60 2019-05-09 stsp }
5483 768aea60 2019-05-09 stsp
5484 036813ee 2019-05-09 stsp static const struct got_error *
5485 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5486 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5487 036813ee 2019-05-09 stsp {
5488 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5489 ca2503ea 2019-05-09 stsp
5490 036813ee 2019-05-09 stsp *new_te = NULL;
5491 0b5cc0d6 2019-05-09 stsp
5492 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5493 036813ee 2019-05-09 stsp if (err)
5494 036813ee 2019-05-09 stsp goto done;
5495 0b5cc0d6 2019-05-09 stsp
5496 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5497 036813ee 2019-05-09 stsp
5498 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5499 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5500 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5501 5f8a88c6 2019-08-03 stsp else
5502 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5503 036813ee 2019-05-09 stsp done:
5504 036813ee 2019-05-09 stsp if (err && *new_te) {
5505 56e0773d 2019-11-28 stsp free(*new_te);
5506 036813ee 2019-05-09 stsp *new_te = NULL;
5507 036813ee 2019-05-09 stsp }
5508 036813ee 2019-05-09 stsp return err;
5509 036813ee 2019-05-09 stsp }
5510 036813ee 2019-05-09 stsp
5511 036813ee 2019-05-09 stsp static const struct got_error *
5512 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5513 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5514 036813ee 2019-05-09 stsp {
5515 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5516 102b254e 2020-10-19 stsp char *ct_name = NULL;
5517 036813ee 2019-05-09 stsp
5518 036813ee 2019-05-09 stsp *new_te = NULL;
5519 036813ee 2019-05-09 stsp
5520 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5521 036813ee 2019-05-09 stsp if (*new_te == NULL)
5522 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5523 036813ee 2019-05-09 stsp
5524 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5525 102b254e 2020-10-19 stsp if (err)
5526 036813ee 2019-05-09 stsp goto done;
5527 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5528 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5529 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5530 036813ee 2019-05-09 stsp goto done;
5531 036813ee 2019-05-09 stsp }
5532 036813ee 2019-05-09 stsp
5533 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5534 036813ee 2019-05-09 stsp
5535 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5536 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5537 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5538 5f8a88c6 2019-08-03 stsp else
5539 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5540 036813ee 2019-05-09 stsp done:
5541 102b254e 2020-10-19 stsp free(ct_name);
5542 036813ee 2019-05-09 stsp if (err && *new_te) {
5543 56e0773d 2019-11-28 stsp free(*new_te);
5544 036813ee 2019-05-09 stsp *new_te = NULL;
5545 036813ee 2019-05-09 stsp }
5546 036813ee 2019-05-09 stsp return err;
5547 036813ee 2019-05-09 stsp }
5548 036813ee 2019-05-09 stsp
5549 036813ee 2019-05-09 stsp static const struct got_error *
5550 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5551 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5552 036813ee 2019-05-09 stsp {
5553 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5554 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5555 036813ee 2019-05-09 stsp
5556 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5557 036813ee 2019-05-09 stsp if (err)
5558 036813ee 2019-05-09 stsp return err;
5559 036813ee 2019-05-09 stsp if (new_pe == NULL)
5560 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5561 036813ee 2019-05-09 stsp return NULL;
5562 afa376bf 2019-05-09 stsp }
5563 afa376bf 2019-05-09 stsp
5564 afa376bf 2019-05-09 stsp static const struct got_error *
5565 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5566 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5567 afa376bf 2019-05-09 stsp {
5568 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5569 5f8a88c6 2019-08-03 stsp unsigned char status;
5570 0ff8d236 2021-09-28 stsp
5571 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5572 0ff8d236 2021-09-28 stsp return NULL;
5573 5f8a88c6 2019-08-03 stsp
5574 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5575 afa376bf 2019-05-09 stsp ct_path++;
5576 5f8a88c6 2019-08-03 stsp
5577 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5578 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5579 5f8a88c6 2019-08-03 stsp else
5580 5f8a88c6 2019-08-03 stsp status = ct->status;
5581 5f8a88c6 2019-08-03 stsp
5582 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5583 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5584 036813ee 2019-05-09 stsp }
5585 036813ee 2019-05-09 stsp
5586 036813ee 2019-05-09 stsp static const struct got_error *
5587 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5588 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5589 44d03001 2019-05-09 stsp {
5590 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5591 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5592 44d03001 2019-05-09 stsp char *te_path;
5593 44d03001 2019-05-09 stsp
5594 44d03001 2019-05-09 stsp *modified = 0;
5595 44d03001 2019-05-09 stsp
5596 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5597 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5598 44d03001 2019-05-09 stsp te->name) == -1)
5599 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5600 44d03001 2019-05-09 stsp
5601 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5602 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5603 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5604 44d03001 2019-05-09 stsp strlen(te_path));
5605 62d463ca 2020-10-20 naddy if (*modified)
5606 44d03001 2019-05-09 stsp break;
5607 44d03001 2019-05-09 stsp }
5608 44d03001 2019-05-09 stsp
5609 44d03001 2019-05-09 stsp free(te_path);
5610 44d03001 2019-05-09 stsp return err;
5611 44d03001 2019-05-09 stsp }
5612 44d03001 2019-05-09 stsp
5613 44d03001 2019-05-09 stsp static const struct got_error *
5614 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5615 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5616 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_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 *pe;
5620 036813ee 2019-05-09 stsp
5621 036813ee 2019-05-09 stsp *ctp = NULL;
5622 036813ee 2019-05-09 stsp
5623 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5624 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5625 036813ee 2019-05-09 stsp char *ct_name = NULL;
5626 036813ee 2019-05-09 stsp int path_matches;
5627 036813ee 2019-05-09 stsp
5628 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5629 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5630 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5631 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5632 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5633 5f8a88c6 2019-08-03 stsp continue;
5634 5f8a88c6 2019-08-03 stsp } else {
5635 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5636 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5637 5f8a88c6 2019-08-03 stsp continue;
5638 5f8a88c6 2019-08-03 stsp }
5639 036813ee 2019-05-09 stsp
5640 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5641 036813ee 2019-05-09 stsp continue;
5642 036813ee 2019-05-09 stsp
5643 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5644 62d463ca 2020-10-20 naddy if (err)
5645 036813ee 2019-05-09 stsp return err;
5646 036813ee 2019-05-09 stsp if (!path_matches)
5647 036813ee 2019-05-09 stsp continue;
5648 036813ee 2019-05-09 stsp
5649 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5650 d34b633e 2020-10-19 stsp if (err)
5651 d34b633e 2020-10-19 stsp return err;
5652 d34b633e 2020-10-19 stsp
5653 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5654 d34b633e 2020-10-19 stsp free(ct_name);
5655 036813ee 2019-05-09 stsp continue;
5656 d34b633e 2020-10-19 stsp }
5657 d34b633e 2020-10-19 stsp free(ct_name);
5658 036813ee 2019-05-09 stsp
5659 036813ee 2019-05-09 stsp *ctp = ct;
5660 036813ee 2019-05-09 stsp break;
5661 036813ee 2019-05-09 stsp }
5662 2b496619 2019-07-10 stsp
5663 2b496619 2019-07-10 stsp return err;
5664 2b496619 2019-07-10 stsp }
5665 2b496619 2019-07-10 stsp
5666 2b496619 2019-07-10 stsp static const struct got_error *
5667 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5668 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5669 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5670 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5671 2b496619 2019-07-10 stsp struct got_repository *repo)
5672 2b496619 2019-07-10 stsp {
5673 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5674 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5675 2b496619 2019-07-10 stsp char *subtree_path;
5676 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5677 ba580f68 2020-03-22 stsp int nentries;
5678 2b496619 2019-07-10 stsp
5679 2b496619 2019-07-10 stsp *new_tep = NULL;
5680 2b496619 2019-07-10 stsp
5681 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5682 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5683 2b496619 2019-07-10 stsp child_path) == -1)
5684 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5685 036813ee 2019-05-09 stsp
5686 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5687 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5688 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5689 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5690 56e0773d 2019-11-28 stsp
5691 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5692 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5693 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5694 2b496619 2019-07-10 stsp goto done;
5695 2b496619 2019-07-10 stsp }
5696 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5697 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5698 2b496619 2019-07-10 stsp if (err) {
5699 56e0773d 2019-11-28 stsp free(new_te);
5700 2b496619 2019-07-10 stsp goto done;
5701 2b496619 2019-07-10 stsp }
5702 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5703 2b496619 2019-07-10 stsp done:
5704 56e0773d 2019-11-28 stsp free(id);
5705 2b496619 2019-07-10 stsp free(subtree_path);
5706 2b496619 2019-07-10 stsp if (err == NULL)
5707 2b496619 2019-07-10 stsp *new_tep = new_te;
5708 036813ee 2019-05-09 stsp return err;
5709 036813ee 2019-05-09 stsp }
5710 036813ee 2019-05-09 stsp
5711 036813ee 2019-05-09 stsp static const struct got_error *
5712 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5713 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5714 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5715 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5716 036813ee 2019-05-09 stsp struct got_repository *repo)
5717 036813ee 2019-05-09 stsp {
5718 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5719 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5720 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5721 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5722 036813ee 2019-05-09 stsp
5723 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5724 ba580f68 2020-03-22 stsp *nentries = 0;
5725 036813ee 2019-05-09 stsp
5726 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5727 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5728 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5729 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5730 036813ee 2019-05-09 stsp
5731 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5732 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5733 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5734 036813ee 2019-05-09 stsp continue;
5735 036813ee 2019-05-09 stsp
5736 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5737 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5738 036813ee 2019-05-09 stsp continue;
5739 036813ee 2019-05-09 stsp
5740 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5741 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5742 036813ee 2019-05-09 stsp if (err)
5743 036813ee 2019-05-09 stsp goto done;
5744 036813ee 2019-05-09 stsp
5745 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5746 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5747 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5748 036813ee 2019-05-09 stsp if (err)
5749 036813ee 2019-05-09 stsp goto done;
5750 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5751 afa376bf 2019-05-09 stsp if (err)
5752 afa376bf 2019-05-09 stsp goto done;
5753 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5754 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5755 2b496619 2019-07-10 stsp if (err)
5756 2f51b5b3 2019-05-09 stsp goto done;
5757 ba580f68 2020-03-22 stsp (*nentries)++;
5758 2b496619 2019-07-10 stsp } else {
5759 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5760 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5761 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5762 2b496619 2019-07-10 stsp == NULL) {
5763 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5764 2b496619 2019-07-10 stsp child_path, path_base_tree,
5765 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5766 2b496619 2019-07-10 stsp repo);
5767 2b496619 2019-07-10 stsp if (err)
5768 2b496619 2019-07-10 stsp goto done;
5769 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5770 2b496619 2019-07-10 stsp if (err)
5771 2b496619 2019-07-10 stsp goto done;
5772 ba580f68 2020-03-22 stsp (*nentries)++;
5773 9ba0479c 2019-05-10 stsp }
5774 ed175427 2019-05-09 stsp }
5775 2f51b5b3 2019-05-09 stsp }
5776 2f51b5b3 2019-05-09 stsp
5777 2f51b5b3 2019-05-09 stsp if (base_tree) {
5778 56e0773d 2019-11-28 stsp int i, nbase_entries;
5779 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5780 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5781 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5782 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5783 63c5ca5d 2019-08-24 stsp
5784 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5785 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5786 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5787 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5788 63c5ca5d 2019-08-24 stsp if (err)
5789 63c5ca5d 2019-08-24 stsp goto done;
5790 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5791 63c5ca5d 2019-08-24 stsp if (err)
5792 63c5ca5d 2019-08-24 stsp goto done;
5793 ba580f68 2020-03-22 stsp (*nentries)++;
5794 63c5ca5d 2019-08-24 stsp continue;
5795 63c5ca5d 2019-08-24 stsp }
5796 2f51b5b3 2019-05-09 stsp
5797 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5798 44d03001 2019-05-09 stsp int modified;
5799 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5800 036813ee 2019-05-09 stsp if (err)
5801 036813ee 2019-05-09 stsp goto done;
5802 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5803 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5804 2f51b5b3 2019-05-09 stsp if (err)
5805 2f51b5b3 2019-05-09 stsp goto done;
5806 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5807 44d03001 2019-05-09 stsp if (modified) {
5808 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5809 ba580f68 2020-03-22 stsp int nsubentries;
5810 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5811 ba580f68 2020-03-22 stsp &nsubentries, te,
5812 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5813 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5814 44d03001 2019-05-09 stsp if (err)
5815 44d03001 2019-05-09 stsp goto done;
5816 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5817 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5818 ba580f68 2020-03-22 stsp free(new_id);
5819 ba580f68 2020-03-22 stsp continue;
5820 ba580f68 2020-03-22 stsp }
5821 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5822 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5823 56e0773d 2019-11-28 stsp free(new_id);
5824 44d03001 2019-05-09 stsp }
5825 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5826 036813ee 2019-05-09 stsp if (err)
5827 036813ee 2019-05-09 stsp goto done;
5828 ba580f68 2020-03-22 stsp (*nentries)++;
5829 2f51b5b3 2019-05-09 stsp continue;
5830 036813ee 2019-05-09 stsp }
5831 2f51b5b3 2019-05-09 stsp
5832 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5833 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5834 f66c734c 2019-09-22 stsp if (err)
5835 f66c734c 2019-09-22 stsp goto done;
5836 2f51b5b3 2019-05-09 stsp if (ct) {
5837 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5838 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5839 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5840 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
5841 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5842 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5843 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5844 2f51b5b3 2019-05-09 stsp if (err)
5845 2f51b5b3 2019-05-09 stsp goto done;
5846 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5847 2f51b5b3 2019-05-09 stsp if (err)
5848 2f51b5b3 2019-05-09 stsp goto done;
5849 ba580f68 2020-03-22 stsp (*nentries)++;
5850 2f51b5b3 2019-05-09 stsp }
5851 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5852 b416585c 2019-05-13 stsp status_arg);
5853 afa376bf 2019-05-09 stsp if (err)
5854 afa376bf 2019-05-09 stsp goto done;
5855 2f51b5b3 2019-05-09 stsp } else {
5856 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5857 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5858 2f51b5b3 2019-05-09 stsp if (err)
5859 2f51b5b3 2019-05-09 stsp goto done;
5860 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5861 2f51b5b3 2019-05-09 stsp if (err)
5862 2f51b5b3 2019-05-09 stsp goto done;
5863 ba580f68 2020-03-22 stsp (*nentries)++;
5864 2f51b5b3 2019-05-09 stsp }
5865 ca2503ea 2019-05-09 stsp }
5866 ed175427 2019-05-09 stsp }
5867 0b5cc0d6 2019-05-09 stsp
5868 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5869 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5870 ed175427 2019-05-09 stsp done:
5871 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5872 2e1fa222 2020-07-23 stsp return err;
5873 2e1fa222 2020-07-23 stsp }
5874 2e1fa222 2020-07-23 stsp
5875 2e1fa222 2020-07-23 stsp static const struct got_error *
5876 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5877 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5878 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5879 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5880 ebf99748 2019-05-09 stsp {
5881 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5882 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5883 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5884 ebf99748 2019-05-09 stsp
5885 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5886 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5887 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5888 ebf99748 2019-05-09 stsp
5889 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5890 437adc9d 2020-12-10 yzhong
5891 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5892 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5893 437adc9d 2020-12-10 yzhong if (err)
5894 437adc9d 2020-12-10 yzhong goto done;
5895 437adc9d 2020-12-10 yzhong
5896 ebf99748 2019-05-09 stsp if (ie) {
5897 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5898 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5899 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5900 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5901 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5902 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5903 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5904 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5905 437adc9d 2020-12-10 yzhong
5906 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5907 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5908 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5909 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5910 72fd46fa 2019-09-06 stsp !have_staged_files);
5911 ebf99748 2019-05-09 stsp } else
5912 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5913 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5914 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5915 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5916 72fd46fa 2019-09-06 stsp !have_staged_files);
5917 ebf99748 2019-05-09 stsp } else {
5918 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5919 ebf99748 2019-05-09 stsp if (err)
5920 437adc9d 2020-12-10 yzhong goto done;
5921 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5922 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5923 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5924 3969253a 2020-03-07 stsp if (err) {
5925 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5926 437adc9d 2020-12-10 yzhong goto done;
5927 3969253a 2020-03-07 stsp }
5928 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5929 3969253a 2020-03-07 stsp if (err) {
5930 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5931 437adc9d 2020-12-10 yzhong goto done;
5932 3969253a 2020-03-07 stsp }
5933 ebf99748 2019-05-09 stsp }
5934 437adc9d 2020-12-10 yzhong free(relpath);
5935 437adc9d 2020-12-10 yzhong relpath = NULL;
5936 ebf99748 2019-05-09 stsp }
5937 437adc9d 2020-12-10 yzhong done:
5938 437adc9d 2020-12-10 yzhong free(relpath);
5939 d56d26ce 2019-05-10 stsp return err;
5940 d56d26ce 2019-05-10 stsp }
5941 735ef5ac 2019-08-03 stsp
5942 d56d26ce 2019-05-10 stsp
5943 d56d26ce 2019-05-10 stsp static const struct got_error *
5944 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5945 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5946 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5947 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5948 735ef5ac 2019-08-03 stsp int ood_errcode)
5949 d56d26ce 2019-05-10 stsp {
5950 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5951 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5952 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5953 1a36436d 2019-06-10 stsp
5954 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5955 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5956 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5957 1a36436d 2019-06-10 stsp return NULL;
5958 9bead371 2019-07-28 stsp /*
5959 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5960 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5961 9bead371 2019-07-28 stsp */
5962 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5963 a44927cc 2022-04-07 stsp if (err)
5964 a44927cc 2022-04-07 stsp goto done;
5965 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5966 9bead371 2019-07-28 stsp if (err) {
5967 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5968 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5969 1a36436d 2019-06-10 stsp goto done;
5970 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5971 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5972 1a36436d 2019-06-10 stsp } else {
5973 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5974 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5975 a44927cc 2022-04-07 stsp if (err)
5976 a44927cc 2022-04-07 stsp goto done;
5977 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5978 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5979 1a36436d 2019-06-10 stsp goto done;
5980 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5981 1a36436d 2019-06-10 stsp }
5982 1a36436d 2019-06-10 stsp done:
5983 1a36436d 2019-06-10 stsp free(id);
5984 a44927cc 2022-04-07 stsp if (commit)
5985 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5986 1a36436d 2019-06-10 stsp return err;
5987 ebf99748 2019-05-09 stsp }
5988 ebf99748 2019-05-09 stsp
5989 336075a4 2022-06-25 op static const struct got_error *
5990 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5991 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5992 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5993 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5994 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5995 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5996 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5997 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5998 afa376bf 2019-05-09 stsp struct got_repository *repo)
5999 c4296144 2019-05-09 stsp {
6000 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6001 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6002 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6003 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6004 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6005 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6006 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6007 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6008 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6009 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6010 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6011 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6012 f8399b8f 2022-07-22 stsp time_t timestamp;
6013 c4296144 2019-05-09 stsp
6014 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6015 c4296144 2019-05-09 stsp
6016 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6017 675c7539 2019-05-09 stsp
6018 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6019 588edf97 2019-05-10 stsp if (err)
6020 588edf97 2019-05-10 stsp goto done;
6021 de18fc63 2019-05-09 stsp
6022 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6023 588edf97 2019-05-10 stsp if (err)
6024 588edf97 2019-05-10 stsp goto done;
6025 588edf97 2019-05-10 stsp
6026 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6027 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6028 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6029 33ad4cbe 2019-05-12 jcs if (err)
6030 33ad4cbe 2019-05-12 jcs goto done;
6031 33ad4cbe 2019-05-12 jcs }
6032 c4296144 2019-05-09 stsp
6033 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6034 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6035 33ad4cbe 2019-05-12 jcs goto done;
6036 33ad4cbe 2019-05-12 jcs }
6037 33ad4cbe 2019-05-12 jcs
6038 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6039 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6040 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6041 cf066bf8 2019-05-09 stsp char *ondisk_path;
6042 cf066bf8 2019-05-09 stsp
6043 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6044 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6045 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6046 5f8a88c6 2019-08-03 stsp continue;
6047 5f8a88c6 2019-08-03 stsp
6048 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6049 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6050 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6051 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6052 cf066bf8 2019-05-09 stsp continue;
6053 cf066bf8 2019-05-09 stsp
6054 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6055 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6056 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6057 cf066bf8 2019-05-09 stsp goto done;
6058 cf066bf8 2019-05-09 stsp }
6059 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6060 2e1fa222 2020-07-23 stsp free(ondisk_path);
6061 2e1fa222 2020-07-23 stsp if (err)
6062 cf066bf8 2019-05-09 stsp goto done;
6063 cf066bf8 2019-05-09 stsp }
6064 036813ee 2019-05-09 stsp
6065 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6066 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6067 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6068 036813ee 2019-05-09 stsp if (err)
6069 036813ee 2019-05-09 stsp goto done;
6070 036813ee 2019-05-09 stsp
6071 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6072 de18fc63 2019-05-09 stsp if (err)
6073 de18fc63 2019-05-09 stsp goto done;
6074 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6075 f259c4c1 2021-09-24 stsp nparents++;
6076 f259c4c1 2021-09-24 stsp if (parent_id2) {
6077 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6078 f259c4c1 2021-09-24 stsp if (err)
6079 f259c4c1 2021-09-24 stsp goto done;
6080 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6081 f259c4c1 2021-09-24 stsp nparents++;
6082 f259c4c1 2021-09-24 stsp }
6083 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6084 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6085 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6086 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6087 33ad4cbe 2019-05-12 jcs free(logmsg);
6088 9d40349a 2019-05-09 stsp if (err)
6089 9d40349a 2019-05-09 stsp goto done;
6090 9d40349a 2019-05-09 stsp
6091 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6092 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6093 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6094 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6095 09f5bd90 2019-05-09 stsp goto done;
6096 09f5bd90 2019-05-09 stsp }
6097 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6098 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6099 09f5bd90 2019-05-09 stsp if (err)
6100 09f5bd90 2019-05-09 stsp goto done;
6101 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6102 ebf99748 2019-05-09 stsp if (err)
6103 ebf99748 2019-05-09 stsp goto done;
6104 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6105 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6106 09f5bd90 2019-05-09 stsp goto done;
6107 09f5bd90 2019-05-09 stsp }
6108 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6109 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6110 f2c16586 2019-05-09 stsp if (err)
6111 f2c16586 2019-05-09 stsp goto done;
6112 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6113 f2c16586 2019-05-09 stsp if (err)
6114 f2c16586 2019-05-09 stsp goto done;
6115 f2c16586 2019-05-09 stsp
6116 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6117 09f5bd90 2019-05-09 stsp if (err)
6118 09f5bd90 2019-05-09 stsp goto done;
6119 09f5bd90 2019-05-09 stsp
6120 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6121 ebf99748 2019-05-09 stsp if (err)
6122 ebf99748 2019-05-09 stsp goto done;
6123 c4296144 2019-05-09 stsp done:
6124 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6125 588edf97 2019-05-10 stsp if (head_tree)
6126 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6127 588edf97 2019-05-10 stsp if (head_commit)
6128 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6129 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6130 2f17228e 2019-05-12 stsp if (head_ref2) {
6131 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6132 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6133 2f17228e 2019-05-12 stsp err = unlockerr;
6134 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6135 39cd0ff6 2019-07-12 stsp }
6136 39cd0ff6 2019-07-12 stsp return err;
6137 39cd0ff6 2019-07-12 stsp }
6138 5c1e53bc 2019-07-28 stsp
6139 5c1e53bc 2019-07-28 stsp static const struct got_error *
6140 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6141 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6142 5c1e53bc 2019-07-28 stsp {
6143 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6144 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6145 39cd0ff6 2019-07-12 stsp
6146 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6147 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6148 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6149 5c1e53bc 2019-07-28 stsp
6150 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6151 5c1e53bc 2019-07-28 stsp ct_path++;
6152 5c1e53bc 2019-07-28 stsp
6153 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6154 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6155 5c1e53bc 2019-07-28 stsp break;
6156 5c1e53bc 2019-07-28 stsp }
6157 5c1e53bc 2019-07-28 stsp
6158 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6159 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6160 5c1e53bc 2019-07-28 stsp
6161 5c1e53bc 2019-07-28 stsp return NULL;
6162 5c1e53bc 2019-07-28 stsp }
6163 5c1e53bc 2019-07-28 stsp
6164 f0b75401 2019-08-03 stsp static const struct got_error *
6165 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6166 f0b75401 2019-08-03 stsp {
6167 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6168 f0b75401 2019-08-03 stsp
6169 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6170 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6171 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6172 f0b75401 2019-08-03 stsp }
6173 f0b75401 2019-08-03 stsp
6174 f0b75401 2019-08-03 stsp return NULL;
6175 f0b75401 2019-08-03 stsp }
6176 f0b75401 2019-08-03 stsp
6177 5f8a88c6 2019-08-03 stsp static const struct got_error *
6178 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6179 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6180 5f8a88c6 2019-08-03 stsp {
6181 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6182 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6183 5f8a88c6 2019-08-03 stsp
6184 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6185 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6186 5f8a88c6 2019-08-03 stsp continue;
6187 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6188 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6189 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6190 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6191 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6192 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6193 5f8a88c6 2019-08-03 stsp }
6194 5f8a88c6 2019-08-03 stsp
6195 5f8a88c6 2019-08-03 stsp return NULL;
6196 5f8a88c6 2019-08-03 stsp }
6197 5f8a88c6 2019-08-03 stsp
6198 39cd0ff6 2019-07-12 stsp const struct got_error *
6199 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6200 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6201 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6202 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6203 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6204 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6205 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6206 39cd0ff6 2019-07-12 stsp {
6207 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6208 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6209 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6210 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6211 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6212 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6213 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6214 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6215 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6216 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6217 39cd0ff6 2019-07-12 stsp
6218 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6219 39cd0ff6 2019-07-12 stsp
6220 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6221 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6222 39cd0ff6 2019-07-12 stsp
6223 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6224 39cd0ff6 2019-07-12 stsp if (err)
6225 39cd0ff6 2019-07-12 stsp goto done;
6226 39cd0ff6 2019-07-12 stsp
6227 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6228 39cd0ff6 2019-07-12 stsp if (err)
6229 39cd0ff6 2019-07-12 stsp goto done;
6230 39cd0ff6 2019-07-12 stsp
6231 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6232 39cd0ff6 2019-07-12 stsp if (err)
6233 39cd0ff6 2019-07-12 stsp goto done;
6234 39cd0ff6 2019-07-12 stsp
6235 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6236 39cd0ff6 2019-07-12 stsp if (err)
6237 39cd0ff6 2019-07-12 stsp goto done;
6238 39cd0ff6 2019-07-12 stsp
6239 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6240 5f8a88c6 2019-08-03 stsp &have_staged_files);
6241 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6242 5f8a88c6 2019-08-03 stsp goto done;
6243 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6244 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6245 5f8a88c6 2019-08-03 stsp if (err)
6246 5f8a88c6 2019-08-03 stsp goto done;
6247 5f8a88c6 2019-08-03 stsp }
6248 5f8a88c6 2019-08-03 stsp
6249 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6250 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6251 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6252 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6253 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6254 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6255 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6256 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6257 2a47b1e5 2022-11-01 stsp if (show_diff) {
6258 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6259 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6260 2a47b1e5 2022-11-01 stsp if (err)
6261 2a47b1e5 2022-11-01 stsp goto done;
6262 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6263 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6264 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6265 2a47b1e5 2022-11-01 stsp goto done;
6266 2a47b1e5 2022-11-01 stsp }
6267 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6268 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6269 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6270 2a47b1e5 2022-11-01 stsp goto done;
6271 2a47b1e5 2022-11-01 stsp }
6272 2a47b1e5 2022-11-01 stsp }
6273 2a47b1e5 2022-11-01 stsp
6274 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6275 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6276 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6277 5c1e53bc 2019-07-28 stsp if (err)
6278 5c1e53bc 2019-07-28 stsp goto done;
6279 5c1e53bc 2019-07-28 stsp }
6280 39cd0ff6 2019-07-12 stsp
6281 2a47b1e5 2022-11-01 stsp if (show_diff) {
6282 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6283 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6284 2a47b1e5 2022-11-01 stsp goto done;
6285 2a47b1e5 2022-11-01 stsp }
6286 2a47b1e5 2022-11-01 stsp }
6287 2a47b1e5 2022-11-01 stsp
6288 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6289 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6290 39cd0ff6 2019-07-12 stsp goto done;
6291 5c1e53bc 2019-07-28 stsp }
6292 5c1e53bc 2019-07-28 stsp
6293 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6294 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6295 5c1e53bc 2019-07-28 stsp if (err)
6296 5c1e53bc 2019-07-28 stsp goto done;
6297 39cd0ff6 2019-07-12 stsp }
6298 f0b75401 2019-08-03 stsp
6299 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6300 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6301 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6302 f0b75401 2019-08-03 stsp
6303 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6304 f0b75401 2019-08-03 stsp ct_path++;
6305 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6306 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6307 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6308 b50cabdf 2019-07-12 stsp if (err)
6309 b50cabdf 2019-07-12 stsp goto done;
6310 f0b75401 2019-08-03 stsp
6311 b50cabdf 2019-07-12 stsp }
6312 b50cabdf 2019-07-12 stsp
6313 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6314 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6315 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6316 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6317 39cd0ff6 2019-07-12 stsp if (err)
6318 39cd0ff6 2019-07-12 stsp goto done;
6319 39cd0ff6 2019-07-12 stsp
6320 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6321 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6322 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6323 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6324 39cd0ff6 2019-07-12 stsp err = sync_err;
6325 39cd0ff6 2019-07-12 stsp done:
6326 39cd0ff6 2019-07-12 stsp if (fileindex)
6327 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6328 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6329 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6330 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6331 39cd0ff6 2019-07-12 stsp err = unlockerr;
6332 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6333 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6334 d8bacb93 2023-01-10 mark
6335 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6336 39cd0ff6 2019-07-12 stsp }
6337 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6338 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6339 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6340 2a47b1e5 2022-11-01 stsp free(diff_path);
6341 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6342 2a47b1e5 2022-11-01 stsp err == NULL)
6343 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6344 c4296144 2019-05-09 stsp return err;
6345 8656d6c4 2019-05-20 stsp }
6346 8656d6c4 2019-05-20 stsp
6347 8656d6c4 2019-05-20 stsp const char *
6348 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6349 8656d6c4 2019-05-20 stsp {
6350 8656d6c4 2019-05-20 stsp return ct->path;
6351 8656d6c4 2019-05-20 stsp }
6352 8656d6c4 2019-05-20 stsp
6353 8656d6c4 2019-05-20 stsp unsigned int
6354 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6355 8656d6c4 2019-05-20 stsp {
6356 8656d6c4 2019-05-20 stsp return ct->status;
6357 818c7501 2019-07-11 stsp }
6358 818c7501 2019-07-11 stsp
6359 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6360 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6361 818c7501 2019-07-11 stsp struct got_repository *repo;
6362 818c7501 2019-07-11 stsp };
6363 818c7501 2019-07-11 stsp
6364 818c7501 2019-07-11 stsp static const struct got_error *
6365 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6366 818c7501 2019-07-11 stsp {
6367 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6368 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6369 818c7501 2019-07-11 stsp unsigned char status;
6370 818c7501 2019-07-11 stsp struct stat sb;
6371 818c7501 2019-07-11 stsp char *ondisk_path;
6372 818c7501 2019-07-11 stsp
6373 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6374 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6375 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6376 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6377 818c7501 2019-07-11 stsp
6378 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6379 818c7501 2019-07-11 stsp == -1)
6380 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6381 818c7501 2019-07-11 stsp
6382 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6383 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6384 818c7501 2019-07-11 stsp free(ondisk_path);
6385 818c7501 2019-07-11 stsp if (err)
6386 818c7501 2019-07-11 stsp return err;
6387 818c7501 2019-07-11 stsp
6388 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6389 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6390 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6391 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6392 818c7501 2019-07-11 stsp
6393 818c7501 2019-07-11 stsp return NULL;
6394 818c7501 2019-07-11 stsp }
6395 818c7501 2019-07-11 stsp
6396 818c7501 2019-07-11 stsp const struct got_error *
6397 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6398 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6399 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6400 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6401 818c7501 2019-07-11 stsp {
6402 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6403 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6404 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6405 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6406 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6407 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6408 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6409 818c7501 2019-07-11 stsp
6410 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6411 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6412 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6413 818c7501 2019-07-11 stsp
6414 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6415 818c7501 2019-07-11 stsp if (err)
6416 818c7501 2019-07-11 stsp return err;
6417 818c7501 2019-07-11 stsp
6418 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6419 818c7501 2019-07-11 stsp if (err)
6420 818c7501 2019-07-11 stsp goto done;
6421 818c7501 2019-07-11 stsp
6422 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6423 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6424 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6425 818c7501 2019-07-11 stsp &ok_arg);
6426 818c7501 2019-07-11 stsp if (err)
6427 818c7501 2019-07-11 stsp goto done;
6428 818c7501 2019-07-11 stsp
6429 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6430 818c7501 2019-07-11 stsp if (err)
6431 818c7501 2019-07-11 stsp goto done;
6432 818c7501 2019-07-11 stsp
6433 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6434 818c7501 2019-07-11 stsp if (err)
6435 818c7501 2019-07-11 stsp goto done;
6436 818c7501 2019-07-11 stsp
6437 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6438 818c7501 2019-07-11 stsp if (err)
6439 818c7501 2019-07-11 stsp goto done;
6440 818c7501 2019-07-11 stsp
6441 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6442 818c7501 2019-07-11 stsp 0);
6443 e51d7b55 2020-01-04 stsp if (err)
6444 e51d7b55 2020-01-04 stsp goto done;
6445 e51d7b55 2020-01-04 stsp
6446 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6447 818c7501 2019-07-11 stsp if (err)
6448 818c7501 2019-07-11 stsp goto done;
6449 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6450 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6451 e51d7b55 2020-01-04 stsp goto done;
6452 e51d7b55 2020-01-04 stsp }
6453 818c7501 2019-07-11 stsp
6454 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6455 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6456 818c7501 2019-07-11 stsp if (err)
6457 818c7501 2019-07-11 stsp goto done;
6458 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6459 818c7501 2019-07-11 stsp if (err)
6460 818c7501 2019-07-11 stsp goto done;
6461 818c7501 2019-07-11 stsp
6462 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6463 818c7501 2019-07-11 stsp
6464 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6465 818c7501 2019-07-11 stsp if (err)
6466 818c7501 2019-07-11 stsp goto done;
6467 818c7501 2019-07-11 stsp
6468 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6469 818c7501 2019-07-11 stsp if (err)
6470 818c7501 2019-07-11 stsp goto done;
6471 818c7501 2019-07-11 stsp
6472 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6473 818c7501 2019-07-11 stsp worktree->base_commit_id);
6474 818c7501 2019-07-11 stsp if (err)
6475 818c7501 2019-07-11 stsp goto done;
6476 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6477 818c7501 2019-07-11 stsp if (err)
6478 818c7501 2019-07-11 stsp goto done;
6479 818c7501 2019-07-11 stsp
6480 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6481 818c7501 2019-07-11 stsp if (err)
6482 818c7501 2019-07-11 stsp goto done;
6483 818c7501 2019-07-11 stsp done:
6484 818c7501 2019-07-11 stsp free(fileindex_path);
6485 818c7501 2019-07-11 stsp free(tmp_branch_name);
6486 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6487 818c7501 2019-07-11 stsp free(branch_ref_name);
6488 818c7501 2019-07-11 stsp if (branch_ref)
6489 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6490 818c7501 2019-07-11 stsp if (wt_branch)
6491 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6492 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6493 818c7501 2019-07-11 stsp if (err) {
6494 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6495 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6496 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6497 818c7501 2019-07-11 stsp }
6498 818c7501 2019-07-11 stsp if (*tmp_branch) {
6499 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6500 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6501 818c7501 2019-07-11 stsp }
6502 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6503 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6504 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6505 3e3a69f1 2019-07-25 stsp }
6506 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6507 818c7501 2019-07-11 stsp }
6508 818c7501 2019-07-11 stsp return err;
6509 818c7501 2019-07-11 stsp }
6510 818c7501 2019-07-11 stsp
6511 818c7501 2019-07-11 stsp const struct got_error *
6512 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6513 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6514 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6515 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6516 818c7501 2019-07-11 stsp {
6517 818c7501 2019-07-11 stsp const struct got_error *err;
6518 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6519 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6520 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6521 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6522 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6523 818c7501 2019-07-11 stsp
6524 818c7501 2019-07-11 stsp *commit_id = NULL;
6525 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6526 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6527 3e3a69f1 2019-07-25 stsp *branch = NULL;
6528 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6529 3e3a69f1 2019-07-25 stsp
6530 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6531 3e3a69f1 2019-07-25 stsp if (err)
6532 3e3a69f1 2019-07-25 stsp return err;
6533 818c7501 2019-07-11 stsp
6534 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6535 3e3a69f1 2019-07-25 stsp if (err)
6536 f032f1f7 2019-08-04 stsp goto done;
6537 f032f1f7 2019-08-04 stsp
6538 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6539 f032f1f7 2019-08-04 stsp &have_staged_files);
6540 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6541 f032f1f7 2019-08-04 stsp goto done;
6542 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6543 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6544 3e3a69f1 2019-07-25 stsp goto done;
6545 f032f1f7 2019-08-04 stsp }
6546 3e3a69f1 2019-07-25 stsp
6547 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6548 818c7501 2019-07-11 stsp if (err)
6549 f032f1f7 2019-08-04 stsp goto done;
6550 818c7501 2019-07-11 stsp
6551 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6552 818c7501 2019-07-11 stsp if (err)
6553 818c7501 2019-07-11 stsp goto done;
6554 818c7501 2019-07-11 stsp
6555 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6556 818c7501 2019-07-11 stsp if (err)
6557 818c7501 2019-07-11 stsp goto done;
6558 818c7501 2019-07-11 stsp
6559 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6560 818c7501 2019-07-11 stsp if (err)
6561 818c7501 2019-07-11 stsp goto done;
6562 818c7501 2019-07-11 stsp
6563 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6564 818c7501 2019-07-11 stsp if (err)
6565 818c7501 2019-07-11 stsp goto done;
6566 818c7501 2019-07-11 stsp
6567 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6568 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6569 818c7501 2019-07-11 stsp if (err)
6570 818c7501 2019-07-11 stsp goto done;
6571 818c7501 2019-07-11 stsp
6572 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6573 818c7501 2019-07-11 stsp if (err)
6574 818c7501 2019-07-11 stsp goto done;
6575 818c7501 2019-07-11 stsp
6576 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6577 818c7501 2019-07-11 stsp if (err)
6578 818c7501 2019-07-11 stsp goto done;
6579 818c7501 2019-07-11 stsp
6580 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6581 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6582 818c7501 2019-07-11 stsp if (err)
6583 818c7501 2019-07-11 stsp goto done;
6584 818c7501 2019-07-11 stsp
6585 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6586 818c7501 2019-07-11 stsp if (err)
6587 818c7501 2019-07-11 stsp goto done;
6588 818c7501 2019-07-11 stsp done:
6589 818c7501 2019-07-11 stsp free(commit_ref_name);
6590 818c7501 2019-07-11 stsp free(branch_ref_name);
6591 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6592 818c7501 2019-07-11 stsp if (commit_ref)
6593 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6594 818c7501 2019-07-11 stsp if (branch_ref)
6595 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6596 818c7501 2019-07-11 stsp if (err) {
6597 818c7501 2019-07-11 stsp free(*commit_id);
6598 818c7501 2019-07-11 stsp *commit_id = NULL;
6599 818c7501 2019-07-11 stsp if (*tmp_branch) {
6600 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6601 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6602 818c7501 2019-07-11 stsp }
6603 818c7501 2019-07-11 stsp if (*new_base_branch) {
6604 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6605 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6606 818c7501 2019-07-11 stsp }
6607 818c7501 2019-07-11 stsp if (*branch) {
6608 818c7501 2019-07-11 stsp got_ref_close(*branch);
6609 818c7501 2019-07-11 stsp *branch = NULL;
6610 818c7501 2019-07-11 stsp }
6611 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6612 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6613 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6614 3e3a69f1 2019-07-25 stsp }
6615 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6616 818c7501 2019-07-11 stsp }
6617 818c7501 2019-07-11 stsp return err;
6618 c4296144 2019-05-09 stsp }
6619 818c7501 2019-07-11 stsp
6620 818c7501 2019-07-11 stsp const struct got_error *
6621 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6622 818c7501 2019-07-11 stsp {
6623 818c7501 2019-07-11 stsp const struct got_error *err;
6624 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6625 818c7501 2019-07-11 stsp
6626 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6627 818c7501 2019-07-11 stsp if (err)
6628 818c7501 2019-07-11 stsp return err;
6629 818c7501 2019-07-11 stsp
6630 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6631 818c7501 2019-07-11 stsp free(tmp_branch_name);
6632 818c7501 2019-07-11 stsp return NULL;
6633 818c7501 2019-07-11 stsp }
6634 818c7501 2019-07-11 stsp
6635 818c7501 2019-07-11 stsp static const struct got_error *
6636 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6637 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6638 818c7501 2019-07-11 stsp {
6639 0ebf8283 2019-07-24 stsp *logmsg = arg;
6640 818c7501 2019-07-11 stsp return NULL;
6641 818c7501 2019-07-11 stsp }
6642 818c7501 2019-07-11 stsp
6643 818c7501 2019-07-11 stsp static const struct got_error *
6644 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6645 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6646 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6647 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6648 818c7501 2019-07-11 stsp {
6649 818c7501 2019-07-11 stsp return NULL;
6650 01757395 2019-07-12 stsp }
6651 01757395 2019-07-12 stsp
6652 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6653 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6654 01757395 2019-07-12 stsp void *progress_arg;
6655 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6656 01757395 2019-07-12 stsp };
6657 01757395 2019-07-12 stsp
6658 01757395 2019-07-12 stsp static const struct got_error *
6659 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6660 01757395 2019-07-12 stsp {
6661 01757395 2019-07-12 stsp const struct got_error *err;
6662 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6663 01757395 2019-07-12 stsp char *p;
6664 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6665 01757395 2019-07-12 stsp
6666 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6667 01757395 2019-07-12 stsp if (err)
6668 01757395 2019-07-12 stsp return err;
6669 01757395 2019-07-12 stsp
6670 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6671 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6672 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6673 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6674 01757395 2019-07-12 stsp return NULL;
6675 01757395 2019-07-12 stsp
6676 01757395 2019-07-12 stsp p = strdup(path);
6677 01757395 2019-07-12 stsp if (p == NULL)
6678 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6679 01757395 2019-07-12 stsp
6680 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6681 01757395 2019-07-12 stsp if (err || new == NULL)
6682 01757395 2019-07-12 stsp free(p);
6683 01757395 2019-07-12 stsp return err;
6684 818c7501 2019-07-11 stsp }
6685 818c7501 2019-07-11 stsp
6686 0ebf8283 2019-07-24 stsp static const struct got_error *
6687 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6688 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6689 818c7501 2019-07-11 stsp {
6690 818c7501 2019-07-11 stsp const struct got_error *err;
6691 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6692 818c7501 2019-07-11 stsp
6693 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6694 818c7501 2019-07-11 stsp if (err) {
6695 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6696 818c7501 2019-07-11 stsp goto done;
6697 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6698 818c7501 2019-07-11 stsp if (err)
6699 818c7501 2019-07-11 stsp goto done;
6700 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6701 818c7501 2019-07-11 stsp if (err)
6702 818c7501 2019-07-11 stsp goto done;
6703 de05890f 2020-03-05 stsp } else if (is_rebase) {
6704 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6705 818c7501 2019-07-11 stsp int cmp;
6706 818c7501 2019-07-11 stsp
6707 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6708 818c7501 2019-07-11 stsp if (err)
6709 818c7501 2019-07-11 stsp goto done;
6710 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6711 818c7501 2019-07-11 stsp free(stored_id);
6712 818c7501 2019-07-11 stsp if (cmp != 0) {
6713 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6714 818c7501 2019-07-11 stsp goto done;
6715 818c7501 2019-07-11 stsp }
6716 818c7501 2019-07-11 stsp }
6717 0ebf8283 2019-07-24 stsp done:
6718 0ebf8283 2019-07-24 stsp if (commit_ref)
6719 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6720 0ebf8283 2019-07-24 stsp return err;
6721 0ebf8283 2019-07-24 stsp }
6722 0ebf8283 2019-07-24 stsp
6723 0ebf8283 2019-07-24 stsp static const struct got_error *
6724 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6725 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6726 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6727 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6728 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6729 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6730 0ebf8283 2019-07-24 stsp {
6731 0ebf8283 2019-07-24 stsp const struct got_error *err;
6732 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6733 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6734 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6735 818c7501 2019-07-11 stsp
6736 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6737 0ebf8283 2019-07-24 stsp
6738 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6739 0ebf8283 2019-07-24 stsp if (err)
6740 0ebf8283 2019-07-24 stsp return err;
6741 0ebf8283 2019-07-24 stsp
6742 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6743 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6744 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6745 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6746 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6747 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6748 818c7501 2019-07-11 stsp if (commit_ref)
6749 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6750 818c7501 2019-07-11 stsp return err;
6751 818c7501 2019-07-11 stsp }
6752 818c7501 2019-07-11 stsp
6753 818c7501 2019-07-11 stsp const struct got_error *
6754 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6755 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6756 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6757 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6758 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6759 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6760 818c7501 2019-07-11 stsp {
6761 0ebf8283 2019-07-24 stsp const struct got_error *err;
6762 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6763 0ebf8283 2019-07-24 stsp
6764 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6765 0ebf8283 2019-07-24 stsp if (err)
6766 0ebf8283 2019-07-24 stsp return err;
6767 0ebf8283 2019-07-24 stsp
6768 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6769 0ebf8283 2019-07-24 stsp if (err)
6770 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp
6772 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6773 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6774 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6775 0ebf8283 2019-07-24 stsp done:
6776 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6777 0ebf8283 2019-07-24 stsp return err;
6778 0ebf8283 2019-07-24 stsp }
6779 0ebf8283 2019-07-24 stsp
6780 0ebf8283 2019-07-24 stsp const struct got_error *
6781 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6782 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6783 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6784 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6785 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6786 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6787 0ebf8283 2019-07-24 stsp {
6788 0ebf8283 2019-07-24 stsp const struct got_error *err;
6789 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6790 0ebf8283 2019-07-24 stsp
6791 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6792 0ebf8283 2019-07-24 stsp if (err)
6793 0ebf8283 2019-07-24 stsp return err;
6794 0ebf8283 2019-07-24 stsp
6795 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6796 0ebf8283 2019-07-24 stsp if (err)
6797 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp
6799 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6800 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6801 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6802 0ebf8283 2019-07-24 stsp done:
6803 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6804 0ebf8283 2019-07-24 stsp return err;
6805 0ebf8283 2019-07-24 stsp }
6806 0ebf8283 2019-07-24 stsp
6807 0ebf8283 2019-07-24 stsp static const struct got_error *
6808 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6809 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6810 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6811 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6812 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6813 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6814 0ebf8283 2019-07-24 stsp {
6815 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6816 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6817 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6818 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6819 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6820 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6821 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6822 818c7501 2019-07-11 stsp
6823 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6824 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6825 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6826 a0e95631 2019-07-12 stsp
6827 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6828 818c7501 2019-07-11 stsp
6829 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6830 a0e95631 2019-07-12 stsp if (err)
6831 3e3a69f1 2019-07-25 stsp return err;
6832 a0e95631 2019-07-12 stsp
6833 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6834 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6835 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6836 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6837 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
6838 01757395 2019-07-12 stsp /*
6839 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6840 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6841 6c13b005 2021-09-02 stsp *
6842 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6843 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6844 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6845 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6846 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6847 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6848 01757395 2019-07-12 stsp */
6849 01757395 2019-07-12 stsp if (merged_paths) {
6850 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6851 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6852 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6853 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6854 f2a9dc41 2019-12-13 tracey 0);
6855 01757395 2019-07-12 stsp if (err)
6856 01757395 2019-07-12 stsp goto done;
6857 01757395 2019-07-12 stsp }
6858 01757395 2019-07-12 stsp } else {
6859 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6860 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6861 01757395 2019-07-12 stsp if (err)
6862 01757395 2019-07-12 stsp goto done;
6863 01757395 2019-07-12 stsp }
6864 a0e95631 2019-07-12 stsp
6865 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6866 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6867 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6868 a0e95631 2019-07-12 stsp if (err)
6869 a0e95631 2019-07-12 stsp goto done;
6870 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6871 a0e95631 2019-07-12 stsp goto done;
6872 ff0d2220 2019-07-11 stsp }
6873 818c7501 2019-07-11 stsp
6874 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6875 edd02c5e 2019-07-11 stsp if (err)
6876 edd02c5e 2019-07-11 stsp goto done;
6877 edd02c5e 2019-07-11 stsp
6878 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6879 818c7501 2019-07-11 stsp if (err)
6880 818c7501 2019-07-11 stsp goto done;
6881 818c7501 2019-07-11 stsp
6882 5943eee2 2019-08-13 stsp if (new_logmsg) {
6883 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6884 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6885 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6886 5943eee2 2019-08-13 stsp goto done;
6887 5943eee2 2019-08-13 stsp }
6888 5943eee2 2019-08-13 stsp } else {
6889 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6890 5943eee2 2019-08-13 stsp if (err)
6891 5943eee2 2019-08-13 stsp goto done;
6892 5943eee2 2019-08-13 stsp }
6893 0ebf8283 2019-07-24 stsp
6894 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6895 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6896 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6897 50e7a649 2022-07-22 stsp committer ? committer :
6898 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6899 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6900 a0e95631 2019-07-12 stsp if (err)
6901 a0e95631 2019-07-12 stsp goto done;
6902 a0e95631 2019-07-12 stsp
6903 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6904 a0e95631 2019-07-12 stsp if (err)
6905 a0e95631 2019-07-12 stsp goto done;
6906 a0e95631 2019-07-12 stsp
6907 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6908 a0e95631 2019-07-12 stsp if (err)
6909 a0e95631 2019-07-12 stsp goto done;
6910 a0e95631 2019-07-12 stsp
6911 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6912 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6913 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6914 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6915 a0e95631 2019-07-12 stsp err = sync_err;
6916 818c7501 2019-07-11 stsp done:
6917 a0e95631 2019-07-12 stsp free(fileindex_path);
6918 a0e95631 2019-07-12 stsp free(head_commit_id);
6919 a0e95631 2019-07-12 stsp if (head_ref)
6920 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6921 818c7501 2019-07-11 stsp if (err) {
6922 818c7501 2019-07-11 stsp free(*new_commit_id);
6923 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6924 818c7501 2019-07-11 stsp }
6925 818c7501 2019-07-11 stsp return err;
6926 818c7501 2019-07-11 stsp }
6927 818c7501 2019-07-11 stsp
6928 818c7501 2019-07-11 stsp const struct got_error *
6929 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6930 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6931 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6932 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6933 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
6934 12383673 2023-02-18 mark struct got_repository *repo)
6935 0ebf8283 2019-07-24 stsp {
6936 0ebf8283 2019-07-24 stsp const struct got_error *err;
6937 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6938 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6939 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6940 0ebf8283 2019-07-24 stsp
6941 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6942 0ebf8283 2019-07-24 stsp if (err)
6943 0ebf8283 2019-07-24 stsp return err;
6944 0ebf8283 2019-07-24 stsp
6945 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6946 0ebf8283 2019-07-24 stsp if (err)
6947 0ebf8283 2019-07-24 stsp goto done;
6948 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6949 0ebf8283 2019-07-24 stsp if (err)
6950 0ebf8283 2019-07-24 stsp goto done;
6951 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6952 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp }
6955 0ebf8283 2019-07-24 stsp
6956 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6957 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6958 12383673 2023-02-18 mark NULL, allow_conflict, repo);
6959 0ebf8283 2019-07-24 stsp done:
6960 0ebf8283 2019-07-24 stsp if (commit_ref)
6961 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6962 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6963 0ebf8283 2019-07-24 stsp free(commit_id);
6964 0ebf8283 2019-07-24 stsp return err;
6965 0ebf8283 2019-07-24 stsp }
6966 0ebf8283 2019-07-24 stsp
6967 0ebf8283 2019-07-24 stsp const struct got_error *
6968 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6969 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6970 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6971 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6972 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6973 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6974 0ebf8283 2019-07-24 stsp {
6975 0ebf8283 2019-07-24 stsp const struct got_error *err;
6976 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6977 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6978 0ebf8283 2019-07-24 stsp
6979 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp return err;
6982 0ebf8283 2019-07-24 stsp
6983 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6984 0ebf8283 2019-07-24 stsp if (err)
6985 0ebf8283 2019-07-24 stsp goto done;
6986 0ebf8283 2019-07-24 stsp
6987 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6988 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6989 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
6990 0ebf8283 2019-07-24 stsp done:
6991 0ebf8283 2019-07-24 stsp if (commit_ref)
6992 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6993 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6994 0ebf8283 2019-07-24 stsp return err;
6995 0ebf8283 2019-07-24 stsp }
6996 0ebf8283 2019-07-24 stsp
6997 0ebf8283 2019-07-24 stsp const struct got_error *
6998 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6999 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7000 818c7501 2019-07-11 stsp {
7001 3e3a69f1 2019-07-25 stsp if (fileindex)
7002 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7003 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7004 69844fba 2019-07-11 stsp }
7005 69844fba 2019-07-11 stsp
7006 69844fba 2019-07-11 stsp static const struct got_error *
7007 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7008 69844fba 2019-07-11 stsp {
7009 69844fba 2019-07-11 stsp const struct got_error *err;
7010 69844fba 2019-07-11 stsp struct got_reference *ref;
7011 69844fba 2019-07-11 stsp
7012 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7013 69844fba 2019-07-11 stsp if (err) {
7014 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7015 69844fba 2019-07-11 stsp return NULL;
7016 69844fba 2019-07-11 stsp return err;
7017 69844fba 2019-07-11 stsp }
7018 69844fba 2019-07-11 stsp
7019 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7020 69844fba 2019-07-11 stsp got_ref_close(ref);
7021 69844fba 2019-07-11 stsp return err;
7022 818c7501 2019-07-11 stsp }
7023 818c7501 2019-07-11 stsp
7024 69844fba 2019-07-11 stsp static const struct got_error *
7025 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7026 69844fba 2019-07-11 stsp {
7027 69844fba 2019-07-11 stsp const struct got_error *err;
7028 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7029 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7030 69844fba 2019-07-11 stsp
7031 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7032 69844fba 2019-07-11 stsp if (err)
7033 69844fba 2019-07-11 stsp goto done;
7034 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7035 69844fba 2019-07-11 stsp if (err)
7036 69844fba 2019-07-11 stsp goto done;
7037 69844fba 2019-07-11 stsp
7038 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7039 69844fba 2019-07-11 stsp if (err)
7040 69844fba 2019-07-11 stsp goto done;
7041 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7042 69844fba 2019-07-11 stsp if (err)
7043 69844fba 2019-07-11 stsp goto done;
7044 69844fba 2019-07-11 stsp
7045 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7046 69844fba 2019-07-11 stsp if (err)
7047 69844fba 2019-07-11 stsp goto done;
7048 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7049 69844fba 2019-07-11 stsp if (err)
7050 69844fba 2019-07-11 stsp goto done;
7051 69844fba 2019-07-11 stsp
7052 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7053 69844fba 2019-07-11 stsp if (err)
7054 69844fba 2019-07-11 stsp goto done;
7055 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7056 69844fba 2019-07-11 stsp if (err)
7057 69844fba 2019-07-11 stsp goto done;
7058 69844fba 2019-07-11 stsp
7059 69844fba 2019-07-11 stsp done:
7060 69844fba 2019-07-11 stsp free(tmp_branch_name);
7061 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7062 69844fba 2019-07-11 stsp free(branch_ref_name);
7063 69844fba 2019-07-11 stsp free(commit_ref_name);
7064 e600f124 2021-03-21 stsp return err;
7065 e600f124 2021-03-21 stsp }
7066 e600f124 2021-03-21 stsp
7067 336075a4 2022-06-25 op static const struct got_error *
7068 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7069 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7070 e600f124 2021-03-21 stsp {
7071 e600f124 2021-03-21 stsp const struct got_error *err;
7072 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7073 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7074 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7075 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7076 e600f124 2021-03-21 stsp char *refname = NULL;
7077 e600f124 2021-03-21 stsp
7078 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7079 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7080 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7081 e600f124 2021-03-21 stsp branch_name += 11;
7082 e600f124 2021-03-21 stsp
7083 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7084 e600f124 2021-03-21 stsp if (err)
7085 e600f124 2021-03-21 stsp return err;
7086 e600f124 2021-03-21 stsp
7087 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7088 e600f124 2021-03-21 stsp new_id_str) == -1) {
7089 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7090 e600f124 2021-03-21 stsp goto done;
7091 e600f124 2021-03-21 stsp }
7092 e600f124 2021-03-21 stsp
7093 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7094 e600f124 2021-03-21 stsp if (err)
7095 e600f124 2021-03-21 stsp goto done;
7096 e600f124 2021-03-21 stsp
7097 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7098 e600f124 2021-03-21 stsp if (err)
7099 e600f124 2021-03-21 stsp goto done;
7100 e600f124 2021-03-21 stsp
7101 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7102 e600f124 2021-03-21 stsp done:
7103 e600f124 2021-03-21 stsp free(new_id_str);
7104 e600f124 2021-03-21 stsp free(refname);
7105 e600f124 2021-03-21 stsp free(old_commit_id);
7106 e600f124 2021-03-21 stsp if (ref)
7107 e600f124 2021-03-21 stsp got_ref_close(ref);
7108 69844fba 2019-07-11 stsp return err;
7109 69844fba 2019-07-11 stsp }
7110 69844fba 2019-07-11 stsp
7111 818c7501 2019-07-11 stsp const struct got_error *
7112 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7113 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7114 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7115 ef85a376 2023-02-01 mark int create_backup)
7116 818c7501 2019-07-11 stsp {
7117 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7118 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7119 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7120 818c7501 2019-07-11 stsp
7121 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7122 818c7501 2019-07-11 stsp if (err)
7123 818c7501 2019-07-11 stsp return err;
7124 e600f124 2021-03-21 stsp
7125 e600f124 2021-03-21 stsp if (create_backup) {
7126 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7127 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7128 e600f124 2021-03-21 stsp if (err)
7129 e600f124 2021-03-21 stsp goto done;
7130 e600f124 2021-03-21 stsp }
7131 818c7501 2019-07-11 stsp
7132 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7133 818c7501 2019-07-11 stsp if (err)
7134 818c7501 2019-07-11 stsp goto done;
7135 818c7501 2019-07-11 stsp
7136 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7137 818c7501 2019-07-11 stsp if (err)
7138 818c7501 2019-07-11 stsp goto done;
7139 818c7501 2019-07-11 stsp
7140 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7141 818c7501 2019-07-11 stsp if (err)
7142 818c7501 2019-07-11 stsp goto done;
7143 818c7501 2019-07-11 stsp
7144 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7145 a615e0e7 2020-12-16 stsp if (err)
7146 a615e0e7 2020-12-16 stsp goto done;
7147 a615e0e7 2020-12-16 stsp
7148 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7149 a615e0e7 2020-12-16 stsp if (err)
7150 a615e0e7 2020-12-16 stsp goto done;
7151 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7152 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7153 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7154 a615e0e7 2020-12-16 stsp err = sync_err;
7155 818c7501 2019-07-11 stsp done:
7156 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7157 a615e0e7 2020-12-16 stsp free(fileindex_path);
7158 818c7501 2019-07-11 stsp free(new_head_commit_id);
7159 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7160 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7161 818c7501 2019-07-11 stsp err = unlockerr;
7162 818c7501 2019-07-11 stsp return err;
7163 818c7501 2019-07-11 stsp }
7164 818c7501 2019-07-11 stsp
7165 818c7501 2019-07-11 stsp const struct got_error *
7166 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7167 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7168 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7169 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7170 818c7501 2019-07-11 stsp {
7171 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7172 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7173 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7174 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7175 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7176 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7177 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7178 818c7501 2019-07-11 stsp
7179 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7180 818c7501 2019-07-11 stsp if (err)
7181 818c7501 2019-07-11 stsp return err;
7182 818c7501 2019-07-11 stsp
7183 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7184 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7185 a44927cc 2022-04-07 stsp if (err)
7186 a44927cc 2022-04-07 stsp goto done;
7187 a44927cc 2022-04-07 stsp
7188 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7189 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7190 818c7501 2019-07-11 stsp if (err)
7191 818c7501 2019-07-11 stsp goto done;
7192 818c7501 2019-07-11 stsp
7193 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7194 818c7501 2019-07-11 stsp if (err)
7195 818c7501 2019-07-11 stsp goto done;
7196 818c7501 2019-07-11 stsp
7197 818c7501 2019-07-11 stsp /*
7198 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7199 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7200 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7201 818c7501 2019-07-11 stsp */
7202 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7203 818c7501 2019-07-11 stsp if (err)
7204 818c7501 2019-07-11 stsp goto done;
7205 818c7501 2019-07-11 stsp
7206 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7207 818c7501 2019-07-11 stsp if (err)
7208 818c7501 2019-07-11 stsp goto done;
7209 818c7501 2019-07-11 stsp
7210 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7211 a44927cc 2022-04-07 stsp worktree->path_prefix);
7212 ca355955 2019-07-12 stsp if (err)
7213 ca355955 2019-07-12 stsp goto done;
7214 ca355955 2019-07-12 stsp
7215 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7216 ca355955 2019-07-12 stsp if (err)
7217 ca355955 2019-07-12 stsp goto done;
7218 ca355955 2019-07-12 stsp
7219 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7220 818c7501 2019-07-11 stsp if (err)
7221 818c7501 2019-07-11 stsp goto done;
7222 818c7501 2019-07-11 stsp
7223 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7224 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7225 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7226 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7227 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7228 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7229 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7230 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7231 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7232 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7233 55bd499d 2019-07-12 stsp if (err)
7234 1f1abb7e 2019-08-08 stsp goto sync;
7235 55bd499d 2019-07-12 stsp
7236 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7237 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7238 ca355955 2019-07-12 stsp sync:
7239 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7240 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7241 ca355955 2019-07-12 stsp err = sync_err;
7242 818c7501 2019-07-11 stsp done:
7243 818c7501 2019-07-11 stsp got_ref_close(resolved);
7244 a3a2faf2 2019-07-12 stsp free(tree_id);
7245 818c7501 2019-07-11 stsp free(commit_id);
7246 a44927cc 2022-04-07 stsp if (commit)
7247 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7248 0ebf8283 2019-07-24 stsp if (fileindex)
7249 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7250 0ebf8283 2019-07-24 stsp free(fileindex_path);
7251 0ebf8283 2019-07-24 stsp
7252 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7253 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7254 0ebf8283 2019-07-24 stsp err = unlockerr;
7255 0ebf8283 2019-07-24 stsp return err;
7256 0ebf8283 2019-07-24 stsp }
7257 0ebf8283 2019-07-24 stsp
7258 0ebf8283 2019-07-24 stsp const struct got_error *
7259 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7260 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7261 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7262 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7263 0ebf8283 2019-07-24 stsp {
7264 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7265 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7266 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7267 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7268 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7269 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7270 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7271 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7272 0ebf8283 2019-07-24 stsp
7273 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7274 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7275 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7276 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7277 0ebf8283 2019-07-24 stsp
7278 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7279 0ebf8283 2019-07-24 stsp if (err)
7280 0ebf8283 2019-07-24 stsp return err;
7281 0ebf8283 2019-07-24 stsp
7282 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7283 0ebf8283 2019-07-24 stsp if (err)
7284 0ebf8283 2019-07-24 stsp goto done;
7285 0ebf8283 2019-07-24 stsp
7286 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7287 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7288 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7289 0ebf8283 2019-07-24 stsp &ok_arg);
7290 0ebf8283 2019-07-24 stsp if (err)
7291 0ebf8283 2019-07-24 stsp goto done;
7292 0ebf8283 2019-07-24 stsp
7293 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7294 0ebf8283 2019-07-24 stsp if (err)
7295 0ebf8283 2019-07-24 stsp goto done;
7296 0ebf8283 2019-07-24 stsp
7297 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7298 0ebf8283 2019-07-24 stsp if (err)
7299 0ebf8283 2019-07-24 stsp goto done;
7300 0ebf8283 2019-07-24 stsp
7301 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7302 0ebf8283 2019-07-24 stsp worktree);
7303 0ebf8283 2019-07-24 stsp if (err)
7304 0ebf8283 2019-07-24 stsp goto done;
7305 0ebf8283 2019-07-24 stsp
7306 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7307 0ebf8283 2019-07-24 stsp 0);
7308 0ebf8283 2019-07-24 stsp if (err)
7309 0ebf8283 2019-07-24 stsp goto done;
7310 0ebf8283 2019-07-24 stsp
7311 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7312 0ebf8283 2019-07-24 stsp if (err)
7313 0ebf8283 2019-07-24 stsp goto done;
7314 0ebf8283 2019-07-24 stsp
7315 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7316 0ebf8283 2019-07-24 stsp if (err)
7317 0ebf8283 2019-07-24 stsp goto done;
7318 0ebf8283 2019-07-24 stsp
7319 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7320 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7321 0ebf8283 2019-07-24 stsp if (err)
7322 0ebf8283 2019-07-24 stsp goto done;
7323 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7324 0ebf8283 2019-07-24 stsp if (err)
7325 0ebf8283 2019-07-24 stsp goto done;
7326 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7327 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7328 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7329 0ebf8283 2019-07-24 stsp goto done;
7330 0ebf8283 2019-07-24 stsp }
7331 0ebf8283 2019-07-24 stsp
7332 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7333 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7334 0ebf8283 2019-07-24 stsp if (err)
7335 0ebf8283 2019-07-24 stsp goto done;
7336 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7337 0ebf8283 2019-07-24 stsp if (err)
7338 0ebf8283 2019-07-24 stsp goto done;
7339 0ebf8283 2019-07-24 stsp
7340 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7341 0ebf8283 2019-07-24 stsp if (err)
7342 0ebf8283 2019-07-24 stsp goto done;
7343 0ebf8283 2019-07-24 stsp done:
7344 0ebf8283 2019-07-24 stsp free(fileindex_path);
7345 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7346 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7347 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7348 0ebf8283 2019-07-24 stsp if (wt_branch)
7349 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7350 0ebf8283 2019-07-24 stsp if (err) {
7351 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7352 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7353 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7354 0ebf8283 2019-07-24 stsp }
7355 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7356 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7357 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7358 0ebf8283 2019-07-24 stsp }
7359 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7360 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7361 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7362 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7363 3e3a69f1 2019-07-25 stsp }
7364 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7365 0ebf8283 2019-07-24 stsp }
7366 0ebf8283 2019-07-24 stsp return err;
7367 0ebf8283 2019-07-24 stsp }
7368 0ebf8283 2019-07-24 stsp
7369 0ebf8283 2019-07-24 stsp const struct got_error *
7370 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7371 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7372 0ebf8283 2019-07-24 stsp {
7373 3e3a69f1 2019-07-25 stsp if (fileindex)
7374 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7375 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7376 0ebf8283 2019-07-24 stsp }
7377 0ebf8283 2019-07-24 stsp
7378 0ebf8283 2019-07-24 stsp const struct got_error *
7379 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7380 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7381 0ebf8283 2019-07-24 stsp {
7382 0ebf8283 2019-07-24 stsp const struct got_error *err;
7383 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7384 0ebf8283 2019-07-24 stsp
7385 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7386 0ebf8283 2019-07-24 stsp if (err)
7387 0ebf8283 2019-07-24 stsp return err;
7388 0ebf8283 2019-07-24 stsp
7389 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7390 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7391 0ebf8283 2019-07-24 stsp return NULL;
7392 0ebf8283 2019-07-24 stsp }
7393 0ebf8283 2019-07-24 stsp
7394 0ebf8283 2019-07-24 stsp const struct got_error *
7395 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7396 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7397 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7398 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7399 0ebf8283 2019-07-24 stsp {
7400 0ebf8283 2019-07-24 stsp const struct got_error *err;
7401 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7402 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7403 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7404 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7405 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7406 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7407 0ebf8283 2019-07-24 stsp
7408 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7409 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7410 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7411 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7412 0ebf8283 2019-07-24 stsp
7413 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7414 3e3a69f1 2019-07-25 stsp if (err)
7415 3e3a69f1 2019-07-25 stsp return err;
7416 3e3a69f1 2019-07-25 stsp
7417 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7418 3e3a69f1 2019-07-25 stsp if (err)
7419 f032f1f7 2019-08-04 stsp goto done;
7420 f032f1f7 2019-08-04 stsp
7421 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7422 f032f1f7 2019-08-04 stsp &have_staged_files);
7423 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7424 f032f1f7 2019-08-04 stsp goto done;
7425 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7426 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7427 3e3a69f1 2019-07-25 stsp goto done;
7428 f032f1f7 2019-08-04 stsp }
7429 3e3a69f1 2019-07-25 stsp
7430 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7431 0ebf8283 2019-07-24 stsp if (err)
7432 f032f1f7 2019-08-04 stsp goto done;
7433 0ebf8283 2019-07-24 stsp
7434 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7435 0ebf8283 2019-07-24 stsp if (err)
7436 0ebf8283 2019-07-24 stsp goto done;
7437 0ebf8283 2019-07-24 stsp
7438 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7439 0ebf8283 2019-07-24 stsp if (err)
7440 0ebf8283 2019-07-24 stsp goto done;
7441 0ebf8283 2019-07-24 stsp
7442 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7443 0ebf8283 2019-07-24 stsp worktree);
7444 0ebf8283 2019-07-24 stsp if (err)
7445 0ebf8283 2019-07-24 stsp goto done;
7446 0ebf8283 2019-07-24 stsp
7447 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7448 0ebf8283 2019-07-24 stsp if (err)
7449 0ebf8283 2019-07-24 stsp goto done;
7450 0ebf8283 2019-07-24 stsp
7451 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7452 0ebf8283 2019-07-24 stsp if (err)
7453 0ebf8283 2019-07-24 stsp goto done;
7454 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7455 0ebf8283 2019-07-24 stsp if (err)
7456 0ebf8283 2019-07-24 stsp goto done;
7457 0ebf8283 2019-07-24 stsp
7458 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7459 0ebf8283 2019-07-24 stsp if (err)
7460 0ebf8283 2019-07-24 stsp goto done;
7461 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7462 0ebf8283 2019-07-24 stsp if (err)
7463 0ebf8283 2019-07-24 stsp goto done;
7464 0ebf8283 2019-07-24 stsp
7465 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7466 0ebf8283 2019-07-24 stsp if (err)
7467 0ebf8283 2019-07-24 stsp goto done;
7468 0ebf8283 2019-07-24 stsp done:
7469 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7470 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7471 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7472 0ebf8283 2019-07-24 stsp if (commit_ref)
7473 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7474 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7475 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7476 0ebf8283 2019-07-24 stsp if (err) {
7477 0ebf8283 2019-07-24 stsp free(*commit_id);
7478 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7479 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7480 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7481 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7482 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7483 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7484 0ebf8283 2019-07-24 stsp }
7485 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7486 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7487 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7488 3e3a69f1 2019-07-25 stsp }
7489 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7490 0ebf8283 2019-07-24 stsp }
7491 0ebf8283 2019-07-24 stsp return err;
7492 0ebf8283 2019-07-24 stsp }
7493 0ebf8283 2019-07-24 stsp
7494 0ebf8283 2019-07-24 stsp static const struct got_error *
7495 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7496 0ebf8283 2019-07-24 stsp {
7497 0ebf8283 2019-07-24 stsp const struct got_error *err;
7498 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7499 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7500 0ebf8283 2019-07-24 stsp
7501 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7502 0ebf8283 2019-07-24 stsp if (err)
7503 0ebf8283 2019-07-24 stsp goto done;
7504 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7505 0ebf8283 2019-07-24 stsp if (err)
7506 0ebf8283 2019-07-24 stsp goto done;
7507 0ebf8283 2019-07-24 stsp
7508 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7509 0ebf8283 2019-07-24 stsp worktree);
7510 0ebf8283 2019-07-24 stsp if (err)
7511 0ebf8283 2019-07-24 stsp goto done;
7512 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
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 = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7517 0ebf8283 2019-07-24 stsp if (err)
7518 0ebf8283 2019-07-24 stsp goto done;
7519 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
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 = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7524 0ebf8283 2019-07-24 stsp if (err)
7525 0ebf8283 2019-07-24 stsp goto done;
7526 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7527 0ebf8283 2019-07-24 stsp if (err)
7528 0ebf8283 2019-07-24 stsp goto done;
7529 0ebf8283 2019-07-24 stsp done:
7530 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7531 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7532 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7533 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7534 0ebf8283 2019-07-24 stsp return err;
7535 0ebf8283 2019-07-24 stsp }
7536 0ebf8283 2019-07-24 stsp
7537 0ebf8283 2019-07-24 stsp const struct got_error *
7538 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7539 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7540 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7541 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7542 0ebf8283 2019-07-24 stsp {
7543 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7544 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7545 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7546 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7547 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7548 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7549 0ebf8283 2019-07-24 stsp
7550 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7551 0ebf8283 2019-07-24 stsp if (err)
7552 0ebf8283 2019-07-24 stsp return err;
7553 0ebf8283 2019-07-24 stsp
7554 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7555 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7556 a44927cc 2022-04-07 stsp if (err)
7557 a44927cc 2022-04-07 stsp goto done;
7558 a44927cc 2022-04-07 stsp
7559 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7560 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7561 0ebf8283 2019-07-24 stsp if (err)
7562 0ebf8283 2019-07-24 stsp goto done;
7563 0ebf8283 2019-07-24 stsp
7564 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7565 0ebf8283 2019-07-24 stsp if (err)
7566 0ebf8283 2019-07-24 stsp goto done;
7567 0ebf8283 2019-07-24 stsp
7568 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7569 0ebf8283 2019-07-24 stsp if (err)
7570 0ebf8283 2019-07-24 stsp goto done;
7571 0ebf8283 2019-07-24 stsp
7572 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7573 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7574 0ebf8283 2019-07-24 stsp if (err)
7575 0ebf8283 2019-07-24 stsp goto done;
7576 0ebf8283 2019-07-24 stsp
7577 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7578 0ebf8283 2019-07-24 stsp if (err)
7579 0ebf8283 2019-07-24 stsp goto done;
7580 0ebf8283 2019-07-24 stsp
7581 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7582 0ebf8283 2019-07-24 stsp if (err)
7583 0ebf8283 2019-07-24 stsp goto done;
7584 0ebf8283 2019-07-24 stsp
7585 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7586 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7587 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7588 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7589 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7590 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7591 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7592 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7593 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7594 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7595 0ebf8283 2019-07-24 stsp if (err)
7596 1f1abb7e 2019-08-08 stsp goto sync;
7597 0ebf8283 2019-07-24 stsp
7598 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7599 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7600 0ebf8283 2019-07-24 stsp sync:
7601 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7602 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7603 0ebf8283 2019-07-24 stsp err = sync_err;
7604 0ebf8283 2019-07-24 stsp done:
7605 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7606 0ebf8283 2019-07-24 stsp free(tree_id);
7607 818c7501 2019-07-11 stsp free(fileindex_path);
7608 818c7501 2019-07-11 stsp
7609 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7610 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7611 818c7501 2019-07-11 stsp err = unlockerr;
7612 818c7501 2019-07-11 stsp return err;
7613 818c7501 2019-07-11 stsp }
7614 0ebf8283 2019-07-24 stsp
7615 0ebf8283 2019-07-24 stsp const struct got_error *
7616 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7617 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7618 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7619 0ebf8283 2019-07-24 stsp {
7620 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7621 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7622 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7623 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7624 0ebf8283 2019-07-24 stsp
7625 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7626 0ebf8283 2019-07-24 stsp if (err)
7627 0ebf8283 2019-07-24 stsp return err;
7628 0ebf8283 2019-07-24 stsp
7629 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7630 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7631 e600f124 2021-03-21 stsp if (err)
7632 e600f124 2021-03-21 stsp goto done;
7633 e600f124 2021-03-21 stsp
7634 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7635 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7636 0ebf8283 2019-07-24 stsp if (err)
7637 0ebf8283 2019-07-24 stsp goto done;
7638 0ebf8283 2019-07-24 stsp
7639 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7640 0ebf8283 2019-07-24 stsp if (err)
7641 0ebf8283 2019-07-24 stsp goto done;
7642 0ebf8283 2019-07-24 stsp
7643 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7644 0ebf8283 2019-07-24 stsp if (err)
7645 0ebf8283 2019-07-24 stsp goto done;
7646 0ebf8283 2019-07-24 stsp
7647 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7648 0ebf8283 2019-07-24 stsp if (err)
7649 0ebf8283 2019-07-24 stsp goto done;
7650 0ebf8283 2019-07-24 stsp
7651 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7652 a615e0e7 2020-12-16 stsp if (err)
7653 a615e0e7 2020-12-16 stsp goto done;
7654 a615e0e7 2020-12-16 stsp
7655 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7656 a615e0e7 2020-12-16 stsp if (err)
7657 a615e0e7 2020-12-16 stsp goto done;
7658 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7659 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7660 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7661 a615e0e7 2020-12-16 stsp err = sync_err;
7662 0ebf8283 2019-07-24 stsp done:
7663 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7664 a615e0e7 2020-12-16 stsp free(fileindex_path);
7665 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7666 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7667 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7668 0ebf8283 2019-07-24 stsp err = unlockerr;
7669 0ebf8283 2019-07-24 stsp return err;
7670 0ebf8283 2019-07-24 stsp }
7671 0ebf8283 2019-07-24 stsp
7672 0ebf8283 2019-07-24 stsp const struct got_error *
7673 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7674 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7675 0ebf8283 2019-07-24 stsp {
7676 0ebf8283 2019-07-24 stsp const struct got_error *err;
7677 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7678 0ebf8283 2019-07-24 stsp
7679 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7680 0ebf8283 2019-07-24 stsp if (err)
7681 0ebf8283 2019-07-24 stsp return err;
7682 0ebf8283 2019-07-24 stsp
7683 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7684 0ebf8283 2019-07-24 stsp if (err)
7685 0ebf8283 2019-07-24 stsp goto done;
7686 0ebf8283 2019-07-24 stsp
7687 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7688 0ebf8283 2019-07-24 stsp done:
7689 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7690 2822a352 2019-10-15 stsp return err;
7691 2822a352 2019-10-15 stsp }
7692 2822a352 2019-10-15 stsp
7693 2822a352 2019-10-15 stsp const struct got_error *
7694 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7695 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7696 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7697 2822a352 2019-10-15 stsp struct got_repository *repo)
7698 2822a352 2019-10-15 stsp {
7699 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7700 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7701 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7702 2822a352 2019-10-15 stsp
7703 2822a352 2019-10-15 stsp *fileindex = NULL;
7704 2822a352 2019-10-15 stsp *branch_ref = NULL;
7705 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7706 2822a352 2019-10-15 stsp
7707 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7708 2822a352 2019-10-15 stsp if (err)
7709 2822a352 2019-10-15 stsp return err;
7710 2822a352 2019-10-15 stsp
7711 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7712 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7713 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7714 2822a352 2019-10-15 stsp "update -b or different branch name required");
7715 2822a352 2019-10-15 stsp goto done;
7716 2822a352 2019-10-15 stsp }
7717 2822a352 2019-10-15 stsp
7718 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7719 2822a352 2019-10-15 stsp if (err)
7720 2822a352 2019-10-15 stsp goto done;
7721 2822a352 2019-10-15 stsp
7722 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7723 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7724 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7725 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7726 2822a352 2019-10-15 stsp &ok_arg);
7727 2822a352 2019-10-15 stsp if (err)
7728 2822a352 2019-10-15 stsp goto done;
7729 2822a352 2019-10-15 stsp
7730 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7731 2822a352 2019-10-15 stsp if (err)
7732 2822a352 2019-10-15 stsp goto done;
7733 2822a352 2019-10-15 stsp
7734 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7735 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7736 2822a352 2019-10-15 stsp done:
7737 2822a352 2019-10-15 stsp if (err) {
7738 2822a352 2019-10-15 stsp if (*branch_ref) {
7739 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7740 2822a352 2019-10-15 stsp *branch_ref = NULL;
7741 2822a352 2019-10-15 stsp }
7742 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7743 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7744 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7745 2822a352 2019-10-15 stsp }
7746 2822a352 2019-10-15 stsp if (*fileindex) {
7747 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7748 2822a352 2019-10-15 stsp *fileindex = NULL;
7749 2822a352 2019-10-15 stsp }
7750 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7751 2822a352 2019-10-15 stsp }
7752 0cb83759 2019-08-03 stsp return err;
7753 0cb83759 2019-08-03 stsp }
7754 0cb83759 2019-08-03 stsp
7755 2822a352 2019-10-15 stsp const struct got_error *
7756 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7757 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7758 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7759 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7760 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7761 2822a352 2019-10-15 stsp {
7762 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7763 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7764 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7765 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7766 2822a352 2019-10-15 stsp
7767 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7768 2822a352 2019-10-15 stsp if (err)
7769 2822a352 2019-10-15 stsp goto done;
7770 2822a352 2019-10-15 stsp
7771 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7772 2822a352 2019-10-15 stsp if (err)
7773 2822a352 2019-10-15 stsp goto done;
7774 2822a352 2019-10-15 stsp
7775 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7776 a44927cc 2022-04-07 stsp if (err)
7777 a44927cc 2022-04-07 stsp goto done;
7778 a44927cc 2022-04-07 stsp
7779 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7780 2822a352 2019-10-15 stsp worktree->path_prefix);
7781 2822a352 2019-10-15 stsp if (err)
7782 2822a352 2019-10-15 stsp goto done;
7783 2822a352 2019-10-15 stsp
7784 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7785 2822a352 2019-10-15 stsp if (err)
7786 2822a352 2019-10-15 stsp goto done;
7787 2822a352 2019-10-15 stsp
7788 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7789 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7790 2822a352 2019-10-15 stsp if (err)
7791 2822a352 2019-10-15 stsp goto sync;
7792 2822a352 2019-10-15 stsp
7793 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7794 2822a352 2019-10-15 stsp if (err)
7795 2822a352 2019-10-15 stsp goto sync;
7796 2822a352 2019-10-15 stsp
7797 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7798 6e210706 2021-01-22 stsp if (err)
7799 6e210706 2021-01-22 stsp goto sync;
7800 6e210706 2021-01-22 stsp
7801 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7802 2822a352 2019-10-15 stsp sync:
7803 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7804 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7805 2822a352 2019-10-15 stsp err = sync_err;
7806 2822a352 2019-10-15 stsp
7807 2822a352 2019-10-15 stsp done:
7808 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7809 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7810 2822a352 2019-10-15 stsp err = unlockerr;
7811 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7812 2822a352 2019-10-15 stsp
7813 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7814 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7815 2822a352 2019-10-15 stsp err = unlockerr;
7816 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7817 2822a352 2019-10-15 stsp
7818 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7819 2822a352 2019-10-15 stsp free(fileindex_path);
7820 2822a352 2019-10-15 stsp free(tree_id);
7821 a44927cc 2022-04-07 stsp if (commit)
7822 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7823 2822a352 2019-10-15 stsp
7824 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7825 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7826 2822a352 2019-10-15 stsp err = unlockerr;
7827 2822a352 2019-10-15 stsp return err;
7828 2822a352 2019-10-15 stsp }
7829 2822a352 2019-10-15 stsp
7830 2822a352 2019-10-15 stsp const struct got_error *
7831 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7832 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7833 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7834 2822a352 2019-10-15 stsp {
7835 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7836 8b692cd0 2019-10-21 stsp
7837 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7838 8b692cd0 2019-10-21 stsp
7839 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7840 8b692cd0 2019-10-21 stsp
7841 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7842 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7843 8b692cd0 2019-10-21 stsp err = unlockerr;
7844 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7845 8b692cd0 2019-10-21 stsp
7846 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7847 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7848 8b692cd0 2019-10-21 stsp err = unlockerr;
7849 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7850 f259c4c1 2021-09-24 stsp
7851 f259c4c1 2021-09-24 stsp return err;
7852 f259c4c1 2021-09-24 stsp }
7853 f259c4c1 2021-09-24 stsp
7854 f259c4c1 2021-09-24 stsp const struct got_error *
7855 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7856 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7857 f259c4c1 2021-09-24 stsp {
7858 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7859 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7860 f259c4c1 2021-09-24 stsp
7861 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7862 f259c4c1 2021-09-24 stsp if (err)
7863 f259c4c1 2021-09-24 stsp goto done;
7864 8b692cd0 2019-10-21 stsp
7865 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7866 f259c4c1 2021-09-24 stsp
7867 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7868 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7869 f259c4c1 2021-09-24 stsp err = sync_err;
7870 f259c4c1 2021-09-24 stsp done:
7871 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7872 f259c4c1 2021-09-24 stsp free(fileindex_path);
7873 8b692cd0 2019-10-21 stsp return err;
7874 2822a352 2019-10-15 stsp }
7875 2822a352 2019-10-15 stsp
7876 f259c4c1 2021-09-24 stsp static const struct got_error *
7877 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7878 f259c4c1 2021-09-24 stsp {
7879 f259c4c1 2021-09-24 stsp const struct got_error *err;
7880 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7881 f259c4c1 2021-09-24 stsp
7882 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7883 f259c4c1 2021-09-24 stsp if (err)
7884 f259c4c1 2021-09-24 stsp goto done;
7885 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7886 f259c4c1 2021-09-24 stsp if (err)
7887 f259c4c1 2021-09-24 stsp goto done;
7888 f259c4c1 2021-09-24 stsp
7889 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7890 f259c4c1 2021-09-24 stsp if (err)
7891 f259c4c1 2021-09-24 stsp goto done;
7892 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7893 f259c4c1 2021-09-24 stsp if (err)
7894 f259c4c1 2021-09-24 stsp goto done;
7895 f259c4c1 2021-09-24 stsp
7896 f259c4c1 2021-09-24 stsp done:
7897 f259c4c1 2021-09-24 stsp free(branch_refname);
7898 f259c4c1 2021-09-24 stsp free(commit_refname);
7899 f259c4c1 2021-09-24 stsp return err;
7900 f259c4c1 2021-09-24 stsp }
7901 f259c4c1 2021-09-24 stsp
7902 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7903 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7904 f259c4c1 2021-09-24 stsp const char *branch_name;
7905 f259c4c1 2021-09-24 stsp };
7906 f259c4c1 2021-09-24 stsp
7907 f259c4c1 2021-09-24 stsp static const struct got_error *
7908 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7909 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7910 f259c4c1 2021-09-24 stsp {
7911 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7912 f259c4c1 2021-09-24 stsp
7913 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7914 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7915 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7916 f259c4c1 2021-09-24 stsp
7917 f259c4c1 2021-09-24 stsp return NULL;
7918 f259c4c1 2021-09-24 stsp }
7919 f259c4c1 2021-09-24 stsp
7920 f259c4c1 2021-09-24 stsp
7921 f259c4c1 2021-09-24 stsp const struct got_error *
7922 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7923 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7924 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7925 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7926 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7927 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7928 f259c4c1 2021-09-24 stsp {
7929 f259c4c1 2021-09-24 stsp const struct got_error *err;
7930 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7931 f259c4c1 2021-09-24 stsp
7932 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7933 f259c4c1 2021-09-24 stsp if (err)
7934 f259c4c1 2021-09-24 stsp goto done;
7935 f259c4c1 2021-09-24 stsp
7936 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7937 f259c4c1 2021-09-24 stsp worktree);
7938 f259c4c1 2021-09-24 stsp if (err)
7939 f259c4c1 2021-09-24 stsp goto done;
7940 f259c4c1 2021-09-24 stsp
7941 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7942 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7943 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7944 f259c4c1 2021-09-24 stsp done:
7945 f259c4c1 2021-09-24 stsp free(fileindex_path);
7946 f259c4c1 2021-09-24 stsp return err;
7947 f259c4c1 2021-09-24 stsp }
7948 f259c4c1 2021-09-24 stsp
7949 f259c4c1 2021-09-24 stsp const struct got_error *
7950 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7951 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7952 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7953 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7954 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
7955 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7956 0ff8d236 2021-09-28 stsp
7957 f259c4c1 2021-09-24 stsp {
7958 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7959 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7960 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7961 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7962 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7963 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7964 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7965 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7966 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7967 f259c4c1 2021-09-24 stsp
7968 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7969 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7970 f259c4c1 2021-09-24 stsp
7971 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7972 f259c4c1 2021-09-24 stsp
7973 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7974 f259c4c1 2021-09-24 stsp if (err)
7975 f259c4c1 2021-09-24 stsp goto done;
7976 f259c4c1 2021-09-24 stsp
7977 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7978 f259c4c1 2021-09-24 stsp if (err)
7979 f259c4c1 2021-09-24 stsp goto done;
7980 f259c4c1 2021-09-24 stsp
7981 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7982 f259c4c1 2021-09-24 stsp if (err)
7983 f259c4c1 2021-09-24 stsp goto done;
7984 f259c4c1 2021-09-24 stsp
7985 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7986 f259c4c1 2021-09-24 stsp &have_staged_files);
7987 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7988 f259c4c1 2021-09-24 stsp goto done;
7989 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7990 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7991 f259c4c1 2021-09-24 stsp goto done;
7992 f259c4c1 2021-09-24 stsp }
7993 f259c4c1 2021-09-24 stsp
7994 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7995 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7996 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7997 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7998 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7999 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8000 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8001 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8002 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8003 f259c4c1 2021-09-24 stsp if (err)
8004 f259c4c1 2021-09-24 stsp goto done;
8005 f259c4c1 2021-09-24 stsp
8006 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
8007 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8008 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
8009 f259c4c1 2021-09-24 stsp goto done;
8010 f259c4c1 2021-09-24 stsp }
8011 f259c4c1 2021-09-24 stsp
8012 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8013 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8014 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
8015 f259c4c1 2021-09-24 stsp
8016 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
8017 f259c4c1 2021-09-24 stsp ct_path++;
8018 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
8019 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8020 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8021 f259c4c1 2021-09-24 stsp if (err)
8022 f259c4c1 2021-09-24 stsp goto done;
8023 f259c4c1 2021-09-24 stsp
8024 f259c4c1 2021-09-24 stsp }
8025 f259c4c1 2021-09-24 stsp
8026 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8027 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8028 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8029 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8030 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8031 f259c4c1 2021-09-24 stsp if (err)
8032 f259c4c1 2021-09-24 stsp goto done;
8033 f259c4c1 2021-09-24 stsp
8034 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8035 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8036 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8037 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8038 f259c4c1 2021-09-24 stsp err = sync_err;
8039 f259c4c1 2021-09-24 stsp done:
8040 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8041 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8042 d8bacb93 2023-01-10 mark
8043 f259c4c1 2021-09-24 stsp free_commitable(ct);
8044 f259c4c1 2021-09-24 stsp }
8045 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8046 f259c4c1 2021-09-24 stsp free(fileindex_path);
8047 f259c4c1 2021-09-24 stsp return err;
8048 f259c4c1 2021-09-24 stsp }
8049 f259c4c1 2021-09-24 stsp
8050 f259c4c1 2021-09-24 stsp const struct got_error *
8051 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8052 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8053 f259c4c1 2021-09-24 stsp {
8054 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8055 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8056 f259c4c1 2021-09-24 stsp
8057 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8058 f259c4c1 2021-09-24 stsp if (err)
8059 f259c4c1 2021-09-24 stsp goto done;
8060 f259c4c1 2021-09-24 stsp
8061 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8062 f259c4c1 2021-09-24 stsp if (err)
8063 f259c4c1 2021-09-24 stsp goto done;
8064 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8065 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8066 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8067 f259c4c1 2021-09-24 stsp err = sync_err;
8068 f259c4c1 2021-09-24 stsp done:
8069 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8070 f259c4c1 2021-09-24 stsp free(fileindex_path);
8071 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8072 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8073 f259c4c1 2021-09-24 stsp err = unlockerr;
8074 f259c4c1 2021-09-24 stsp return err;
8075 f259c4c1 2021-09-24 stsp }
8076 f259c4c1 2021-09-24 stsp
8077 f259c4c1 2021-09-24 stsp const struct got_error *
8078 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8079 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8080 f259c4c1 2021-09-24 stsp {
8081 f259c4c1 2021-09-24 stsp const struct got_error *err;
8082 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8083 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8084 f259c4c1 2021-09-24 stsp
8085 f259c4c1 2021-09-24 stsp *in_progress = 0;
8086 f259c4c1 2021-09-24 stsp
8087 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8088 f259c4c1 2021-09-24 stsp if (err)
8089 f259c4c1 2021-09-24 stsp return err;
8090 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8091 793fcac3 2021-09-24 stsp free(branch_refname);
8092 f259c4c1 2021-09-24 stsp if (err) {
8093 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8094 f259c4c1 2021-09-24 stsp return err;
8095 f259c4c1 2021-09-24 stsp } else
8096 f259c4c1 2021-09-24 stsp *in_progress = 1;
8097 f259c4c1 2021-09-24 stsp
8098 f259c4c1 2021-09-24 stsp return NULL;
8099 f259c4c1 2021-09-24 stsp }
8100 f259c4c1 2021-09-24 stsp
8101 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8102 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8103 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8104 f259c4c1 2021-09-24 stsp {
8105 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8106 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8107 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8108 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8109 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8110 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8111 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8112 f259c4c1 2021-09-24 stsp
8113 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8114 f259c4c1 2021-09-24 stsp
8115 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8116 f259c4c1 2021-09-24 stsp if (err)
8117 f259c4c1 2021-09-24 stsp return err;
8118 f259c4c1 2021-09-24 stsp
8119 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8120 f259c4c1 2021-09-24 stsp if (err)
8121 f259c4c1 2021-09-24 stsp goto done;
8122 f259c4c1 2021-09-24 stsp
8123 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8124 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8125 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8126 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8127 f259c4c1 2021-09-24 stsp &ok_arg);
8128 f259c4c1 2021-09-24 stsp if (err)
8129 f259c4c1 2021-09-24 stsp goto done;
8130 f259c4c1 2021-09-24 stsp
8131 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8132 f259c4c1 2021-09-24 stsp if (err)
8133 f259c4c1 2021-09-24 stsp return err;
8134 f259c4c1 2021-09-24 stsp
8135 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8136 f259c4c1 2021-09-24 stsp if (err)
8137 f259c4c1 2021-09-24 stsp return err;
8138 f259c4c1 2021-09-24 stsp
8139 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8140 f259c4c1 2021-09-24 stsp 0);
8141 f259c4c1 2021-09-24 stsp if (err)
8142 f259c4c1 2021-09-24 stsp goto done;
8143 f259c4c1 2021-09-24 stsp
8144 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8145 f259c4c1 2021-09-24 stsp if (err)
8146 f259c4c1 2021-09-24 stsp goto done;
8147 f259c4c1 2021-09-24 stsp
8148 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8149 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8150 f259c4c1 2021-09-24 stsp goto done;
8151 f259c4c1 2021-09-24 stsp }
8152 f259c4c1 2021-09-24 stsp
8153 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8154 f259c4c1 2021-09-24 stsp if (err)
8155 f259c4c1 2021-09-24 stsp goto done;
8156 f259c4c1 2021-09-24 stsp
8157 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8158 f259c4c1 2021-09-24 stsp if (err)
8159 f259c4c1 2021-09-24 stsp goto done;
8160 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8161 f259c4c1 2021-09-24 stsp if (err)
8162 f259c4c1 2021-09-24 stsp goto done;
8163 f259c4c1 2021-09-24 stsp
8164 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8165 f259c4c1 2021-09-24 stsp if (err)
8166 f259c4c1 2021-09-24 stsp goto done;
8167 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8168 f259c4c1 2021-09-24 stsp if (err)
8169 f259c4c1 2021-09-24 stsp goto done;
8170 f259c4c1 2021-09-24 stsp
8171 f259c4c1 2021-09-24 stsp done:
8172 f259c4c1 2021-09-24 stsp free(branch_refname);
8173 f259c4c1 2021-09-24 stsp free(commit_refname);
8174 f259c4c1 2021-09-24 stsp free(fileindex_path);
8175 f259c4c1 2021-09-24 stsp if (branch_ref)
8176 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8177 f259c4c1 2021-09-24 stsp if (commit_ref)
8178 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8179 f259c4c1 2021-09-24 stsp if (wt_branch)
8180 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8181 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8182 f259c4c1 2021-09-24 stsp if (err) {
8183 f259c4c1 2021-09-24 stsp if (*fileindex) {
8184 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8185 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8186 f259c4c1 2021-09-24 stsp }
8187 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8188 f259c4c1 2021-09-24 stsp }
8189 f259c4c1 2021-09-24 stsp return err;
8190 f259c4c1 2021-09-24 stsp }
8191 f259c4c1 2021-09-24 stsp
8192 f259c4c1 2021-09-24 stsp const struct got_error *
8193 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8194 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8195 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8196 f259c4c1 2021-09-24 stsp {
8197 f259c4c1 2021-09-24 stsp const struct got_error *err;
8198 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8199 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8200 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8201 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8202 f259c4c1 2021-09-24 stsp
8203 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8204 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8205 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8206 f259c4c1 2021-09-24 stsp
8207 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8208 f259c4c1 2021-09-24 stsp if (err)
8209 f259c4c1 2021-09-24 stsp return err;
8210 f259c4c1 2021-09-24 stsp
8211 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8212 f259c4c1 2021-09-24 stsp if (err)
8213 f259c4c1 2021-09-24 stsp goto done;
8214 f259c4c1 2021-09-24 stsp
8215 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8216 f259c4c1 2021-09-24 stsp &have_staged_files);
8217 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8218 f259c4c1 2021-09-24 stsp goto done;
8219 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8220 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8221 f259c4c1 2021-09-24 stsp goto done;
8222 f259c4c1 2021-09-24 stsp }
8223 f259c4c1 2021-09-24 stsp
8224 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8225 f259c4c1 2021-09-24 stsp if (err)
8226 f259c4c1 2021-09-24 stsp goto done;
8227 f259c4c1 2021-09-24 stsp
8228 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8229 f259c4c1 2021-09-24 stsp if (err)
8230 f259c4c1 2021-09-24 stsp goto done;
8231 f259c4c1 2021-09-24 stsp
8232 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8233 f259c4c1 2021-09-24 stsp if (err)
8234 f259c4c1 2021-09-24 stsp goto done;
8235 f259c4c1 2021-09-24 stsp
8236 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8237 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8238 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8239 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8240 f259c4c1 2021-09-24 stsp goto done;
8241 f259c4c1 2021-09-24 stsp }
8242 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8243 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8244 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8245 f259c4c1 2021-09-24 stsp goto done;
8246 f259c4c1 2021-09-24 stsp }
8247 f259c4c1 2021-09-24 stsp
8248 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8249 f259c4c1 2021-09-24 stsp if (err)
8250 f259c4c1 2021-09-24 stsp goto done;
8251 f259c4c1 2021-09-24 stsp
8252 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8253 f259c4c1 2021-09-24 stsp if (err)
8254 f259c4c1 2021-09-24 stsp goto done;
8255 f259c4c1 2021-09-24 stsp done:
8256 f259c4c1 2021-09-24 stsp free(commit_refname);
8257 f259c4c1 2021-09-24 stsp free(branch_refname);
8258 f259c4c1 2021-09-24 stsp free(fileindex_path);
8259 f259c4c1 2021-09-24 stsp if (commit_ref)
8260 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8261 f259c4c1 2021-09-24 stsp if (branch_ref)
8262 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8263 f259c4c1 2021-09-24 stsp if (err) {
8264 f259c4c1 2021-09-24 stsp if (*branch_name) {
8265 f259c4c1 2021-09-24 stsp free(*branch_name);
8266 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8267 f259c4c1 2021-09-24 stsp }
8268 f259c4c1 2021-09-24 stsp free(*branch_tip);
8269 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8270 f259c4c1 2021-09-24 stsp if (*fileindex) {
8271 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8272 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8273 f259c4c1 2021-09-24 stsp }
8274 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8275 f259c4c1 2021-09-24 stsp }
8276 f259c4c1 2021-09-24 stsp return err;
8277 f259c4c1 2021-09-24 stsp }
8278 f259c4c1 2021-09-24 stsp
8279 f259c4c1 2021-09-24 stsp const struct got_error *
8280 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8281 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8282 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8283 f259c4c1 2021-09-24 stsp {
8284 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8285 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8286 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8287 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8288 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8289 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8290 f259c4c1 2021-09-24 stsp
8291 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8292 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8293 a44927cc 2022-04-07 stsp if (err)
8294 a44927cc 2022-04-07 stsp goto done;
8295 a44927cc 2022-04-07 stsp
8296 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8297 a44927cc 2022-04-07 stsp worktree->path_prefix);
8298 f259c4c1 2021-09-24 stsp if (err)
8299 f259c4c1 2021-09-24 stsp goto done;
8300 f259c4c1 2021-09-24 stsp
8301 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8302 f259c4c1 2021-09-24 stsp if (err)
8303 f259c4c1 2021-09-24 stsp goto done;
8304 f259c4c1 2021-09-24 stsp
8305 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8306 f259c4c1 2021-09-24 stsp if (err)
8307 f259c4c1 2021-09-24 stsp goto done;
8308 f259c4c1 2021-09-24 stsp
8309 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8310 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8311 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8312 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8313 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8314 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8315 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8316 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8317 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8318 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8319 f259c4c1 2021-09-24 stsp if (err)
8320 f259c4c1 2021-09-24 stsp goto sync;
8321 f259c4c1 2021-09-24 stsp
8322 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8323 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8324 f259c4c1 2021-09-24 stsp sync:
8325 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8326 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8327 f259c4c1 2021-09-24 stsp err = sync_err;
8328 f259c4c1 2021-09-24 stsp done:
8329 f259c4c1 2021-09-24 stsp free(tree_id);
8330 f259c4c1 2021-09-24 stsp free(commit_id);
8331 a44927cc 2022-04-07 stsp if (commit)
8332 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8333 f259c4c1 2021-09-24 stsp if (fileindex)
8334 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8335 f259c4c1 2021-09-24 stsp free(fileindex_path);
8336 f259c4c1 2021-09-24 stsp
8337 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8338 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8339 f259c4c1 2021-09-24 stsp err = unlockerr;
8340 f259c4c1 2021-09-24 stsp return err;
8341 f259c4c1 2021-09-24 stsp }
8342 f259c4c1 2021-09-24 stsp
8343 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8344 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8345 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8346 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8347 2db2652d 2019-08-07 stsp struct got_repository *repo;
8348 2db2652d 2019-08-07 stsp int have_changes;
8349 2db2652d 2019-08-07 stsp };
8350 2db2652d 2019-08-07 stsp
8351 336075a4 2022-06-25 op static const struct got_error *
8352 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8353 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8354 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8355 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8356 735ef5ac 2019-08-03 stsp {
8357 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8358 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8359 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8360 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8361 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8362 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8363 8b13ce36 2019-08-08 stsp
8364 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8365 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8366 8b13ce36 2019-08-08 stsp return NULL;
8367 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8368 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8369 735ef5ac 2019-08-03 stsp
8370 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8371 735ef5ac 2019-08-03 stsp if (ie == NULL)
8372 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8373 735ef5ac 2019-08-03 stsp
8374 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8375 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8376 735ef5ac 2019-08-03 stsp relpath) == -1)
8377 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8378 735ef5ac 2019-08-03 stsp
8379 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8380 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8381 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8382 735ef5ac 2019-08-03 stsp }
8383 735ef5ac 2019-08-03 stsp
8384 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8385 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8386 3aa5969e 2019-08-06 stsp goto done;
8387 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8388 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8389 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8390 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8391 735ef5ac 2019-08-03 stsp goto done;
8392 3aa5969e 2019-08-06 stsp }
8393 735ef5ac 2019-08-03 stsp
8394 2db2652d 2019-08-07 stsp a->have_changes = 1;
8395 2db2652d 2019-08-07 stsp
8396 735ef5ac 2019-08-03 stsp p = in_repo_path;
8397 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8398 735ef5ac 2019-08-03 stsp p++;
8399 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8400 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8401 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8402 735ef5ac 2019-08-03 stsp done:
8403 735ef5ac 2019-08-03 stsp free(in_repo_path);
8404 dc424a06 2019-08-07 stsp return err;
8405 dc424a06 2019-08-07 stsp }
8406 dc424a06 2019-08-07 stsp
8407 2db2652d 2019-08-07 stsp struct stage_path_arg {
8408 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8409 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8410 2db2652d 2019-08-07 stsp struct got_repository *repo;
8411 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8412 2db2652d 2019-08-07 stsp void *status_arg;
8413 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8414 2db2652d 2019-08-07 stsp void *patch_arg;
8415 7b5dc508 2019-10-28 stsp int staged_something;
8416 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8417 2db2652d 2019-08-07 stsp };
8418 2db2652d 2019-08-07 stsp
8419 2db2652d 2019-08-07 stsp static const struct got_error *
8420 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8421 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8422 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8423 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8424 0cb83759 2019-08-03 stsp {
8425 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8426 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8427 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8428 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8429 0cb83759 2019-08-03 stsp uint32_t stage;
8430 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8431 0aeb8099 2020-07-23 stsp struct stat sb;
8432 8b13ce36 2019-08-08 stsp
8433 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8434 8b13ce36 2019-08-08 stsp return NULL;
8435 0cb83759 2019-08-03 stsp
8436 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8437 d3e7c587 2019-08-03 stsp if (ie == NULL)
8438 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8439 0cb83759 2019-08-03 stsp
8440 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8441 2db2652d 2019-08-07 stsp relpath)== -1)
8442 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8443 0cb83759 2019-08-03 stsp
8444 0cb83759 2019-08-03 stsp switch (status) {
8445 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8446 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8447 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8448 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8449 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8450 0aeb8099 2020-07-23 stsp break;
8451 0aeb8099 2020-07-23 stsp }
8452 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8453 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8454 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8455 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8456 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8457 dc424a06 2019-08-07 stsp if (err)
8458 dc424a06 2019-08-07 stsp break;
8459 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8460 dc424a06 2019-08-07 stsp break;
8461 dc424a06 2019-08-07 stsp } else {
8462 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8463 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8464 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8465 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8466 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8467 dc424a06 2019-08-07 stsp break;
8468 dc424a06 2019-08-07 stsp }
8469 dc424a06 2019-08-07 stsp }
8470 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8471 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8472 0cb83759 2019-08-03 stsp if (err)
8473 dc424a06 2019-08-07 stsp break;
8474 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8475 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8476 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8477 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8478 0cb83759 2019-08-03 stsp else
8479 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8480 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8481 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8482 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8483 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8484 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8485 35213c7c 2020-07-23 stsp ssize_t target_len;
8486 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8487 35213c7c 2020-07-23 stsp sizeof(target_path));
8488 35213c7c 2020-07-23 stsp if (target_len == -1) {
8489 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8490 35213c7c 2020-07-23 stsp ondisk_path);
8491 35213c7c 2020-07-23 stsp break;
8492 35213c7c 2020-07-23 stsp }
8493 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8494 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8495 35213c7c 2020-07-23 stsp a->worktree->root_path);
8496 35213c7c 2020-07-23 stsp if (err)
8497 35213c7c 2020-07-23 stsp break;
8498 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8499 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8500 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8501 35213c7c 2020-07-23 stsp break;
8502 35213c7c 2020-07-23 stsp }
8503 35213c7c 2020-07-23 stsp }
8504 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8505 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8506 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8507 35213c7c 2020-07-23 stsp else
8508 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8509 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8510 0aeb8099 2020-07-23 stsp } else {
8511 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8512 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8513 0aeb8099 2020-07-23 stsp }
8514 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8515 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8516 dc424a06 2019-08-07 stsp break;
8517 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8518 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8519 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8520 9fdde394 2022-06-04 op if (err)
8521 9fdde394 2022-06-04 op break;
8522 9fdde394 2022-06-04 op /*
8523 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8524 9fdde394 2022-06-04 op * implicitly unstage the file.
8525 9fdde394 2022-06-04 op */
8526 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8527 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8528 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8529 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8530 9fdde394 2022-06-04 op }
8531 0cb83759 2019-08-03 stsp break;
8532 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8533 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8534 d3e7c587 2019-08-03 stsp break;
8535 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8536 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8537 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8538 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8539 dc424a06 2019-08-07 stsp if (err)
8540 dc424a06 2019-08-07 stsp break;
8541 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8542 88f33a19 2019-08-08 stsp break;
8543 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8544 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8545 dc424a06 2019-08-07 stsp break;
8546 88f33a19 2019-08-08 stsp }
8547 dc424a06 2019-08-07 stsp }
8548 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8549 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8550 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8551 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8552 dc424a06 2019-08-07 stsp break;
8553 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8554 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8555 12463d8b 2019-12-13 stsp de_name);
8556 0cb83759 2019-08-03 stsp break;
8557 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8558 d3e7c587 2019-08-03 stsp break;
8559 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8560 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8561 ebf48fd5 2019-08-03 stsp break;
8562 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8563 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8564 2a06fe5f 2019-08-24 stsp break;
8565 0cb83759 2019-08-03 stsp default:
8566 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8567 537ac44b 2019-08-03 stsp break;
8568 0cb83759 2019-08-03 stsp }
8569 dc424a06 2019-08-07 stsp
8570 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8571 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8572 dc424a06 2019-08-07 stsp free(path_content);
8573 2db2652d 2019-08-07 stsp free(ondisk_path);
8574 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8575 0ebf8283 2019-07-24 stsp return err;
8576 0ebf8283 2019-07-24 stsp }
8577 0cb83759 2019-08-03 stsp
8578 0cb83759 2019-08-03 stsp const struct got_error *
8579 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8580 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8581 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8582 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8583 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8584 0cb83759 2019-08-03 stsp {
8585 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8586 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8587 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8588 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8589 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8590 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8591 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8592 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8593 0cb83759 2019-08-03 stsp
8594 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8595 0cb83759 2019-08-03 stsp if (err)
8596 0cb83759 2019-08-03 stsp return err;
8597 0cb83759 2019-08-03 stsp
8598 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8599 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8600 735ef5ac 2019-08-03 stsp if (err)
8601 735ef5ac 2019-08-03 stsp goto done;
8602 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8603 735ef5ac 2019-08-03 stsp if (err)
8604 735ef5ac 2019-08-03 stsp goto done;
8605 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8606 0cb83759 2019-08-03 stsp if (err)
8607 0cb83759 2019-08-03 stsp goto done;
8608 0cb83759 2019-08-03 stsp
8609 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8610 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8611 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8612 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8613 2db2652d 2019-08-07 stsp oka.repo = repo;
8614 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8615 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8616 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8617 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8618 735ef5ac 2019-08-03 stsp if (err)
8619 735ef5ac 2019-08-03 stsp goto done;
8620 735ef5ac 2019-08-03 stsp }
8621 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8622 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8623 2db2652d 2019-08-07 stsp goto done;
8624 2db2652d 2019-08-07 stsp }
8625 735ef5ac 2019-08-03 stsp
8626 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8627 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8628 2db2652d 2019-08-07 stsp spa.repo = repo;
8629 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8630 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8631 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8632 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8633 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8634 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8635 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8636 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8637 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8638 42005733 2019-08-03 stsp if (err)
8639 2db2652d 2019-08-07 stsp goto done;
8640 7b5dc508 2019-10-28 stsp }
8641 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8642 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8643 7b5dc508 2019-10-28 stsp goto done;
8644 0cb83759 2019-08-03 stsp }
8645 0cb83759 2019-08-03 stsp
8646 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8647 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8648 0cb83759 2019-08-03 stsp err = sync_err;
8649 0cb83759 2019-08-03 stsp done:
8650 735ef5ac 2019-08-03 stsp if (head_ref)
8651 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8652 735ef5ac 2019-08-03 stsp free(head_commit_id);
8653 0cb83759 2019-08-03 stsp free(fileindex_path);
8654 0cb83759 2019-08-03 stsp if (fileindex)
8655 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8656 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8657 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8658 0cb83759 2019-08-03 stsp err = unlockerr;
8659 0cb83759 2019-08-03 stsp return err;
8660 0cb83759 2019-08-03 stsp }
8661 ad493afc 2019-08-03 stsp
8662 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8663 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8664 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8665 ad493afc 2019-08-03 stsp struct got_repository *repo;
8666 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8667 ad493afc 2019-08-03 stsp void *progress_arg;
8668 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8669 2e1f37b0 2019-08-08 stsp void *patch_arg;
8670 ad493afc 2019-08-03 stsp };
8671 2e1f37b0 2019-08-08 stsp
8672 2e1f37b0 2019-08-08 stsp static const struct got_error *
8673 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8674 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8675 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8676 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8677 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8678 2e1f37b0 2019-08-08 stsp {
8679 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8680 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8681 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8682 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8683 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8684 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8685 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8686 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8687 2e1f37b0 2019-08-08 stsp
8688 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8689 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8690 2e1f37b0 2019-08-08 stsp
8691 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8692 2e1f37b0 2019-08-08 stsp if (err)
8693 2e1f37b0 2019-08-08 stsp return err;
8694 eb81bc23 2022-06-28 tracey
8695 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8696 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8697 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8698 eb81bc23 2022-06-28 tracey goto done;
8699 eb81bc23 2022-06-28 tracey }
8700 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8701 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8702 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8703 eb81bc23 2022-06-28 tracey goto done;
8704 eb81bc23 2022-06-28 tracey }
8705 eb81bc23 2022-06-28 tracey
8706 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8707 2e1f37b0 2019-08-08 stsp if (err)
8708 2e1f37b0 2019-08-08 stsp goto done;
8709 2e1f37b0 2019-08-08 stsp
8710 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8711 2e1f37b0 2019-08-08 stsp if (err)
8712 2e1f37b0 2019-08-08 stsp goto done;
8713 2e1f37b0 2019-08-08 stsp
8714 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8715 2e1f37b0 2019-08-08 stsp if (err)
8716 2e1f37b0 2019-08-08 stsp goto done;
8717 2e1f37b0 2019-08-08 stsp
8718 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8719 eb81bc23 2022-06-28 tracey fd2);
8720 2e1f37b0 2019-08-08 stsp if (err)
8721 2e1f37b0 2019-08-08 stsp goto done;
8722 2e1f37b0 2019-08-08 stsp
8723 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8724 2e1f37b0 2019-08-08 stsp if (err)
8725 2e1f37b0 2019-08-08 stsp goto done;
8726 ad493afc 2019-08-03 stsp
8727 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8728 2e1f37b0 2019-08-08 stsp if (err)
8729 2e1f37b0 2019-08-08 stsp goto done;
8730 2e1f37b0 2019-08-08 stsp
8731 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8732 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8733 2e1f37b0 2019-08-08 stsp if (err)
8734 2e1f37b0 2019-08-08 stsp goto done;
8735 2e1f37b0 2019-08-08 stsp
8736 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8737 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8738 2e1f37b0 2019-08-08 stsp if (err)
8739 2e1f37b0 2019-08-08 stsp goto done;
8740 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8741 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8742 2e1f37b0 2019-08-08 stsp if (err)
8743 2e1f37b0 2019-08-08 stsp goto done;
8744 2e1f37b0 2019-08-08 stsp
8745 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8746 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8747 2e1f37b0 2019-08-08 stsp goto done;
8748 2e1f37b0 2019-08-08 stsp }
8749 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8750 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8751 2e1f37b0 2019-08-08 stsp goto done;
8752 2e1f37b0 2019-08-08 stsp }
8753 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8754 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8755 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8756 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8757 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8758 fe621944 2020-11-10 stsp nchanges++;
8759 fe621944 2020-11-10 stsp }
8760 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8761 2e1f37b0 2019-08-08 stsp int choice;
8762 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8763 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8764 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8765 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8766 2e1f37b0 2019-08-08 stsp if (err)
8767 2e1f37b0 2019-08-08 stsp goto done;
8768 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8769 2e1f37b0 2019-08-08 stsp have_content = 1;
8770 19e4b907 2019-08-08 stsp else
8771 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8772 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8773 2e1f37b0 2019-08-08 stsp break;
8774 2e1f37b0 2019-08-08 stsp }
8775 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8776 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8777 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8778 2e1f37b0 2019-08-08 stsp done:
8779 2e1f37b0 2019-08-08 stsp free(label1);
8780 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8781 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8782 2e1f37b0 2019-08-08 stsp if (blob)
8783 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8784 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8785 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8786 2e1f37b0 2019-08-08 stsp if (staged_blob)
8787 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8788 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8789 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8790 fe621944 2020-11-10 stsp err = free_err;
8791 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8792 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8793 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8794 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8795 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8796 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8797 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8798 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8799 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8800 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8801 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8802 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8803 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8804 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8805 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8806 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8807 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8808 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8809 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8810 2e1f37b0 2019-08-08 stsp }
8811 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8812 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8813 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8814 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8815 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8816 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8817 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8818 2e1f37b0 2019-08-08 stsp }
8819 2e1f37b0 2019-08-08 stsp free(path1);
8820 2e1f37b0 2019-08-08 stsp free(path2);
8821 fda8017d 2020-07-23 stsp return err;
8822 fda8017d 2020-07-23 stsp }
8823 fda8017d 2020-07-23 stsp
8824 fda8017d 2020-07-23 stsp static const struct got_error *
8825 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8826 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8827 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8828 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8829 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8830 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8831 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8832 fda8017d 2020-07-23 stsp {
8833 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8834 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8835 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8836 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8837 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8838 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8839 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8840 fda8017d 2020-07-23 stsp struct stat sb;
8841 fda8017d 2020-07-23 stsp
8842 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8843 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8844 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8845 fda8017d 2020-07-23 stsp if (err)
8846 fda8017d 2020-07-23 stsp return err;
8847 fda8017d 2020-07-23 stsp
8848 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8849 fda8017d 2020-07-23 stsp return NULL;
8850 fda8017d 2020-07-23 stsp
8851 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8852 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8853 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8854 fda8017d 2020-07-23 stsp if (err)
8855 fda8017d 2020-07-23 stsp goto done;
8856 fda8017d 2020-07-23 stsp }
8857 fda8017d 2020-07-23 stsp
8858 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8859 fda8017d 2020-07-23 stsp if (f == NULL) {
8860 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8861 fda8017d 2020-07-23 stsp path_unstaged_content);
8862 fda8017d 2020-07-23 stsp goto done;
8863 fda8017d 2020-07-23 stsp }
8864 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8865 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8866 fda8017d 2020-07-23 stsp goto done;
8867 fda8017d 2020-07-23 stsp }
8868 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8869 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8870 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8871 fda8017d 2020-07-23 stsp size_t r;
8872 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8873 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8874 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8875 fda8017d 2020-07-23 stsp goto done;
8876 fda8017d 2020-07-23 stsp }
8877 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8878 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8879 fda8017d 2020-07-23 stsp goto done;
8880 fda8017d 2020-07-23 stsp }
8881 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8882 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8883 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8884 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8885 fda8017d 2020-07-23 stsp progress_arg);
8886 fda8017d 2020-07-23 stsp } else {
8887 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8888 67a66647 2021-05-31 stsp
8889 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8890 67a66647 2021-05-31 stsp if (err)
8891 67a66647 2021-05-31 stsp return err;
8892 67a66647 2021-05-31 stsp
8893 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8894 67a66647 2021-05-31 stsp parent) == -1) {
8895 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8896 67a66647 2021-05-31 stsp base_path = NULL;
8897 67a66647 2021-05-31 stsp goto done;
8898 67a66647 2021-05-31 stsp }
8899 67a66647 2021-05-31 stsp
8900 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8901 b90054ed 2022-11-01 stsp base_path, "");
8902 67a66647 2021-05-31 stsp if (err)
8903 67a66647 2021-05-31 stsp goto done;
8904 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8905 67a66647 2021-05-31 stsp blob_base);
8906 67a66647 2021-05-31 stsp if (err)
8907 67a66647 2021-05-31 stsp goto done;
8908 67a66647 2021-05-31 stsp
8909 5e91dae4 2022-08-30 stsp /*
8910 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8911 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8912 eec2f5a9 2021-06-03 stsp */
8913 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8914 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8915 eec2f5a9 2021-06-03 stsp ondisk_path);
8916 eec2f5a9 2021-06-03 stsp if (err)
8917 eec2f5a9 2021-06-03 stsp goto done;
8918 eec2f5a9 2021-06-03 stsp } else {
8919 eec2f5a9 2021-06-03 stsp int fd;
8920 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8921 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8922 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8923 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8924 eec2f5a9 2021-06-03 stsp goto done;
8925 eec2f5a9 2021-06-03 stsp }
8926 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8927 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8928 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8929 eec2f5a9 2021-06-03 stsp close(fd);
8930 eec2f5a9 2021-06-03 stsp goto done;
8931 eec2f5a9 2021-06-03 stsp }
8932 eec2f5a9 2021-06-03 stsp }
8933 eec2f5a9 2021-06-03 stsp
8934 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8935 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8936 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8937 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8938 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8939 fda8017d 2020-07-23 stsp }
8940 fda8017d 2020-07-23 stsp if (err)
8941 fda8017d 2020-07-23 stsp goto done;
8942 fda8017d 2020-07-23 stsp
8943 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8944 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8945 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8946 2ac8aa02 2020-11-09 stsp } else {
8947 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8948 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8949 2ac8aa02 2020-11-09 stsp }
8950 fda8017d 2020-07-23 stsp done:
8951 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8952 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8953 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8954 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8955 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8956 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8957 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8958 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8959 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8960 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8961 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8962 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8963 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8964 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8965 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8966 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8967 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8968 67a66647 2021-05-31 stsp free(blob_base_path);
8969 67a66647 2021-05-31 stsp free(parent);
8970 67a66647 2021-05-31 stsp free(base_path);
8971 2e1f37b0 2019-08-08 stsp return err;
8972 2e1f37b0 2019-08-08 stsp }
8973 2e1f37b0 2019-08-08 stsp
8974 ad493afc 2019-08-03 stsp static const struct got_error *
8975 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8976 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8977 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8978 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8979 ad493afc 2019-08-03 stsp {
8980 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8981 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8982 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8983 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8984 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8985 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8986 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8987 9bc94a15 2019-08-03 stsp struct stat sb;
8988 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8989 ad493afc 2019-08-03 stsp
8990 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8991 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8992 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8993 2e1f37b0 2019-08-08 stsp return NULL;
8994 2e1f37b0 2019-08-08 stsp
8995 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8996 ad493afc 2019-08-03 stsp if (ie == NULL)
8997 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8998 9bc94a15 2019-08-03 stsp
8999 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9000 9bc94a15 2019-08-03 stsp == -1)
9001 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9002 ad493afc 2019-08-03 stsp
9003 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9004 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9005 f69721c3 2019-10-21 stsp if (err)
9006 f69721c3 2019-10-21 stsp goto done;
9007 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9008 f69721c3 2019-10-21 stsp id_str) == -1) {
9009 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9010 eb81bc23 2022-06-28 tracey goto done;
9011 eb81bc23 2022-06-28 tracey }
9012 eb81bc23 2022-06-28 tracey
9013 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9014 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9015 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9016 eb81bc23 2022-06-28 tracey goto done;
9017 eb81bc23 2022-06-28 tracey }
9018 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9019 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9020 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9021 f69721c3 2019-10-21 stsp goto done;
9022 f69721c3 2019-10-21 stsp }
9023 f69721c3 2019-10-21 stsp
9024 ad493afc 2019-08-03 stsp switch (staged_status) {
9025 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9026 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9027 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9028 ad493afc 2019-08-03 stsp if (err)
9029 ad493afc 2019-08-03 stsp break;
9030 ad493afc 2019-08-03 stsp /* fall through */
9031 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9032 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9033 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9034 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9035 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9036 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9037 2e1f37b0 2019-08-08 stsp if (err)
9038 2e1f37b0 2019-08-08 stsp break;
9039 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9040 2e1f37b0 2019-08-08 stsp break;
9041 2e1f37b0 2019-08-08 stsp } else {
9042 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9043 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9044 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9045 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9046 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9047 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9048 2e1f37b0 2019-08-08 stsp }
9049 2e1f37b0 2019-08-08 stsp }
9050 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9051 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9052 ad493afc 2019-08-03 stsp if (err)
9053 ad493afc 2019-08-03 stsp break;
9054 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9055 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9056 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9057 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9058 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9059 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9060 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9061 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9062 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9063 ea7786be 2020-07-23 stsp break;
9064 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9065 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9066 36bf999c 2020-07-23 stsp char *staged_target;
9067 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9068 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9069 36bf999c 2020-07-23 stsp if (err)
9070 36bf999c 2020-07-23 stsp goto done;
9071 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9072 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9073 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9074 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9075 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9076 36bf999c 2020-07-23 stsp free(staged_target);
9077 dfe9fba0 2020-07-23 stsp } else {
9078 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9079 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9080 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9081 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9082 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9083 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9084 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9085 dfe9fba0 2020-07-23 stsp }
9086 ea7786be 2020-07-23 stsp break;
9087 ea7786be 2020-07-23 stsp default:
9088 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9089 ea7786be 2020-07-23 stsp break;
9090 ea7786be 2020-07-23 stsp }
9091 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9092 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9093 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9094 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9095 2ac8aa02 2020-11-09 stsp }
9096 ad493afc 2019-08-03 stsp break;
9097 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9098 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
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_NO)
9105 2e1f37b0 2019-08-08 stsp break;
9106 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9107 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9108 2e1f37b0 2019-08-08 stsp break;
9109 2e1f37b0 2019-08-08 stsp }
9110 2e1f37b0 2019-08-08 stsp }
9111 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9112 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9113 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9114 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9115 9bc94a15 2019-08-03 stsp if (err)
9116 9bc94a15 2019-08-03 stsp break;
9117 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9118 ad493afc 2019-08-03 stsp break;
9119 ad493afc 2019-08-03 stsp }
9120 f69721c3 2019-10-21 stsp done:
9121 ad493afc 2019-08-03 stsp free(ondisk_path);
9122 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9123 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9124 ad493afc 2019-08-03 stsp if (blob_base)
9125 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9126 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9127 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9128 ad493afc 2019-08-03 stsp if (blob_staged)
9129 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9130 f69721c3 2019-10-21 stsp free(id_str);
9131 f69721c3 2019-10-21 stsp free(label_orig);
9132 ad493afc 2019-08-03 stsp return err;
9133 ad493afc 2019-08-03 stsp }
9134 ad493afc 2019-08-03 stsp
9135 ad493afc 2019-08-03 stsp const struct got_error *
9136 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9137 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9138 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9139 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9140 ad493afc 2019-08-03 stsp struct got_repository *repo)
9141 ad493afc 2019-08-03 stsp {
9142 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9143 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9144 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9145 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9146 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9147 ad493afc 2019-08-03 stsp
9148 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9149 ad493afc 2019-08-03 stsp if (err)
9150 ad493afc 2019-08-03 stsp return err;
9151 ad493afc 2019-08-03 stsp
9152 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9153 ad493afc 2019-08-03 stsp if (err)
9154 ad493afc 2019-08-03 stsp goto done;
9155 ad493afc 2019-08-03 stsp
9156 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9157 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9158 ad493afc 2019-08-03 stsp upa.repo = repo;
9159 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9160 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9161 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9162 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9163 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9164 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9165 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9166 ad493afc 2019-08-03 stsp if (err)
9167 ad493afc 2019-08-03 stsp goto done;
9168 ad493afc 2019-08-03 stsp }
9169 ad493afc 2019-08-03 stsp
9170 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9171 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9172 ad493afc 2019-08-03 stsp err = sync_err;
9173 ad493afc 2019-08-03 stsp done:
9174 ad493afc 2019-08-03 stsp free(fileindex_path);
9175 ad493afc 2019-08-03 stsp if (fileindex)
9176 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9177 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9178 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9179 ad493afc 2019-08-03 stsp err = unlockerr;
9180 ad493afc 2019-08-03 stsp return err;
9181 ad493afc 2019-08-03 stsp }
9182 b2118c49 2020-07-28 stsp
9183 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9184 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9185 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9186 b2118c49 2020-07-28 stsp void *info_arg;
9187 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9188 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9189 b2118c49 2020-07-28 stsp void *cancel_arg;
9190 b2118c49 2020-07-28 stsp };
9191 b2118c49 2020-07-28 stsp
9192 b2118c49 2020-07-28 stsp static const struct got_error *
9193 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9194 b2118c49 2020-07-28 stsp {
9195 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9196 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9197 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9198 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9199 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9200 b2118c49 2020-07-28 stsp int stage;
9201 b2118c49 2020-07-28 stsp
9202 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9203 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9204 b2118c49 2020-07-28 stsp
9205 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9206 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9207 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9208 b2118c49 2020-07-28 stsp break;
9209 b2118c49 2020-07-28 stsp }
9210 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9211 b2118c49 2020-07-28 stsp return NULL;
9212 b2118c49 2020-07-28 stsp
9213 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9214 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9215 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9216 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9217 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9218 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9219 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9220 b2118c49 2020-07-28 stsp }
9221 b2118c49 2020-07-28 stsp
9222 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9223 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9224 b2118c49 2020-07-28 stsp
9225 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9226 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9227 b2118c49 2020-07-28 stsp }
9228 b2118c49 2020-07-28 stsp
9229 b2118c49 2020-07-28 stsp const struct got_error *
9230 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9231 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9232 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9233 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9234 b2118c49 2020-07-28 stsp
9235 b2118c49 2020-07-28 stsp {
9236 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9237 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9238 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9239 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9240 b2118c49 2020-07-28 stsp
9241 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9242 b2118c49 2020-07-28 stsp if (err)
9243 b2118c49 2020-07-28 stsp return err;
9244 b2118c49 2020-07-28 stsp
9245 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9246 b2118c49 2020-07-28 stsp if (err)
9247 b2118c49 2020-07-28 stsp goto done;
9248 b2118c49 2020-07-28 stsp
9249 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9250 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9251 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9252 b2118c49 2020-07-28 stsp arg.paths = paths;
9253 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9254 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9255 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9256 b2118c49 2020-07-28 stsp &arg);
9257 b2118c49 2020-07-28 stsp done:
9258 b2118c49 2020-07-28 stsp free(fileindex_path);
9259 b2118c49 2020-07-28 stsp if (fileindex)
9260 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9261 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9262 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9263 b2118c49 2020-07-28 stsp err = unlockerr;
9264 b2118c49 2020-07-28 stsp return err;
9265 b2118c49 2020-07-28 stsp }
9266 78f5ac24 2022-03-19 op
9267 78f5ac24 2022-03-19 op static const struct got_error *
9268 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9269 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9270 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9271 78f5ac24 2022-03-19 op {
9272 78f5ac24 2022-03-19 op const struct got_error *err;
9273 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9274 78f5ac24 2022-03-19 op struct stat sb;
9275 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9276 78f5ac24 2022-03-19 op
9277 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9278 78f5ac24 2022-03-19 op if (err)
9279 78f5ac24 2022-03-19 op return err;
9280 78f5ac24 2022-03-19 op
9281 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9282 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9283 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9284 78f5ac24 2022-03-19 op
9285 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9286 78f5ac24 2022-03-19 op if (ie) {
9287 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9288 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9289 a05fb460 2022-04-23 op repo);
9290 78f5ac24 2022-03-19 op if (err)
9291 78f5ac24 2022-03-19 op goto done;
9292 78f5ac24 2022-03-19 op } else {
9293 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9294 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9295 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9296 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9297 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9298 78f5ac24 2022-03-19 op ondisk_path);
9299 78f5ac24 2022-03-19 op goto done;
9300 78f5ac24 2022-03-19 op }
9301 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9302 78f5ac24 2022-03-19 op }
9303 78f5ac24 2022-03-19 op }
9304 78f5ac24 2022-03-19 op
9305 78f5ac24 2022-03-19 op done:
9306 78f5ac24 2022-03-19 op free(ondisk_path);
9307 78f5ac24 2022-03-19 op return err;
9308 78f5ac24 2022-03-19 op }
9309 78f5ac24 2022-03-19 op
9310 78f5ac24 2022-03-19 op static const struct got_error *
9311 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9312 78f5ac24 2022-03-19 op unsigned char staged_status)
9313 78f5ac24 2022-03-19 op {
9314 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9315 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9316 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9317 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9318 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9319 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9320 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9321 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9322 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9323 78f5ac24 2022-03-19 op return NULL;
9324 78f5ac24 2022-03-19 op }
9325 78f5ac24 2022-03-19 op
9326 78f5ac24 2022-03-19 op static const struct got_error *
9327 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9328 78f5ac24 2022-03-19 op {
9329 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9330 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9331 78f5ac24 2022-03-19 op return NULL;
9332 78f5ac24 2022-03-19 op }
9333 78f5ac24 2022-03-19 op
9334 78f5ac24 2022-03-19 op static const struct got_error *
9335 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9336 78f5ac24 2022-03-19 op unsigned char staged_status)
9337 78f5ac24 2022-03-19 op {
9338 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9339 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9340 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9341 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9342 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9343 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9344 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9345 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9346 78f5ac24 2022-03-19 op return NULL;
9347 78f5ac24 2022-03-19 op }
9348 78f5ac24 2022-03-19 op
9349 78f5ac24 2022-03-19 op const struct got_error *
9350 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9351 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9352 78f5ac24 2022-03-19 op {
9353 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9354 78f5ac24 2022-03-19 op }
9355 78f5ac24 2022-03-19 op
9356 78f5ac24 2022-03-19 op const struct got_error *
9357 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9358 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9359 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9360 78f5ac24 2022-03-19 op {
9361 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9362 78f5ac24 2022-03-19 op int file_renamed = 0;
9363 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9364 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9365 78f5ac24 2022-03-19 op
9366 78f5ac24 2022-03-19 op *oldpath = NULL;
9367 78f5ac24 2022-03-19 op *newpath = NULL;
9368 78f5ac24 2022-03-19 op
9369 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9370 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9371 78f5ac24 2022-03-19 op if (err)
9372 78f5ac24 2022-03-19 op goto done;
9373 78f5ac24 2022-03-19 op
9374 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9375 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9376 78f5ac24 2022-03-19 op if (err)
9377 78f5ac24 2022-03-19 op goto done;
9378 78f5ac24 2022-03-19 op
9379 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9380 78f5ac24 2022-03-19 op file_renamed = 1;
9381 78f5ac24 2022-03-19 op
9382 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9383 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9384 78f5ac24 2022-03-19 op else if (file_renamed) {
9385 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9386 78f5ac24 2022-03-19 op if (err == NULL)
9387 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9388 78f5ac24 2022-03-19 op } else if (old == NULL)
9389 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9390 78f5ac24 2022-03-19 op else
9391 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9392 78f5ac24 2022-03-19 op
9393 78f5ac24 2022-03-19 op done:
9394 78f5ac24 2022-03-19 op if (err) {
9395 78f5ac24 2022-03-19 op free(*oldpath);
9396 78f5ac24 2022-03-19 op *oldpath = NULL;
9397 78f5ac24 2022-03-19 op free(*newpath);
9398 78f5ac24 2022-03-19 op *newpath = NULL;
9399 78f5ac24 2022-03-19 op }
9400 78f5ac24 2022-03-19 op return err;
9401 78f5ac24 2022-03-19 op }
9402 78f5ac24 2022-03-19 op
9403 78f5ac24 2022-03-19 op const struct got_error *
9404 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9405 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9406 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9407 78f5ac24 2022-03-19 op {
9408 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9409 f2dd7807 2022-05-17 op
9410 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9411 f2dd7807 2022-05-17 op saa.worktree = worktree;
9412 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9413 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9414 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9415 f2dd7807 2022-05-17 op saa.repo = repo;
9416 f2dd7807 2022-05-17 op
9417 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9418 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9419 78f5ac24 2022-03-19 op }
9420 f2dd7807 2022-05-17 op
9421 f2dd7807 2022-05-17 op const struct got_error *
9422 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9423 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9424 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9425 f2dd7807 2022-05-17 op {
9426 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9427 f2dd7807 2022-05-17 op
9428 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9429 f2dd7807 2022-05-17 op sda.worktree = worktree;
9430 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9431 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9432 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9433 f2dd7807 2022-05-17 op sda.repo = repo;
9434 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9435 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9436 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9437 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9438 f2dd7807 2022-05-17 op
9439 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9440 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9441 f2dd7807 2022-05-17 op }
9442 f2dd7807 2022-05-17 op
9443 f2dd7807 2022-05-17 op const struct got_error *
9444 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9445 4bcdc895 2022-05-17 op const char *fileindex_path)
9446 f2dd7807 2022-05-17 op {
9447 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9448 f2dd7807 2022-05-17 op
9449 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9450 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9451 f2dd7807 2022-05-17 op
9452 f2dd7807 2022-05-17 op return err;
9453 f2dd7807 2022-05-17 op }