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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 86c3caaf 2018-03-09 stsp
20 d1f6d47b 2019-02-04 stsp #include <dirent.h>
21 12ce7a6c 2019-08-12 stsp #include <limits.h>
22 f5c49f82 2019-01-06 stsp #include <stddef.h>
23 86c3caaf 2018-03-09 stsp #include <string.h>
24 86c3caaf 2018-03-09 stsp #include <stdio.h>
25 86c3caaf 2018-03-09 stsp #include <stdlib.h>
26 303e14b5 2019-09-23 stsp #include <time.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
999 67a66647 2021-05-31 stsp if (err)
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1002 db590691 2021-06-02 stsp } else {
1003 db590691 2021-06-02 stsp /*
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp */
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1012 db590691 2021-06-02 stsp }
1013 67a66647 2021-05-31 stsp }
1014 67a66647 2021-05-31 stsp
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1019 14c901f1 2019-08-08 stsp }
1020 14c901f1 2019-08-08 stsp
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp }
1037 eec2f5a9 2021-06-03 stsp
1038 b6b86fd1 2022-08-30 thomas /*
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp */
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp if (err)
1045 eec2f5a9 2021-06-03 stsp goto done;
1046 eec2f5a9 2021-06-03 stsp } else {
1047 eec2f5a9 2021-06-03 stsp int fd;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp }
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1058 eec2f5a9 2021-06-03 stsp }
1059 14c901f1 2019-08-08 stsp }
1060 14c901f1 2019-08-08 stsp
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 14c901f1 2019-08-08 stsp done:
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 67a66647 2021-05-31 stsp }
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp }
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1084 4a1ddfc2 2019-01-12 stsp }
1085 4a1ddfc2 2019-01-12 stsp
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp {
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 13d9040b 2019-03-27 stsp
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 65b05cec 2020-07-23 stsp
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp if (err)
1098 054041d0 2020-06-24 stsp return err;
1099 054041d0 2020-06-24 stsp
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1106 054041d0 2020-06-24 stsp done:
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp else
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1112 1ebedb77 2019-10-19 stsp }
1113 1ebedb77 2019-10-19 stsp
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp {
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1126 1ebedb77 2019-10-19 stsp }
1127 1ebedb77 2019-10-19 stsp
1128 a2c162eb 2022-10-30 thomas return st_mode;
1129 a2c162eb 2022-10-30 thomas }
1130 a2c162eb 2022-10-30 thomas
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1133 a2c162eb 2022-10-30 thomas {
1134 a2c162eb 2022-10-30 thomas mode_t um;
1135 a2c162eb 2022-10-30 thomas
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1139 13d9040b 2019-03-27 stsp }
1140 13d9040b 2019-03-27 stsp
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 8ba819a3 2020-07-23 stsp
1150 5a1fbc73 2020-07-23 stsp /*
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1153 5a1fbc73 2020-07-23 stsp */
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1157 5a1fbc73 2020-07-23 stsp {
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 5a1fbc73 2020-07-23 stsp int fd;
1162 5a1fbc73 2020-07-23 stsp
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 c6e8a826 2021-04-05 stsp
1165 5a1fbc73 2020-07-23 stsp /*
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1172 5a1fbc73 2020-07-23 stsp */
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1177 5a1fbc73 2020-07-23 stsp
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 5a1fbc73 2020-07-23 stsp }
1187 5a1fbc73 2020-07-23 stsp
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp }
1194 3c1ec782 2020-07-23 stsp
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 3c1ec782 2020-07-23 stsp {
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1208 3c1ec782 2020-07-23 stsp }
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp /*
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1216 3c1ec782 2020-07-23 stsp */
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1220 ce031e9e 2020-10-20 stsp if (err)
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1225 ce031e9e 2020-10-20 stsp }
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp }
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1234 3c1ec782 2020-07-23 stsp if (err)
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp } else {
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp if (err)
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp }
1258 5a1fbc73 2020-07-23 stsp
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 9d31a1d8 2018-03-11 stsp {
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 8ba819a3 2020-07-23 stsp
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 2e1fa222 2020-07-23 stsp
1275 9e39ca77 2022-07-21 thomas /*
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp */
1281 8ba819a3 2020-07-23 stsp do {
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1285 3521b466 2022-07-21 thomas
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1295 8ba819a3 2020-07-23 stsp }
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1305 8ba819a3 2020-07-23 stsp
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1308 3c1ec782 2020-07-23 stsp if (err)
1309 3c1ec782 2020-07-23 stsp return err;
1310 8ba819a3 2020-07-23 stsp
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1319 906c123b 2020-07-23 stsp }
1320 906c123b 2020-07-23 stsp
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1328 c90c8ce3 2020-07-23 stsp }
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1331 5a1fbc73 2020-07-23 stsp if (err)
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1339 c6e8a826 2021-04-05 stsp } else {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 c6e8a826 2021-04-05 stsp }
1343 f35fa46a 2020-07-23 stsp }
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp }
1346 f35fa46a 2020-07-23 stsp
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1350 f4994adc 2020-10-20 stsp if (err)
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1354 f35fa46a 2020-07-23 stsp if (err)
1355 9e39ca77 2022-07-21 thomas return err;
1356 f35fa46a 2020-07-23 stsp /*
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1359 f35fa46a 2020-07-23 stsp */
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1379 8ba819a3 2020-07-23 stsp } else {
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1382 8ba819a3 2020-07-23 stsp }
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp }
1388 8ba819a3 2020-07-23 stsp
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 8ba819a3 2020-07-23 stsp {
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1403 8ba819a3 2020-07-23 stsp
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1411 f5375317 2020-10-20 stsp if (err)
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 f5375317 2020-10-20 stsp free(parent);
1415 21908da4 2019-01-13 stsp if (err)
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 a2c162eb 2022-10-30 thomas mode);
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1428 3b9f0f87 2020-07-23 stsp }
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1435 d70b8e30 2018-12-27 stsp } else {
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1438 507dc3bb 2018-12-29 stsp if (err)
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1441 a2c162eb 2022-10-30 thomas
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1446 a2c162eb 2022-10-30 thomas }
1447 9d31a1d8 2018-03-11 stsp }
1448 507dc3bb 2018-12-29 stsp } else
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1450 3818e3c4 2020-11-01 naddy }
1451 3818e3c4 2020-11-01 naddy
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 bd6aa359 2020-07-23 stsp path);
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 bd6aa359 2020-07-23 stsp if (err)
1463 bd6aa359 2020-07-23 stsp goto done;
1464 bd6aa359 2020-07-23 stsp }
1465 d7b62c98 2018-12-27 stsp
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1467 9d31a1d8 2018-03-11 stsp do {
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1470 9d31a1d8 2018-03-11 stsp if (err)
1471 9d31a1d8 2018-03-11 stsp break;
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1481 9d31a1d8 2018-03-11 stsp }
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1485 9d31a1d8 2018-03-11 stsp
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1489 816dc654 2019-02-16 stsp }
1490 9d31a1d8 2018-03-11 stsp
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1495 f6d8c0ac 2020-11-09 stsp }
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1500 68ed9ba5 2019-02-10 stsp }
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1503 507dc3bb 2018-12-29 stsp }
1504 507dc3bb 2018-12-29 stsp
1505 9d31a1d8 2018-03-11 stsp done:
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1512 6353ad76 2019-02-08 stsp }
1513 6353ad76 2019-02-08 stsp
1514 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1515 6353ad76 2019-02-08 stsp static const struct got_error *
1516 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1517 7154f6ce 2019-03-27 stsp {
1518 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1519 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1520 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1521 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1522 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1523 7154f6ce 2019-03-27 stsp };
1524 7154f6ce 2019-03-27 stsp int i = 0;
1525 9bdd68dd 2020-01-02 naddy char *line = NULL;
1526 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1527 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1528 7154f6ce 2019-03-27 stsp
1529 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1530 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1531 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1532 7154f6ce 2019-03-27 stsp if (feof(f))
1533 7154f6ce 2019-03-27 stsp break;
1534 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1535 7154f6ce 2019-03-27 stsp break;
1536 7154f6ce 2019-03-27 stsp }
1537 7154f6ce 2019-03-27 stsp
1538 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1539 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1540 19332e6d 2019-05-13 stsp == 0)
1541 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1542 7154f6ce 2019-03-27 stsp else
1543 7154f6ce 2019-03-27 stsp i++;
1544 7154f6ce 2019-03-27 stsp }
1545 7154f6ce 2019-03-27 stsp }
1546 9bdd68dd 2020-01-02 naddy free(line);
1547 7154f6ce 2019-03-27 stsp
1548 7154f6ce 2019-03-27 stsp return err;
1549 e2b1e152 2019-07-13 stsp }
1550 e2b1e152 2019-07-13 stsp
1551 e2b1e152 2019-07-13 stsp static int
1552 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1553 1ebedb77 2019-10-19 stsp {
1554 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1555 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1556 1ebedb77 2019-10-19 stsp }
1557 1ebedb77 2019-10-19 stsp
1558 1ebedb77 2019-10-19 stsp static int
1559 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1560 e2b1e152 2019-07-13 stsp {
1561 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1562 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1563 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1564 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1565 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1566 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1567 c363b2c1 2019-08-03 stsp }
1568 c363b2c1 2019-08-03 stsp
1569 c363b2c1 2019-08-03 stsp static unsigned char
1570 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1571 c363b2c1 2019-08-03 stsp {
1572 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1573 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1574 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1575 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1576 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1577 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1578 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1579 c363b2c1 2019-08-03 stsp default:
1580 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1581 a919d5c4 2020-07-23 stsp }
1582 a919d5c4 2020-07-23 stsp }
1583 a919d5c4 2020-07-23 stsp
1584 a919d5c4 2020-07-23 stsp static const struct got_error *
1585 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1586 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1587 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1588 a919d5c4 2020-07-23 stsp {
1589 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1590 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1591 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1592 a919d5c4 2020-07-23 stsp ssize_t elen;
1593 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1594 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1595 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1596 a919d5c4 2020-07-23 stsp
1597 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1598 a919d5c4 2020-07-23 stsp
1599 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1600 a919d5c4 2020-07-23 stsp do {
1601 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1602 a919d5c4 2020-07-23 stsp if (err)
1603 a919d5c4 2020-07-23 stsp return err;
1604 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1605 a919d5c4 2020-07-23 stsp /*
1606 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1607 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1608 a919d5c4 2020-07-23 stsp */
1609 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1610 a919d5c4 2020-07-23 stsp }
1611 a919d5c4 2020-07-23 stsp if (len > 0) {
1612 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1613 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1614 a919d5c4 2020-07-23 stsp len - hdrlen);
1615 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1616 a919d5c4 2020-07-23 stsp hdrlen = 0;
1617 a919d5c4 2020-07-23 stsp }
1618 a919d5c4 2020-07-23 stsp } while (len != 0);
1619 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1620 a919d5c4 2020-07-23 stsp
1621 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1622 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1623 a919d5c4 2020-07-23 stsp if (elen == -1)
1624 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1625 a919d5c4 2020-07-23 stsp } else {
1626 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1627 a919d5c4 2020-07-23 stsp if (elen == -1)
1628 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1629 c363b2c1 2019-08-03 stsp }
1630 a919d5c4 2020-07-23 stsp
1631 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1632 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1633 a919d5c4 2020-07-23 stsp
1634 a919d5c4 2020-07-23 stsp return NULL;
1635 7154f6ce 2019-03-27 stsp }
1636 7154f6ce 2019-03-27 stsp
1637 7154f6ce 2019-03-27 stsp static const struct got_error *
1638 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1639 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1640 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1641 6353ad76 2019-02-08 stsp {
1642 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1643 6353ad76 2019-02-08 stsp struct got_object_id id;
1644 6353ad76 2019-02-08 stsp size_t hdrlen;
1645 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1646 6353ad76 2019-02-08 stsp FILE *f = NULL;
1647 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1648 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1649 6353ad76 2019-02-08 stsp size_t flen, blen;
1650 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1651 6353ad76 2019-02-08 stsp
1652 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1653 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1654 6353ad76 2019-02-08 stsp
1655 7f91a133 2019-12-13 stsp /*
1656 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1657 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1658 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1659 7f91a133 2019-12-13 stsp */
1660 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1661 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1662 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1663 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1664 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1665 882ef1b9 2019-12-13 stsp else
1666 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1667 882ef1b9 2019-12-13 stsp goto done;
1668 882ef1b9 2019-12-13 stsp }
1669 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1670 3d35a492 2019-12-13 stsp goto done;
1671 3d35a492 2019-12-13 stsp }
1672 7f91a133 2019-12-13 stsp } else {
1673 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1674 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1675 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1676 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1677 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1678 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1679 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1680 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1681 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1682 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1683 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1684 3d35a492 2019-12-13 stsp else
1685 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1686 3d35a492 2019-12-13 stsp goto done;
1687 3d35a492 2019-12-13 stsp }
1688 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1689 1338848f 2019-12-13 stsp goto done;
1690 a378724f 2019-02-10 stsp }
1691 a378724f 2019-02-10 stsp }
1692 6353ad76 2019-02-08 stsp
1693 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1694 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1695 1338848f 2019-12-13 stsp goto done;
1696 3f148bc6 2019-07-27 stsp }
1697 efdd40df 2019-07-27 stsp
1698 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1699 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1700 1338848f 2019-12-13 stsp goto done;
1701 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1702 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1703 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1704 1338848f 2019-12-13 stsp goto done;
1705 d00136be 2019-03-26 stsp }
1706 b8f41171 2019-02-10 stsp
1707 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1708 f179e66d 2020-07-23 stsp goto done;
1709 f179e66d 2020-07-23 stsp
1710 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1711 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1712 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1713 1338848f 2019-12-13 stsp goto done;
1714 f179e66d 2020-07-23 stsp }
1715 6353ad76 2019-02-08 stsp
1716 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1717 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1718 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1719 c363b2c1 2019-08-03 stsp else
1720 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1721 c363b2c1 2019-08-03 stsp
1722 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1723 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1724 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1725 f4ae6ddb 2022-07-01 thomas goto done;
1726 f4ae6ddb 2022-07-01 thomas }
1727 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1728 6353ad76 2019-02-08 stsp if (err)
1729 1338848f 2019-12-13 stsp goto done;
1730 6353ad76 2019-02-08 stsp
1731 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1732 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1733 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1734 a919d5c4 2020-07-23 stsp goto done;
1735 a919d5c4 2020-07-23 stsp }
1736 a919d5c4 2020-07-23 stsp
1737 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1738 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1739 ab0d4361 2019-12-13 stsp if (fd == -1) {
1740 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1741 ab0d4361 2019-12-13 stsp goto done;
1742 ab0d4361 2019-12-13 stsp }
1743 3d35a492 2019-12-13 stsp }
1744 3d35a492 2019-12-13 stsp
1745 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1746 6353ad76 2019-02-08 stsp if (f == NULL) {
1747 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1748 6353ad76 2019-02-08 stsp goto done;
1749 6353ad76 2019-02-08 stsp }
1750 1338848f 2019-12-13 stsp fd = -1;
1751 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1752 656b1f76 2019-05-11 jcs for (;;) {
1753 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1754 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1755 6353ad76 2019-02-08 stsp if (err)
1756 7154f6ce 2019-03-27 stsp goto done;
1757 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1758 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1759 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1760 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1761 7154f6ce 2019-03-27 stsp goto done;
1762 80c5c120 2019-02-19 stsp }
1763 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1764 6353ad76 2019-02-08 stsp if (flen != 0)
1765 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1766 6353ad76 2019-02-08 stsp break;
1767 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1768 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1769 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1770 6353ad76 2019-02-08 stsp break;
1771 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1772 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1773 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1774 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1775 6353ad76 2019-02-08 stsp break;
1776 6353ad76 2019-02-08 stsp }
1777 6353ad76 2019-02-08 stsp } else {
1778 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1779 6353ad76 2019-02-08 stsp break;
1780 6353ad76 2019-02-08 stsp }
1781 6353ad76 2019-02-08 stsp hdrlen = 0;
1782 6353ad76 2019-02-08 stsp }
1783 7154f6ce 2019-03-27 stsp
1784 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1785 7154f6ce 2019-03-27 stsp rewind(f);
1786 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1787 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1788 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1789 6353ad76 2019-02-08 stsp done:
1790 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1791 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1792 6353ad76 2019-02-08 stsp if (blob)
1793 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1794 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1795 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1796 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1797 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1798 9d31a1d8 2018-03-11 stsp return err;
1799 e2b1e152 2019-07-13 stsp }
1800 e2b1e152 2019-07-13 stsp
1801 e2b1e152 2019-07-13 stsp /*
1802 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1803 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1804 e2b1e152 2019-07-13 stsp */
1805 e2b1e152 2019-07-13 stsp static const struct got_error *
1806 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1807 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1808 e2b1e152 2019-07-13 stsp {
1809 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1810 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1811 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1812 e2b1e152 2019-07-13 stsp
1813 e2b1e152 2019-07-13 stsp return NULL;
1814 9d31a1d8 2018-03-11 stsp }
1815 9d31a1d8 2018-03-11 stsp
1816 9d31a1d8 2018-03-11 stsp static const struct got_error *
1817 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1818 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1819 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1820 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1821 0584f854 2019-04-06 stsp void *progress_arg)
1822 9d31a1d8 2018-03-11 stsp {
1823 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1824 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1825 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1826 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1827 b8f41171 2019-02-10 stsp struct stat sb;
1828 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1829 9d31a1d8 2018-03-11 stsp
1830 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1831 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1832 6353ad76 2019-02-08 stsp
1833 abb4604f 2019-07-27 stsp if (ie) {
1834 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1835 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1836 a76c42e6 2019-08-03 stsp goto done;
1837 a76c42e6 2019-08-03 stsp }
1838 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1839 7f91a133 2019-12-13 stsp repo);
1840 abb4604f 2019-07-27 stsp if (err)
1841 abb4604f 2019-07-27 stsp goto done;
1842 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1843 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1844 c90c8ce3 2020-07-23 stsp } else {
1845 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1846 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1847 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1848 e6f4ba31 2021-09-24 thomas ondisk_path);
1849 e6f4ba31 2021-09-24 thomas goto done;
1850 e6f4ba31 2021-09-24 thomas }
1851 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1852 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1853 e6f4ba31 2021-09-24 thomas } else {
1854 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1855 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1856 e6f4ba31 2021-09-24 thomas else
1857 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1858 e6f4ba31 2021-09-24 thomas }
1859 c90c8ce3 2020-07-23 stsp }
1860 6353ad76 2019-02-08 stsp
1861 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1862 2c41dce7 2021-06-27 stsp if (ie)
1863 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1864 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1865 b8f41171 2019-02-10 stsp goto done;
1866 b8f41171 2019-02-10 stsp }
1867 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1868 a769b60b 2021-06-27 stsp if (ie)
1869 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1870 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1871 5036ab18 2020-04-18 stsp path);
1872 5036ab18 2020-04-18 stsp goto done;
1873 5036ab18 2020-04-18 stsp }
1874 b8f41171 2019-02-10 stsp
1875 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1876 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1877 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1878 c6e8a826 2021-04-05 stsp /*
1879 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1880 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1881 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1882 c6e8a826 2021-04-05 stsp * updating contents of this file.
1883 c6e8a826 2021-04-05 stsp */
1884 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1885 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1886 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1887 c6e8a826 2021-04-05 stsp /* Same commit. */
1888 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1889 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1890 e2b1e152 2019-07-13 stsp if (err)
1891 e2b1e152 2019-07-13 stsp goto done;
1892 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1893 b8f41171 2019-02-10 stsp path);
1894 6353ad76 2019-02-08 stsp goto done;
1895 a378724f 2019-02-10 stsp }
1896 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1897 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1898 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1899 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1900 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1901 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1902 0f58026f 2021-04-05 stsp if (err)
1903 0f58026f 2021-04-05 stsp goto done;
1904 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1905 0f58026f 2021-04-05 stsp path);
1906 b8f41171 2019-02-10 stsp goto done;
1907 e2b1e152 2019-07-13 stsp }
1908 9d31a1d8 2018-03-11 stsp }
1909 9d31a1d8 2018-03-11 stsp
1910 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1911 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1912 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1913 f4ae6ddb 2022-07-01 thomas goto done;
1914 f4ae6ddb 2022-07-01 thomas }
1915 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1916 8da9e5f4 2019-01-12 stsp if (err)
1917 6353ad76 2019-02-08 stsp goto done;
1918 512f0d0e 2019-01-02 stsp
1919 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1920 234035bc 2019-06-01 stsp int update_timestamps;
1921 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1922 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1923 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1924 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1925 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1926 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1927 f4ae6ddb 2022-07-01 thomas goto done;
1928 f4ae6ddb 2022-07-01 thomas }
1929 234035bc 2019-06-01 stsp struct got_object_id id2;
1930 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1931 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1932 f4ae6ddb 2022-07-01 thomas fd2);
1933 234035bc 2019-06-01 stsp if (err)
1934 f69721c3 2019-10-21 stsp goto done;
1935 f69721c3 2019-10-21 stsp }
1936 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1937 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1940 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1941 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1942 f69721c3 2019-10-21 stsp goto done;
1943 f69721c3 2019-10-21 stsp }
1944 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1945 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1947 234035bc 2019-06-01 stsp goto done;
1948 f69721c3 2019-10-21 stsp }
1949 234035bc 2019-06-01 stsp }
1950 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 36bf999c 2020-07-23 stsp char *link_target;
1952 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1953 36bf999c 2020-07-23 stsp if (err)
1954 36bf999c 2020-07-23 stsp goto done;
1955 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1957 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1958 36bf999c 2020-07-23 stsp free(link_target);
1959 993e2a1b 2020-07-23 stsp } else {
1960 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1961 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1962 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1963 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1964 993e2a1b 2020-07-23 stsp }
1965 f69721c3 2019-10-21 stsp free(label_orig);
1966 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1967 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1968 f4ae6ddb 2022-07-01 thomas goto done;
1969 f4ae6ddb 2022-07-01 thomas }
1970 234035bc 2019-06-01 stsp if (blob2)
1971 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1972 909d120e 2019-09-22 stsp if (err)
1973 909d120e 2019-09-22 stsp goto done;
1974 234035bc 2019-06-01 stsp /*
1975 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1976 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1977 234035bc 2019-06-01 stsp * unmodified files again.
1978 234035bc 2019-06-01 stsp */
1979 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1980 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1981 234035bc 2019-06-01 stsp update_timestamps);
1982 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1983 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1985 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1986 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1987 1ee397ad 2019-07-12 stsp if (err)
1988 1ee397ad 2019-07-12 stsp goto done;
1989 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1990 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1991 13d9040b 2019-03-27 stsp if (err)
1992 13d9040b 2019-03-27 stsp goto done;
1993 13d9040b 2019-03-27 stsp } else {
1994 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1995 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1996 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1997 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1998 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1999 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2000 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2001 2e1fa222 2020-07-23 stsp } else {
2002 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2003 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2004 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2005 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2006 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2007 2e1fa222 2020-07-23 stsp }
2008 13d9040b 2019-03-27 stsp if (err)
2009 13d9040b 2019-03-27 stsp goto done;
2010 65b05cec 2020-07-23 stsp
2011 054041d0 2020-06-24 stsp if (ie) {
2012 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2013 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2014 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2015 054041d0 2020-06-24 stsp } else {
2016 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2017 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2018 054041d0 2020-06-24 stsp &blob->id);
2019 054041d0 2020-06-24 stsp }
2020 13d9040b 2019-03-27 stsp if (err)
2021 13d9040b 2019-03-27 stsp goto done;
2022 65b05cec 2020-07-23 stsp
2023 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2024 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2025 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2026 2e1fa222 2020-07-23 stsp }
2027 13d9040b 2019-03-27 stsp }
2028 f4ae6ddb 2022-07-01 thomas
2029 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2030 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2031 f4ae6ddb 2022-07-01 thomas goto done;
2032 f4ae6ddb 2022-07-01 thomas }
2033 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2034 6353ad76 2019-02-08 stsp done:
2035 6353ad76 2019-02-08 stsp free(ondisk_path);
2036 8da9e5f4 2019-01-12 stsp return err;
2037 90285c3b 2019-01-08 stsp }
2038 90285c3b 2019-01-08 stsp
2039 90285c3b 2019-01-08 stsp static const struct got_error *
2040 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2041 90285c3b 2019-01-08 stsp {
2042 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2043 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2044 90285c3b 2019-01-08 stsp
2045 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2046 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2047 90285c3b 2019-01-08 stsp
2048 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2049 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2050 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2051 c97665ae 2019-01-13 stsp } else {
2052 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2053 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2054 1c4cdd89 2021-06-20 stsp if (err)
2055 1c4cdd89 2021-06-20 stsp goto done;
2056 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2057 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2058 fddefe3b 2020-10-20 stsp free(ondisk_path);
2059 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2060 1c4cdd89 2021-06-20 stsp parent = NULL;
2061 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2062 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2063 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2064 fddefe3b 2020-10-20 stsp ondisk_path);
2065 90285c3b 2019-01-08 stsp break;
2066 90285c3b 2019-01-08 stsp }
2067 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2068 1c4cdd89 2021-06-20 stsp if (err)
2069 1c4cdd89 2021-06-20 stsp break;
2070 1c4cdd89 2021-06-20 stsp }
2071 90285c3b 2019-01-08 stsp }
2072 1c4cdd89 2021-06-20 stsp done:
2073 90285c3b 2019-01-08 stsp free(ondisk_path);
2074 1c4cdd89 2021-06-20 stsp free(parent);
2075 90285c3b 2019-01-08 stsp return err;
2076 512f0d0e 2019-01-02 stsp }
2077 512f0d0e 2019-01-02 stsp
2078 708d8e67 2019-03-27 stsp static const struct got_error *
2079 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2081 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2082 708d8e67 2019-03-27 stsp {
2083 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2084 708d8e67 2019-03-27 stsp unsigned char status;
2085 708d8e67 2019-03-27 stsp struct stat sb;
2086 708d8e67 2019-03-27 stsp char *ondisk_path;
2087 708d8e67 2019-03-27 stsp
2088 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2089 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2090 a76c42e6 2019-08-03 stsp
2091 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2092 708d8e67 2019-03-27 stsp == -1)
2093 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2094 708d8e67 2019-03-27 stsp
2095 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2096 708d8e67 2019-03-27 stsp if (err)
2097 5a58a424 2020-06-23 stsp goto done;
2098 708d8e67 2019-03-27 stsp
2099 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2100 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2101 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2102 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2103 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2104 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2105 993e2a1b 2020-07-23 stsp goto done;
2106 993e2a1b 2020-07-23 stsp }
2107 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2108 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2109 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2110 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2111 993e2a1b 2020-07-23 stsp if (err)
2112 993e2a1b 2020-07-23 stsp goto done;
2113 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2114 993e2a1b 2020-07-23 stsp ie->path);
2115 993e2a1b 2020-07-23 stsp goto done;
2116 993e2a1b 2020-07-23 stsp }
2117 993e2a1b 2020-07-23 stsp
2118 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2119 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2120 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2121 1ee397ad 2019-07-12 stsp if (err)
2122 5a58a424 2020-06-23 stsp goto done;
2123 fc6346c4 2019-03-27 stsp /*
2124 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2125 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2126 fc6346c4 2019-03-27 stsp */
2127 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2128 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2129 fc6346c4 2019-03-27 stsp } else {
2130 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2131 1ee397ad 2019-07-12 stsp if (err)
2132 5a58a424 2020-06-23 stsp goto done;
2133 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2134 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2135 fc6346c4 2019-03-27 stsp if (err)
2136 5a58a424 2020-06-23 stsp goto done;
2137 fc6346c4 2019-03-27 stsp }
2138 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2139 708d8e67 2019-03-27 stsp }
2140 5a58a424 2020-06-23 stsp done:
2141 5a58a424 2020-06-23 stsp free(ondisk_path);
2142 fc6346c4 2019-03-27 stsp return err;
2143 708d8e67 2019-03-27 stsp }
2144 708d8e67 2019-03-27 stsp
2145 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2146 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2147 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2148 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2149 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2150 8da9e5f4 2019-01-12 stsp void *progress_arg;
2151 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2152 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2153 8da9e5f4 2019-01-12 stsp };
2154 8da9e5f4 2019-01-12 stsp
2155 512f0d0e 2019-01-02 stsp static const struct got_error *
2156 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2157 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2158 512f0d0e 2019-01-02 stsp {
2159 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2160 0584f854 2019-04-06 stsp
2161 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2162 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2163 512f0d0e 2019-01-02 stsp
2164 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2165 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2166 8da9e5f4 2019-01-12 stsp }
2167 512f0d0e 2019-01-02 stsp
2168 8da9e5f4 2019-01-12 stsp static const struct got_error *
2169 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2170 8da9e5f4 2019-01-12 stsp {
2171 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2172 7a9df742 2019-01-08 stsp
2173 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2174 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2175 0584f854 2019-04-06 stsp
2176 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2177 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2178 9d31a1d8 2018-03-11 stsp }
2179 9d31a1d8 2018-03-11 stsp
2180 9d31a1d8 2018-03-11 stsp static const struct got_error *
2181 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2182 9d31a1d8 2018-03-11 stsp {
2183 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2184 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2185 8da9e5f4 2019-01-12 stsp char *path;
2186 9d31a1d8 2018-03-11 stsp
2187 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2189 0584f854 2019-04-06 stsp
2190 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2191 63c5ca5d 2019-08-24 stsp return NULL;
2192 63c5ca5d 2019-08-24 stsp
2193 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2194 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2195 8da9e5f4 2019-01-12 stsp == -1)
2196 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2197 9d31a1d8 2018-03-11 stsp
2198 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2199 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2200 8da9e5f4 2019-01-12 stsp else
2201 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2202 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2203 9d31a1d8 2018-03-11 stsp
2204 8da9e5f4 2019-01-12 stsp free(path);
2205 0cd1c46a 2019-03-11 stsp return err;
2206 0cd1c46a 2019-03-11 stsp }
2207 0cd1c46a 2019-03-11 stsp
2208 b2118c49 2020-07-28 stsp const struct got_error *
2209 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2210 b2118c49 2020-07-28 stsp {
2211 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2212 b2118c49 2020-07-28 stsp
2213 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2214 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2215 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2216 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2217 b2118c49 2020-07-28 stsp }
2218 b2118c49 2020-07-28 stsp
2219 b2118c49 2020-07-28 stsp return NULL;
2220 b2118c49 2020-07-28 stsp }
2221 b2118c49 2020-07-28 stsp
2222 818c7501 2019-07-11 stsp static const struct got_error *
2223 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2224 0cd1c46a 2019-03-11 stsp {
2225 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2226 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2227 0cd1c46a 2019-03-11 stsp
2228 517bab73 2019-03-11 stsp *refname = NULL;
2229 517bab73 2019-03-11 stsp
2230 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2231 b2118c49 2020-07-28 stsp if (err)
2232 b2118c49 2020-07-28 stsp return err;
2233 0cd1c46a 2019-03-11 stsp
2234 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2235 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2236 0647c563 2019-03-11 stsp *refname = NULL;
2237 0cd1c46a 2019-03-11 stsp }
2238 517bab73 2019-03-11 stsp free(uuidstr);
2239 517bab73 2019-03-11 stsp return err;
2240 517bab73 2019-03-11 stsp }
2241 517bab73 2019-03-11 stsp
2242 818c7501 2019-07-11 stsp const struct got_error *
2243 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2244 818c7501 2019-07-11 stsp {
2245 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2246 818c7501 2019-07-11 stsp }
2247 818c7501 2019-07-11 stsp
2248 818c7501 2019-07-11 stsp static const struct got_error *
2249 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2250 818c7501 2019-07-11 stsp {
2251 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2252 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2253 818c7501 2019-07-11 stsp }
2254 818c7501 2019-07-11 stsp
2255 818c7501 2019-07-11 stsp static const struct got_error *
2256 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2257 818c7501 2019-07-11 stsp {
2258 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2259 818c7501 2019-07-11 stsp }
2260 818c7501 2019-07-11 stsp
2261 818c7501 2019-07-11 stsp static const struct got_error *
2262 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2263 818c7501 2019-07-11 stsp {
2264 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2265 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2266 818c7501 2019-07-11 stsp }
2267 818c7501 2019-07-11 stsp
2268 818c7501 2019-07-11 stsp static const struct got_error *
2269 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2270 818c7501 2019-07-11 stsp {
2271 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2272 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2273 0ebf8283 2019-07-24 stsp }
2274 0ebf8283 2019-07-24 stsp
2275 0ebf8283 2019-07-24 stsp static const struct got_error *
2276 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2277 0ebf8283 2019-07-24 stsp {
2278 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2279 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2280 0ebf8283 2019-07-24 stsp }
2281 0ebf8283 2019-07-24 stsp
2282 0ebf8283 2019-07-24 stsp static const struct got_error *
2283 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2284 0ebf8283 2019-07-24 stsp {
2285 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2286 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2287 0ebf8283 2019-07-24 stsp }
2288 0ebf8283 2019-07-24 stsp
2289 0ebf8283 2019-07-24 stsp static const struct got_error *
2290 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2291 0ebf8283 2019-07-24 stsp {
2292 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2293 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2294 818c7501 2019-07-11 stsp }
2295 818c7501 2019-07-11 stsp
2296 0ebf8283 2019-07-24 stsp static const struct got_error *
2297 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2298 0ebf8283 2019-07-24 stsp {
2299 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2300 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2301 0ebf8283 2019-07-24 stsp }
2302 818c7501 2019-07-11 stsp
2303 0ebf8283 2019-07-24 stsp const struct got_error *
2304 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2305 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2306 0ebf8283 2019-07-24 stsp {
2307 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2308 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2309 0ebf8283 2019-07-24 stsp *path = NULL;
2310 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2311 0ebf8283 2019-07-24 stsp }
2312 0ebf8283 2019-07-24 stsp return NULL;
2313 10604dce 2021-09-24 thomas }
2314 10604dce 2021-09-24 thomas
2315 10604dce 2021-09-24 thomas static const struct got_error *
2316 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2317 10604dce 2021-09-24 thomas {
2318 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2319 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2320 0ebf8283 2019-07-24 stsp }
2321 0ebf8283 2019-07-24 stsp
2322 10604dce 2021-09-24 thomas static const struct got_error *
2323 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2324 10604dce 2021-09-24 thomas {
2325 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2326 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2327 10604dce 2021-09-24 thomas }
2328 10604dce 2021-09-24 thomas
2329 517bab73 2019-03-11 stsp /*
2330 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2331 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2332 517bab73 2019-03-11 stsp */
2333 517bab73 2019-03-11 stsp static const struct got_error *
2334 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2335 517bab73 2019-03-11 stsp {
2336 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2337 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2338 517bab73 2019-03-11 stsp char *refname;
2339 517bab73 2019-03-11 stsp
2340 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2341 517bab73 2019-03-11 stsp if (err)
2342 517bab73 2019-03-11 stsp return err;
2343 0cd1c46a 2019-03-11 stsp
2344 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2345 0cd1c46a 2019-03-11 stsp if (err)
2346 0cd1c46a 2019-03-11 stsp goto done;
2347 0cd1c46a 2019-03-11 stsp
2348 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2349 0cd1c46a 2019-03-11 stsp done:
2350 0cd1c46a 2019-03-11 stsp free(refname);
2351 0cd1c46a 2019-03-11 stsp if (ref)
2352 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2353 3e3a69f1 2019-07-25 stsp return err;
2354 3e3a69f1 2019-07-25 stsp }
2355 3e3a69f1 2019-07-25 stsp
2356 3e3a69f1 2019-07-25 stsp static const struct got_error *
2357 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2358 3e3a69f1 2019-07-25 stsp {
2359 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2360 3e3a69f1 2019-07-25 stsp
2361 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2362 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2363 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2364 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2365 3e3a69f1 2019-07-25 stsp }
2366 9d31a1d8 2018-03-11 stsp return err;
2367 9d31a1d8 2018-03-11 stsp }
2368 9d31a1d8 2018-03-11 stsp
2369 3e3a69f1 2019-07-25 stsp
2370 ebf99748 2019-05-09 stsp static const struct got_error *
2371 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2372 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2373 ebf99748 2019-05-09 stsp {
2374 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2375 ebf99748 2019-05-09 stsp FILE *index = NULL;
2376 0cd1c46a 2019-03-11 stsp
2377 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2378 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2379 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2380 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2381 ebf99748 2019-05-09 stsp
2382 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2383 3e3a69f1 2019-07-25 stsp if (err)
2384 ebf99748 2019-05-09 stsp goto done;
2385 ebf99748 2019-05-09 stsp
2386 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2387 ebf99748 2019-05-09 stsp if (index == NULL) {
2388 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2389 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2390 ebf99748 2019-05-09 stsp } else {
2391 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2392 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2393 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2394 ebf99748 2019-05-09 stsp }
2395 ebf99748 2019-05-09 stsp done:
2396 ebf99748 2019-05-09 stsp if (err) {
2397 ebf99748 2019-05-09 stsp free(*fileindex_path);
2398 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2399 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2400 ebf99748 2019-05-09 stsp *fileindex = NULL;
2401 ebf99748 2019-05-09 stsp }
2402 ebf99748 2019-05-09 stsp return err;
2403 ebf99748 2019-05-09 stsp }
2404 c932eeeb 2019-05-22 stsp
2405 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2406 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2407 c932eeeb 2019-05-22 stsp const char *path;
2408 c932eeeb 2019-05-22 stsp size_t path_len;
2409 c932eeeb 2019-05-22 stsp const char *entry_name;
2410 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2411 a484d721 2019-06-10 stsp void *progress_arg;
2412 c932eeeb 2019-05-22 stsp };
2413 ebf99748 2019-05-09 stsp
2414 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2415 c932eeeb 2019-05-22 stsp static const struct got_error *
2416 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2417 c932eeeb 2019-05-22 stsp {
2418 1ee397ad 2019-07-12 stsp const struct got_error *err;
2419 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2420 c932eeeb 2019-05-22 stsp
2421 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2422 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2423 c932eeeb 2019-05-22 stsp return NULL;
2424 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2425 102ff934 2019-06-11 stsp return NULL;
2426 102ff934 2019-06-11 stsp
2427 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2428 a769b60b 2021-06-27 stsp return NULL;
2429 a769b60b 2021-06-27 stsp
2430 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2431 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2432 c932eeeb 2019-05-22 stsp return NULL;
2433 c932eeeb 2019-05-22 stsp
2434 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2435 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2436 edd02c5e 2019-07-11 stsp ie->path);
2437 1ee397ad 2019-07-12 stsp if (err)
2438 1ee397ad 2019-07-12 stsp return err;
2439 1ee397ad 2019-07-12 stsp }
2440 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2441 c932eeeb 2019-05-22 stsp return NULL;
2442 a615e0e7 2020-12-16 stsp }
2443 a615e0e7 2020-12-16 stsp
2444 a615e0e7 2020-12-16 stsp static const struct got_error *
2445 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2446 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2447 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2448 a615e0e7 2020-12-16 stsp {
2449 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2450 a615e0e7 2020-12-16 stsp
2451 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2452 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2453 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2454 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2455 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2456 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2457 a615e0e7 2020-12-16 stsp
2458 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2459 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2460 9c6338c4 2019-06-02 stsp }
2461 9c6338c4 2019-06-02 stsp
2462 9c6338c4 2019-06-02 stsp static const struct got_error *
2463 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2464 9c6338c4 2019-06-02 stsp {
2465 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2466 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2467 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2468 867630bb 2020-01-17 stsp struct timespec timeout;
2469 9c6338c4 2019-06-02 stsp
2470 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2471 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2472 9c6338c4 2019-06-02 stsp if (err)
2473 9c6338c4 2019-06-02 stsp goto done;
2474 9c6338c4 2019-06-02 stsp
2475 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2476 9c6338c4 2019-06-02 stsp if (err)
2477 9c6338c4 2019-06-02 stsp goto done;
2478 9c6338c4 2019-06-02 stsp
2479 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2480 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2481 9c6338c4 2019-06-02 stsp fileindex_path);
2482 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2483 9c6338c4 2019-06-02 stsp }
2484 867630bb 2020-01-17 stsp
2485 867630bb 2020-01-17 stsp /*
2486 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2487 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2488 867630bb 2020-01-17 stsp * was recorded in the file index.
2489 867630bb 2020-01-17 stsp */
2490 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2491 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2492 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2493 9c6338c4 2019-06-02 stsp done:
2494 9c6338c4 2019-06-02 stsp if (new_index)
2495 9c6338c4 2019-06-02 stsp fclose(new_index);
2496 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2497 9c6338c4 2019-06-02 stsp return err;
2498 c932eeeb 2019-05-22 stsp }
2499 6ced4176 2019-07-12 stsp
2500 6ced4176 2019-07-12 stsp static const struct got_error *
2501 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2502 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2503 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2504 945f9229 2022-04-16 thomas struct got_repository *repo)
2505 6ced4176 2019-07-12 stsp {
2506 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2507 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2508 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2509 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2510 6ced4176 2019-07-12 stsp
2511 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2512 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2513 6ced4176 2019-07-12 stsp *tree_id = NULL;
2514 6ced4176 2019-07-12 stsp
2515 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2516 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2517 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2518 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2519 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2520 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2521 6ced4176 2019-07-12 stsp goto done;
2522 6ced4176 2019-07-12 stsp }
2523 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2524 945f9229 2022-04-16 thomas worktree->path_prefix);
2525 6ced4176 2019-07-12 stsp if (err)
2526 6ced4176 2019-07-12 stsp goto done;
2527 6ced4176 2019-07-12 stsp return NULL;
2528 6ced4176 2019-07-12 stsp }
2529 6ced4176 2019-07-12 stsp
2530 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2531 6ced4176 2019-07-12 stsp
2532 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2533 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2534 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2535 6ced4176 2019-07-12 stsp goto done;
2536 6ced4176 2019-07-12 stsp }
2537 6ced4176 2019-07-12 stsp
2538 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2539 6ced4176 2019-07-12 stsp if (err)
2540 6ced4176 2019-07-12 stsp goto done;
2541 6ced4176 2019-07-12 stsp
2542 6ced4176 2019-07-12 stsp free(in_repo_path);
2543 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2544 6ced4176 2019-07-12 stsp
2545 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2546 6ced4176 2019-07-12 stsp if (err)
2547 6ced4176 2019-07-12 stsp goto done;
2548 c932eeeb 2019-05-22 stsp
2549 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2550 6ced4176 2019-07-12 stsp /* Check out a single file. */
2551 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2552 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2553 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2554 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2555 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2556 6ced4176 2019-07-12 stsp goto done;
2557 6ced4176 2019-07-12 stsp }
2558 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2559 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2560 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2561 6ced4176 2019-07-12 stsp goto done;
2562 6ced4176 2019-07-12 stsp }
2563 6ced4176 2019-07-12 stsp } else {
2564 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2565 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2566 6ced4176 2019-07-12 stsp if (err)
2567 6ced4176 2019-07-12 stsp return err;
2568 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2569 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2570 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2571 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2572 6ced4176 2019-07-12 stsp goto done;
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp }
2575 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2576 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2577 6ced4176 2019-07-12 stsp } else {
2578 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2579 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2580 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2581 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2582 6ced4176 2019-07-12 stsp goto done;
2583 6ced4176 2019-07-12 stsp }
2584 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2585 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2586 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2587 6ced4176 2019-07-12 stsp goto done;
2588 6ced4176 2019-07-12 stsp }
2589 6ced4176 2019-07-12 stsp }
2590 6ced4176 2019-07-12 stsp done:
2591 6ced4176 2019-07-12 stsp free(id);
2592 6ced4176 2019-07-12 stsp free(in_repo_path);
2593 6ced4176 2019-07-12 stsp if (err) {
2594 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2595 6ced4176 2019-07-12 stsp free(*tree_relpath);
2596 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2597 6ced4176 2019-07-12 stsp free(*tree_id);
2598 6ced4176 2019-07-12 stsp *tree_id = NULL;
2599 6ced4176 2019-07-12 stsp }
2600 07ed472d 2019-07-12 stsp return err;
2601 07ed472d 2019-07-12 stsp }
2602 07ed472d 2019-07-12 stsp
2603 07ed472d 2019-07-12 stsp static const struct got_error *
2604 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2605 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2606 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2607 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 07ed472d 2019-07-12 stsp {
2609 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2610 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2611 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2612 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2613 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2614 07ed472d 2019-07-12 stsp
2615 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2616 7f47418f 2019-12-20 stsp if (err) {
2617 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2618 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2619 7f47418f 2019-12-20 stsp goto done;
2620 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2621 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2622 7f47418f 2019-12-20 stsp if (err)
2623 7f47418f 2019-12-20 stsp return err;
2624 7f47418f 2019-12-20 stsp }
2625 07ed472d 2019-07-12 stsp
2626 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2627 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2628 07ed472d 2019-07-12 stsp if (err)
2629 07ed472d 2019-07-12 stsp goto done;
2630 07ed472d 2019-07-12 stsp
2631 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2632 07ed472d 2019-07-12 stsp if (err)
2633 07ed472d 2019-07-12 stsp goto done;
2634 07ed472d 2019-07-12 stsp
2635 07ed472d 2019-07-12 stsp if (entry_name &&
2636 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2637 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2638 07ed472d 2019-07-12 stsp goto done;
2639 07ed472d 2019-07-12 stsp }
2640 07ed472d 2019-07-12 stsp
2641 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2642 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2643 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2644 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2645 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2646 07ed472d 2019-07-12 stsp arg.repo = repo;
2647 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2648 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2649 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2650 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2651 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2652 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2653 07ed472d 2019-07-12 stsp done:
2654 07ed472d 2019-07-12 stsp if (tree)
2655 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2656 07ed472d 2019-07-12 stsp if (commit)
2657 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2658 6ced4176 2019-07-12 stsp return err;
2659 6ced4176 2019-07-12 stsp }
2660 6ced4176 2019-07-12 stsp
2661 9d31a1d8 2018-03-11 stsp const struct got_error *
2662 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2663 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2664 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2665 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2666 9d31a1d8 2018-03-11 stsp {
2667 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2668 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2669 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2670 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2671 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2672 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2673 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2674 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2675 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2676 f2ea84fa 2019-07-27 stsp int entry_type;
2677 f2ea84fa 2019-07-27 stsp char *relpath;
2678 f2ea84fa 2019-07-27 stsp char *entry_name;
2679 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2680 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2681 9d31a1d8 2018-03-11 stsp
2682 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2683 f2ea84fa 2019-07-27 stsp
2684 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2685 9d31a1d8 2018-03-11 stsp if (err)
2686 9d31a1d8 2018-03-11 stsp return err;
2687 945f9229 2022-04-16 thomas
2688 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2689 945f9229 2022-04-16 thomas worktree->base_commit_id);
2690 945f9229 2022-04-16 thomas if (err)
2691 945f9229 2022-04-16 thomas goto done;
2692 93a30277 2018-12-24 stsp
2693 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2694 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2695 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2696 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2697 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2698 f2ea84fa 2019-07-27 stsp goto done;
2699 f2ea84fa 2019-07-27 stsp }
2700 6ced4176 2019-07-12 stsp
2701 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2702 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2703 945f9229 2022-04-16 thomas worktree, repo);
2704 f2ea84fa 2019-07-27 stsp if (err) {
2705 f2ea84fa 2019-07-27 stsp free(tpd);
2706 6ced4176 2019-07-12 stsp goto done;
2707 6ced4176 2019-07-12 stsp }
2708 f2ea84fa 2019-07-27 stsp
2709 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2710 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2711 f2ea84fa 2019-07-27 stsp if (err) {
2712 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2713 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2714 f2ea84fa 2019-07-27 stsp free(tpd);
2715 f2ea84fa 2019-07-27 stsp goto done;
2716 f2ea84fa 2019-07-27 stsp }
2717 f2ea84fa 2019-07-27 stsp } else
2718 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2719 f2ea84fa 2019-07-27 stsp
2720 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2721 6ced4176 2019-07-12 stsp }
2722 6ced4176 2019-07-12 stsp
2723 51514078 2018-12-25 stsp /*
2724 51514078 2018-12-25 stsp * Read the file index.
2725 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2726 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2727 51514078 2018-12-25 stsp */
2728 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2729 8da9e5f4 2019-01-12 stsp if (err)
2730 c4cdcb68 2019-04-03 stsp goto done;
2731 c4cdcb68 2019-04-03 stsp
2732 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2733 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2734 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2735 f2ea84fa 2019-07-27 stsp
2736 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2737 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2738 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2739 f2ea84fa 2019-07-27 stsp if (err)
2740 f2ea84fa 2019-07-27 stsp break;
2741 f2ea84fa 2019-07-27 stsp
2742 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2743 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2744 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2745 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2746 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2747 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2748 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2749 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2750 f2ea84fa 2019-07-27 stsp if (err)
2751 f2ea84fa 2019-07-27 stsp break;
2752 f2ea84fa 2019-07-27 stsp
2753 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2754 711d9cd9 2019-07-12 stsp }
2755 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2756 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2757 af12c6b9 2019-06-04 stsp err = sync_err;
2758 9d31a1d8 2018-03-11 stsp done:
2759 9c6338c4 2019-06-02 stsp free(fileindex_path);
2760 edfa7d7f 2018-09-11 stsp if (tree)
2761 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2762 9d31a1d8 2018-03-11 stsp if (commit)
2763 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2764 5ade8233 2019-07-12 stsp if (fileindex)
2765 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2766 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2767 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2768 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2769 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2770 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2771 f2ea84fa 2019-07-27 stsp free(tpd);
2772 f2ea84fa 2019-07-27 stsp }
2773 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2774 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2775 9d31a1d8 2018-03-11 stsp err = unlockerr;
2776 f8d1f275 2019-02-04 stsp return err;
2777 f8d1f275 2019-02-04 stsp }
2778 f8d1f275 2019-02-04 stsp
2779 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2780 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2781 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2782 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2783 234035bc 2019-06-01 stsp void *progress_arg;
2784 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2785 234035bc 2019-06-01 stsp void *cancel_arg;
2786 f69721c3 2019-10-21 stsp const char *label_orig;
2787 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2788 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2789 234035bc 2019-06-01 stsp };
2790 234035bc 2019-06-01 stsp
2791 234035bc 2019-06-01 stsp static const struct got_error *
2792 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2793 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2794 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2795 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2796 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2797 234035bc 2019-06-01 stsp {
2798 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2799 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2800 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2801 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2802 234035bc 2019-06-01 stsp struct stat sb;
2803 234035bc 2019-06-01 stsp unsigned char status;
2804 234035bc 2019-06-01 stsp int local_changes_subsumed;
2805 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2806 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2807 234035bc 2019-06-01 stsp
2808 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2809 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2810 d6c87207 2019-08-02 stsp strlen(path2));
2811 1ee397ad 2019-07-12 stsp if (ie == NULL)
2812 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2813 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2814 234035bc 2019-06-01 stsp
2815 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2816 234035bc 2019-06-01 stsp path2) == -1)
2817 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2818 234035bc 2019-06-01 stsp
2819 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2820 7f91a133 2019-12-13 stsp repo);
2821 234035bc 2019-06-01 stsp if (err)
2822 234035bc 2019-06-01 stsp goto done;
2823 234035bc 2019-06-01 stsp
2824 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2825 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2826 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2827 234035bc 2019-06-01 stsp goto done;
2828 234035bc 2019-06-01 stsp }
2829 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2830 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2831 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2832 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2833 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2834 234035bc 2019-06-01 stsp goto done;
2835 234035bc 2019-06-01 stsp }
2836 234035bc 2019-06-01 stsp
2837 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2838 36bf999c 2020-07-23 stsp char *link_target2;
2839 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2840 36bf999c 2020-07-23 stsp if (err)
2841 36bf999c 2020-07-23 stsp goto done;
2842 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2843 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2844 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2845 36bf999c 2020-07-23 stsp free(link_target2);
2846 af57b12a 2020-07-23 stsp } else {
2847 1af628f4 2021-06-03 stsp int fd;
2848 1af628f4 2021-06-03 stsp
2849 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2850 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2851 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2852 1af628f4 2021-06-03 stsp goto done;
2853 1af628f4 2021-06-03 stsp }
2854 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2855 1af628f4 2021-06-03 stsp f_orig, blob1);
2856 1af628f4 2021-06-03 stsp if (err)
2857 1af628f4 2021-06-03 stsp goto done;
2858 1af628f4 2021-06-03 stsp
2859 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2860 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2861 1af628f4 2021-06-03 stsp goto done;
2862 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2863 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2864 1af628f4 2021-06-03 stsp if (err)
2865 1af628f4 2021-06-03 stsp goto done;
2866 1af628f4 2021-06-03 stsp
2867 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2868 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2869 1af628f4 2021-06-03 stsp if (fd == -1) {
2870 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2871 1af628f4 2021-06-03 stsp ondisk_path);
2872 1af628f4 2021-06-03 stsp goto done;
2873 1af628f4 2021-06-03 stsp }
2874 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2875 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2876 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2877 1af628f4 2021-06-03 stsp ondisk_path);
2878 1af628f4 2021-06-03 stsp close(fd);
2879 1af628f4 2021-06-03 stsp goto done;
2880 1af628f4 2021-06-03 stsp }
2881 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2882 1af628f4 2021-06-03 stsp if (err)
2883 1af628f4 2021-06-03 stsp goto done;
2884 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2885 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2886 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2887 1af628f4 2021-06-03 stsp goto done;
2888 1af628f4 2021-06-03 stsp }
2889 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2890 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2891 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2892 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2893 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2894 af57b12a 2020-07-23 stsp }
2895 234035bc 2019-06-01 stsp } else if (blob1) {
2896 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2897 d6c87207 2019-08-02 stsp strlen(path1));
2898 1ee397ad 2019-07-12 stsp if (ie == NULL)
2899 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2900 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2901 2b92fad7 2019-06-02 stsp
2902 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2903 2b92fad7 2019-06-02 stsp path1) == -1)
2904 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2905 2b92fad7 2019-06-02 stsp
2906 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2907 7f91a133 2019-12-13 stsp repo);
2908 2b92fad7 2019-06-02 stsp if (err)
2909 2b92fad7 2019-06-02 stsp goto done;
2910 2b92fad7 2019-06-02 stsp
2911 2b92fad7 2019-06-02 stsp switch (status) {
2912 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2913 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2914 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2915 1ee397ad 2019-07-12 stsp if (err)
2916 1ee397ad 2019-07-12 stsp goto done;
2917 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2918 2b92fad7 2019-06-02 stsp if (err)
2919 2b92fad7 2019-06-02 stsp goto done;
2920 2b92fad7 2019-06-02 stsp if (ie)
2921 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2922 2b92fad7 2019-06-02 stsp break;
2923 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2924 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2925 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2926 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2927 1ee397ad 2019-07-12 stsp if (err)
2928 1ee397ad 2019-07-12 stsp goto done;
2929 2b92fad7 2019-06-02 stsp if (ie)
2930 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2931 2b92fad7 2019-06-02 stsp break;
2932 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2933 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2934 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2935 e8f02263 2022-01-23 thomas off_t blob1_size;
2936 0a22ca1a 2020-09-23 stsp /*
2937 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2938 0a22ca1a 2020-09-23 stsp * exists in the repository.
2939 0a22ca1a 2020-09-23 stsp */
2940 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2941 e8f02263 2022-01-23 thomas &blob1_size, path1);
2942 0a22ca1a 2020-09-23 stsp if (err)
2943 0a22ca1a 2020-09-23 stsp goto done;
2944 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2945 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2946 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2947 0a22ca1a 2020-09-23 stsp if (err)
2948 0a22ca1a 2020-09-23 stsp goto done;
2949 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2950 0a22ca1a 2020-09-23 stsp path1);
2951 0a22ca1a 2020-09-23 stsp if (err)
2952 0a22ca1a 2020-09-23 stsp goto done;
2953 0a22ca1a 2020-09-23 stsp if (ie)
2954 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2955 0a22ca1a 2020-09-23 stsp ie);
2956 0a22ca1a 2020-09-23 stsp } else {
2957 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2958 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2959 0a22ca1a 2020-09-23 stsp }
2960 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2961 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2962 0a22ca1a 2020-09-23 stsp free(id);
2963 0a22ca1a 2020-09-23 stsp if (err)
2964 0a22ca1a 2020-09-23 stsp goto done;
2965 0a22ca1a 2020-09-23 stsp break;
2966 0a22ca1a 2020-09-23 stsp }
2967 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2968 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2969 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2970 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2971 1ee397ad 2019-07-12 stsp if (err)
2972 1ee397ad 2019-07-12 stsp goto done;
2973 2b92fad7 2019-06-02 stsp break;
2974 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2975 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2976 1ee397ad 2019-07-12 stsp if (err)
2977 1ee397ad 2019-07-12 stsp goto done;
2978 2b92fad7 2019-06-02 stsp break;
2979 2b92fad7 2019-06-02 stsp default:
2980 2b92fad7 2019-06-02 stsp break;
2981 2b92fad7 2019-06-02 stsp }
2982 234035bc 2019-06-01 stsp } else if (blob2) {
2983 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 234035bc 2019-06-01 stsp path2) == -1)
2985 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2986 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2987 d6c87207 2019-08-02 stsp strlen(path2));
2988 234035bc 2019-06-01 stsp if (ie) {
2989 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2990 7f91a133 2019-12-13 stsp -1, NULL, repo);
2991 234035bc 2019-06-01 stsp if (err)
2992 234035bc 2019-06-01 stsp goto done;
2993 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2994 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2995 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2996 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2997 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2998 1ee397ad 2019-07-12 stsp status, path2);
2999 234035bc 2019-06-01 stsp goto done;
3000 234035bc 2019-06-01 stsp }
3001 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3002 36bf999c 2020-07-23 stsp char *link_target2;
3003 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3004 36bf999c 2020-07-23 stsp blob2);
3005 36bf999c 2020-07-23 stsp if (err)
3006 36bf999c 2020-07-23 stsp goto done;
3007 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3008 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3009 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3010 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3011 36bf999c 2020-07-23 stsp free(link_target2);
3012 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3013 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3014 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3015 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3016 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3017 526a746f 2020-07-23 stsp a->progress_arg);
3018 dfe9fba0 2020-07-23 stsp } else {
3019 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3020 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3021 526a746f 2020-07-23 stsp }
3022 dfe9fba0 2020-07-23 stsp if (err)
3023 dfe9fba0 2020-07-23 stsp goto done;
3024 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3025 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3026 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3027 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3028 234035bc 2019-06-01 stsp if (err)
3029 234035bc 2019-06-01 stsp goto done;
3030 234035bc 2019-06-01 stsp }
3031 234035bc 2019-06-01 stsp } else {
3032 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3033 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3034 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3035 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3036 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3037 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3038 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3039 2e1fa222 2020-07-23 stsp } else {
3040 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3041 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3042 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3043 2e1fa222 2020-07-23 stsp }
3044 234035bc 2019-06-01 stsp if (err)
3045 234035bc 2019-06-01 stsp goto done;
3046 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3047 234035bc 2019-06-01 stsp if (err)
3048 234035bc 2019-06-01 stsp goto done;
3049 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3050 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3051 3969253a 2020-03-07 stsp if (err) {
3052 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3053 3969253a 2020-03-07 stsp goto done;
3054 3969253a 2020-03-07 stsp }
3055 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3056 2b92fad7 2019-06-02 stsp if (err) {
3057 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3058 2b92fad7 2019-06-02 stsp goto done;
3059 2b92fad7 2019-06-02 stsp }
3060 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3061 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3062 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3063 2e1fa222 2020-07-23 stsp }
3064 234035bc 2019-06-01 stsp }
3065 234035bc 2019-06-01 stsp }
3066 234035bc 2019-06-01 stsp done:
3067 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3068 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3069 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3070 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3071 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3072 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3073 1af628f4 2021-06-03 stsp free(id_str);
3074 54d5be07 2021-06-03 stsp free(label_deriv2);
3075 234035bc 2019-06-01 stsp free(ondisk_path);
3076 234035bc 2019-06-01 stsp return err;
3077 234035bc 2019-06-01 stsp }
3078 234035bc 2019-06-01 stsp
3079 69de9dd4 2021-09-03 stsp static const struct got_error *
3080 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3081 69de9dd4 2021-09-03 stsp {
3082 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3083 69de9dd4 2021-09-03 stsp
3084 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3085 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3086 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3087 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3088 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3089 69de9dd4 2021-09-03 stsp
3090 69de9dd4 2021-09-03 stsp return NULL;
3091 69de9dd4 2021-09-03 stsp }
3092 69de9dd4 2021-09-03 stsp
3093 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3094 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3095 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3096 234035bc 2019-06-01 stsp struct got_repository *repo;
3097 234035bc 2019-06-01 stsp };
3098 234035bc 2019-06-01 stsp
3099 234035bc 2019-06-01 stsp static const struct got_error *
3100 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3101 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3102 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3103 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3104 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3105 234035bc 2019-06-01 stsp {
3106 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3107 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3108 234035bc 2019-06-01 stsp unsigned char status;
3109 234035bc 2019-06-01 stsp struct stat sb;
3110 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3111 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3112 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3113 234035bc 2019-06-01 stsp char *ondisk_path;
3114 234035bc 2019-06-01 stsp
3115 69de9dd4 2021-09-03 stsp if (id == NULL)
3116 69de9dd4 2021-09-03 stsp return NULL;
3117 234035bc 2019-06-01 stsp
3118 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3119 69de9dd4 2021-09-03 stsp if (ie == NULL)
3120 69de9dd4 2021-09-03 stsp return NULL;
3121 69de9dd4 2021-09-03 stsp
3122 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3123 234035bc 2019-06-01 stsp == -1)
3124 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3125 234035bc 2019-06-01 stsp
3126 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3127 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3128 5546d466 2021-09-02 stsp free(ondisk_path);
3129 234035bc 2019-06-01 stsp if (err)
3130 234035bc 2019-06-01 stsp return err;
3131 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3132 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3133 234035bc 2019-06-01 stsp
3134 234035bc 2019-06-01 stsp return NULL;
3135 234035bc 2019-06-01 stsp }
3136 234035bc 2019-06-01 stsp
3137 818c7501 2019-07-11 stsp static const struct got_error *
3138 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3139 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3140 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3141 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3142 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3143 234035bc 2019-06-01 stsp {
3144 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3145 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3146 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3147 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3148 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3149 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3150 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3151 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3152 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3153 234035bc 2019-06-01 stsp
3154 03415a1a 2019-06-02 stsp if (commit_id1) {
3155 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3156 945f9229 2022-04-16 thomas if (err)
3157 945f9229 2022-04-16 thomas goto done;
3158 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3159 03415a1a 2019-06-02 stsp worktree->path_prefix);
3160 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3161 03415a1a 2019-06-02 stsp goto done;
3162 69d57f3d 2020-07-31 stsp }
3163 69d57f3d 2020-07-31 stsp if (tree_id1) {
3164 69d57f3d 2020-07-31 stsp char *id_str;
3165 03415a1a 2019-06-02 stsp
3166 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3167 03415a1a 2019-06-02 stsp if (err)
3168 03415a1a 2019-06-02 stsp goto done;
3169 f69721c3 2019-10-21 stsp
3170 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3171 f69721c3 2019-10-21 stsp if (err)
3172 f69721c3 2019-10-21 stsp goto done;
3173 f69721c3 2019-10-21 stsp
3174 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3175 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3176 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3177 f69721c3 2019-10-21 stsp free(id_str);
3178 f69721c3 2019-10-21 stsp goto done;
3179 f69721c3 2019-10-21 stsp }
3180 f69721c3 2019-10-21 stsp free(id_str);
3181 a0f32f33 2022-06-13 thomas
3182 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3183 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3184 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3185 a0f32f33 2022-06-13 thomas goto done;
3186 a0f32f33 2022-06-13 thomas }
3187 03415a1a 2019-06-02 stsp }
3188 234035bc 2019-06-01 stsp
3189 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3190 945f9229 2022-04-16 thomas if (err)
3191 945f9229 2022-04-16 thomas goto done;
3192 945f9229 2022-04-16 thomas
3193 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3194 234035bc 2019-06-01 stsp worktree->path_prefix);
3195 234035bc 2019-06-01 stsp if (err)
3196 234035bc 2019-06-01 stsp goto done;
3197 234035bc 2019-06-01 stsp
3198 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3199 234035bc 2019-06-01 stsp if (err)
3200 234035bc 2019-06-01 stsp goto done;
3201 234035bc 2019-06-01 stsp
3202 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3203 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3204 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3205 19a6a6b5 2022-07-01 thomas goto done;
3206 19a6a6b5 2022-07-01 thomas }
3207 19a6a6b5 2022-07-01 thomas
3208 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3209 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3210 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3211 a0f32f33 2022-06-13 thomas goto done;
3212 a0f32f33 2022-06-13 thomas }
3213 a0f32f33 2022-06-13 thomas
3214 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3215 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3216 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3217 19a6a6b5 2022-07-01 thomas goto done;
3218 19a6a6b5 2022-07-01 thomas }
3219 19a6a6b5 2022-07-01 thomas
3220 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3221 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3222 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3223 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3224 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3225 69de9dd4 2021-09-03 stsp if (err)
3226 69de9dd4 2021-09-03 stsp goto done;
3227 69de9dd4 2021-09-03 stsp
3228 234035bc 2019-06-01 stsp arg.worktree = worktree;
3229 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3230 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3231 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3232 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3233 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3234 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3235 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3236 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3237 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3238 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3239 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3240 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3241 af12c6b9 2019-06-04 stsp err = sync_err;
3242 234035bc 2019-06-01 stsp done:
3243 945f9229 2022-04-16 thomas if (commit1)
3244 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3245 945f9229 2022-04-16 thomas if (commit2)
3246 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3247 234035bc 2019-06-01 stsp if (tree1)
3248 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3249 234035bc 2019-06-01 stsp if (tree2)
3250 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3251 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3252 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3253 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3254 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3255 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3256 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3257 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3258 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3259 f69721c3 2019-10-21 stsp free(label_orig);
3260 818c7501 2019-07-11 stsp return err;
3261 818c7501 2019-07-11 stsp }
3262 234035bc 2019-06-01 stsp
3263 818c7501 2019-07-11 stsp const struct got_error *
3264 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3265 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3266 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3267 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3268 818c7501 2019-07-11 stsp {
3269 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3270 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3271 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3272 818c7501 2019-07-11 stsp
3273 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3274 818c7501 2019-07-11 stsp if (err)
3275 818c7501 2019-07-11 stsp return err;
3276 818c7501 2019-07-11 stsp
3277 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3278 818c7501 2019-07-11 stsp if (err)
3279 818c7501 2019-07-11 stsp goto done;
3280 818c7501 2019-07-11 stsp
3281 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3282 69de9dd4 2021-09-03 stsp worktree);
3283 818c7501 2019-07-11 stsp if (err)
3284 818c7501 2019-07-11 stsp goto done;
3285 818c7501 2019-07-11 stsp
3286 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3287 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3288 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3289 818c7501 2019-07-11 stsp done:
3290 818c7501 2019-07-11 stsp if (fileindex)
3291 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3292 818c7501 2019-07-11 stsp free(fileindex_path);
3293 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3294 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3295 234035bc 2019-06-01 stsp err = unlockerr;
3296 234035bc 2019-06-01 stsp return err;
3297 234035bc 2019-06-01 stsp }
3298 234035bc 2019-06-01 stsp
3299 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3300 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3301 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3302 927df6b7 2019-02-10 stsp const char *status_path;
3303 927df6b7 2019-02-10 stsp size_t status_path_len;
3304 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3305 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3306 f8d1f275 2019-02-04 stsp void *status_arg;
3307 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3308 f8d1f275 2019-02-04 stsp void *cancel_arg;
3309 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3310 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3311 f2a9dc41 2019-12-13 tracey int report_unchanged;
3312 3143d852 2020-06-25 stsp int no_ignores;
3313 f8d1f275 2019-02-04 stsp };
3314 88d0e355 2019-08-03 stsp
3315 f8d1f275 2019-02-04 stsp static const struct got_error *
3316 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3317 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3318 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3319 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3320 927df6b7 2019-02-10 stsp {
3321 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3322 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3323 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3324 927df6b7 2019-02-10 stsp struct stat sb;
3325 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3326 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3327 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3328 927df6b7 2019-02-10 stsp
3329 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3330 98eaaa12 2019-08-03 stsp if (err)
3331 98eaaa12 2019-08-03 stsp return err;
3332 98eaaa12 2019-08-03 stsp
3333 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3334 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3335 98eaaa12 2019-08-03 stsp return NULL;
3336 98eaaa12 2019-08-03 stsp
3337 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3338 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3339 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3340 98eaaa12 2019-08-03 stsp }
3341 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3342 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3343 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3344 927df6b7 2019-02-10 stsp }
3345 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3346 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3347 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3348 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3349 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3350 98eaaa12 2019-08-03 stsp }
3351 98eaaa12 2019-08-03 stsp
3352 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3353 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3354 927df6b7 2019-02-10 stsp }
3355 927df6b7 2019-02-10 stsp
3356 927df6b7 2019-02-10 stsp static const struct got_error *
3357 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3358 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3359 f8d1f275 2019-02-04 stsp {
3360 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3361 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3362 f8d1f275 2019-02-04 stsp char *abspath;
3363 f8d1f275 2019-02-04 stsp
3364 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3365 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3366 0584f854 2019-04-06 stsp
3367 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3368 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3369 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3370 927df6b7 2019-02-10 stsp return NULL;
3371 927df6b7 2019-02-10 stsp
3372 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3373 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3374 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3375 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3376 f8d1f275 2019-02-04 stsp } else {
3377 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3378 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3379 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3380 f8d1f275 2019-02-04 stsp }
3381 f8d1f275 2019-02-04 stsp
3382 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3383 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3384 f8d1f275 2019-02-04 stsp free(abspath);
3385 9d31a1d8 2018-03-11 stsp return err;
3386 9d31a1d8 2018-03-11 stsp }
3387 f8d1f275 2019-02-04 stsp
3388 f8d1f275 2019-02-04 stsp static const struct got_error *
3389 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3390 f8d1f275 2019-02-04 stsp {
3391 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3392 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3393 2ec1f75b 2019-03-26 stsp unsigned char status;
3394 927df6b7 2019-02-10 stsp
3395 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3396 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3397 0584f854 2019-04-06 stsp
3398 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3399 927df6b7 2019-02-10 stsp return NULL;
3400 927df6b7 2019-02-10 stsp
3401 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3402 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3403 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3404 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3405 2ec1f75b 2019-03-26 stsp else
3406 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3407 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3408 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3409 6841da00 2019-08-08 stsp }
3410 6841da00 2019-08-08 stsp
3411 ef20f542 2022-06-26 thomas static void
3412 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3413 6841da00 2019-08-08 stsp {
3414 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3415 6841da00 2019-08-08 stsp
3416 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3417 6841da00 2019-08-08 stsp free((char *)pe->path);
3418 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3419 6841da00 2019-08-08 stsp }
3420 6841da00 2019-08-08 stsp
3421 ef20f542 2022-06-26 thomas static void
3422 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3423 6841da00 2019-08-08 stsp {
3424 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3425 6841da00 2019-08-08 stsp
3426 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3427 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3428 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3429 6841da00 2019-08-08 stsp free((char *)pe->path);
3430 6841da00 2019-08-08 stsp }
3431 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3432 6841da00 2019-08-08 stsp }
3433 6841da00 2019-08-08 stsp
3434 6841da00 2019-08-08 stsp static const struct got_error *
3435 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3436 6841da00 2019-08-08 stsp {
3437 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3438 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3439 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3440 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3441 6841da00 2019-08-08 stsp size_t linesize = 0;
3442 6841da00 2019-08-08 stsp ssize_t linelen;
3443 6841da00 2019-08-08 stsp
3444 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3445 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3446 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3447 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3448 6841da00 2019-08-08 stsp
3449 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3450 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3451 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3452 bd8de430 2019-10-04 stsp
3453 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3454 bd8de430 2019-10-04 stsp if (line[0] == '#')
3455 bd8de430 2019-10-04 stsp continue;
3456 bd8de430 2019-10-04 stsp
3457 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3458 bd8de430 2019-10-04 stsp if (line[0] == '!')
3459 bd8de430 2019-10-04 stsp continue;
3460 bd8de430 2019-10-04 stsp
3461 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3462 6841da00 2019-08-08 stsp line) == -1) {
3463 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3464 6841da00 2019-08-08 stsp goto done;
3465 6841da00 2019-08-08 stsp }
3466 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3467 6841da00 2019-08-08 stsp if (err)
3468 6841da00 2019-08-08 stsp goto done;
3469 6841da00 2019-08-08 stsp }
3470 6841da00 2019-08-08 stsp if (ferror(f)) {
3471 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3472 6841da00 2019-08-08 stsp goto done;
3473 6841da00 2019-08-08 stsp }
3474 6841da00 2019-08-08 stsp
3475 6841da00 2019-08-08 stsp dirpath = strdup(path);
3476 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3477 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3478 6841da00 2019-08-08 stsp goto done;
3479 6841da00 2019-08-08 stsp }
3480 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3481 6841da00 2019-08-08 stsp done:
3482 6841da00 2019-08-08 stsp free(line);
3483 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3484 6841da00 2019-08-08 stsp free(dirpath);
3485 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3486 6841da00 2019-08-08 stsp }
3487 6841da00 2019-08-08 stsp return err;
3488 6841da00 2019-08-08 stsp }
3489 6841da00 2019-08-08 stsp
3490 ef20f542 2022-06-26 thomas static int
3491 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3492 6841da00 2019-08-08 stsp {
3493 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3494 bd8de430 2019-10-04 stsp
3495 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3496 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3497 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3498 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3499 bd8de430 2019-10-04 stsp
3500 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3501 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3502 6841da00 2019-08-08 stsp
3503 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3504 bd8de430 2019-10-04 stsp continue;
3505 bd8de430 2019-10-04 stsp pattern += 3;
3506 bd8de430 2019-10-04 stsp p = path;
3507 bd8de430 2019-10-04 stsp while (*p) {
3508 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3509 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3510 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3511 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3512 bd8de430 2019-10-04 stsp p++;
3513 bd8de430 2019-10-04 stsp while (*p == '/')
3514 bd8de430 2019-10-04 stsp p++;
3515 bd8de430 2019-10-04 stsp continue;
3516 bd8de430 2019-10-04 stsp }
3517 bd8de430 2019-10-04 stsp return 1;
3518 bd8de430 2019-10-04 stsp }
3519 bd8de430 2019-10-04 stsp }
3520 bd8de430 2019-10-04 stsp }
3521 bd8de430 2019-10-04 stsp
3522 6841da00 2019-08-08 stsp /*
3523 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3524 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3525 6841da00 2019-08-08 stsp * ignores backwards.
3526 6841da00 2019-08-08 stsp */
3527 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3528 6841da00 2019-08-08 stsp while (pe) {
3529 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3530 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3531 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3532 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3533 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3534 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3535 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3536 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3537 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3538 6841da00 2019-08-08 stsp continue;
3539 6841da00 2019-08-08 stsp return 1;
3540 6841da00 2019-08-08 stsp }
3541 6841da00 2019-08-08 stsp }
3542 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3543 6841da00 2019-08-08 stsp }
3544 6841da00 2019-08-08 stsp
3545 6841da00 2019-08-08 stsp return 0;
3546 6841da00 2019-08-08 stsp }
3547 6841da00 2019-08-08 stsp
3548 6841da00 2019-08-08 stsp static const struct got_error *
3549 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3550 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3551 6841da00 2019-08-08 stsp {
3552 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3553 6841da00 2019-08-08 stsp char *ignorespath;
3554 886cec17 2019-12-15 stsp int fd = -1;
3555 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3556 6841da00 2019-08-08 stsp
3557 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3558 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3559 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3560 6841da00 2019-08-08 stsp
3561 886cec17 2019-12-15 stsp if (dirfd != -1) {
3562 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3563 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3564 886cec17 2019-12-15 stsp if (fd == -1) {
3565 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3566 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3567 886cec17 2019-12-15 stsp ignorespath);
3568 886cec17 2019-12-15 stsp } else {
3569 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3570 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3571 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3572 886cec17 2019-12-15 stsp ignorespath);
3573 886cec17 2019-12-15 stsp else {
3574 886cec17 2019-12-15 stsp fd = -1;
3575 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3576 886cec17 2019-12-15 stsp }
3577 886cec17 2019-12-15 stsp }
3578 886cec17 2019-12-15 stsp } else {
3579 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3580 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3581 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3582 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3583 886cec17 2019-12-15 stsp ignorespath);
3584 886cec17 2019-12-15 stsp } else
3585 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3586 886cec17 2019-12-15 stsp }
3587 6841da00 2019-08-08 stsp
3588 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3589 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3590 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3591 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3592 6841da00 2019-08-08 stsp free(ignorespath);
3593 6841da00 2019-08-08 stsp return err;
3594 f8d1f275 2019-02-04 stsp }
3595 f8d1f275 2019-02-04 stsp
3596 f8d1f275 2019-02-04 stsp static const struct got_error *
3597 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3598 6092c299 2021-10-04 thomas int dirfd)
3599 f8d1f275 2019-02-04 stsp {
3600 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3601 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3602 f8d1f275 2019-02-04 stsp char *path = NULL;
3603 6092c299 2021-10-04 thomas
3604 6092c299 2021-10-04 thomas if (ignore != NULL)
3605 6092c299 2021-10-04 thomas *ignore = 0;
3606 f8d1f275 2019-02-04 stsp
3607 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3608 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3609 0584f854 2019-04-06 stsp
3610 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3611 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3613 f8d1f275 2019-02-04 stsp } else {
3614 f8d1f275 2019-02-04 stsp path = de->d_name;
3615 f8d1f275 2019-02-04 stsp }
3616 f8d1f275 2019-02-04 stsp
3617 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3618 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3619 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3620 6092c299 2021-10-04 thomas *ignore = 1;
3621 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3622 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3623 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3624 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3625 f8d1f275 2019-02-04 stsp if (parent_path[0])
3626 f8d1f275 2019-02-04 stsp free(path);
3627 b72f483a 2019-02-05 stsp return err;
3628 f8d1f275 2019-02-04 stsp }
3629 f8d1f275 2019-02-04 stsp
3630 347d1d3e 2019-07-12 stsp static const struct got_error *
3631 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3632 3143d852 2020-06-25 stsp {
3633 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3634 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3635 3143d852 2020-06-25 stsp
3636 3143d852 2020-06-25 stsp if (a->no_ignores)
3637 3143d852 2020-06-25 stsp return NULL;
3638 3143d852 2020-06-25 stsp
3639 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3640 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3641 3143d852 2020-06-25 stsp if (err)
3642 3143d852 2020-06-25 stsp return err;
3643 3143d852 2020-06-25 stsp
3644 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3645 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3646 3143d852 2020-06-25 stsp
3647 3143d852 2020-06-25 stsp return err;
3648 3143d852 2020-06-25 stsp }
3649 3143d852 2020-06-25 stsp
3650 3143d852 2020-06-25 stsp static const struct got_error *
3651 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3652 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3653 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3654 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3655 abb4604f 2019-07-27 stsp {
3656 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3657 abb4604f 2019-07-27 stsp struct stat sb;
3658 ff56836b 2021-07-08 stsp
3659 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3660 abb4604f 2019-07-27 stsp if (ie)
3661 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3662 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3663 abb4604f 2019-07-27 stsp
3664 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3665 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3666 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3667 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3668 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3669 abb4604f 2019-07-27 stsp }
3670 abb4604f 2019-07-27 stsp
3671 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3672 fe1d8685 2022-02-12 thomas return NULL;
3673 fe1d8685 2022-02-12 thomas
3674 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3675 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3676 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3677 abb4604f 2019-07-27 stsp
3678 abb4604f 2019-07-27 stsp return NULL;
3679 3143d852 2020-06-25 stsp }
3680 3143d852 2020-06-25 stsp
3681 3143d852 2020-06-25 stsp static const struct got_error *
3682 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3683 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3684 3143d852 2020-06-25 stsp {
3685 3143d852 2020-06-25 stsp const struct got_error *err;
3686 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3687 3143d852 2020-06-25 stsp
3688 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3689 3143d852 2020-06-25 stsp ".cvsignore");
3690 3143d852 2020-06-25 stsp if (err)
3691 3143d852 2020-06-25 stsp return err;
3692 3143d852 2020-06-25 stsp
3693 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3694 3143d852 2020-06-25 stsp ".gitignore");
3695 3143d852 2020-06-25 stsp if (err)
3696 3143d852 2020-06-25 stsp return err;
3697 3143d852 2020-06-25 stsp
3698 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3699 3143d852 2020-06-25 stsp if (err) {
3700 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3701 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3702 3143d852 2020-06-25 stsp return err;
3703 3143d852 2020-06-25 stsp }
3704 3143d852 2020-06-25 stsp for (;;) {
3705 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3706 3143d852 2020-06-25 stsp ".cvsignore");
3707 3143d852 2020-06-25 stsp if (err)
3708 3143d852 2020-06-25 stsp break;
3709 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3710 3143d852 2020-06-25 stsp ".gitignore");
3711 3143d852 2020-06-25 stsp if (err)
3712 3143d852 2020-06-25 stsp break;
3713 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3714 3143d852 2020-06-25 stsp if (err) {
3715 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3716 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3717 3143d852 2020-06-25 stsp break;
3718 3143d852 2020-06-25 stsp }
3719 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3720 ff56836b 2021-07-08 stsp break;
3721 b737c85a 2020-06-26 stsp free(parent_path);
3722 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3723 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3724 3143d852 2020-06-25 stsp }
3725 3143d852 2020-06-25 stsp
3726 b737c85a 2020-06-26 stsp free(parent_path);
3727 b737c85a 2020-06-26 stsp free(next_parent_path);
3728 3143d852 2020-06-25 stsp return err;
3729 abb4604f 2019-07-27 stsp }
3730 abb4604f 2019-07-27 stsp
3731 abb4604f 2019-07-27 stsp static const struct got_error *
3732 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3733 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3734 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3735 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3736 f2a9dc41 2019-12-13 tracey int report_unchanged)
3737 f8d1f275 2019-02-04 stsp {
3738 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3739 6fc93f37 2019-12-13 stsp int fd = -1;
3740 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3741 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3742 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3743 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3744 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3745 3143d852 2020-06-25 stsp
3746 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3747 f8d1f275 2019-02-04 stsp
3748 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3749 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3750 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3751 8dc303cc 2019-07-27 stsp
3752 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3753 a78810f8 2022-03-13 thomas if (ie) {
3754 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3755 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3756 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3757 a78810f8 2022-03-13 thomas goto done;
3758 a78810f8 2022-03-13 thomas }
3759 a78810f8 2022-03-13 thomas
3760 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3761 6fc93f37 2019-12-13 stsp if (fd == -1) {
3762 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3763 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3764 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3765 ff56836b 2021-07-08 stsp else {
3766 ff56836b 2021-07-08 stsp if (!no_ignores) {
3767 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3768 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3769 ff56836b 2021-07-08 stsp if (err)
3770 ff56836b 2021-07-08 stsp goto done;
3771 ff56836b 2021-07-08 stsp }
3772 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3773 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3774 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3775 ff56836b 2021-07-08 stsp }
3776 abb4604f 2019-07-27 stsp } else {
3777 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3778 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3779 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3780 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3781 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3782 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3783 abb4604f 2019-07-27 stsp arg.status_path = path;
3784 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3785 abb4604f 2019-07-27 stsp arg.repo = repo;
3786 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3787 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3788 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3789 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3790 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3791 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3792 022fae89 2019-12-06 tracey if (!no_ignores) {
3793 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3794 3143d852 2020-06-25 stsp worktree->root_path, path);
3795 3143d852 2020-06-25 stsp if (err)
3796 3143d852 2020-06-25 stsp goto done;
3797 022fae89 2019-12-06 tracey }
3798 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3799 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3800 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3801 927df6b7 2019-02-10 stsp }
3802 3143d852 2020-06-25 stsp done:
3803 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3804 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3805 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3806 927df6b7 2019-02-10 stsp free(ondisk_path);
3807 347d1d3e 2019-07-12 stsp return err;
3808 347d1d3e 2019-07-12 stsp }
3809 347d1d3e 2019-07-12 stsp
3810 347d1d3e 2019-07-12 stsp const struct got_error *
3811 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3812 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3813 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3814 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3815 347d1d3e 2019-07-12 stsp {
3816 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3817 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3818 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3819 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3820 347d1d3e 2019-07-12 stsp
3821 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3822 347d1d3e 2019-07-12 stsp if (err)
3823 347d1d3e 2019-07-12 stsp return err;
3824 347d1d3e 2019-07-12 stsp
3825 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3826 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3827 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3828 f6343036 2021-06-22 stsp no_ignores, 0);
3829 72ea6654 2019-07-27 stsp if (err)
3830 72ea6654 2019-07-27 stsp break;
3831 72ea6654 2019-07-27 stsp }
3832 f8d1f275 2019-02-04 stsp free(fileindex_path);
3833 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3834 f8d1f275 2019-02-04 stsp return err;
3835 f8d1f275 2019-02-04 stsp }
3836 6c7ab921 2019-03-18 stsp
3837 6c7ab921 2019-03-18 stsp const struct got_error *
3838 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3839 6c7ab921 2019-03-18 stsp const char *arg)
3840 6c7ab921 2019-03-18 stsp {
3841 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3842 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3843 6c7ab921 2019-03-18 stsp size_t len;
3844 00bb5ea0 2020-07-23 stsp struct stat sb;
3845 01740607 2020-11-04 stsp char *abspath = NULL;
3846 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3847 6c7ab921 2019-03-18 stsp
3848 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3849 6c7ab921 2019-03-18 stsp
3850 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3851 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3852 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3853 00bb5ea0 2020-07-23 stsp
3854 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3855 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3856 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3857 00bb5ea0 2020-07-23 stsp goto done;
3858 00bb5ea0 2020-07-23 stsp }
3859 727173c3 2020-11-06 stsp sb.st_mode = 0;
3860 00bb5ea0 2020-07-23 stsp }
3861 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3862 00bb5ea0 2020-07-23 stsp /*
3863 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3864 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3865 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3866 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3867 00bb5ea0 2020-07-23 stsp */
3868 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3869 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3870 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3871 00bb5ea0 2020-07-23 stsp goto done;
3872 00bb5ea0 2020-07-23 stsp }
3873 00bb5ea0 2020-07-23 stsp
3874 00bb5ea0 2020-07-23 stsp }
3875 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3876 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3877 00bb5ea0 2020-07-23 stsp if (err)
3878 d0710d08 2019-07-22 stsp goto done;
3879 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3880 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3881 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3882 00bb5ea0 2020-07-23 stsp goto done;
3883 00bb5ea0 2020-07-23 stsp }
3884 00bb5ea0 2020-07-23 stsp } else {
3885 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3886 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3887 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3888 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3889 00bb5ea0 2020-07-23 stsp goto done;
3890 00bb5ea0 2020-07-23 stsp }
3891 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3892 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3893 01740607 2020-11-04 stsp goto done;
3894 01740607 2020-11-04 stsp }
3895 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3896 01740607 2020-11-04 stsp sizeof(canonpath));
3897 01740607 2020-11-04 stsp if (err)
3898 00bb5ea0 2020-07-23 stsp goto done;
3899 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3900 01740607 2020-11-04 stsp if (resolved == NULL) {
3901 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3902 01740607 2020-11-04 stsp goto done;
3903 00bb5ea0 2020-07-23 stsp }
3904 d0710d08 2019-07-22 stsp }
3905 d0710d08 2019-07-22 stsp }
3906 6c7ab921 2019-03-18 stsp
3907 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3908 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3909 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3910 6c7ab921 2019-03-18 stsp goto done;
3911 6c7ab921 2019-03-18 stsp }
3912 6c7ab921 2019-03-18 stsp
3913 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3914 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3915 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3916 f6d88e1a 2019-05-29 stsp if (err)
3917 f6d88e1a 2019-05-29 stsp goto done;
3918 f6d88e1a 2019-05-29 stsp } else {
3919 f6d88e1a 2019-05-29 stsp path = strdup("");
3920 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3921 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3922 f6d88e1a 2019-05-29 stsp goto done;
3923 f6d88e1a 2019-05-29 stsp }
3924 6c7ab921 2019-03-18 stsp }
3925 6c7ab921 2019-03-18 stsp
3926 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3927 6c7ab921 2019-03-18 stsp len = strlen(path);
3928 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3929 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3930 6c7ab921 2019-03-18 stsp len--;
3931 6c7ab921 2019-03-18 stsp }
3932 6c7ab921 2019-03-18 stsp done:
3933 01740607 2020-11-04 stsp free(abspath);
3934 6c7ab921 2019-03-18 stsp free(resolved);
3935 d0710d08 2019-07-22 stsp free(cwd);
3936 6c7ab921 2019-03-18 stsp if (err == NULL)
3937 6c7ab921 2019-03-18 stsp *wt_path = path;
3938 6c7ab921 2019-03-18 stsp else
3939 6c7ab921 2019-03-18 stsp free(path);
3940 d00136be 2019-03-26 stsp return err;
3941 d00136be 2019-03-26 stsp }
3942 d00136be 2019-03-26 stsp
3943 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3944 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3945 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3946 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3947 4e68cba3 2019-11-23 stsp void *progress_arg;
3948 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3949 4e68cba3 2019-11-23 stsp };
3950 4e68cba3 2019-11-23 stsp
3951 4e68cba3 2019-11-23 stsp static const struct got_error *
3952 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3953 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3954 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3955 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3956 07f5b47a 2019-06-02 stsp {
3957 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3958 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3959 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3960 6d022e97 2019-08-04 stsp struct stat sb;
3961 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3962 07f5b47a 2019-06-02 stsp
3963 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3964 dbb83fbd 2019-12-12 stsp relpath) == -1)
3965 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3966 dbb83fbd 2019-12-12 stsp
3967 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3968 6d022e97 2019-08-04 stsp if (ie) {
3969 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3970 12463d8b 2019-12-13 stsp de_name, a->repo);
3971 6d022e97 2019-08-04 stsp if (err)
3972 dbb83fbd 2019-12-12 stsp goto done;
3973 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3974 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3975 dbb83fbd 2019-12-12 stsp goto done;
3976 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3977 dbb83fbd 2019-12-12 stsp if (err)
3978 dbb83fbd 2019-12-12 stsp goto done;
3979 6d022e97 2019-08-04 stsp }
3980 07f5b47a 2019-06-02 stsp
3981 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3982 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3983 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3984 84bf00a6 2022-02-12 thomas else
3985 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3986 dbb83fbd 2019-12-12 stsp goto done;
3987 4e68cba3 2019-11-23 stsp }
3988 07f5b47a 2019-06-02 stsp
3989 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3990 4e68cba3 2019-11-23 stsp if (err)
3991 4e68cba3 2019-11-23 stsp goto done;
3992 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3993 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3994 3969253a 2020-03-07 stsp if (err) {
3995 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3996 3969253a 2020-03-07 stsp goto done;
3997 3969253a 2020-03-07 stsp }
3998 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3999 07f5b47a 2019-06-02 stsp if (err) {
4000 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4001 4e68cba3 2019-11-23 stsp goto done;
4002 07f5b47a 2019-06-02 stsp }
4003 4e68cba3 2019-11-23 stsp done:
4004 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4005 dbb83fbd 2019-12-12 stsp if (err)
4006 dbb83fbd 2019-12-12 stsp return err;
4007 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4008 dbb83fbd 2019-12-12 stsp return NULL;
4009 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4010 07f5b47a 2019-06-02 stsp }
4011 07f5b47a 2019-06-02 stsp
4012 d00136be 2019-03-26 stsp const struct got_error *
4013 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4014 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4015 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4017 d00136be 2019-03-26 stsp {
4018 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4019 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4020 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4021 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4022 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4023 d00136be 2019-03-26 stsp
4024 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4025 d00136be 2019-03-26 stsp if (err)
4026 d00136be 2019-03-26 stsp return err;
4027 d00136be 2019-03-26 stsp
4028 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4029 d00136be 2019-03-26 stsp if (err)
4030 d00136be 2019-03-26 stsp goto done;
4031 d00136be 2019-03-26 stsp
4032 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4033 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4034 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4035 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4036 4e68cba3 2019-11-23 stsp saa.repo = repo;
4037 4e68cba3 2019-11-23 stsp
4038 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4039 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4040 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4041 1dd54920 2019-05-11 stsp if (err)
4042 af12c6b9 2019-06-04 stsp break;
4043 1dd54920 2019-05-11 stsp }
4044 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4045 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4046 af12c6b9 2019-06-04 stsp err = sync_err;
4047 d00136be 2019-03-26 stsp done:
4048 fb399478 2019-07-12 stsp free(fileindex_path);
4049 d00136be 2019-03-26 stsp if (fileindex)
4050 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4051 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4052 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4053 d00136be 2019-03-26 stsp err = unlockerr;
4054 6c7ab921 2019-03-18 stsp return err;
4055 6c7ab921 2019-03-18 stsp }
4056 17ed4618 2019-06-02 stsp
4057 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4058 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4059 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4060 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4061 f2a9dc41 2019-12-13 tracey void *progress_arg;
4062 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4063 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4064 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4065 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4066 766841c2 2020-08-13 stsp const char *status_codes;
4067 f2a9dc41 2019-12-13 tracey };
4068 f2a9dc41 2019-12-13 tracey
4069 f2a9dc41 2019-12-13 tracey static const struct got_error *
4070 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4071 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4072 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4073 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 17ed4618 2019-06-02 stsp {
4075 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4076 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4077 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4078 17ed4618 2019-06-02 stsp struct stat sb;
4079 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4080 d64cc78a 2022-01-25 thomas
4081 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4082 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4083 d64cc78a 2022-01-25 thomas return NULL;
4084 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4085 d64cc78a 2022-01-25 thomas }
4086 17ed4618 2019-06-02 stsp
4087 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4088 17ed4618 2019-06-02 stsp if (ie == NULL)
4089 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4090 2ec1f75b 2019-03-26 stsp
4091 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4092 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4093 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4094 9acbc4fa 2019-08-03 stsp return NULL;
4095 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4096 9acbc4fa 2019-08-03 stsp }
4097 9acbc4fa 2019-08-03 stsp
4098 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4099 f2a9dc41 2019-12-13 tracey relpath) == -1)
4100 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4101 f2a9dc41 2019-12-13 tracey
4102 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4103 12463d8b 2019-12-13 stsp a->repo);
4104 17ed4618 2019-06-02 stsp if (err)
4105 f2a9dc41 2019-12-13 tracey goto done;
4106 17ed4618 2019-06-02 stsp
4107 766841c2 2020-08-13 stsp if (a->status_codes) {
4108 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4109 766841c2 2020-08-13 stsp int i;
4110 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4111 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4112 766841c2 2020-08-13 stsp break;
4113 766841c2 2020-08-13 stsp }
4114 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4115 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4116 766841c2 2020-08-13 stsp free(ondisk_path);
4117 766841c2 2020-08-13 stsp return NULL;
4118 766841c2 2020-08-13 stsp }
4119 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4120 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4121 766841c2 2020-08-13 stsp static char msg[64];
4122 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4123 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4124 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4125 766841c2 2020-08-13 stsp goto done;
4126 766841c2 2020-08-13 stsp }
4127 766841c2 2020-08-13 stsp }
4128 766841c2 2020-08-13 stsp
4129 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4130 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4131 f2a9dc41 2019-12-13 tracey goto done;
4132 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4133 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4134 f2a9dc41 2019-12-13 tracey goto done;
4135 f2a9dc41 2019-12-13 tracey }
4136 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4137 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4138 d64cc78a 2022-01-25 thomas goto done;
4139 d64cc78a 2022-01-25 thomas }
4140 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4141 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4142 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4143 f2a9dc41 2019-12-13 tracey goto done;
4144 f2a9dc41 2019-12-13 tracey }
4145 17ed4618 2019-06-02 stsp }
4146 17ed4618 2019-06-02 stsp
4147 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4148 20a2ad1c 2020-10-20 stsp size_t root_len;
4149 20a2ad1c 2020-10-20 stsp
4150 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4151 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4152 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4153 12463d8b 2019-12-13 stsp ondisk_path);
4154 12463d8b 2019-12-13 stsp goto done;
4155 12463d8b 2019-12-13 stsp }
4156 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4157 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4158 12463d8b 2019-12-13 stsp goto done;
4159 15341bfd 2020-03-05 tracey }
4160 15341bfd 2020-03-05 tracey
4161 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4162 20a2ad1c 2020-10-20 stsp do {
4163 20a2ad1c 2020-10-20 stsp char *parent;
4164 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4165 20a2ad1c 2020-10-20 stsp if (err)
4166 2513f20a 2020-10-20 stsp goto done;
4167 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4168 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4169 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4170 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4171 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4172 20a2ad1c 2020-10-20 stsp ondisk_path);
4173 15341bfd 2020-03-05 tracey break;
4174 15341bfd 2020-03-05 tracey }
4175 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4176 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4177 f2a9dc41 2019-12-13 tracey }
4178 17ed4618 2019-06-02 stsp
4179 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4180 f2a9dc41 2019-12-13 tracey done:
4181 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4182 f2a9dc41 2019-12-13 tracey if (err)
4183 f2a9dc41 2019-12-13 tracey return err;
4184 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4185 f2a9dc41 2019-12-13 tracey return NULL;
4186 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4187 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4188 17ed4618 2019-06-02 stsp }
4189 17ed4618 2019-06-02 stsp
4190 2ec1f75b 2019-03-26 stsp const struct got_error *
4191 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4192 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4193 766841c2 2020-08-13 stsp const char *status_codes,
4194 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4195 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4196 2ec1f75b 2019-03-26 stsp {
4197 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4198 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4199 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4200 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4201 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4202 2ec1f75b 2019-03-26 stsp
4203 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4204 2ec1f75b 2019-03-26 stsp if (err)
4205 2ec1f75b 2019-03-26 stsp return err;
4206 2ec1f75b 2019-03-26 stsp
4207 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4208 2ec1f75b 2019-03-26 stsp if (err)
4209 2ec1f75b 2019-03-26 stsp goto done;
4210 f2a9dc41 2019-12-13 tracey
4211 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4212 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4213 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4214 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4215 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4216 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4217 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4218 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4219 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4220 2ec1f75b 2019-03-26 stsp
4221 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4222 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4223 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4224 17ed4618 2019-06-02 stsp if (err)
4225 af12c6b9 2019-06-04 stsp break;
4226 2ec1f75b 2019-03-26 stsp }
4227 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4228 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4229 af12c6b9 2019-06-04 stsp err = sync_err;
4230 2ec1f75b 2019-03-26 stsp done:
4231 fb399478 2019-07-12 stsp free(fileindex_path);
4232 2ec1f75b 2019-03-26 stsp if (fileindex)
4233 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4234 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4235 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4236 2ec1f75b 2019-03-26 stsp err = unlockerr;
4237 33aa809d 2019-08-08 stsp return err;
4238 33aa809d 2019-08-08 stsp }
4239 33aa809d 2019-08-08 stsp
4240 33aa809d 2019-08-08 stsp static const struct got_error *
4241 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4242 33aa809d 2019-08-08 stsp {
4243 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4244 33aa809d 2019-08-08 stsp char *line = NULL;
4245 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4246 33aa809d 2019-08-08 stsp ssize_t linelen;
4247 33aa809d 2019-08-08 stsp
4248 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4249 33aa809d 2019-08-08 stsp if (linelen == -1) {
4250 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4251 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4252 33aa809d 2019-08-08 stsp goto done;
4253 33aa809d 2019-08-08 stsp }
4254 33aa809d 2019-08-08 stsp return NULL;
4255 33aa809d 2019-08-08 stsp }
4256 33aa809d 2019-08-08 stsp if (outfile) {
4257 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4258 33aa809d 2019-08-08 stsp if (n != linelen) {
4259 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4260 33aa809d 2019-08-08 stsp goto done;
4261 33aa809d 2019-08-08 stsp }
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp if (rejectfile) {
4264 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4265 33aa809d 2019-08-08 stsp if (n != linelen)
4266 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4267 33aa809d 2019-08-08 stsp }
4268 33aa809d 2019-08-08 stsp done:
4269 33aa809d 2019-08-08 stsp free(line);
4270 2ec1f75b 2019-03-26 stsp return err;
4271 2ec1f75b 2019-03-26 stsp }
4272 1f1abb7e 2019-08-08 stsp
4273 33aa809d 2019-08-08 stsp static const struct got_error *
4274 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4275 33aa809d 2019-08-08 stsp {
4276 33aa809d 2019-08-08 stsp char *line = NULL;
4277 33aa809d 2019-08-08 stsp size_t linesize = 0;
4278 33aa809d 2019-08-08 stsp ssize_t linelen;
4279 33aa809d 2019-08-08 stsp
4280 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4281 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4282 500467ff 2019-09-25 hiltjo if (ferror(f))
4283 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4284 500467ff 2019-09-25 hiltjo return NULL;
4285 500467ff 2019-09-25 hiltjo }
4286 33aa809d 2019-08-08 stsp free(line);
4287 33aa809d 2019-08-08 stsp return NULL;
4288 33aa809d 2019-08-08 stsp }
4289 33aa809d 2019-08-08 stsp
4290 33aa809d 2019-08-08 stsp static const struct got_error *
4291 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4292 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4293 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4294 abc59930 2021-09-05 naddy {
4295 33aa809d 2019-08-08 stsp const struct got_error *err;
4296 33aa809d 2019-08-08 stsp
4297 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4298 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4299 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4300 33aa809d 2019-08-08 stsp if (err)
4301 33aa809d 2019-08-08 stsp return err;
4302 33aa809d 2019-08-08 stsp (*line_cur1)++;
4303 33aa809d 2019-08-08 stsp }
4304 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4305 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4306 33aa809d 2019-08-08 stsp if (rejectfile)
4307 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4308 33aa809d 2019-08-08 stsp else
4309 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4310 33aa809d 2019-08-08 stsp if (err)
4311 33aa809d 2019-08-08 stsp return err;
4312 33aa809d 2019-08-08 stsp (*line_cur2)++;
4313 33aa809d 2019-08-08 stsp }
4314 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4315 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4316 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4317 33aa809d 2019-08-08 stsp if (err)
4318 33aa809d 2019-08-08 stsp return err;
4319 33aa809d 2019-08-08 stsp (*line_cur2)++;
4320 33aa809d 2019-08-08 stsp }
4321 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4322 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4323 33aa809d 2019-08-08 stsp if (rejectfile)
4324 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4325 33aa809d 2019-08-08 stsp else
4326 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4327 33aa809d 2019-08-08 stsp if (err)
4328 33aa809d 2019-08-08 stsp return err;
4329 33aa809d 2019-08-08 stsp (*line_cur1)++;
4330 33aa809d 2019-08-08 stsp }
4331 f1e81a05 2019-08-10 stsp
4332 f1e81a05 2019-08-10 stsp return NULL;
4333 f1e81a05 2019-08-10 stsp }
4334 f1e81a05 2019-08-10 stsp
4335 f1e81a05 2019-08-10 stsp static const struct got_error *
4336 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4338 f1e81a05 2019-08-10 stsp {
4339 f1e81a05 2019-08-10 stsp const struct got_error *err;
4340 f1e81a05 2019-08-10 stsp
4341 f1e81a05 2019-08-10 stsp if (outfile) {
4342 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4343 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4344 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4345 f1e81a05 2019-08-10 stsp if (err)
4346 f1e81a05 2019-08-10 stsp return err;
4347 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4348 f1e81a05 2019-08-10 stsp }
4349 f1e81a05 2019-08-10 stsp }
4350 f1e81a05 2019-08-10 stsp if (rejectfile) {
4351 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4352 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4353 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4354 f1e81a05 2019-08-10 stsp if (err)
4355 f1e81a05 2019-08-10 stsp return err;
4356 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4357 f1e81a05 2019-08-10 stsp }
4358 33aa809d 2019-08-08 stsp }
4359 33aa809d 2019-08-08 stsp
4360 33aa809d 2019-08-08 stsp return NULL;
4361 33aa809d 2019-08-08 stsp }
4362 33aa809d 2019-08-08 stsp
4363 33aa809d 2019-08-08 stsp static const struct got_error *
4364 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4365 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4366 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4367 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4368 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4369 33aa809d 2019-08-08 stsp {
4370 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4371 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4372 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4373 33aa809d 2019-08-08 stsp FILE *hunkfile;
4374 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4375 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4376 fe621944 2020-11-10 stsp int rc;
4377 33aa809d 2019-08-08 stsp
4378 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4379 33aa809d 2019-08-08 stsp
4380 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4381 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4382 fe621944 2020-11-10 stsp start_old = cc.left.start;
4383 fe621944 2020-11-10 stsp end_old = cc.left.end;
4384 fe621944 2020-11-10 stsp start_new = cc.right.start;
4385 fe621944 2020-11-10 stsp end_new = cc.right.end;
4386 33aa809d 2019-08-08 stsp
4387 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4388 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4389 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4390 33aa809d 2019-08-08 stsp
4391 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4392 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4393 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4394 33aa809d 2019-08-08 stsp
4395 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4396 fe621944 2020-11-10 stsp if (diff_state == NULL)
4397 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4398 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4399 fe621944 2020-11-10 stsp
4400 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4401 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4402 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4403 33aa809d 2019-08-08 stsp goto done;
4404 33aa809d 2019-08-08 stsp }
4405 fe621944 2020-11-10 stsp
4406 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4407 fe621944 2020-11-10 stsp diff_result, &cc);
4408 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4409 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4410 33aa809d 2019-08-08 stsp goto done;
4411 33aa809d 2019-08-08 stsp }
4412 fe621944 2020-11-10 stsp
4413 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4414 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4415 33aa809d 2019-08-08 stsp goto done;
4416 33aa809d 2019-08-08 stsp }
4417 33aa809d 2019-08-08 stsp
4418 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4419 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4420 33aa809d 2019-08-08 stsp if (err)
4421 33aa809d 2019-08-08 stsp goto done;
4422 33aa809d 2019-08-08 stsp
4423 33aa809d 2019-08-08 stsp switch (*choice) {
4424 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4425 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4427 33aa809d 2019-08-08 stsp break;
4428 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4429 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4430 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4431 33aa809d 2019-08-08 stsp break;
4432 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4433 33aa809d 2019-08-08 stsp break;
4434 33aa809d 2019-08-08 stsp default:
4435 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4436 33aa809d 2019-08-08 stsp break;
4437 33aa809d 2019-08-08 stsp }
4438 33aa809d 2019-08-08 stsp done:
4439 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4440 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4441 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4442 33aa809d 2019-08-08 stsp return err;
4443 33aa809d 2019-08-08 stsp }
4444 33aa809d 2019-08-08 stsp
4445 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4446 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4447 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4448 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4449 1f1abb7e 2019-08-08 stsp void *progress_arg;
4450 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4451 33aa809d 2019-08-08 stsp void *patch_arg;
4452 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4453 10604dce 2021-09-24 thomas int unlink_added_files;
4454 1f1abb7e 2019-08-08 stsp };
4455 a129376b 2019-03-28 stsp
4456 e20a8b6f 2019-06-04 stsp static const struct got_error *
4457 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4458 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4459 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4460 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4461 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4462 33aa809d 2019-08-08 stsp {
4463 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4464 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4465 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4466 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4467 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4468 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4469 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4470 fe621944 2020-11-10 stsp struct stat sb2;
4471 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4472 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4473 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4474 33aa809d 2019-08-08 stsp
4475 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4476 33aa809d 2019-08-08 stsp
4477 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4478 33aa809d 2019-08-08 stsp if (err)
4479 33aa809d 2019-08-08 stsp return err;
4480 33aa809d 2019-08-08 stsp
4481 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4482 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4483 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4484 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4485 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4486 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4487 fa3cef63 2020-07-23 stsp goto done;
4488 fa3cef63 2020-07-23 stsp }
4489 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4490 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4491 48b4f239 2021-12-31 thomas if (link_len == -1) {
4492 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4493 48b4f239 2021-12-31 thomas path2);
4494 48b4f239 2021-12-31 thomas }
4495 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4496 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4497 12463d8b 2019-12-13 stsp }
4498 12463d8b 2019-12-13 stsp } else {
4499 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4500 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4501 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4502 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4503 fa3cef63 2020-07-23 stsp goto done;
4504 fa3cef63 2020-07-23 stsp }
4505 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4506 fa3cef63 2020-07-23 stsp sizeof(link_target));
4507 fa3cef63 2020-07-23 stsp if (link_len == -1)
4508 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4509 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4510 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4511 12463d8b 2019-12-13 stsp }
4512 1ebedb77 2019-10-19 stsp }
4513 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4514 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4515 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4516 fa3cef63 2020-07-23 stsp goto done;
4517 fa3cef63 2020-07-23 stsp }
4518 1ebedb77 2019-10-19 stsp
4519 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4520 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4521 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4522 fa3cef63 2020-07-23 stsp goto done;
4523 fa3cef63 2020-07-23 stsp }
4524 fa3cef63 2020-07-23 stsp fd2 = -1;
4525 fa3cef63 2020-07-23 stsp } else {
4526 fa3cef63 2020-07-23 stsp size_t n;
4527 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4528 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4529 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4530 fa3cef63 2020-07-23 stsp goto done;
4531 fa3cef63 2020-07-23 stsp }
4532 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4533 fa3cef63 2020-07-23 stsp if (n != link_len) {
4534 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4535 fa3cef63 2020-07-23 stsp goto done;
4536 fa3cef63 2020-07-23 stsp }
4537 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4538 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4539 fa3cef63 2020-07-23 stsp goto done;
4540 fa3cef63 2020-07-23 stsp }
4541 fa3cef63 2020-07-23 stsp rewind(f2);
4542 33aa809d 2019-08-08 stsp }
4543 33aa809d 2019-08-08 stsp
4544 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4545 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4546 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4547 f4ae6ddb 2022-07-01 thomas goto done;
4548 f4ae6ddb 2022-07-01 thomas }
4549 f4ae6ddb 2022-07-01 thomas
4550 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4551 33aa809d 2019-08-08 stsp if (err)
4552 33aa809d 2019-08-08 stsp goto done;
4553 33aa809d 2019-08-08 stsp
4554 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4555 33aa809d 2019-08-08 stsp if (err)
4556 33aa809d 2019-08-08 stsp goto done;
4557 33aa809d 2019-08-08 stsp
4558 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4559 33aa809d 2019-08-08 stsp if (err)
4560 33aa809d 2019-08-08 stsp goto done;
4561 33aa809d 2019-08-08 stsp
4562 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4563 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4564 33aa809d 2019-08-08 stsp if (err)
4565 33aa809d 2019-08-08 stsp goto done;
4566 33aa809d 2019-08-08 stsp
4567 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4568 fc2a50f2 2022-11-01 thomas "");
4569 33aa809d 2019-08-08 stsp if (err)
4570 33aa809d 2019-08-08 stsp goto done;
4571 33aa809d 2019-08-08 stsp
4572 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4573 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4574 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4575 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4576 fe621944 2020-11-10 stsp
4577 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4578 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4580 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4581 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4582 fe621944 2020-11-10 stsp nchanges++;
4583 fe621944 2020-11-10 stsp }
4584 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4585 33aa809d 2019-08-08 stsp int choice;
4586 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4587 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4588 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4589 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4590 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4591 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4592 33aa809d 2019-08-08 stsp if (err)
4593 33aa809d 2019-08-08 stsp goto done;
4594 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4595 33aa809d 2019-08-08 stsp have_content = 1;
4596 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4597 33aa809d 2019-08-08 stsp break;
4598 33aa809d 2019-08-08 stsp }
4599 1ebedb77 2019-10-19 stsp if (have_content) {
4600 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4602 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4603 1ebedb77 2019-10-19 stsp if (err)
4604 1ebedb77 2019-10-19 stsp goto done;
4605 1ebedb77 2019-10-19 stsp
4606 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4607 a2c162eb 2022-10-30 thomas mode_t mode;
4608 a2c162eb 2022-10-30 thomas
4609 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4610 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4611 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4612 fa3cef63 2020-07-23 stsp goto done;
4613 fa3cef63 2020-07-23 stsp }
4614 1ebedb77 2019-10-19 stsp }
4615 1ebedb77 2019-10-19 stsp }
4616 33aa809d 2019-08-08 stsp done:
4617 33aa809d 2019-08-08 stsp free(id_str);
4618 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4619 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4620 33aa809d 2019-08-08 stsp if (blob)
4621 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4622 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4623 fe621944 2020-11-10 stsp if (err == NULL)
4624 fe621944 2020-11-10 stsp err = free_err;
4625 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4626 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4627 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4628 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4629 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4631 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4632 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4633 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4634 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4635 33aa809d 2019-08-08 stsp if (err || !have_content) {
4636 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4638 33aa809d 2019-08-08 stsp free(*path_outfile);
4639 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4640 33aa809d 2019-08-08 stsp }
4641 33aa809d 2019-08-08 stsp free(path1);
4642 33aa809d 2019-08-08 stsp return err;
4643 33aa809d 2019-08-08 stsp }
4644 33aa809d 2019-08-08 stsp
4645 33aa809d 2019-08-08 stsp static const struct got_error *
4646 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4648 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4650 a129376b 2019-03-28 stsp {
4651 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4652 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4653 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4654 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4655 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4656 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4657 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4658 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4659 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4660 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4661 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4662 f4ae6ddb 2022-07-01 thomas int fd = -1;
4663 a129376b 2019-03-28 stsp
4664 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4665 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4666 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4667 d3bcc3d1 2019-08-08 stsp return NULL;
4668 3d69ad8d 2019-08-17 semarie
4669 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4670 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4671 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4672 d3bcc3d1 2019-08-08 stsp
4673 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4674 65084dad 2019-08-08 stsp if (ie == NULL)
4675 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4676 a129376b 2019-03-28 stsp
4677 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4678 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4679 a129376b 2019-03-28 stsp if (err) {
4680 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4681 a129376b 2019-03-28 stsp goto done;
4682 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4683 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4684 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4685 e20a8b6f 2019-06-04 stsp goto done;
4686 e20a8b6f 2019-06-04 stsp }
4687 a129376b 2019-03-28 stsp }
4688 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4690 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4691 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4692 a129376b 2019-03-28 stsp goto done;
4693 a129376b 2019-03-28 stsp }
4694 a129376b 2019-03-28 stsp } else {
4695 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4696 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4697 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4698 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4699 a129376b 2019-03-28 stsp goto done;
4700 a129376b 2019-03-28 stsp }
4701 a129376b 2019-03-28 stsp } else {
4702 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4703 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4704 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4705 a129376b 2019-03-28 stsp goto done;
4706 a129376b 2019-03-28 stsp }
4707 a129376b 2019-03-28 stsp }
4708 a129376b 2019-03-28 stsp }
4709 a129376b 2019-03-28 stsp
4710 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4711 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4712 945f9229 2022-04-16 thomas if (err)
4713 945f9229 2022-04-16 thomas goto done;
4714 945f9229 2022-04-16 thomas
4715 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4716 a9fa2909 2019-07-27 stsp if (err) {
4717 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4719 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4720 a9fa2909 2019-07-27 stsp goto done;
4721 a9fa2909 2019-07-27 stsp } else {
4722 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4723 a9fa2909 2019-07-27 stsp if (err)
4724 a9fa2909 2019-07-27 stsp goto done;
4725 a9fa2909 2019-07-27 stsp
4726 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4727 1233e6b6 2020-10-19 stsp if (err)
4728 a9fa2909 2019-07-27 stsp goto done;
4729 a9fa2909 2019-07-27 stsp
4730 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4731 1233e6b6 2020-10-19 stsp free(te_name);
4732 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4733 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4734 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4735 a9fa2909 2019-07-27 stsp goto done;
4736 a9fa2909 2019-07-27 stsp }
4737 a129376b 2019-03-28 stsp }
4738 a129376b 2019-03-28 stsp
4739 a129376b 2019-03-28 stsp switch (status) {
4740 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4741 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4742 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4743 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4744 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4745 33aa809d 2019-08-08 stsp if (err)
4746 33aa809d 2019-08-08 stsp goto done;
4747 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4748 33aa809d 2019-08-08 stsp break;
4749 33aa809d 2019-08-08 stsp }
4750 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4751 1f1abb7e 2019-08-08 stsp ie->path);
4752 1ee397ad 2019-07-12 stsp if (err)
4753 1ee397ad 2019-07-12 stsp goto done;
4754 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4755 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4756 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4757 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4758 10604dce 2021-09-24 thomas relpath) == -1) {
4759 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4760 10604dce 2021-09-24 thomas goto done;
4761 10604dce 2021-09-24 thomas }
4762 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4763 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4764 10604dce 2021-09-24 thomas ondisk_path);
4765 10604dce 2021-09-24 thomas break;
4766 10604dce 2021-09-24 thomas }
4767 10604dce 2021-09-24 thomas }
4768 a129376b 2019-03-28 stsp break;
4769 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4770 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4771 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4772 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4773 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4774 33aa809d 2019-08-08 stsp if (err)
4775 33aa809d 2019-08-08 stsp goto done;
4776 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4777 33aa809d 2019-08-08 stsp break;
4778 33aa809d 2019-08-08 stsp }
4779 33aa809d 2019-08-08 stsp /* fall through */
4780 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4781 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4782 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4783 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4784 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4785 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4786 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4787 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4788 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4789 24278f30 2019-08-03 stsp } else
4790 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4791 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4792 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4793 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4794 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4795 f4ae6ddb 2022-07-01 thomas goto done;
4796 f4ae6ddb 2022-07-01 thomas }
4797 f4ae6ddb 2022-07-01 thomas
4798 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4799 a129376b 2019-03-28 stsp if (err)
4800 65084dad 2019-08-08 stsp goto done;
4801 65084dad 2019-08-08 stsp
4802 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4803 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4805 a129376b 2019-03-28 stsp goto done;
4806 65084dad 2019-08-08 stsp }
4807 65084dad 2019-08-08 stsp
4808 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4810 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4811 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4812 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4814 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4815 33aa809d 2019-08-08 stsp break;
4816 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4817 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4818 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4819 369fd7e5 2020-07-23 stsp path_content);
4820 369fd7e5 2020-07-23 stsp break;
4821 369fd7e5 2020-07-23 stsp }
4822 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4823 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4824 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4825 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4826 369fd7e5 2020-07-23 stsp } else {
4827 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4828 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4829 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4830 369fd7e5 2020-07-23 stsp goto done;
4831 369fd7e5 2020-07-23 stsp }
4832 33aa809d 2019-08-08 stsp }
4833 33aa809d 2019-08-08 stsp } else {
4834 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4835 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4836 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4837 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4838 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4839 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4840 2e1fa222 2020-07-23 stsp } else {
4841 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4842 2e1fa222 2020-07-23 stsp ie->path,
4843 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4845 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4846 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4847 2e1fa222 2020-07-23 stsp }
4848 a129376b 2019-03-28 stsp if (err)
4849 a129376b 2019-03-28 stsp goto done;
4850 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4851 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4852 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4853 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4854 437adc9d 2020-12-10 yzhong blob->id.sha1,
4855 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4856 2e1fa222 2020-07-23 stsp if (err)
4857 2e1fa222 2020-07-23 stsp goto done;
4858 2e1fa222 2020-07-23 stsp }
4859 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4860 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4861 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4862 33aa809d 2019-08-08 stsp }
4863 a129376b 2019-03-28 stsp }
4864 a129376b 2019-03-28 stsp break;
4865 e20a8b6f 2019-06-04 stsp }
4866 a129376b 2019-03-28 stsp default:
4867 1f1abb7e 2019-08-08 stsp break;
4868 a129376b 2019-03-28 stsp }
4869 a129376b 2019-03-28 stsp done:
4870 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4871 33aa809d 2019-08-08 stsp free(path_content);
4872 e20a8b6f 2019-06-04 stsp free(parent_path);
4873 a129376b 2019-03-28 stsp free(tree_path);
4874 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4875 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4876 a129376b 2019-03-28 stsp if (blob)
4877 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4878 a129376b 2019-03-28 stsp if (tree)
4879 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4880 a129376b 2019-03-28 stsp free(tree_id);
4881 945f9229 2022-04-16 thomas if (base_commit)
4882 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4883 e20a8b6f 2019-06-04 stsp return err;
4884 e20a8b6f 2019-06-04 stsp }
4885 e20a8b6f 2019-06-04 stsp
4886 e20a8b6f 2019-06-04 stsp const struct got_error *
4887 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4888 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4889 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4891 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4892 e20a8b6f 2019-06-04 stsp {
4893 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4894 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4895 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4896 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4897 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4898 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4899 e20a8b6f 2019-06-04 stsp
4900 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4901 e20a8b6f 2019-06-04 stsp if (err)
4902 e20a8b6f 2019-06-04 stsp return err;
4903 e20a8b6f 2019-06-04 stsp
4904 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4905 e20a8b6f 2019-06-04 stsp if (err)
4906 e20a8b6f 2019-06-04 stsp goto done;
4907 e20a8b6f 2019-06-04 stsp
4908 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4909 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4910 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4911 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4912 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4913 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4914 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4915 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4916 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4917 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4918 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4919 e20a8b6f 2019-06-04 stsp if (err)
4920 af12c6b9 2019-06-04 stsp break;
4921 e20a8b6f 2019-06-04 stsp }
4922 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4923 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4924 e20a8b6f 2019-06-04 stsp err = sync_err;
4925 af12c6b9 2019-06-04 stsp done:
4926 fb399478 2019-07-12 stsp free(fileindex_path);
4927 a129376b 2019-03-28 stsp if (fileindex)
4928 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4929 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4930 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4931 a129376b 2019-03-28 stsp err = unlockerr;
4932 c4296144 2019-05-09 stsp return err;
4933 c4296144 2019-05-09 stsp }
4934 c4296144 2019-05-09 stsp
4935 cf066bf8 2019-05-09 stsp static void
4936 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4937 cf066bf8 2019-05-09 stsp {
4938 24519714 2019-05-09 stsp free(ct->path);
4939 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4940 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4941 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4942 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4943 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4944 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4945 cf066bf8 2019-05-09 stsp free(ct);
4946 cf066bf8 2019-05-09 stsp }
4947 24519714 2019-05-09 stsp
4948 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4949 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4950 24519714 2019-05-09 stsp struct got_repository *repo;
4951 24519714 2019-05-09 stsp struct got_worktree *worktree;
4952 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4953 5f8a88c6 2019-08-03 stsp int have_staged_files;
4954 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4955 ef899790 2022-11-01 thomas int diff_header_shown;
4956 ef899790 2022-11-01 thomas FILE *diff_outfile;
4957 ef899790 2022-11-01 thomas FILE *f1;
4958 ef899790 2022-11-01 thomas FILE *f2;
4959 24519714 2019-05-09 stsp };
4960 ef899790 2022-11-01 thomas
4961 ef899790 2022-11-01 thomas /*
4962 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
4963 ef899790 2022-11-01 thomas * it as content to the diff engine.
4964 ef899790 2022-11-01 thomas */
4965 ef899790 2022-11-01 thomas static const struct got_error *
4966 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4967 ef899790 2022-11-01 thomas const char *abspath)
4968 ef899790 2022-11-01 thomas {
4969 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
4970 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
4971 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
4972 ef899790 2022-11-01 thomas
4973 ef899790 2022-11-01 thomas *fd = -1;
4974 ef899790 2022-11-01 thomas
4975 ef899790 2022-11-01 thomas if (dirfd != -1) {
4976 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4977 ef899790 2022-11-01 thomas if (target_len == -1)
4978 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
4979 ef899790 2022-11-01 thomas } else {
4980 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
4981 ef899790 2022-11-01 thomas if (target_len == -1)
4982 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
4983 ef899790 2022-11-01 thomas }
4984 ef899790 2022-11-01 thomas
4985 ef899790 2022-11-01 thomas *fd = got_opentempfd();
4986 ef899790 2022-11-01 thomas if (*fd == -1)
4987 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
4988 ef899790 2022-11-01 thomas
4989 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
4990 ef899790 2022-11-01 thomas if (outlen == -1) {
4991 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
4992 ef899790 2022-11-01 thomas goto done;
4993 ef899790 2022-11-01 thomas }
4994 ef899790 2022-11-01 thomas
4995 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
4996 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
4997 ef899790 2022-11-01 thomas goto done;
4998 ef899790 2022-11-01 thomas }
4999 ef899790 2022-11-01 thomas done:
5000 ef899790 2022-11-01 thomas if (err) {
5001 ef899790 2022-11-01 thomas close(*fd);
5002 ef899790 2022-11-01 thomas *fd = -1;
5003 ef899790 2022-11-01 thomas }
5004 ef899790 2022-11-01 thomas return err;
5005 ef899790 2022-11-01 thomas }
5006 ef899790 2022-11-01 thomas
5007 ef899790 2022-11-01 thomas static const struct got_error *
5008 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5009 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5010 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5011 ef899790 2022-11-01 thomas {
5012 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5013 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5014 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5015 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5016 ef899790 2022-11-01 thomas char *label1 = NULL;
5017 ef899790 2022-11-01 thomas struct stat sb;
5018 ef899790 2022-11-01 thomas off_t size1 = 0;
5019 ef899790 2022-11-01 thomas int f2_exists = 0;
5020 ef899790 2022-11-01 thomas char *id_str = NULL;
5021 ef899790 2022-11-01 thomas
5022 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5023 d259e491 2022-11-01 thomas
5024 d259e491 2022-11-01 thomas if (diff_staged) {
5025 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5026 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5027 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5028 d259e491 2022-11-01 thomas return NULL;
5029 d259e491 2022-11-01 thomas } else {
5030 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5031 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5032 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_DELETE)
5033 d259e491 2022-11-01 thomas return NULL;
5034 d259e491 2022-11-01 thomas }
5035 ef899790 2022-11-01 thomas
5036 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5037 ef899790 2022-11-01 thomas if (err)
5038 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5039 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5040 ef899790 2022-11-01 thomas if (err)
5041 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5042 ef899790 2022-11-01 thomas
5043 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5044 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5045 ef899790 2022-11-01 thomas if (err)
5046 ef899790 2022-11-01 thomas return err;
5047 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5048 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5049 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5050 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5051 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5052 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5053 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5054 ef899790 2022-11-01 thomas }
5055 ef899790 2022-11-01 thomas
5056 ef899790 2022-11-01 thomas if (diff_staged) {
5057 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5058 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5059 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5060 ef899790 2022-11-01 thomas label1 = ct->path;
5061 ef899790 2022-11-01 thomas label2 = ct->path;
5062 ef899790 2022-11-01 thomas break;
5063 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5064 ef899790 2022-11-01 thomas label2 = ct->path;
5065 ef899790 2022-11-01 thomas break;
5066 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5067 ef899790 2022-11-01 thomas label1 = ct->path;
5068 ef899790 2022-11-01 thomas break;
5069 ef899790 2022-11-01 thomas default:
5070 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5071 ef899790 2022-11-01 thomas }
5072 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5073 ef899790 2022-11-01 thomas if (fd1 == -1) {
5074 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5075 ef899790 2022-11-01 thomas goto done;
5076 ef899790 2022-11-01 thomas }
5077 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5078 ef899790 2022-11-01 thomas if (fd2 == -1) {
5079 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5080 ef899790 2022-11-01 thomas goto done;
5081 ef899790 2022-11-01 thomas }
5082 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5083 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5084 ef899790 2022-11-01 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5085 ef899790 2022-11-01 thomas repo, diff_outfile);
5086 ef899790 2022-11-01 thomas goto done;
5087 ef899790 2022-11-01 thomas }
5088 ef899790 2022-11-01 thomas
5089 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5090 ef899790 2022-11-01 thomas if (fd1 == -1) {
5091 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5092 ef899790 2022-11-01 thomas goto done;
5093 ef899790 2022-11-01 thomas }
5094 ef899790 2022-11-01 thomas
5095 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5096 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5097 ef899790 2022-11-01 thomas 8192, fd1);
5098 ef899790 2022-11-01 thomas if (err)
5099 ef899790 2022-11-01 thomas goto done;
5100 ef899790 2022-11-01 thomas }
5101 ef899790 2022-11-01 thomas
5102 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5103 ef899790 2022-11-01 thomas if (dirfd != -1) {
5104 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5105 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5106 ef899790 2022-11-01 thomas if (fd == -1) {
5107 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5108 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5109 ef899790 2022-11-01 thomas ct->ondisk_path);
5110 ef899790 2022-11-01 thomas goto done;
5111 ef899790 2022-11-01 thomas }
5112 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5113 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5114 ef899790 2022-11-01 thomas if (err)
5115 ef899790 2022-11-01 thomas goto done;
5116 ef899790 2022-11-01 thomas }
5117 ef899790 2022-11-01 thomas } else {
5118 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5119 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5120 ef899790 2022-11-01 thomas if (fd == -1) {
5121 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5122 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5123 ef899790 2022-11-01 thomas ct->ondisk_path);
5124 ef899790 2022-11-01 thomas goto done;
5125 ef899790 2022-11-01 thomas }
5126 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5127 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5128 ef899790 2022-11-01 thomas if (err)
5129 ef899790 2022-11-01 thomas goto done;
5130 ef899790 2022-11-01 thomas }
5131 ef899790 2022-11-01 thomas }
5132 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5133 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5134 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5135 ef899790 2022-11-01 thomas goto done;
5136 ef899790 2022-11-01 thomas }
5137 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5138 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5139 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5140 ef899790 2022-11-01 thomas goto done;
5141 ef899790 2022-11-01 thomas }
5142 ef899790 2022-11-01 thomas fd = -1;
5143 ef899790 2022-11-01 thomas f2_exists = 1;
5144 ef899790 2022-11-01 thomas }
5145 ef899790 2022-11-01 thomas
5146 ef899790 2022-11-01 thomas if (blob1) {
5147 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5148 ef899790 2022-11-01 thomas f1, blob1);
5149 ef899790 2022-11-01 thomas if (err)
5150 ef899790 2022-11-01 thomas goto done;
5151 ef899790 2022-11-01 thomas }
5152 ef899790 2022-11-01 thomas
5153 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5154 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5155 ef899790 2022-11-01 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, diff_outfile);
5156 ef899790 2022-11-01 thomas done:
5157 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5158 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5159 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5160 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5161 ef899790 2022-11-01 thomas if (blob1)
5162 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5163 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5164 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5165 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5166 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5167 ef899790 2022-11-01 thomas return err;
5168 ef899790 2022-11-01 thomas }
5169 cf066bf8 2019-05-09 stsp
5170 c4296144 2019-05-09 stsp static const struct got_error *
5171 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5172 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5173 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5174 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5175 c4296144 2019-05-09 stsp {
5176 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5177 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5178 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5179 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5180 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5181 768aea60 2019-05-09 stsp struct stat sb;
5182 c4296144 2019-05-09 stsp
5183 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5184 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5185 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5186 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5187 5f8a88c6 2019-08-03 stsp return NULL;
5188 5f8a88c6 2019-08-03 stsp } else {
5189 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
5190 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5191 c4296144 2019-05-09 stsp
5192 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5193 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5194 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5195 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
5196 5f8a88c6 2019-08-03 stsp return NULL;
5197 5f8a88c6 2019-08-03 stsp }
5198 0b5cc0d6 2019-05-09 stsp
5199 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5200 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5201 036813ee 2019-05-09 stsp goto done;
5202 036813ee 2019-05-09 stsp }
5203 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5204 036813ee 2019-05-09 stsp parent_path = strdup("");
5205 69960a46 2019-05-09 stsp if (parent_path == NULL)
5206 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5207 69960a46 2019-05-09 stsp } else {
5208 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5209 69960a46 2019-05-09 stsp if (err)
5210 69960a46 2019-05-09 stsp return err;
5211 69960a46 2019-05-09 stsp }
5212 c4296144 2019-05-09 stsp
5213 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5214 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5215 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5216 c4296144 2019-05-09 stsp goto done;
5217 768aea60 2019-05-09 stsp }
5218 768aea60 2019-05-09 stsp
5219 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5220 768aea60 2019-05-09 stsp relpath) == -1) {
5221 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5222 768aea60 2019-05-09 stsp goto done;
5223 768aea60 2019-05-09 stsp }
5224 0aeb8099 2020-07-23 stsp
5225 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5226 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5227 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5228 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5229 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5230 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5231 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5232 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5233 0aeb8099 2020-07-23 stsp break;
5234 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5235 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5236 0aeb8099 2020-07-23 stsp break;
5237 0aeb8099 2020-07-23 stsp default:
5238 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5239 0aeb8099 2020-07-23 stsp goto done;
5240 0aeb8099 2020-07-23 stsp }
5241 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5242 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5243 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5244 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5245 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5246 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5247 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5248 12463d8b 2019-12-13 stsp ct->ondisk_path);
5249 12463d8b 2019-12-13 stsp goto done;
5250 12463d8b 2019-12-13 stsp }
5251 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5252 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5253 768aea60 2019-05-09 stsp goto done;
5254 768aea60 2019-05-09 stsp }
5255 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5256 c4296144 2019-05-09 stsp }
5257 c4296144 2019-05-09 stsp
5258 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5259 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5260 44d03001 2019-05-09 stsp relpath) == -1) {
5261 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5262 44d03001 2019-05-09 stsp goto done;
5263 44d03001 2019-05-09 stsp }
5264 44d03001 2019-05-09 stsp
5265 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5266 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5267 35213c7c 2020-07-23 stsp int is_bad_symlink;
5268 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5269 35213c7c 2020-07-23 stsp ssize_t target_len;
5270 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5271 35213c7c 2020-07-23 stsp sizeof(target_path));
5272 35213c7c 2020-07-23 stsp if (target_len == -1) {
5273 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5274 35213c7c 2020-07-23 stsp ct->ondisk_path);
5275 35213c7c 2020-07-23 stsp goto done;
5276 35213c7c 2020-07-23 stsp }
5277 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5278 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5279 35213c7c 2020-07-23 stsp if (err)
5280 35213c7c 2020-07-23 stsp goto done;
5281 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5282 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5283 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5284 35213c7c 2020-07-23 stsp goto done;
5285 35213c7c 2020-07-23 stsp }
5286 35213c7c 2020-07-23 stsp }
5287 35213c7c 2020-07-23 stsp
5288 35213c7c 2020-07-23 stsp
5289 cf066bf8 2019-05-09 stsp ct->status = status;
5290 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5291 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5292 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5293 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5294 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5295 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5296 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5297 b416585c 2019-05-13 stsp goto done;
5298 b416585c 2019-05-13 stsp }
5299 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5300 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5301 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5302 f0b75401 2019-08-03 stsp goto done;
5303 f0b75401 2019-08-03 stsp }
5304 f0b75401 2019-08-03 stsp }
5305 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5306 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5307 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5308 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5309 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5310 036813ee 2019-05-09 stsp goto done;
5311 036813ee 2019-05-09 stsp }
5312 ca2503ea 2019-05-09 stsp }
5313 24519714 2019-05-09 stsp ct->path = strdup(path);
5314 24519714 2019-05-09 stsp if (ct->path == NULL) {
5315 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5316 24519714 2019-05-09 stsp goto done;
5317 24519714 2019-05-09 stsp }
5318 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5319 ef899790 2022-11-01 thomas if (err)
5320 ef899790 2022-11-01 thomas goto done;
5321 ef899790 2022-11-01 thomas
5322 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5323 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5324 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5325 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5326 ef899790 2022-11-01 thomas if (err)
5327 ef899790 2022-11-01 thomas goto done;
5328 ef899790 2022-11-01 thomas }
5329 c4296144 2019-05-09 stsp done:
5330 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5331 ed175427 2019-05-09 stsp free_commitable(ct);
5332 24519714 2019-05-09 stsp free(parent_path);
5333 036813ee 2019-05-09 stsp free(path);
5334 a129376b 2019-03-28 stsp return err;
5335 a129376b 2019-03-28 stsp }
5336 c4296144 2019-05-09 stsp
5337 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5338 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5339 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5340 036813ee 2019-05-09 stsp struct got_repository *);
5341 ed175427 2019-05-09 stsp
5342 ed175427 2019-05-09 stsp static const struct got_error *
5343 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5344 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5345 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5346 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5347 afa376bf 2019-05-09 stsp struct got_repository *repo)
5348 ed175427 2019-05-09 stsp {
5349 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5350 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5351 036813ee 2019-05-09 stsp char *subpath;
5352 ed175427 2019-05-09 stsp
5353 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5354 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5355 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5356 ed175427 2019-05-09 stsp
5357 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5358 ed175427 2019-05-09 stsp if (err)
5359 ed175427 2019-05-09 stsp return err;
5360 ed175427 2019-05-09 stsp
5361 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5362 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5363 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5364 036813ee 2019-05-09 stsp free(subpath);
5365 036813ee 2019-05-09 stsp return err;
5366 036813ee 2019-05-09 stsp }
5367 ed175427 2019-05-09 stsp
5368 036813ee 2019-05-09 stsp static const struct got_error *
5369 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5370 036813ee 2019-05-09 stsp {
5371 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5372 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5373 ed175427 2019-05-09 stsp
5374 036813ee 2019-05-09 stsp *match = 0;
5375 ed175427 2019-05-09 stsp
5376 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5377 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5378 0f63689d 2019-05-10 stsp return NULL;
5379 036813ee 2019-05-09 stsp }
5380 0b5cc0d6 2019-05-09 stsp
5381 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5382 0f63689d 2019-05-10 stsp if (err)
5383 0f63689d 2019-05-10 stsp return err;
5384 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5385 036813ee 2019-05-09 stsp free(ct_parent_path);
5386 036813ee 2019-05-09 stsp return err;
5387 036813ee 2019-05-09 stsp }
5388 0b5cc0d6 2019-05-09 stsp
5389 768aea60 2019-05-09 stsp static mode_t
5390 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5391 768aea60 2019-05-09 stsp {
5392 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5393 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5394 3d9a4ec4 2020-07-23 stsp
5395 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5396 768aea60 2019-05-09 stsp }
5397 768aea60 2019-05-09 stsp
5398 036813ee 2019-05-09 stsp static const struct got_error *
5399 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5400 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5401 036813ee 2019-05-09 stsp {
5402 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5403 ca2503ea 2019-05-09 stsp
5404 036813ee 2019-05-09 stsp *new_te = NULL;
5405 0b5cc0d6 2019-05-09 stsp
5406 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5407 036813ee 2019-05-09 stsp if (err)
5408 036813ee 2019-05-09 stsp goto done;
5409 0b5cc0d6 2019-05-09 stsp
5410 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5411 036813ee 2019-05-09 stsp
5412 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5413 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5414 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5415 5f8a88c6 2019-08-03 stsp else
5416 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5417 036813ee 2019-05-09 stsp done:
5418 036813ee 2019-05-09 stsp if (err && *new_te) {
5419 56e0773d 2019-11-28 stsp free(*new_te);
5420 036813ee 2019-05-09 stsp *new_te = NULL;
5421 036813ee 2019-05-09 stsp }
5422 036813ee 2019-05-09 stsp return err;
5423 036813ee 2019-05-09 stsp }
5424 036813ee 2019-05-09 stsp
5425 036813ee 2019-05-09 stsp static const struct got_error *
5426 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5427 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5428 036813ee 2019-05-09 stsp {
5429 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5430 102b254e 2020-10-19 stsp char *ct_name = NULL;
5431 036813ee 2019-05-09 stsp
5432 036813ee 2019-05-09 stsp *new_te = NULL;
5433 036813ee 2019-05-09 stsp
5434 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5435 036813ee 2019-05-09 stsp if (*new_te == NULL)
5436 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5437 036813ee 2019-05-09 stsp
5438 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5439 102b254e 2020-10-19 stsp if (err)
5440 036813ee 2019-05-09 stsp goto done;
5441 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5442 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5443 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5444 036813ee 2019-05-09 stsp goto done;
5445 036813ee 2019-05-09 stsp }
5446 036813ee 2019-05-09 stsp
5447 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5448 036813ee 2019-05-09 stsp
5449 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5450 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5451 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5452 5f8a88c6 2019-08-03 stsp else
5453 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5454 036813ee 2019-05-09 stsp done:
5455 102b254e 2020-10-19 stsp free(ct_name);
5456 036813ee 2019-05-09 stsp if (err && *new_te) {
5457 56e0773d 2019-11-28 stsp free(*new_te);
5458 036813ee 2019-05-09 stsp *new_te = NULL;
5459 036813ee 2019-05-09 stsp }
5460 036813ee 2019-05-09 stsp return err;
5461 036813ee 2019-05-09 stsp }
5462 036813ee 2019-05-09 stsp
5463 036813ee 2019-05-09 stsp static const struct got_error *
5464 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5465 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5466 036813ee 2019-05-09 stsp {
5467 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5468 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5469 036813ee 2019-05-09 stsp
5470 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5471 036813ee 2019-05-09 stsp if (err)
5472 036813ee 2019-05-09 stsp return err;
5473 036813ee 2019-05-09 stsp if (new_pe == NULL)
5474 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5475 036813ee 2019-05-09 stsp return NULL;
5476 afa376bf 2019-05-09 stsp }
5477 afa376bf 2019-05-09 stsp
5478 afa376bf 2019-05-09 stsp static const struct got_error *
5479 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5480 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5481 afa376bf 2019-05-09 stsp {
5482 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5483 5f8a88c6 2019-08-03 stsp unsigned char status;
5484 ae1e948a 2021-09-28 thomas
5485 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5486 ae1e948a 2021-09-28 thomas return NULL;
5487 5f8a88c6 2019-08-03 stsp
5488 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5489 afa376bf 2019-05-09 stsp ct_path++;
5490 5f8a88c6 2019-08-03 stsp
5491 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5492 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5493 5f8a88c6 2019-08-03 stsp else
5494 5f8a88c6 2019-08-03 stsp status = ct->status;
5495 5f8a88c6 2019-08-03 stsp
5496 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5497 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5498 036813ee 2019-05-09 stsp }
5499 036813ee 2019-05-09 stsp
5500 036813ee 2019-05-09 stsp static const struct got_error *
5501 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5502 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5503 44d03001 2019-05-09 stsp {
5504 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5505 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5506 44d03001 2019-05-09 stsp char *te_path;
5507 44d03001 2019-05-09 stsp
5508 44d03001 2019-05-09 stsp *modified = 0;
5509 44d03001 2019-05-09 stsp
5510 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5511 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5512 44d03001 2019-05-09 stsp te->name) == -1)
5513 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5514 44d03001 2019-05-09 stsp
5515 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5516 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5517 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5518 44d03001 2019-05-09 stsp strlen(te_path));
5519 62d463ca 2020-10-20 naddy if (*modified)
5520 44d03001 2019-05-09 stsp break;
5521 44d03001 2019-05-09 stsp }
5522 44d03001 2019-05-09 stsp
5523 44d03001 2019-05-09 stsp free(te_path);
5524 44d03001 2019-05-09 stsp return err;
5525 44d03001 2019-05-09 stsp }
5526 44d03001 2019-05-09 stsp
5527 44d03001 2019-05-09 stsp static const struct got_error *
5528 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5529 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5530 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5531 036813ee 2019-05-09 stsp {
5532 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5533 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5534 036813ee 2019-05-09 stsp
5535 036813ee 2019-05-09 stsp *ctp = NULL;
5536 036813ee 2019-05-09 stsp
5537 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5538 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5539 036813ee 2019-05-09 stsp char *ct_name = NULL;
5540 036813ee 2019-05-09 stsp int path_matches;
5541 036813ee 2019-05-09 stsp
5542 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5543 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5544 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5545 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5546 5f8a88c6 2019-08-03 stsp continue;
5547 5f8a88c6 2019-08-03 stsp } else {
5548 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5549 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5550 5f8a88c6 2019-08-03 stsp continue;
5551 5f8a88c6 2019-08-03 stsp }
5552 036813ee 2019-05-09 stsp
5553 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5554 036813ee 2019-05-09 stsp continue;
5555 036813ee 2019-05-09 stsp
5556 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5557 62d463ca 2020-10-20 naddy if (err)
5558 036813ee 2019-05-09 stsp return err;
5559 036813ee 2019-05-09 stsp if (!path_matches)
5560 036813ee 2019-05-09 stsp continue;
5561 036813ee 2019-05-09 stsp
5562 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5563 d34b633e 2020-10-19 stsp if (err)
5564 d34b633e 2020-10-19 stsp return err;
5565 d34b633e 2020-10-19 stsp
5566 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5567 d34b633e 2020-10-19 stsp free(ct_name);
5568 036813ee 2019-05-09 stsp continue;
5569 d34b633e 2020-10-19 stsp }
5570 d34b633e 2020-10-19 stsp free(ct_name);
5571 036813ee 2019-05-09 stsp
5572 036813ee 2019-05-09 stsp *ctp = ct;
5573 036813ee 2019-05-09 stsp break;
5574 036813ee 2019-05-09 stsp }
5575 2b496619 2019-07-10 stsp
5576 2b496619 2019-07-10 stsp return err;
5577 2b496619 2019-07-10 stsp }
5578 2b496619 2019-07-10 stsp
5579 2b496619 2019-07-10 stsp static const struct got_error *
5580 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5581 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5582 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5583 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5584 2b496619 2019-07-10 stsp struct got_repository *repo)
5585 2b496619 2019-07-10 stsp {
5586 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5587 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5588 2b496619 2019-07-10 stsp char *subtree_path;
5589 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5590 ba580f68 2020-03-22 stsp int nentries;
5591 2b496619 2019-07-10 stsp
5592 2b496619 2019-07-10 stsp *new_tep = NULL;
5593 2b496619 2019-07-10 stsp
5594 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5595 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5596 2b496619 2019-07-10 stsp child_path) == -1)
5597 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5598 036813ee 2019-05-09 stsp
5599 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5600 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5601 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5602 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5603 56e0773d 2019-11-28 stsp
5604 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5605 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5606 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5607 2b496619 2019-07-10 stsp goto done;
5608 2b496619 2019-07-10 stsp }
5609 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5610 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5611 2b496619 2019-07-10 stsp if (err) {
5612 56e0773d 2019-11-28 stsp free(new_te);
5613 2b496619 2019-07-10 stsp goto done;
5614 2b496619 2019-07-10 stsp }
5615 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5616 2b496619 2019-07-10 stsp done:
5617 56e0773d 2019-11-28 stsp free(id);
5618 2b496619 2019-07-10 stsp free(subtree_path);
5619 2b496619 2019-07-10 stsp if (err == NULL)
5620 2b496619 2019-07-10 stsp *new_tep = new_te;
5621 036813ee 2019-05-09 stsp return err;
5622 036813ee 2019-05-09 stsp }
5623 036813ee 2019-05-09 stsp
5624 036813ee 2019-05-09 stsp static const struct got_error *
5625 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5626 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5627 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5628 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5629 036813ee 2019-05-09 stsp struct got_repository *repo)
5630 036813ee 2019-05-09 stsp {
5631 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5632 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5633 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5634 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5635 036813ee 2019-05-09 stsp
5636 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5637 ba580f68 2020-03-22 stsp *nentries = 0;
5638 036813ee 2019-05-09 stsp
5639 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5640 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5641 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5642 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5643 036813ee 2019-05-09 stsp
5644 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5645 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5646 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5647 036813ee 2019-05-09 stsp continue;
5648 036813ee 2019-05-09 stsp
5649 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5650 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5651 036813ee 2019-05-09 stsp continue;
5652 036813ee 2019-05-09 stsp
5653 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5654 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5655 036813ee 2019-05-09 stsp if (err)
5656 036813ee 2019-05-09 stsp goto done;
5657 036813ee 2019-05-09 stsp
5658 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5659 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5660 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5661 036813ee 2019-05-09 stsp if (err)
5662 036813ee 2019-05-09 stsp goto done;
5663 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5664 afa376bf 2019-05-09 stsp if (err)
5665 afa376bf 2019-05-09 stsp goto done;
5666 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5667 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5668 2b496619 2019-07-10 stsp if (err)
5669 2f51b5b3 2019-05-09 stsp goto done;
5670 ba580f68 2020-03-22 stsp (*nentries)++;
5671 2b496619 2019-07-10 stsp } else {
5672 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5673 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5674 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5675 2b496619 2019-07-10 stsp == NULL) {
5676 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5677 2b496619 2019-07-10 stsp child_path, path_base_tree,
5678 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5679 2b496619 2019-07-10 stsp repo);
5680 2b496619 2019-07-10 stsp if (err)
5681 2b496619 2019-07-10 stsp goto done;
5682 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5683 2b496619 2019-07-10 stsp if (err)
5684 2b496619 2019-07-10 stsp goto done;
5685 ba580f68 2020-03-22 stsp (*nentries)++;
5686 9ba0479c 2019-05-10 stsp }
5687 ed175427 2019-05-09 stsp }
5688 2f51b5b3 2019-05-09 stsp }
5689 2f51b5b3 2019-05-09 stsp
5690 2f51b5b3 2019-05-09 stsp if (base_tree) {
5691 56e0773d 2019-11-28 stsp int i, nbase_entries;
5692 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5693 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5694 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5695 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5696 63c5ca5d 2019-08-24 stsp
5697 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5698 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5699 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5700 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5701 63c5ca5d 2019-08-24 stsp if (err)
5702 63c5ca5d 2019-08-24 stsp goto done;
5703 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5704 63c5ca5d 2019-08-24 stsp if (err)
5705 63c5ca5d 2019-08-24 stsp goto done;
5706 ba580f68 2020-03-22 stsp (*nentries)++;
5707 63c5ca5d 2019-08-24 stsp continue;
5708 63c5ca5d 2019-08-24 stsp }
5709 2f51b5b3 2019-05-09 stsp
5710 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5711 44d03001 2019-05-09 stsp int modified;
5712 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5713 036813ee 2019-05-09 stsp if (err)
5714 036813ee 2019-05-09 stsp goto done;
5715 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5716 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5717 2f51b5b3 2019-05-09 stsp if (err)
5718 2f51b5b3 2019-05-09 stsp goto done;
5719 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5720 44d03001 2019-05-09 stsp if (modified) {
5721 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5722 ba580f68 2020-03-22 stsp int nsubentries;
5723 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5724 ba580f68 2020-03-22 stsp &nsubentries, te,
5725 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5726 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5727 44d03001 2019-05-09 stsp if (err)
5728 44d03001 2019-05-09 stsp goto done;
5729 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5730 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5731 ba580f68 2020-03-22 stsp free(new_id);
5732 ba580f68 2020-03-22 stsp continue;
5733 ba580f68 2020-03-22 stsp }
5734 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5735 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5736 56e0773d 2019-11-28 stsp free(new_id);
5737 44d03001 2019-05-09 stsp }
5738 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5739 036813ee 2019-05-09 stsp if (err)
5740 036813ee 2019-05-09 stsp goto done;
5741 ba580f68 2020-03-22 stsp (*nentries)++;
5742 2f51b5b3 2019-05-09 stsp continue;
5743 036813ee 2019-05-09 stsp }
5744 2f51b5b3 2019-05-09 stsp
5745 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5746 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5747 f66c734c 2019-09-22 stsp if (err)
5748 f66c734c 2019-09-22 stsp goto done;
5749 2f51b5b3 2019-05-09 stsp if (ct) {
5750 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5751 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5752 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5753 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5754 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5755 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5756 2f51b5b3 2019-05-09 stsp if (err)
5757 2f51b5b3 2019-05-09 stsp goto done;
5758 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5759 2f51b5b3 2019-05-09 stsp if (err)
5760 2f51b5b3 2019-05-09 stsp goto done;
5761 ba580f68 2020-03-22 stsp (*nentries)++;
5762 2f51b5b3 2019-05-09 stsp }
5763 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5764 b416585c 2019-05-13 stsp status_arg);
5765 afa376bf 2019-05-09 stsp if (err)
5766 afa376bf 2019-05-09 stsp goto done;
5767 2f51b5b3 2019-05-09 stsp } else {
5768 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5769 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5770 2f51b5b3 2019-05-09 stsp if (err)
5771 2f51b5b3 2019-05-09 stsp goto done;
5772 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5773 2f51b5b3 2019-05-09 stsp if (err)
5774 2f51b5b3 2019-05-09 stsp goto done;
5775 ba580f68 2020-03-22 stsp (*nentries)++;
5776 2f51b5b3 2019-05-09 stsp }
5777 ca2503ea 2019-05-09 stsp }
5778 ed175427 2019-05-09 stsp }
5779 0b5cc0d6 2019-05-09 stsp
5780 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5781 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5782 ed175427 2019-05-09 stsp done:
5783 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5784 2e1fa222 2020-07-23 stsp return err;
5785 2e1fa222 2020-07-23 stsp }
5786 2e1fa222 2020-07-23 stsp
5787 2e1fa222 2020-07-23 stsp static const struct got_error *
5788 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5789 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5790 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5791 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5792 ebf99748 2019-05-09 stsp {
5793 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5794 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5795 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5796 ebf99748 2019-05-09 stsp
5797 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5798 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5799 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5800 ebf99748 2019-05-09 stsp
5801 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5802 437adc9d 2020-12-10 yzhong
5803 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5804 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5805 437adc9d 2020-12-10 yzhong if (err)
5806 437adc9d 2020-12-10 yzhong goto done;
5807 437adc9d 2020-12-10 yzhong
5808 ebf99748 2019-05-09 stsp if (ie) {
5809 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5810 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5811 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5812 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5813 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5814 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5815 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5816 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5817 437adc9d 2020-12-10 yzhong
5818 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5819 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5820 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5821 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5822 72fd46fa 2019-09-06 stsp !have_staged_files);
5823 ebf99748 2019-05-09 stsp } else
5824 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5825 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5826 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5827 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5828 72fd46fa 2019-09-06 stsp !have_staged_files);
5829 ebf99748 2019-05-09 stsp } else {
5830 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5831 ebf99748 2019-05-09 stsp if (err)
5832 437adc9d 2020-12-10 yzhong goto done;
5833 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5834 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5835 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5836 3969253a 2020-03-07 stsp if (err) {
5837 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5838 437adc9d 2020-12-10 yzhong goto done;
5839 3969253a 2020-03-07 stsp }
5840 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5841 3969253a 2020-03-07 stsp if (err) {
5842 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5843 437adc9d 2020-12-10 yzhong goto done;
5844 3969253a 2020-03-07 stsp }
5845 ebf99748 2019-05-09 stsp }
5846 437adc9d 2020-12-10 yzhong free(relpath);
5847 437adc9d 2020-12-10 yzhong relpath = NULL;
5848 ebf99748 2019-05-09 stsp }
5849 437adc9d 2020-12-10 yzhong done:
5850 437adc9d 2020-12-10 yzhong free(relpath);
5851 d56d26ce 2019-05-10 stsp return err;
5852 d56d26ce 2019-05-10 stsp }
5853 735ef5ac 2019-08-03 stsp
5854 d56d26ce 2019-05-10 stsp
5855 d56d26ce 2019-05-10 stsp static const struct got_error *
5856 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5857 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5858 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5859 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5860 735ef5ac 2019-08-03 stsp int ood_errcode)
5861 d56d26ce 2019-05-10 stsp {
5862 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5863 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5864 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5865 1a36436d 2019-06-10 stsp
5866 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5867 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5868 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5869 1a36436d 2019-06-10 stsp return NULL;
5870 9bead371 2019-07-28 stsp /*
5871 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5872 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5873 9bead371 2019-07-28 stsp */
5874 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5875 945f9229 2022-04-16 thomas if (err)
5876 945f9229 2022-04-16 thomas goto done;
5877 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5878 9bead371 2019-07-28 stsp if (err) {
5879 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5880 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5881 1a36436d 2019-06-10 stsp goto done;
5882 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5883 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5884 1a36436d 2019-06-10 stsp } else {
5885 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5886 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5887 945f9229 2022-04-16 thomas if (err)
5888 945f9229 2022-04-16 thomas goto done;
5889 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5890 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5891 1a36436d 2019-06-10 stsp goto done;
5892 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5893 1a36436d 2019-06-10 stsp }
5894 1a36436d 2019-06-10 stsp done:
5895 1a36436d 2019-06-10 stsp free(id);
5896 945f9229 2022-04-16 thomas if (commit)
5897 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5898 1a36436d 2019-06-10 stsp return err;
5899 ebf99748 2019-05-09 stsp }
5900 ebf99748 2019-05-09 stsp
5901 ef20f542 2022-06-26 thomas static const struct got_error *
5902 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5903 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5904 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5905 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5906 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5907 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5908 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5909 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5910 afa376bf 2019-05-09 stsp struct got_repository *repo)
5911 c4296144 2019-05-09 stsp {
5912 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5913 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5914 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5915 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5916 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5917 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5918 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5919 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5920 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5921 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5922 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5923 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5924 892eab0a 2022-07-22 thomas time_t timestamp;
5925 c4296144 2019-05-09 stsp
5926 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5927 c4296144 2019-05-09 stsp
5928 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5929 675c7539 2019-05-09 stsp
5930 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5931 588edf97 2019-05-10 stsp if (err)
5932 588edf97 2019-05-10 stsp goto done;
5933 de18fc63 2019-05-09 stsp
5934 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5935 588edf97 2019-05-10 stsp if (err)
5936 588edf97 2019-05-10 stsp goto done;
5937 588edf97 2019-05-10 stsp
5938 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5939 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
5940 ef899790 2022-11-01 thomas &logmsg, commit_arg);
5941 33ad4cbe 2019-05-12 jcs if (err)
5942 33ad4cbe 2019-05-12 jcs goto done;
5943 33ad4cbe 2019-05-12 jcs }
5944 c4296144 2019-05-09 stsp
5945 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5946 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5947 33ad4cbe 2019-05-12 jcs goto done;
5948 33ad4cbe 2019-05-12 jcs }
5949 33ad4cbe 2019-05-12 jcs
5950 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5951 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5952 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5953 cf066bf8 2019-05-09 stsp char *ondisk_path;
5954 cf066bf8 2019-05-09 stsp
5955 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5956 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5957 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5958 5f8a88c6 2019-08-03 stsp continue;
5959 5f8a88c6 2019-08-03 stsp
5960 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5961 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5962 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5963 cf066bf8 2019-05-09 stsp continue;
5964 cf066bf8 2019-05-09 stsp
5965 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5966 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5967 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5968 cf066bf8 2019-05-09 stsp goto done;
5969 cf066bf8 2019-05-09 stsp }
5970 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5971 2e1fa222 2020-07-23 stsp free(ondisk_path);
5972 2e1fa222 2020-07-23 stsp if (err)
5973 cf066bf8 2019-05-09 stsp goto done;
5974 cf066bf8 2019-05-09 stsp }
5975 036813ee 2019-05-09 stsp
5976 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5977 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5978 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5979 036813ee 2019-05-09 stsp if (err)
5980 036813ee 2019-05-09 stsp goto done;
5981 036813ee 2019-05-09 stsp
5982 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5983 de18fc63 2019-05-09 stsp if (err)
5984 de18fc63 2019-05-09 stsp goto done;
5985 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5986 10604dce 2021-09-24 thomas nparents++;
5987 10604dce 2021-09-24 thomas if (parent_id2) {
5988 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5989 10604dce 2021-09-24 thomas if (err)
5990 10604dce 2021-09-24 thomas goto done;
5991 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5992 10604dce 2021-09-24 thomas nparents++;
5993 10604dce 2021-09-24 thomas }
5994 892eab0a 2022-07-22 thomas timestamp = time(NULL);
5995 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5996 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
5997 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5998 33ad4cbe 2019-05-12 jcs free(logmsg);
5999 9d40349a 2019-05-09 stsp if (err)
6000 9d40349a 2019-05-09 stsp goto done;
6001 9d40349a 2019-05-09 stsp
6002 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6003 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6004 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6005 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6006 09f5bd90 2019-05-09 stsp goto done;
6007 09f5bd90 2019-05-09 stsp }
6008 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6009 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6010 09f5bd90 2019-05-09 stsp if (err)
6011 09f5bd90 2019-05-09 stsp goto done;
6012 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6013 ebf99748 2019-05-09 stsp if (err)
6014 ebf99748 2019-05-09 stsp goto done;
6015 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6016 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6017 09f5bd90 2019-05-09 stsp goto done;
6018 09f5bd90 2019-05-09 stsp }
6019 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6020 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6021 f2c16586 2019-05-09 stsp if (err)
6022 f2c16586 2019-05-09 stsp goto done;
6023 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6024 f2c16586 2019-05-09 stsp if (err)
6025 f2c16586 2019-05-09 stsp goto done;
6026 f2c16586 2019-05-09 stsp
6027 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6028 09f5bd90 2019-05-09 stsp if (err)
6029 09f5bd90 2019-05-09 stsp goto done;
6030 09f5bd90 2019-05-09 stsp
6031 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6032 ebf99748 2019-05-09 stsp if (err)
6033 ebf99748 2019-05-09 stsp goto done;
6034 c4296144 2019-05-09 stsp done:
6035 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6036 588edf97 2019-05-10 stsp if (head_tree)
6037 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6038 588edf97 2019-05-10 stsp if (head_commit)
6039 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6040 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6041 2f17228e 2019-05-12 stsp if (head_ref2) {
6042 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6043 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6044 2f17228e 2019-05-12 stsp err = unlockerr;
6045 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6046 39cd0ff6 2019-07-12 stsp }
6047 39cd0ff6 2019-07-12 stsp return err;
6048 39cd0ff6 2019-07-12 stsp }
6049 5c1e53bc 2019-07-28 stsp
6050 5c1e53bc 2019-07-28 stsp static const struct got_error *
6051 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6052 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6053 5c1e53bc 2019-07-28 stsp {
6054 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6055 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6056 39cd0ff6 2019-07-12 stsp
6057 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6058 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6059 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6060 5c1e53bc 2019-07-28 stsp
6061 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6062 5c1e53bc 2019-07-28 stsp ct_path++;
6063 5c1e53bc 2019-07-28 stsp
6064 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6065 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6066 5c1e53bc 2019-07-28 stsp break;
6067 5c1e53bc 2019-07-28 stsp }
6068 5c1e53bc 2019-07-28 stsp
6069 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6070 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6071 5c1e53bc 2019-07-28 stsp
6072 5c1e53bc 2019-07-28 stsp return NULL;
6073 5c1e53bc 2019-07-28 stsp }
6074 5c1e53bc 2019-07-28 stsp
6075 f0b75401 2019-08-03 stsp static const struct got_error *
6076 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6077 f0b75401 2019-08-03 stsp {
6078 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6079 f0b75401 2019-08-03 stsp
6080 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6081 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6082 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6083 f0b75401 2019-08-03 stsp }
6084 f0b75401 2019-08-03 stsp
6085 f0b75401 2019-08-03 stsp return NULL;
6086 f0b75401 2019-08-03 stsp }
6087 f0b75401 2019-08-03 stsp
6088 5f8a88c6 2019-08-03 stsp static const struct got_error *
6089 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6090 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6091 5f8a88c6 2019-08-03 stsp {
6092 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6093 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6094 5f8a88c6 2019-08-03 stsp
6095 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6096 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6097 5f8a88c6 2019-08-03 stsp continue;
6098 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6099 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6100 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6101 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6102 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6103 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6104 5f8a88c6 2019-08-03 stsp }
6105 5f8a88c6 2019-08-03 stsp
6106 5f8a88c6 2019-08-03 stsp return NULL;
6107 5f8a88c6 2019-08-03 stsp }
6108 5f8a88c6 2019-08-03 stsp
6109 39cd0ff6 2019-07-12 stsp const struct got_error *
6110 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6111 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6112 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6113 ef899790 2022-11-01 thomas int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6114 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6115 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6116 39cd0ff6 2019-07-12 stsp {
6117 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6118 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6119 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6120 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6121 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6122 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6123 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6124 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6125 ef899790 2022-11-01 thomas char *diff_path = NULL;
6126 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6127 39cd0ff6 2019-07-12 stsp
6128 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6129 39cd0ff6 2019-07-12 stsp
6130 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6131 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6132 39cd0ff6 2019-07-12 stsp
6133 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6134 39cd0ff6 2019-07-12 stsp if (err)
6135 39cd0ff6 2019-07-12 stsp goto done;
6136 39cd0ff6 2019-07-12 stsp
6137 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6138 39cd0ff6 2019-07-12 stsp if (err)
6139 39cd0ff6 2019-07-12 stsp goto done;
6140 39cd0ff6 2019-07-12 stsp
6141 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6142 39cd0ff6 2019-07-12 stsp if (err)
6143 39cd0ff6 2019-07-12 stsp goto done;
6144 39cd0ff6 2019-07-12 stsp
6145 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6146 39cd0ff6 2019-07-12 stsp if (err)
6147 39cd0ff6 2019-07-12 stsp goto done;
6148 39cd0ff6 2019-07-12 stsp
6149 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6150 5f8a88c6 2019-08-03 stsp &have_staged_files);
6151 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6152 5f8a88c6 2019-08-03 stsp goto done;
6153 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6154 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6155 5f8a88c6 2019-08-03 stsp if (err)
6156 5f8a88c6 2019-08-03 stsp goto done;
6157 5f8a88c6 2019-08-03 stsp }
6158 5f8a88c6 2019-08-03 stsp
6159 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6160 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6161 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6162 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6163 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6164 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6165 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6166 ef899790 2022-11-01 thomas if (show_diff) {
6167 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6168 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6169 ef899790 2022-11-01 thomas if (err)
6170 ef899790 2022-11-01 thomas goto done;
6171 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6172 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6173 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6174 ef899790 2022-11-01 thomas goto done;
6175 ef899790 2022-11-01 thomas }
6176 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6177 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6178 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6179 ef899790 2022-11-01 thomas goto done;
6180 ef899790 2022-11-01 thomas }
6181 ef899790 2022-11-01 thomas }
6182 ef899790 2022-11-01 thomas
6183 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6184 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6185 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6186 5c1e53bc 2019-07-28 stsp if (err)
6187 5c1e53bc 2019-07-28 stsp goto done;
6188 5c1e53bc 2019-07-28 stsp }
6189 39cd0ff6 2019-07-12 stsp
6190 ef899790 2022-11-01 thomas if (show_diff) {
6191 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6192 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6193 ef899790 2022-11-01 thomas goto done;
6194 ef899790 2022-11-01 thomas }
6195 ef899790 2022-11-01 thomas }
6196 ef899790 2022-11-01 thomas
6197 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6198 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6199 39cd0ff6 2019-07-12 stsp goto done;
6200 5c1e53bc 2019-07-28 stsp }
6201 5c1e53bc 2019-07-28 stsp
6202 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6203 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6204 5c1e53bc 2019-07-28 stsp if (err)
6205 5c1e53bc 2019-07-28 stsp goto done;
6206 39cd0ff6 2019-07-12 stsp }
6207 f0b75401 2019-08-03 stsp
6208 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6209 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6210 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6211 f0b75401 2019-08-03 stsp
6212 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6213 f0b75401 2019-08-03 stsp ct_path++;
6214 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6215 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6216 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6217 b50cabdf 2019-07-12 stsp if (err)
6218 b50cabdf 2019-07-12 stsp goto done;
6219 f0b75401 2019-08-03 stsp
6220 b50cabdf 2019-07-12 stsp }
6221 b50cabdf 2019-07-12 stsp
6222 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6223 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6224 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6225 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6226 39cd0ff6 2019-07-12 stsp if (err)
6227 39cd0ff6 2019-07-12 stsp goto done;
6228 39cd0ff6 2019-07-12 stsp
6229 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6230 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6231 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6232 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6233 39cd0ff6 2019-07-12 stsp err = sync_err;
6234 39cd0ff6 2019-07-12 stsp done:
6235 39cd0ff6 2019-07-12 stsp if (fileindex)
6236 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6237 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6238 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6239 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6240 39cd0ff6 2019-07-12 stsp err = unlockerr;
6241 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6242 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6243 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6244 39cd0ff6 2019-07-12 stsp }
6245 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
6246 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6247 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6248 ef899790 2022-11-01 thomas free(diff_path);
6249 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6250 ef899790 2022-11-01 thomas err == NULL)
6251 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6252 c4296144 2019-05-09 stsp return err;
6253 8656d6c4 2019-05-20 stsp }
6254 8656d6c4 2019-05-20 stsp
6255 8656d6c4 2019-05-20 stsp const char *
6256 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6257 8656d6c4 2019-05-20 stsp {
6258 8656d6c4 2019-05-20 stsp return ct->path;
6259 8656d6c4 2019-05-20 stsp }
6260 8656d6c4 2019-05-20 stsp
6261 8656d6c4 2019-05-20 stsp unsigned int
6262 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6263 8656d6c4 2019-05-20 stsp {
6264 8656d6c4 2019-05-20 stsp return ct->status;
6265 818c7501 2019-07-11 stsp }
6266 818c7501 2019-07-11 stsp
6267 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6268 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6269 818c7501 2019-07-11 stsp struct got_repository *repo;
6270 818c7501 2019-07-11 stsp };
6271 818c7501 2019-07-11 stsp
6272 818c7501 2019-07-11 stsp static const struct got_error *
6273 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6274 818c7501 2019-07-11 stsp {
6275 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6276 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6277 818c7501 2019-07-11 stsp unsigned char status;
6278 818c7501 2019-07-11 stsp struct stat sb;
6279 818c7501 2019-07-11 stsp char *ondisk_path;
6280 818c7501 2019-07-11 stsp
6281 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6282 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6283 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6284 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6285 818c7501 2019-07-11 stsp
6286 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6287 818c7501 2019-07-11 stsp == -1)
6288 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6289 818c7501 2019-07-11 stsp
6290 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6291 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6292 818c7501 2019-07-11 stsp free(ondisk_path);
6293 818c7501 2019-07-11 stsp if (err)
6294 818c7501 2019-07-11 stsp return err;
6295 818c7501 2019-07-11 stsp
6296 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6297 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6298 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6299 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6300 818c7501 2019-07-11 stsp
6301 818c7501 2019-07-11 stsp return NULL;
6302 818c7501 2019-07-11 stsp }
6303 818c7501 2019-07-11 stsp
6304 818c7501 2019-07-11 stsp const struct got_error *
6305 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6306 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6307 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6308 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6309 818c7501 2019-07-11 stsp {
6310 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6311 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6312 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6313 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6314 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6315 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6316 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6317 818c7501 2019-07-11 stsp
6318 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6319 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6320 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6321 818c7501 2019-07-11 stsp
6322 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6323 818c7501 2019-07-11 stsp if (err)
6324 818c7501 2019-07-11 stsp return err;
6325 818c7501 2019-07-11 stsp
6326 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6327 818c7501 2019-07-11 stsp if (err)
6328 818c7501 2019-07-11 stsp goto done;
6329 818c7501 2019-07-11 stsp
6330 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6331 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6332 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6333 818c7501 2019-07-11 stsp &ok_arg);
6334 818c7501 2019-07-11 stsp if (err)
6335 818c7501 2019-07-11 stsp goto done;
6336 818c7501 2019-07-11 stsp
6337 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6338 818c7501 2019-07-11 stsp if (err)
6339 818c7501 2019-07-11 stsp goto done;
6340 818c7501 2019-07-11 stsp
6341 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6342 818c7501 2019-07-11 stsp if (err)
6343 818c7501 2019-07-11 stsp goto done;
6344 818c7501 2019-07-11 stsp
6345 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6346 818c7501 2019-07-11 stsp if (err)
6347 818c7501 2019-07-11 stsp goto done;
6348 818c7501 2019-07-11 stsp
6349 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6350 818c7501 2019-07-11 stsp 0);
6351 e51d7b55 2020-01-04 stsp if (err)
6352 e51d7b55 2020-01-04 stsp goto done;
6353 e51d7b55 2020-01-04 stsp
6354 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6355 818c7501 2019-07-11 stsp if (err)
6356 818c7501 2019-07-11 stsp goto done;
6357 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6358 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6359 e51d7b55 2020-01-04 stsp goto done;
6360 e51d7b55 2020-01-04 stsp }
6361 818c7501 2019-07-11 stsp
6362 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6363 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6364 818c7501 2019-07-11 stsp if (err)
6365 818c7501 2019-07-11 stsp goto done;
6366 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6367 818c7501 2019-07-11 stsp if (err)
6368 818c7501 2019-07-11 stsp goto done;
6369 818c7501 2019-07-11 stsp
6370 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6371 818c7501 2019-07-11 stsp
6372 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6373 818c7501 2019-07-11 stsp if (err)
6374 818c7501 2019-07-11 stsp goto done;
6375 818c7501 2019-07-11 stsp
6376 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6377 818c7501 2019-07-11 stsp if (err)
6378 818c7501 2019-07-11 stsp goto done;
6379 818c7501 2019-07-11 stsp
6380 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6381 818c7501 2019-07-11 stsp worktree->base_commit_id);
6382 818c7501 2019-07-11 stsp if (err)
6383 818c7501 2019-07-11 stsp goto done;
6384 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6385 818c7501 2019-07-11 stsp if (err)
6386 818c7501 2019-07-11 stsp goto done;
6387 818c7501 2019-07-11 stsp
6388 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6389 818c7501 2019-07-11 stsp if (err)
6390 818c7501 2019-07-11 stsp goto done;
6391 818c7501 2019-07-11 stsp done:
6392 818c7501 2019-07-11 stsp free(fileindex_path);
6393 818c7501 2019-07-11 stsp free(tmp_branch_name);
6394 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6395 818c7501 2019-07-11 stsp free(branch_ref_name);
6396 818c7501 2019-07-11 stsp if (branch_ref)
6397 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6398 818c7501 2019-07-11 stsp if (wt_branch)
6399 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6400 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6401 818c7501 2019-07-11 stsp if (err) {
6402 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6403 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6404 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6405 818c7501 2019-07-11 stsp }
6406 818c7501 2019-07-11 stsp if (*tmp_branch) {
6407 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6408 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6409 818c7501 2019-07-11 stsp }
6410 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6411 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6412 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6413 3e3a69f1 2019-07-25 stsp }
6414 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6415 818c7501 2019-07-11 stsp }
6416 818c7501 2019-07-11 stsp return err;
6417 818c7501 2019-07-11 stsp }
6418 818c7501 2019-07-11 stsp
6419 818c7501 2019-07-11 stsp const struct got_error *
6420 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6421 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6422 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6423 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6424 818c7501 2019-07-11 stsp {
6425 818c7501 2019-07-11 stsp const struct got_error *err;
6426 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6427 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6428 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6429 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6430 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6431 818c7501 2019-07-11 stsp
6432 818c7501 2019-07-11 stsp *commit_id = NULL;
6433 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6434 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6435 3e3a69f1 2019-07-25 stsp *branch = NULL;
6436 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6437 3e3a69f1 2019-07-25 stsp
6438 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6439 3e3a69f1 2019-07-25 stsp if (err)
6440 3e3a69f1 2019-07-25 stsp return err;
6441 818c7501 2019-07-11 stsp
6442 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6443 3e3a69f1 2019-07-25 stsp if (err)
6444 f032f1f7 2019-08-04 stsp goto done;
6445 f032f1f7 2019-08-04 stsp
6446 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6447 f032f1f7 2019-08-04 stsp &have_staged_files);
6448 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6449 f032f1f7 2019-08-04 stsp goto done;
6450 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6451 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6452 3e3a69f1 2019-07-25 stsp goto done;
6453 f032f1f7 2019-08-04 stsp }
6454 3e3a69f1 2019-07-25 stsp
6455 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6456 818c7501 2019-07-11 stsp if (err)
6457 f032f1f7 2019-08-04 stsp goto done;
6458 818c7501 2019-07-11 stsp
6459 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6460 818c7501 2019-07-11 stsp if (err)
6461 818c7501 2019-07-11 stsp goto done;
6462 818c7501 2019-07-11 stsp
6463 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6464 818c7501 2019-07-11 stsp if (err)
6465 818c7501 2019-07-11 stsp goto done;
6466 818c7501 2019-07-11 stsp
6467 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6468 818c7501 2019-07-11 stsp if (err)
6469 818c7501 2019-07-11 stsp goto done;
6470 818c7501 2019-07-11 stsp
6471 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6472 818c7501 2019-07-11 stsp if (err)
6473 818c7501 2019-07-11 stsp goto done;
6474 818c7501 2019-07-11 stsp
6475 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6476 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
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_ref_open(&commit_ref, repo, commit_ref_name, 0);
6481 818c7501 2019-07-11 stsp if (err)
6482 818c7501 2019-07-11 stsp goto done;
6483 818c7501 2019-07-11 stsp
6484 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6485 818c7501 2019-07-11 stsp if (err)
6486 818c7501 2019-07-11 stsp goto done;
6487 818c7501 2019-07-11 stsp
6488 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6489 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6490 818c7501 2019-07-11 stsp if (err)
6491 818c7501 2019-07-11 stsp goto done;
6492 818c7501 2019-07-11 stsp
6493 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6494 818c7501 2019-07-11 stsp if (err)
6495 818c7501 2019-07-11 stsp goto done;
6496 818c7501 2019-07-11 stsp done:
6497 818c7501 2019-07-11 stsp free(commit_ref_name);
6498 818c7501 2019-07-11 stsp free(branch_ref_name);
6499 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6500 818c7501 2019-07-11 stsp if (commit_ref)
6501 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6502 818c7501 2019-07-11 stsp if (branch_ref)
6503 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6504 818c7501 2019-07-11 stsp if (err) {
6505 818c7501 2019-07-11 stsp free(*commit_id);
6506 818c7501 2019-07-11 stsp *commit_id = NULL;
6507 818c7501 2019-07-11 stsp if (*tmp_branch) {
6508 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6509 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6510 818c7501 2019-07-11 stsp }
6511 818c7501 2019-07-11 stsp if (*new_base_branch) {
6512 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6513 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6514 818c7501 2019-07-11 stsp }
6515 818c7501 2019-07-11 stsp if (*branch) {
6516 818c7501 2019-07-11 stsp got_ref_close(*branch);
6517 818c7501 2019-07-11 stsp *branch = NULL;
6518 818c7501 2019-07-11 stsp }
6519 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6520 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6521 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6522 3e3a69f1 2019-07-25 stsp }
6523 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6524 818c7501 2019-07-11 stsp }
6525 818c7501 2019-07-11 stsp return err;
6526 c4296144 2019-05-09 stsp }
6527 818c7501 2019-07-11 stsp
6528 818c7501 2019-07-11 stsp const struct got_error *
6529 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6530 818c7501 2019-07-11 stsp {
6531 818c7501 2019-07-11 stsp const struct got_error *err;
6532 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6533 818c7501 2019-07-11 stsp
6534 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6535 818c7501 2019-07-11 stsp if (err)
6536 818c7501 2019-07-11 stsp return err;
6537 818c7501 2019-07-11 stsp
6538 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6539 818c7501 2019-07-11 stsp free(tmp_branch_name);
6540 818c7501 2019-07-11 stsp return NULL;
6541 818c7501 2019-07-11 stsp }
6542 818c7501 2019-07-11 stsp
6543 818c7501 2019-07-11 stsp static const struct got_error *
6544 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6545 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6546 818c7501 2019-07-11 stsp {
6547 0ebf8283 2019-07-24 stsp *logmsg = arg;
6548 818c7501 2019-07-11 stsp return NULL;
6549 818c7501 2019-07-11 stsp }
6550 818c7501 2019-07-11 stsp
6551 818c7501 2019-07-11 stsp static const struct got_error *
6552 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6553 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6554 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6555 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6556 818c7501 2019-07-11 stsp {
6557 818c7501 2019-07-11 stsp return NULL;
6558 01757395 2019-07-12 stsp }
6559 01757395 2019-07-12 stsp
6560 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6561 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6562 01757395 2019-07-12 stsp void *progress_arg;
6563 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6564 01757395 2019-07-12 stsp };
6565 01757395 2019-07-12 stsp
6566 01757395 2019-07-12 stsp static const struct got_error *
6567 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6568 01757395 2019-07-12 stsp {
6569 01757395 2019-07-12 stsp const struct got_error *err;
6570 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6571 01757395 2019-07-12 stsp char *p;
6572 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6573 01757395 2019-07-12 stsp
6574 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6575 01757395 2019-07-12 stsp if (err)
6576 01757395 2019-07-12 stsp return err;
6577 01757395 2019-07-12 stsp
6578 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6579 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6580 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6581 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6582 01757395 2019-07-12 stsp return NULL;
6583 01757395 2019-07-12 stsp
6584 01757395 2019-07-12 stsp p = strdup(path);
6585 01757395 2019-07-12 stsp if (p == NULL)
6586 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6587 01757395 2019-07-12 stsp
6588 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6589 01757395 2019-07-12 stsp if (err || new == NULL)
6590 01757395 2019-07-12 stsp free(p);
6591 01757395 2019-07-12 stsp return err;
6592 01757395 2019-07-12 stsp }
6593 01757395 2019-07-12 stsp
6594 01757395 2019-07-12 stsp void
6595 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6596 01757395 2019-07-12 stsp {
6597 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6598 01757395 2019-07-12 stsp
6599 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6600 01757395 2019-07-12 stsp free((char *)pe->path);
6601 01757395 2019-07-12 stsp
6602 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6603 818c7501 2019-07-11 stsp }
6604 818c7501 2019-07-11 stsp
6605 0ebf8283 2019-07-24 stsp static const struct got_error *
6606 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6607 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6608 818c7501 2019-07-11 stsp {
6609 818c7501 2019-07-11 stsp const struct got_error *err;
6610 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6611 818c7501 2019-07-11 stsp
6612 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6613 818c7501 2019-07-11 stsp if (err) {
6614 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6615 818c7501 2019-07-11 stsp goto done;
6616 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6617 818c7501 2019-07-11 stsp if (err)
6618 818c7501 2019-07-11 stsp goto done;
6619 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6620 818c7501 2019-07-11 stsp if (err)
6621 818c7501 2019-07-11 stsp goto done;
6622 de05890f 2020-03-05 stsp } else if (is_rebase) {
6623 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6624 818c7501 2019-07-11 stsp int cmp;
6625 818c7501 2019-07-11 stsp
6626 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6627 818c7501 2019-07-11 stsp if (err)
6628 818c7501 2019-07-11 stsp goto done;
6629 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6630 818c7501 2019-07-11 stsp free(stored_id);
6631 818c7501 2019-07-11 stsp if (cmp != 0) {
6632 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6633 818c7501 2019-07-11 stsp goto done;
6634 818c7501 2019-07-11 stsp }
6635 818c7501 2019-07-11 stsp }
6636 0ebf8283 2019-07-24 stsp done:
6637 0ebf8283 2019-07-24 stsp if (commit_ref)
6638 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6639 0ebf8283 2019-07-24 stsp return err;
6640 0ebf8283 2019-07-24 stsp }
6641 0ebf8283 2019-07-24 stsp
6642 0ebf8283 2019-07-24 stsp static const struct got_error *
6643 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6644 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6645 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6646 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6647 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6648 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6649 0ebf8283 2019-07-24 stsp {
6650 0ebf8283 2019-07-24 stsp const struct got_error *err;
6651 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6652 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6653 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6654 818c7501 2019-07-11 stsp
6655 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6656 0ebf8283 2019-07-24 stsp
6657 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6658 0ebf8283 2019-07-24 stsp if (err)
6659 0ebf8283 2019-07-24 stsp return err;
6660 0ebf8283 2019-07-24 stsp
6661 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6662 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6663 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6664 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6665 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6666 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6667 818c7501 2019-07-11 stsp if (commit_ref)
6668 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6669 818c7501 2019-07-11 stsp return err;
6670 818c7501 2019-07-11 stsp }
6671 818c7501 2019-07-11 stsp
6672 818c7501 2019-07-11 stsp const struct got_error *
6673 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6674 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6675 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6676 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6677 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6678 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6679 818c7501 2019-07-11 stsp {
6680 0ebf8283 2019-07-24 stsp const struct got_error *err;
6681 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6682 0ebf8283 2019-07-24 stsp
6683 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6684 0ebf8283 2019-07-24 stsp if (err)
6685 0ebf8283 2019-07-24 stsp return err;
6686 0ebf8283 2019-07-24 stsp
6687 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6688 0ebf8283 2019-07-24 stsp if (err)
6689 0ebf8283 2019-07-24 stsp goto done;
6690 0ebf8283 2019-07-24 stsp
6691 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6692 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6693 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6694 0ebf8283 2019-07-24 stsp done:
6695 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6696 0ebf8283 2019-07-24 stsp return err;
6697 0ebf8283 2019-07-24 stsp }
6698 0ebf8283 2019-07-24 stsp
6699 0ebf8283 2019-07-24 stsp const struct got_error *
6700 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6701 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6702 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6703 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6704 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6705 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6706 0ebf8283 2019-07-24 stsp {
6707 0ebf8283 2019-07-24 stsp const struct got_error *err;
6708 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6709 0ebf8283 2019-07-24 stsp
6710 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6711 0ebf8283 2019-07-24 stsp if (err)
6712 0ebf8283 2019-07-24 stsp return err;
6713 0ebf8283 2019-07-24 stsp
6714 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6715 0ebf8283 2019-07-24 stsp if (err)
6716 0ebf8283 2019-07-24 stsp goto done;
6717 0ebf8283 2019-07-24 stsp
6718 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6719 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6720 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6721 0ebf8283 2019-07-24 stsp done:
6722 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6723 0ebf8283 2019-07-24 stsp return err;
6724 0ebf8283 2019-07-24 stsp }
6725 0ebf8283 2019-07-24 stsp
6726 0ebf8283 2019-07-24 stsp static const struct got_error *
6727 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6728 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6729 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6730 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6731 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6732 150d7275 2022-07-22 thomas struct got_repository *repo)
6733 0ebf8283 2019-07-24 stsp {
6734 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6735 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6736 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6737 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6738 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6739 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6740 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6741 818c7501 2019-07-11 stsp
6742 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6743 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6744 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6745 a0e95631 2019-07-12 stsp
6746 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6747 818c7501 2019-07-11 stsp
6748 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6749 a0e95631 2019-07-12 stsp if (err)
6750 3e3a69f1 2019-07-25 stsp return err;
6751 a0e95631 2019-07-12 stsp
6752 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6753 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6754 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6755 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6756 01757395 2019-07-12 stsp /*
6757 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6758 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6759 6c13b005 2021-09-02 stsp *
6760 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6761 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6762 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6763 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6764 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6765 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6766 01757395 2019-07-12 stsp */
6767 01757395 2019-07-12 stsp if (merged_paths) {
6768 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6769 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6770 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6771 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6772 f2a9dc41 2019-12-13 tracey 0);
6773 01757395 2019-07-12 stsp if (err)
6774 01757395 2019-07-12 stsp goto done;
6775 01757395 2019-07-12 stsp }
6776 01757395 2019-07-12 stsp } else {
6777 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6778 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6779 01757395 2019-07-12 stsp if (err)
6780 01757395 2019-07-12 stsp goto done;
6781 01757395 2019-07-12 stsp }
6782 a0e95631 2019-07-12 stsp
6783 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6784 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6785 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6786 a0e95631 2019-07-12 stsp if (err)
6787 a0e95631 2019-07-12 stsp goto done;
6788 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6789 a0e95631 2019-07-12 stsp goto done;
6790 ff0d2220 2019-07-11 stsp }
6791 818c7501 2019-07-11 stsp
6792 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6793 edd02c5e 2019-07-11 stsp if (err)
6794 edd02c5e 2019-07-11 stsp goto done;
6795 edd02c5e 2019-07-11 stsp
6796 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6797 818c7501 2019-07-11 stsp if (err)
6798 818c7501 2019-07-11 stsp goto done;
6799 818c7501 2019-07-11 stsp
6800 5943eee2 2019-08-13 stsp if (new_logmsg) {
6801 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6802 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6803 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6804 5943eee2 2019-08-13 stsp goto done;
6805 5943eee2 2019-08-13 stsp }
6806 5943eee2 2019-08-13 stsp } else {
6807 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6808 5943eee2 2019-08-13 stsp if (err)
6809 5943eee2 2019-08-13 stsp goto done;
6810 5943eee2 2019-08-13 stsp }
6811 0ebf8283 2019-07-24 stsp
6812 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6813 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6814 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6815 8db00f97 2022-07-22 thomas committer ? committer :
6816 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6817 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6818 a0e95631 2019-07-12 stsp if (err)
6819 a0e95631 2019-07-12 stsp goto done;
6820 a0e95631 2019-07-12 stsp
6821 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6822 a0e95631 2019-07-12 stsp if (err)
6823 a0e95631 2019-07-12 stsp goto done;
6824 a0e95631 2019-07-12 stsp
6825 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6826 a0e95631 2019-07-12 stsp if (err)
6827 a0e95631 2019-07-12 stsp goto done;
6828 a0e95631 2019-07-12 stsp
6829 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6830 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6831 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6832 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6833 a0e95631 2019-07-12 stsp err = sync_err;
6834 818c7501 2019-07-11 stsp done:
6835 a0e95631 2019-07-12 stsp free(fileindex_path);
6836 a0e95631 2019-07-12 stsp free(head_commit_id);
6837 a0e95631 2019-07-12 stsp if (head_ref)
6838 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6839 818c7501 2019-07-11 stsp if (err) {
6840 818c7501 2019-07-11 stsp free(*new_commit_id);
6841 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6842 818c7501 2019-07-11 stsp }
6843 818c7501 2019-07-11 stsp return err;
6844 818c7501 2019-07-11 stsp }
6845 818c7501 2019-07-11 stsp
6846 818c7501 2019-07-11 stsp const struct got_error *
6847 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6848 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6849 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6850 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6851 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6852 0ebf8283 2019-07-24 stsp {
6853 0ebf8283 2019-07-24 stsp const struct got_error *err;
6854 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6855 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6856 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6857 0ebf8283 2019-07-24 stsp
6858 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6859 0ebf8283 2019-07-24 stsp if (err)
6860 0ebf8283 2019-07-24 stsp return err;
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6863 0ebf8283 2019-07-24 stsp if (err)
6864 0ebf8283 2019-07-24 stsp goto done;
6865 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6866 0ebf8283 2019-07-24 stsp if (err)
6867 0ebf8283 2019-07-24 stsp goto done;
6868 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6869 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6870 0ebf8283 2019-07-24 stsp goto done;
6871 0ebf8283 2019-07-24 stsp }
6872 0ebf8283 2019-07-24 stsp
6873 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6874 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6875 150d7275 2022-07-22 thomas NULL, repo);
6876 0ebf8283 2019-07-24 stsp done:
6877 0ebf8283 2019-07-24 stsp if (commit_ref)
6878 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6879 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6880 0ebf8283 2019-07-24 stsp free(commit_id);
6881 0ebf8283 2019-07-24 stsp return err;
6882 0ebf8283 2019-07-24 stsp }
6883 0ebf8283 2019-07-24 stsp
6884 0ebf8283 2019-07-24 stsp const struct got_error *
6885 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6886 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6887 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6888 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6889 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6890 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6891 0ebf8283 2019-07-24 stsp {
6892 0ebf8283 2019-07-24 stsp const struct got_error *err;
6893 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6894 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6895 0ebf8283 2019-07-24 stsp
6896 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6897 0ebf8283 2019-07-24 stsp if (err)
6898 0ebf8283 2019-07-24 stsp return err;
6899 0ebf8283 2019-07-24 stsp
6900 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6901 0ebf8283 2019-07-24 stsp if (err)
6902 0ebf8283 2019-07-24 stsp goto done;
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6905 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6906 150d7275 2022-07-22 thomas new_logmsg, repo);
6907 0ebf8283 2019-07-24 stsp done:
6908 0ebf8283 2019-07-24 stsp if (commit_ref)
6909 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6910 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6911 0ebf8283 2019-07-24 stsp return err;
6912 0ebf8283 2019-07-24 stsp }
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp const struct got_error *
6915 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6916 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6917 818c7501 2019-07-11 stsp {
6918 3e3a69f1 2019-07-25 stsp if (fileindex)
6919 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6920 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6921 69844fba 2019-07-11 stsp }
6922 69844fba 2019-07-11 stsp
6923 69844fba 2019-07-11 stsp static const struct got_error *
6924 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6925 69844fba 2019-07-11 stsp {
6926 69844fba 2019-07-11 stsp const struct got_error *err;
6927 69844fba 2019-07-11 stsp struct got_reference *ref;
6928 69844fba 2019-07-11 stsp
6929 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6930 69844fba 2019-07-11 stsp if (err) {
6931 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6932 69844fba 2019-07-11 stsp return NULL;
6933 69844fba 2019-07-11 stsp return err;
6934 69844fba 2019-07-11 stsp }
6935 69844fba 2019-07-11 stsp
6936 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6937 69844fba 2019-07-11 stsp got_ref_close(ref);
6938 69844fba 2019-07-11 stsp return err;
6939 818c7501 2019-07-11 stsp }
6940 818c7501 2019-07-11 stsp
6941 69844fba 2019-07-11 stsp static const struct got_error *
6942 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6943 69844fba 2019-07-11 stsp {
6944 69844fba 2019-07-11 stsp const struct got_error *err;
6945 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6946 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6947 69844fba 2019-07-11 stsp
6948 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6949 69844fba 2019-07-11 stsp if (err)
6950 69844fba 2019-07-11 stsp goto done;
6951 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6952 69844fba 2019-07-11 stsp if (err)
6953 69844fba 2019-07-11 stsp goto done;
6954 69844fba 2019-07-11 stsp
6955 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6956 69844fba 2019-07-11 stsp if (err)
6957 69844fba 2019-07-11 stsp goto done;
6958 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6959 69844fba 2019-07-11 stsp if (err)
6960 69844fba 2019-07-11 stsp goto done;
6961 69844fba 2019-07-11 stsp
6962 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6963 69844fba 2019-07-11 stsp if (err)
6964 69844fba 2019-07-11 stsp goto done;
6965 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6966 69844fba 2019-07-11 stsp if (err)
6967 69844fba 2019-07-11 stsp goto done;
6968 69844fba 2019-07-11 stsp
6969 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6970 69844fba 2019-07-11 stsp if (err)
6971 69844fba 2019-07-11 stsp goto done;
6972 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6973 69844fba 2019-07-11 stsp if (err)
6974 69844fba 2019-07-11 stsp goto done;
6975 69844fba 2019-07-11 stsp
6976 69844fba 2019-07-11 stsp done:
6977 69844fba 2019-07-11 stsp free(tmp_branch_name);
6978 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6979 69844fba 2019-07-11 stsp free(branch_ref_name);
6980 69844fba 2019-07-11 stsp free(commit_ref_name);
6981 e600f124 2021-03-21 stsp return err;
6982 e600f124 2021-03-21 stsp }
6983 e600f124 2021-03-21 stsp
6984 ef20f542 2022-06-26 thomas static const struct got_error *
6985 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6986 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6987 e600f124 2021-03-21 stsp {
6988 e600f124 2021-03-21 stsp const struct got_error *err;
6989 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6990 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6991 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6992 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6993 e600f124 2021-03-21 stsp char *refname = NULL;
6994 e600f124 2021-03-21 stsp
6995 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6996 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6997 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6998 e600f124 2021-03-21 stsp branch_name += 11;
6999 e600f124 2021-03-21 stsp
7000 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7001 e600f124 2021-03-21 stsp if (err)
7002 e600f124 2021-03-21 stsp return err;
7003 e600f124 2021-03-21 stsp
7004 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7005 e600f124 2021-03-21 stsp new_id_str) == -1) {
7006 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7007 e600f124 2021-03-21 stsp goto done;
7008 e600f124 2021-03-21 stsp }
7009 e600f124 2021-03-21 stsp
7010 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7011 e600f124 2021-03-21 stsp if (err)
7012 e600f124 2021-03-21 stsp goto done;
7013 e600f124 2021-03-21 stsp
7014 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7015 e600f124 2021-03-21 stsp if (err)
7016 e600f124 2021-03-21 stsp goto done;
7017 e600f124 2021-03-21 stsp
7018 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7019 e600f124 2021-03-21 stsp done:
7020 e600f124 2021-03-21 stsp free(new_id_str);
7021 e600f124 2021-03-21 stsp free(refname);
7022 e600f124 2021-03-21 stsp free(old_commit_id);
7023 e600f124 2021-03-21 stsp if (ref)
7024 e600f124 2021-03-21 stsp got_ref_close(ref);
7025 69844fba 2019-07-11 stsp return err;
7026 69844fba 2019-07-11 stsp }
7027 69844fba 2019-07-11 stsp
7028 818c7501 2019-07-11 stsp const struct got_error *
7029 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7030 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7031 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7032 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
7033 818c7501 2019-07-11 stsp {
7034 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7035 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7036 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7037 818c7501 2019-07-11 stsp
7038 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7039 818c7501 2019-07-11 stsp if (err)
7040 818c7501 2019-07-11 stsp return err;
7041 e600f124 2021-03-21 stsp
7042 e600f124 2021-03-21 stsp if (create_backup) {
7043 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7044 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7045 e600f124 2021-03-21 stsp if (err)
7046 e600f124 2021-03-21 stsp goto done;
7047 e600f124 2021-03-21 stsp }
7048 818c7501 2019-07-11 stsp
7049 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7050 818c7501 2019-07-11 stsp if (err)
7051 818c7501 2019-07-11 stsp goto done;
7052 818c7501 2019-07-11 stsp
7053 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7054 818c7501 2019-07-11 stsp if (err)
7055 818c7501 2019-07-11 stsp goto done;
7056 818c7501 2019-07-11 stsp
7057 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7058 818c7501 2019-07-11 stsp if (err)
7059 818c7501 2019-07-11 stsp goto done;
7060 818c7501 2019-07-11 stsp
7061 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7062 a615e0e7 2020-12-16 stsp if (err)
7063 a615e0e7 2020-12-16 stsp goto done;
7064 a615e0e7 2020-12-16 stsp
7065 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7066 a615e0e7 2020-12-16 stsp if (err)
7067 a615e0e7 2020-12-16 stsp goto done;
7068 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7069 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7070 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7071 a615e0e7 2020-12-16 stsp err = sync_err;
7072 818c7501 2019-07-11 stsp done:
7073 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7074 a615e0e7 2020-12-16 stsp free(fileindex_path);
7075 818c7501 2019-07-11 stsp free(new_head_commit_id);
7076 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7077 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7078 818c7501 2019-07-11 stsp err = unlockerr;
7079 818c7501 2019-07-11 stsp return err;
7080 818c7501 2019-07-11 stsp }
7081 818c7501 2019-07-11 stsp
7082 818c7501 2019-07-11 stsp const struct got_error *
7083 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7084 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7085 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7086 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7087 818c7501 2019-07-11 stsp {
7088 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7089 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7090 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7091 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7092 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7093 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7094 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7095 818c7501 2019-07-11 stsp
7096 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7097 818c7501 2019-07-11 stsp if (err)
7098 818c7501 2019-07-11 stsp return err;
7099 818c7501 2019-07-11 stsp
7100 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7101 945f9229 2022-04-16 thomas worktree->base_commit_id);
7102 945f9229 2022-04-16 thomas if (err)
7103 945f9229 2022-04-16 thomas goto done;
7104 945f9229 2022-04-16 thomas
7105 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7106 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7107 818c7501 2019-07-11 stsp if (err)
7108 818c7501 2019-07-11 stsp goto done;
7109 818c7501 2019-07-11 stsp
7110 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7111 818c7501 2019-07-11 stsp if (err)
7112 818c7501 2019-07-11 stsp goto done;
7113 818c7501 2019-07-11 stsp
7114 818c7501 2019-07-11 stsp /*
7115 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7116 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7117 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7118 818c7501 2019-07-11 stsp */
7119 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7120 818c7501 2019-07-11 stsp if (err)
7121 818c7501 2019-07-11 stsp goto done;
7122 818c7501 2019-07-11 stsp
7123 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7124 818c7501 2019-07-11 stsp if (err)
7125 818c7501 2019-07-11 stsp goto done;
7126 818c7501 2019-07-11 stsp
7127 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7128 945f9229 2022-04-16 thomas worktree->path_prefix);
7129 ca355955 2019-07-12 stsp if (err)
7130 ca355955 2019-07-12 stsp goto done;
7131 ca355955 2019-07-12 stsp
7132 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7133 ca355955 2019-07-12 stsp if (err)
7134 ca355955 2019-07-12 stsp goto done;
7135 ca355955 2019-07-12 stsp
7136 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7137 818c7501 2019-07-11 stsp if (err)
7138 818c7501 2019-07-11 stsp goto done;
7139 818c7501 2019-07-11 stsp
7140 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7141 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7142 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7143 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7144 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7145 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7146 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7147 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7148 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7149 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7150 55bd499d 2019-07-12 stsp if (err)
7151 1f1abb7e 2019-08-08 stsp goto sync;
7152 55bd499d 2019-07-12 stsp
7153 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7154 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7155 ca355955 2019-07-12 stsp sync:
7156 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7157 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7158 ca355955 2019-07-12 stsp err = sync_err;
7159 818c7501 2019-07-11 stsp done:
7160 818c7501 2019-07-11 stsp got_ref_close(resolved);
7161 a3a2faf2 2019-07-12 stsp free(tree_id);
7162 818c7501 2019-07-11 stsp free(commit_id);
7163 945f9229 2022-04-16 thomas if (commit)
7164 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7165 0ebf8283 2019-07-24 stsp if (fileindex)
7166 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7167 0ebf8283 2019-07-24 stsp free(fileindex_path);
7168 0ebf8283 2019-07-24 stsp
7169 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7170 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7171 0ebf8283 2019-07-24 stsp err = unlockerr;
7172 0ebf8283 2019-07-24 stsp return err;
7173 0ebf8283 2019-07-24 stsp }
7174 0ebf8283 2019-07-24 stsp
7175 0ebf8283 2019-07-24 stsp const struct got_error *
7176 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7177 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7178 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7179 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7180 0ebf8283 2019-07-24 stsp {
7181 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7182 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7183 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7184 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7185 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7186 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7187 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7188 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7189 0ebf8283 2019-07-24 stsp
7190 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7191 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7192 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7193 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7194 0ebf8283 2019-07-24 stsp
7195 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7196 0ebf8283 2019-07-24 stsp if (err)
7197 0ebf8283 2019-07-24 stsp return err;
7198 0ebf8283 2019-07-24 stsp
7199 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7200 0ebf8283 2019-07-24 stsp if (err)
7201 0ebf8283 2019-07-24 stsp goto done;
7202 0ebf8283 2019-07-24 stsp
7203 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7204 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7205 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7206 0ebf8283 2019-07-24 stsp &ok_arg);
7207 0ebf8283 2019-07-24 stsp if (err)
7208 0ebf8283 2019-07-24 stsp goto done;
7209 0ebf8283 2019-07-24 stsp
7210 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7211 0ebf8283 2019-07-24 stsp if (err)
7212 0ebf8283 2019-07-24 stsp goto done;
7213 0ebf8283 2019-07-24 stsp
7214 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7215 0ebf8283 2019-07-24 stsp if (err)
7216 0ebf8283 2019-07-24 stsp goto done;
7217 0ebf8283 2019-07-24 stsp
7218 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7219 0ebf8283 2019-07-24 stsp worktree);
7220 0ebf8283 2019-07-24 stsp if (err)
7221 0ebf8283 2019-07-24 stsp goto done;
7222 0ebf8283 2019-07-24 stsp
7223 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7224 0ebf8283 2019-07-24 stsp 0);
7225 0ebf8283 2019-07-24 stsp if (err)
7226 0ebf8283 2019-07-24 stsp goto done;
7227 0ebf8283 2019-07-24 stsp
7228 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7229 0ebf8283 2019-07-24 stsp if (err)
7230 0ebf8283 2019-07-24 stsp goto done;
7231 0ebf8283 2019-07-24 stsp
7232 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7233 0ebf8283 2019-07-24 stsp if (err)
7234 0ebf8283 2019-07-24 stsp goto done;
7235 0ebf8283 2019-07-24 stsp
7236 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7237 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7238 0ebf8283 2019-07-24 stsp if (err)
7239 0ebf8283 2019-07-24 stsp goto done;
7240 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7241 0ebf8283 2019-07-24 stsp if (err)
7242 0ebf8283 2019-07-24 stsp goto done;
7243 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7244 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7245 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7246 0ebf8283 2019-07-24 stsp goto done;
7247 0ebf8283 2019-07-24 stsp }
7248 0ebf8283 2019-07-24 stsp
7249 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7250 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7251 0ebf8283 2019-07-24 stsp if (err)
7252 0ebf8283 2019-07-24 stsp goto done;
7253 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7254 0ebf8283 2019-07-24 stsp if (err)
7255 0ebf8283 2019-07-24 stsp goto done;
7256 0ebf8283 2019-07-24 stsp
7257 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7258 0ebf8283 2019-07-24 stsp if (err)
7259 0ebf8283 2019-07-24 stsp goto done;
7260 0ebf8283 2019-07-24 stsp done:
7261 0ebf8283 2019-07-24 stsp free(fileindex_path);
7262 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7263 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7264 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7265 0ebf8283 2019-07-24 stsp if (wt_branch)
7266 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7267 0ebf8283 2019-07-24 stsp if (err) {
7268 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7269 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7270 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7271 0ebf8283 2019-07-24 stsp }
7272 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7273 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7274 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7275 0ebf8283 2019-07-24 stsp }
7276 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7277 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7278 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7279 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7280 3e3a69f1 2019-07-25 stsp }
7281 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7282 0ebf8283 2019-07-24 stsp }
7283 0ebf8283 2019-07-24 stsp return err;
7284 0ebf8283 2019-07-24 stsp }
7285 0ebf8283 2019-07-24 stsp
7286 0ebf8283 2019-07-24 stsp const struct got_error *
7287 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7288 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7289 0ebf8283 2019-07-24 stsp {
7290 3e3a69f1 2019-07-25 stsp if (fileindex)
7291 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7292 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7293 0ebf8283 2019-07-24 stsp }
7294 0ebf8283 2019-07-24 stsp
7295 0ebf8283 2019-07-24 stsp const struct got_error *
7296 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7297 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7298 0ebf8283 2019-07-24 stsp {
7299 0ebf8283 2019-07-24 stsp const struct got_error *err;
7300 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7301 0ebf8283 2019-07-24 stsp
7302 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7303 0ebf8283 2019-07-24 stsp if (err)
7304 0ebf8283 2019-07-24 stsp return err;
7305 0ebf8283 2019-07-24 stsp
7306 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7307 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7308 0ebf8283 2019-07-24 stsp return NULL;
7309 0ebf8283 2019-07-24 stsp }
7310 0ebf8283 2019-07-24 stsp
7311 0ebf8283 2019-07-24 stsp const struct got_error *
7312 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7313 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7314 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7315 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7316 0ebf8283 2019-07-24 stsp {
7317 0ebf8283 2019-07-24 stsp const struct got_error *err;
7318 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7319 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7320 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7321 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7322 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7323 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7324 0ebf8283 2019-07-24 stsp
7325 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7326 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7327 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7328 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7329 0ebf8283 2019-07-24 stsp
7330 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7331 3e3a69f1 2019-07-25 stsp if (err)
7332 3e3a69f1 2019-07-25 stsp return err;
7333 3e3a69f1 2019-07-25 stsp
7334 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7335 3e3a69f1 2019-07-25 stsp if (err)
7336 f032f1f7 2019-08-04 stsp goto done;
7337 f032f1f7 2019-08-04 stsp
7338 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7339 f032f1f7 2019-08-04 stsp &have_staged_files);
7340 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7341 f032f1f7 2019-08-04 stsp goto done;
7342 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7343 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7344 3e3a69f1 2019-07-25 stsp goto done;
7345 f032f1f7 2019-08-04 stsp }
7346 3e3a69f1 2019-07-25 stsp
7347 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7348 0ebf8283 2019-07-24 stsp if (err)
7349 f032f1f7 2019-08-04 stsp goto done;
7350 0ebf8283 2019-07-24 stsp
7351 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7352 0ebf8283 2019-07-24 stsp if (err)
7353 0ebf8283 2019-07-24 stsp goto done;
7354 0ebf8283 2019-07-24 stsp
7355 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7356 0ebf8283 2019-07-24 stsp if (err)
7357 0ebf8283 2019-07-24 stsp goto done;
7358 0ebf8283 2019-07-24 stsp
7359 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7360 0ebf8283 2019-07-24 stsp worktree);
7361 0ebf8283 2019-07-24 stsp if (err)
7362 0ebf8283 2019-07-24 stsp goto done;
7363 0ebf8283 2019-07-24 stsp
7364 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7365 0ebf8283 2019-07-24 stsp if (err)
7366 0ebf8283 2019-07-24 stsp goto done;
7367 0ebf8283 2019-07-24 stsp
7368 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7369 0ebf8283 2019-07-24 stsp if (err)
7370 0ebf8283 2019-07-24 stsp goto done;
7371 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7372 0ebf8283 2019-07-24 stsp if (err)
7373 0ebf8283 2019-07-24 stsp goto done;
7374 0ebf8283 2019-07-24 stsp
7375 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7376 0ebf8283 2019-07-24 stsp if (err)
7377 0ebf8283 2019-07-24 stsp goto done;
7378 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7379 0ebf8283 2019-07-24 stsp if (err)
7380 0ebf8283 2019-07-24 stsp goto done;
7381 0ebf8283 2019-07-24 stsp
7382 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7383 0ebf8283 2019-07-24 stsp if (err)
7384 0ebf8283 2019-07-24 stsp goto done;
7385 0ebf8283 2019-07-24 stsp done:
7386 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7387 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7388 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7389 0ebf8283 2019-07-24 stsp if (commit_ref)
7390 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7391 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7392 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7393 0ebf8283 2019-07-24 stsp if (err) {
7394 0ebf8283 2019-07-24 stsp free(*commit_id);
7395 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7396 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7397 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7398 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7399 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7400 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7401 0ebf8283 2019-07-24 stsp }
7402 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7403 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7404 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7405 3e3a69f1 2019-07-25 stsp }
7406 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7407 0ebf8283 2019-07-24 stsp }
7408 0ebf8283 2019-07-24 stsp return err;
7409 0ebf8283 2019-07-24 stsp }
7410 0ebf8283 2019-07-24 stsp
7411 0ebf8283 2019-07-24 stsp static const struct got_error *
7412 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7413 0ebf8283 2019-07-24 stsp {
7414 0ebf8283 2019-07-24 stsp const struct got_error *err;
7415 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7416 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7417 0ebf8283 2019-07-24 stsp
7418 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7419 0ebf8283 2019-07-24 stsp if (err)
7420 0ebf8283 2019-07-24 stsp goto done;
7421 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7422 0ebf8283 2019-07-24 stsp if (err)
7423 0ebf8283 2019-07-24 stsp goto done;
7424 0ebf8283 2019-07-24 stsp
7425 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7426 0ebf8283 2019-07-24 stsp worktree);
7427 0ebf8283 2019-07-24 stsp if (err)
7428 0ebf8283 2019-07-24 stsp goto done;
7429 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7430 0ebf8283 2019-07-24 stsp if (err)
7431 0ebf8283 2019-07-24 stsp goto done;
7432 0ebf8283 2019-07-24 stsp
7433 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7434 0ebf8283 2019-07-24 stsp if (err)
7435 0ebf8283 2019-07-24 stsp goto done;
7436 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7437 0ebf8283 2019-07-24 stsp if (err)
7438 0ebf8283 2019-07-24 stsp goto done;
7439 0ebf8283 2019-07-24 stsp
7440 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7441 0ebf8283 2019-07-24 stsp if (err)
7442 0ebf8283 2019-07-24 stsp goto done;
7443 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7444 0ebf8283 2019-07-24 stsp if (err)
7445 0ebf8283 2019-07-24 stsp goto done;
7446 0ebf8283 2019-07-24 stsp done:
7447 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7448 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7449 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7450 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7451 0ebf8283 2019-07-24 stsp return err;
7452 0ebf8283 2019-07-24 stsp }
7453 0ebf8283 2019-07-24 stsp
7454 0ebf8283 2019-07-24 stsp const struct got_error *
7455 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7456 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7457 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7458 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7459 0ebf8283 2019-07-24 stsp {
7460 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7461 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7462 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7463 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7464 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7465 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7466 0ebf8283 2019-07-24 stsp
7467 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7468 0ebf8283 2019-07-24 stsp if (err)
7469 0ebf8283 2019-07-24 stsp return err;
7470 0ebf8283 2019-07-24 stsp
7471 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7472 945f9229 2022-04-16 thomas worktree->base_commit_id);
7473 945f9229 2022-04-16 thomas if (err)
7474 945f9229 2022-04-16 thomas goto done;
7475 945f9229 2022-04-16 thomas
7476 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7477 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7478 0ebf8283 2019-07-24 stsp if (err)
7479 0ebf8283 2019-07-24 stsp goto done;
7480 0ebf8283 2019-07-24 stsp
7481 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7482 0ebf8283 2019-07-24 stsp if (err)
7483 0ebf8283 2019-07-24 stsp goto done;
7484 0ebf8283 2019-07-24 stsp
7485 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7486 0ebf8283 2019-07-24 stsp if (err)
7487 0ebf8283 2019-07-24 stsp goto done;
7488 0ebf8283 2019-07-24 stsp
7489 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7490 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7491 0ebf8283 2019-07-24 stsp if (err)
7492 0ebf8283 2019-07-24 stsp goto done;
7493 0ebf8283 2019-07-24 stsp
7494 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7495 0ebf8283 2019-07-24 stsp if (err)
7496 0ebf8283 2019-07-24 stsp goto done;
7497 0ebf8283 2019-07-24 stsp
7498 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7499 0ebf8283 2019-07-24 stsp if (err)
7500 0ebf8283 2019-07-24 stsp goto done;
7501 0ebf8283 2019-07-24 stsp
7502 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7503 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7504 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7505 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7506 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7507 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7508 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7509 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7510 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7511 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7512 0ebf8283 2019-07-24 stsp if (err)
7513 1f1abb7e 2019-08-08 stsp goto sync;
7514 0ebf8283 2019-07-24 stsp
7515 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7516 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7517 0ebf8283 2019-07-24 stsp sync:
7518 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7519 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7520 0ebf8283 2019-07-24 stsp err = sync_err;
7521 0ebf8283 2019-07-24 stsp done:
7522 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7523 0ebf8283 2019-07-24 stsp free(tree_id);
7524 818c7501 2019-07-11 stsp free(fileindex_path);
7525 818c7501 2019-07-11 stsp
7526 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7527 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7528 818c7501 2019-07-11 stsp err = unlockerr;
7529 818c7501 2019-07-11 stsp return err;
7530 818c7501 2019-07-11 stsp }
7531 0ebf8283 2019-07-24 stsp
7532 0ebf8283 2019-07-24 stsp const struct got_error *
7533 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7534 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7535 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7536 0ebf8283 2019-07-24 stsp {
7537 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7538 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7539 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7540 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7541 0ebf8283 2019-07-24 stsp
7542 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7543 0ebf8283 2019-07-24 stsp if (err)
7544 0ebf8283 2019-07-24 stsp return err;
7545 0ebf8283 2019-07-24 stsp
7546 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7547 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7548 e600f124 2021-03-21 stsp if (err)
7549 e600f124 2021-03-21 stsp goto done;
7550 e600f124 2021-03-21 stsp
7551 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7552 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7553 0ebf8283 2019-07-24 stsp if (err)
7554 0ebf8283 2019-07-24 stsp goto done;
7555 0ebf8283 2019-07-24 stsp
7556 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7557 0ebf8283 2019-07-24 stsp if (err)
7558 0ebf8283 2019-07-24 stsp goto done;
7559 0ebf8283 2019-07-24 stsp
7560 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
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 = delete_histedit_refs(worktree, repo);
7569 a615e0e7 2020-12-16 stsp if (err)
7570 a615e0e7 2020-12-16 stsp goto done;
7571 a615e0e7 2020-12-16 stsp
7572 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7573 a615e0e7 2020-12-16 stsp if (err)
7574 a615e0e7 2020-12-16 stsp goto done;
7575 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7576 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7577 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7578 a615e0e7 2020-12-16 stsp err = sync_err;
7579 0ebf8283 2019-07-24 stsp done:
7580 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7581 a615e0e7 2020-12-16 stsp free(fileindex_path);
7582 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7583 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7584 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7585 0ebf8283 2019-07-24 stsp err = unlockerr;
7586 0ebf8283 2019-07-24 stsp return err;
7587 0ebf8283 2019-07-24 stsp }
7588 0ebf8283 2019-07-24 stsp
7589 0ebf8283 2019-07-24 stsp const struct got_error *
7590 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7591 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7592 0ebf8283 2019-07-24 stsp {
7593 0ebf8283 2019-07-24 stsp const struct got_error *err;
7594 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7595 0ebf8283 2019-07-24 stsp
7596 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7597 0ebf8283 2019-07-24 stsp if (err)
7598 0ebf8283 2019-07-24 stsp return err;
7599 0ebf8283 2019-07-24 stsp
7600 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7601 0ebf8283 2019-07-24 stsp if (err)
7602 0ebf8283 2019-07-24 stsp goto done;
7603 0ebf8283 2019-07-24 stsp
7604 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7605 0ebf8283 2019-07-24 stsp done:
7606 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7607 2822a352 2019-10-15 stsp return err;
7608 2822a352 2019-10-15 stsp }
7609 2822a352 2019-10-15 stsp
7610 2822a352 2019-10-15 stsp const struct got_error *
7611 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7612 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7613 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7614 2822a352 2019-10-15 stsp struct got_repository *repo)
7615 2822a352 2019-10-15 stsp {
7616 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7617 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7618 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7619 2822a352 2019-10-15 stsp
7620 2822a352 2019-10-15 stsp *fileindex = NULL;
7621 2822a352 2019-10-15 stsp *branch_ref = NULL;
7622 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7623 2822a352 2019-10-15 stsp
7624 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7625 2822a352 2019-10-15 stsp if (err)
7626 2822a352 2019-10-15 stsp return err;
7627 2822a352 2019-10-15 stsp
7628 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7629 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7630 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7631 2822a352 2019-10-15 stsp "update -b or different branch name required");
7632 2822a352 2019-10-15 stsp goto done;
7633 2822a352 2019-10-15 stsp }
7634 2822a352 2019-10-15 stsp
7635 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7636 2822a352 2019-10-15 stsp if (err)
7637 2822a352 2019-10-15 stsp goto done;
7638 2822a352 2019-10-15 stsp
7639 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7640 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7641 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7642 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7643 2822a352 2019-10-15 stsp &ok_arg);
7644 2822a352 2019-10-15 stsp if (err)
7645 2822a352 2019-10-15 stsp goto done;
7646 2822a352 2019-10-15 stsp
7647 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7648 2822a352 2019-10-15 stsp if (err)
7649 2822a352 2019-10-15 stsp goto done;
7650 2822a352 2019-10-15 stsp
7651 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7652 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7653 2822a352 2019-10-15 stsp done:
7654 2822a352 2019-10-15 stsp if (err) {
7655 2822a352 2019-10-15 stsp if (*branch_ref) {
7656 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7657 2822a352 2019-10-15 stsp *branch_ref = NULL;
7658 2822a352 2019-10-15 stsp }
7659 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7660 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7661 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7662 2822a352 2019-10-15 stsp }
7663 2822a352 2019-10-15 stsp if (*fileindex) {
7664 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7665 2822a352 2019-10-15 stsp *fileindex = NULL;
7666 2822a352 2019-10-15 stsp }
7667 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7668 2822a352 2019-10-15 stsp }
7669 0cb83759 2019-08-03 stsp return err;
7670 0cb83759 2019-08-03 stsp }
7671 0cb83759 2019-08-03 stsp
7672 2822a352 2019-10-15 stsp const struct got_error *
7673 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7674 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7675 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7676 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7677 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7678 2822a352 2019-10-15 stsp {
7679 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7680 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7681 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7682 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7683 2822a352 2019-10-15 stsp
7684 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7685 2822a352 2019-10-15 stsp if (err)
7686 2822a352 2019-10-15 stsp goto done;
7687 2822a352 2019-10-15 stsp
7688 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7689 2822a352 2019-10-15 stsp if (err)
7690 2822a352 2019-10-15 stsp goto done;
7691 2822a352 2019-10-15 stsp
7692 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7693 945f9229 2022-04-16 thomas if (err)
7694 945f9229 2022-04-16 thomas goto done;
7695 945f9229 2022-04-16 thomas
7696 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7697 2822a352 2019-10-15 stsp worktree->path_prefix);
7698 2822a352 2019-10-15 stsp if (err)
7699 2822a352 2019-10-15 stsp goto done;
7700 2822a352 2019-10-15 stsp
7701 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7702 2822a352 2019-10-15 stsp if (err)
7703 2822a352 2019-10-15 stsp goto done;
7704 2822a352 2019-10-15 stsp
7705 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7706 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7707 2822a352 2019-10-15 stsp if (err)
7708 2822a352 2019-10-15 stsp goto sync;
7709 2822a352 2019-10-15 stsp
7710 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7711 2822a352 2019-10-15 stsp if (err)
7712 2822a352 2019-10-15 stsp goto sync;
7713 2822a352 2019-10-15 stsp
7714 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7715 6e210706 2021-01-22 stsp if (err)
7716 6e210706 2021-01-22 stsp goto sync;
7717 6e210706 2021-01-22 stsp
7718 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7719 2822a352 2019-10-15 stsp sync:
7720 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7721 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7722 2822a352 2019-10-15 stsp err = sync_err;
7723 2822a352 2019-10-15 stsp
7724 2822a352 2019-10-15 stsp done:
7725 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7726 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7727 2822a352 2019-10-15 stsp err = unlockerr;
7728 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7729 2822a352 2019-10-15 stsp
7730 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7731 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7732 2822a352 2019-10-15 stsp err = unlockerr;
7733 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7734 2822a352 2019-10-15 stsp
7735 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7736 2822a352 2019-10-15 stsp free(fileindex_path);
7737 2822a352 2019-10-15 stsp free(tree_id);
7738 945f9229 2022-04-16 thomas if (commit)
7739 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7740 2822a352 2019-10-15 stsp
7741 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7742 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7743 2822a352 2019-10-15 stsp err = unlockerr;
7744 2822a352 2019-10-15 stsp return err;
7745 2822a352 2019-10-15 stsp }
7746 2822a352 2019-10-15 stsp
7747 2822a352 2019-10-15 stsp const struct got_error *
7748 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7749 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7750 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7751 2822a352 2019-10-15 stsp {
7752 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7753 8b692cd0 2019-10-21 stsp
7754 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7755 8b692cd0 2019-10-21 stsp
7756 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7757 8b692cd0 2019-10-21 stsp
7758 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7759 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7760 8b692cd0 2019-10-21 stsp err = unlockerr;
7761 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7762 8b692cd0 2019-10-21 stsp
7763 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7764 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7765 8b692cd0 2019-10-21 stsp err = unlockerr;
7766 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7767 10604dce 2021-09-24 thomas
7768 10604dce 2021-09-24 thomas return err;
7769 10604dce 2021-09-24 thomas }
7770 10604dce 2021-09-24 thomas
7771 10604dce 2021-09-24 thomas const struct got_error *
7772 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7773 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7774 10604dce 2021-09-24 thomas {
7775 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7776 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7777 10604dce 2021-09-24 thomas
7778 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7779 10604dce 2021-09-24 thomas if (err)
7780 10604dce 2021-09-24 thomas goto done;
7781 8b692cd0 2019-10-21 stsp
7782 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7783 10604dce 2021-09-24 thomas
7784 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7785 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7786 10604dce 2021-09-24 thomas err = sync_err;
7787 10604dce 2021-09-24 thomas done:
7788 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7789 10604dce 2021-09-24 thomas free(fileindex_path);
7790 8b692cd0 2019-10-21 stsp return err;
7791 2822a352 2019-10-15 stsp }
7792 2822a352 2019-10-15 stsp
7793 10604dce 2021-09-24 thomas static const struct got_error *
7794 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7795 10604dce 2021-09-24 thomas {
7796 10604dce 2021-09-24 thomas const struct got_error *err;
7797 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7798 10604dce 2021-09-24 thomas
7799 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7800 10604dce 2021-09-24 thomas if (err)
7801 10604dce 2021-09-24 thomas goto done;
7802 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7803 10604dce 2021-09-24 thomas if (err)
7804 10604dce 2021-09-24 thomas goto done;
7805 10604dce 2021-09-24 thomas
7806 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7807 10604dce 2021-09-24 thomas if (err)
7808 10604dce 2021-09-24 thomas goto done;
7809 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7810 10604dce 2021-09-24 thomas if (err)
7811 10604dce 2021-09-24 thomas goto done;
7812 10604dce 2021-09-24 thomas
7813 10604dce 2021-09-24 thomas done:
7814 10604dce 2021-09-24 thomas free(branch_refname);
7815 10604dce 2021-09-24 thomas free(commit_refname);
7816 10604dce 2021-09-24 thomas return err;
7817 10604dce 2021-09-24 thomas }
7818 10604dce 2021-09-24 thomas
7819 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7820 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7821 10604dce 2021-09-24 thomas const char *branch_name;
7822 10604dce 2021-09-24 thomas };
7823 10604dce 2021-09-24 thomas
7824 10604dce 2021-09-24 thomas static const struct got_error *
7825 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7826 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7827 10604dce 2021-09-24 thomas {
7828 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7829 10604dce 2021-09-24 thomas
7830 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7831 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7832 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7833 10604dce 2021-09-24 thomas
7834 10604dce 2021-09-24 thomas return NULL;
7835 10604dce 2021-09-24 thomas }
7836 10604dce 2021-09-24 thomas
7837 10604dce 2021-09-24 thomas
7838 10604dce 2021-09-24 thomas const struct got_error *
7839 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7840 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7841 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7842 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7843 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7844 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7845 10604dce 2021-09-24 thomas {
7846 10604dce 2021-09-24 thomas const struct got_error *err;
7847 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7848 10604dce 2021-09-24 thomas
7849 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7850 10604dce 2021-09-24 thomas if (err)
7851 10604dce 2021-09-24 thomas goto done;
7852 10604dce 2021-09-24 thomas
7853 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7854 10604dce 2021-09-24 thomas worktree);
7855 10604dce 2021-09-24 thomas if (err)
7856 10604dce 2021-09-24 thomas goto done;
7857 10604dce 2021-09-24 thomas
7858 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7859 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7860 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7861 10604dce 2021-09-24 thomas done:
7862 10604dce 2021-09-24 thomas free(fileindex_path);
7863 10604dce 2021-09-24 thomas return err;
7864 10604dce 2021-09-24 thomas }
7865 10604dce 2021-09-24 thomas
7866 10604dce 2021-09-24 thomas const struct got_error *
7867 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7868 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7869 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7870 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7871 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7872 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7873 ae1e948a 2021-09-28 thomas
7874 10604dce 2021-09-24 thomas {
7875 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7876 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7877 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7878 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7879 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7880 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7881 10604dce 2021-09-24 thomas int have_staged_files = 0;
7882 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7883 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7884 10604dce 2021-09-24 thomas
7885 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7886 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7887 10604dce 2021-09-24 thomas
7888 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7889 10604dce 2021-09-24 thomas
7890 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7891 10604dce 2021-09-24 thomas if (err)
7892 10604dce 2021-09-24 thomas goto done;
7893 10604dce 2021-09-24 thomas
7894 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7895 10604dce 2021-09-24 thomas if (err)
7896 10604dce 2021-09-24 thomas goto done;
7897 10604dce 2021-09-24 thomas
7898 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7899 10604dce 2021-09-24 thomas if (err)
7900 10604dce 2021-09-24 thomas goto done;
7901 10604dce 2021-09-24 thomas
7902 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7903 10604dce 2021-09-24 thomas &have_staged_files);
7904 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7905 10604dce 2021-09-24 thomas goto done;
7906 10604dce 2021-09-24 thomas if (have_staged_files) {
7907 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7908 10604dce 2021-09-24 thomas goto done;
7909 10604dce 2021-09-24 thomas }
7910 10604dce 2021-09-24 thomas
7911 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7912 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7913 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7914 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7915 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7916 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7917 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7918 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7919 10604dce 2021-09-24 thomas if (err)
7920 10604dce 2021-09-24 thomas goto done;
7921 10604dce 2021-09-24 thomas
7922 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7923 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7924 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7925 10604dce 2021-09-24 thomas goto done;
7926 10604dce 2021-09-24 thomas }
7927 10604dce 2021-09-24 thomas
7928 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7929 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7930 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7931 10604dce 2021-09-24 thomas
7932 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7933 10604dce 2021-09-24 thomas ct_path++;
7934 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7935 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7936 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7937 10604dce 2021-09-24 thomas if (err)
7938 10604dce 2021-09-24 thomas goto done;
7939 10604dce 2021-09-24 thomas
7940 10604dce 2021-09-24 thomas }
7941 10604dce 2021-09-24 thomas
7942 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7943 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7944 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7945 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
7946 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7947 10604dce 2021-09-24 thomas if (err)
7948 10604dce 2021-09-24 thomas goto done;
7949 10604dce 2021-09-24 thomas
7950 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7951 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7952 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7953 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7954 10604dce 2021-09-24 thomas err = sync_err;
7955 10604dce 2021-09-24 thomas done:
7956 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7957 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7958 10604dce 2021-09-24 thomas free_commitable(ct);
7959 10604dce 2021-09-24 thomas }
7960 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7961 10604dce 2021-09-24 thomas free(fileindex_path);
7962 10604dce 2021-09-24 thomas return err;
7963 10604dce 2021-09-24 thomas }
7964 10604dce 2021-09-24 thomas
7965 10604dce 2021-09-24 thomas const struct got_error *
7966 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7967 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7968 10604dce 2021-09-24 thomas {
7969 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7970 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7971 10604dce 2021-09-24 thomas
7972 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7973 10604dce 2021-09-24 thomas if (err)
7974 10604dce 2021-09-24 thomas goto done;
7975 10604dce 2021-09-24 thomas
7976 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7977 10604dce 2021-09-24 thomas if (err)
7978 10604dce 2021-09-24 thomas goto done;
7979 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7980 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7981 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7982 10604dce 2021-09-24 thomas err = sync_err;
7983 10604dce 2021-09-24 thomas done:
7984 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7985 10604dce 2021-09-24 thomas free(fileindex_path);
7986 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7987 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7988 10604dce 2021-09-24 thomas err = unlockerr;
7989 10604dce 2021-09-24 thomas return err;
7990 10604dce 2021-09-24 thomas }
7991 10604dce 2021-09-24 thomas
7992 10604dce 2021-09-24 thomas const struct got_error *
7993 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7994 10604dce 2021-09-24 thomas struct got_repository *repo)
7995 10604dce 2021-09-24 thomas {
7996 10604dce 2021-09-24 thomas const struct got_error *err;
7997 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7998 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7999 10604dce 2021-09-24 thomas
8000 10604dce 2021-09-24 thomas *in_progress = 0;
8001 10604dce 2021-09-24 thomas
8002 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8003 10604dce 2021-09-24 thomas if (err)
8004 10604dce 2021-09-24 thomas return err;
8005 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8006 dfe86d1f 2021-09-24 thomas free(branch_refname);
8007 10604dce 2021-09-24 thomas if (err) {
8008 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8009 10604dce 2021-09-24 thomas return err;
8010 10604dce 2021-09-24 thomas } else
8011 10604dce 2021-09-24 thomas *in_progress = 1;
8012 10604dce 2021-09-24 thomas
8013 10604dce 2021-09-24 thomas return NULL;
8014 10604dce 2021-09-24 thomas }
8015 10604dce 2021-09-24 thomas
8016 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8017 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8018 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8019 10604dce 2021-09-24 thomas {
8020 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8021 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8022 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8023 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8024 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8025 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8026 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8027 10604dce 2021-09-24 thomas
8028 10604dce 2021-09-24 thomas *fileindex = NULL;
8029 10604dce 2021-09-24 thomas
8030 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8031 10604dce 2021-09-24 thomas if (err)
8032 10604dce 2021-09-24 thomas return err;
8033 10604dce 2021-09-24 thomas
8034 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8035 10604dce 2021-09-24 thomas if (err)
8036 10604dce 2021-09-24 thomas goto done;
8037 10604dce 2021-09-24 thomas
8038 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8039 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8040 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8041 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8042 10604dce 2021-09-24 thomas &ok_arg);
8043 10604dce 2021-09-24 thomas if (err)
8044 10604dce 2021-09-24 thomas goto done;
8045 10604dce 2021-09-24 thomas
8046 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8047 10604dce 2021-09-24 thomas if (err)
8048 10604dce 2021-09-24 thomas return err;
8049 10604dce 2021-09-24 thomas
8050 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8051 10604dce 2021-09-24 thomas if (err)
8052 10604dce 2021-09-24 thomas return err;
8053 10604dce 2021-09-24 thomas
8054 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8055 10604dce 2021-09-24 thomas 0);
8056 10604dce 2021-09-24 thomas if (err)
8057 10604dce 2021-09-24 thomas goto done;
8058 10604dce 2021-09-24 thomas
8059 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8060 10604dce 2021-09-24 thomas if (err)
8061 10604dce 2021-09-24 thomas goto done;
8062 10604dce 2021-09-24 thomas
8063 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8064 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8065 10604dce 2021-09-24 thomas goto done;
8066 10604dce 2021-09-24 thomas }
8067 10604dce 2021-09-24 thomas
8068 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8069 10604dce 2021-09-24 thomas if (err)
8070 10604dce 2021-09-24 thomas goto done;
8071 10604dce 2021-09-24 thomas
8072 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8073 10604dce 2021-09-24 thomas if (err)
8074 10604dce 2021-09-24 thomas goto done;
8075 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8076 10604dce 2021-09-24 thomas if (err)
8077 10604dce 2021-09-24 thomas goto done;
8078 10604dce 2021-09-24 thomas
8079 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8080 10604dce 2021-09-24 thomas if (err)
8081 10604dce 2021-09-24 thomas goto done;
8082 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8083 10604dce 2021-09-24 thomas if (err)
8084 10604dce 2021-09-24 thomas goto done;
8085 10604dce 2021-09-24 thomas
8086 10604dce 2021-09-24 thomas done:
8087 10604dce 2021-09-24 thomas free(branch_refname);
8088 10604dce 2021-09-24 thomas free(commit_refname);
8089 10604dce 2021-09-24 thomas free(fileindex_path);
8090 10604dce 2021-09-24 thomas if (branch_ref)
8091 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8092 10604dce 2021-09-24 thomas if (commit_ref)
8093 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8094 10604dce 2021-09-24 thomas if (wt_branch)
8095 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8096 10604dce 2021-09-24 thomas free(wt_branch_tip);
8097 10604dce 2021-09-24 thomas if (err) {
8098 10604dce 2021-09-24 thomas if (*fileindex) {
8099 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8100 10604dce 2021-09-24 thomas *fileindex = NULL;
8101 10604dce 2021-09-24 thomas }
8102 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8103 10604dce 2021-09-24 thomas }
8104 10604dce 2021-09-24 thomas return err;
8105 10604dce 2021-09-24 thomas }
8106 10604dce 2021-09-24 thomas
8107 10604dce 2021-09-24 thomas const struct got_error *
8108 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8109 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8110 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8111 10604dce 2021-09-24 thomas {
8112 10604dce 2021-09-24 thomas const struct got_error *err;
8113 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8114 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8115 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8116 10604dce 2021-09-24 thomas int have_staged_files = 0;
8117 10604dce 2021-09-24 thomas
8118 10604dce 2021-09-24 thomas *branch_name = NULL;
8119 10604dce 2021-09-24 thomas *branch_tip = NULL;
8120 10604dce 2021-09-24 thomas *fileindex = NULL;
8121 10604dce 2021-09-24 thomas
8122 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8123 10604dce 2021-09-24 thomas if (err)
8124 10604dce 2021-09-24 thomas return err;
8125 10604dce 2021-09-24 thomas
8126 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8127 10604dce 2021-09-24 thomas if (err)
8128 10604dce 2021-09-24 thomas goto done;
8129 10604dce 2021-09-24 thomas
8130 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8131 10604dce 2021-09-24 thomas &have_staged_files);
8132 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8133 10604dce 2021-09-24 thomas goto done;
8134 10604dce 2021-09-24 thomas if (have_staged_files) {
8135 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8136 10604dce 2021-09-24 thomas goto done;
8137 10604dce 2021-09-24 thomas }
8138 10604dce 2021-09-24 thomas
8139 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8140 10604dce 2021-09-24 thomas if (err)
8141 10604dce 2021-09-24 thomas goto done;
8142 10604dce 2021-09-24 thomas
8143 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8144 10604dce 2021-09-24 thomas if (err)
8145 10604dce 2021-09-24 thomas goto done;
8146 10604dce 2021-09-24 thomas
8147 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8148 10604dce 2021-09-24 thomas if (err)
8149 10604dce 2021-09-24 thomas goto done;
8150 10604dce 2021-09-24 thomas
8151 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8152 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8153 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8154 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8155 10604dce 2021-09-24 thomas goto done;
8156 10604dce 2021-09-24 thomas }
8157 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8158 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8159 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8160 10604dce 2021-09-24 thomas goto done;
8161 10604dce 2021-09-24 thomas }
8162 10604dce 2021-09-24 thomas
8163 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8164 10604dce 2021-09-24 thomas if (err)
8165 10604dce 2021-09-24 thomas goto done;
8166 10604dce 2021-09-24 thomas
8167 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8168 10604dce 2021-09-24 thomas if (err)
8169 10604dce 2021-09-24 thomas goto done;
8170 10604dce 2021-09-24 thomas done:
8171 10604dce 2021-09-24 thomas free(commit_refname);
8172 10604dce 2021-09-24 thomas free(branch_refname);
8173 10604dce 2021-09-24 thomas free(fileindex_path);
8174 10604dce 2021-09-24 thomas if (commit_ref)
8175 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8176 10604dce 2021-09-24 thomas if (branch_ref)
8177 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8178 10604dce 2021-09-24 thomas if (err) {
8179 10604dce 2021-09-24 thomas if (*branch_name) {
8180 10604dce 2021-09-24 thomas free(*branch_name);
8181 10604dce 2021-09-24 thomas *branch_name = NULL;
8182 10604dce 2021-09-24 thomas }
8183 10604dce 2021-09-24 thomas free(*branch_tip);
8184 10604dce 2021-09-24 thomas *branch_tip = NULL;
8185 10604dce 2021-09-24 thomas if (*fileindex) {
8186 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8187 10604dce 2021-09-24 thomas *fileindex = NULL;
8188 10604dce 2021-09-24 thomas }
8189 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8190 10604dce 2021-09-24 thomas }
8191 10604dce 2021-09-24 thomas return err;
8192 10604dce 2021-09-24 thomas }
8193 10604dce 2021-09-24 thomas
8194 10604dce 2021-09-24 thomas const struct got_error *
8195 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8196 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8197 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8198 10604dce 2021-09-24 thomas {
8199 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8200 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8201 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8202 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8203 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8204 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8205 10604dce 2021-09-24 thomas
8206 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8207 945f9229 2022-04-16 thomas worktree->base_commit_id);
8208 945f9229 2022-04-16 thomas if (err)
8209 945f9229 2022-04-16 thomas goto done;
8210 945f9229 2022-04-16 thomas
8211 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8212 945f9229 2022-04-16 thomas worktree->path_prefix);
8213 10604dce 2021-09-24 thomas if (err)
8214 10604dce 2021-09-24 thomas goto done;
8215 10604dce 2021-09-24 thomas
8216 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8217 10604dce 2021-09-24 thomas if (err)
8218 10604dce 2021-09-24 thomas goto done;
8219 10604dce 2021-09-24 thomas
8220 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8221 10604dce 2021-09-24 thomas if (err)
8222 10604dce 2021-09-24 thomas goto done;
8223 10604dce 2021-09-24 thomas
8224 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8225 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8226 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8227 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8228 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8229 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8230 10604dce 2021-09-24 thomas rfa.repo = repo;
8231 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8232 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8233 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8234 10604dce 2021-09-24 thomas if (err)
8235 10604dce 2021-09-24 thomas goto sync;
8236 10604dce 2021-09-24 thomas
8237 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8238 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8239 10604dce 2021-09-24 thomas sync:
8240 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8241 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8242 10604dce 2021-09-24 thomas err = sync_err;
8243 10604dce 2021-09-24 thomas done:
8244 10604dce 2021-09-24 thomas free(tree_id);
8245 10604dce 2021-09-24 thomas free(commit_id);
8246 945f9229 2022-04-16 thomas if (commit)
8247 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8248 10604dce 2021-09-24 thomas if (fileindex)
8249 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8250 10604dce 2021-09-24 thomas free(fileindex_path);
8251 10604dce 2021-09-24 thomas
8252 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8253 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8254 10604dce 2021-09-24 thomas err = unlockerr;
8255 10604dce 2021-09-24 thomas return err;
8256 10604dce 2021-09-24 thomas }
8257 10604dce 2021-09-24 thomas
8258 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8259 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8260 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8261 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8262 2db2652d 2019-08-07 stsp struct got_repository *repo;
8263 2db2652d 2019-08-07 stsp int have_changes;
8264 2db2652d 2019-08-07 stsp };
8265 2db2652d 2019-08-07 stsp
8266 ef20f542 2022-06-26 thomas static const struct got_error *
8267 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8268 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8269 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8270 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8271 735ef5ac 2019-08-03 stsp {
8272 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8273 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8274 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8275 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8276 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8277 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8278 8b13ce36 2019-08-08 stsp
8279 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8280 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8281 8b13ce36 2019-08-08 stsp return NULL;
8282 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8283 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8284 735ef5ac 2019-08-03 stsp
8285 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8286 735ef5ac 2019-08-03 stsp if (ie == NULL)
8287 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8288 735ef5ac 2019-08-03 stsp
8289 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8290 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8291 735ef5ac 2019-08-03 stsp relpath) == -1)
8292 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8293 735ef5ac 2019-08-03 stsp
8294 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8295 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8296 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8297 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8298 735ef5ac 2019-08-03 stsp }
8299 735ef5ac 2019-08-03 stsp
8300 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8301 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8302 3aa5969e 2019-08-06 stsp goto done;
8303 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8304 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8305 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8306 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8307 735ef5ac 2019-08-03 stsp goto done;
8308 3aa5969e 2019-08-06 stsp }
8309 735ef5ac 2019-08-03 stsp
8310 2db2652d 2019-08-07 stsp a->have_changes = 1;
8311 2db2652d 2019-08-07 stsp
8312 735ef5ac 2019-08-03 stsp p = in_repo_path;
8313 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8314 735ef5ac 2019-08-03 stsp p++;
8315 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8316 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8317 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8318 735ef5ac 2019-08-03 stsp done:
8319 735ef5ac 2019-08-03 stsp free(in_repo_path);
8320 dc424a06 2019-08-07 stsp return err;
8321 dc424a06 2019-08-07 stsp }
8322 dc424a06 2019-08-07 stsp
8323 2db2652d 2019-08-07 stsp struct stage_path_arg {
8324 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8325 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8326 2db2652d 2019-08-07 stsp struct got_repository *repo;
8327 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8328 2db2652d 2019-08-07 stsp void *status_arg;
8329 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8330 2db2652d 2019-08-07 stsp void *patch_arg;
8331 7b5dc508 2019-10-28 stsp int staged_something;
8332 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8333 2db2652d 2019-08-07 stsp };
8334 2db2652d 2019-08-07 stsp
8335 2db2652d 2019-08-07 stsp static const struct got_error *
8336 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8337 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8338 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8339 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8340 0cb83759 2019-08-03 stsp {
8341 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8342 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8343 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8344 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8345 0cb83759 2019-08-03 stsp uint32_t stage;
8346 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8347 0aeb8099 2020-07-23 stsp struct stat sb;
8348 8b13ce36 2019-08-08 stsp
8349 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8350 8b13ce36 2019-08-08 stsp return NULL;
8351 0cb83759 2019-08-03 stsp
8352 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8353 d3e7c587 2019-08-03 stsp if (ie == NULL)
8354 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8355 0cb83759 2019-08-03 stsp
8356 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8357 2db2652d 2019-08-07 stsp relpath)== -1)
8358 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8359 0cb83759 2019-08-03 stsp
8360 0cb83759 2019-08-03 stsp switch (status) {
8361 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8362 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8363 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8364 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8365 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8366 0aeb8099 2020-07-23 stsp break;
8367 0aeb8099 2020-07-23 stsp }
8368 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8369 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8370 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8371 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8372 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8373 dc424a06 2019-08-07 stsp if (err)
8374 dc424a06 2019-08-07 stsp break;
8375 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8376 dc424a06 2019-08-07 stsp break;
8377 dc424a06 2019-08-07 stsp } else {
8378 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8379 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8380 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8381 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8382 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8383 dc424a06 2019-08-07 stsp break;
8384 dc424a06 2019-08-07 stsp }
8385 dc424a06 2019-08-07 stsp }
8386 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8387 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8388 0cb83759 2019-08-03 stsp if (err)
8389 dc424a06 2019-08-07 stsp break;
8390 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8391 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8392 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8393 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8394 0cb83759 2019-08-03 stsp else
8395 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8396 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8397 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8398 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8399 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8400 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8401 35213c7c 2020-07-23 stsp ssize_t target_len;
8402 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8403 35213c7c 2020-07-23 stsp sizeof(target_path));
8404 35213c7c 2020-07-23 stsp if (target_len == -1) {
8405 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8406 35213c7c 2020-07-23 stsp ondisk_path);
8407 35213c7c 2020-07-23 stsp break;
8408 35213c7c 2020-07-23 stsp }
8409 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8410 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8411 35213c7c 2020-07-23 stsp a->worktree->root_path);
8412 35213c7c 2020-07-23 stsp if (err)
8413 35213c7c 2020-07-23 stsp break;
8414 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8415 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8416 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8417 35213c7c 2020-07-23 stsp break;
8418 35213c7c 2020-07-23 stsp }
8419 35213c7c 2020-07-23 stsp }
8420 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8421 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8422 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8423 35213c7c 2020-07-23 stsp else
8424 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8425 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8426 0aeb8099 2020-07-23 stsp } else {
8427 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8428 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8429 0aeb8099 2020-07-23 stsp }
8430 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8431 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8432 dc424a06 2019-08-07 stsp break;
8433 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8434 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8435 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8436 f289c82c 2022-06-13 thomas if (err)
8437 f289c82c 2022-06-13 thomas break;
8438 f289c82c 2022-06-13 thomas /*
8439 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8440 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8441 f289c82c 2022-06-13 thomas */
8442 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8443 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8444 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8445 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8446 f289c82c 2022-06-13 thomas }
8447 0cb83759 2019-08-03 stsp break;
8448 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8449 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8450 d3e7c587 2019-08-03 stsp break;
8451 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8452 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8453 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8454 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8455 dc424a06 2019-08-07 stsp if (err)
8456 dc424a06 2019-08-07 stsp break;
8457 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8458 88f33a19 2019-08-08 stsp break;
8459 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8460 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8461 dc424a06 2019-08-07 stsp break;
8462 88f33a19 2019-08-08 stsp }
8463 dc424a06 2019-08-07 stsp }
8464 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8465 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8466 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8467 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8468 dc424a06 2019-08-07 stsp break;
8469 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8470 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8471 12463d8b 2019-12-13 stsp de_name);
8472 0cb83759 2019-08-03 stsp break;
8473 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8474 d3e7c587 2019-08-03 stsp break;
8475 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8476 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8477 ebf48fd5 2019-08-03 stsp break;
8478 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8479 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8480 2a06fe5f 2019-08-24 stsp break;
8481 0cb83759 2019-08-03 stsp default:
8482 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8483 537ac44b 2019-08-03 stsp break;
8484 0cb83759 2019-08-03 stsp }
8485 dc424a06 2019-08-07 stsp
8486 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8487 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8488 dc424a06 2019-08-07 stsp free(path_content);
8489 2db2652d 2019-08-07 stsp free(ondisk_path);
8490 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8491 0ebf8283 2019-07-24 stsp return err;
8492 0ebf8283 2019-07-24 stsp }
8493 0cb83759 2019-08-03 stsp
8494 0cb83759 2019-08-03 stsp const struct got_error *
8495 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8496 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8497 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8498 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8499 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8500 0cb83759 2019-08-03 stsp {
8501 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8502 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8503 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8504 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8505 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8506 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8507 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8508 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8509 0cb83759 2019-08-03 stsp
8510 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8511 0cb83759 2019-08-03 stsp if (err)
8512 0cb83759 2019-08-03 stsp return err;
8513 0cb83759 2019-08-03 stsp
8514 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8515 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8516 735ef5ac 2019-08-03 stsp if (err)
8517 735ef5ac 2019-08-03 stsp goto done;
8518 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8519 735ef5ac 2019-08-03 stsp if (err)
8520 735ef5ac 2019-08-03 stsp goto done;
8521 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8522 0cb83759 2019-08-03 stsp if (err)
8523 0cb83759 2019-08-03 stsp goto done;
8524 0cb83759 2019-08-03 stsp
8525 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8526 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8527 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8528 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8529 2db2652d 2019-08-07 stsp oka.repo = repo;
8530 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8531 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8532 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8533 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8534 735ef5ac 2019-08-03 stsp if (err)
8535 735ef5ac 2019-08-03 stsp goto done;
8536 735ef5ac 2019-08-03 stsp }
8537 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8538 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8539 2db2652d 2019-08-07 stsp goto done;
8540 2db2652d 2019-08-07 stsp }
8541 735ef5ac 2019-08-03 stsp
8542 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8543 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8544 2db2652d 2019-08-07 stsp spa.repo = repo;
8545 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8546 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8547 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8548 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8549 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8550 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8551 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8552 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8553 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8554 42005733 2019-08-03 stsp if (err)
8555 2db2652d 2019-08-07 stsp goto done;
8556 7b5dc508 2019-10-28 stsp }
8557 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8558 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8559 7b5dc508 2019-10-28 stsp goto done;
8560 0cb83759 2019-08-03 stsp }
8561 0cb83759 2019-08-03 stsp
8562 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8563 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8564 0cb83759 2019-08-03 stsp err = sync_err;
8565 0cb83759 2019-08-03 stsp done:
8566 735ef5ac 2019-08-03 stsp if (head_ref)
8567 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8568 735ef5ac 2019-08-03 stsp free(head_commit_id);
8569 0cb83759 2019-08-03 stsp free(fileindex_path);
8570 0cb83759 2019-08-03 stsp if (fileindex)
8571 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8572 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8573 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8574 0cb83759 2019-08-03 stsp err = unlockerr;
8575 0cb83759 2019-08-03 stsp return err;
8576 0cb83759 2019-08-03 stsp }
8577 ad493afc 2019-08-03 stsp
8578 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8579 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8580 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8581 ad493afc 2019-08-03 stsp struct got_repository *repo;
8582 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8583 ad493afc 2019-08-03 stsp void *progress_arg;
8584 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8585 2e1f37b0 2019-08-08 stsp void *patch_arg;
8586 ad493afc 2019-08-03 stsp };
8587 2e1f37b0 2019-08-08 stsp
8588 2e1f37b0 2019-08-08 stsp static const struct got_error *
8589 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8590 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8591 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8592 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8593 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8594 2e1f37b0 2019-08-08 stsp {
8595 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8596 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8597 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8598 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8599 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8600 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8601 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8602 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8603 2e1f37b0 2019-08-08 stsp
8604 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8605 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8606 2e1f37b0 2019-08-08 stsp
8607 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8608 2e1f37b0 2019-08-08 stsp if (err)
8609 2e1f37b0 2019-08-08 stsp return err;
8610 f4ae6ddb 2022-07-01 thomas
8611 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8612 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8613 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8614 f4ae6ddb 2022-07-01 thomas goto done;
8615 f4ae6ddb 2022-07-01 thomas }
8616 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8617 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8618 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8619 f4ae6ddb 2022-07-01 thomas goto done;
8620 f4ae6ddb 2022-07-01 thomas }
8621 f4ae6ddb 2022-07-01 thomas
8622 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8623 2e1f37b0 2019-08-08 stsp if (err)
8624 2e1f37b0 2019-08-08 stsp goto done;
8625 2e1f37b0 2019-08-08 stsp
8626 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8627 2e1f37b0 2019-08-08 stsp if (err)
8628 2e1f37b0 2019-08-08 stsp goto done;
8629 2e1f37b0 2019-08-08 stsp
8630 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8631 2e1f37b0 2019-08-08 stsp if (err)
8632 2e1f37b0 2019-08-08 stsp goto done;
8633 2e1f37b0 2019-08-08 stsp
8634 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8635 f4ae6ddb 2022-07-01 thomas fd2);
8636 2e1f37b0 2019-08-08 stsp if (err)
8637 2e1f37b0 2019-08-08 stsp goto done;
8638 2e1f37b0 2019-08-08 stsp
8639 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8640 2e1f37b0 2019-08-08 stsp if (err)
8641 2e1f37b0 2019-08-08 stsp goto done;
8642 ad493afc 2019-08-03 stsp
8643 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8644 2e1f37b0 2019-08-08 stsp if (err)
8645 2e1f37b0 2019-08-08 stsp goto done;
8646 2e1f37b0 2019-08-08 stsp
8647 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8648 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8649 2e1f37b0 2019-08-08 stsp if (err)
8650 2e1f37b0 2019-08-08 stsp goto done;
8651 2e1f37b0 2019-08-08 stsp
8652 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8653 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8654 2e1f37b0 2019-08-08 stsp if (err)
8655 2e1f37b0 2019-08-08 stsp goto done;
8656 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8657 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8658 2e1f37b0 2019-08-08 stsp if (err)
8659 2e1f37b0 2019-08-08 stsp goto done;
8660 2e1f37b0 2019-08-08 stsp
8661 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8662 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8663 2e1f37b0 2019-08-08 stsp goto done;
8664 2e1f37b0 2019-08-08 stsp }
8665 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8666 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8667 2e1f37b0 2019-08-08 stsp goto done;
8668 2e1f37b0 2019-08-08 stsp }
8669 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8670 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8671 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8672 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8673 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8674 fe621944 2020-11-10 stsp nchanges++;
8675 fe621944 2020-11-10 stsp }
8676 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8677 2e1f37b0 2019-08-08 stsp int choice;
8678 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8679 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8680 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8681 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8682 2e1f37b0 2019-08-08 stsp if (err)
8683 2e1f37b0 2019-08-08 stsp goto done;
8684 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8685 2e1f37b0 2019-08-08 stsp have_content = 1;
8686 19e4b907 2019-08-08 stsp else
8687 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8688 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8689 2e1f37b0 2019-08-08 stsp break;
8690 2e1f37b0 2019-08-08 stsp }
8691 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8692 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8693 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8694 2e1f37b0 2019-08-08 stsp done:
8695 2e1f37b0 2019-08-08 stsp free(label1);
8696 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8697 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8698 2e1f37b0 2019-08-08 stsp if (blob)
8699 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8700 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8701 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8702 2e1f37b0 2019-08-08 stsp if (staged_blob)
8703 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8704 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8705 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8706 fe621944 2020-11-10 stsp err = free_err;
8707 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8708 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8709 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8710 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8711 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8712 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8713 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8714 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8715 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8716 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8717 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8718 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8719 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8720 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8721 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8722 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8723 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8724 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8725 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8726 2e1f37b0 2019-08-08 stsp }
8727 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8728 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8729 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8730 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8731 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8732 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8733 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8734 2e1f37b0 2019-08-08 stsp }
8735 2e1f37b0 2019-08-08 stsp free(path1);
8736 2e1f37b0 2019-08-08 stsp free(path2);
8737 fda8017d 2020-07-23 stsp return err;
8738 fda8017d 2020-07-23 stsp }
8739 fda8017d 2020-07-23 stsp
8740 fda8017d 2020-07-23 stsp static const struct got_error *
8741 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8742 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8743 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8744 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8745 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8746 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8747 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8748 fda8017d 2020-07-23 stsp {
8749 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8750 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8751 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8752 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8753 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8754 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8755 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8756 fda8017d 2020-07-23 stsp struct stat sb;
8757 fda8017d 2020-07-23 stsp
8758 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8759 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8760 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8761 fda8017d 2020-07-23 stsp if (err)
8762 fda8017d 2020-07-23 stsp return err;
8763 fda8017d 2020-07-23 stsp
8764 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8765 fda8017d 2020-07-23 stsp return NULL;
8766 fda8017d 2020-07-23 stsp
8767 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8768 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8769 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8770 fda8017d 2020-07-23 stsp if (err)
8771 fda8017d 2020-07-23 stsp goto done;
8772 fda8017d 2020-07-23 stsp }
8773 fda8017d 2020-07-23 stsp
8774 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8775 fda8017d 2020-07-23 stsp if (f == NULL) {
8776 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8777 fda8017d 2020-07-23 stsp path_unstaged_content);
8778 fda8017d 2020-07-23 stsp goto done;
8779 fda8017d 2020-07-23 stsp }
8780 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8781 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8782 fda8017d 2020-07-23 stsp goto done;
8783 fda8017d 2020-07-23 stsp }
8784 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8785 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8786 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8787 fda8017d 2020-07-23 stsp size_t r;
8788 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8789 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8790 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8791 fda8017d 2020-07-23 stsp goto done;
8792 fda8017d 2020-07-23 stsp }
8793 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8794 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8795 fda8017d 2020-07-23 stsp goto done;
8796 fda8017d 2020-07-23 stsp }
8797 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8798 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8799 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8800 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8801 fda8017d 2020-07-23 stsp progress_arg);
8802 fda8017d 2020-07-23 stsp } else {
8803 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8804 67a66647 2021-05-31 stsp
8805 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8806 67a66647 2021-05-31 stsp if (err)
8807 67a66647 2021-05-31 stsp return err;
8808 67a66647 2021-05-31 stsp
8809 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8810 67a66647 2021-05-31 stsp parent) == -1) {
8811 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8812 67a66647 2021-05-31 stsp base_path = NULL;
8813 67a66647 2021-05-31 stsp goto done;
8814 67a66647 2021-05-31 stsp }
8815 67a66647 2021-05-31 stsp
8816 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8817 fc2a50f2 2022-11-01 thomas base_path, "");
8818 67a66647 2021-05-31 stsp if (err)
8819 67a66647 2021-05-31 stsp goto done;
8820 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8821 67a66647 2021-05-31 stsp blob_base);
8822 67a66647 2021-05-31 stsp if (err)
8823 67a66647 2021-05-31 stsp goto done;
8824 67a66647 2021-05-31 stsp
8825 b6b86fd1 2022-08-30 thomas /*
8826 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8827 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8828 eec2f5a9 2021-06-03 stsp */
8829 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8830 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8831 eec2f5a9 2021-06-03 stsp ondisk_path);
8832 eec2f5a9 2021-06-03 stsp if (err)
8833 eec2f5a9 2021-06-03 stsp goto done;
8834 eec2f5a9 2021-06-03 stsp } else {
8835 eec2f5a9 2021-06-03 stsp int fd;
8836 06340621 2021-12-31 thomas fd = open(ondisk_path,
8837 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8838 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8839 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8840 eec2f5a9 2021-06-03 stsp goto done;
8841 eec2f5a9 2021-06-03 stsp }
8842 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8843 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8844 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8845 eec2f5a9 2021-06-03 stsp close(fd);
8846 eec2f5a9 2021-06-03 stsp goto done;
8847 eec2f5a9 2021-06-03 stsp }
8848 eec2f5a9 2021-06-03 stsp }
8849 eec2f5a9 2021-06-03 stsp
8850 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8851 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8852 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8853 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8854 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8855 fda8017d 2020-07-23 stsp }
8856 fda8017d 2020-07-23 stsp if (err)
8857 fda8017d 2020-07-23 stsp goto done;
8858 fda8017d 2020-07-23 stsp
8859 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8860 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8861 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8862 2ac8aa02 2020-11-09 stsp } else {
8863 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8864 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8865 2ac8aa02 2020-11-09 stsp }
8866 fda8017d 2020-07-23 stsp done:
8867 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8868 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8869 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8870 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8871 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8872 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8873 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8874 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8875 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8876 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8877 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8878 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8879 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8880 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8881 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8882 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8883 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8884 67a66647 2021-05-31 stsp free(blob_base_path);
8885 67a66647 2021-05-31 stsp free(parent);
8886 67a66647 2021-05-31 stsp free(base_path);
8887 2e1f37b0 2019-08-08 stsp return err;
8888 2e1f37b0 2019-08-08 stsp }
8889 2e1f37b0 2019-08-08 stsp
8890 ad493afc 2019-08-03 stsp static const struct got_error *
8891 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8892 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8893 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8894 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8895 ad493afc 2019-08-03 stsp {
8896 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8897 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8898 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8899 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8900 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8901 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8902 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8903 9bc94a15 2019-08-03 stsp struct stat sb;
8904 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8905 ad493afc 2019-08-03 stsp
8906 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8907 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8908 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8909 2e1f37b0 2019-08-08 stsp return NULL;
8910 2e1f37b0 2019-08-08 stsp
8911 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8912 ad493afc 2019-08-03 stsp if (ie == NULL)
8913 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8914 9bc94a15 2019-08-03 stsp
8915 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8916 9bc94a15 2019-08-03 stsp == -1)
8917 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8918 ad493afc 2019-08-03 stsp
8919 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8920 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8921 f69721c3 2019-10-21 stsp if (err)
8922 f69721c3 2019-10-21 stsp goto done;
8923 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8924 f69721c3 2019-10-21 stsp id_str) == -1) {
8925 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8926 f4ae6ddb 2022-07-01 thomas goto done;
8927 f4ae6ddb 2022-07-01 thomas }
8928 f4ae6ddb 2022-07-01 thomas
8929 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8930 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8931 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8932 f4ae6ddb 2022-07-01 thomas goto done;
8933 f4ae6ddb 2022-07-01 thomas }
8934 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8935 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8936 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8937 f69721c3 2019-10-21 stsp goto done;
8938 f69721c3 2019-10-21 stsp }
8939 f69721c3 2019-10-21 stsp
8940 ad493afc 2019-08-03 stsp switch (staged_status) {
8941 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8942 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8943 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8944 ad493afc 2019-08-03 stsp if (err)
8945 ad493afc 2019-08-03 stsp break;
8946 ad493afc 2019-08-03 stsp /* fall through */
8947 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8948 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8949 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8950 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8951 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8952 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8953 2e1f37b0 2019-08-08 stsp if (err)
8954 2e1f37b0 2019-08-08 stsp break;
8955 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8956 2e1f37b0 2019-08-08 stsp break;
8957 2e1f37b0 2019-08-08 stsp } else {
8958 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8959 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8960 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8961 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8962 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8963 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8964 2e1f37b0 2019-08-08 stsp }
8965 2e1f37b0 2019-08-08 stsp }
8966 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8967 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
8968 ad493afc 2019-08-03 stsp if (err)
8969 ad493afc 2019-08-03 stsp break;
8970 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8971 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8972 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8973 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8974 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8975 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8976 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8977 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8978 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8979 ea7786be 2020-07-23 stsp break;
8980 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8981 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8982 36bf999c 2020-07-23 stsp char *staged_target;
8983 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8984 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8985 36bf999c 2020-07-23 stsp if (err)
8986 36bf999c 2020-07-23 stsp goto done;
8987 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8988 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8989 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8990 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8991 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8992 36bf999c 2020-07-23 stsp free(staged_target);
8993 dfe9fba0 2020-07-23 stsp } else {
8994 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8995 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8996 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8997 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8998 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8999 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9000 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9001 dfe9fba0 2020-07-23 stsp }
9002 ea7786be 2020-07-23 stsp break;
9003 ea7786be 2020-07-23 stsp default:
9004 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9005 ea7786be 2020-07-23 stsp break;
9006 ea7786be 2020-07-23 stsp }
9007 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9008 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9009 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9010 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9011 2ac8aa02 2020-11-09 stsp }
9012 ad493afc 2019-08-03 stsp break;
9013 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9014 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9015 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9016 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9017 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9018 2e1f37b0 2019-08-08 stsp if (err)
9019 2e1f37b0 2019-08-08 stsp break;
9020 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9021 2e1f37b0 2019-08-08 stsp break;
9022 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9023 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9024 2e1f37b0 2019-08-08 stsp break;
9025 2e1f37b0 2019-08-08 stsp }
9026 2e1f37b0 2019-08-08 stsp }
9027 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9028 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9029 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9030 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9031 9bc94a15 2019-08-03 stsp if (err)
9032 9bc94a15 2019-08-03 stsp break;
9033 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9034 ad493afc 2019-08-03 stsp break;
9035 ad493afc 2019-08-03 stsp }
9036 f69721c3 2019-10-21 stsp done:
9037 ad493afc 2019-08-03 stsp free(ondisk_path);
9038 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9039 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9040 ad493afc 2019-08-03 stsp if (blob_base)
9041 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9042 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9043 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9044 ad493afc 2019-08-03 stsp if (blob_staged)
9045 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9046 f69721c3 2019-10-21 stsp free(id_str);
9047 f69721c3 2019-10-21 stsp free(label_orig);
9048 ad493afc 2019-08-03 stsp return err;
9049 ad493afc 2019-08-03 stsp }
9050 ad493afc 2019-08-03 stsp
9051 ad493afc 2019-08-03 stsp const struct got_error *
9052 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9053 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9054 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9055 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9056 ad493afc 2019-08-03 stsp struct got_repository *repo)
9057 ad493afc 2019-08-03 stsp {
9058 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9059 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9060 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9061 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9062 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9063 ad493afc 2019-08-03 stsp
9064 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9065 ad493afc 2019-08-03 stsp if (err)
9066 ad493afc 2019-08-03 stsp return err;
9067 ad493afc 2019-08-03 stsp
9068 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9069 ad493afc 2019-08-03 stsp if (err)
9070 ad493afc 2019-08-03 stsp goto done;
9071 ad493afc 2019-08-03 stsp
9072 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9073 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9074 ad493afc 2019-08-03 stsp upa.repo = repo;
9075 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9076 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9077 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9078 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9079 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9080 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9081 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9082 ad493afc 2019-08-03 stsp if (err)
9083 ad493afc 2019-08-03 stsp goto done;
9084 ad493afc 2019-08-03 stsp }
9085 ad493afc 2019-08-03 stsp
9086 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9087 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9088 ad493afc 2019-08-03 stsp err = sync_err;
9089 ad493afc 2019-08-03 stsp done:
9090 ad493afc 2019-08-03 stsp free(fileindex_path);
9091 ad493afc 2019-08-03 stsp if (fileindex)
9092 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9093 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9094 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9095 ad493afc 2019-08-03 stsp err = unlockerr;
9096 ad493afc 2019-08-03 stsp return err;
9097 ad493afc 2019-08-03 stsp }
9098 b2118c49 2020-07-28 stsp
9099 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9100 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9101 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9102 b2118c49 2020-07-28 stsp void *info_arg;
9103 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9104 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9105 b2118c49 2020-07-28 stsp void *cancel_arg;
9106 b2118c49 2020-07-28 stsp };
9107 b2118c49 2020-07-28 stsp
9108 b2118c49 2020-07-28 stsp static const struct got_error *
9109 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9110 b2118c49 2020-07-28 stsp {
9111 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9112 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9113 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9114 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9115 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9116 b2118c49 2020-07-28 stsp int stage;
9117 b2118c49 2020-07-28 stsp
9118 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9119 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9120 b2118c49 2020-07-28 stsp
9121 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9122 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9123 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9124 b2118c49 2020-07-28 stsp break;
9125 b2118c49 2020-07-28 stsp }
9126 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9127 b2118c49 2020-07-28 stsp return NULL;
9128 b2118c49 2020-07-28 stsp
9129 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
9130 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9131 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
9132 b2118c49 2020-07-28 stsp }
9133 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9134 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9135 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9136 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9137 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
9138 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
9139 b2118c49 2020-07-28 stsp }
9140 b2118c49 2020-07-28 stsp
9141 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
9142 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9143 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
9144 b2118c49 2020-07-28 stsp }
9145 b2118c49 2020-07-28 stsp
9146 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9147 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9148 b2118c49 2020-07-28 stsp }
9149 b2118c49 2020-07-28 stsp
9150 b2118c49 2020-07-28 stsp const struct got_error *
9151 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9152 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9153 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9154 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9155 b2118c49 2020-07-28 stsp
9156 b2118c49 2020-07-28 stsp {
9157 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9158 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9159 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9160 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9161 b2118c49 2020-07-28 stsp
9162 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9163 b2118c49 2020-07-28 stsp if (err)
9164 b2118c49 2020-07-28 stsp return err;
9165 b2118c49 2020-07-28 stsp
9166 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9167 b2118c49 2020-07-28 stsp if (err)
9168 b2118c49 2020-07-28 stsp goto done;
9169 b2118c49 2020-07-28 stsp
9170 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9171 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9172 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9173 b2118c49 2020-07-28 stsp arg.paths = paths;
9174 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9175 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9176 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9177 b2118c49 2020-07-28 stsp &arg);
9178 b2118c49 2020-07-28 stsp done:
9179 b2118c49 2020-07-28 stsp free(fileindex_path);
9180 b2118c49 2020-07-28 stsp if (fileindex)
9181 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9182 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9183 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9184 b2118c49 2020-07-28 stsp err = unlockerr;
9185 b2118c49 2020-07-28 stsp return err;
9186 b2118c49 2020-07-28 stsp }
9187 814624e7 2022-03-22 thomas
9188 814624e7 2022-03-22 thomas static const struct got_error *
9189 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9190 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9191 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9192 814624e7 2022-03-22 thomas {
9193 814624e7 2022-03-22 thomas const struct got_error *err;
9194 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9195 814624e7 2022-03-22 thomas struct stat sb;
9196 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9197 814624e7 2022-03-22 thomas
9198 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9199 814624e7 2022-03-22 thomas if (err)
9200 814624e7 2022-03-22 thomas return err;
9201 814624e7 2022-03-22 thomas
9202 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9203 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9204 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9205 814624e7 2022-03-22 thomas
9206 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9207 814624e7 2022-03-22 thomas if (ie) {
9208 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9209 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9210 12de5570 2022-05-01 thomas repo);
9211 814624e7 2022-03-22 thomas if (err)
9212 814624e7 2022-03-22 thomas goto done;
9213 814624e7 2022-03-22 thomas } else {
9214 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9215 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9216 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9217 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9218 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9219 814624e7 2022-03-22 thomas ondisk_path);
9220 814624e7 2022-03-22 thomas goto done;
9221 814624e7 2022-03-22 thomas }
9222 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9223 814624e7 2022-03-22 thomas }
9224 814624e7 2022-03-22 thomas }
9225 814624e7 2022-03-22 thomas
9226 814624e7 2022-03-22 thomas done:
9227 814624e7 2022-03-22 thomas free(ondisk_path);
9228 814624e7 2022-03-22 thomas return err;
9229 814624e7 2022-03-22 thomas }
9230 814624e7 2022-03-22 thomas
9231 814624e7 2022-03-22 thomas static const struct got_error *
9232 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9233 814624e7 2022-03-22 thomas unsigned char staged_status)
9234 814624e7 2022-03-22 thomas {
9235 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9236 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9237 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9238 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9239 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9240 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9241 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9242 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9243 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 814624e7 2022-03-22 thomas return NULL;
9245 814624e7 2022-03-22 thomas }
9246 814624e7 2022-03-22 thomas
9247 814624e7 2022-03-22 thomas static const struct got_error *
9248 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9249 814624e7 2022-03-22 thomas {
9250 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9251 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9252 814624e7 2022-03-22 thomas return NULL;
9253 814624e7 2022-03-22 thomas }
9254 814624e7 2022-03-22 thomas
9255 814624e7 2022-03-22 thomas static const struct got_error *
9256 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9257 814624e7 2022-03-22 thomas unsigned char staged_status)
9258 814624e7 2022-03-22 thomas {
9259 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9260 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9261 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9262 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9263 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9264 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9265 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9266 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9267 814624e7 2022-03-22 thomas return NULL;
9268 814624e7 2022-03-22 thomas }
9269 814624e7 2022-03-22 thomas
9270 814624e7 2022-03-22 thomas const struct got_error *
9271 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9272 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9273 814624e7 2022-03-22 thomas {
9274 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9275 814624e7 2022-03-22 thomas }
9276 814624e7 2022-03-22 thomas
9277 814624e7 2022-03-22 thomas const struct got_error *
9278 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9279 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9280 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9281 814624e7 2022-03-22 thomas {
9282 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9283 814624e7 2022-03-22 thomas int file_renamed = 0;
9284 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9285 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9286 814624e7 2022-03-22 thomas
9287 814624e7 2022-03-22 thomas *oldpath = NULL;
9288 814624e7 2022-03-22 thomas *newpath = NULL;
9289 814624e7 2022-03-22 thomas
9290 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9291 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9292 814624e7 2022-03-22 thomas if (err)
9293 814624e7 2022-03-22 thomas goto done;
9294 814624e7 2022-03-22 thomas
9295 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9296 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9297 814624e7 2022-03-22 thomas if (err)
9298 814624e7 2022-03-22 thomas goto done;
9299 814624e7 2022-03-22 thomas
9300 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9301 814624e7 2022-03-22 thomas file_renamed = 1;
9302 814624e7 2022-03-22 thomas
9303 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9304 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9305 814624e7 2022-03-22 thomas else if (file_renamed) {
9306 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9307 814624e7 2022-03-22 thomas if (err == NULL)
9308 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9309 814624e7 2022-03-22 thomas } else if (old == NULL)
9310 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9311 814624e7 2022-03-22 thomas else
9312 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9313 814624e7 2022-03-22 thomas
9314 814624e7 2022-03-22 thomas done:
9315 814624e7 2022-03-22 thomas if (err) {
9316 814624e7 2022-03-22 thomas free(*oldpath);
9317 814624e7 2022-03-22 thomas *oldpath = NULL;
9318 814624e7 2022-03-22 thomas free(*newpath);
9319 814624e7 2022-03-22 thomas *newpath = NULL;
9320 814624e7 2022-03-22 thomas }
9321 814624e7 2022-03-22 thomas return err;
9322 814624e7 2022-03-22 thomas }
9323 814624e7 2022-03-22 thomas
9324 814624e7 2022-03-22 thomas const struct got_error *
9325 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9326 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9327 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9328 814624e7 2022-03-22 thomas {
9329 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9330 5e22b737 2022-05-31 thomas
9331 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9332 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9333 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9334 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9335 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9336 5e22b737 2022-05-31 thomas saa.repo = repo;
9337 5e22b737 2022-05-31 thomas
9338 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9339 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9340 814624e7 2022-03-22 thomas }
9341 5e22b737 2022-05-31 thomas
9342 5e22b737 2022-05-31 thomas const struct got_error *
9343 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9344 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9345 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9346 5e22b737 2022-05-31 thomas {
9347 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9348 5e22b737 2022-05-31 thomas
9349 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9350 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9351 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9352 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9353 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9354 5e22b737 2022-05-31 thomas sda.repo = repo;
9355 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9356 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9357 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9358 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9359 5e22b737 2022-05-31 thomas
9360 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9361 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9362 5e22b737 2022-05-31 thomas }
9363 5e22b737 2022-05-31 thomas
9364 5e22b737 2022-05-31 thomas const struct got_error *
9365 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9366 36832a8e 2022-05-31 thomas const char *fileindex_path)
9367 5e22b737 2022-05-31 thomas {
9368 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9369 5e22b737 2022-05-31 thomas
9370 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9371 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9372 5e22b737 2022-05-31 thomas
9373 5e22b737 2022-05-31 thomas return err;
9374 5e22b737 2022-05-31 thomas }